soundstarth:	equ	2
soundstartm:	equ	4
soundstartl:	equ	6
soundendh:	equ	$e
soundendm:	equ	$10
soundendl:	equ	$12
soundmode:	equ	$20
sounddma:	equ	0
*****************************************************************
*	OK here we go with an idea from Steve Walter??		*
*	Steals the Trap #2 (GEM) vector either from an auto	*
*	folder or when run from the desktop.			*
*	All it does is check to see if the routine to be	*
*	executed is an alert, if so see which symbol has been	*
*	requested and make a sound appropriate to each.		*
*	then just let GEM do it's stuff as normal. Should be	*
*	totally transparent to all but the most cantankerous	*
*	of programs.						*
*	icons:	0=noicon					*
*		1=NOTE						*
*		2=WAIT						*
*		3=STOP						*
*****************************************************************
*  Program initialization
start:
        move.l  #prog_end,d6    ; Get address of end of this program
        sub.l   4(sp),d6        ; Subtract start of basepage - save in d6

checkdmasound:
	bsr	dc
	pea     cdma(pc)   ; Steal trap #2 right away if run from desktop
        move    #38,-(sp)       ; Supexec
        trap    #14
        addq    #6,sp
	cmpi.w	#1,dma
	beq.s	allok

        move.l  #nodma,addrin ; Try to do an alert box
        move    #1,intin
        move.l  #contrl,aespb
        move.l  #aespb,d1
        move    #$C8,d0
        trap    #2
        tst     intout          ; If intout is zero, we're in \AUTO
        beq.s   autoerror
        clr     -(sp)           ; Pterm0
        trap    #1              ; outta here
autoerror:
        pea     nodmamessage           ; Print title message
        move    #9,-(sp)
        trap    #1
        addq    #6,sp
        clr     -(sp)           ; Pterm0
        trap    #1              ; outta here


allok:
	pea     dintro(pc)	;this starts the intro sound
        move    #38,-(sp)       ; Supexec
        trap    #14
        addq    #6,sp

        move.l  #not_auto,addrin ; Try to do an alert box
        move    #1,intin
        move.l  #contrl,aespb
        move.l  #aespb,d1
        move    #$C8,d0
        trap    #2
        tst     intout          ; If intout is zero, we're in \AUTO
        beq.s   start1

        cmp     #1,intout       ; Install?
        beq.s   cont0              ; Yes, continue

        clr     -(sp)           ; Pterm0
        trap    #1              ; outta here

cont0:
	pea     prg_start(pc)   ; Steal trap #2 right away if run from desktop
        move    #38,-(sp)       ; Supexec
        trap    #14
        addq    #6,sp

        move    #1,prgflg       ; Set flag indicating desktop load
        bra.s   start2

start1:
        pea     title           ; Print title message
        move    #9,-(sp)
        trap    #1
        addq    #6,sp
start2:
	pea     waitsound(pc)	;wait until intro sound finished
        move    #38,-(sp)       ; Supexec
        trap    #14
        addq    #6,sp

	pea     putcookie(pc)	;put the cookie in the jar
        move    #38,-(sp)       ; Supexec
        trap    #14
        addq    #6,sp


        pea     set_bios(pc)    ; Appropriate the Trap #13 vector
        move    #38,-(sp)
        trap    #14
        addq.l  #6,sp

        clr.w   -(sp)           ; Terminate and Stay Resident
        move.l  d6,-(sp)        ; Number of bytes to keep
        move    #$31,-(sp)      ; That's all folks!
        trap    #1              ; We are now happily resident in RAM

* -------------------------------
*  Desktop vector initialization
* -------------------------------

prg_start:
        move.l  $88,fallthrough+2      ; Set my fall throughs
        move.l  $88,oldvec      ; Set XBRA

*        move.l  $88,aesvec
        move.l  #mytrap2,$88   ; Steal trap #2 (GEM)
        rts

* -----------------------
*  Steal the BIOS vector
* -----------------------

set_bios:
        move.l  $B4,t13adr      ; Set Bios fall through
        move.l  #my_t13,$B4     ; Steal trap #13 (BIOS)
        rts

* ------------------------
*  Trap #13 wedge routine
* ------------------------

my_t13:
        btst    #5,(sp)         ; Was the trap called from super or user mode?
        beq.s   t13_ex          ; If from user mode, bail out

        lea     6(sp),a0        ; Pointer to function code on stack

        tst     $59E            ; See what _longframe has to tell us
        beq.s   notlng          ; If _longframe is zero, it's a 68000

        lea     8(sp),a0        ; Advance past the vector offset word

