tile_width_bytes equ 8/2
tile_height equ 16
tile_data_size equ tile_width_bytes*tile_height
num_tiles equ 4
scr_width_pixels equ 160
scr_height_pixels equ 200
scr_width_bytes equ scr_width_pixels/2
tilemap_width_tiles equ scr_width_bytes/tile_width_bytes
tilemap_height_tiles equ scr_height_pixels/tile_height
tilemap_dir_left equ 0
tilemap_dir_right equ tilemap_dir_left+1
tilemap_dir_up equ tilemap_dir_right+1
tilemap_dir_down equ tilemap_dir_up+1
tilemap_num_dirs equ tilemap_dir_down+1
tilemap_dir_invalid equ &ff
size_tilemap_ptrs equ end_tilemap_ptrs - tilemap_ptrs
num_tilemaps equ size_tilemap_ptrs/2
scr_next_line equ &bc26
scr_set_mode equ &bc0e
scr_set_border equ &bc38
scr_set_ink equ &bc32
mc_wait_flyback equ &bd19
km_test_key equ &bb1e
org &1000
nolist
start:
ld a,0
call scr_set_mode
ld bc,0
call scr_set_border
ld hl,pens
ld b,16
xor a
set_pens
push bc
push af
ld c,(hl)
ld b,c
inc hl
push hl
call scr_set_ink
pop hl
pop af
pop bc
inc a
djnz set_pens
call make_scr_table
call make_tile_ptr_table
xor a
ld (tilemap_cur),a
ld a,1
ld (tilemap_changed),a
main_loop:
call mc_wait_flyback
call check_keys
ld a,(tilemap_changed)
or a
jp z,main_loop
xor a
ld (tilemap_changed),a
call tilemap_draw
jp main_loop
check_keys:
ld a,0
call km_test_key
jp nz,tilemap_up
ld a,2
call km_test_key
jp nz,tilemap_down
ld a,8
call km_test_key
jp nz,tilemap_left
ld a,1
call km_test_key
jp nz,tilemap_right
ret
get_tilemap_dirs:
ld a,(tilemap_cur)
ld l,a
ld h,0
add hl,hl
add hl,hl
ld de,tilemap_dirs
add hl,de
push hl
pop ix
ret
tilemap_left:
call get_tilemap_dirs
ld a,(ix+tilemap_dir_left)
cp tilemap_dir_invalid
ret z
ld (tilemap_cur),a
ld a,1
ld (tilemap_changed),a
ret
tilemap_right:
call get_tilemap_dirs
ld a,(ix+tilemap_dir_right)
cp tilemap_dir_invalid
ret z
ld (tilemap_cur),a
ld a,1
ld (tilemap_changed),a
ret
tilemap_up:
call get_tilemap_dirs
ld a,(ix+tilemap_dir_up)
cp tilemap_dir_invalid
ret z
ld (tilemap_cur),a
ld a,1
ld (tilemap_changed),a
ret
tilemap_down:
call get_tilemap_dirs
ld a,(ix+tilemap_dir_down)
cp tilemap_dir_invalid
ret z
ld (tilemap_cur),a
ld a,1
ld (tilemap_changed),a
ret
tilemap_draw:
ld a,(tilemap_cur)
ld l,a
ld h,0
add hl,hl
ld de,tilemap_ptrs
add hl,de
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
push hl
pop ix
ld h,0
ld l,0
ld b,tilemap_height_tiles
ld c,tilemap_width_tiles
tilemap_draw_y:
push hl
push bc
call get_scr_addr
tilemap_draw_x:
ld a,(ix+0)
inc ix
push bc
push hl
call get_tile_ptr
call tile_draw
pop hl
ld bc,tile_width_bytes
add hl,bc
pop bc
dec c
jr nz,tilemap_draw_x
pop bc
pop hl
ld a,l
add a,tile_height
ld l,a
djnz tilemap_draw_y
ret
get_tile_ptr:
push hl
ld l,a
ld h,0
add hl,hl
ld de,tile_ptr_table
add hl,de
ld e,(hl)
inc hl
ld d,(hl)
pop hl
ret
get_scr_addr:
push bc
push de
ld c,h
ld h,0
add hl,hl
ld de,scr_addr_table
add hl,de
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
ld b,0
add hl,bc
pop de
pop bc
ret
make_scr_table:
ld ix,scr_addr_table
ld hl,&c000
ld b,200
mst1:
ld (ix+0),l
ld (ix+1),h
inc ix
inc ix
push bc
call scr_next_line
pop bc
djnz mst1
ret
make_tile_ptr_table:
ld ix,tile_ptr_table
ld hl,tile_gfx
ld de,tile_data_size
ld b,num_tiles
mtpt1:
ld (ix+0),l
ld (ix+1),h
inc ix
inc ix
add hl,de
djnz mtpt1
ret
tile_draw:
ld b,tile_height
ld c,tile_width_bytes
tile_draw_height:
push bc
push hl
tile_draw_width:
ld a,(de)
ld (hl),a
inc hl
inc de
dec c
jr nz,tile_draw_width
pop hl
call scr_next_line
pop bc
djnz tile_draw_height
ret
scr_addr_table:
defs 200*2
pens:
defb &00
defb &01
defb &01
defb &01
defb &02
defb &02
defb &06
defb &06
defb &0c
defb &0a
defb &0a
defb &0e
defb &18
defb &18
defb &1a
defb &1a
tilemap_cur:
defb 0
tilemap_changed:
defb 0
tilemap_dirs:
defb 1
defb 2
defb 3
defb 4
defb tilemap_dir_invalid
defb 0
defb tilemap_dir_invalid
defb tilemap_dir_invalid
defb 0
defb tilemap_dir_invalid
defb tilemap_dir_invalid
defb tilemap_dir_invalid
defb tilemap_dir_invalid
defb tilemap_dir_invalid
defb tilemap_dir_invalid
defb 0
defb tilemap_dir_invalid
defb tilemap_dir_invalid
defb 0
defb tilemap_dir_invalid
tilemap_ptrs:
defw tilemap0
defw tilemap1
defw tilemap2
defw tilemap3
defw tilemap4
end_tilemap_ptrs:
tile_ptr_table:
defs num_tiles*2
tilemap0:
defb 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0
defb 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0
defb 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0
defb 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0
defb 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0
defb 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
defb 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
defb 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
defb 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0
defb 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
defb 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
tile_gfx:
defb &00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00
defb &00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00
defb &00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00
defb &00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00,&00
defb &8c,&0c,&0c,&4c,&9a,&30,&30,&a4,&9a,&30,&30,&a4,&9a,&30,&30,&a4
defb &da,&4b,&87,&a4,&9a,&f0,&f0,&24,&da,&4b,&87,&a4,&9a,&f0,&f0,&24
defb &da,&4b,&87,&a4,&9a,&f0,&f0,&24,&da,&4b,&87,&a4,&9a,&f0,&f0,&24
defb &da,&4b,&87,&a4,&9a,&30,&30,&a4,&9a,&30,&30,&a4,&cf,&cf,&cf,&ca
defb &03,&03,&03,&57,&53,&f3,&f3,&f7,&53,&33,&33,&77,&53,&33,&33,&77
defb &53,&b3,&73,&77,&53,&51,&a2,&f7,&53,&b3,&73,&77,&53,&33,&33,&77
defb &53,&b3,&73,&77,&53,&51,&a2,&f7,&53,&a2,&51,&77,&53,&73,&b3,&77
defb &53,&33,&33,&77,&53,&33,&33,&77,&53,&33,&33,&77,&bf,&3f,&3f,&7f
defb &3c,&3c,&a8,&bc,&3c,&3c,&a8,&bc,&fc,&fc,&a8,&fc,&00,&00,&00,&00
defb &fc,&a8,&fc,&fc,&3c,&a8,&bc,&3c,&fc,&a8,&fc,&fc,&00,&00,&00,&00
defb &54,&fc,&fc,&54,&54,&3c,&7c,&54,&54,&fc,&fc,&54,&00,&00,&00,&00
defb &fc,&a8,&fc,&fc,&3c,&a8,&bc,&3c,&fc,&a8,&fc,&fc,&00,&00,&00,&00