	section	Sfx,code_c
	opt	c-

; ©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©
; © Sound FX 1.8 replay - NO INTERRUPT - Devpac ][. ©
; ©   Coded by Kerni of Magnetic Fields (C) 1991.   ©
; ©     RASTER TIME = $2c-$40 (21 raster lines)     ©      
; ©-------------------------------------------------©
; ©This player was rewritten so that it can be used ©
; © in demos by calling it each frame as with the   ©
; ©  Noisetracker & Soundtracker replay routines    ©
; © The original version ran under a lev6 interrupt ©
; © (!) this version needs the following info....   ©
; © Speed of Song: Change 'Speed' to speed of song  ©
; © call 'SfxStart' once, 'SfxPlay' every frame and ©
; © 'SfxStop' to end music, Datas at 'SfxDatas' !!  © 
; ©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©

	bsr	SfxStart
Test:	;move.l	$dff004,d2
	;and.l	#$0001ff00,d2
	;cmp.l	#$00001000,d2
	;bne.s	Test
	cmp.b	#200,$dff006
	bne.s	test
;	move.w	#$00f,$dff180	
	bsr	SfxPlay
;	move.w	#$000,$dff180

Mouse:	btst	#6,$bfe001
	bne	Test
End:	bsr	SfxStop
	bclr	#1,$bfe001

	moveq	#0,d0
	rts

SfxStart:
	bset	#1,$bfe001
;	move.l	4,a6			
;	move.l	SongPointer,a1	
;	jsr	-534(A6)		
;	move.l	#SfxDatas,d0		
;	addq.l	#4,d0			
;	moveq	#2,d1			
;	jsr	-198(a6)		
	moveq	#-1,d5			
	move.l	d0,a1			
	move.l	Songpointer,a0	
	move.l	#SfxDatas,d1
	addq.l	#4,d1			
	lsr.l	#2,d1			
	subq	#1,d1			
	move.l	SongPointer,a0	
	add	#60,a0			
	move.b	470(a0),AnzPat+1	
	move	4(A0),DelayValue 	
	bsr	SongLen			
	add.l	d0,a0			
	add.w	#600,a0			
	move.l	SongPointer,a2
	lea	Instruments,a1	
	moveq	#14,d7			
CalcIns:
	move.l	a0,(A1)+		
	add.l	(a2)+,a0		
	dbf	d7,CalcIns
;	lea	CiaaResource,a1	
;	moveq	#0,d0			
;	jsr	-498(A6)		
;	move.l	d0,CiaaBase		
;	move.l	d0,a6
	jsr	PlayDisable		
	jsr	PlayInit		
	jsr	PlayEnable		
	jsr	InitTimer		
	rts
SfxPlay:				
	movem.l	d0-d7/a0-a6,-(SP)
	addq.w	#1,Timer		
;	move.w	speed,d7
	cmp.w	#speed,Timer
	bne	CheckEffects		
	clr.w	Timer			
	bsr 	PlaySound		
	movem.l	(SP)+,d0-d7/a0-a6
	rts
SfxStop:
	clr.l	$dff0a8
	clr.l	$dff0b8
	clr.l	$dff0c8
	clr.l	$dff0d8
	rts
SongLen:
	movem.l	d1-d7/a0-a6,-(SP)
	move.l	SongPointer,a0
	lea	532(A0),a0
	move	AnzPat(pc),d2		
	subq	#1,d2			
	moveq	#0,d1
	moveq	#0,d0
SongLenLoop:
	move.b	(a0)+,d0		
	cmp.b	d0,d1			
	bhi.s	LenHigher		
	move.b	d0,d1		
LenHigher:
	dbf	d2,SongLenLoop
	move.l	d1,d0			
	addq	#1,d0			
	mulu	#1024,d0		
	movem.l	(SP)+,d1-d7/a0-a6
	rts
InitTimer:
;	move.b	#%10000001,$bfee01	
;	lea	DelayValue(pc),a1
;	move.b	1(a1),$bfe401		
;	move.b	0(a1),$bfe501		
	rts
PlayInit:
	lea	Instruments(pc),a0	
	moveq	#14,d7			
InitLoop:	
	move.l	(A0)+,a1		
	clr.l	(A1)			
	dbf	d7,InitLoop
	rts
PlayEnable:
	lea	$dff000,a0		
	move.w	#-1,PlayLock		
	clr	$a8(A0)			
	clr	$b8(A0)
	clr	$c8(a0)
	clr	$d8(a0)
	clr.w	Timer			
	clr.l	TrackPos		
	clr.l	PosCounter		
	rts
PlayDisable:
	lea	$dff000,a0		
	clr.w	PlayLock		
	clr	$a8(a0)			
	clr	$b8(a0)
	clr	$c8(a0)
	clr	$d8(a0)
	move.w	#$f,$96(A0)		
	rts
IntCode:
	bsr	SfxPlay		
	moveq	#0,d0			
	rts
CheckEffects:
	moveq	#3,d7			
	lea	StepControl0,a4
	lea	Channel0(pc),a6	
	lea	$dff0a0,a5		
EffLoop:
	movem.l	d7/a5,-(SP)
	bsr.s	MakeEffects		
	movem.l	(Sp)+,d7/a5
NoEff:
	add	#8,a4
	add	#$10,a5			
	add	#22,a6			
	dbf	d7,EffLoop
	movem.l	(a7)+,d0-d7/a0-a6
	rts
MakeEffects:
	move	(A4),d0
	beq.s	NoStep
	bmi.s	StepItUp
	add	d0,2(A4)
	move	2(A4),d0
	move	4(A4),d1
	cmp	d0,d1
	bhi.s	StepOk
	move	d1,d0
StepOk:
	move	d0,6(a5)
	move	D0,2(A4)
	rts
StepItUp:
	add	d0,2(A4)
	move	2(A4),d0
	move	4(A4),d1
	cmp	d0,d1
	blt.s	StepOk
	move	d1,d0
	bra.s	StepOk
NoStep:
	move.b	2(a6),d0
	and.b	#$0f,d0
	cmp.b	#1,d0
	beq	Arpeggio
	cmp.b	#2,d0
	beq	pitchbend
	cmp.b	#7,d0
	beq.s	SetStepUp
	cmp.b	#8,d0
	beq.s	SetStepDown
	rts
SetStepUp:
	moveq	#0,d4
StepFinder:
	clr	(a4)
	move	(A6),2(a4)
	moveq	#0,d2
	move.b	3(a6),d2
	and	#$0f,d2
	tst	d4
	beq.s	NoNegIt
	neg	d2
NoNegIt:	
	move	d2,(a4)
	moveq	#0,d2
	move.b	3(a6),d2
	lsr	#4,d2
	move	(a6),d0
	lea	NoteTable,a0
StepUpFindLoop:
	move	(A0),d1
	cmp	#-1,d1
	beq.s	EndStepUpFind
	cmp	d1,d0
	beq.s	StepUpFound
	addq	#2,a0
	bra.s	StepUpFindLoop
StepUpFound:
	lsl	#1,d2
	tst	d4
	bne.s	NoNegStep
	neg	d2
NoNegStep:
	move	(a0,d2.w),d0
	move	d0,4(A4)
	rts
EndStepUpFind:
	move	d0,4(A4)
	rts
SetStepDown:
	st	d4
	bra.s	StepFinder
StepControl0:
	dc.l	0,0
StepControl1:
	dc.l	0,0
StepControl2:
	dc.l	0,0
StepControl3:
	dc.l	0,0
Arpeggio:
	lea	ArpeTable,a0
	moveq	#0,d0
	move	Timer,d0
	subq	#1,d0
	lsl	#2,d0
	move.l	(A0,d0.l),a0
	jmp	(A0)
Arpe4:	lsl.l	#1,d0
	clr.l	d1
	move.w	16(a6),d1
	lea.l	NoteTable,a0
Arpe5:	move.w	(a0,d0.l),d2
	cmp.w	(a0),d1
	beq.s	Arpe6
	addq.l	#2,a0
	bra.s	Arpe5
Arpe1:	clr.l	d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	bra.s	Arpe4
Arpe2:	clr.l	d0
	move.b	3(a6),d0
	and.b	#$0f,d0
	bra.s	Arpe4
Arpe3:	move.w	16(a6),d2
Arpe6:	move.w	d2,6(a5)
	rts
Pitchbend:
	clr.l	d0
	move.b	3(a6),d0
	lsr.b	#4,d0
	tst.b	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
	tst.b	d0
	beq.s	pitch3
	sub.w	d0,(a6)
	move.w	(a6),6(a5)
Pitch3:	rts



PlaySound:
	move.l	SongPointer(pc),a0	
	add	#60,a0			
	move.l	a0,a3
	move.l	a0,a2
	lea	600(A0),a0		
	add	#472,a2			
	add	#12,a3			
	move.l	TrackPos(pc),d0		
	clr.l	d1
	move.b	(a2,d0.l),d1		
	moveq	#10,d7
	lsl.l	d7,d1			
	add.l	PosCounter,d1		
	clr.w	DmaCon
	lea	StepControl0,a4
	lea	$dff0a0,a5		
	lea	Channel0(pc),a6	
	moveq	#3,d7			
	lea	datas(pc),a2
SoundHandleLoop:
	bsr	PlayNote
	add	#8,a4
	add.l	#$10,a5
	add.l	#22,a6
	dbf	d7,SoundHandleLoop

	lea	datas(pc),a2
	lea	equals(pc),a3
	move.l	#0,(a3)
	move.l	#0,4(a3)
	move.w	#3,d7
Dowot:
	move.l	(a2)+,a5
	move.l	(a2)+,a6


	cmp.l	#dummyarea,a5
	beq.s	OobyDooby
	move.w	(a2),(a3)
OobyDooby:
	addq.w	#2,a2
	addq.w	#2,a3

	cmp	#-2,(A6)		
	bne.s	NoStop			
	clr	8(A5)
	bra	Super
NoStop:
	move.l	4(a6),0(a5)		
	move.w	8(a6),4(a5)		
	move.w	0(a6),6(a5)		
Super:
	move.w	20(a6),d0		
	or.w	d0,DmaCon		

	dbf	d7,dowot

	move	DmaCon(pc),d0		
	bset	#15,d0			
	move.w	d0,$dff096		


	move	#50,d0					*******300
Delay2:
	dbf	d0,Delay2

	lea	Channel3(pc),a6
	lea	$dff0d0,a5
	moveq	#3,d7
SetRegsLoop:
	move.l	10(A6),(a5)		
	move	14(A6),4(A5)		
NoSetRegs:
	sub	#22,a6			
	sub	#$10,a5			
	dbf	d7,SetRegsLoop
	tst	PlayLock
	beq.s	NoEndPattern
	add.l	#16,PosCounter		
	cmp.l	#1024,PosCounter	
	blt.s	NoEndPattern
	clr.l	PosCounter		
	addq.l	#1,TrackPos		
NoAddPos:
	move.w	Anzpat(pc),d0		
	move.l	TrackPos(pc),d1		
	cmp.w	d0,d1			
	bne.s	NoEndPattern		
	clr.l	TrackPos		
NoEndPattern:
	rts

datas:	dc.l	0,0
	dc.w	0
	dc.l	0,0
	dc.w	0
	dc.l	0,0
	dc.w	0
	dc.l	0,0
	dc.w	0
equals:	dc.w	0,0,0,0



PlayNote:
	clr.l	(A6)
	tst	PlayLock		
	beq	NoGetNote		
	move.l	(a0,d1.l),(a6)		
NoGetNote:
	addq.l	#4,d1			
	clr.l	d2
	cmp	#-3,(A6)		
	beq	NoInstr2		
	move.b	2(a6),d2			
	and.b	#$f0,d2			
	lsr.b	#4,d2			
	tst.b	d2			
	beq	NoInstr2			
	clr.l	d3
	lea.l	Instruments(pc),a1	
	move.l	d2,d4			
	subq	#1,d2
	lsl	#2,d2			
	mulu	#30,d4			
	move.l	(a1,d2.w),4(a6)		
	move.w	(a3,d4.l),8(a6)		
	move.w	2(a3,d4.l),18(a6)	
	move.w	4(a3,d4.l),d3		
	tst	d3			
	beq.s	NoRepeat								
	move.l	4(a6),d2		
	add.l	d3,d2			
	move.l	d2,10(a6)		
	move.w	6(a3,d4),14(a6)		
	move.w	18(a6),d3 		
	bra.s	NoInstr
NoRepeat:
	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),d3 		
