*
*		- SoundTracker pro playroutine -
*
*  To use this playroutine you have to take the following steps:
*
*  -	At the end of this source you will see labels named
*	song,sample1,sample2,...,sample15, followed by 
*	blk.b $.
*	Fill in the length in bytes of the songdata and the samples
*	behind the $-sign
*	If you don't use one of the samples set its length to zero
*
*  -	Assemble the source
*
*  -    Load the song and the samples with the ri command:
*	SEKA>ri
*	FILENAME>songname
*	START>song
*	END>
*	SEKA>ri
*	FILENAME>sample1
*	START>sample1
*	END>
*	SEKA>
*		etc...
*
*  -	Write the object file with the wo command.
*
*		READY !
*	(make sure you load the samples into chip mem)
*
*	To all who want to use this playroutine as part of their
*	own program:
*
*	Because Soundtracker pro uses speeds with a resolution of 1/4
*	the subroutine 'playmusic' must be called four times per
*	display build-up.
*	You should spread these four times evenly,so for PAL-mode
*	this would mean the vertical blanking area and raster lines
*	$4f,$9e and $ed
*
*	If you don't use speeds of 1/4 or 3/4 you can call the routine
*	twice during the vertical blanking period,and twice at raster
*	line $9e.
*
*	If you don't use speeds of 1/4,1/2 or 3/4 you can call the
*	routine four times during the vertical blanking period,
*	or any other raster line
*	Only one of these four times will the notes actually be played
*	so your interupt code can still be fast.
*
*	Before playing the music you must call 'initmusic'



start:	bsr	initmusic
	move.l	$6c,a0
	move.l	$2230,a1
	move.l	#level3int,$6c
waitforbutton:
	btst	#10,$dff016			; RIGHT button
	bne	waitforbutton
	move.l	a0,$6c
	move.l	a1,$2230
	move.w	#$000f,$dff096			; DMA off
	rts

********	actually you should use graphicsbase+$32
********	instead of $2230.

initmusic:
	move.w	#$0400,label72
	clr.l	d0
	move.b	song+$1d7,d0
	cmp.b	#64,d0
	bgt	lengthok
	mulu	#$10,d0
	move.w	d0,label72
lengthok:
	move.l	#6,label64
	clr.w	label66
	clr.l	label68
	clr.l	label69
	lea	$dff0a8,a0
	clr.w	(a0)
	clr.w	$10(a0)
	clr.w	$20(a0)
	clr.w	$30(a0)

	lea	label73,a0
	move.l	#15,d0
clearloop:
	move.l	(a0)+,a1
	clr.l	(a1)
	dbf	d0,clearloop
	rts

coplist:
	dc.w $0180,0
	dc.w $4f01,$ff00
	dc.w $009a,$8010
	dc.w $009c,$8010

	dc.w $5001,$ff00
	dc.w $0180,$0800
	dc.w $5101,$ff00
	dc.w $0180,$0c00
	dc.w $5201,$ff00
	dc.w $0180,$0f00
	dc.w $5301,$ff00
	dc.w $0180,$0c00
	dc.w $5401,$ff00
	dc.w $0180,$0800
	dc.w $5501,$ff00
	dc.w $0180,0


	dc.w $9e01,$ff00
	dc.w $009a,$8010
	dc.w $009c,$8010

	dc.w $9f01,$ff00
	dc.w $0180,$0555
	dc.w $a001,$ff00
	dc.w $0180,$0999
	dc.w $a101,$ff00
	dc.w $0180,$0ccc
	dc.w $a201,$ff00
	dc.w $0180,$0999
	dc.w $a301,$ff00
	dc.w $0180,$0555
	dc.w $a401,$ff00
	dc.w $0180,0


	dc.w $ed01,$ff00
	dc.w $009a,$8010
	dc.w $009c,$8010

	dc.w $ee01,$ff00
	dc.w $0180,$0008
	dc.w $ef01,$ff00
	dc.w $0180,$000b
	dc.w $f001,$ff00
	dc.w $0180,$000f
	dc.w $f101,$ff00
	dc.w $0180,$000b
	dc.w $f201,$ff00
	dc.w $0180,$0008
	dc.w $f301,$ff00
	dc.w $0180,0
	dc.w $ffff,$fffe

