*************************************************
*                                               *
*          Soundtracker Replay-Routine          *
*        Version 24  - Update 15.12.1987        *
*						*
*        Written 1987  by Karsten Obarski   	*
*      3000 Hannover 61  Anderter Strasse 1	*
*                                               *
*               start   =   START               *
*          breakpoint   =   END                 *
*                bugs   =   not known           *
*      images to read   =   see bottom          *
*************************************************

*************************************************
* demorun
*************************************************
START:	move.w	#$4000,$dff09a		;no irq
	JSR	REPLAY_INIT		;for startsettings
	JSR	REPLAY_ENABLE		;to start song on top

main:	move.w	$dff01e,d0		;request
	btst	#5,d0			;frame flop?
	beq.s	main			;no
	move.w	#$20,$dff09c		;clear request
	JSR	REPLAY_SONG		;jump continiously here.
					;use only the enable or
					;disable function to switch
					;on or off
	btst	#6,$bfe001
	bne.s	main
	JSR	REPLAY_DISABLE		;to pause or stop replay

	;here now time to do some else
	;after that perhaps REPLAY_ENABLE to start again,
	;or nothing to aboart program

	move.w	#$c000,$dff09a
END:	rts

*************************************************
* begin of muzak replay module  
*************************************************

*------------------------------------------------
REPLAY_INIT:
	move.l	#snd1,sounds+0
	move.l	#snd2,sounds+4
	move.l	#snd3,sounds+8
	move.l	#snd4,sounds+12
	move.l	#snd5,sounds+16
	move.l	#snd6,sounds+20
	move.l	#snd7,sounds+24
	move.l	#snd8,sounds+28
	move.l	#snd9,sounds+32
	move.l	#snda,sounds+36
	move.l	#sndb,sounds+40
	move.l	#sndc,sounds+44
	move.l	#sndd,sounds+48
	move.l	#snde,sounds+52
	move.l	#sndf,sounds+56
	move.l	#sounds,a0
	clr.l	d0
rint1:	move.l	(a0,d0.l),a1
	clr.l	(a1)
	addq.l	#4,d0
	cmp.l	#60,d0
	bne	rint1
	lea.l	muzakdata,a0
	move.b	470(a0),numpat+1
	rts

*------------------------------------------------
REPLAY_ENABLE:
	move.w	#-1,plamod
	move.w	#00,$dff0a8
	move.w	#00,$dff0b8
	move.w	#00,$dff0c8
	move.w	#00,$dff0d8
	clr.w	timpos
	clr.l	trapos
	clr.l	patpos	
	rts

*------------------------------------------------
REPLAY_DISABLE:
	clr.w	plamod
	move.w	#00,$dff0a8
	move.w	#00,$dff0b8
	move.w	#00,$dff0c8
	move.w	#00,$dff0d8
	move.w	#$f,$dff096
	rts

*------------------------------------------------
REPLAY_SONG:
	movem.l	d0-d7/a0-a6,-(a7)
	cmp.w	#0,plamod
	beq.s	rps1
	addq.w	#1,timpos
	cmp.w	#6,timpos
	bne.s	effecthandler
	clr.w	timpos
	bra 	replayhandler
rps1:	movem.l	(a7)+,d0-d7/a0-a6
	rts

*------------------------------------------------
effecthandler:
	lea.l	datach0,a6
	cmp.b	#0,3(a6)
	beq.s	eff1
	move.l	#$dff0a0,a5
	bsr.s	eff5
eff1:	lea.l	datach1,a6
	cmp.b	#0,3(a6)
	beq.s	eff2
	move.l	#$dff0b0,a5
	bsr.s	eff5
eff2:	lea.l	datach2,a6
	cmp.b	#0,3(a6)
	beq.s	eff3
	move.l	#$dff0c0,a5
	bsr.s	eff5
eff3:	lea.l	datach3,a6
	cmp.b	#0,3(a6)
	beq.s	eff4
	move.l	#$dff0d0,a5
	bsr.s	eff5
eff4:	movem.l	(a7)+,d0-d7/a0-a6
	rts
eff5:	move.b	2(a6),d0
	and.b	#$0f,d0
	cmp.b	#1,d0
	beq	appreggiato
	cmp.b	#2,d0
	beq	pitchbend
	rts

*------------------------------------------------
appreggiato:
	cmp.w	#1,timpos
	beq.s	appre1
	cmp.w	#2,timpos
	beq.s	appre2
	cmp.w	#3,timpos
	beq.s	appre3
	cmp.w	#4,timpos
	beq.s	appre1
	cmp.w	#5,timpos
	beq.s	appre2
	rts
appre1:	clr.l	d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	bra.s	appre4
appre2:	clr.l	d0
	move.b	3(a6),d0
	and.b	#$0f,d0
	bra.s	appre4
appre3:	move.w	16(a6),d2
	bra.s	appre6
appre4:	lsl.l	#1,d0
	clr.l	d1
	move.w	16(a6),d1
	lea.l	notetable,a0
appre5:	move.w	(a0,d0.l),d2
	cmp.w	(a0),d1
	beq.s	appre6
	addq.l	#2,a0
	bra.s	appre5
appre6:	move.w	d2,6(a5)
	rts

*------------------------------------------------
pitchbend:
	clr.l	d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	cmp.b	#0,d0
	beq.s	pitch2
	add.w	d0,(a6)
	move.w	(a6),6(a5)
	rts
