arch spc700-raw

;; Some extra commands that the 5A22 can request through $1DFA writes
;; just so no one thinks about playing them as sound effects

derpHack:
	mov	y, #ProcessAPU1SFX>>8	;\ Return address
	push	y			; |
	mov	y, #ProcessAPU1SFX&$FF	; |
	push	y			;/
	and	a, #$0F			;\ Get the actual index
	asl	a			; |
	mov	x, a			;/
	jmp	(derpPtrs+x)		; le jump

derpPtrs:
	dw excmdF0
	dw excmdF1
	dw excmdF2
	dw excmdF3
	dw excmdF4
	dw excmdF5
	dw excmdF6
	dw excmdF7
	dw excmdF8
	dw excmdF9
	dw excmdFA
	dw excmdFB
	dw excmdFC
	;dw excmdFD
	;dw excmdFE
	;dw excmdFF

excmdF0:				;;; increase global pitch
{
	inc	$43
	ret
}

excmdF1:				;;; decrease global pitch
{
	dec	$43
	ret
}

excmdF2:				;;; increase global volume left
{
	mov	$F2, #$0C
	inc	$F3
	ret
}

excmdF3:				;;; decrease global volume left
{
	mov	$F2, #$0C
	dec	$F3
	ret
}

excmdF4:				;;; increase global volume right
{
	mov	$F2, #$1C
	inc	$F3
	ret
}

excmdF5:				;;; decrease global volume right
{
	mov	$F2, #$1C
	dec	$F3
	ret
}

excmdF6:				;;; invert phase left
{
	mov	$F2, #$0C
	bra	invertPhase
excmdF7:				;;; invert phase right
	mov	$F2, #$1C
invertPhase:
	eor	$F3, #$FF
	inc	$F3
	ret
}

excmdF8:				;;; toggle echo on sfx
{
	mov	a, !SFXEchoChannels
	beq	.turnOn
	inc	!SFXEchoChannels	; #$00
	bra	+
.turnOn
	dec	!SFXEchoChannels	; #$FF
+	call	EffectModifier
	ret
}

excmdF9:				;;; toggle yoshi drums
{
	mov	a, $0386
	beq	.turnOn
	dec	$0386			; #$00
	bra	+
.turnOn
	inc	$0386			; #$01
+	call	HandleYoshiDrums
	ret
}

excmdFA:				;;; increase tempo (buggy?)
{
	mov	a, #$0A
	bra	changeTempo
excmdFB:				;;; decrease tempo (buggy?)
	mov	a, #$F2
changeTempo:
	adc	a, $0387
	mov	$0387, a
	call	L_0E14
	ret
}

excmdFC:				;;; toggle sending envelope values to the 5A22
{
	mov	a, $038A
	eor	a, #$01
	mov	$038A, a
	ret
}

;;;; This handles toggling channels as per 5A22 request ($E0-$EF)

toggleRequest:
	and	a, #$07
	mov	x, a
	mov	a, maskTable+x
	eor	a, $5E
	mov	$5E, a
	jmp	ProcessAPU1SFX

maskTable:
	db $01,$02,$04,$08,$10,$20,$40,$80

;;;; This handles sending envelope data to the 5A22 when said option is enabled

SendEnvelopes:
	mov	x, #$07
	mov	y, #$03
.loop
	mov	a, DSPEnv+x
	mov	$F2, a
	mov	a, $F3
	and	a, #$70		;E0
	;lsr	a
	mov	$0166+y, a
	dec	x
	mov	a, DSPEnv+x
	mov	$F2, a
	mov	a, $F3
	and	a, #$70		;E0
	; rol	a
	; rol	a
	; rol	a
	lsr	a
	lsr	a
	lsr	a
	lsr	a
	or	a, $0166+y
	mov	$0166+y, a
	dec	x
	dec	y
	bpl	.loop
	ret
	
DSPEnv:
	db $08,$18,$28,$38,$48,$58,$68,$78
