Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created February 7, 2019 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuro-sys/5036bd4409f877eca4c434eab9eebeb1 to your computer and use it in GitHub Desktop.
Save neuro-sys/5036bd4409f877eca4c434eab9eebeb1 to your computer and use it in GitHub Desktop.
org &4000
run &4000
di
im 1
; set ei, ret to &38 interrupt
ld a, &fb
ld (&38), a
ld a, &c9
ld (&39), a
ei
;; disable roms set mode 1
ld bc, &7f8d
out (c), c
;; clear both screen banks
ld hl, &c000
ld de, &8000
xor a
.clear_screen
ld (hl), a
ld (de), a
inc hl
inc de
cp h
jp nz, clear_screen
;; generate screen address table from 0x0000 to 0x4000
ld de, scr_addr_table
call GenScrAddrs
ld hl, dir_x
ld (hl), 1
ld hl, dir_y
ld (hl), 1
ld hl, screen_offset
ld (hl), &c0
loop:
;; wait until vsync
ld b, &f5
in a, (c)
rra
jp nc, $-3
;; set border color
ld bc, &7f10
out (c), c
ld c, &56
out (c), c
ld hl, pos_x
ld a, (hl)
ld hl, prev_x
ld (hl), a
ld hl, pos_y
ld a, (hl)
ld hl, prev_y
ld (hl), a
ld hl, pos_x
ld b, (hl)
ld hl, pos_y
ld c, (hl)
exx
ld b, 6
ld c, 32
exx
; call FillRect
ld hl, pos_x
ld a, (hl)
ld hl, dir_x
ld b ,(hl)
add b
ld hl, pos_x
ld (hl), a
ld b, 80-6
cp b
jp nz, skip_change_dir_x
ld hl, dir_x
ld (hl), -1
jp done_change_dir_x
.skip_change_dir_x
or a
jp nz, done_change_dir_x
ld hl, dir_x
ld (hl), 1
.done_change_dir_x
ld hl, pos_y
ld a, (hl)
ld hl, dir_y
ld b ,(hl)
add b
ld hl, pos_y
ld (hl), a
ld b, 200-32
cp b
jp nz, skip_change_dir_y
ld hl, dir_y
ld (hl), -1
jp done_change_dir_y
.skip_change_dir_y
or a
jp nz, done_change_dir_y
ld hl, dir_y
ld (hl), 1
.done_change_dir_y
ld hl, CurSprite
ld de, Sprite1
ld (hl), e
inc hl
ld (hl), d
ld hl, pos_x
ld b, (hl)
ld hl, pos_y
ld c, (hl)
exx
ld b, 6
ld c, 32
exx
call PutSprite
ld bc, &7f10
out (c), c
ld c, &54
out (c), c
ld bc, &bc00+12
out (c), c
inc b
ld hl, screen_offset
ld a, (hl)
cp &c0
jr z, screen_offset_set_80
ld a, &20
jr screen_offset_done
.screen_offset_set_80
ld a, &30
ld c, a
.screen_offset_done
;out (c), a
;ld (hl), a
jp loop
delay:
dec a
jp nz, delay
ret
stack:
ds 2
;; x, y, w, h
;; B: x
;; C: y
;; B': W
;; C'; H
;; CurSprite
PutSprite:
ld l, c
ld h, 0 ; hl = Y
add hl, hl ; multiply by 2
ld de, scr_addr_table
add hl, de ; add screen lookup
ld a, (hl)
inc hl
ld h, (hl)
ld l, a ; hl = screen y addr
ld e, b ; de = Screen x
ld d, 0
add hl, de
ex de, hl ; de = screen addr
exx
ld a, b
exx
ld c, a ; bc = Sprite width
ld b, 0
ld hl, CurSprite
ld a, (hl)
inc hl
ld h, (hl)
ld l, a ; hl = Sprite Address
exx
ld l, c
ld h, 0 ; hl' = Sprite height
exx
.PutSprite_L1
push hl
push de
push bc
exx
ld de, screen_offset
ld a, (de)
exx
ex de, hl
add h
ld h, a ; hl = &c000 + screen addr
ex de, hl ; de = hl
ldir
pop bc
pop de
pop hl
ex de, hl
push bc
push de
exx
ld de, screen_offset
ld a, (de)
exx
ld d, a
ld a, h
sub d
ld h, a
call GetNextLine ; hl = next screen line
exx
ld a, (de)
exx
add h
ld h, a
pop de
pop bc
ex de, hl
add hl, bc ; hl = next sprite line
exx
dec l
ret z
exx
jr PutSprite_L1
ret
CurSprite:
ds 2
; x, y, w, h
FillRect:
ld l, c
ld h, 0 ; hl = Y
add hl, hl ; multiply by 2
ld de, scr_addr_table
add hl, de ; add screen lookup
ld a, (hl)
inc hl
ld h, (hl)
ld l, a ; hl = screen y addr
ld e, b ; de = Screen x
ld d, 0
add hl, de ; hl = pixel addr
exx
ld a, b
exx
ld c, a ; bc = Sprite width
ld b, 0
exx
ld a, c
exx
ld e, a
ld d, 0 ; de = Sprite height
.FillRect_L1
push hl
push de
push bc
exx
ld de, screen_offset
ld a, (de)
exx
add h
ld h, a ; hl = &c000 + screen addr
.FillRect_L2
ld a, 0
ld (hl), a
inc hl
dec bc
xor b
cp c
jr nz, FillRect_L2
cp b
jr nz, FillRect_L2
pop bc
pop de
pop hl
push bc
push de
exx
ld de, screen_offset
ld a, (de)
exx
ld d, a
call GetNextLine ; hl = next screen line
pop de
pop bc
dec e
jr nz, FillRect_L1
ret
;; HL current address
;; D screen offset
GetNextLine:
ld a, h
add &08 ; add next line
ld h, a
bit 6, h ; check overflow
ret nz
ld e, &50 ; add offset
add hl, de
ret
;; DE Sprite Address
;; BC Sprite size
FillSprite:
ld a, c
ld (de), a
inc de
dec bc
ld l, 0
ld h, l
or a
sbc hl, bc
add hl, bc
jr nz, FillSprite
ret
;; DE memory addr for LUT
GenScrAddrs:
ld hl, 0
exx
ld c, 199
exx
.GenScrAddrLoop
ld a, h
add &08
ld h, a
bit 6, h
jr z, GenScrAddrSkipRow
res 6, h
ld bc, &0050
add hl, bc
.GenScrAddrSkipRow
exx
dec c
ret z
exx
ld a, l
ld (de), a
inc de
ld a, h
ld (de), a
inc de
jr GenScrAddrLoop
pos_x: ds 1
pos_y: ds 1
prev_x: ds 1
prev_y: ds 1
dir_x: ds 1
dir_y: ds 1
screen_offset: ds 1
Sprite1:
db &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00
db &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00
db &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00
db &00, &00, &00, &00, &00, &00, &00, &00, &03, &0e, &00, &00, &00, &01, &0f, &0f
db &08, &00, &00, &03, &0e, &0f, &0c, &00, &00, &01, &00, &01, &0c, &00, &00, &00
db &00, &01, &0c, &00, &00, &00, &00, &00, &0e, &00, &00, &00, &03, &0f, &0e, &00
db &00, &00, &0f, &0f, &0e, &00, &00, &03, &0c, &00, &0e, &00, &00, &03, &08, &00
db &0e, &00, &00, &07, &00, &00, &0e, &00, &00, &07, &00, &01, &0e, &00, &00, &07
db &08, &07, &0e, &00, &00, &03, &0f, &0e, &0e, &00, &00, &01, &0f, &08, &0e, &00
db &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00
db &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00
db &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00
Sprite1_Size equ $-Sprite1
align 2
scr_addr_table:
ds 200*2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment