************************************************************************
*     Noisetracker 1.3d compressed Playroutine by United Forces/UFO    *
*            to use within Delitracker and Eagleplayer                 *
*                    Adaption by Buggs                                 *
************************************************************************
	;
mt_speed	=0
mt_songpos	=1
mt_pattpos	=2
mt_counter	=4
mt_break	=5
mt_dmacon	=6
mt_samplestarts =8
MT_Voice1	=132
mt_voice2	=166
mt_voice3	=200
mt_voice4	=234

test = 0
	ifne	test
	bsr	mt_init
wart
	btst	#6,$bfe001
	beq	end
	move.b	$dff006,d0
	cmp.b	#$ff,d0
	bne	wart
	bsr	mt_music
	bra	wart
end
	bsr	mt_end
	illegal
	endc

	incdir	"Include:"
	include "misc/EaglePlayer.i"

	PLAYERHEADER	Tags
	dc.b `$VER: Noisetracker Compressed Playroutine V1.0 (Mar/12/93)`,0,0
Tags
	dc.l	DTP_RequestDTVersion,$ffff
	dc.l	EP_PlayerVersion,4
	dc.l	DTP_Volume,SetVoices
	dc.l	DTP_Balance,SetVoices
	dc.l	EP_Voices,SetVoices
	dc.l	DTP_PlayerVersion,2
	dc.l	DTP_PlayerName,_MT_Name
	dc.l	DTP_Creator,MT_CName
	dc.l	DTP_Check2,_MT_Check
	dc.l	DTP_Interrupt,MT_Music2
	dc.l	DTP_InitPlayer,InitPlay
	dc.l	DTP_EndPlayer,EndPlay
	dc.l	DTP_InitSound,MT_Init
	dc.l	DTP_EndSound,RemSnd
	dc.l	DTP_NextPatt,_MT_NextPattern
	dc.l	DTP_PrevPatt,_MT_BackPattern

	dc.l	EP_GetPositionNr,_MT_GetPosNr

	dc.l	EP_Get_ModuleInfo,GetInfos

;	dc.l	EP_SampleInit,SampleInit
;	dc.l	EP_SampleEnd,SampleEnd

	dc.l	EP_StructInit,StructInit

	dc.l	EP_flags,EPB_Volvoices!EPB_Packable!EPB_Save!EPB_restart!EPB_songend!EPB_Volume!EPB_Balance!EPB_Voices!EPB_Analyzer!EPB_Moduleinfo!EPB_Prevpatt!EPB_Nextpatt
	dc.l	0

	ifeq	test
mt_data:	dc.l	0
	endc
mt_len:		dc.l	0
DTbase		dc.l	0
MT_Structadr	ds.l	ups_sizeof

MT_VolVoice1	dc.w	1
MT_VolVoice2	dc.w	1
MT_VolVoice3	dc.w	1
MT_VolVoice4	dc.w	1

*-----------------------------------------------------------------------*
*		d0 Bit 0-3 = Set Voices Bit=1 Voice on			*
SetVoices:	lea	MT_StructAdr+UPS_DmaCon(pc),a0
		move.w	EPG_Voices(a5),(a0)				;Voices retten
		lea	MT_VolVoice1(pc),a1
		move.l	EPG_Voice1Vol(a5),(a1)
		move.l	EPG_Voice3Vol(a5),4(a1)

		lea	MT_StructAdr+UPS_Voice1Vol(pc),a0
		lea	$dff0a0,a5
		moveq	#3,d1
.SetNew		moveq	#0,d0
		move.w	(a0),d0
		bsr.s	MT_SetVoices
		moveq	#UPS_Modulo,d0
		add.l	d0,a0
		addq.l	#8,a5
		addq.l	#8,a5
		dbf	d1,.SetNew
		rts

*-----------------------------------------------------------------------*
MT_SetVoices:	movem.l	a0/d0,-(a7)
		and.w	#$7f,d0
		lea	MT_StructAdr(pc),a0
		cmp.l	#$dff0a0,a5			;Left Volume
		bne.s	.NoVoice1
		move.w	d0,UPS_Voice1Vol(a0)
		mulu.w	MT_VolVoice1(pc),d0
		bra.b	.SetIt
.NoVoice1:	cmp.l	#$dff0b0,a5			;Right Volume
		bne.s	.NoVoice2
		move.w	d0,UPS_Voice2Vol(a0)
		mulu.w	MT_VolVoice2(pc),d0
		bra.b	.SetIt
.NoVoice2:	cmp.l	#$dff0c0,a5			;Right Volume
		bne.s	.NoVoice3
		move.w	d0,UPS_Voice3Vol(a0)
		mulu.w	MT_VolVoice3(pc),d0
		bra.b	.SetIt
.NoVoice3:	move.w	d0,UPS_Voice4Vol(a0)
		mulu.w	MT_VolVoice4(pc),d0
.SetIt:		lsr.w	#6,d0
		move.w	d0,8(a5)
.Return:	movem.l	(a7)+,a0/d0
		rts
;========================================================================
_MT_Name:	dc.b	"Noisetracker Compressed",0
MT_CName:	dc.b	"United Forces/UFO,",10
		dc.b	"adapted by Buggs of DEFECT",0
		even
;========================================================================
_MT_InfoBuffer:
	dc.l	MI_Samples,0			;4
	dc.l	MI_MaxSamples,31		;12
	dc.l	MI_Length,0			;20
	dc.l	MI_Pattern,0			;28
	dc.l	MI_SongSize,0			;36
	dc.l	MI_SamplesSize,0		;44
	dc.l	MI_Calcsize,0			;52
	dc.l	MI_Unpacked,0			;60
	dc.l	MI_Unpackedsystem,MIUS_Noisetracker
	dc.l	MI_Maxpattern,64
	dc.l	MI_Maxlength,127
	dc.l	0
;=============================================================================
;                    Get Infos (Create InfoTag-Itemliste)
GetInfos:
	lea	_MT_InfoBuffer(pc),a0
	move.l	mt_data(pc),d0
	beq.s	.nomod
	move.l	d0,a1
	lea	(a1),a2
	moveq	#30,d0
	moveq	#-1,d1		;kleinstes Offset (Songlänge)
	moveq	#0,d2		;Anzahl der Samples
	moveq	#0,d3		;Länge der Samples
	moveq	#0,d4
.numsam
	tst.l	(a2)
	beq.s	.not
	cmp.l	(a2),d1
	bls.s	.not2
	move.l	(A2),d1
.not2
	move.w	4(A2),d4
	add.l	d4,d3
	add.l	d4,d3
	addq.l	#1,d2
.not
	addq.l	#8,a2
	addq.l	#4,a2
	dbf	d0,.numsam

	move.l	d1,36(a0)
	move.l	d2,4(a0)

	move.l	d3,44(a0)	;Samplelänge
	move.l	d3,52(a0)	;Calc Samplelänge
	move.l	d3,60(a0)	;Unpacked Samplelänge

	add.l	d1,52(a0)	;Calc Songlänge

	moveq	#0,d0
	move.b	$174(a1),d0
	move.l	d0,20(a0)

	lea	$176(a1),a1
	moveq	#$7f,d0
	moveq	#0,d1
	moveq	#0,d2

.seek	move.b	(a1)+,d1
	cmp.b	d1,d2
	bge.s	.nohi
	move.b	d1,d2
.nohi	dbf	d0,.seek
	addq.w	#1,d2
	move.l	d2,28(a0)

	mulu	#1024,d2
	add.l	#$43c,d2
	add.l	d2,60(a0)
.nomod
	moveq	#-1,d0
Return:	rts
;=============================================================================
StructInit
	lea	mt_StructAdr(pc),a0
Structend
	rts
;=============================================================================
MT_getvoice:
	movem.l	d0-d1/a1,-(sp)
	lea	MT_Structadr(pc),a1	;1.Kanal
	cmp.l	#$dff0a0,a5
	beq.s	.yes
	lea	MT_Structadr+ups_modulo(pc),a1	;2.Kanal
	cmp.l	#$dff0b0,a5
	beq.s	.yes
	lea	MT_Structadr+ups_modulo*2(pc),a1	;3.Kanal
	cmp.l	#$dff0c0,a5
	beq.s	.yes
	lea	MT_Structadr+ups_modulo*3(pc),a1	;4.Kanal
.yes
	move.w	$10(a6),d0
	and.w	#$fff,d0
	move.w	d0,UPS_Voice1Per(a1)

	move.l	4(a6),d0		;Sampleadresse
	move.l	d0,UPS_Voice1Adr(a1)

	moveq	#0,d1
	cmp.l	$a(A6),d0		;Repeatbeginn = Sampleadresse ?
	beq.s	.ok
	moveq	#1,d1	;nein,Repeat "off" setzen
.ok	move.w	d1,UPS_Voice1Repeat(a1)

	move.w	8(a6),UPS_Voice1len(a1)	;Länge

	movem.l	(sp)+,d0-d1/a1
	rts
;=========================================================================
_MT_Check:
	moveq	#-1,d0
	move.l	dtg_ChkData(a5),a0
	lea	$174(a0),a1
	moveq	#$7f,d1
.check1
	tst.b	(a1)+
	blt.s	.fehler
	dbf	d1,.check1

	move.l	$1f6(a0),d0
	sub.l	#`PATT`,d0
.fehler
	rts
;=========================================================================
InitPlay:
	moveq	#0,d0
	move.l	dtg_GetListData(a5),a0	; Function
	jsr	(a0)
	move.l	a5,dtbase
	move.l	a0,mt_data
	move.l	d0,mt_len

	move.l	dtg_AudioAlloc(a5),a0
	jmp	(a0)
.Return:
	moveq	#1,d0
	rts
;=========================================================================
EndPlay:
	clr.l	mt_data

	move.l	dtg_AudioFree(a5),a0
	jmp	(a0)
;=========================================================================
RemSnd:
	lea	$dff000,a0
	move.w	#$F,$96(a0)			; End Sound
	moveq	#0,d0
	move.w	d0,$a8(a0)
	move.w	d0,$b8(a0)
	move.w	d0,$c8(a0)
	move.w	d0,$d8(a0)

	lea	mt_structadr(pc),a0
	move.w	d0,UPS_Flags(a0)
	move.l	d0,ups_voice1adr(a0)
	move.l	d0,ups_voice2adr(a0)
	move.l	d0,ups_voice3adr(a0)
	move.l	d0,ups_voice4adr(a0)
	move.w	#1,ups_enabled(a0)
	rts
;=========================================================================
_MT_NextPattern:
	moveq	#0,d0
	lea	mt_vars(pc),a4

	clr.b	mt_pattpos+1(a4)
	clr.b	mt_break(a4)
	addq.b	#1,mt_songpos(a4)
	andi.b	#$7f,mt_songpos(a4)
	move.b	mt_songpos(a4),d1

	move.l	mt_data(pc),a0
	cmp.b	$174(a0),d1
	bne.s	.not
	move.b	$175(a0),mt_songpos(a4)	

	move.l	a2,-(sp)
	move.l	dtbase(pc),a2
	move.l	dtg_songend(A2),a2
	jsr	(A2)
	move.l	(sp)+,a2

.not	moveq	#0,d0
	move.b	mt_songpos(a4),d0
	bsr	setChannel
	moveq	#0,d0
	move.b	mt_songpos(a4),d0
	rts
;=========================================================================
_MT_BackPattern:
	moveq	#0,d0
	lea	mt_vars(pc),a4
	move.l	mt_data(pc),a0

	clr.b	mt_pattpos+1(a4)
	clr.b	mt_break(a4)
	subq.b	#1,mt_songpos(a4)
	bgt.s	.ok
;	move.b	$174(a0),mt_songpos(a4)
;	subq.b	#1,mt_songpos(a4)
;	and.b	#$7f,mt_songpos(a4)

	clr.b	mt_songpos(a4)

.ok
	moveq	#0,d0
	move.b	mt_songpos(a4),d0
	bsr	setChannel
	moveq	#0,d0
	move.b	mt_songpos(a4),d0
	rts
;=========================================================================
_MT_GetPosNr:
	moveq	#0,d0
	move.b	mt_vars+mt_songpos(pc),d0
	rts
mt_music2:
	movem.l	d1-a6,-(a7)

	lea	mt_structadr(pc),a0
	move.w	#UPSB_Adr!UPSB_LEN!UPSB_Per!UPSB_Vol!UPSB_DMACON,d0

	move.w	d0,UPS_Flags(a0)
	clr.w	ups_voice1per(a0)
	clr.w	ups_voice2per(a0)
	clr.w	ups_voice3per(a0)
	clr.w	ups_voice4per(a0)
	move.w	#1,ups_enabled(a0)

	bsr.w	mt_Music

	lea	mt_structadr(pc),a0
	clr.w	ups_enabled(a0)
	movem.l	(a7)+,d1-a6
	moveq	#0,d0
	rts

	;*******************************
	;*** NOISETRACKER COMPRESSED ***
	;*** REPLAYROUTINE 1.3 D     ***
	;*** IMPROVED ON 15/03/91    ***
	;*** BY ANTICHRIST OF UFO    ***
	;*******************************

; This routine is fully pc-relative.
; Don't change anything in mt_vars
; ('coz the offsets below won't fit anymore!).

; Include your PACKED module at mt_data (still the same!)
; To init, jump to Offset 0
; »» Load your start position to d0.l ««
; To play, jump to Offset 4
; To stop, jump to Offset 8


mt_init:
	lea	mt_vars(pc),a4
	lea	(a4),a0
	move.w	#mt_varslen-1,d0
.clr
	clr.b	(a0)+
	dbf	d0,.clr

	move.w	#1,mt_voice1+20(a4)
	move.w	#2,mt_voice2+20(a4)
	move.w	#4,mt_voice3+20(a4)
	move.w	#8,mt_voice4+20(a4)

	move.l	mt_data(pc),a0
	move.l	a0,a1
	lea	$176(a1),a1
	moveq	#$7f,d0
	moveq	#0,d1
	moveq	#0,d2

.seek	move.b	(a1)+,d1
	cmp.b	d1,d2
	bge.s	.nohi
	move.b	d1,d2
.nohi	dbf	d0,.seek

	move.l	mt_data(pc),a0
	lea	$1f6(a0),a0
	lea	mt_patterns(pc),a1
	move.w	#$5041,d0
	move.w	#$5454,d1
.getPatt
	cmp.w	(a0)+,d0
	bne.s	.getPatt
	cmp.w	(a0)+,d1
	bne.s	.getPatt
	move.l	a0,(a1)+
	dbf	d2,.getPatt

	lea	mt_samplestarts(a4),a1
	move.l	mt_data(pc),a2
	move.l	a2,d2
	moveq	#31-1,d1
mt_lop3:
	move.l	(a2),d0
	add.l	d2,d0
	move.l	d0,(a1)+
	lea	12(a2),a2
	dbf	d1,mt_lop3

	moveq	#0,d0
	bsr	setChannel

;	bset	#1,$bfe001

	move.b	#6,mt_speed(a4)

	clr.w	$dff0a8
	clr.w	$dff0b8
	clr.w	$dff0c8
	clr.w	$dff0d8
	clr.b	mt_counter(a4)
	clr.w	mt_pattpos(a4)
	rts

mt_end:
	clr.w	$dff0a8
	clr.w	$dff0b8
	clr.w	$dff0c8
	clr.w	$dff0d8
	move.w	#$f,$dff096
	rts

