****************************************************************************
*                        CCC  OO  DDD  EEEE                                *
*                       C    O  O D  D E                                   *
*                       C    O  O D  D EEE                                 *
*                       C    O  O D D  E                                   *
*                        CCC  OO  DD   EEEEs  STe REPLAY-ROUTINE           *
*                                                                          *
*   Based on:   WizzCatŗs ProTracker-replayroutine                         *
*               Amiga ProTracker replayroutine                             *
*                                                                          *
*   Features:   Soundchip running on 50 kHz                                *
*               Updating sound at 25 kHz                                   *
*               Do not use timer A                                         *
*               Uses hardware for some volumecontrol                       *
*                                                                          *
*--------------------------------------------------------------------------*
*                                                                          *
*   Thanx to:   WizzCat, Blade                                             *
*                                                                          *
*--------------------------------------------------------------------------*
*                                                                          *
* The following commands exists on the Amiga ProTracker 2.3A. Thouse with  *
* the""-tecknet is supported by this replay                               *
*                                                                          *
*                                                                          *
*        Protracker V2.3A Effect Commands                                  *
*--------------------------------------------------------------------------*
*  0 - Normal play or Arpeggio        0xy : x-first halfnote add,y-second *
*  1 - Slide Up                       1xx : upspeed                       *
*  2 - Slide Down                     2xx : downspeed                     *
*  3 - Tone Portamento                3xx : up/down speed                 *
*  4 - Vibrato                        4xy : x-speed,   y-depth            *
*  5 - Tone Portamento+Volume Slide   5xy : x-upspeed, y-downspeed        *
*  6 - Vibrato + Volume Slide         6xy : x-upspeed, y-downspeed        *
*  7 - Tremolo                        7xy : x-speed,   y-depth            *
*  8 - NOT USED                                                           *
*  9 - Set SampleOffset               9xx : offset ($23 -> $2300)         *
*  A - VolumeSlide                    Axy : x-upspeed, y-downspeed        *
*  B - Position Jump                  Bxx : songposition                  *
*  C - Set Volume                     Cxx : volume, $00-$40               *
*  D - Pattern Break                  Dxx : break position in next patt   *
*  E - E-Commands                     Exy : see below...                  *
*  F - Set Speed                      Fxx : speed($00-$1F)/tempo($20-$FF) *
*                                          OBS tempo supportas ej          *
*--------------------------------------------------------------------------*
*  E0- Set Filter                     E0x : 0-filter on,1-filter off Hehe *
*  E1- FineSlide Up                   E1x : value                         *
*  E2- FineSlide Down                 E2x : value                         *
*   E3- Glissando Control              E3x : 0-off, 1-on (use with tonep.) *
*   E4- Set Vibrato Waveform           E4x : 0-sine, 1-ramp down, 2-square *
*   E5- Set Loop                       E5x : set loop point                *
*   E6- Jump to Loop                   E6x : jump to loop, play x times    *
*   E7- Set Tremolo Waveform           E7x : 0-sine, 1-ramp down. 2-square *
*   E8- NOT USED                                                           *
*   E9- Retrig Note                    E9x : retrig from note + x vblanks  *
*  EA- Fine VolumeSlide Up            EAx : add x to volume               *
*  EB- Fine VolumeSlide Down          EBx : subtract x from volume        *
*   EC- NoteCut                        ECx : cut from note + x vblanks     *
*   ED- NoteDelay                      EDx : delay note x vblanks          *
*   EE- PatternDelay                   EEx : delay pattern x notes         *
*   EF- Invert Loop                    EFx : speed                         *
*--------------------------------------------------------------------------*
****************************************************************************


INC	EQU 9371195			; 3579546/25033*65536


	move.l	a7,a5
	move.l	4(a5),a5
	move.l	$c(a5),d0
	add.l	$14(a5),d0
	add.l	$1c(a5),d0
	add.l	#$100,d0
	move.l	d0,-(sp)
	move.l	a5,-(sp)
	move.w	#0,-(sp)
	move.w	#$4a,-(sp)
	trap	#1			; Registrera programet
	add.l	#12,sp

	move.l	#300000,-(sp)
	move.w	#$48,-(sp)
	trap	#1			; Beg„r minne f”r musik
	addq.l	#6,sp
	tst.l	d0
	beq	fel
	move.l	d0,datagrejer
	add.l	#300000,d0
	move.l	d0,workspc

	lea	superfastfinetune,a0
	moveq	#125-1,d0
	moveq	#0,d1		; Rensa buffern
rensasuperfastfinetune:
	move.l	d1,(a0)+
	move.l	d1,(a0)+
	dbra	d0,rensasuperfastfinetune

	lea	superfastfinetune,a0
	moveq	#36-1,d0
	moveq	#0,d1		; Skriv dit superfast finetune
	lea	mt_PeriodTable,a1
monteraintalenisuperfastfinetune:
	move.w	(a1)+,d2
	move.b	d1,0(a0,d2.w)
	addq.w	#2,d1
	dbra	d0,monteraintalenisuperfastfinetune


	lea	buffrar,a0
	move.l	#sample,d0
	moveq	#0,d1
	moveq	#3-1,d2
keloop: move.w	d1,d3
	mulu	#1004*2,d3
	add.l	d0,d3
	move.l	d3,(a0)+
	addq.w	#1,d1
	dbra	d2,keloop
	move.l	#sample,(a0)+

	move.l	datagrejer,d0
	add.l	#$3b6,d0
	move.l	d0,set1+2
	addq.l	#1,d0
	move.l	d0,set2+2

	clr.l	-(sp)
	move.w	#$20,-(sp)
	trap	#1		; Super
	addq.l	#6,sp

	move.b	$484,keyclick
	bclr.b	#0,$484
	move.l	$408,controlc
	move.l	#controlleramera,$408
	move.l	$70.w,oldtima
	move.l	$70.w,jumptooldvbl+2

loopmajor:
	pea	infotext
	move.w	#9,-(sp)
	trap	#1		; Skriv ut infotext
	addq.l	#6,sp


	pea	matain
	move.w	#10,-(sp)
	trap	#1		; Mata in filnamn
	addq.l	#6,sp

	jsr	musoff		; Musik av

	move.l	#320000,laslangd
	move.l	datagrejer,lasbuffer
	move.l	#musiknamn,filnamn
	jsr	ladda			; Ladda musik

	move.w	#6,speed	; ¸terst„ll alla variabler
	clr.w	counter
	clr.b	songpos
	clr.b	break
	clr.w	pattpos
	lea	sample,a0
	move.w	#252*3-1,d0
clearplay:
	clr.w	(a0)+
	dbra	d0,clearplay
	move.l	#sample,samplepek
        lea     wiz1lc,a0
        moveq   #8-1,d0
resetwiz:				; Sluta spela instrument
        move.l	#dummy,(a0)+
	move.l	#$514,(a0)+
	move.l	#$1f4,(a0)+
	move.l	#$454,(a0)+
        move.w	#$651c,(a0)+
        dbra    d0,resetwiz
        lea     aud1lc,a0
        moveq   #8-1,d0
