; AtariWars - DLI raster colors and Player/Missile sprite overlays.
; ASCII sketches below are schematic; bitmap units remain visible without PM.

        icl	"src/graphics.i"

;************************************************
;* initGraphics
;*
;* Input : OS immediate VBI vector; initial PM buffers
;* Output: PM/raster setup and chained immediate VBI installed once
;* Trash: a,x,y,flags; GTIA/shadows and gfx state
;*
;* Bitmap battlefield ----+
;* P0..P3: unit lights ---+--> GTIA --> visible picture
;* M0..M3: tile brackets -+
;* VBI --> bank/page handoff; DLI --> colors + player X positions
;* Three PM banks start hidden; first complete build is queued later.
;************************************************

initGraphics

        lda	#0
        sta	gfx.frontBank
        sta	gfx.activeOffset
        sta	gfx.band
        sta	gfx.hudFront
        ldx	#3

?graphicsHide
        sta	HPOSP0,x
        sta	HPOSM0,x
        sta	SIZEP0,x
        dex
        bpl	?graphicsHide

        sta	SIZEM
        sta	VDELAY
        lda	#NONE
        sta	gfx.pendingBank
        sta	gfx.hudPending
        lda	#>PM_RAM
        sta	PMBASE
        lda	#$11
        sta	GPRIOR
        lda	#3
        sta	GRACTL
        lda	#SPRITE_LIGHT
        sta	PCOLR0
        sta	PCOLR1
        sta	PCOLR2
        sta	PCOLR3
        lda	#SPRITE_CURSOR
        sta	COLOR3
        lda	#<rasterDli
        sta	VDSLSTL
        lda	#>rasterDli
        sta	VDSLSTH
        lda	VVBLKIL
        sta	graphicsOldVbi
        lda	VVBLKIH
        sta	graphicsOldVbi+1
        lda	#6
        ldx	#>graphicsVbi
        ldy	#<graphicsVbi
        jsr	SETVBV

        rts

;************************************************
;* buildSprites
;*
;* Input : units, cursor/selection, battle state and PM bank ownership
;* Output: complete spare PM bank published in gfx.pendingBank
;* Trash: a,x,y,flags,pmWrite; gfx scratch and spare bank tables
;*
;* Re-reading frontBank protects against a concurrent VBI handoff.
;* Neither the visible bank nor the queued bank is overwritten.
;*
;* [front: visible]  [pending: queued]  [spare: safe to clear/build]
;*                                        |
;*                unit lights + brackets -+
;*                                        v
;*                                  [pending] --VBI--> [front]
;* Selected unit gets first light slot; other units follow.
;* Title/briefing/battle: publish a cleared overlay, not map brackets.
;************************************************

buildSprites

?graphicsSnapshot
        lda	gfx.frontBank
        sta	gfx.snapshotFront
        lda	gfx.pendingBank
        sta	gfx.snapshotPending
        lda	gfx.frontBank
        cmp	gfx.snapshotFront
        bne	?graphicsSnapshot

        ldx	#0

?graphicsChooseBank
        cpx	gfx.snapshotFront
        beq	?graphicsNextBank

        cpx	gfx.snapshotPending
        bne	?graphicsBankReady

?graphicsNextBank
        inx
        jmp	?graphicsChooseBank

?graphicsBankReady
        stx	gfx.buildBank
        lda	pmOffsets,x
        sta	gfx.buildOffset
        tay
        ldx	#RASTER_BANDS
        lda	#0

?graphicsClearPositions
        sta	pmHpos0,y
        sta	pmHpos1,y
        sta	pmHpos2,y
        sta	pmHpos3,y
        iny
        dex
        bne	?graphicsClearPositions

        ldx	#MAP_HEIGHT-1

?graphicsClearRows
        sta	gfx.rowUsed,x
        dex
        bpl	?graphicsClearRows

        sta	gfx.dropped
        sta	pmWrite
        ldx	gfx.buildBank
        lda	pmBaseHigh,x
        clc
        adc	#3
        sta	pmWrite+1
        lda	#5
        sta	gfx.clearPages
        lda	#0
        ldy	#0

