        icl	"src/titlemotion.i"

;************************************************
;* resetTitleMotion
;*
;* Input : entering the title screen
;* Output: convoy, clock and logo raster colors reset
;* Trash: a,x,y,flags; private titleMotion and logo raster state
;************************************************

resetTitleMotion

        lda	#0
        sta	titleMotion.clock
        sta	titleMotion.position
        sta	titleMotion.logoPhase
        jmp	titleLogoColors

;************************************************
;* titleMotionTick
;*
;* Input : once per observed frame; app.screen and help.active
;* Output: convoy and logo color gradient advance every fourth frame
;* Trash: a,x,y,flags,src,dst,maskptr; draw/private scratch
;*
;* Rebuild only scanlines 128..143, below the static illustration.
;* The rightmost column clips the second sprite byte at the edge.
;************************************************

titleMotionTick

        lda	app.screen
        cmp	#app_screen.TITLE
        bne	?titleMotionReturn

        lda	help.active
        bne	?titleMotionReturn

        inc	titleMotion.clock
        lda	titleMotion.clock
        cmp	#4
        bcc	?titleMotionReturn

        lda	#0
        sta	titleMotion.clock
        inc	titleMotion.logoPhase
        jsr	titleLogoColors

        inc	titleMotion.position
        lda	titleMotion.position
        cmp	#40
        bcc	?titleMotionDraw

        lda	#0
        sta	titleMotion.position

?titleMotionDraw
        lda	#<(frameBuffer+128*40)
        sta	dst
        lda	#>(frameBuffer+128*40)
        sta	dst+1
        lda	#16
        sta	draw.lines

?titleMotionClearRow
        lda	#$55
        ldx	draw.lines
        cpx	#9
        bcs	?titleMotionClearBytes

        lda	#0

?titleMotionClearBytes
        ldy	#39

?titleMotionClearByte
        sta	(dst),y
        dey
        sta	(dst),y
        dey
        sta	(dst),y
        dey
        sta	(dst),y
        dey
        bpl	?titleMotionClearByte

        jsr	advanceRow

        dec	draw.lines
        bne	?titleMotionClearRow

        lda	#0
        sta	titleMotion.unit
        jmp	?titleMotionUnit

?titleMotionReturn
        rts

?titleMotionUnit
        ldx	titleMotion.unit
        lda	titleMotion.position
        clc
        adc	titleMotionOffsets,x
        cmp	#40
        bcc	?titleMotionColumn

        sbc	#40

?titleMotionColumn
        sta	titleMotion.column
        lda	titleMotionTiles,x
        tax
        lda	tileLow,x
        sta	src
        sta	maskptr
        lda	tileHigh,x
        sta	src+1
        lda	maskHigh,x
        sta	maskptr+1
        lda	#<(frameBuffer+128*40)
        clc
        adc	titleMotion.column
        sta	dst
        lda	#>(frameBuffer+128*40)
        adc	#0
        sta	dst+1
        lda	#16
        sta	draw.lines

?titleMotionUnitRow
        ldy	#0

?titleMotionUnitByte
        lda	(maskptr),y
        eor	#$ff
        and	(dst),y
        ora	(src),y
        sta	(dst),y
        iny
        cpy	#2
        beq	?titleMotionNextRow

        lda	titleMotion.column
        cmp	#39
        bne	?titleMotionUnitByte

?titleMotionNextRow
        clc
        lda	src
        adc	#2
        sta	src
        sta	maskptr
        bcc	?titleMotionSourceReady

        inc	src+1
        inc	maskptr+1

?titleMotionSourceReady
        jsr	advanceRow

        dec	draw.lines
        bne	?titleMotionUnitRow

        inc	titleMotion.unit
        lda	titleMotion.unit
        cmp	#TITLE_UNITS
        beq	?titleMotionDone

        jmp	?titleMotionUnit

?titleMotionDone
        rts

;************************************************
;* titleLogoColors
;*
;* Input : titleMotion.logoPhase and sixteen palette entries
;* Output: rolling DLI colors only in logo scanlines 12..63
;* Trash: a,x,y,flags; rasterOrange/rasterBlue bands 3..15
;*
;* Cached DLI entry timing is unchanged; flags, credit and HUD stay fixed.
;************************************************

titleLogoColors

        ldx	#15

?titleLogoBand
        txa
        clc
        adc	titleMotion.logoPhase
        and	#15
        tay
        lda	titleLogoPalette,y
        sta	rasterOrange,x
        tya
        clc
        adc	#8
        and	#15
        tay
        lda	titleLogoPalette,y
        sta	rasterBlue,x
        dex
        cpx	#2
        bne	?titleLogoBand

        rts