resetaud:
        move.l  #dummy,(a0)+
	move.w	#$320,(a0)+
	move.w	#$358,(a0)+
	move.w	#$40,(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
        dbra    d0,resetaud
        lea     voice1,a0
        moveq   #8-1,d0
resetvoice:
	clr.l	(a0)+
	move.l	#dummy,(a0)+
	move.w	#$320,(a0)+
	move.l	dummy,(a0)+
	move.w	#$1f4,(a0)+
	move.w	#$358,(a0)+
	move.w	#$40,(a0)+
	addq.l	#2,a0
	clr.w	(a0)+
	clr.l	(a0)+
	move.l	#itab0,(a0)+
	move.l	#$1f4,(a0)+
	clr.l	(a0)+
	dbra	d0,resetvoice


	pea	spelatext
	move.w	#9,-(sp)
	trap	#1		; Skriv ut spelatext
	addq.l	#6,sp
	move.l	datagrejer,-(sp)
	move.w	#9,-(sp)
	trap	#1		; Skriv ut s†ngnamnet
	addq.l	#6,sp

	jsr	muson		; Musik p†

	bra	loopmajor

fel:	illegal

;------------------------------- Cut here ------------------------------;

controlleramera:
	movem.l d0-d7/a0-a6,-(sp)
	move.l	datagrejer,-(sp)
	move.w	#$49,-(sp)
	trap	#1		; Sl„pp minne
	addq.l	#6,sp

	move.w	#$FFF,$ff8240

	move.w	#'',-(sp)
	move.w	#2,-(sp)
	move.w	#3,-(sp)
	trap	#13		; Ta bort kursorn
	addq.l	#6,sp
	move.w	#'f',-(sp)
	move.w	#2,-(sp)
	move.w	#3,-(sp)
	trap	#13
	addq.l	#6,sp

	move.b	keyclick,$484
	jsr	musoff		; Musik av

	move.l	controlc,$408
	movem.l (sp)+,d0-d7/a0-a6
	move.l	controlc,-(sp)	; Hoppa till tidigare registrerad rutin
	rts

ladda:	clr.w	-(sp)			; L„ser in en fil
	move.l	filnamn,-(sp)
	move.w	#$3d,-(sp)
	trap	#1			; ™ppna fil
	addq.l	#8,sp
	tst.l	d0
	bmi	loopmajor
	move.w	d0,handle

	move.l	lasbuffer,-(sp)
	move.l	laslangd,-(sp)
	move.w	handle,-(sp)
	move.w	#$3f,-(sp)
	trap	#1			; L„s fil
	lea	12(sp),sp

	move.w	handle,-(sp)
	move.w	#$3e,-(sp)
	trap	#1			; St„ng fil
	addq.l	#4,sp

	rts

;---------------------------------------------------- Interrupts on/off --
muson	jsr	incrcal 		; Calculate tonetables
	bsr	vol			; Calculate volume tables

	jsr	init			; Initialize music
	jsr	prepare 		; Prepare samples

	move.l	datagrejer,d0
	nop
	nop

	move	#$2700,sr

	move.l	$70,oldtima
	move.l	#stereo,$70

	move.w	#$7ff,$ffff8924.w

	move.b	#3,$FF8921		    ; Frequency

	lea	$FF8907,a0

	move.l	#sample,d0
	move.b	d0,(a0)
	lsr.w	#8,d0
	move.l	d0,-5(a0)

	move.l	#sample+(1004*3*2),d0
	move.b	d0,12(a0)
	lsr.w	#8,d0
	move.l	d0,7(a0)

	clr.l	$466
	move	#$2300,sr
sdf:	tst.l	$466
	beq.s	sdf

	move.b	#3,$FF8901	    ; Start DMA

	rts

musoff	move	#$2700,sr

	move.l	oldtima,$70	; Restore everything

	clr.b	$FF8901 	    ; Stop DMA

	move	#$2300,sr
	rts

oldtima DC.L 0

;------------------------------------------------------ Increment-table --
incrcal:lea	stab0,a0
	move.w	#$30,d1
	move.w	#$039F-$30,d0
	move.l	#INC,d2

recalc	swap	d2
	moveq	#0,d3
	move.w	d2,d3
	divu	d1,d3
	move.w	d3,d4
	swap	d4

	swap	d2
	move.w	d2,d3
	divu	d1,d3
	move.w	d3,d4
	move.l	d4,(a0)+

	addq.w	#1,d1
	dbra	d0,recalc
	rts

itab0:	DS.L $30
stab0:	DS.L $03A0-$30

;---------------------------------------- R„kna ut volymkontrollbuffern --

vol:	moveq	#64,d0
	lea	vtabend,a0
	moveq	#64,d3
.ploop:	move.w	#255,d1
.mloop:	move.w	d1,d2
	ext.w	d2
	muls	d0,d2
	divs	d3,d2			; <---- Master volume
	move.b	d2,-(a0)
	dbra	d1,.mloop
	dbra	d0,.ploop
	rts

vtab:	ds.b	65*256			; Volymtabell
vtabend:

;-------------------------------------------------------- DMA interrupt --

stereo
	movem.l d0-d7/a0-a6,-(sp)

	clr.w	$ff8240

	move.w	righthardvol,$ffff8922.w
v„ntainh”gerh†rdvaruvolym:
	cmp.w	#$7ff,$ffff8924.w
	bne.s	v„ntainh”gerh†rdvaruvolym

	move.w	lefthardvol,$ffff8922.w

	addq.w	#1,vantapasam
	move.w	vantapasam,d0
	cmp.w	#2,d0
	bls.s	ejborjaom
	clr.w	vantapasam
ejborjaom:
	move.w	vantapasam,d0
	add.w	d0,d0
	add.w	d0,d0
	lea	buffrar,a0
	move.l	(a0,d0.w),samplepek
	addq.w	#4,d0
	move.l	(a0,d0.w),d1
	move.b	$ffff8909.w,ultradummy+1
	move.b	$ffff890b.w,ultradummy+2
	move.b	$ffff890d.w,ultradummy+3
	move.l	ultradummy,d0
	cmp.l	d1,d0
	bgt.s	hejhopp
	cmp.l	samplepek,d0
	bls.s	hejhopp
	subq.w	#1,vantapasam
	jmp	ejspeladennagang
hejhopp:

	jsr	music

	moveq	#0,d0
	moveq	#0,d4
	move.w	aud3vol,d6
	move.w	aud2vol,d7
	and.w	#$FF,d7
	and.w	#$FF,d6
	cmp.w	d7,d6
	ble.s	s„tt2anh”gst
	move.w	#$ff,$ffff8240.w
	mulu	#64,d7
	divu	d6,d7
	lsl.w	#8,d7
	lea	vtab,a3
	add.w	d7,a3
	movea.l wiz3lc,a0
	move.l	wiz3pos,d0
	add.w	d6,d6
	bne.s	intedummy1
	move.w	#$FFF,$ffff8240.w
	lea	dummy,a0
	moveq	#0,d0
intedummy1:
	lea	hardvolumesetright,a6
	move.w	#%10100000000,d7
	or.w	(a6,d6.w),d7
	move.w	d7,righthardvol		; S„tt h†rdvaruvolym h”ger
	move.w	wiz3frc,d1
	move.w	aud3per,d7
	movea.l wiz2lc,a1
	move.l	wiz2pos,d4
	move.w	wiz2frc,d5
	move.w	aud2per,d6
	clr.w	first
	bra.s	satt3anh”gst
