keen: make more things use tilemap lookup

only saves handful of cycles it seems
This commit is contained in:
Vince Weaver 2024-04-14 22:35:37 -04:00
parent ed5053bb1e
commit 0ec8efc1be
3 changed files with 69 additions and 51 deletions

View File

@ -69,7 +69,7 @@ level1.o: level1.s zp.inc hardware.inc \
graphics/keen_graphics.inc sprites/keen_sprites.inc \ graphics/keen_graphics.inc sprites/keen_sprites.inc \
maps/level1_map.lzsa \ maps/level1_map.lzsa \
status_bar.s draw_keen.s move_keen.s gr_putsprite_crop.s \ status_bar.s draw_keen.s move_keen.s gr_putsprite_crop.s \
draw_tilemap.s \ draw_tilemap.s tilemap_lookup.s \
level1_sfx.s longer_sound.s \ level1_sfx.s longer_sound.s \
keyboard.s handle_laser.s keyboard.s handle_laser.s
ca65 -o level1.o level1.s -l level1.lst ca65 -o level1.o level1.s -l level1.lst

View File

@ -4,62 +4,64 @@
; tilemap is 20x12 grid with 2x4 (well, 2x2) tiles ; tilemap is 20x12 grid with 2x4 (well, 2x2) tiles
draw_tilemap: draw_tilemap:
ldy #0 ; current screen Ypos to draw at ldx #0 ; offset in current tilemap ; 2
sty TILEY ; (we draw two at a time as lores stx TILEMAP_OFFSET ; ; 3
; is two blocks per byte)
ldx #0 ; offset in current tilemap ; ldy #0 ; current screen Ypos to draw at ; 2
stx TILEMAP_OFFSET ; stx TILEY ; (we draw two at a time as lores ; 3
; is two blocks per byte)
lda #0 ; init odd/even
sta TILE_ODD ; (tiles are two rows tall)
; lda #0 ; init odd/even ; 2
stx TILE_ODD ; (tiles are two rows tall) ; 3
tilemap_outer_loop: tilemap_outer_loop:
ldy TILEY ; setup output pointer to current Ypos ldy TILEY ; setup output pointer to current Ypos ; 3
lda gr_offsets,Y ; get address of start of row lda gr_offsets,Y ; get address of start of row ; 4+
sta GBASL sta GBASL ; 3
lda gr_offsets+1,Y lda gr_offsets+1,Y ; 4+
clc clc ; 2
adc DRAW_PAGE ; adjust for page adc DRAW_PAGE ; adjust for page ; 3
sta GBASH sta GBASH ; 3
ldy #0 ; draw row from 0..39 ldy #0 ; draw row from 0..39 ; 2
; might be faster to count backwards ; might be faster to count backwards
; but would have to adjust a lot ; but would have to adjust a lot
tilemap_loop: tilemap_loop:
ldx TILEMAP_OFFSET ; get actual tile number ldx TILEMAP_OFFSET ; get actual tile number ; 3
lda small_tilemap,X ; from tilemap lda small_tilemap,X ; from tilemap ; 4
asl ; *4 ; point to tile to draw (4 bytes each) asl ; *4 ; point to tile to draw (4 bytes each) ; 2
asl asl ; 2
tax tax ; 2
lda TILE_ODD ; check to see if top or bottom lda TILE_ODD ; check to see if top or bottom ; 3
beq not_odd_line beq not_odd_line ; 2/3
inx ; point to bottom half of tile inx ; point to bottom half of tile ; 2
inx inx ; 2
not_odd_line: not_odd_line:
; draw two blocks ; draw two blocks
; note we don't handle transparency in the keen engine ; note we don't handle transparency in the keen engine
lda tiles,X lda tiles,X ; 4
sta (GBASL),Y ; draw upper right sta (GBASL),Y ; draw upper right ; 6
iny iny ; 2
lda tiles+1,X lda tiles+1,X ; 4
sta (GBASL),Y ; draw upper left sta (GBASL),Y ; draw upper left ; 6
iny iny ; 2
inc TILEMAP_OFFSET ; point to next tile inc TILEMAP_OFFSET ; point to next tile ; 5
cpy #40 ; until done cpy #40 ; until done ; 2
bne tilemap_loop bne tilemap_loop ; 2/3
@ -126,22 +128,35 @@ copy_tilemap_subset:
lda #0 lda #0
sta tilemap_count_smc+1 sta tilemap_count_smc+1
; original worse case: 23 cycles
; lookup table: 19 cycles
; set start ; set start
lda TILEMAP_Y
lsr ldx TILEMAP_Y ; 3
lda tilemap_lookup_high,X ; 4
sta cptl1_smc+2 ; set proper row in big tilemap ; 4
lda tilemap_lookup_low,X ; 4
sta cptl1_smc+1 ; set proper row in big tilemap ; 4
; set start
; lda TILEMAP_Y ; 3
; lsr ; 2
; set odd/even ; set odd/even
ldx #0 ; ldx #0 ; 2
bcc skip_odd_row ; bcc skip_odd_row ; 2/3
ldx #$80 ; ldx #$80 ; 2
skip_odd_row: ;skip_odd_row:
stx cptl1_smc+1 ; stx cptl1_smc+1 ; 4
clc ; set start ; clc ; set start ; 2
adc #>big_tilemap ; each even row is a page, so adding ; adc #>big_tilemap ; each even row is a page, so adding ; 2
; Y to top byte is indexing to row ; Y to top byte is indexing to row
; sta cptl1_smc+2 ; set proper row in big tilemap ; 4
sta cptl1_smc+2 ; set proper row in big tilemap
lda #<small_tilemap lda #<small_tilemap
@ -153,14 +168,15 @@ cp_tilemap_outer_loop:
ldy #0 ldy #0
cp_tilemap_inner_loop: cp_tilemap_inner_loop:
; FIXME: make cptl1 take into account X offset and use one index?
; TODO: optimize, totally unroll? ; TODO: optimize, totally unroll?
cptl1_smc: cptl1_smc:
lda $9400,X lda $9400,X ; 4
cptl2_smc: cptl2_smc:
sta $BC00,Y sta $BC00,Y ; 5
iny iny ; 2
inx inx ; 2
cpy #TILEMAP_X_COPY_SIZE cpy #TILEMAP_X_COPY_SIZE
bne cp_tilemap_inner_loop bne cp_tilemap_inner_loop

View File

@ -302,5 +302,7 @@ level1_levelover:
.include "random16.s" .include "random16.s"
.include "tilemap_lookup.s"
level1_data_zx02: level1_data_zx02:
.incbin "maps/level1_map.zx02" .incbin "maps/level1_map.zx02"