level3int:
	move.l	#coplist,$2230
	MOVEM.L D0-D1/A0-A1/A5-A6,-(A7)
	LEA     $00DFF000,A0
	MOVE.L	$00000004,A6		
	MOVE.W	$001C(A0),D1
	BTST	#$0E,D1
	BEQ     level3end
	AND.W   $001E(A0),D1
	BTST    #$06,D1
	BEQ     blitternotfinished
	MOVEM.L $009C(A6),A1/A5
	PEA     -$0024(A6)
	JMP     (A5)
blitternotfinished:
	BTST	#$05,D1
	BEQ	noblank
	MOVEM.L	D0-D7/A0-A6,-(A7)
	bsr	playmusic
	MOVEM.L	(A7)+,D0-D7/A0-A6
	MOVEM.L	$0090(A6),A1/A5
	PEA	-$0024(A6)
	JMP	(A5)
noblank:BTST	#$04,D1
	BEQ	level3end
	MOVEM.L	D0-D7/A0-A6,-(A7)
	bsr	playmusic
	movem.l	(a7)+,d0-d7/a0-a6
	MOVEM.L	$0084(A6),A1/A5
	PEA	-$0024(A6)
	JMP	(A5)
level3end:
	MOVEM.L (A7)+,D0-D1/A0-A1/A5-A6
	RTE


playmusic:
	subq.w	#1,label64
	bpl	label1
	move.w	#3,label64
	bra	label2
label1:	rts
label2:	tst.w	label70
	bne	label3
	bsr	label38
label3:	addq.w	#1,label70
	move.w	label70,d0
	cmp.w	label65,d0
	blt.s	label4
	clr.w	label70
	move.w	label66,d0
	add.w	d0,label64
	bra.L	label35
label4:	lea	label74,a6
	move.l	#$dff0a0,a5
	bsr	label10
	tst.b	3(a6)
	beq.s	label5
	bsr.s	label9
label5:	lea	label75,a6
	move.l	#$dff0b0,a5
	bsr	label10
	tst.b	3(a6)
	beq.s	label6
	bsr.s	label9
label6:	lea	label76,a6
	move.l	#$dff0c0,a5
	bsr	label10
	tst.b	3(a6)
	beq.s	label7
	bsr.s	label9
label7:	lea	label77,a6
	move.l	#$dff0d0,a5
	bsr	label10
	tst.b	3(a6)
	beq.s	label8
	bsr.s	label9
label8:	bra.L	label44
label9:	move.b	2(a6),d0
	and.b	#$0f,d0
	tst.b	d0
	beq.L	label28
	cmp.b	#1,d0
	beq	label17
	cmp.b	#2,d0
	beq	label21
	rts
label10:btst	#2,26(a6)
	bne	label16
	btst	#3,26(a6)
	bne	label20
	rts
label11:move.b	3(a6),19(a6)
	rts
label12:and.w	#$00ff,d0
	mulu	label67,d0
	lsr.l	#6,d0
	move.w	d0,8(a5)
	rts
label13:move.b	3(a6),d0
	lsr.w	#4,d0
	and.w	#$f,d0
	cmp.w	#2,d0
	blt	label14
	move.w	d0,label65
	move.b	3(a6),d0
	and.w	#$3,d0
	move.w	d0,label66
label14:rts
label15:move.b	3(a6),d0
	and.b	#$01,d0
	lsl.b	#1,d0
	eor.b	#2,d0
	and.b	#$fd,$bfe001
	or.b	d0,$bfe001
	rts
label16:clr.w	d0
	move.b	24(a6),d0
	bra	label18
label17:and.b	#%11110000,26(a6)
	clr.w	d0
	move.b	3(a6),d0
label18:sub.w	d0,22(a6)
	cmp.w	#$71,22(a6)
	bpl.s	label19
	move.w	#$71,22(a6)
label19:move.w	22(a6),6(a5)
	rts
label20:clr.w	d0
	move.b	24(a6),d0
	bra	label22
label21:and.b	#%11110000,26(a6)
	clr.w	d0
	move.b	3(a6),d0
label22:add.w	d0,22(a6)
	cmp.w	#$358,22(a6)
	bmi.s	label23
	move.w	#$358,22(a6)
label23:move.w	22(a6),6(a5)
	rts
label24:clr.w	d0
	move.b	3(a6),d0
label25:move.w	d0,d2
	lsr.b	#4,d0
	and.b	#$0f,d2
	add.w	d2,18(a6)
	sub.w	d0,18(a6)
	bmi	label27
	cmp.w	#$40,18(a6)
	ble	label26
	move.w	#$40,18(a6)