s„tt2anh”gst:
	move.w	#$f0f,$ffff8240.w
	mulu	#64,d6
	divu	d7,d6
	lsl.w	#8,d6
	lea	vtab,a3
	add.w	d6,a3
	movea.l wiz2lc,a0
	move.l	wiz2pos,d0
	add.w	d7,d7
	bne.s	intedummy2
	move.w	#$FFF,$ffff8240.w
	lea	dummy,a0
	moveq	#0,d0
intedummy2:
	lea	hardvolumesetright,a6
	move.w	#%10100000000,d6
	or.w	(a6,d7.w),d6
	move.w	d6,righthardvol		; S„tt h†rdvaruvolym h”ger
	move.w	wiz2frc,d1
	move.w	aud2per,d7
	movea.l wiz3lc,a1
	move.l	wiz3pos,d4
	move.w	wiz3frc,d5
	move.w	aud3per,d6
	move.w	#1,first
satt3anh”gst:

	lea	itab0,a5
	clr.w	extraoffsetadded1
	cmp.l	#$8000,d0
	blt.s	inteenkonstigoffset2
	sub.l	#$8000,d0
	add.l	#$8000,a0
	move.w	#1,extraoffsetadded1
inteenkonstigoffset2:
	add.w	d7,d7
	add.w	d7,d7
	move.w	0(a5,d7.w),d2
	movea.w 2(a5,d7.w),a4
	lea	itab0,a5
	move.w	d6,d7
	clr.w	extraoffsetadded2
	cmp.l	#$8000,d4
	blt.s	inteenkonstigoffset3
	sub.l	#$8000,d4
	add.l	#$8000,a1
	move.w	#1,extraoffsetadded2
inteenkonstigoffset3:
	add.w	d7,d7
	add.w	d7,d7			; 8
	move.w	0(a5,d7.w),d6		; 14
	movea.w 2(a5,d7.w),a5		; 14
	move.l	samplepek,a6		; 20
	moveq	#0,d3			; 4
	moveq	#0,d7



	rept	502
	add.w	a4,d1
	addx.w	d2,d0
	add.w	a5,d5
	addx.w	d6,d4
	move.b	0(a0,d0.l),d3
	move.b	0(a1,d4.l),d7
	add.b	0(a3,d7.w),d3
	move.w	d3,(a6)+
	move.w	d3,(a6)+
	endr



	tst.w	extraoffsetadded1
	beq.s	inteensubattg”ra2
	sub.l	#$8000,a0
	add.l	#$8000,d0
inteensubattg”ra2:
	tst.w	extraoffsetadded2
	beq.s	inteensubattg”ra3
	sub.l	#$8000,a1
	add.l	#$8000,d4
inteensubattg”ra3:

	tst.w	first
	beq.s	nonononono
	move.w	#$F00,$ffff8240.w
	cmp.l	wiz2len,d0
	blt.s	.ok2
	sub.l	wiz2rpt,d0
.ok2	move.l	d0,wiz2pos
	move.w	d1,wiz2frc
	cmp.l	wiz3len,d4
	blt.s	.ok3
	sub.l	wiz3rpt,d4
.ok3	move.l	d4,wiz3pos
	move.w	d5,wiz3frc
	bra.s	jaajjaja
nonononono:
	move.w	#$F0,$ffff8240.w
	cmp.l	wiz3len,d0
	blt.s	.ok22
	sub.l	wiz3rpt,d0
.ok22	move.l	d0,wiz3pos
	move.w	d1,wiz3frc
	cmp.l	wiz2len,d4
	blt.s	.ok32
	sub.l	wiz2rpt,d4
.ok32	move.l	d4,wiz2pos
	move.w	d5,wiz2frc
jaajjaja:


	moveq	#0,d0
	moveq	#0,d4
	move.w	aud4vol,d6
	move.w	aud1vol,d7
	and.w	#$FF,d7
	and.w	#$FF,d6
	cmp.w	d7,d6
	ble.s	s„tt1anh”gst
	move.w	#$ff,$ffff8240.w
	mulu	#64,d7
	divu	d6,d7
	lsl.w	#8,d7
	lea	vtab,a3
	add.w	d7,a3
	movea.l wiz4lc,a0
	move.l	wiz4pos,d0
	add.w	d6,d6
	bne.s	intedummy3
	move.w	#$FFF,$ffff8240.w
	lea	dummy,a0
	moveq	#0,d0
intedummy3:
	lea	hardvolumesetright,a6
	move.w	#%10101000000,d7
	or.w	(a6,d6.w),d7
	move.w	d7,lefthardvol		; S„tt h†rdvaruvolym v„nster
	move.w	wiz4frc,d1
	move.w	aud4per,d7
	movea.l wiz1lc,a1
	move.l	wiz1pos,d4
	move.w	wiz1frc,d5
	move.w	aud1per,d6
	clr.w	first
	bra.s	satt4anh”gst
s„tt1anh”gst:
	move.w	#$f0f,$ffff8240.w
	mulu	#64,d6
	divu	d7,d6
	lsl.w	#8,d6
	lea	vtab,a3
	add.w	d6,a3
	movea.l wiz1lc,a0
	move.l	wiz1pos,d0
	add.w	d7,d7
	bne.s	intedummy4
	move.w	#$FFF,$ffff8240.w
	lea	dummy,a0
	moveq	#0,d0
intedummy4:
	lea	hardvolumesetright,a6
	move.w	#%10101000000,d6
	or.w	(a6,d7.w),d6
	move.w	d6,lefthardvol		; S„tt h†rdvaruvolym v„nster
	move.w	wiz1frc,d1
	move.w	aud1per,d7
	movea.l wiz4lc,a1
	move.l	wiz4pos,d4
	move.w	wiz4frc,d5
	move.w	aud4per,d6
	move.w	#1,first
satt4anh”gst:

	lea	itab0,a5
	clr.w	extraoffsetadded1
	cmp.l	#$8000,d0
	blt.s	inteenkonstigoffset1
	sub.l	#$8000,d0
	add.l	#$8000,a0
	move.w	#1,extraoffsetadded1
inteenkonstigoffset1:
	add.w	d7,d7
	add.w	d7,d7
	move.w	0(a5,d7.w),d2
	movea.w 2(a5,d7.w),a4
	lea	itab0,a5
	move.w	d6,d7
	clr.w	extraoffsetadded2
	cmp.l	#$8000,d4
	blt.s	inteenkonstigoffset4
	sub.l	#$8000,d4
	add.l	#$8000,a1
	move.w	#1,extraoffsetadded2
inteenkonstigoffset4:
	add.w	d7,d7
	add.w	d7,d7			; 8
	move.w	0(a5,d7.w),d6		; 14
	movea.w 2(a5,d7.w),a5		; 14
	move.l	samplepek,a6		; 20
	moveq	#0,d3			; 4
	moveq	#0,d7



	rept	502
	add.w	a4,d1
	addx.w	d2,d0
	add.w	a5,d5
	addx.w	d6,d4
	move.b	0(a0,d0.l),d3
	move.b	0(a1,d4.l),d7
	add.b	0(a3,d7.w),d3
	move.b	d3,(a6)
	move.b	d3,2(a6)
	addq.w	#4,a6
	endr



	tst.w	extraoffsetadded1
	beq.s	inteensubattg”ra1
	sub.l	#$8000,a0
	add.l	#$8000,d0
