;	GLUE MASTER'S SMALLER SIZE TUNE PLAYER
;	CREATED TO FIT IN A BOOT-INTRO !!!
;	MODIFIED 26/2-90 TO BE EVEN SHORTER ...
;	AND RE-ORGANIZED 25/6-90 TO SUPPLY A MODULE-FORMAT!!!
;
;	THIS IS THE CREATER SOURCE...FOR MY OWN USE ONLY!!!
;
;	© 1990 GLUE MASTER

W=	255
BRK=	254
ZA=	0	;C
SA=	2
XA=	4
DA=	6
CA=	8
VA=	10
GA=	12
BA=	14
HA=	16
NA=	18
JA=	20
MA=	22
ZB=	24	;C
SB=	26
XB=	28
DB=	30
CB=	32
VB=	34
GB=	36
BB=	38
HB=	40
NB=	42
JB=	44
MB=	46
ZC=	48	;C
SC=	50
XC=	52
DC=	54
CC=	56
VC=	58
GC=	60
BC=	62
HC=	64
NC=	66
JC=	68
MC=	70
ZD=	72	;C
SD=	74
XD=	76
DD=	78
CD=	80
VD=	82
GD=	84
BD=	86
HD=	88
ND=	90
JD=	92
MD=	94
ZE=	96	;C

OPENLIBRARY= 	-408
CLOSELIBRARY= 	-414
FORBID= 	-132
PERMIT= 	-138
EXECBASE= 	$04

;MUS_MODULE=	$40000

>EXTERN "MUS_MODULES/GNU-SONG",MUS_MODULE

ORG $30000
LOAD $30000
S:
	MOVEM.L	D0-D7/A0-A6,-(A7)
	MOVE.L	EXECBASE,A6
	JSR	FORBID(A6)

	BSR.L MUS_INIT

;JMP END
	MOVE.W %000001000001000,$098(A6)
TESTTOP:
	TST.B $06(A6)
	BNE.S TESTTOP
	MOVE.W #$4020,$9A(A6)
	MOVE.W #$0020,$9C(A6)


VBLIRQ:
	BTST #6,$BFE001
	BEQ END
	MOVE.W $1E(A6),D0
	AND.W #$0020,D0
	BEQ.S VBLIRQ
	MOVE.W #$0020,$9C(A6)

	MOVE.W #$0FF0,$DFF180
SHOWWAIT:
	CMP.B #$60,$DFF006
	BNE.S SHOWWAIT
	MOVE.W #$00F0,$DFF180

;;;;;;;;;;;;;;;;;;;;;;;;;;;MAIN ROUTINE;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	BSR.S MUS_PLAY

OVERANDOUT:
	MOVE.L MUS_CURRPAT,A0
	MOVE.W MUS_MODPAT,D0

BLINKET:
	MOVE.B 0(A0,D0.W),D1
	CMP.B #255,D1
	BEQ.S NOBLINK
	MOVE.W #$0000,$DFF182
NOBLINK:
	MOVE.W D0,D1
	ROL.W #4,D1
	AND.W #$0F00,D1
	AND.W #$000F,D0
	OR.W D1,D0
	MOVE.W D0,$DFF180

;	MOVE.W #$000F,$DFF180
	BRA VBLIRQ

;;;;;;;;;;;;;;;;;;;;;;
END:		
	MOVE.W #$C000,$09A(A6)
	MOVE.W	#$000F,$096(A6)
	MOVE.L	EXECBASE,A6
	JSR	PERMIT(A6)
	MOVEM.L	(A7)+,D0-D7/A0-A6
ERROR:
	MOVE.W	#$C020,$DFF09A
	MOVEQ #0,D0
	RTS	; RETURN TO SEKA/CLI




;;;;;;;;;;;;;;;;;;;;;;;;;;;

MUS_PLAY:
	LEA $DFF000,A6

MUS_S1:	CMP.W #0,MUS_VOL1		;SUSTAIN-LEVEL
	BLS.S MUS_SETVOL2
MUS_D1:	SUB.W #1,MUS_VOL1		;DECAY-RATE
MUS_SETVOL2:
MUS_S2:	CMP.W #0,MUS_VOL2		;
	BLS.S MUS_SETVOL3
MUS_D2:	SUB.W #1,MUS_VOL2		;
MUS_SETVOL3:
MUS_S3:	CMP.W #0,MUS_VOL3		;
	BLS.S MUS_SETVOL4
MUS_D3:	SUB.W #1,MUS_VOL3		;
MUS_SETVOL4:
MUS_S4:	CMP.W #$18,MUS_VOL4		;
	BLS.S MUS_TESTNEWTONE
MUS_D4:	SUB.W #1,MUS_VOL4		;

