Fixed stack-based clip/unclip

This commit is contained in:
blondie7575 2018-03-05 12:32:05 -08:00
parent 3292511ba8
commit 24269638fb
6 changed files with 73 additions and 53 deletions

View File

@ -39,12 +39,12 @@ lastCompiledTerrainY = $75 ; The highest Y value that the compiled renderer must
; Terrain constants ; Terrain constants
TERRAINWIDTH = 640 ; In pixels TERRAINWIDTH = 640 ; In pixels
MAXTERRAINHEIGHT = 100 ; In pixels MAXTERRAINHEIGHT = 100 ; In pixels
COMPILEDTERRAINROW = TERRAINWIDTH/4+3 ; In words, +3 to make room for clipping jump at end of row COMPILEDTERRAINROW = TERRAINWIDTH/4+4 ; In words, +4 to make room for clipping jump at end of row
VISIBLETERRAINWIDTH = TERRAINWIDTH/4 ; In words- width minus jump return padding VISIBLETERRAINWIDTH = TERRAINWIDTH/4 ; In words- width minus jump return padding
VISIBLETERRAINWINDOW = 80 ; In words VISIBLETERRAINWINDOW = 80 ; In words
MAXSPANSPERROW = 15 MAXSPANSPERROW = 15
SPANROWBYTES = MAXSPANSPERROW*2 + 2 ; In bytes SPANROWBYTES = MAXSPANSPERROW*2 + 2 ; In bytes
CLIPPEDTERRAINSTACK = $3fff ; Location of stack where clipped terrain data lives
; Terrain data, stored as height values 2 pixels wide (bytes) ; Terrain data, stored as height values 2 pixels wide (bytes)
terrainDataFar = $02f500 terrainDataFar = $02f500

View File

@ -99,12 +99,9 @@ gameplayLoopRender:
; ;
; Render the terrain if needed ; Render the terrain if needed
; lda terrainDirty lda terrainDirty
; beq gameplayLoopProjectiles beq gameplayLoopProjectiles
BORDER_COLOR #$3 BORDER_COLOR #$3
jsr unclipTerrain
jsl unrenderTerrainSpans
jsr clipTerrain
jsl renderTerrainSpans jsl renderTerrainSpans
jsr renderTerrain jsr renderTerrain

Binary file not shown.

View File

@ -30,7 +30,7 @@
.macro DBR bankNum .macro DBR bankNum
BITS8 BITS8
lda bankNum lda #bankNum
pha pha
plb plb
BITS16 BITS16
@ -109,7 +109,7 @@
.macro FASTGRAPHICS ;34 cycles, 12 bytes .macro FASTGRAPHICS ;34 cycles, 12 bytes
sei ;2 sei ;2
phd ;4 phd ;4
sep #%00100000 ; 3 16-bit A only, to preserve X/Y sep #%00100000 ; 3 8-bit A only, to preserve X/Y
.a8 .a8
lda STACKCTL ;5 lda STACKCTL ;5
@ -125,7 +125,7 @@
.macro SLOWGRAPHICS ;28 cycles, 12 bytes .macro SLOWGRAPHICS ;28 cycles, 12 bytes
sep #%00100000 ; 3 16-bit A only, to preserve X/Y sep #%00100000 ; 3 8-bit A only, to preserve X/Y
.a8 .a8
lda STACKREGISTER ;4 lda STACKREGISTER ;4
@ -177,6 +177,16 @@ nobrk:
pla pla
.endmacro .endmacro
.macro BREAK_NOSTACK
lda breakpoint
beq nobrk
lda #1
sta $e1c029
brk
nobrk:
.endmacro
.macro HARDBRK .macro HARDBRK
pha pha
lda #1 lda #1

View File

@ -20,7 +20,7 @@ renderTerrain:
tcs ; 2 tcs ; 2
sec sec
lda #compiledTerrainEnd-VISIBLETERRAINWINDOW-3 lda #compiledTerrainEnd-VISIBLETERRAINWINDOW-4
sbc mapScrollPos sbc mapScrollPos
sta PARAML0 sta PARAML0
@ -32,6 +32,7 @@ renderTerrainLoop:
jmp (PARAML0) jmp (PARAML0)
renderRowComplete: renderRowComplete:
lda PARAML0 lda PARAML0
sec sec
sbc #COMPILEDTERRAINROW sbc #COMPILEDTERRAINROW
@ -127,44 +128,43 @@ craterTerrainDone:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; clipTerrain ; clipTerrain
; ;
; Saves the compiled terrain data that we overwrite into
; a buffer at $E04000. We do this by shadowing the stack
; to that area and pushing.
; ;
clipTerrain: clipTerrain:
SAVE_AXY SAVE_AXY
; Shadow stack into $E04000 to save clipped data fast
tsc
sta STACKPTR
lda #CLIPPEDTERRAINSTACK
tcs
sec sec
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-3 lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-4
sbc mapScrollPos sbc mapScrollPos
tay tay
ldx #0 ldx #0
clipTerrainLoop: clipTerrainLoop:
clc ; Compute buffer to use for saved data
txa
asl
asl
adc #clippedTerrainData;-4
sta PARAML0
lda compiledTerrain,y lda compiledTerrain,y
sta (PARAML0) ; Preserve data we're overwriting pha
inc PARAML0 lda #$4cea ; NOP followed by JMP
inc PARAML0
and #$ff00
ora #$004c ; jmp in low byte
sta compiledTerrain,y sta compiledTerrain,y
iny
iny
iny
lda compiledTerrain,y lda compiledTerrain,y
sta (PARAML0) ; Preserve data we're overwriting pha
lda #renderRowComplete lda #renderRowComplete
sta compiledTerrain,y sta compiledTerrain,y
tya tya
sec sec
sbc #COMPILEDTERRAINROW+1 sbc #COMPILEDTERRAINROW+2
tay tay
inx inx
@ -172,6 +172,12 @@ clipTerrainLoop:
bcc clipTerrainLoop bcc clipTerrainLoop
beq clipTerrainLoop beq clipTerrainLoop
; Put stack back where it belongs
tsc
sta clippedTerrainStackPtr
lda STACKPTR
tcs
RESTORE_AXY RESTORE_AXY
rts rts
@ -179,42 +185,52 @@ clipTerrainLoop:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; unclipTerrain ; unclipTerrain
; ;
; Restores the compiled terrain data that we wrote
; to a buffer at $E04000. We do this by mapping the stack
; to $4000, then using stack-relative addressing to pull the data.
; We can't pop the data because it's all stored in reverse.
; ;
; On first move-left unclip every second row is unclipped incorrectly
unclipTerrain: unclipTerrain:
SAVE_AXY SAVE_AXY
phd
lda #(CLIPPEDTERRAINSTACK & $ff00)
pha
pld ; Point direct page at our clip data
sec sec
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-3 lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-4
sbc mapScrollPos sbc mapScrollPos
tay tay
ldx #0
lda clippedTerrainStackPtr
and #$00ff
tax
inx
unclipTerrainLoop: unclipTerrainLoop:
clc ; Compute buffer that saved data is in lda 2,x
txa
asl
asl
adc #clippedTerrainData;-4
sta PARAML0
lda (PARAML0)
sta compiledTerrain,y sta compiledTerrain,y
inc PARAML0 iny
inc PARAML0
iny iny
lda (PARAML0) lda 0,x
sta compiledTerrain,y sta compiledTerrain,y
tya tya
sec sec
sbc #COMPILEDTERRAINROW+1 sbc #COMPILEDTERRAINROW+2
tay tay
inx inx
cpx lastCompiledTerrainY inx
bcc unclipTerrainLoop inx
beq unclipTerrainLoop inx
cpx #$100 ; When x hits the top of the stack, we're done
bne unclipTerrainLoop
pld
RESTORE_AXY RESTORE_AXY
rts rts
@ -471,13 +487,10 @@ generateTerrainLoop:
compiledTerrain: compiledTerrain:
.repeat COMPILEDTERRAINROW * MAXTERRAINHEIGHT .repeat COMPILEDTERRAINROW * MAXTERRAINHEIGHT
.byte 0 .byte $00
.endrepeat .endrepeat
compiledTerrainEnd: compiledTerrainEnd:
clippedTerrainData: clippedTerrainStackPtr:
.repeat MAXTERRAINHEIGHT .word 0
.byte 0,0,0,0 ; xx,jmp,addr
.endrepeat

View File

@ -10,7 +10,7 @@
.include "equates.s" .include "equates.s"
.include "macros.s" .include "macros.s"
UNRENDERCACHESTACK = $7ff UNRENDERCACHESTACK = $7ff ; Stack downwards from bottom of VRAM. Mirrored from $01 into $E1!
OP16 OP16