inteensubattg”ra1:
	tst.w	extraoffsetadded2
	beq.s	inteensubattg”ra4
	sub.l	#$8000,a1
	add.l	#$8000,d4
inteensubattg”ra4:

	tst.w	first
	beq.s	nonononono2
	move.w	#$F00,$ffff8240.w
	cmp.l	wiz1len,d0
	blt.s	.ok1
	sub.l	wiz1rpt,d0
.ok1	move.l	d0,wiz1pos
	move.w	d1,wiz1frc
	cmp.l	wiz4len,d4
	blt.s	.ok4
	sub.l	wiz4rpt,d4
.ok4	move.l	d4,wiz4pos
	move.w	d5,wiz4frc
	bra.s	jaajjaja2
nonononono2:
	move.w	#$F0,$ffff8240.w
	cmp.l	wiz4len,d0
	blt.s	.ok12
	sub.l	wiz4rpt,d0
.ok12	move.l	d0,wiz4pos
	move.w	d1,wiz4frc
	cmp.l	wiz1len,d4
	blt.s	.ok42
	sub.l	wiz1rpt,d4
.ok42	move.l	d4,wiz1pos
	move.w	d5,wiz1frc
jaajjaja2:




ejspeladennagang:
	movem.l (sp)+,d0-d7/a0-a6
	move.w	#$FFF,$ff8240
jumptooldvbl:
	jmp	$12345678

;-------------------------------------------- Hardware-registers & data --

lefthardvol:
	dc.w	%10101000000
righthardvol:
	dc.w	%10100000000
first:	dc.w	0

voice1:	DS.W 10
	DC.W $01	; Till f”r dmacon
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0
voice2	DS.W 10
	DC.W $02
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0
voice3	DS.W 10
	DC.W $04
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0
voice4	DS.W 10
	DC.W $08
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0
voice5	DS.W 10
	DC.W $10
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0
voice6	DS.W 10
	DC.W $20
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0
voice7	DS.W 10
	DC.W $40
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0
voice8	DS.W 10
	DC.W $80
	DS.W 3
	dc.l	itab0
	dc.l	0
	dc.l	0

wiz1lc: DC.L sample		; Inneh†ller data om hur sampling skall skrivas till buffern
wiz1len:DC.L 0			; L„ngd
wiz1rpt:DC.L 0			; Repetition
wiz1pos:DC.L 0			; Vart ligger i dma-buffern
wiz1frc:DC.W 0			; Vilken frekvens spelar vi p†?

wiz2lc: DC.L sample
wiz2len:DC.L 0
wiz2rpt:DC.L 0
wiz2pos:DC.L 0
wiz2frc:DC.W 0

wiz3lc: DC.L sample
wiz3len:DC.L 0
wiz3rpt:DC.L 0
wiz3pos:DC.L 0
wiz3frc:DC.W 0

wiz4lc: DC.L sample
wiz4len:DC.L 0
wiz4rpt:DC.L 0
wiz4pos:DC.L 0
wiz4frc:DC.W 0

wiz5lc: DC.L sample
wiz5len:DC.L 0
wiz5rpt:DC.L 0
wiz5pos:DC.L 0
wiz5frc:DC.W 0

wiz6lc: DC.L sample
wiz6len:DC.L 0
wiz6rpt:DC.L 0
wiz6pos:DC.L 0
wiz6frc:DC.W 0

wiz7lc: DC.L sample
wiz7len:DC.L 0
wiz7rpt:DC.L 0
wiz7pos:DC.L 0
wiz7frc:DC.W 0

wiz8lc: DC.L sample
wiz8len:DC.L 0
wiz8rpt:DC.L 0
wiz8pos:DC.L 0
wiz8frc:DC.W 0

aud1lc: DC.L dummy		; Vart skall vi h„mta samplingen n†gonstans?
aud1len:DC.W 0			; L„ngd p† den
aud1per:DC.W 0			; Frekvens
aud1vol:DC.W 0			; Volym p† samplingen
aud1off:DC.W 0			; Sampleoffsetten
	DS.W 3

aud2lc: DC.L dummy
aud2len:DC.W 0
aud2per:DC.W 0
aud2vol:DC.W 0
aud2off:DC.W 0
	DS.W 3

aud3lc: DC.L dummy
aud3len:DC.W 0
aud3per:DC.W 0
aud3vol:DC.W 0
aud3off:DC.W 0
	DS.W 3

aud4lc: DC.L dummy
aud4len:DC.W 0
aud4per:DC.W 0
aud4vol:DC.W 0
aud4off:DC.W 0
	DS.W 3

aud5lc: DC.L dummy
aud5len:DC.W 0
aud5per:DC.W 0
aud5vol:DC.W 0
aud5off:DC.W 0
	DS.W 3

aud6lc: DC.L dummy
aud6len:DC.W 0
aud6per:DC.W 0
aud6vol:DC.W 0
aud6off:DC.W 0
	DS.W 3

aud7lc: DC.L dummy
aud7len:DC.W 0
aud7per:DC.W 0
aud7vol:DC.W 0
aud7off:DC.W 0
	DS.W 3

aud8lc: DC.L dummy
aud8len:DC.W 0
aud8per:DC.W 0
aud8vol:DC.W 0
aud8off:DC.W 0
	DS.W 3

dmactrl:DC.W 0

dummy:	DS.B 2000

samplepek:
	DC.L sample

sample: DS.W 1004*3*2
samdummy:
	dc.l	0
vantapasam:
	dc.w	-1
buffrar:ds.l	4

;========================================================= EMULATOR END ==

prepare:
	move.l	workspc,a6
	movea.l samplestarts,a0
	movea.l end_of_samples,a1

tostack move.w	-(a1),-(a6)
	cmpa.l	a0,a1			; Move all samples to stack
	bgt.s	tostack

	lea	samplestarts,a2
	move.l	datagrejer,a1		  ; Module
	movea.l (a2),a0 		; Start of samples
	movea.l a0,a5			; Save samplestart in a5

	moveq	#30,d7

roop	move.l	a0,(a2)+		; Sampleposition

	tst.w	$2A(a1)
	beq.s	samplok 		; Len=0 -> no sample

	tst.w	$2E(a1) 		; Test repstrt
	bne.s	repne			; Jump if not zero


repeq	move.w	$2A(a1),d0		; Length of sample
	move.w	d0,d4
	subq.w	#1,d0

	movea.l a0,a4
fromstk move.w	(a6)+,(a0)+		; Move all samples back from stack
	dbra	d0,fromstk

	bra.s	rep



repne	move.w	$2E(a1),d0
	move.w	d0,d4
	subq.w	#1,d0

	movea.l a6,a4
get1st	move.w	(a4)+,(a0)+		; Fetch first part
	dbra	d0,get1st

	adda.w	$2A(a1),a6		; Move a6 to next sample
	adda.w	$2A(a1),a6



