;---------------------------------------
;
; PSG subroutines:
;
; * ResetPSG - Reset the PSG
;
; * SetPSGFreq - Set the frequency of a channel
;
; * SetPSGVol - Set the volume of a channel
;
;
; by drx [www.hacking-cult.org]
;        [www.bluehedgehog.org]
;        [www.shadowsoft-games.com]
;
;---------------------------------------


ResetPSG:


		move.b	#$9F,($C00011).l	;set channel 1 volume to 0
		move.b	#$BF,($C00011).l	;set channel 2 volume to 0
		move.b	#$DF,($C00011).l	;set channel 3 volume to 0
		move.b	#$FF,($C00011).l	;set noise volume to 0
		
		rts
				
;---------------------
; Set frequency
;
; d0 - frequency (0-$3ff)
; d1 - channel (0, 1, 2)
;---------------------		
SetPSGFreq:

		move.b	d0,d2
		and.b	#$F,d2
		add.b	#$80,d2
		and.b	#$3,d1
		lsl.b	#$5,d1
		or.b	d1,d2
		move.b	d2,($C00011).l	;$80+channel<<5+(freq&$f)
		lsr.w	#$4,d0
		move.b	d0,($C00011).l	;freq>>4
		rts
		
;---------------------
; Set volume
;
; d0 - volume (0-$f)
; d1 - channel (0, 1, 2, 3)
;---------------------			
SetPSGVol:
		and.b	#$F,d0
		and.b	#$3,d1
		moveq	#0,d2
		move.b	#$F,d2
		sub.b	d0,d2
		add.b	#$90,d2
		lsl.b	#$5,d1
		or.b	d1,d2
		move.b	d2,($C00011).l	;$90+channel<<5+($f-vol)
		rts