
mt_init	lea	$dff000,a4
	lea	songs(pc),a1
	lea	stuff(pc),a0
	moveq	#0,d1
	muls	#3,d0
	clr.b	(a0)
	move.b	(a1,d0.w),3(a0)
	move.b	2(a1,d0.w),2(a0)
	move.b	1(a1,d0.w),d1
	lsl	#4,d1
	move	d1,4(a0)
	clr	8(a0)
	lea	mt_data,a0
	lea	module(pc),a1
	move.l	a0,(a1)
	move.l	a0,a1
	lea	$3b8(a1),a1
	moveq	#$7f,d0
	moveq	#0,d1
mt_loop	move.l	d1,d2
	subq	#1,d0
mt_lop2	move.b	(a1)+,d1
	cmp.b	d2,d1
	bgt.s	mt_loop
	dbf	d0,mt_lop2
	addq.b	#1,d2
	lea	mt_samplestarts(pc),a1
	asl.l	#8,d2
	asl.l	#2,d2
	add.l	#$43c,d2
	add.l	a0,d2
	move.l	d2,a2
	moveq	#$1e,d0
mt_lop3	clr.l	(a2)
	move.l	a2,(a1)+
	moveq	#0,d1
	move.w	42(a0),d1
	asl.l	#1,d1
	add.l	d1,a2
	lea	$1e(a0),a0
	dbf	d0,mt_lop3
	bset	#1,$bfe001
	bra.s	vol0

mt_end	lea	$dff000,a4
	bclr	#1,$bfe001
	move	#$f,$96(a4)
vol0	clr	$a8(a4)
	clr	$b8(a4)
	clr	$c8(a4)
	clr	$d8(a4)
	rts

mt_music
	movem.l	d0-d4/a0-a3/a5-a6,-(a7)
	lea	$dff000,a4
	move.l	module(pc),a0
	lea	stuff(pc),a1
	tst	d5
	beq.s	normal
	addq	#1,8(a1)
	cmp	#$40,8(a1)
	ble.s	normal
	move	#$40,8(a1)
	moveq	#-1,d5
normal	addq.b	#1,(a1)
	move.b	(a1),D0
	cmp.b	mt_speed(pc),D0
	blt.s	mt_nonew
	clr.b	(a1)
	bra	mt_getnew
mt_nonew
	lea	mt_voice1(pc),a6
	lea	$a0(a4),a5
	bsr	mt_checkcom
	lea	mt_voice2(pc),a6
	lea	$b0(a4),a5
	bsr	mt_checkcom
	lea	mt_voice3(pc),a6
	lea	$c0(a4),a5
	bsr	mt_checkcom
	lea	mt_voice4(pc),a6
	lea	$d0(a4),a5
	bsr	mt_checkcom
	bra	mt_endr

mt_arpeggio
	moveq	#0,d0
	move.b	mt_counter(pc),d0
	divs	#3,d0
	swap	d0
	cmp	#0,d0
	beq.s	mt_arp2
	cmp	#2,d0
	beq.s	mt_arp1
	moveq	#0,d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	bra.s	mt_arp3
mt_arp1	moveq	#0,d0
	move.b	3(a6),d0
	and.b	#$f,d0
	bra.s	mt_arp3
mt_arp2	move.w	$10(a6),d2
	bra.s	mt_arp4
mt_arp3	asl.w	#1,d0
	moveq	#0,d1
	move.w	$10(a6),d1
	lea	mt_periods(pc),a0
	moveq	#$24,d7
mt_arpl	move.w	(a0,d0.w),d2
	cmp.w	(a0),d1
	bge.s	mt_arp4
	lea	2(a0),a0
	dbf	d7,mt_arpl
	rts

mt_arp4	move.w	d2,6(a5)
	rts