rep	movea.l a0,a5
	moveq	#0,d1
toosmal movea.l a4,a3
	move.w	$30(a1),d0
	subq.w	#1,d0
moverep move.w	(a3)+,(a0)+		; Repeatsample
	addq.w	#2,d1
	dbra	d0,moverep
	cmp.w	#1000,d1		 ; Must be > 1000
	blt.s	toosmal

	move.w	#1000/2-1,d2
last13200
	move.w	(a5)+,(a0)+		; Safety 1000 bytes
	dbra	d2,last13200

done	add.w	d4,d4

	move.w	d4,$2A(a1)		; length
	move.w	d1,$30(a1)		; Replen
	clr.w	$2E(a1)

samplok lea	$1E(a1),a1
	dbra	d7,roop

	cmp.l	workspc,a0
	bgt.s	.nospac



	rts

.nospac illegal

end_of_samples	DC.L 0

;------------------------------------------------------ Main replayrout --
init	move.l	datagrejer,a0
	lea	$03B8(a0),a1

	moveq	#$7F,d0
	moveq	#0,d1
loop	move.l	d1,d2
	subq.w	#1,d0
lop2	move.b	(a1)+,d1
	cmp.b	d2,d1
	bgt.s	loop
	dbra	d0,lop2
	addq.b	#1,d2

	lea	samplestarts,a1
	asl.l	#8,d2
	asl.l	#2,d2
	add.l	#$043C,d2
	add.l	a0,d2
	movea.l d2,a2
	move.l	a2,a4
	moveq	#0,d5

	moveq	#$1E,d0
lop3	clr.l	(a2)
	move.l	a2,(a1)+
	moveq	#0,d1
	move.w	42(a0),d1
	add.l	d1,d1
	adda.l	d1,a2
	add.l	d1,d5
	adda.l	#$1E,a0
	dbra	d0,lop3

lldd:	move.b	(a4),d6
	asr.b	#1,d6			; Dela ner samplingen s† att vi kan
	move.b	d6,(a4)+		; spela med den utan volymkontroll
	subq.l	#1,d5			; om vi vill
	bne.s	lldd

	move.l	a2,end_of_samples	;
	rts



music:	addq.w	#$01,counter		; Dax att g† fram ett steg i patternet?
	move.w	counter(pc),d0
	cmp.w	speed(pc),d0
	blt	nonew			; Nix, forts„tt att spela p† instrumenten
	clr.w	counter 		; Jepp, rensa
	bra	getnew			; Fix och trixa med kommandon mm

nonew:	lea	voice1(pc),a4	; a4 Pekare till ? 
	lea	aud1lc(pc),a3	; a3 Pekare till sampling och div data om den
	bsr	checkcom	; Testa kommandon
	lea	voice2(pc),a4
	lea	aud2lc(pc),a3
	bsr	checkcom
	lea	voice3(pc),a4
	lea	aud3lc(pc),a3
	bsr	checkcom
	lea	voice4(pc),a4
	lea	aud4lc(pc),a3
	bsr	checkcom
	bra	endread 		; G”r slut p†'t

arpeggio:			; Kommando 0, arpeggio
	moveq	#0,d0
	move.w	counter(pc),d0
	divs	#$03,d0
	swap	d0
	tst.w	d0
	beq.s	arp2
	cmp.w	#$02,d0
	beq.s	arp1

	moveq	#0,d0
	move.b	$03(a4),d0
	lsr.b	#4,d0
	bra.s	arp3

arp1:	moveq	#0,d0
	move.b	$03(a4),d0
	and.b	#$0F,d0
	bra.s	arp3

arp2:	move.w	$10(a4),d2
	bra.s	arp4

arp3:	add.w	d0,d0
	moveq	#0,d1
	move.w	$10(a4),d1
	lea	periods,a0
	moveq	#$24,d4
arploop:move.w	0(a0,d0.w),d2
	cmp.w	(a0),d1
	bge.s	arp4
	addq.l	#2,a0
	dbra	d4,arploop
	rts

arp4:	move.w	d2,$06(a3)
	rts
				; Fixa n„sta position och tolka den
getnew:	move.l	datagrejer,a0
	lea	$43c(a0),a0
	lea	-$043C+$0C(a0),a2
	lea	-$043C+$03B8(a0),a1


	moveq	#0,d0
	move.l	d0,d1
	move.b	songpos(pc),d0		; d0 vart i songen vi ligger
	move.b	0(a1,d0.w),d1		; Vilket pattern skall spelas nu?
	asl.l	#8,d1			; *  2048
tre7:	asl.l	#2,d1
	add.w	pattpos(pc),d1		; Addera vart i patternet vi ligger just nu
	clr.w	dmacon			; Ingen dmacon =  inga nya anslag „n d† l„nge

	lea	aud1lc(pc),a3		; Pekare till samplingsdata
	lea	voice1(pc),a4		; Pekare till voiceinfon
	bsr	playvoice		; Spela kanal
	lea	aud2lc(pc),a3		; G”r samma sak f”r resten av kanalerna
	lea	voice2(pc),a4
	bsr.s	playvoice
	lea	aud3lc(pc),a3
	lea	voice3(pc),a4
	bsr.s	playvoice
	lea	aud4lc(pc),a3
	lea	voice4(pc),a4
	bsr.s	playvoice
	bra	setdma
				; Spela en kanal
playvoice:
	move.l	0(a0,d1.l),(a4) ; H„mta in kanalen till voice
	addq.l	#4,d1		; n„sta kanal i d1
	moveq	#0,d2
	move.b	$02(a4),d2
	and.b	#$F0,d2 	; Plocka ut om det finns n†gon instrument-data
	lsr.b	#4,d2
	move.b	(a4),d0
	and.b	#$F0,d0
	or.b	d0,d2
	tst.b	d2		; d2 vilket instrument 0-$1f
	beq	setregs 	; Inget nytt instrument?
	moveq	#0,d3
	lea	samplestarts(pc),a1	; Pekare till tabellen ”ver vart samplingarna b”rjar i minnet
	move.l	d2,d4		; d2 Instrument till d4
	subq.l	#$01,d2
	asl.l	#2,d2
	mulu	#$1E,d4		; R„tt offset f”r modulinstrumenten
	move.l	0(a1,d2.l),$04(a4)	; Pekare till sampling till voice
	move.w	0(a2,d4.l),$08(a4)	; L„ngden p† samplingen i bytes +22

	move.w	$02(a2,d4.l),$12(a4)	; Finetune + volym till voice	+24
					; Ingen looping h„r
	move.l	$04(a4),$0A(a4)		; Loopb”rjan till voice
	move.w	$06(a2,d4.l),$0E(a4)	; Loopl„ngd till voice
	move.l	$04(a2,d4.l),32(a4)	; Loopl„ngd till voice
	move.w	$12(a4),$08(a3) 	; voice-volym till aud
setregs:move.w	(a4),d0
	and.w	#$0FFF,d0		; Finns det n†gon tonangivelse?
	beq	checkcom2		; Nix, kolla om det finns kommandon
	move.b	$02(a4),d0		; Effectkommando 3? (Slida till angiven ton?)
	and.b	#$0F,d0
	cmp.b	#$03,d0
	bne.s	setperiod		; Nix
	bsr	setmyport		; Jepp, slida p†
	bra	checkcom2		; Testa flera kommandon oxo

setperiod:			; S„tt frekvensen p† kanalen
	move.w	(a4),d0
	and.w	#$0FFF,d0
	lea	superfastfinetune,a6
	moveq	#0,d6
	move.b	(a6,d0.w),d6
	moveq	#0,d7
	move.b	$12(a4),d7	; Finetunen
	mulu	#36*2,d7
	add.w	d6,d7
	add.l	#mt_PeriodTable,d7
	move.l	d7,a6
	move.w	(a6),$10(a4)

	move.w	$14(a4),d0
	move.w	d0,dmactrl
	clr.b	$1B(a4)

	clr.l	-132(a3)		; ¸terstarta samplingen vid 0

	move.l	$04(a4),(a3)
	move.w	$08(a4),$04(a3)
	move.w	$10(a4),$06(a3)
	move.w	$14(a4),d0
	or.w	d0,dmacon
	bra	checkcom2

setdma: move.w	dmacon,d0
	move.w	dmacon2,d7

	btst	#0,d0			;-------------------
	beq	wz_nch1 		;
	btst	#0,d7
	bne.s	intes„ttafrekviwiz1
	move.l	aud1lc(pc),wiz1lc	; S„tt Wizcats uppspelare
intes„ttafrekviwiz1:
	moveq	#0,d1			; med data
	moveq	#0,d2			;
	move.w	aud1len(pc),d1		;
	move.l	voice1+32(pc),d2	;
	add.l	d2,d1			;
	move.l	d1,wiz1len		;
	move.l	d2,wiz1rpt		;

wz_nch1:btst	#1,d0			;
	beq	wz_nch2 		;
	btst	#1,d7
	bne.s	intes„ttafrekviwiz2
	move.l	aud2lc(pc),wiz2lc	;
intes„ttafrekviwiz2:
	moveq	#0,d1			;
	moveq	#0,d2			;
	move.w	aud2len(pc),d1		;
	move.l	voice2+32(pc),d2	;
	add.l	d2,d1			;
	move.l	d1,wiz2len		;
	move.l	d2,wiz2rpt		;

wz_nch2:btst	#2,d0			;
	beq	wz_nch3 		;
	btst	#2,d7
	bne.s	intes„ttafrekviwiz3
	move.l	aud3lc(pc),wiz3lc	;
intes„ttafrekviwiz3:
	moveq	#0,d1			;
	moveq	#0,d2			;
	move.w	aud3len(pc),d1		;
	move.l	voice3+32(pc),d2	;
	add.l	d2,d1			;
	move.l	d1,wiz3len		;
	move.l	d2,wiz3rpt		;


wz_nch3:btst	#3,d0			;
	beq	wz_nch4 		;
	btst	#3,d7
	bne.s	intes„ttafrekviwiz4
	move.l	aud4lc(pc),wiz4lc	;
intes„ttafrekviwiz4:
	moveq	#0,d1			;
	moveq	#0,d2			;
	move.w	aud4len(pc),d1		;
	move.l	voice4+32(pc),d2	;
	add.l	d2,d1			;
	move.l	d1,wiz4len		;
	move.l	d2,wiz4rpt		;


wz_nch4:clr.w	dmacon2
	addi.w	#$10,pattpos
	cmpi.w	#$0400,pattpos
	bne.s	endread
nex:	clr.w	pattpos
	clr.b	break
	addq.b	#1,songpos
	andi.b	#$7F,songpos
	move.b	songpos(pc),d1
set1:	cmp.b	$12345678,d1		;$03B6
	bne.s	endread
set2:	move.b	$12345678,songpos 	;$03B7
endread:tst.b	break
	bne.s	nex
	rts

setmyport:
	move.w	(a4),d2
	and.w	#$0FFF,d2
	move.w	d2,$18(a4)
	move.w	$10(a4),d0
	clr.b	$16(a4)
	cmp.w	d0,d2
	beq.s	clrport
	bge.s	rt
	move.b	#$01,$16(a4)
	rts

clrport:clr.w	$18(a4)
rt:	rts

myport: move.b	$03(a4),d0
	beq.s	myslide
	move.b	d0,$17(a4)
	clr.b	$03(a4)
myslide:tst.w	$18(a4)
	beq.s	rt
	moveq	#0,d0
	move.b	$17(a4),d0
	tst.b	$16(a4)
	bne.s	mysub
	add.w	d0,$10(a4)
	move.w	$18(a4),d0
	cmp.w	$10(a4),d0
	bgt.s	myok
	move.w	$18(a4),$10(a4)
	clr.w	$18(a4)

myok:	move.w	$10(a4),$06(a3)
	rts

mysub:	sub.w	d0,$10(a4)
	move.w	$18(a4),d0
	cmp.w	$10(a4),d0
	blt.s	myok
	move.w	$18(a4),$10(a4)
	clr.w	$18(a4)
	move.w	$10(a4),$06(a3)
	rts

vib:
	move.b	$03(a4),d0
	beq.s	vi
	move.b	d0,$1A(a4)

vi:	move.b	$1B(a4),d0
	lea	sin(pc),a1
	lsr.w	#$02,d0
	and.w	#$1F,d0
	moveq	#0,d2
	move.b	0(a1,d0.w),d2
	move.b	$1A(a4),d0
	and.w	#$0F,d0
	mulu	d0,d2
	lsr.w	#$06,d2
	move.w	$10(a4),d0
	tst.b	$1B(a4)
	bmi.s	vibmin
	add.w	d2,d0
	bra.s	vib2

vibmin: sub.w	d2,d0
vib2:	move.w	d0,$06(a3)
	move.b	$1A(a4),d0
	lsr.w	#$02,d0
	and.w	#$3C,d0
	add.b	d0,$1B(a4)
	rts

nopp:	move.w	$10(a4),$06(a3)
	rts

checkcom:
	move.w	$02(a4),d0	; ˇr det ett effectkommando p† den h„r kanalen?
	and.w	#$0FFF,d0
	beq.s	nopp		; Nix
	move.b	$02(a4),d0	; Jepp, h„mta in vilket
	and.b	#$0F,d0 	; Plocka ut vilket kommando det „r
	tst.b	d0		; Inget h„r betyder arpeggio
	beq	arpeggio
	cmp.b	#$01,d0 	; Effectkommando 1, slida upp tonen
	beq	portup
	cmp.b	#$02,d0 	; Effectkommando 2, slida ner tonen
	beq	portdown
	cmp.b	#$03,d0 	; Effectkommando 3, slida mot ny not
	beq	myport
	cmp.b	#$04,d0 	; Effectkommando 4, Vibrato
	beq	vib
	cmp.b	#$05,d0 	; Effectkommando 5, Tone+vol slide
	beq	port_toneslide
	cmp.b	#$06,d0 	; Effectkommando 6, Vibrato+vol slide
	beq	vib_toneslide
	cmp.b	#$0E,d0
	beq	setallEcommands
SetBack:move.w	$10(a4),$06(a3) ; flytta period till aud
	CMP.B	#7,D0		; Effectkommando 7, Tremolo
	BEQ	mt_Tremolo
	cmp.b	#$0A,d0 	; Effectkommando A, slida volymen
	beq	volslide
