 ; Amiga Interrupts. By Ian Potts. (c)1989 CoolSoft.
 ; Listing 2. Example of SetIntVector

 ; Assembled with DevPac V2.08 By HiSoft Ltd.


		opt		d-,o+,ow-

		incdir		"rad:include/"
		include		"exec/exec_lib.i"	;exec
		include		"hardware/intbits.i	;for int bits
		include		"hardware/dmabits.i"	;for audio DMA
		include		"hardware/custom.i"	;for chip registers
		include		"exec/interrupts.i"	;for server structure

		include		"misc/easystart.i"

custom		equ		$dff000		;address of custom chips

		move.l		#wavelength,d0		;get memory for 
		move.l		#MEMF_CHIP!MEMF_PUBLIC,d1	;waveform
		CALLEXEC	AllocMem
		tst.l		d0			;got it?
		beq.s		exit			;no, exit
		move.l		d0,waveform

		move.l		#1600-1,d1		;copy waveform into
		move.l		waveform(pc),a1		;memory 1600 times
copywave:	lea		wavetable(pc),a0
		clr.l		d0
copywave1:	move.l		(a0,d0),(a1)+
		addq.l		#4,d0
		cmp.l		#24,d0
		bne.s		copywave1
		dbra		d1,copywave

		move.l		#soundinterrupt,soundint+IS_CODE
		move.l		#channel0data,soundint+IS_DATA
		clr.b		soundint+LN_PRI
		lea		soundint(pc),a1
		move.l		#INTB_AUD0,d0
		CALLEXEC	SetIntVector	;set up our interrupt routine
		move.l		d0,oldint	;returned previous vector

		move.l		waveform,custom+aud0+ac_ptr
		move.w		#wavelength/2,custom+aud0+ac_len
		move.w		#64,custom+aud0+ac_vol	
		move.w		#240,custom+aud0+ac_per	
		move.w		#1,channel0data+8	;play sound once

		lea		channel0data(pc),a1
		bsr		startsound		;start channel 0

snooze:		cmp.w		#0,channel0data+8	;wait for sound to
		bne.s		snooze			;finish

		move.l		oldint(pc),a1
		move.l		#INTB_AUD0,d0
		CALLEXEC	SetIntVector	;restore old interrupt code

		move.l		#wavelength,d0	;Free up sound waveform
		move.l		waveform(pc),a1
		CALLEXEC	FreeMem
exit:		rts				;and say goodbye...

startsound:	add.w		#2,8(a1)
		move.w		4(a1),d0
		or.w		#DMAF_SETCLR+DMAF_MASTER,d0
		move.w		d0,custom+dmacon	;start audio DMA
		move.w		6(a1),d0
		or.w		#INTF_SETCLR,d0
		move.w		d0,custom+intena	;enable audio interrupt
		rts

stopsound:	move.w		4(a1),custom+dmacon	;stop audio DMA
		move.w		6(a1),custom+intena	;disable audio interrupt
		rts

soundinterrupt:	cmp.w		#0,8(a1)	;played waveform enough
		beq.s		soundintexit	;times? yes, exit
		sub.w		#1,8(a1)	;no count down times
		cmp.w		#0,8(a1)	;all done?
		bne.s		soundintexit	;no exit
		bsr.s		stopsound	;switch off DMA
soundintexit:	move.w		6(a1),custom+intreq	;clear interrupt bit
		rts				;exit interrupt

soundint:	ds.b		IS_SIZE		;interrupt server structure
		even				;for installing our routine
channel0data:	dc.l		aud0		;pointer to audio channel regs
		dc.w		DMAF_AUD0	;dma bit
		dc.w		INTF_AUD0	;interrupt bit
		dc.w		0		;num of times to repeat sound+2
oldint:		dc.l		0		;pointer to old interrupt
						;node structure
waveform:	dc.l		0		;address of waveform
wavelength	equ		1600*24		;length of waveform
						;triangle wave:-
wavetable:	dc.b		120,100,80,60,40,20,0,-20,-40,-60,-80,-100
		dc.b		-120,-100,-80,-60,-40,-20,0,20,40,60,80,100
