
; Audio Eexampl 5: Spot sound effect system

; Press fire button on joystick to start a sample playing.
; Press LMB to stop program.

		include		hardware.i		hardware equates
		include		hw_macros.i
		include		hw_start.i		startup code

; Set processor level 3 autovector

Main		move.l		#NewLevel3,$6c

; Enable VERTB interrupts

		move.w		#SETIT!INTEN!VERTB,INTENA(a5)

; Wait for user to press left mouse button

LeftMouse	btst		#6,CIAAPRA		button pressed
		bne.s		LeftMouse		no, keep looping

; And exit

		rts

;*****	Level 3 interrupt handler

NewLevel3	lea		$dff000,a5		hardware base

		bsr		SamplePlayer		do spot effects

		bsr		CheckFire		check fire button

; Clear interrupt bit and exit

		move.w		INTREQR(a5),d0		get bit
		and.w		#$7fff,d0		clear bit 15
		move.w		d0,INTREQ(a5)		clear request
		
		rte					back to User Mode

;*****	Sample player, first part of spot effect system

SamplePlayer	move.l		Chan0,d0		get addr of sample
		beq		NoNewSample		exit if not one
		move.l		d0,a0			a0->structure

; Initialise audio channel 0

		move.w		(a0)+,AUD0LEN(a5)	sample length
		move.l		a0,AUD0LCH(a5)		samples address
		move.w		#$12c,AUD0PER(a5)	sample period
		move.w		#$64,AUD0VOL(a5)	sample volume

; Now start channel 0 DMA

		move.w		#SETIT!DMAEN!AUD0EN,DMACON(a5) start DMA

; And initialise a quiet sample to follow it

		move.w		#1,AUD0LEN(a5)

; Clear this sample request

		move.l		#0,Chan0		clear request

NoNewSample	rts					and exit


;*****	Check fire button

; A timeout counter is used to stop the player firing every VBI. This gives
;a sample a chance to start before being interrupted. It is most unlikely
;that a player could fire fifety shots a second in a game anyhow.

CheckFire	tst.w		VBL_Count		timeout clear
		beq.s		CanFire			yes, continue
		
		subq.w		#1,VBL_Count		no, dec timeout
		bra.s		FireDone		and exit

CanFire		tst.b		CIAAPRA			fire button pressed?
		bmi.s		FireDone		skip if not

		lea		Sample1,a0
		bsr		PlaySFX			request sample
		
		move.w		#3,VBL_Count		set timeout
FireDone	rts

;*****	Sample Request, final part of spot effects system

; Entry		a0->sample structure to play

PlaySFX		move.l		a0,Chan0		set sample to play
		move.w		#AUD0EN,DMACON(a5)	stop current sample
		rts


;*****	Data

VBL_Count	dc.w		0
Chan0		dc.l		0

;*****	Sample data -- must be in CHIP memory

section		data custom,chip		****  Use this for A68K  ****

;		Section		custom,data_C	**** Use this for Devpac ****

Sample1		dc.w		Smp1Len			sample word length
Smp1		dc.w		0		
		incbin		'Sample.raw'		sample data
Smp1Len		equ		(*-Smp1)>>1

		end