mt_getnew
	move.l	module(pc),a0
	lea	stuff(pc),a5
	move.l	a0,a3
	move.l	a0,a2
	lea	$c(a3),a3
	lea	$3b8(a2),a2
	lea	$43c(a0),a0
	moveq	#0,d0
	move.l	d0,d1
	move.b	3(a5),d0
	move.b	(a2,d0.w),d1
	asl.l	#8,d1
	asl.l	#2,d1
	add	4(a5),d1
	clr	6(a5)

	lea	$a0(a4),a5
	lea	mt_voice1(pc),a6
	bsr.s	mt_playvoice
	lea	$b0(a4),a5
	lea	mt_voice2(pc),a6
	bsr.s	mt_playvoice
	lea	$c0(a4),a5
	lea	mt_voice3(pc),a6
	bsr.s	mt_playvoice
	lea	$d0(a4),a5
	lea	mt_voice4(pc),a6
	bsr.s	mt_playvoice
	bra	mt_setdma

mt_playvoice
	move.l	(a0,d1.l),(a6)
	addq.l	#4,d1
	moveq	#0,d2
	move.b	2(a6),d2
	and.b	#$f0,d2
	lsr.b	#4,d2
	move.b	(a6),d0
	and.b	#$f0,d0
	or.b	d0,d2
	tst.b	d2
	beq.s	mt_setregs
	moveq	#0,d3
	lea	mt_samplestarts(pc),a1
	move.l	d2,d4
	subq.l	#1,d2
	asl.l	#2,d2
	mulu	#$1e,d4
	move.l	(a1,d2.l),$4(a6)
	move.w	(a3,d4.l),$8(a6)
	move.w	$2(a3,d4.l),$12(a6)
	move.w	$4(a3,d4.l),d3
	tst.w	d3
	beq.s	mt_noloop
	move.l	4(a6),d2
	asl.w	#1,d3
	add.l	d3,d2
	move.l	d2,$a(a6)
	move.w	$4(a3,d4.l),d0
	add.w	$6(a3,d4.l),d0
	move.w	d0,8(a6)
	move.w	$6(a3,d4.l),$e(a6)
	bsr.s	mt_setmaxvol
	bra.s	mt_setregs
mt_noloop
	move.l	$4(a6),d2
	add.l	d3,d2
	move.l	d2,$a(a6)
	move.w	$6(a3,d4.l),$e(a6)
	bsr.s	mt_setmaxvol

mt_setregs
	move.w	(a6),d0
	and.w	#$fff,d0
	beq	mt_checkcom2
	move.b	2(a6),d0
	and.b	#15,d0
	cmp.b	#3,d0
	bne.s	mt_setperiod
	bsr	mt_setmyport
	bra	mt_checkcom2

mt_setmaxvol
	move	d0,-(sp)
	move	$12(a6),d0
	sub	maxvolume(pc),d0
	bpl.s	@ok
	move	#0,d0
@ok	move	d0,8(a5)
   	move	(sp)+,d0
	rts

mt_setperiod
	lea	stuff(pc),a1
	move	(a6),$10(a6)
	and	#$fff,$10(a6)
	move	$14(a6),d0
	move	d0,$96(a4)
	clr.b	$1b(a6)
	move.l	4(a6),(a5)
	move	8(a6),4(a5)
	move	$10(a6),d0
	and	#$fff,d0
	move	d0,6(a5)
	move	$14(a6),d0
	or	d0,6(a1)
	bra	mt_checkcom2

mt_setdma
	lea	stuff(pc),a1
	move	#$12c,d0
mt_wait	dbf	d0,mt_wait
	move	6(a1),d0
	or	#$8000,d0
	move	d0,$96(a4)
	move	#$12c,d0
mt_wai2	dbf	d0,mt_wai2
	lea	mt_voice4(pc),a6
	move.l	$a(a6),$d0(a4)
	move.w	$e(a6),$d4(a4)
	lea	mt_voice3(pc),a6
	move.l	$a(a6),$c0(a4)
	move.w	$e(a6),$c4(a4)
	lea	mt_voice2(pc),a6
	move.l	$a(a6),$b0(a4)
	move.w	$e(a6),$b4(a4)
	lea	mt_voice1(pc),a6
	move.l	$a(a6),$a0(a4)
	move.w	$e(a6),$a4(a4)
	add	#$10,4(a1)
	cmp	#$400,4(a1)
	bne.s	mt_endr