MUS_TESTNEWTONE:
	ADD.B #1,MUS_WAITPOS
MUS_RP:	CMP.B #4,MUS_WAITPOS	;REPLAY-SPEED
	BCS MUS_SOUNDFIN

MUS_DOSONG:
	CLR.B MUS_WAITPOS
	ADD.W #4,MUS_MODPAT
MUS_PL:	CMP.W #[64*4],MUS_MODPAT	;PATTERN-LENGTH * 4
	BCS.S MUS_CONTPAT
	CLR.W MUS_MODPAT
	ADD.W #1,MUS_MODSONG
	MOVE.W MUS_MODSONG,D0
	LEA MUS_MODULE+151,A0	;SONG-START
	MOVE.B (A0,D0.W),D1
	CMP.B #$FF,D1
	BNE.S MUS_CONTSONG
	CLR.W MUS_MODSONG
	MOVE.B (A0),D1

MUS_CONTSONG:
	AND.W #$00FF,D1
	ASL.W #2,D1
	LEA MUS_PATTERNS(PC),A0
	MOVE.L 0(A0,D1.W),A2
	MOVE.L A2,MUS_CURRPAT

MUS_CONTPAT:
	MOVE.L MUS_CURRPAT,A0
	MOVE.W MUS_MODPAT,D0

MUS_CHAN1HANDL:
	MOVE.B 0(A0,D0.W),D1
	CMP.B #BRK,D1
	BNE.S MUS_CHAN1NOBRK
	CLR.W MUS_VOL1
	BRA.S MUS_CHAN2HANDL
MUS_CHAN1NOBRK:
	CMP.B #W,D1
	BEQ.S MUS_CHAN2HANDL
	BSR.L MUS_GETPER
	MOVE.W D1,$0A6(A6)
MUS_A1:	MOVE.W #$40,MUS_VOL1	;ATTACK-LEVEL
MUS_CHAN2HANDL:
	MOVE.B 1(A0,D0.W),D1
	CMP.B #BRK,D1
	BNE.S MUS_CHAN2NOBRK
	CLR.W MUS_VOL2
	BRA.S MUS_CHAN3HANDL
MUS_CHAN2NOBRK:
	CMP.B #W,D1
	BEQ.S MUS_CHAN3HANDL
	BSR.S MUS_GETPER
	MOVE.W D1,$0B6(A6)
MUS_A2:	MOVE.W #$28,MUS_VOL2	;
MUS_CHAN3HANDL:
	MOVE.B 2(A0,D0.W),D1
	CMP.B #BRK,D1
	BNE.S MUS_CHAN3NOBRK
	CLR.W MUS_VOL3
	BRA.S MUS_CHAN4HANDL
MUS_CHAN3NOBRK:
	CMP.B #W,D1
	BEQ.S MUS_CHAN4HANDL
	BSR.S MUS_GETPER
	MOVE.W D1,$0C6(A6)
MUS_A3:	MOVE.W #$30,MUS_VOL3	;
MUS_CHAN4HANDL:
	MOVE.B 3(A0,D0.W),D1
	CMP.B #BRK,D1
	BNE.S MUS_CHAN4NOBRK
	CLR.W MUS_VOL4
	BRA.S MUS_SOUNDFIN
MUS_CHAN4NOBRK:
	CMP.B #W,D1
	BEQ.S MUS_SOUNDFIN
	BSR.S MUS_GETPER
	MOVE.W D1,$0D6(A6)
MUS_A4:	MOVE.W #$30,MUS_VOL4	;

MUS_SOUNDFIN:
	MOVE.W MUS_VOL1,$0A8(A6)
	MOVE.W MUS_VOL2,$0B8(A6)
	MOVE.W MUS_VOL3,$0C8(A6)
	MOVE.W MUS_VOL4,$0D8(A6)
	RTS

;;;;;;;;;;;

MUS_GETPER:
	LEA MUS_PERIODTAB(PC),A1
	AND.W #$00FE,D1
	MOVE.W 0(A1,D1.W),D1
	RTS

;;;;;;;;;;;;;;;;;;;

