Optimization

This commit is contained in:
blondie7575 2017-08-06 16:33:56 -07:00
parent fa19888d4d
commit af525a26fe
3 changed files with 87 additions and 21 deletions

Binary file not shown.

View File

@ -30,6 +30,7 @@ mainBank2:
jsr colorFill
jsr generateTerrain
jsr computeScrollClipBoundaries
mainGameLoop:
ldy mapScrollPos
@ -107,7 +108,7 @@ kbdScanSpace:
basePalette:
.word $0800,$0080,$0000,$000F,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
mapScrollPos: ; 4-pixel columns distance from right terrain edge
.word 80
.word 0
quitRequested:
.word $0000

105
terrain.s
View File

@ -13,6 +13,9 @@ MAXTERRAINHEIGHT = 100 ; In pixels
;
; No stack operations permitted here!
;
; Initial implementation: 535 cycles per row
; Current implementation: 443 cycles per row
;
renderTerrain:
lda #199*2
sta <ROWINDEX
@ -62,15 +65,14 @@ renderClippedSpanChain:
lda #renderClippedSpanChainRenderNext ; 2
sta renderSpanComplete+1 ; 4
; Find right edge of screen within span chains
; = 27 cycles per skipped span
; Look up right edge clipping data from precalculated table
; = 15 cycles per row
renderClippedSpanChainLoop:
lda spanChain,y ; 4
sec ; 2
sbc <RIGHTEDGE ; 3
bmi renderClippedSpanChainNextSpan ; 2/3
beq renderClippedSpanChainNextSpan ; 2/3
lda <MAPSCROLLPOS ; 3
asl ; 2
tax ; 2
ldy scrollClipIndices,x ; 4
lda scrollClipIndices+2,x ; 4
renderClippedSpanChainLoop2:
; Now render spans until left edge of screen
@ -91,7 +93,6 @@ renderSpanComplete:
; unrolled span rendering blocks
jmp renderClippedSpanChainRenderNext ; 3
renderClippedSpanChainRenderNext:
; Track remaining words until left edge
; = 26 cycles per span rendered
@ -108,16 +109,6 @@ renderClippedSpanChainRenderNext:
lda spanChain,y ; 5
bra renderClippedSpanChainLoop2 ; 3
renderClippedSpanChainNextSpan:
; Track remaining distance from right edge and
; continue searching for visible right edge
eor #$ffff ; 2
inc ; 2
sta <RIGHTEDGE ; 3
dey ; 2
dey ; 2
bra renderClippedSpanChainLoop ; 3
renderClippedSpanChainLastSpan:
; Render visible portion of last visible span
; = 26 cycles per row
@ -151,6 +142,75 @@ spanColors:
spanChainIndex:
.word 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; computeScrollClipBoundaries
;
; Generates a table of the clipped span indices for the right edge
; at every possible scroll position
;
computeScrollClipBoundaries:
SAVE_AXY
lda #0
sta <MAPSCROLLPOS
computeScrollClipBoundariesOuterLoop:
; Prepare our state
lda #80 ; 2
sta <XLEFT ; 3
ldy #spanChainEnd-spanChain-2 ; 2
lda <MAPSCROLLPOS ; 3
sta <RIGHTEDGE ; 3
; Find right edge of screen within span chains
computeScrollClipBoundariesInnerLoop:
lda spanChain,y ; 4
sec ; 2
sbc <RIGHTEDGE ; 3
bmi computeScrollClipBoundariesNextSpan ; 2/3
beq computeScrollClipBoundariesNextSpan ; 2/3
; Y now contains the pointer to span that will be clipped at this scroll position
; A now contains remaining words of clipped span to render
; Write these values into our tuple data structure
pha
ldx <MAPSCROLLPOS
txa
asl
tax
tya
sta scrollClipIndices,x
inx
inx
pla
sta scrollClipIndices,x
; On to the next scroll position
lda <MAPSCROLLPOS
inc
inc
cmp #(TERRAINWIDTH-320)/4+2
beq computeScrollClipBoundariesDone
sta <MAPSCROLLPOS
bra computeScrollClipBoundariesOuterLoop
computeScrollClipBoundariesNextSpan:
; Track remaining distance from right edge and
; continue searching for visible right edge
eor #$ffff ; 2
inc ; 2
sta <RIGHTEDGE ; 3
dey ; 2
dey ; 2
bra computeScrollClipBoundariesInnerLoop ; 3
computeScrollClipBoundariesDone:
RESTORE_AXY
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; generateTerrain
;
@ -186,9 +246,14 @@ generateTerrainLoop:
terrainData:
.repeat TERRAINWIDTH/2
.byte 0
.word 0
.endrepeat
scrollClipIndices:
; Tuples: (Clipped span, Words to Render)
.repeat (TERRAINWIDTH-320)/4+2 ; Always scroll by two words
.word 0
.endrepeat
.if 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;