mt_music:
	lea	mt_vars(pc),a4
	addq.b	#1,mt_counter(a4)
	move.b	mt_counter(a4),D0
	cmp.b	mt_speed(a4),D0
	blt.s	mt_nonew
	clr.b	mt_counter(a4)
	bra	mt_getnew

mt_nonew:
	lea	mt_voice1(a4),a6
	lea	$dff0a0,a5
	bsr	mt_checkcom
	lea	mt_voice2(a4),a6
	lea	$10(a5),a5
	bsr	mt_checkcom
	lea	mt_voice3(a4),a6
	lea	$10(a5),a5
	bsr	mt_checkcom
	lea	mt_voice4(a4),a6
	lea	$10(a5),a5
	bsr	mt_checkcom
	bra	mt_endr

mt_arpeggio:
	moveq	#0,d0
	move.b	mt_counter(a4),d0
	divs	#3,d0
	swap	d0
	cmp.w	#0,d0
	beq.s	mt_arp2
	cmp.w	#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_arploop:
	move.w	(a0,d0.w),d2
	cmp.w	(a0),d1
	bge.s	mt_arp4
	addq.l	#2,a0
	dbf	d7,mt_arploop
	rts
mt_arp4:move.w	d2,$6(a5)
	rts

mt_getnew:
	move.l	mt_data(pc),a3
	clr.w	mt_dmacon(a4)
	lea	$dff0a0,a5
	lea	mt_voice1(a4),a6
	bsr.s	mt_playvoice
	lea	$10(a5),a5
	lea	mt_voice2(a4),a6
	bsr.s	mt_playvoice
	lea	$10(a5),a5
	lea	mt_voice3(a4),a6
	bsr.s	mt_playvoice
	lea	$10(a5),a5
	lea	mt_voice4(a4),a6
	bsr.s	mt_playvoice
	bra	mt_setdma

mt_playvoice:
	tst.w	32(a6)
	beq.s	.nodelay
	subq.w	#1,32(a6)
	bra.s	.morevoice
.nodelay
	move.l	28(a6),a0
	moveq	#0,d2
	move.b	(a0),d2
	cmpi.b	#$ff,d2
	bne.s	.nocrunch
	move.b	1(a0),33(a6)
	addq.l	#2,28(a6)
	clr.l	(a6)
	bra.s	mt_playvoice
.nocrunch
	move.b	d2,d0
	andi.b	#$3f,d2
	beq.s	.noset
	lsl.w	#1,d2
	lea	mt_periods(pc),a1
	move.w	-2(a1,d2.w),d2
.noset	btst	#7,d0
	beq.s	.nohisamp
	bset	#12,d2
.nohisamp
	move.w	d2,(a6)
	move.b	1(a0),2(a6)
	clr.b	3(a6)
	btst	#6,d0
	bne.s	.nocom
	move.b	2(a0),3(a6)
	addq.l	#1,28(a6)
.nocom	addq.l	#2,28(a6)
.morevoice
	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
	lea	mt_samplestarts(a4),a1
	subq.l	#1,d2
	move.l	d2,d4
	mulu	#12,d4				
	asl.l	#2,d2				
	move.l	(a1,d2.l),4(a6)			
	move.w	4(a3,d4.l),8(a6)		
	move.w	6(a3,d4.l),18(a6)		
	moveq	#0,d3
	move.w	8(a3,d4.l),d3
	tst.w	d3
	beq.s	mt_noloop
	move.l	4(a6),d2
	lsl.l	#1,d3
	add.l	d3,d2
	move.l	d2,$a(a6)
	move.w	8(a3,d4.l),d0
	add.w	10(a3,d4.l),d0
	move.w	d0,8(a6)
	move.w	10(a3,d4.l),$e(a6)

	move.w	$12(a6),d0
	bsr	mt_setvoices
;	move.w	$12(a6),$8(a5)
	bra.s	mt_setregs
mt_noloop:
	move.l	4(a6),d2
	add.l	d3,d2
	move.l	d2,10(a6)
	move.w	10(a3,d4.l),14(a6)

	move.w	18(a6),d0
	bsr	mt_setvoices