MUS_INIT:
	LEA $DFF000,A6
	LEA MUS_MODULE,A5

	MOVE.W #$000F,$096(A6)		;SET WAVEFORMS
	MOVE.L A5,D0
	ADD.L #22,D0
	MOVE.L D0,$0A0(A6)
	MOVE.W #16,$0A4(A6)
	ADD.L #32,D0
	MOVE.L D0,$0B0(A6)
	MOVE.W #16,$0B4(A6)
	ADD.L #32,D0
	MOVE.L D0,$0C0(A6)
	MOVE.W #16,$0C4(A6)
	ADD.L #32,D0
	MOVE.L D0,$0D0(A6)
	MOVE.W #16,$0D4(A6)
	MOVE.W #$800F,$096(A6)

	MOVE.B 21(A5),D1	;PATTERN-LENGTH
	AND.W #$FF,D1
	ASL.W #2,D1		; * 4
	MOVE.W D1,MUS_PL+2

	MOVE.W D1,D2
	SUB.W #4,D2
	MOVE.W D2,MUS_MODPAT	;LAST STEP IN PATTERN

	MOVE.B 150(A5),D0	;SONG-LENGTH
	MOVE.B D0,D2
	SUB.B #2,D2
	AND.W #$00FF,D2
	MOVE.W D2,MUS_MODSONG	;LAST STEP IN SONG

	ADD.B #150+1,D0		;BEGIN OF PATTERNS
	AND.L #$FF,D0
	ADD.L A5,D0

	LEA MUS_PATTERNS(PC),A0

	AND.L #$FFFF,D1
	MOVEQ #63,D7
MUS_SETPATTERNS:
	MOVE.L D0,(A0)+
	ADD.L D1,D0		;NEXT PATTERN
	DBF D7,MUS_SETPATTERNS

	MOVE.B 08(A5),MUS_A1+3	;ENVELOPES
	MOVE.B 09(A5),MUS_D1+3
	MOVE.B 10(A5),MUS_S1+3
	MOVE.B 11(A5),MUS_A2+3
	MOVE.B 12(A5),MUS_D2+3
	MOVE.B 13(A5),MUS_S2+3
	MOVE.B 14(A5),MUS_A3+3
	MOVE.B 15(A5),MUS_D3+3
	MOVE.B 16(A5),MUS_S3+3
	MOVE.B 17(A5),MUS_A4+3
	MOVE.B 18(A5),MUS_D4+3
	MOVE.B 19(A5),MUS_S4+3

	MOVE.B 20(A5),MUS_RP+3	;REPLAY-SPEED

	MOVE.B 20(A5),D0
	SUB.B #1,D0
	MOVE.B D0,MUS_WAITPOS	;LAST FRAME ON TONE

	CLR.L MUS_VOL1
	CLR.L MUS_VOL3

	RTS

;;;;;

EVEN
MUS_PERIODTAB:
	DC.W 3424,3232,3048,2880,2712,2560,2416,2280,2152,2032,1920,1808
	DC.W 1712,1616,1524,1440,1356,1280,1208,1140,1076,1016,960,904
	DC.W 856,808,762,720,678,640,604,570,538,508,480,452
	DC.W 428,404,381,360,339,320,302,285,269,254,240,226
	DC.W 214

MUS_VOL1:
	DC.W 0
MUS_VOL2:
	DC.W 0
MUS_VOL3:
	DC.W 0
MUS_VOL4:
	DC.W 0
MUS_WAITPOS:
	DC.B 0
EVEN
MUS_CURRPAT:
	DC.L 0

MUS_MODSONG:
	DC.W 0

MUS_PATTERNS:
	BLK.L 64,0

MUS_MODPAT:
	DC.W 0

MUS_MODULE:
;************** MODULE-DATA ********************

MOD_NAME:
	DC.B 'SONGNAME'		;8 BYTES

MOD_ENVELOPES:;	ATTACK-LEVEL	DECAY-RATE	SUSTAIN-LEVEL
	DC.B	$40		,$4		,$20
	DC.B	$40		,$1		,$20
	DC.B	$30		,$1		,$20
	DC.B	$30		,$1		,$18

MOD_REPLAYSPEED:
	DC.B 6
MOD_PATTERNLENGTH:
	DC.B 64

MOD_WAVE1:
	DC.B 10,20,40,50,30,20,30,50
	DC.B 70,90,110,140,170,200,220,230
	DC.B 240,245,230,220,200,180,150,120
	DC.B 100,80,60,50,40,30,20,10

MOD_WAVE2:
	DC.B 0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150
	DC.B 160,170,180,190,195,200,205,210,215,220,225,230,235
	DC.B 240,245,250

MOD_WAVE3:
	DC.B 127,127,127,127,-127,-127,-127,-127
	DC.B -127,-121,-103,-75,-39,0,39,103

	DC.B 127,127,127,127,-127,-127,-127,-127
	DC.B -127,-121,-103,-75,-39,0,39,103

MOD_WAVE4:
	DC.B 00,20,40,60,80,100,120,140
	DC.B 160,140,120,100,80,60,40,20

	DC.B 00,20,40,60,80,100,120,140
	DC.B 160,140,120,100,80,60,40,20

MOD_SONGLEN:
	DC.B MOD_SONGEND-MOD_SONG+1