ret2:	rts			; Slutjobbat

mt_Tremolo:			; n_tremolocmd +32
	move.b	3(a4),d0	; n_termolopos +33
	beq.s	mt_Tremolo2	; n_wavecontrol +34
	move.b	32(a4),d2
	and.b	#$0F,d0
	beq.s	mt_treskip
	and.b	#$F0,d2
	or.b	d0,d2
mt_treskip:
	move.b	3(a4),d0
	and.b	#$F0,d0
	beq.s	mt_treskip2
	and.b	#$0F,d2
	or.b	d0,d2
mt_treskip2:
	move.b	d2,32(a4)
mt_Tremolo2:
	move.b	33(a4),D0
	lea	sin(pc),a6	; a4 -> a6!
	lsr.w	#2,d0
	and.w	#$001F,d0
	moveq	#0,d2
	move.b	34(a4),d2
	lsr.b	#4,d2
	and.b	#$03,d2
	beq.s	mt_tre_sine
	lsl.b	#3,d0
	cmp.b	#1,d2
	beq.s	mt_tre_rampdown
	move.b	#255,d2
	bra.s	mt_tre_set
mt_tre_rampdown:
	tst.b	33(a4)
	bpl.s	mt_tre_rampdown2
	move.b	#255,d2
	sub.b	d0,d2
	bra.s	mt_tre_set
mt_tre_rampdown2:
	move.b	d0,d2
	bra.s	mt_tre_set
mt_tre_sine:
	move.b	0(a6,d0.w),d2
mt_tre_set:
	move.b	32(a4),d0
	and.w	#15,d0
	mulu	d0,d2
	lsr.w	#6,d2
	moveq	#0,d0
	move.b	13(a4),d0	; Volym
	tst.b	33(a4)
	bmi.s	mt_TremoloNeg
	add.w	d2,d0
	bra.s	mt_Tremolo3
mt_TremoloNeg:
	sub.w	d2,d0
mt_Tremolo3:
	bpl.s	mt_TremoloSkip
	clr.w	d0
mt_TremoloSkip:
	cmp.w	#$40,d0
	bls.s	mt_TremoloOk
	moveq	#$40,d0
mt_TremoloOk:
	move.w	d0,8(a3)
	move.b	32(a4),d0
	lsr.w	#2,d0
	and.w	#$003C,d0
	add.b	d0,33(a4)
	rts

volslide:			; Kommando A
	moveq	#0,d0
	move.b	$03(a4),d0	; Ta reda p† om den skall slida upp eller ner volymen
	lsr.b	#4,d0
	tst.b	d0
	beq.s	voldown
mt_VolSlideUp:
	add.b	d0,$13(a4)	; Slida upp volymen s† mycket som anges
	cmpi.b	#$40,$13(a4)	; full volym redan?
	bmi.s	vol2
	move.b	#$40,$13(a4)	; S„tt full volym
vol2:	move.w	$12(a4),$08(a3) ; S„tt volymen till aud
	rts

voldown:moveq	#0,d0		; Tona ner volymen	A
	move.b	$03(a4),d0
	and.b	#$0F,d0
mt_VolSlideDown2:
	sub.b	d0,$13(a4)	; Minska volymen
	bpl.s	vol3
	clr.b	$13(a4) 	; S„tt till 0
vol3:	move.w	$12(a4),$08(a3) ; S„tt volymen till aud
	rts

portup: moveq	#0,d0		; Kommando 1
	move.b	$03(a4),d0	; H„mta antal slidsteg
portupfast:
	sub.w	d0,$10(a4)	; Slida upp tonen
	move.w	$10(a4),d0
	and.w	#$0FFF,d0
	cmp.w	#$71,d0 	; H”sta tonen redan?
	bpl.s	por2
	andi.w	#$F000,$10(a4)
	ori.w	#$71,$10(a4)	; S„tt h”gsta ton isf
por2:	move.w	$10(a4),d0
	and.w	#$0FFF,d0
	move.w	d0,$06(a3)	; Ny period (ton = period)
	rts

port_toneslide:
	bsr	myslide
	bra.s	volslide

vib_toneslide:
	bsr	vi
	bra.s	volslide

portdown:
	clr.w	d0
	move.b	$03(a4),d0
portdownfast:
	add.w	d0,$10(a4)
	move.w	$10(a4),d0
	and.w	#$0FFF,d0
	cmp.w	#$0358,d0
	bmi.s	por3
	andi.w	#$F000,$10(a4)
	ori.w	#$0358,$10(a4)
por3:	move.w	$10(a4),d0
	and.w	#$0FFF,d0
	move.w	d0,$06(a3)
	rts

checkcom2:		; Fler kommandon...
	move.b	$02(a4),d0
	and.b	#$0F,d0
	cmp.b	#$09,d0		; Effectkommando 9, sample offset
	beq.s	setsampleoffset
	cmp.b	#$0D,d0 	; Effectkommando D, hoppa till n„sta pattern
	beq.s	pattbreak
	cmp.b	#$0B,d0 	; Effectkommando B, hoppa till angivet pattern
	beq.s	posjmp
	cmp.b	#$0C,d0 	; Effectkommando C, s„tt volym
	beq.s	setvol
	cmp.b	#$0E,d0
	beq	setallEcommands
	cmp.b	#$0F,d0 	; Effectkommando F, s„tt fart
	beq	setspeed
	rts


setsampleoffset:
	move.w	dmacon,d7
	move.w	20(a4),d0
	and.w	d0,d7
	bne.s	redansattg”ringemer
	or.w	d0,dmacon
	or.w	d0,dmacon2
redansattg”ringemer:
	moveq	#0,d0
	moveq	#0,d7
	move.b	$03(a4),d0
	beq.s	inteennysampleoffset
	move.b	d0,12(a3)
inteennysampleoffset:
	move.b	12(a3),D0
	beq.s	skitideth„rihuvudtaget
	lsl.l	#8,d0			; * 256
	move.w	4(a3),d7
	cmp.l	d7,d0
	bge.s	skitideth„rihuvudtaget
	move.l	d0,-132(a3)
skitideth„rihuvudtaget:
	rts


pattbreak:			; Kommando D, n„sta pattern
	st	break
	rts

posjmp: move.b	$03(a4),d0	; Kommando B, hoppa till angivet pattern
	subq.b	#$01,d0
	move.b	d0,songpos	; L„gg det nya pattert i songpos
	st	break
	rts

setvol: moveq	#0,d0		; Kommando C, s„tt volym
	move.b	$03(a4),d0
	cmp.w	#$40,d0 	; Full volym?
	bmi.s	vol4
	move.b	#$40,$03(a4)	; S„tt full volym
vol4:	move.w	$02(a4),$08(a3) ; S„tt volym i aud
	move.b	$03(a4),$13(a4) ; S„tt volym i voice
	rts

setspeed:			; Kommando F, s„tt fart
	move.b	$03(a4),d0
	beq.s	rts2
	move.w	d0,speed	; S„tt fart
	clr.w	counter 	; Rensa counter
rts2:	rts