pitch2:	clr.l	d0
	move.b	3(a6),d0
	and.b	#$0f,d0
	cmp.b	#0,d0
	beq.s	pitch3
	sub.w	d0,(a6)
	move.w	(a6),6(a5)
pitch3:	rts

*------------------------------------------------
replayhandler:
	lea.l	muzakdata,a0
	move.l	a0,a3
	add.l	#12,a3
	move.l	a0,a2
	add.l	#472,a2
	add.l	#600,a0
	move.l	trapos,d0
	clr.l	d1
	move.b	(a2,d0.l),d1
	mulu	#1024,d1
	add.l	patpos,d1
	clr.w	enabit
	move.l	#$dff0a0,a5
	lea.l	datach0,a6
	bsr	soundhandler
	move.l	#$dff0b0,a5
	lea.l	datach1,a6
	bsr	soundhandler
	move.l	#$dff0c0,a5
	lea.l	datach2,a6
	bsr	soundhandler
	move.l	#$dff0d0,a5
	lea.l	datach3,a6
	bsr	soundhandler
	move.l	#500,d0
repl1:	dbra	d0,repl1
	move.l	#$8000,d0
	add.w	enabit,d0
	move.w	d0,$dff096
	move.l	#datach3,a6
	cmp.w	#1,14(a6)
	bne.s	repl2
	move.l	10(a6),$dff0d0
	move.w	14(a6),$dff0d4
repl2:	move.l	#datach2,a6
	cmp.w	#1,14(a6)
	bne.s	repl3
	move.l	10(a6),$dff0c0
	move.w	14(a6),$dff0c4
repl3:	move.l	#datach1,a6
	cmp.w	#1,14(a6)
	bne.s	repl4
	move.l	10(a6),$dff0b0
	move.w	14(a6),$dff0b4
repl4:	move.l	#datach0,a6
	cmp.w	#1,14(a6)
	bne.s	repl5
	move.l	10(a6),$dff0a0	
	move.w	14(a6),$dff0a4
repl5:	add.l	#16,patpos
	cmp.l	#1024,patpos
	bne.s	repl6
	clr.l	patpos
	addq.l	#1,trapos
	clr.l	d0
	move.w	numpat,d0
	move.l	trapos,d1
	cmp.w	d0,d1
	bne.s	repl6
	clr.l	trapos
repl6:	movem.l	(a7)+,d0-d7/a0-a6
	rts

*------------------------------------------------
soundhandler:
	move.l	(a0,d1.l),(a6)
	addq.l	#4,d1
	clr.l	d2
	move.b	2(a6),d2
	and.b	#$f0,d2
	lsr.b	#4,d2
	cmp.b	#0,d2
	beq.s	shand2
	clr.l	d3
	lea.l	sounds-4,a1
	move.l	d2,d4
	mulu	#4,d2
	mulu	#30,d4
	move.l	(a1,d2.l),4(a6)
	move.w	(a3,d4.l),8(a6)
	move.w	2(a3,d4.l),18(a6)
	move.w	4(a3,d4.l),d3
	cmp.w	#0,d3
	beq.s	shand1
	move.l	4(a6),d2
	add.l	d3,d2
	move.l	d2,4(a6)
	move.l	d2,10(a6)
	move.w	6(a3,d4),8(a6)
	move.w	6(a3,d4),14(a6)
	move.w	18(a6),8(a5) 
	bra.s	shand2
shand1:	move.l	4(a6),d2
	add.l	d3,d2
	move.l	d2,10(a6)
	move.w	6(a3,d4.l),14(a6)
	move.w	18(a6),8(a5) 
shand2:	cmp.w	#0,(a6)
	beq.s	shand3
	move.w	(a6),16(a6)
	move.w	20(a6),$dff096
	move.l	4(a6),0(a5)
	move.w	8(a6),4(a5)
	move.w	0(a6),6(a5)
	move.w	20(a6),d0
	or.w	d0,enabit
shand3:	rts

*------------------------------------------------
	datach0:	blk.l	5,0
			dc.w	1
	datach1:	blk.l	5,0
			dc.w	2
	datach2:	blk.l	5,0
			dc.w	4
	datach3:	blk.l	5,0
			dc.w	8
	sounds:		blk.l	15,0
	patpos:		dc.l	0
	trapos:		dc.l	0
	timpos:		dc.w	0
	enabit:		dc.w	0
	numpat:		dc.w	0
	plamod:		dc.w	0
	notetable:
	dc.w	856,808,762,720,678,640,604,570,538,508,480,453
	dc.w	428,404,381,360,339,320,302,285,269,254,240,226  
	dc.w	214,202,190,180,170,160,151,143,135,127,120,113,000

*------------------------------------------------
* read images here
*------------------------------------------------
	snd1:		blk.b	2,0	;samples
	snd2:		blk.b	2,0
	snd3:		blk.b	2,0
	snd4:		blk.b	2,0
	snd5:		blk.b	2,0
	snd6:		blk.b	2,0
	snd7:		blk.b	2,0
	snd8:		blk.b	2,0
	snd9:		blk.b	2,0
	snda:		blk.b	2,0
	sndb:		blk.b	2,0
	sndc:		blk.b	2,0
	sndd:		blk.b	2,0
	snde:		blk.b	2,0
	sndf:		blk.b	2,0
   muzakdata:		blk.b	2,0	;songfile
*************************************************
* end of muzak replay module  
*************************************************