NoInstr:
	move.b	2(A6),d2
	and	#$0f,d2
	cmp.b	#5,d2
	beq.s	ChangeUpVolume
	cmp.b	#6,d2
	bne	SetVolume2
	moveq	#0,d2
	move.b	3(A6),d2
	sub	d2,d3		
	tst	d3
	bpl	SetVolume2	
	clr	d3
	bra	SetVolume2
ChangeUpVolume:
	moveq	#0,d2
	move.b	3(A6),d2
	add	d2,d3
	tst	d3
	cmp	#64,d3
	ble	SetVolume2
	move	#64,d3
SetVolume2:
	move	d3,8(A5)
NoInstr2:
	cmp	#-3,(A6)		
	bne.s	NoPic		
	clr	2(A6)			
	bra.s	NoNote	
NoPic:
	tst	(A6)			
	beq.s	NoNote			
	clr	(a4)
	move.w	(a6),16(a6)		
	move.w	20(a6),$dff096		

;	move	#50,d7				*********300
;Delay1:
;	dbf	d7,Delay1		

	move.l	a5,(a2)+
	move.l	a6,(a2)+
	move.w	d3,(a2)+
	rts
NoNote:
	move.l	#dummyarea,(a2)+
	move.l	#dummyarea,(a2)+
	move.w	d3,(a2)+
	rts

dummyarea:
	dcb.l	100,0

ArpeTable:
	dc.l	Arpe1
	dc.l	Arpe2
	dc.l	Arpe3
	dc.l	Arpe2
	dc.l	Arpe1
Channel0:
	dcb.l	5,0			
	dc.w	1			
Channel1:	
	dcb.l	5,0			
	dc.w	2
Channel2:	
	dcb.l	5,0			
	dc.w	4
Channel3:	
	dcb.l	5,0			
	dc.w	8
Instruments:
	dcb.l	15,0			
PosCounter:
	dc.l	0			
TrackPos:
	dc.l	0			
Timer:
	dc.w	0			
DmaCon:
	dc.w	0			
AnzPat:
	dc.w	1			
PlayLock:
	dc.w	0			
DelayValue:
	dc.w	14565
SongPointer:
	dc.l	LenghtTable
Reserve:
	dc.w	856,856,856,856,856,856,856,856,856,856,856,856
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	  
	dc.w	113,113,113,113,113,113,113,113,113,113,113,113	  
	dc.w	-1

LenghtTable:

Songstr equ lenghttable+60

Speed:	equ	6

SfxDatas:

	incbin	"df0:sfx.cubik(loudremix)"
	even
	