;	move.w	18(a6),8(a5)
mt_setregs:
	move.w	(a6),d0
	andi.w	#$fff,d0
	beq	mt_checkcom2
	move.b	2(a6),d0
	and.b	#$F,d0
	cmp.b	#$3,d0
	bne.s	mt_setperiod
	bsr	mt_setmyport
	bsr	MT_getvoice
	bra	mt_checkcom2
mt_setperiod:
	move.w	(a6),$10(a6)
	and.w	#$fff,$10(a6)
	bsr	MT_getvoice

	move.w	$14(a6),d0
	move.w	d0,$dff096
	clr.b	$1b(a6)

	move.l	4(a6),(a5)
	move.w	8(a6),4(a5)
	move.w	$10(a6),d0
	and.w	#$fff,d0
	move.w	d0,$6(a5)
	move.w	20(a6),d0
	or.w	d0,mt_dmacon(a4)
	bra	mt_checkcom2

mt_setdma:
	bsr	waitdma

	move.w	mt_dmacon(a4),d0
	or.w	#$8000,d0
	move.w	d0,$dff096

	bsr	waitdma

	lea	$dff000,a5
	lea	mt_voice4(a4),a6
	move.l	$a(a6),$d0(a5)
	move.w	$e(a6),$d4(a5)
	lea	mt_voice3(a4),a6
	move.l	$a(a6),$c0(a5)
	move.w	$e(a6),$c4(a5)
	lea	mt_voice2(a4),a6
	move.l	$a(a6),$b0(a5)
	move.w	$e(a6),$b4(a5)
	lea	mt_voice1(a4),a6
	move.l	$a(a6),$a0(a5)
	move.w	$e(a6),$a4(a5)
	addq.b	#1,mt_pattpos+1(a4)
	cmpi.b	#64,mt_pattpos+1(a4)
	bne.s	mt_endr
mt_nex:	clr.b	mt_pattpos+1(a4)
	clr.b	mt_break(a4)
	addq.b	#1,mt_songpos(a4)
	andi.b	#$7f,mt_songpos(a4)
	move.b	mt_songpos(a4),d1

	move.l	mt_data(pc),a0
	cmp.b	$174(a0),d1
	bne.s	.not
	move.b	$175(a0),mt_songpos(a4)	
.not	moveq	#0,d0
	move.b	mt_songpos(a4),d0
	bsr	setChannel
mt_endr:
	tst.b	mt_break(a4)
	bne.s	mt_nex
	rts
waitdma
	movem.l	d0/d1,-(sp)
	moveq	#4,d0
.wait2
	move.b	$dff006,d1
.wart1
	cmp.b	$dff006,d1
	beq.s	.wart1
	dbf	d0,.wait2

	movem.l	(sp)+,d0/d1
	rts
mt_setmyport:
	move.w	(a6),d2
	and.w	#$fff,d2
	move.w	d2,$18(a6)
	move.w	$10(a6),d0
	clr.b	$16(a6)
	cmp.w	d0,d2
	beq.s	mt_clrport
	bge.s	mt_rt
	move.b	#$1,$16(a6)
	rts
mt_clrport:
	clr.w	$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
	lea	mt_sin(pc),a4
	lsr.w	#$2,d0
	and.w	#$1f,d0
	moveq	#0,d2
	move.b	(a4,d0.w),d2
	move.b	$1a(a6),d0
	and.w	#$f,d0
	mulu	d0,d2
	lsr.w	#$6,d2
	move.w	$10(a6),d0
	tst.b	$1b(a6)
	bmi.s	mt_vibmin
	add.w	d2,d0
	bra.s	mt_vib2
mt_vibmin:
	sub.w	d2,d0
mt_vib2:move.w	d0,$6(a5)
	move.b	$1a(a6),d0
	lsr.w	#$2,d0
	and.w	#$3c,d0
	add.b	d0,$1b(a6)
	lea	mt_vars(pc),a4
	rts

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