label26:rts
label27:clr.w	18(a6)
	rts
label28:cmp.w	#1,label70
	beq.s	label29
	cmp.w	#2,label70
	beq.s	label30
	cmp.w	#3,label70
	beq.s	label31
	cmp.w	#4,label70
	beq.s	label29
	cmp.w	#5,label70
	beq.s	label30
	rts
label29:clr.l	d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	bra.s	label32
label30:clr.l	d0
	move.b	3(a6),d0
	and.b	#$0f,d0
	bra.s	label32
label31:move.w	16(a6),d2
	bra.s	label34
label32:lsl.w	#1,d0
	move.w	16(a6),d3
	lea	label78,a0
label33:move.w	(a0,d0),d2
	cmp.w	(a0)+,d3
	bne	label33
label34:move.w	d2,6(a5)
	rts
label35:lea	song,a0
	move.l	a0,a3
	add.l	#$0c,a3
	move.l	a0,a2
	add.l	#$1d8,a2
	add.l	#$258,a0 
	move.l	label69,d0
	clr.l	d1
	move.b	(a2,d0),d1
	mulu	label72,d1
	add.l	label68,d1
	clr.w	label71
	move.l	#$dff0a0,a5
	lea	label74,a6
	bsr.L	label45
	move.l	#$dff0b0,a5
	lea	label75,a6
	bsr.L	label45
	move.l	#$dff0c0,a5
	lea	label76,a6
	bsr.L	label45
	move.l	#$dff0d0,a5
	lea	label77,a6
	bsr.L	label45
	move.b	label77+27,d0
	beq	label36
	bsr	label61
label36:bsr	label59
	move.l #$1f4,d0
label37:dbf	d0,label37
	move.w	label71,d0
	or.w	#$8000,d0
	move.w	d0,$dff096
	bra     label43
label38:lea	label77,a6
	tst.w	(a6)
	beq	label39
	move.w	14(a6),$dff0d4
	cmp.w   #1,14(a6)
	beq	label39
	move.l	10(a6),$dff0d0
label39:lea	label76,a6
	tst.w	(a6)
	beq	label40
	move.w	14(a6),$dff0c4
	cmp.w   #1,14(a6)
	beq	label40
	move.l	10(a6),$dff0c0
label40:lea	label75,a6
	tst.w	(a6)
	beq	label41
	move.w	14(a6),$dff0b4
	cmp.w   #1,14(a6)
	beq	label41 
	move.l	10(a6),$dff0b0
label41:lea	label74,a6
	tst.w	(a6)
	beq	label42
	move.w	14(a6),$dff0a4
	cmp.w   #1,14(a6)
	beq	label42
	move.l	10(a6),$dff0a0
label42:rts
label43:move.l	label68,d0
	add.l	#$10,d0
	move.l	d0,label68
	cmp.w	label72,d0
	bne.s	label44
	clr.l	label68
	addq.l	#1,label69
	clr.l	d0
	move.b	song+$1d6,d0
	move.l	label69,d1
	cmp.l	d0,d1
	blt.s	label44
	clr.l	label69
label44:rts
label45:move.l	(a0,d1),(a6)
	addq.l	#4,d1
	clr.l	d2
	move.b	2(a6),d2 
	lsr.b	#4,d2
	tst.b	d2
	beq.s	label46
	clr.l	d3
	lea	label73-4,a1
	move.l	d2,d4 
	mulu	#4,d2
	mulu	#$1e,d4
	move.l	(a1,d2),4(a6)
	move.w	(a3,d4),8(a6)
	move.w	2(a3,d4),18(a6)
	clr.b	25(a6)
	move.w	4(a3,d4),d3
	move.l	4(a6),d2
	add.l	d3,d2
	move.l	d2,10(a6)
	move.w	6(a3,d4),14(a6)
label46:tst.w	(a6)
	beq.s	label47
	move.w	(a6),16(a6)
	move.w	(a6),22(a6)
	and.b	#%11110000,26(a6)
	move.w	20(a6),$dff096
	move.l	4(a6),(a5)
	move.w	8(a6),4(a5)
	move.w	(a6),6(a5)
	move.w	20(a6),d0
	or.w	d0,label71
label47:move.w	20(a6),d0
	lsl.w	#4,d0
	add.w	20(a6),d0
	move.w	d0,$dff09e
	bsr	label49
	clr.w	d0
	move.b	25(a6),d0
	beq	label48
	bsr	label25