mt_nex	lea	stuff(pc),a1
	clr	4(a1)
	clr.b	1(a1)
	addq.b	#1,3(a1)
	and.b	#$7f,3(a1)
	move.b	3(a1),d1
	move.l	module(pc),a5
	lea	$3b6(a5),a5
	cmp.b	(a5),d1
	bne.s	mt_endr
	clr.b	3(a1)
mt_endr	tst.b	1(a1)
	bne.s	mt_nex
@d	movem.l	(a7)+,d0-d4/a0-a3/a5-a6
	rts

mt_setmyport
	move	(a6),d2
	and	#$fff,d2
	move	d2,$18(a6)
	move	$10(a6),d0
	clr.b	$16(a6)
	cmp	d0,d2
	beq.s	mt_clrport
	bge.s	mt_rt
	move.b	#1,$16(a6)
	rts

mt_clrport
	clr	$18(a6)
mt_rt	rts

mt_myport
	move.b	3(a6),d0
	beq.s	mt_myslide
	move.b	d0,$17(a6)
	clr.b	3(a6)
mt_myslide
	tst.w	$18(a6)
	beq.s	mt_rt
	moveq	#0,d0
	move.b	$17(a6),d0
	tst.b	$16(a6)
	bne.s	mt_mysub
	add.w	d0,$10(a6)
	move.w	$18(a6),d0
	cmp.w	$10(a6),d0
	bgt.s	mt_myok
	move.w	$18(a6),$10(a6)
	clr.w	$18(a6)
mt_myok	move.w	$10(a6),$6(a5)
	rts

mt_mysub
	sub.w	d0,$10(a6)
	move.w	$18(a6),d0
	cmp.w	$10(a6),d0
	blt.s	mt_myok
	move.w	$18(a6),$10(a6)
	clr.w	$18(a6)
	move.w	$10(a6),$6(a5)
	rts

mt_vib	move.b	3(a6),d0
	beq.s	mt_vi
	move.b	d0,$1a(a6)

mt_vi	move.b	$1b(a6),d0
	lsr	#2,d0
	and	#$1f,d0
	moveq	#0,d2
	move.b	mt_sin(pc,d0.w),d2
	move.b	$1a(a6),d0
	and	#15,d0
	mulu	d0,d2
	lsr	#6,d2
	move	$10(a6),d0
	tst.b	$1b(a6)
	bmi.s	mt_vibmin
	add	d2,d0
	bra.s	mt_vib2

mt_sin	dc.b	$00,$18,$31,$4a,$61,$78,$8d,$a1,$b4,$c5,$d4,$e0,$eb,$f4,$fa,$fd
	dc.b	$ff,$fd,$fa,$f4,$eb,$e0,$d4,$c5,$b4,$a1,$8d,$78,$61,$4a,$31,$18

mt_vibmin
	sub	d2,d0
mt_vib2	move	d0,$6(a5)
	move.b	$1a(a6),d0
	lsr	#2,d0
	and	#$3c,d0
	add.b	d0,$1b(a6)
	rts

mt_nop	move.w	$10(a6),$6(a5)
	rts

mt_checkcom
	move	2(a6),d0
	and	#$fff,d0
	beq.s	mt_nop
	move.b	2(a6),d0
	and.b	#15,d0
	tst.b	d0
	beq	mt_arpeggio
	cmp.b	#$1,d0
	beq.s	mt_portup
	cmp.b	#$2,d0
	beq	mt_portdown
	cmp.b	#$3,d0
	beq	mt_myport
	cmp.b	#$4,d0
	beq	mt_vib
	move	$10(a6),$6(a5)
	cmp.b	#$a,d0
	beq.s	mt_volslide
	rts

