


;;    ---  bsr.s-routines  ---
WB2:					;to save codespace only
	btst #6,(a6)			;bit 14 of DMACONR	;tst.w (a6)
.l:	btst #6,(a6)			;bit 14 of DMACONR
	bne.s .l
	RTS

WEOF:
	BSR.b WB2
	move.w #$12e,d0

WRAS:	;wait for, and DON'T leave (CHANGED 130724!!!), rasterline d0.b
	move.l #$1ff00,d2
	lsl.l #8,d0
	and.l d2,d0
.wr:	move.l 4-C(A6),d1
	and.l d2,d1
	cmp.l d1,d0
	bne.s .wr
	RTS

;;    ---  other routines  ---
*** CalcRnd by Photon of Scoopex ***
*** Inspired by and improved from Park and Miller, 1988 ***
Seed:	dc.l 0
NextRnd:
	move.l Seed(PC),d0
	bsr.s CalcRnd
	move.l d0,Seed-R(A5)
	RTS
Rnd:	;d0=seed, returns new seed in d0 and d1. trashes d2-d3
	move.l 4-C(A6),d1
	rol.l d0,d1
	eor.l d1,d0
CalcRnd:		;d0=seed, returns new seed in d0 and d1. trashes d2-d3
	not.l d0	;make sure it doesnt get stuck on seed 0
	swap d0

	move.l d0,d1
	mulu #16807,d0

	swap d1
	mulu #16807,d1
	addx.l d0,d0	;vital for spatial progression

	move.l d0,d2
	clr.w d2
	swap d2

	move.l d1,d3
	add.l d2,d3	;overlap the partial sum
	and.l #$ffff,d0	;ext.l is ok, but does not traverse ALL 2^32 values.
	add.l d0,d1	;overlap the partial sum

	swap d3
	eor.l d3,d1	;combine (add is not 100% perfect for bitwise fidelity)
	move.l d1,d0	;result in d1 AND d0, new seed
	RTS

FadeRGB:	;fades d0.w to d1.w, with fade 0-127 value in d2. Result in d3.
	clr.w d3		;trashes d0-d1, uses d0-d5
	moveq #3-1,d5
	swap d0
	swap d1
	clr.w d0		;these 4 take 4 words, just like cmp+dbeq.
	clr.w d1
	rol.l #4,d0
	rol.l #4,d1

.rgbl:	clr.w d0
	clr.w d1
	rol.l #4,d0
	rol.l #4,d1
	move.w d1,d4
	sub.w d0,d4
	muls d2,d4
	add.w #63,d4		;round
	asr.w #7,d4
	add.w d0,d4		;add back base:component, 0-15
	lsl.w #4,d3
	or.w d4,d3
	dbf d5,.rgbl
	RTS			;d3.w=result RGBword

SineGen:	;a1=EVEN dest, d0,d1=ampl,prd-1(16x). 25736
	swap d7			;free for loop count
	move.l #25735,d2
	addq.w #1,d1
	divu d1,d2		;smallstep sindy multiplier
	swap d2			;@save

	moveq #1,d4
	swap d4			;=65536
	divu d1,d4		;smallstep ddy
	move.w d4,d2

	subq.w #1,d1

	lea SineGenTbl8bit(PC),a0
	move.w #15,d4		;not moveq, keep hi part
.bigl:	swap d4
	move.w (a0)+,d4		;sindy+siny
	move.w d4,d5
	lsl.w #8,d5		;siny<<8
	clr.b d4		;sindy<<8
	move.w (a0),d6		;sindy2<<8
	clr.b d6
	sub.w d4,d6		;-sindy
	muls d2,d6		;@@ *65536/d1 smallsteps
	swap d6			;=ddy
	swap d2			;other multiplier
	move.w d1,d7		;prd multiplier (x16=period=sample lenghth)
.smll:	moveq #127,d3		;round
	add.w d5,d3		;siny<<8
	muls d0,d3		;@@ normally 256 = asr #8
	swap d3
	add.b d3,(a1)+		;sample byte
	move.w d4,d3		;sindy
	muls d2,d3		;@@ *pi/200 smallstep=*1029/65536=hor. resolution
	swap d3
	add.w d3,d5		;siny=siny+that.
	add.w d6,d4		;sindy=sindy+ddy
	dbf d7,.smll
	swap d2			;other multiplier
	swap d4
	dbf d4,.bigl
	swap d7
	RTS

SineGenTbl8bit:
	dc.b 127,0,118,48,91,89,50,116,2,126,-47,116,-89,89,-117,48
	dc.b -127,0,-110,-48,-91,-88,-50,-116,-2,-126,47,-116,90,-89,117,-48	;mirror
	dc.b 127,0	;extra.

FixedSineGen:			;a1=dest
        lea SineGenTbl8bit(PC),a0
        moveq #15,d7
.bigl:  move.w (a0)+,d0		;sindy+siny
        move.w d0,d1
        lsl.w #8,d1		;siny<<8
        clr.b d0		;sindy<<8
        move.w (a0),d2		;sindy2<<8
        clr.b d2
        sub.w d0,d2		;-sindy
        muls #2621,d2		;*65536/25 bigsteps
        swap d2			;=ddy
        moveq #24,d6
.smll:  moveq #127,d3		;round
        add.w d1,d3		;siny<<8
        asr.w #8,d3		;>>8
        move.b d3,(a1)+		;sample byte
        move.w d0,d3		;sindy
        muls #1029,d3		;*pi/200 smallstep=*1029/65536=hor. resolution
        swap d3
        add.w d3,d1		;siny=siny+that.
        add.w d2,d0		;sindy=sindy+ddy
        dbf d6,.smll
        dbf d7,.bigl
	RTS

StartSound:	;a0=start,d0,d1,d2,d3=chan,len,prd,vol (.w). Uses d4-d5,a1
	moveq #0,d4
	bset d0,d4
	lsl.w #4,d0
	lea $a0-C(A6),a1
	add.w d0,a1		;dst

	move.w d4,$96-C(A6)	;sound off

	move.b 6-C(A6),d5
	addq.w #4,d5
.wl:	cmp.b 6-C(A6),d5
	bne.s .wl

	move.l a0,(a1)+
	movem.w d1-d3,(a1)
;	move.w #$ff,$9e-C(A6)	;no modulation


	or.w #$8200,d4
	move.w d4,$96-C(A6)	;sound on
	RTS

StopSound:	;d0=chan 0-3	;uses d4
	moveq #0,d4
	bset d0,d4
	move.w d4,$96-C(A6)	;sound off
	RTS