mt_checkcom:
	move.w	2(a6),d0
	and.w	#$fff,d0
	beq.s	mt_nop
	move.b	2(a6),d0
	and.b	#$f,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.w	$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.w	d0,$12(a6)
	cmp.w	#$40,$12(a6)
	bmi.s	mt_vol2
	move.w	#$40,$12(a6)
mt_vol2:
	move.w	$12(a6),d0
	bra	mt_setvoices
;	move.w	d0,$8(a5)
;	rts

mt_voldown:
	moveq	#0,d0
	move.b	$3(a6),d0
	and.b	#$f,d0
	sub.w	d0,$12(a6)
	bpl.s	mt_vol3
	clr.w	$12(a6)
mt_vol3:
	move.w	$12(a6),d0
	bra	mt_setvoices
;	move.w	$12(a6),$8(a5)
;	rts

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

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

mt_checkcom2:
	move.b	2(a6),d0
	and.b	#$f,d0
	cmp.b	#$e,d0
	beq.s	mt_setfilt
	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_setfilt:
;	move.b	$3(a6),d0
;	and.b	#$1,d0
;	asl.b	#$1,d0
;	and.b	#$fd,$bfe001
;	or.b	d0,$bfe001
	rts
mt_posjmp:
	move.b	3(a6),d0
	subq.b	#1,d0
	move.b	d0,mt_songpos(a4)
mt_pattbreak:
	not.b	mt_break(a4)
	rts
mt_setvol:
	cmp.b	#$40,$3(a6)
	ble.s	mt_vol4
	move.b	#$40,$3(a6)
mt_vol4:
	move.l	d0,-(sp)
	moveq	#0,d0
	move.b	3(a6),d0
	bsr	mt_setvoices
	move.l	(sp)+,d0
;	move.b	$3(a6),$8(a5)
	rts

mt_setspeed:
	cmp.b	#$1f,$3(a6)
	ble.s	mt_sets
	move.b	#$1f,$3(a6)
mt_sets:
	move.b	$3(a6),d0
	beq.s	mt_rts2
	move.b	d0,mt_speed(a4)
	clr.b	mt_counter(a4)
mt_rts2:
	rts

setChannel:
	move.l	mt_data(pc),a0

	lea	mt_vars(pc),a4
	cmp.b	$174(a0),d0
	blt.s	.valid
	moveq	#0,d0
.valid	move.b	d0,mt_songpos(a4)

	lea	$176(a0),a0

	move.b	(a0,d0.w),d0
	lsl.l	#2,d0
	lea	mt_voice1(a4),a1
	lea	mt_patterns(pc),a0
	move.l	(a0,d0.w),a0
	move.l	a0,d2
	move.l	d2,d0
	addq.l	#6,d0
	moveq	#3,d1
	bra.s	.dochan
.setchannel
	lea	34(a1),a1
	moveq	#0,d0
	move.w	(a0)+,d0
	add.l	d2,d0
.dochan
	move.l	d0,28(a1)
	clr.w	32(a1)
	dbf	d1,.setchannel
	rts

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_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

mt_patterns:
	dcb.l	64
mt_vars:			;*** DON'T CHANGE ANYTHING BELOW ***
	DC.b	6
	DC.b	0
	DC.w	0
	DC.b	0
	DC.b	0
	DC.w	0
;mt_samplestarts
	ds.l	$1f

;mt_voice1
	ds.w	10
	DC.w	1
	ds.w	3
	dc.l	0		
	dc.w	0		

;mt_voice2
	ds.w	10
	DC.w	2
	ds.w	3
	dc.l	0
	dc.w	0
	
;mt_voice3
	ds.w	10
	DC.w	4
	ds.w	3
	dc.l	0
	dc.w	0
	
;mt_voice4
	dcb.w	10
	DC.w	8
	dcb.w	3
	dc.l	0
	dc.w	0
mt_varslen=*-mt_vars
	dc.l	0

	ifne	test
mt_data:
	dc.l	data
data
	incdir	df1:
	INCBIN	pak.agnostica

	endc
	end