mt_volslide
	moveq	#0,d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	tst.b	d0
	beq.s	mt_voldown
	add	d0,$12(a6)
	cmp	#$40,$12(a6)
	bmi.s	mt_vol2
	move	#$40,$12(a6)
mt_vol2	bra	mt_setmaxvol

mt_voldown
	moveq	#0,d0
	move.b	3(a6),d0
	and.b	#$f,d0
	sub	d0,$12(a6)
	bpl.s	mt_vol3
	clr	$12(a6)
mt_vol3	bra	mt_setmaxvol

mt_portup
	moveq	#0,d0
	move.b	3(a6),d0
	sub	d0,$10(a6)
	move	$10(a6),d0
	and	#$fff,d0
	cmp	#$71,d0
	bpl.s	mt_por2
	and	#$f000,$10(a6)
	or	#$71,$10(a6)
mt_por2	move	$10(a6),d0
	and	#$fff,d0
	move	d0,$6(a5)
	rts

mt_portdown
	clr	d0
	move.b	3(a6),d0
	add	d0,$10(a6)
	move	$10(a6),d0
	and	#$fff,d0
	cmp	#$358,d0
	bmi.s	mt_por3
	and	#$f000,$10(a6)
	or	#$358,$10(a6)
mt_por3	move	$10(a6),d0
	and	#$fff,d0
	move	d0,$6(a5)
	rts

mt_checkcom2	
	lea	stuff(pc),a1
	move.b	2(a6),d0
	and.b	#15,d0
	cmp.b	#$d,d0
	beq.s	mt_pattbreak
	cmp.b	#$b,d0
	beq.s	mt_posjmp
	cmp.b	#$c,d0
	beq.s	mt_setvol
	cmp.b	#$f,d0
	beq.s	mt_setspeed
	rts


mt_pattbreak
	not.b	1(a1)
	rts

mt_posjmp
	move.b	3(a6),d0
	subq.b	#1,d0
	move.b	d0,3(a1)
	not.b	1(a1)
	rts

mt_setvol
	move	d0,-(sp)
	cmp.b	#$40,3(a6)
	ble.s	mt_vol4
	move.b	#$40,$3(a6)
mt_vol4	move.b	3(a6),d0
	sub.b	maxvolume+1(pc),d0
	bpl.s	.ok
	moveq	#0,d0
.ok	move.b	d0,$8+1(a5)
	move	(sp)+,d0
	rts

mt_setspeed
	move.b	3(a6),d0
	and	#$1f,d0
	beq.s	mt_rts2
	clr.b	(a1)
	move.b	d0,2(a1)
mt_rts2	rts

module		dc.l	0		;songs: pattern,pattpos(0-63),speed

songs		dc.b	0,0,8
		dc.b	7,0,6
		dc.b	7,16,6
		dc.b	7,32,6
		dc.b	8,0,6
		dc.b	8,32,6
		dc.b	9,0,6
		dc.b	7,48,6
		dc.b	8,56,6
		EVEN
stuff
mt_counter	dc.b	0
mt_break	dc.b	0
mt_speed	dc.b	0
mt_songpos	dc.b	0
mt_pattpos	dc.w	0
mt_dmacon	dc.w	0
maxvolume	dc.w	0 	0=maximum

mt_samplestarts	ds.l	$1f
mt_voice1	ds.w	10
		dc.w	1
		ds.w	3
mt_voice2	ds.w	10
		dc.w	2
		ds.w	3
mt_voice3	ds.w	10
		dc.w	4
		ds.w	3
mt_voice4	ds.w	10
		dc.w	8
		ds.w	3
mt_periods
	dc.w	$0358,$0328,$02fa,$02d0,$02a6,$0280,$025c,$023a,$021a,$01fc,$01e0
	dc.w	$01c5,$01ac,$0194,$017d,$0168,$0153,$0140,$012e,$011d,$010d,$00fe
	dc.w	$00f0,$00e2,$00d6,$00ca,$00be,$00b4,$00aa,$00a0,$0097,$008f,$0087
	dc.w	$007f,$0078,$0071,$0000,$0000