?graphicsClearPage
        sta	(pmWrite),y
        iny
        bne	?graphicsClearPage

        inc	pmWrite+1
        dec	gfx.clearPages
        bne	?graphicsClearPage

        lda	app.screen
        bne	?graphicsClearMissiles

        lda	battle.active
        bne	?graphicsClearMissiles

        ldx	game.selected
        cpx	#NONE
        beq	?graphicsOtherUnits

        jsr	addUnitLight

?graphicsOtherUnits
        lda	#0
        sta	gfx.unit

?graphicsUnitLoop
        ldx	gfx.unit
        cpx	game.selected
        beq	?graphicsUnitNext

        jsr	addUnitLight

?graphicsUnitNext
        inc	gfx.unit
        lda	gfx.unit
        cmp	app.unitCount
        bne	?graphicsUnitLoop

?graphicsClearMissiles
        ldx	gfx.buildBank
        txa
        asl
        asl
        tax
        lda	#0
        sta	missileHpos,x
        sta	missileHpos+1,x
        sta	missileHpos+2,x
        sta	missileHpos+3,x
        sta	gfx.slot
        lda	app.screen
        bne	?graphicsPublish

        lda	battle.active
        bne	?graphicsPublish

        lda	game.cursorX
        sta	gfx.tileX
        lda	game.cursorY
        sta	gfx.tileY
        jsr	addBracket

        ldx	game.selected
        cpx	#NONE
        beq	?graphicsPublish

        lda	#1
        sta	gfx.slot
        lda	units.x,x
        sta	gfx.tileX
        lda	units.y,x
        sta	gfx.tileY
        jsr	addBracket

?graphicsPublish
        lda	gfx.buildBank
        sta	gfx.pendingBank
        rts

;************************************************
;* addUnitLight
;*
;* Input : X = unit index; gfx.buildBank/buildOffset and row slots
;* Output: highlight/HPOS added, or gfx.dropped incremented on overflow
;* Trash: a,x,y,flags,pmWrite; gfx scratch
;*
;* One tile row (16 scanlines):   [P0] [P1] [P2] [P3]  [extra]
;* Light slot:                    yes  yes  yes  yes   bitmap only
;* Each 8-bit x 16-line mask:     00 / light bits / 00
;*                               ^ two blank rows at each end
;* HPOS table stores X at band row*4; DLI reuses players next row.
;************************************************

addUnitLight

        lda	units.hp,x
        bne	?graphicsLightAlive

        rts

?graphicsLightAlive
        stx	gfx.lightUnit
        ldy	units.y,x
        sty	gfx.row
        lda	gfx.rowUsed,y
        cmp	#4
        bcc	?graphicsLightSlot

        inc	gfx.dropped
        rts

?graphicsLightSlot
        sta	gfx.slot
        clc
        adc	#1
        sta	gfx.rowUsed,y
        tya
        asl
        asl
        asl
        asl
        clc
        adc	#PM_MAP_Y
        sta	pmWrite
        ldx	gfx.buildBank
        lda	pmBaseHigh,x
        clc
        adc	#4
        adc	gfx.slot
        sta	pmWrite+1
        ldx	gfx.lightUnit
        lda	units.kind,x
        asl
        asl
        asl
        asl
        tax
        ldy	#0

?graphicsLightLine
        lda	unitLights,x
        sta	(pmWrite),y
        inx
        iny
        cpy	#16
        bne	?graphicsLightLine

        lda	gfx.row
        asl
        asl
        clc
        adc	gfx.buildOffset
        tay
        ldx	gfx.lightUnit
        lda	units.x,x
        asl
        asl
        asl
        clc
        adc	#PM_MAP_X
        ldx	gfx.slot
        cpx	#1
        beq	?graphicsLightP1

        cpx	#2
        beq	?graphicsLightP2

        cpx	#3
        beq	?graphicsLightP3

        sta	pmHpos0,y
        rts

?graphicsLightP1
        sta	pmHpos1,y
        rts

?graphicsLightP2
        sta	pmHpos2,y
        rts

?graphicsLightP3
        sta	pmHpos3,y
        rts