***  This section is based on the assumption that the OS always calls
***  BIOS setexec() immediately after obnoxiously grabbing back the trap
***  #2 vector with no warning whatsoever.  Yes, this is an empirical
***  condition, which violates Commandment IX.  (But there's no other
***  way to prevent that no-good, thieving TOS from ripping off the
***  vector while you aren't looking.)

notlng: cmp.l   #$050101,(a0)   ; Setexec call for critical error vector?
        bne.s   t13_ex          ; Nope, exit

        tst     prgflg          ; On the desktop? Or are vectors already set?
        beq.s   first_time      ; No, skip ahead

do_crit:
        move.l  #mytrap2,$88   ; Pilfer trap #2
        move.l  $404,d0         ; Get current crit vector
        move.l  4(a0),d1        ; Get address we're setting it to
        bmi.s   t13_x1          ; If minus, return old vector in d0
        move.l  d1,$404         ; Set that vector
t13_x1: rte                     ; We only get here if we're last in the chain

first_time:
        tst.l   4(a0)           ; Reading the vector?
        bmi.s   t13_ex          ; Yes, let the system take care of it

        move.l  $4F2,a1         ; Get address of OS header (could be in RAM)
        move.l  8(a1),a1        ; Get pointer to base of OS from header
        cmp.l   4(a0),a1        ; Is the crit error routine below the OS?
        bhi.s   t13_ex          ; Yes, bail out
        move.l  $14(a1),a1      ; Get address of end of OS (GEMDOS parm block)
        cmp.l   4(a0),a1        ; Is it above the OS?
        blo.s   t13_ex          ; Yes, exit stage left

***  This is a very important part of the code.  In order to maintain the
***  correct vector chaining order when running at \AUTO time, it's necessary
***  that each program first fall through to the BIOS and RETURN TO ITS OWN
***  CODE, grabbing the trap #2 vector on the way back.  This way, the order
***  that each program intercepts trap #2 is the same as the order in which
***  they run from the AUTO folder.

        move    #1,prgflg       ; Set the 'first-time'/'desktop' flag
        move.l  2(sp),retsav    ; Save return address
        move.l  #t13_2,2(sp)    ; Replace it with my own
t13_ex: jmp     $0.L       ; Go to the Bios and come back,
t13adr  =       t13_ex+2        ;   maintaining the correct chaining order

t13_2:  bsr     prg_start       ; Grab the trap #2 vector on the way back
        move.l  retsav(pc),-(sp) ; And return to the caller
        rts

retsav: dc.l    0


*****************************************************************
*	This guy does the dirty work, checks to see if its	*
*	a form alert and makes a sound based on which icon was	*
*	requested.						*
*****************************************************************
	dc.l	'XBRA'
	dc.l	'NALT'
oldvec:	dc.l	0

mytrap2:
	move.l	a0,savea0
	cmpi.w	#$c8,d0		;was it aes or vdi
	bne	fallthrougha	;nope so back to the real thing
	move.l	d1,a0		;get aespb
	move.l	(a0),a0		;get contrl
	cmpi.w	#52,(a0)
	bne	fallthrougha
	move.l	d1,a0
	lea	16(a0),a0
	move.l	(a0),a0
	move.l	(a0),a0
	cmpi.b	#'0',1(a0)
	beq.s	donoiconsound
	cmpi.b	#'1',1(a0)
	beq.s	donotesound
	cmpi.b	#'2',1(a0)
	beq.s	dowaitsound
	cmpi.b	#'3',1(a0)
	beq.s	dostopsound
fallthrougha:
	move.l	savea0,a0
fallthrough:
	jmp	$0.L



dintro:
	move.l	#introstart,d0
	move.l	#introend,d1
	bsr	dosound
	rts


donoiconsound:
	movem.l	d0-d1,saveds
	move.l	#alsound1start,d0
	move.l	#alsound1end,d1
	bsr.s	dosound
	movem.l	saveds,d0-d1
	bra.s	fallthrougha
donotesound:
	movem.l	d0-d1,saveds
	move.l	#alsound2start,d0
	move.l	#alsound2end,d1
	bsr.s	dosound
	movem.l	saveds,d0-d1
	bra.s	fallthrougha
dowaitsound:
	movem.l	d0-d1,saveds
	move.l	#alsound3start,d0
	move.l	#alsound3end,d1
	bsr.s	dosound
	movem.l	saveds,d0-d1
	bra.s	fallthrougha
dostopsound:
	movem.l	d0-d1,saveds
	move.l	#alsound4start,d0
	move.l	#alsound4end,d1
	bsr.s	dosound
	movem.l	saveds,d0-d1
	bra	fallthrougha

*****************************************
*	new sound chip stuff		*
*	pass sound start in d0		*
*	and sound end in d1		*
*****************************************
dosound:
	move.l	flagaddress,a0
	tst.l	(a0)
	bmi	skipsound
	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)
skipsound:
	rts

