GSCats/terrain.s

358 lines
5.3 KiB
ArmAsm
Raw Normal View History

;
; terrain
;
; Created by Quinn Dunki on 7/29/17
;
2017-07-30 22:43:55 +00:00
TERRAINWIDTH = 640 ; In pixels
MAXTERRAINHEIGHT = 100 ; In pixels
COMPILEDTERRAINROW = TERRAINWIDTH/4+3 ; In words, +2 to make room for clipping jump at end of row
VISIBLETERRAINWIDTH = TERRAINWIDTH/4 ; In words- width minus jump return padding
VISIBLETERRAINWINDOW = 80 ; In words
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; renderTerrain
;
; No stack operations permitted here!
;
; Current implementation: X cycles per row
; Trashes all registers
2017-08-08 05:10:16 +00:00
;
renderTerrain:
FASTGRAPHICS
ldy #MAXTERRAINHEIGHT
lda #$9cff ; 4 Point stack to end of VRAM
tcs ; 2
sec
lda #compiledTerrainEnd-VISIBLETERRAINWINDOW-3
sbc mapScrollPos
sta PARAML0
renderTerrainLoop:
lda #$0000 ; Background
ldx #$1111 ; Foreground
jmp (PARAML0)
renderRowComplete:
lda PARAML0
sec
sbc #COMPILEDTERRAINROW
sta PARAML0
dey
bne renderTerrainLoop
SLOWGRAPHICS
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; craterTerrain
;
; PARAML0 = X pos of center in pixels from logical left terrain edge
; PARAML1 = Y pos of center in pixels from bottom terrain edge
; Y = Radius of circle, in pixels
;
; Trashes SCRATCHL
craterTerrain:
SAVE_AX
lda #TERRAINWIDTH ; Convert X pos to terrain-right byte count
sec
sbc PARAML0
sty SCRATCHL ; Center width
sbc SCRATCHL
2017-09-13 16:56:34 +00:00
and #$fffe ; Force even
clc
adc #terrainData
sta PARAML0
2017-09-13 16:56:34 +00:00
lda circleTable,y ; Look up circle data
sta SCRATCHL
2017-09-13 16:56:34 +00:00
tya ; Iterate over diameter
asl
tay
craterTerrainLoop:
dey
dey
bmi craterTerrainDone
lda (SCRATCHL),y ; Fetch circle Y value
clc
adc PARAML1 ; Convert to terrain-space
sta SCRATCHL2
lda (PARAML0),y
cmp SCRATCHL2
bmi craterTerrainLoop
lda SCRATCHL2 ; Circle value is lower, so use that
sta (PARAML0),y
bra craterTerrainLoop
craterTerrainDone:
lda #1
sta terrainDirty
RESTORE_AX
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; clipTerrain
;
;
clipTerrain:
SAVE_AXY
sec
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-3
sbc mapScrollPos
tay
ldx #MAXTERRAINHEIGHT
clipTerrainLoop:
clc ; Compute buffer to for saved data
txa
asl
asl
adc #clippedTerrainData-4
sta PARAML0
2017-07-30 19:16:19 +00:00
lda compiledTerrain,y
sta (PARAML0) ; Preserve data we're overwriting
inc PARAML0
inc PARAML0
2017-07-30 19:16:19 +00:00
and #$ff00
ora #$004c ; jmp in low byte
sta compiledTerrain,y
iny
lda compiledTerrain,y
sta (PARAML0) ; Preserve data we're overwriting
lda #renderRowComplete
sta compiledTerrain,y
tya
sec
sbc #COMPILEDTERRAINROW+1
tay
dex
bne clipTerrainLoop
RESTORE_AXY
rts
2017-08-06 20:26:31 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; unclipTerrain
2017-08-06 20:26:31 +00:00
;
;
unclipTerrain:
SAVE_AXY
2017-08-06 20:26:31 +00:00
sec
lda #COMPILEDTERRAINROW*MAXTERRAINHEIGHT-3
sbc mapScrollPos
tay
ldx #MAXTERRAINHEIGHT
2017-08-06 20:26:31 +00:00
unclipTerrainLoop:
clc ; Compute buffer that saved data is in
txa
asl
2017-08-06 20:26:31 +00:00
asl
adc #clippedTerrainData-4
sta PARAML0
lda (PARAML0)
sta compiledTerrain,y
inc PARAML0
inc PARAML0
iny
2017-08-06 20:26:31 +00:00
lda (PARAML0)
sta compiledTerrain,y
tya
sec
sbc #COMPILEDTERRAINROW+1
tay
dex
bne unclipTerrainLoop
RESTORE_AXY
rts
2017-08-06 20:26:31 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; compileTerrain
2017-08-06 20:26:31 +00:00
;
;
;
compileTerrain:
SAVE_AY
2017-08-06 20:26:31 +00:00
ldy #MAXTERRAINHEIGHT-1
lda #compiledTerrain
sta PARAML0
2017-08-06 20:26:31 +00:00
compileTerrainLoop:
sty PARAML1
jsr compileTerrainRow
dey
bmi compileTerrainDone
2017-08-06 20:26:31 +00:00
clc
lda #COMPILEDTERRAINROW
adc PARAML0
sta PARAML0
2017-08-06 20:26:31 +00:00
bra compileTerrainLoop
2017-08-06 20:26:31 +00:00
compileTerrainDone:
RESTORE_AY
rts
2017-08-06 20:26:31 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; compileTerrainRow
;
; PARAML0 = Start of compiled row data
; PARAML1 = Row index
;
2017-09-13 16:56:34 +00:00
; Note: DA = PHX = BGBG
; 48 = PHA = FGFG
; 5A = PHY =
; 0B = PHD =
compileTerrainRow:
SAVE_AXY
ldy #0
ldx #0
compileTerrainColumnLoop:
stz compileTerrainOpcode
; Right half
lda terrainData,x
cmp PARAML1
bcc compileTerrainColumnBGRight
beq compileTerrainColumnBGRight
lda #$00da
2017-09-13 16:56:34 +00:00
compileTerrainColumnLeft:
sta compileTerrainOpcode
inx
inx
2017-09-13 16:56:34 +00:00
inx ; Double-up for now
inx
lda terrainData,x
cmp PARAML1
bcc compileTerrainColumnBGLeft
beq compileTerrainColumnBGLeft
lda compileTerrainOpcode
ora #$da00
compileTerrainColumnStore:
sta (PARAML0),y
inx
inx
2017-09-13 16:56:34 +00:00
inx ; Double-up for now
inx
iny
iny
cpy #VISIBLETERRAINWIDTH
bne compileTerrainColumnLoop
2017-08-06 20:26:31 +00:00
RESTORE_AXY
2017-08-06 20:26:31 +00:00
rts
compileTerrainColumnBGRight:
lda #$0048
bra compileTerrainColumnLeft
compileTerrainColumnBGLeft:
lda compileTerrainOpcode
ora #$4800
bra compileTerrainColumnStore
compileTerrainOpcode:
2017-08-06 20:26:31 +00:00
.word 0
2017-08-13 21:56:23 +00:00
2017-08-06 20:26:31 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; generateTerrain
2017-08-06 20:26:31 +00:00
;
2017-08-13 04:25:36 +00:00
; Trashes everything
2017-08-06 20:26:31 +00:00
;
generateTerrain:
ldy #0
2017-08-13 04:25:36 +00:00
ldx #0
lda #terrainData
2017-08-06 20:26:31 +00:00
sta SCRATCHL
generateTerrainLoop:
2017-08-13 04:25:36 +00:00
lda sineTable,x
lsr
lsr
clc
adc #30
2017-08-06 20:26:31 +00:00
sta (SCRATCHL),y
iny
iny
2017-09-13 16:56:34 +00:00
sta (SCRATCHL),y ; Double-up for now
iny
iny
2017-08-13 04:25:36 +00:00
inx
inx
inx
inx
inx
inx
2017-08-13 04:25:36 +00:00
txa
and #$03ff
2017-08-13 04:25:36 +00:00
tax
2017-09-13 16:56:34 +00:00
cpy #TERRAINWIDTH
bne generateTerrainLoop
2017-08-06 20:26:31 +00:00
lda #1
sta terrainData
2017-08-06 20:26:31 +00:00
rts
2017-09-13 16:56:34 +00:00
; Terrain data, stored as height values 2 pixels wide (bytes)
terrainData:
2017-09-13 16:56:34 +00:00
.repeat TERRAINWIDTH/2
.word 0
.endrepeat
compiledTerrain:
.repeat COMPILEDTERRAINROW * MAXTERRAINHEIGHT
.byte 0
.endrepeat
compiledTerrainEnd:
clippedTerrainData:
.repeat MAXTERRAINHEIGHT
.byte 0,0,0,0 ; xx,jmp,addr
.endrepeat
2017-08-06 20:26:31 +00:00