; Boot beep routine
;
; Called with RTS6 A3 must be set up w/ memory offset (overlay)
; D3 contains the duration: 40 is boot beep

; make an attractive beep to tell the world that the CPU works. Use
; Charlie Kellner's filter algorithm. First fill the waveForm buffer
; with the initial waveForm.

BootBeep
MOVE.L #VBase,A5 ; get the VIA base address
BCLR #7,VBufB(A5) ;enable the sound

LEA SoundLow,A0 ; get sound buffer address
ADD.L A3,A0 ; overlay bit is on, so offset
MOVE.L A0,A4 ; keep soundBase in A4

MOVEQ #4,D0 ; repeat 74 byte sequence 5 times
@3
MOVE.L #$FA40C006,D1 ; 4 byte table to fill buffer with
@2
MOVEQ #19,D2 ; repeat 19 times
@1
MOVE.B D1,(A0) ; move in a byte
ADDQ #2,A0 ; bump to next buffer location
SUBQ #1,D2 ; done 19 times yet?
BNE.S @1 ; if not, loop

LSR.L #8,D1 ; get next byte value
BNE.S @2 ; fill next 18 values

DBRA D0,@3 ; repeat the whole sequence 5 times

; OK, now filter it for a nice fade -- repeat the filtering process D3 times
; note that D1 and D2 are zero at this point

FLoop0
MOVE.L A4,A0 ; point A0 to start of buffer
LEA 146(A0),A1 ; point A1 74 values in
MOVEQ #73,D0 ; process 74 samples
FLoop1
MOVE.B (A1),D2 ; get 74th value
ADDQ #4,A1 ; bump to 76th value
MOVE.B (A1),D1 ; make it a word value
ADD.W D1,D2 ; add to the accumulator
SUBQ #2,A1 ; point at 75th value
MOVE.B (A1),D1 ; get it
ADD.W D1,D2 ; add to accumulator
ADD.W D1,D2 ; count it twice

ADDQ #3,D2 ; add 3 to round off
ASR #2,D2 ; divide it by 4
MOVE.B D2,(A0) ; store it away
ADDQ #2,A0 ; bump to next value
DBRA D0,FLoop1 ; loop for all 74 values

; now copy first 74 values into the rest of the buffer

FillIt
MOVE #295,D0 ; 296 values to copy
FLoop2
MOVE.B -74(A0),(A0) ; move in next value
ADDQ #2,A0
DBRA D0,FLoop2 ; loop till done

; now wait for blanking before repeating

EndLoop
MOVE.W #5500,D0 ; init timeout factor
@1
BTST #1,VIFR(A5) ; is VBL on?
DBNE D0,@1 ; wait for it to come on
MOVE.B #$02,VIFR(A5) ; mask it off

DBRA D3,FLoop0 ; repeat D3 times

; all done with our boot beep so turn down the sound

BSET #7,VBufB(A5) ;disable the sound

RTS6