setallEcommands:
	moveq	#0,d0
	move.b	$03(a4),d0
	lsr.b	#4,d0
	cmp.b	#1,d0
	beq.s	mt_FinePortaUp
	cmp.b	#2,d0
	beq	mt_FinePortaDown
	cmp.b	#$A,d0
	beq.s	mt_VolumeFineUp
	cmp.b	#$B,D0
	beq.s	mt_VolumeFineDown
	bra	SetBack

mt_VolumeFineUp:
	tst.w	counter
	bne	ret2
	moveq	#0,d0
	move.b	3(a4),d0
	and.b	#$F,d0
	bra	mt_VolSlideUp

mt_VolumeFineDown:
	tst.w	counter
	bne	ret2
	moveq	#0,d0
	move.b	3(a4),d0
	and.b	#$0F,d0
	bra	mt_VolSlideDown2


mt_FinePortaUp:
	tst.w	counter
	bne	ret2
	moveq	#0,d0
	move.b	3(a4),d0
	and.b	#$F,d0
	bra	portupfast

mt_FinePortaDown:
	tst.w	counter
	bne	ret2
	moveq	#0,d0
	move.b	3(a4),d0
	and.b	#$F,d0
	bra	portdownfast


extraoffsetadded1:
	dc.w	0
extraoffsetadded2:
	dc.w	0

ultradummy:
	dc.l	0
handle: dc.w	0
filnamn:dc.l	0
lasbuffer:
	dc.l	0
laslangd:
	dc.l	0
controlc:
	dc.l	0


	CNOP	0,4

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

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,$FE
	DC.W $F0,$E2,$D6,$CA,$BE,$B4,$AA,$A0,$97,$8F,$87
	DC.W $7F,$78,$71,$00,$00

speed	DC.W $06
counter DC.W $00
songpos DC.B $00
break	DC.B $00
pattpos DC.W $00

dmacon:		DC.W $00
dmacon2:	dc.w $00
samplestarts:	DS.L $1F

superfastfinetune:
	ds.b	1000
mt_PeriodTable:
; Tuning 0, Normal
	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
; Tuning 1
	dc.w	850,802,757,715,674,637,601,567,535,505,477,450
	dc.w	425,401,379,357,337,318,300,284,268,253,239,225
	dc.w	213,201,189,179,169,159,150,142,134,126,119,113
; Tuning 2
	dc.w	844,796,752,709,670,632,597,563,532,502,474,447
	dc.w	422,398,376,355,335,316,298,282,266,251,237,224
	dc.w	211,199,188,177,167,158,149,141,133,125,118,112
; Tuning 3
	dc.w	838,791,746,704,665,628,592,559,528,498,470,444
	dc.w	419,395,373,352,332,314,296,280,264,249,235,222
	dc.w	209,198,187,176,166,157,148,140,132,125,118,111
; Tuning 4
	dc.w	832,785,741,699,660,623,588,555,524,495,467,441
	dc.w	416,392,370,350,330,312,294,278,262,247,233,220
	dc.w	208,196,185,175,165,156,147,139,131,124,117,110
; Tuning 5
	dc.w	826,779,736,694,655,619,584,551,520,491,463,437
	dc.w	413,390,368,347,328,309,292,276,260,245,232,219
	dc.w	206,195,184,174,164,155,146,138,130,123,116,109
; Tuning 6
	dc.w	820,774,730,689,651,614,580,547,516,487,460,434
	dc.w	410,387,365,345,325,307,290,274,258,244,230,217
	dc.w	205,193,183,172,163,154,145,137,129,122,115,109
; Tuning 7
	dc.w	814,768,725,684,646,610,575,543,513,484,457,431
	dc.w	407,384,363,342,323,305,288,272,256,242,228,216
	dc.w	204,192,181,171,161,152,144,136,128,121,114,108
; Tuning -8
	dc.w	907,856,808,762,720,678,640,604,570,538,508,480
	dc.w	453,428,404,381,360,339,320,302,285,269,254,240
	dc.w	226,214,202,190,180,170,160,151,143,135,127,120
; Tuning -7
	dc.w	900,850,802,757,715,675,636,601,567,535,505,477
	dc.w	450,425,401,379,357,337,318,300,284,268,253,238
	dc.w	225,212,200,189,179,169,159,150,142,134,126,119
; Tuning -6
	dc.w	894,844,796,752,709,670,632,597,563,532,502,474
	dc.w	447,422,398,376,355,335,316,298,282,266,251,237
	dc.w	223,211,199,188,177,167,158,149,141,133,125,118
; Tuning -5
	dc.w	887,838,791,746,704,665,628,592,559,528,498,470
	dc.w	444,419,395,373,352,332,314,296,280,264,249,235
	dc.w	222,209,198,187,176,166,157,148,140,132,125,118
; Tuning -4
	dc.w	881,832,785,741,699,660,623,588,555,524,494,467
	dc.w	441,416,392,370,350,330,312,294,278,262,247,233
	dc.w	220,208,196,185,175,165,156,147,139,131,123,117
; Tuning -3
	dc.w	875,826,779,736,694,655,619,584,551,520,491,463
	dc.w	437,413,390,368,347,328,309,292,276,260,245,232
	dc.w	219,206,195,184,174,164,155,146,138,130,123,116
; Tuning -2
	dc.w	868,820,774,730,689,651,614,580,547,516,487,460
	dc.w	434,410,387,365,345,325,307,290,274,258,244,230
	dc.w	217,205,193,183,172,163,154,145,137,129,122,115
; Tuning -1
	dc.w	862,814,768,725,684,646,610,575,543,513,484,457
	dc.w	431,407,384,363,342,323,305,288,272,256,242,228
	dc.w	216,203,192,181,171,161,152,144,136,128,121,114


hardvolumesetright:	; 10*lg(x/64)+20
	dc.w	0,2,5,7,8,9,10,10		; 0-7
	dc.w	11,11,12,12,13,13,13,14		; 8-15
	dc.w	14,14,14,15,15,15,15,16		; 16-23
	dc.w	16,16,16,16,16,17,17,17		; 24-31
	dc.w	17,17,17,18,18,18,18,18		; 32-39
	dc.w	18,18,18,18,18,18,19,19		; 40-47
	dc.w	19,19,19,19,19,19,19,19		; 48-55
	dc.w	19,19,20,20,20,20,20,20		; 56-63
	dc.w	20				; 64


datagrejer:
	dc.l	0
workspc dc.l	0

keyclick:
	dc.b	0
infotext:
	dc.b	'bacpEeqw'
	dc.b	10,13
	dc.b	'Wellcome to CODīreplay!!!!!!!!',10,13
	dc.b	'Uppdating the sound with 25 kHz but the soundchip',10,13
	dc.b	'works with 50 kHz!!! Useing the hardware for',10,13
	dc.b	'some volumecontrol. ',10,13,10,13
	dc.b	'Without timer A!!!!!!',10,13,10,13
	dc.b	10,13,10,13,10,13
	dc.b	'(Max ÷ 250 kb)',10,13
	dc.b	'Modules name: ',0

matain: dc.b	50,0
musiknamn:
	ds.b	80

spelatext:
	dc.b	10,13,10,13,10,13,'fNow playing: ',0

	even

	END