label48:btst	#0,26(a6)
	bne	label16
	btst	#1,26(a6)
	bne	label20
	rts
label49:move.b	2(a6),d0
	and.b	#$0f,d0
	cmp.b	#3,d0
	beq	label51
	cmp.b	#4,d0
	beq	label52
	cmp.b	#5,d0
	beq	label53
	cmp.b	#6,d0
	beq	label54
	cmp.b	#7,d0
	beq	label57
	cmp.b	#8,d0
	beq	label60
	cmp.b	#9,d0
	beq	label17
	cmp.b	#$a,d0
	beq	label21
	cmp.b	#$b,d0
	beq	label50
	cmp.b	#$c,d0
	beq	label11
	cmp.b	#$d,d0
	beq	label24
	cmp.b	#$e,d0
	beq	label15
	cmp.b	#$f,d0
	beq	label13
	rts
label50:move.b	3(a6),25(a6)
	rts
label51:move.b	#$0001,d0
	bra	label55
label52:move.b	#%0010,d0
	bra	label55
label53:move.b	#%0101,d0
	bra	label55
label54:move.b	#%1010,d0
label55:and.b	#$f0,26(a6)
	or.b	d0,26(a6)
	move.b	3(a6),24(a6)
	bne	label56
	clr.b	26(a6)
label56:rts
label57:clr.w	d0
	move.b	3(a6),d0
	cmp.w	#$40,d0
	ble	label58
	move.w	#$40,d0
label58:move.w	d0,label67
	clr.b	label77+27
	rts
label59:move.l	#$dff0a0,a5
	move.w	label74+18,d0
	bsr	label12
	move.l	#$dff0b0,a5
	move.w	label75+18,d0
	bsr	label12
	move.l	#$dff0c0,a5
	move.w	label76+18,d0
	bsr	label12
	move.l	#$dff0d0,a5
	move.w	label77+18,d0
	bsr	label12
	rts
label60:move.b	3(a6),label77+27
	rts
label61:clr.w	d0
	move.b	label77+27,d0
	move.w	d0,d2
	lsr.b	#4,d0
	and.b	#$0f,d2
	add.w	d2,label67
	sub.w	d0,label67
	bmi	label63
	cmp.w	#$40,label67
	ble	label62
	move.w	#$40,label67
label62:rts
label63:clr.w	label67
	rts
label64:dc.w 0
label65:dc.w 6
label66:dc.w 0
label67:dc.w $40
label68:dc.l 0
label69:dc.l 0
label70:dc.w 0
label71:dc.w 0
label72:dc.w $400
label73:dc.l	sample1
	dc.l	sample2
	dc.l	sample3
	dc.l	sample4
	dc.l	sample5
	dc.l	sample6
	dc.l	sample7
	dc.l	sample8
	dc.l	sample9
	dc.l	sample10
	dc.l	sample11
	dc.l	sample12
	dc.l	sample13
	dc.l	sample14
	dc.l	sample15
label74:dc.l 0,0,0,0
	dc.w 0,0,1,0,0,0
label75:dc.l 0,0,0,0
	dc.w 0,0,2,0,0,0
label76:dc.l 0,0,0,0
	dc.w 0,0,4,0,0,0
label77:dc.l 0,0,0,0
	dc.w 0,0,8,0,0,0
label78:dc.w $0358,$0328,$02FA,$02D0,$02A6,$0280,$025C,$023A
	dc.w $021A,$01FC,$01E0,$01C5,$01AC,$0194,$017D,$0168
	dc.w $0153,$0140,$012E,$011D,$010D,$00FE,$00F0,$00E2
	dc.w $00D6,$00CA,$00BE,$00B4,$00AA,$00A0,$0097,$008F
	dc.w $0087,$007F,$0078,$0071,$0000
song:			blk.b	$
even				;
	sample1:	blk.b	$
even				;
	sample2:	blk.b	$
even				;
	sample3:	blk.b	$
even				;
	sample4:	blk.b	$
even				;
	sample5:	blk.b	$
even				;
	sample6:	blk.b	$
even				;
	sample7:	blk.b	$
even				;
	sample8:	blk.b	$
even				;
	sample9:	blk.b	$
even				;
	sample10:	blk.b	$
even				;
	sample11:	blk.b	$
even				;
	sample12:	blk.b	$
even				;
	sample13:	blk.b	$
even				;
	sample14:	blk.b	$
even				;
	sample15:	blk.b	$

