*	sound stuff	*
soundstarth:	equ	2
soundstartm:	equ	4
soundstartl:	equ	6
soundendh:	equ	$e
soundendm:	equ	$10
soundendl:	equ	$12
soundmode:	equ	$20
sounddma:	equ	0

gemdos=1
super=$20
ptermres=$31
vbl_list=$4ce
prt_cnt=$4ee
color2=$ff8244
delay=8

start:
	move.l	4(sp),savesize
	clr.l	-(sp)		; supervisor stack becomes user stack
	move.w	#super,-(sp)	; enter supervisor mode
	trap	#gemdos		; now
	addq.l	#6,sp		; clean up stack
	move.l	d0,savesup

	move.l	#text,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp

	bsr	cdma
	tst.w	dma
	bmi	notdmaexit


	bsr	loadsounds
	tst.l	startsa
	bmi.s	waitsound
	bsr	dostartsound
waitsound:
	tst.b	$ff8901		;don't exit till sound finished
	bne.s	waitsound


	move.l	savesup,-(sp)	; old supervisor stack pointer
	move.w	#super,-(sp)	; return to user mode
	trap	#gemdos		; now
	addq.l	#6,sp		; clean up stack
*illegal

	clr.w	-(sp)		; no error code
	move.l	savesize,-(sp)	; reserve this amount of space
	move.w	#ptermres,-(sp)	; terminate processing
	trap	#1		; now
*	rts

notdmaexit:
	move.l	#errortext,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp

	move.l	$4ba,d0
errordelay:
	move.l	d0,d1
	sub.l	$4ba,d1
	cmp.l	#800,d1
	blt.s	errordelay

	clr.w	-(sp)
	trap	#1

dma:	dc.w	-1
cdma:
	move.l	$5a0,a0		;get pointer to cookie jar
fcloop:
	move.l	(a0)+,d0
	beq.s	fcloopover
	cmpi.l	#'_SND',d0
	beq.s	chkdma
	addq.l	#4,a0
	bra.s	fcloop
chkdma:
	move.l	(a0)+,d0
	btst	#1,d0
	beq	fcloopover	;not dma sound
	move.w	#1,dma
	rts

fcloopover:
	move.w	#-1,dma
	rts

loadsounds:
	lea	keysoundfile,a0
	lea	sounds,a1
	bsr	loadfile
	tst.l	d0		;was there a file to load
	bmi.s	nokey
	move.l	a1,keya
	bsr	evend0
	add.l	d0,a1
	move.l	a1,keyb
nokey:
	lea	bellsoundfile,a0
	bsr	loadfile
	tst.l	d0		;was there a file to load
	bmi.s	nobell
	move.l	a1,bella
	bsr	evend0
	add.l	d0,a1
	move.l	a1,bellb
nobell:
	move.l	a1,a2
	sub.l	savesize,a2
	move.l	a2,savesize

	lea	startsoundfile,a0
	bsr	loadfile
	tst.l	d0		;was there a file to load
	bmi.s	nonuthin
	move.l	a1,startsa
	bsr	evend0
	add.l	d0,a1
	move.l	a1,startsb
nonuthin:

	tst.l	bella
	bmi.s	putkey
	move.l	#mybell,$5ac
putkey:
	tst.l	keya
	bmi.s	putnone
	move.l	#mykey,$5b0
putnone:
	rts

evend0:
	addq.l	#1,d0
	and.l	#$fffffffe,d0
	rts

startsa:	dc.l	-1
startsb:	dc.l	-1
keya:		dc.l	-1
keyb:		dc.l	-1
bella:		dc.l	-1
bellb:		dc.l	-1

*************************************************
*	Pass a0 for address of filename		*
*	a1 for address to load to		*
*	returns d0 length of file		*
*	id d0 <0 file not found			*
*************************************************
loadfile:
	movem.l	d1-d7/a0-a6,-(sp)
	move    #0,-(SP)        ;Open file
	move.l  a0,-(SP) ;Address of filename
	move    #$3D,-(SP)
	trap    #1              ;Get handle number in D0
	addq.l  #8,SP
	tst.w	d0
	bmi	loaderror
	move.w	d0,-(sp)
	move.w	(sp),d0
	move.l  a1,-(SP)	;load it straight onto screen
	move.l  #1000000,-(SP)	;shouldn't be any files bigger than this
	move    D0,-(SP)	;Handle
	move    #$3F,-(SP)	;bios number
	trap    #1		;Read file in
	add.l   #12,SP
	tst.l	d0
	bmi	loade2
	move.l	d0,loadlen
	move    #$3E,-(SP)      ;Handle already on stack
	trap    #1              ;Close file
	addq.l  #4,SP
	move.l	loadlen,d0
loadback:
	movem.l	(sp)+,d1-d7/a0-a6
	rts
loadlen:	dc.l	0

loaderror:
	cmpi.w	#-33,d0
	beq	fnferror
	moveq	#-3,d0
	bra	loadback
fnferror:
	moveq	#-1,d0		;say user wants to try again
	bra	loadback
loade2:
	addq.l	#2,sp
	moveq	#-2,d0
	bra	loadback




mybell:
	move.l	bella,d0
	move.l	bellb,d1
	bra.s	dosound
mykey:
	move.l	keya,d0
	move.l	keyb,d1
	bra.s	dosound


dostartsound:
	move.l	startsa,d0
	move.l	startsb,d1
*****************************************
*	new sound chip stuff		*
*	pass sound start in d0		*
*	and sound end in d1		*
*****************************************
dosound:
	lea	$ff8900,a0	;sound register base address
	move.w	#0,sounddma(a0)
	move.w	d0,soundstartl(a0)
	lsr.l	#8,d0
	move.w	d0,soundstartm(a0)
	lsr.l	#8,d0
	move.w	d0,soundstarth(a0)
	move.w	d1,soundendl(a0)
	lsr.l	#8,d1
	move.w	d1,soundendm(a0)
	lsr.l	#8,d1
	move.w	d1,soundendh(a0)
	move.w	#%0000000010000001,soundmode(a0)
	move.w	#%0000000000000001,sounddma(a0)
	rts

text:
	dc.b	27,'E'
	dc.b	'DMA startup message,',13,10
	dc.b	'Keyclick and Bell replacement.',13,10
	dc.b	'V2.0',13,10
	dc.b	'By Tony Barker (c) 1992.',13,10
	dc.b	'MOVING PIXELS',13,10
	dc.b	'This is SHAREWARE, send $10 Australian',13,10
	dc.b	'or equivalent to.....',13,10
	dc.b	'MOVING PIXELS',13,10
	dc.b	'2/39 Frazer St.',13,10
	dc.b	'Leichhardt, 2040',13,10
	dc.b	'Australia',0
	even
errortext:
	dc.b	'This machine does not have',13,10
	dc.b	'a DMA soundchip.',13,10
	dc.b	'Cannot install.',0
	even
savesup:
	dc.l	0
savesize:	dc.l	0

startsoundfile:		dc.b	'start.spl',0
keysoundfile:		dc.b	'key.spl',0
bellsoundfile:		dc.b	'bell.spl',0
	even
sounds:

