        icl	"src/sound.i"

;************************************************
;* initSound
;*
;* Input : OS keyboard/serial state initialized by the loader
;* Output: independent 64 kHz POKEY channels, keyboard scanning enabled
;* Trash: a,flags; sound state and POKEY audio registers
;************************************************

initSound

        lda	#3
        sta	SKCTL
        sta	SSKCTL
        lda	#0
        sta	AUDCTL
        sta	AUDC3
        sta	AUDC4
        jmp	stopSound

;************************************************
;* soundMove / soundAttack / soundCapture / soundTitle / soundVictory
;*
;* Input : X = moving/firing unit; game.winner for capture
;* Output: starts unit effect, capture/title tune or victory fanfare
;* Trash: a,y,flags,src; x preserved; replaces any previous effect
;************************************************

soundMove

        jsr	unitSoundKind

        jmp	?playSound

soundAttack

        jsr	unitSoundKind

        clc
        adc	#4
        jmp	?playSound

soundCapture

        lda	game.winner
        bne	soundVictory

        lda	#8
        bne	?playSound

soundVictory

        lda	#10
        bne	?playSound

soundTitle

        lda	#0
        sta	sound.titleStep
        lda	#1
        sta	sound.titleActive
        lda	#9

?playSound
        tay
        cpy	#9
        beq	?soundSequenceReady

        lda	sound.titleActive
        beq	?soundSequenceReady

        lda	#0
        sta	sound.titleActive
        sta	AUDC3

?soundSequenceReady
        lda	soundLow,y
        sta	sound.low
        lda	soundHigh,y
        sta	sound.high
        jmp	soundLoadStep

;************************************************
;* soundTick
;*
;* Input : once per observed frame; current display/help state
;* Output: advances the envelope without waiting; HELP freezes/mutes it
;* Trash: a,y,flags,src; x preserved
;************************************************

soundTick

        lda	app.screen
        beq	?soundScreenReady

        cmp	#app_screen.TITLE
        bne	stopSound

?soundScreenReady
        lda	help.active
        bne	muteSound

        lda	sound.remaining
        beq	?soundTickDone

        dec	sound.remaining
        bne	soundFrame

        inc	sound.titleStep
        lda	sound.low
        clc
        adc	#3
        sta	sound.low
        bcc	soundLoadStep

        inc	sound.high
        jmp	soundLoadStep

?soundTickDone
        rts

;************************************************
;* stopSound / muteSound
;*
;* Input : none
;* Output: silence owned voices; stop also discards pending playback
;* Trash: a,flags; x/y preserved
;************************************************

stopSound

        jsr	muteSound

        lda	#0
        sta	sound.remaining
        sta	sound.titleActive
        rts

muteSound

        lda	#0
        sta	AUDC1
        sta	AUDC2
        lda	sound.titleActive
        beq	?soundMuted

        lda	#0
        sta	AUDC3

?soundMuted
        rts

;************************************************
;* soundLoadStep / soundFrame / soundSource
;*
;* Input : sound.low/high points at the current private command
;* Output: loads duration, loops title on $ff, or writes its tone
;* Trash: a,y,flags,src; x preserved
;************************************************

soundLoadStep

        jsr	soundSource

        ldy	#2
        lda	(src),y
        beq	stopSound

        cmp	#SOUND_LOOP
        bne	?soundStepDuration

        jmp	soundTitle

?soundStepDuration
        sta	sound.remaining
        jmp	?soundWrite

soundFrame

        jsr	soundSource

?soundWrite
        ldy	#0
        lda	(src),y
        sta	AUDF1
        lsr
        sta	AUDF2
        iny
        lda	(src),y
        sta	AUDC1
        and	#$0f
        lsr
        ora	#$a0
        sta	AUDC2
        lda	sound.titleActive
        beq	?soundWriteDone

        jmp	soundTitleVoices

?soundWriteDone
        rts

soundSource

        lda	sound.low
        sta	src
        lda	sound.high
        sta	src+1
        rts