;************************************************
;* addBracket
;*
;* Input : gfx.tileX/Y, slot (cursor/selection) and buildBank
;* Output: missile bracket bits and horizontal positions added
;* Trash: a,x,y,flags,pmWrite; spare PM bank
;*
;* Tile bracket:   [        ]   top/bottom: two bits per side
;*                |  unit  |   middle: one bit per side
;*                 [        ]
;* Cursor: M0/M1; selected tile: M2/M3 (ORed into missile bytes).
;* Left X = PM_MAP_X + tileX*8; right X = left+6.
;* Top Y = PM_MAP_Y + tileY*16; bracket height = 16 scanlines.
;************************************************

addBracket

        ldx	gfx.buildBank
        lda	pmBaseHigh,x
        clc
        adc	#3
        sta	pmWrite+1
        lda	gfx.tileY
        asl
        asl
        asl
        asl
        clc
        adc	#PM_MAP_Y
        sta	pmWrite
        ldy	#0

?graphicsBracketLine
        lda	gfx.slot
        bne	?graphicsSelectedBracket

        lda	cursorBracket,y
        jmp	?graphicsBracketStore

?graphicsSelectedBracket
        lda	selectBracket,y

?graphicsBracketStore
        ora	(pmWrite),y
        sta	(pmWrite),y
        iny
        cpy	#16
        bne	?graphicsBracketLine

        lda	gfx.buildBank
        asl
        clc
        adc	gfx.slot
        asl
        tay
        lda	gfx.tileX
        asl
        asl
        asl
        clc
        adc	#PM_MAP_X
        sta	missileHpos,y
        clc
        adc	#6
        sta	missileHpos+1,y
        rts

;************************************************
;* graphicsVbi
;*
;* Input : OS immediate VBI context; completed PM/HUD requests
;* Output: resets attract/raster state, commits PM/HUD pointers, chains OS
;* Trash: flags; a/x restored before chaining, y untouched
;*
;* Run before SYSVBL applies attract colors, even if its deferred
;* stage is skipped. Prime the first DLI's immediate color operands.
;*
;* Vertical blank (outside the visible picture):
;* [pending PM]  --> PMBASE + missile X positions --> [front PM]
;* [pending HUD] --> display-list text pointer    --> [front HUD]
;* band = 0; first orange/blue colors --> cached LDA operands
;* Then chain the previous OS VBI; DLI handles the visible bands.
;************************************************

graphicsVbi

        pha
        txa
        pha
        lda	#0
        sta	OS_ATRACT
        sta	gfx.band
        lda	rasterOrange
        sta	graphicsDliOrange+1
        lda	rasterBlue
        sta	graphicsDliBlue+1
        ldx	gfx.pendingBank
        cpx	#NONE
        beq	?graphicsVbiDone

        stx	gfx.frontBank
        lda	#NONE
        sta	gfx.pendingBank
        lda	pmBaseHigh,x
        sta	PMBASE
        lda	pmOffsets,x
        sta	gfx.activeOffset
        txa
        asl
        asl
        tax
        lda	missileHpos,x
        sta	HPOSM0
        lda	missileHpos+1,x
        sta	HPOSM1
        lda	missileHpos+2,x
        sta	HPOSM2
        lda	missileHpos+3,x
        sta	HPOSM3

?graphicsVbiDone
        ldx	gfx.hudPending
        cpx	#NONE
        beq	?graphicsVbiRestore

        stx	gfx.hudFront
        lda	#NONE
        sta	gfx.hudPending
        txa
        clc
        adc	#>PM_RAM
        sta	hudDisplay+1

?graphicsVbiRestore
        pla
        tax
        pla
        jmp	(graphicsOldVbi)

;************************************************
;* rasterDli
;*
;* Input : gfx.band/activeOffset; palette and player-position tables
;* Output: next raster palette/player row, or dark menu palette
;* Trash: none; a,x,y and interrupted flags restored by RTI
;*
;* Cached immediate colors keep entry-to-WSYNC at nine CPU cycles.
;* The next colors are prepared after the visible register writes.
;* PM positions change within the two blank highlight rows.
;*
;* Visible map, 160 scanlines:
;*   bands  0.. 3: [A][B][C][D]  tile row 0, P0..P3 at row-0 X
;*   bands  4.. 7: [A][B][C][D]  tile row 1, P0..P3 at row-1 X
;*        ...     four scanlines per band, 16 per tile row
;*   bands 36..39: [A][B][C][D]  tile row 9
;*   band 40:     [dark HUD palette] (COLPF1=$0e, COLPF2=$02)
;*
;* DLI: cached colors --> WSYNC --> COLPF1/COLPF2
;*      band%4=0: update player X; then cache the next band colors.
;* A..D are four palette steps, not four different sprite images.
;************************************************

rasterDli

        pha

graphicsDliOrange

        lda	#MAP_ORANGE
        sta	WSYNC
        sta	COLPF1

graphicsDliBlue

        lda	#MAP_BLUE
        sta	COLPF2
        txa
        pha
        tya
        pha
        ldx	gfx.band
        cpx	#RASTER_BANDS
        beq	?graphicsMenuBand

        txa
        and	#3
        bne	?graphicsNextBand

        txa
        clc
        adc	gfx.activeOffset
        tax
        lda	pmHpos0,x
        sta	HPOSP0
        lda	pmHpos1,x
        sta	HPOSP1
        lda	pmHpos2,x
        sta	HPOSP2
        lda	pmHpos3,x
        sta	HPOSP3

?graphicsNextBand
        inc	gfx.band
        ldx	gfx.band

?graphicsPrepareBand
        cpx	#RASTER_BANDS
        beq	?graphicsPrepareMenu

        lda	rasterOrange,x
        sta	graphicsDliOrange+1
        lda	rasterBlue,x
        sta	graphicsDliBlue+1
        jmp	?graphicsDliDone

?graphicsPrepareMenu
        lda	#$02
        sta	graphicsDliBlue+1
        lda	#$0e
        sta	graphicsDliOrange+1
        jmp	?graphicsDliDone

?graphicsMenuBand
        lda	#0
        sta	gfx.band
        tax
        jmp	?graphicsPrepareBand

?graphicsDliDone
        pla
        tay
        pla
        tax
        pla
        rti

;************************************************
;* resetRaster
;*
;* Input : immutable four-band orange/blue palette definitions
;* Output: all forty map raster entries restored after battle
;* Trash: a,x,y,flags; rasterOrange/rasterBlue
;*
;* Immutable palettes: [A B C D]
;* Map color tables:   [A B C D][A B C D] ... [A B C D]
;*                     ten tile rows = 40 DLI bands
;* Restores colors only; PM masks and positions are unchanged.
;************************************************

resetRaster

        ldx	#RASTER_BANDS-1

?graphicsRestoreBand
        txa
        and	#3
        tay
        lda	rasterOrangeBands,y
        sta	rasterOrange,x
        lda	rasterBlueBands,y
        sta	rasterBlue,x
        dex
        bpl	?graphicsRestoreBand

        rts

;************************************************
;* endHudUpdate
;*
;* Input : complete 200-byte HUD in the main-thread staging buffer
;* Output: completed spare text page queued for an atomic VBI handoff
;* Trash: a,x,y,flags,pmWrite; shared main-thread snapshot scratch
;*
;* Single-line PM DMA leaves offsets $000..$2ff unused in each bank.
;* Three screen pages use PM bank zero; staging uses PM bank one.
;* Neither the visible nor the pending page is overwritten.
;*
;* [HUD staging: 5 rows x 40 bytes] --copy--> [spare text page]
;*                                               |
;*                                               v
;* [front: visible]  [pending: queued] <-- publish completed page
;*                          |
;*                          +--VBI--> HUD display-list pointer
;* Text pages use bank-0 offsets $000/$100/$200, not PM sprite DMA.
;************************************************

endHudUpdate

?hudSnapshot
        lda	gfx.hudFront
        sta	gfx.snapshotFront
        lda	gfx.hudPending
        sta	gfx.snapshotPending
        lda	gfx.hudFront
        cmp	gfx.snapshotFront
        bne	?hudSnapshot

        ldx	#0

?hudChoosePage
        cpx	gfx.snapshotFront
        beq	?hudNextPage

        cpx	gfx.snapshotPending
        bne	?hudPageReady

?hudNextPage
        inx
        jmp	?hudChoosePage

?hudPageReady
        txa
        clc
        adc	#>PM_RAM
        sta	pmWrite+1
        lda	#0
        sta	pmWrite
        ldy	#199

?hudCopyPage
        lda	hud,y
        sta	(pmWrite),y
        dey
        cpy	#NONE
        bne	?hudCopyPage

        stx	gfx.hudPending
        rts
