; include file

#dontinclude




function	init_sound
rem	init_sound()
rem	sets up a basic sound driver for small wave files to be played back
rem	make sure you are in ram/ram mode for playback if you are $8000+
rem

def	sound_play	byte
def	sound_pos	word
def	sound_end	word

init_sound

	orcc	#$50	;kill interupt
	ldx	#sound_irq2
	lda	#$7e
	sta	$10c
	stx	$10d

;**************************************
; NUKE VSYNC IRQ
;**************************************
	lda	$ff03
	anda	#$fe
	sta	$ff03
	lda	$ff02
;**************************************
; ENABLE HSYNC IRQ
;**************************************
	lda	$ff01
	ora	#$1
	sta	$ff01
	lda	$ff00	


	andcc	#$ef

	rts


;*******************************************************************************
sound_irq





	if	b,sound_play=#1
	lda	$ff01	; Select DAC as sound source
	anda	#$f7
	sta	$ff01
	lda	$ff03
	anda	#$f7
	sta	$ff03
	lda	$ff23
	anda	#247	;or8
	adda	#8
	sta	$ff23

	ldx	sound_pos

	ldb	#100
sound_wait0
	lda	,x+
	sta	$ff20
	ldy	#7
sound_wait1
	leay	-1,y
	bne	sound_wait1
	cmpx	sound_end
	bhs	sound_finished
	decb
	bne	sound_wait0
	leax	10,x
	stx	sound_pos
	endif


sound_exit
	lda	$ff02	;ack irq
	rti

sound_finished
	clr	sound_play
	bra	sound_exit















;*******************************************************************

sound_irq2
;	inc	switch
;	bpl	sound_irqe2
; update system clock
;	ldd	$112	;sys clock
;	addd	#1
;	std	$112
;	clr	switch
;sound_irqe2
	lda	$ff00
	rti

def	switch	byte

endfunction








function	prep_sound	prep_sound_adr,prep_sound_adre
rem	prep_sound( address.w , end.w )
rem	
rem	
rem	

def	prep_sound_adr	word
def	prep_sound_adre	word



prep_sound
	orcc	#$50


;skip 44 bytes
	ldd	prep_sound_adr
	addd	#44
	std	sound_pos
	ldx	sound_pos
;adjust 6 bit
prep_sound_fx
	lda	,x

	anda	#$fc
	sta	,x+
	cmpx	prep_sound_adre
	bls	prep_sound_fx



	ldd	prep_sound_adre
	std	sound_end

	lda	#0
	sta	sound_play
	andcc	#$ef

	rts
endfunction




function	play_sound	play_sound_adr,play_sound_adre
rem	play_sound( address.w , end.w )
rem	
rem	
rem	

def	play_sound_adr	word
def	play_sound_adre	word



play_sound
	orcc	#$50


;skip 44 bytes
	ldd	play_sound_adr
	addd	#44
	std	sound_pos
	ldx	sound_pos

	ldd	play_sound_adre
	std	sound_end

	lda	#1
	sta	sound_play
	andcc	#$ef

	rts
endfunction



function	stop_sound
rem	stop_sound()
rem	
rem	
rem	

stop_sound
	orcc	#$50
	lda	#0
	sta	sound_play
	andcc	#$ef
	rts
endfunction


