savea0:		dc.l	0
saveds:		dc.l	0,0
dma:		dc.w	0

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


putcookie:
	move.l	$5a0,a0
	moveq	#1,d7		;cookie counter
pcloop:
	move.l	(a0)+,d0
	beq	foundlast
	addq.l	#4,a0
	addq.w	#1,d7
	bra	pcloop
foundlast:
	move.l	(a0),d6
	cmp.l	d6,d7
	beq	noroom
	move.l	#'SMC1',-4(a0)	;thats our cookie
	move.l	a0,flagaddress
	move.l	#1,(a0)+	;sound flag 1=on
	move.l	#0,(a0)+
	move.l	d6,(a0)+	;copy the last cookie thing
noroom:
	rts		;no room for our cookie

flagaddress:	dc.l	fd

fd:	dc.l	1

waitsound:
	tst.b	$ff8901
	bne.s	waitsound
	rts

dc:
	lea	dcstart,a0
	lea	dcend,a1
dcloop:
	sub.b	#12,(a0)+
	cmp.l	a0,a1
	bne	dcloop
	rts


dcstart:
not_auto:
	incbin	'c1.img'
*	dc.b	'[0]'
*	dc.b	'['
*	dc.b	'      SOUND ALERT 1.5|'
*	dc.b	' Programming by Tony Barker  |'
*	dc.b	189,17,25,25,17
*	dc.b	' Stephens Music Centre  |'
*	dc.b	'   The Atari Specialists|'
*	dc.b	'(047)51-6196 & (02)637-3128  ]'
*	dc.b	'[Install me|Forget it]',0,0
	even
nodma:
	incbin	'c2.img'
*	dc.b	'[3][SOUND ALERT 1.5|'
*	dc.b	'Programming by Tony Barker|'
*	dc.b	189,'1991 by Stephens Music Centre|'
*	dc.b	'No DMA soundchip detected.|'
*	dc.b	'Unable to install.]'
*	dc.b	'[Sorry]',0,0
	even
title:
	incbin	'c3.img'
*	dc.b	27,'E',$0d,$0a
*	dc.b	27,'p',' Sound Alert 1.5',$0d,$0a,$d,$a
*	dc.b	27,'q',' Programming by Tony Barker.',$0d,$0a
*	dc.b	' (C)1991 by Stephens Music Centre',$d,$a,$d,$a
*	dc.b	' The Australian Specialists in',$d,$a
*	dc.b	' ATARI Software & Hardware.',$d,$a,$d,$a
*	dc.b	' Call us for our FREE newsletter',$d,$a
*	dc.b	' (047)51-6196',$d,$a
*	dc.b	' (02)637-3128',0,0
	even
nodmamessage:
	incbin	'c4.img'
*	dc.b	27,'E',$0d,$0a
*	dc.b	27,'p',' Sound Alert 1.5',$0d,$0a,$d,$a
*	dc.b	' Programming by Tony Barker.',$0d,$0a
*	dc.b	' (C)1991 by Stephens Music Centre',$d,$a,$d,$a
*	dc.b	' (047)51-6196',$d,$a
*	dc.b	' (02)637-3128',$d,$a,$d,$a
*	dc.b	' This machine does not have a DMA',$d,$a
*	dc.b	' Soundchip. Sound Alert will not',$d,$a
*	dc.b	' be installed.',27,'q',0,0
	even
dcend:

aespb:
	dc.l	contrl,global,intin,intout,addrin,addrout
vdipb:
	dc.l	contrl,intin,ptsin,intout,ptsout


*********************************************************
*	Note, contrl is set up here for forma alert	*
*********************************************************
contrl:
opcode:		dc.w	52
sintin:		dc.w	1
sintout:	dc.w	1
saddrin:	dc.w	1
saddrout:	dc.l	0
		dc.w	0,0,0,0,0
global:
apversion:	dc.w	0
apcount:	dc.w	0
apid:		dc.w	0
apprivate:	dc.l	0
apptree:	dc.l	0
ap1resv:	dc.l	0
ap2resv:	dc.l	0
ap3resv:	dc.l	0
ap4resv:	dc.l	0

intin:
	dc.w	1
	ds.w	126
ptsin:
	ds.w	128
intout:
	ds.w	128
ptsout:
	ds.w	128
addrin:
	ds.w	128
addrout:
	ds.w	128
grhandle:	dc.w	0
prgflg:		dc.w	0
alsound1start:
	incbin	'stest2.spl'
alsound1end:
alsound2start:
	incbin	'plsnote.spl'
alsound2end:
alsound3start:
	incbin	'plsdcide.spl'
alsound3end:
alsound4start:
	incbin	'plsstop.spl'
alsound4end:
prog_end:
introstart:
	incbin	'intro1.spl'
introend:



