;---------------------------------------------
;Deklaracje stalych
;---------------------------------------------
	Exec		=	4
	OpenLib		=	-408
	CloseLib	=	-414
	Write		=	-48
	Open		=	-30
	Zap		=	1006
	Odc		=	1005
	Read		=	-42
	Close		=	-36
	AllocMem	=	-198
	FreeMem		=	-210
	Lock		=	-84
	Examine		=	-102
	UnLock		=	-90
;---------------------------------------------
;Tu zaczyna sie wlasciwy program
;---------------------------------------------
START:
	lea	dosname,a1		;Otworzenie Dos.library
	move.l	Exec,a6
	jsr	OpenLib(a6)	
	move.l	d0,dosbase
	tst.l	d0
	beq	error			;Brak dos.library (?)

	move.l	dosbase,a6		;Otworzenie nowego okna
	move.l 	#windowname,d1
	move.l	#Zap,d2
	jsr	Open(a6)			
	move.l	d0,windowbase
	tst.l	d0
	beq	error			;Nie moge otworzyc okna !

POW:
	move.l	dosbase,a6		;Druk tekstu 1
	move.l	windowbase,d1
	move.l	#text1,d2
	move.l	#text2-text1,d3
	jsr	Write(a6)
	
	move.l	windowbase,d1
	move.l	#modname,d2
	move.l	#200,d3
	jsr	Read(a6)

	sub.l	#1,d0
	move.l	d0,equtitle

	add.l	#modname,d0		;Wstawienie zamiast ostatniego
	move.l	d0,a0			; 'RETURN' wartosci zero
	move.b	#0,(a0)

	move.l	dosbase,a6		;Sprawdzenie czy istnieje
	move.l	#modname,d1		; zbior pod taka nazwa
	move.l	#-2,d2		
	jsr	Lock(a6)	
	tst.l	d0
	beq.s	POW			;Nie ma takiego zbioru !
	
	move.l	d0,lockbase
	move.l	d0,d1
	move.l	#bufor,d2
	jsr	Examine(a6)
	move.l	lockbase,d1
	jsr	UnLock(a6)
	move.l	bufor+4,d0
	bpl	POW			;To jest katalog !

	move.l	Exec,a6			;Zarezerwowanie pamieci
	move.l	bufor+124,d0
	move.l	#$10001,d1
	jsr	AllocMem(a6)
	move.l	d0,mem
	tst.l	d0
	beq	POW			;Blad przy rezerwowaniu pamieci

	move.l	dosbase,a6		;Zbior istnieje. Przystepujemy
	move.l	#modname,d1		; do ladowania !
	move.l	#Odc,d2	
	jsr	Open(a6)		;Otwarcie zbioru
	move.l	d0,titlebase
	tst.l	d0
	beq	POW			;Blad otwarcia zbioru !

	move.l	titlebase,d1		;Odczyt zbioru
	move.l	mem,d2
	move.l	bufor+124,d3
	jsr	Read(a6)
	tst.l	d0
	beq	POW			;Blad odczytu !

	move.l	titlebase,d1		;Zamkniecie zbioru
	move.l	dosbase,a6
	jsr	Close(a6)

	move.l	dosbase,a6		;Druk tekstu 1
	move.l	windowbase,d1
	move.l	#text2,d2
	move.l	#text3-text2,d3
	jsr	Write(a6)
	move.l	#1,wybor
;---------------------------------------------
;Tu zaczyna sie procedura odgrywania modulu
;---------------------------------------------

	move.l	mem,a0			;Procedura odtwarzajaca SoundTrackera 2.6
					; potrzebuje w rejestrze A0 adres pamieci
					; w ktorej znajduje sie modul
	jsr	mt_init			;inicjalizacja muzyki
WaitRaster:
	cmp.b	#$80,$dff006
	bne	WaitRaster

	jsr	mt_music		;Odegranie jednego dzwieku

	btst	#6,$bfe001
	beq	Next			;Lewy przycisk myszy
	btst	#2,$dff016
	beq	Quit			;Prawy przycisk myszy
	jmp	WaitRaster

Quit:	move.l	#0,wybor
Next:	jsr	mt_end

	move.l	Exec,a6			;Zwolnienie pamieci
	move.l	bufor+124,d0
	move.l	mem,a1
	jsr	FreeMem(a6)

	move.l	wybor,d0
	tst.l	d0
	bne	POW

	move.l	dosbase,a6		;Zamkniecie okna
	move.l	windowbase,d1
	jsr	Close(a6)

	move.l	Exec,a6			;Zamkniecie biblioteki dos
	move.l	dosbase,a1
	jsr	CloseLib(a6)	
	rts
;---------------------------------------------
error:					;Obsluga bledu
	bchg	#1,$bfe001		;Zmiana stanu diody POWER
	rts
;---------------------------------------------
dosbase:	dc.l	0
windowbase:	dc.l	0
lockbase:	dc.l	0
equtitle:	dc.l	0
mem:		dc.l	0
titlebase:	dc.l	0
wybor:		dc.l	0
bufor:		blk.l	260,0

dosname:	dc.b	"dos.library",0
windowname:	dc.b	"con:0/12/430/220/ClasicPlayer v.0.1 by M.Ferdyn",0
text1:
		dc.b	10,13,"Clasic Player ver. 0.1",10,13
		dc.b	"          by",10,13
		dc.b 	"       M.Ferdyn",10,13,10,13
		dc.b	"ModName :>"
text2:
		dc.b	"LEFT - Nastepny modul,        RIGHT - Wyjscie"
text3:
modname:	blk.b	200,0
;---------------------------------------------
;Tu zaczyna sie podprogram odtwarzajacy
; przykladowo od StarTrekkera 1.2
;---------------------------------------------
; STARTREKKER 1.2 NORMAL REPLAYROUTINE

; call mt_init, then mt_music each frame, call mt_end to stop


TESTPLAY:
	bsr	mt_init

tp_loop:cmp.b	#$f8,$dff006
	bne.s	tp_loop

	bsr	mt_music
	
	btst	#6,$bfe001
	bne.s	tp_loop

	bsr	mt_end
	rts

mt_init:move.l  mem,a0
	lea	$3b8(a0),a1
	moveq	#$7f,d0
	moveq	#0,d2
	moveq	#0,d1
mt_lop2:move.b	(a1)+,d1
	cmp.b	d2,d1
	ble.s	mt_lop
	move.l	d1,d2
mt_lop:	dbf	d0,mt_lop2
	addq.b	#1,d2

	asl.l	#8,d2
	asl.l	#2,d2
	lea	4(a1,d2.l),a2
	lea	mt_samplestarts(pc),a1
	add.w	#42,a0
	moveq	#$1e,d0
mt_lop3:clr.l	(a2)
	move.l	a2,(a1)+
	moveq	#0,d1
	move.w	(a0),d1
	clr.b	2(a0)
	asl.l	#1,d1
	add.l	d1,a2
	add.l	#$1e,a0
	dbf	d0,mt_lop3

	or.b	#2,$bfe001
	move.b	#6,mt_speed
	moveq	#0,d0
	lea	$dff000,a0
	move.w	d0,$a8(a0)
	move.w	d0,$b8(a0)
	move.w	d0,$c8(a0)
	move.w	d0,$d8(a0)
	clr.b	mt_songpos
	clr.b	mt_counter
	clr.w	mt_pattpos
	rts

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

mt_music:
	move.l  mem,a0
	addq.b	#1,mt_counter
	move.b	mt_counter(pc),d0
	cmp.b	mt_speed(pc),d0
	blt	mt_nonew
	clr.b	mt_counter

	move.l  mem,a0
	lea	$c(a0),a3
	lea	$3b8(a0),a2
	lea	$43c(a0),a0

	moveq	#0,d0
	moveq	#0,d1
	move.b	mt_songpos(pc),d0
	move.b	(a2,d0.w),d1
	lsl.w	#8,d1
	lsl.w	#2,d1
	add.w	mt_pattpos(pc),d1
	clr.w	mt_dmacon

	lea	$dff0a0,a5
	lea	mt_voice1(pc),a4
	bsr	mt_playvoice
	addq.l	#4,d1
	lea	$dff0b0,a5
	lea	mt_voice2(pc),a4
	bsr	mt_playvoice
	addq.l	#4,d1
	lea	$dff0c0,a5
	lea	mt_voice3(pc),a4
	bsr	mt_playvoice
	addq.l	#4,d1
	lea	$dff0d0,a5
	lea	mt_voice4(pc),a4
	bsr	mt_playvoice

	bsr	mt_wait
	move.w	mt_dmacon(pc),d0
	or.w	#$8000,d0
	move.w	d0,$dff096
	bsr	mt_wait
mt_nodma:
	lea	mt_voice1(pc),a4
	lea	$dff000,a3
	move.l	$a(a4),$a0(a3)
	move.w	$e(a4),$a4(a3)
	move.w	$12(a4),$a8(a3)
	move.l	$a+$1c(a4),$b0(a3)
	move.w	$e+$1c(a4),$b4(a3)
	move.w	$12+$1c(a4),$b8(a3)
	move.l	$a+$38(a4),$c0(a3)
	move.w	$e+$38(a4),$c4(a3)
	move.w	$12+$38(a4),$c8(a3)
	move.l	$a+$54(a4),$d0(a3)
	move.w	$e+$54(a4),$d4(a3)
	move.w	$12+$54(a4),$d8(a3)

	add.w	#$10,mt_pattpos
	cmp.w	#$400,mt_pattpos
	bne.s	mt_exit
mt_next:clr.w	mt_pattpos
	clr.b	mt_break
	move.l  mem,a0
	addq.b	#1,mt_songpos
	and.b	#$7f,mt_songpos
	move.b	$3b6(a0),d0
	cmp.b	mt_songpos(pc),d0
	bne.s	mt_exit
	move.b	$3b7(a0),mt_songpos
mt_exit:tst.b	mt_break
	bne.s	mt_next
	rts

mt_wait:moveq	#4,d3		
mt_wai2:move.b	$dff006,d2	
mt_wai3:cmp.b	$dff006,d2	
	beq.s	mt_wai3
	dbf	d3,mt_wai2	
	moveq	#8,d2
mt_wai4:dbf	d2,mt_wai4

mt_nonew:
	lea	mt_voice1(pc),a4
	lea	$dff0a0,a5
	bsr	mt_com
	lea	mt_voice2(pc),a4
	lea	$dff0b0,a5
	bsr	mt_com
	lea	mt_voice3(pc),a4
	lea	$dff0c0,a5
	bsr	mt_com
	lea	mt_voice4(pc),a4
	lea	$dff0d0,a5
	bsr	mt_com
	bra.s	mt_exit

mt_mulu:
	dc.w	0,$1e,$3c,$5a,$78,$96,$b4,$d2,$f0,$10e,$12c,$14a
	dc.w	$168,$186,$1a4,$1c2,$1e0,$1fe,$21c,$23a,$258,$276
	dc.w	$294,$2b2,$2d0,$2ee,$30c,$32a,$348,$366,$384,$3a2

mt_playvoice:
	move.l	(a0,d1.l),(a4)
	moveq	#0,d2
	move.b	2(a4),d2
	lsr.b	#4,d2
	move.b	(a4),d0
	and.b	#$f0,d0
	or.b	d0,d2
	beq.s	mt_oldinstr

	lea	mt_samplestarts-4(pc),a1
	asl.w	#2,d2
	move.l	(a1,d2.l),4(a4)
	lsr.w	#1,d2
	move.w	mt_mulu(pc,d2.w),d2
	move.w	(a3,d2.w),8(a4)
	move.w	2(a3,d2.w),$12(a4)
	moveq	#0,d3
	move.w	4(a3,d2.w),d3
	tst.w	d3
	beq.s	mt_noloop
	move.l	4(a4),d0
	asl.w	#1,d3
	add.l	d3,d0
	move.l	d0,$a(a4)
	move.w	4(a3,d2.w),d0
	add.w	6(a3,d2.w),d0
	move.w	d0,8(a4)
	bra.s	mt_hejaSverige
mt_noloop:
	move.l	4(a4),d0
	add.l	d3,d0
	move.l	d0,$a(a4)
mt_hejaSverige:
	move.w	6(a3,d2.w),$e(a4)

mt_oldinstr:
	move.w	(a4),d0
	and.w	#$fff,d0
	beq	mt_com2
	tst.w	8(a4)
	beq.s	mt_stopsound
	tst.b	$12(a4)
	bne.s	mt_stopsound
	move.b	2(a4),d0
	and.b	#$f,d0
	cmp.b	#5,d0
	beq.s	mt_setport
	cmp.b	#3,d0
	beq.s	mt_setport

	move.w	(a4),$10(a4)
	and.w	#$fff,$10(a4)
	move.w	$1a(a4),$dff096
	clr.b	$19(a4)

	move.l	4(a4),(a5)
	move.w	8(a4),4(a5)
	move.w	$10(a4),6(a5)

	move.w	$1a(a4),d0
	or.w	d0,mt_dmacon
	bra	mt_com2

mt_stopsound:
	move.w	$1a(a4),$dff096
	bra	mt_com2

mt_setport:
	move.w	(a4),d2
	and.w	#$fff,d2
	move.w	d2,$16(a4)
	move.w	$10(a4),d0
	clr.b	$14(a4)
	cmp.w	d0,d2
	beq.s	mt_clrport
	bge	mt_com2
	move.b	#1,$14(a4)
	bra	mt_com2
mt_clrport:
	clr.w	$16(a4)
	rts

mt_port:move.b	3(a4),d0
	beq.s	mt_port2
	move.b	d0,$15(a4)
	clr.b	3(a4)
mt_port2:
	tst.w	$16(a4)
	beq.s	mt_rts
	moveq	#0,d0
	move.b	$15(a4),d0
	tst.b	$14(a4)
	bne.s	mt_sub
	add.w	d0,$10(a4)
	move.w	$16(a4),d0
	cmp.w	$10(a4),d0
	bgt.s	mt_portok
	move.w	$16(a4),$10(a4)
	clr.w	$16(a4)
mt_portok:
	move.w	$10(a4),6(a5)
mt_rts:	rts

mt_sub:	sub.w	d0,$10(a4)
	move.w	$16(a4),d0
	cmp.w	$10(a4),d0
	blt.s	mt_portok
	move.w	$16(a4),$10(a4)
	clr.w	$16(a4)
	move.w	$10(a4),6(a5)
	rts

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

mt_vib:	move.b	$3(a4),d0
	beq.s	mt_vib2
	move.b	d0,$18(a4)

mt_vib2:move.b	$19(a4),d0
	lsr.w	#2,d0
	and.w	#$1f,d0
	moveq	#0,d2
	move.b	mt_sin(pc,d0.w),d2
	move.b	$18(a4),d0
	and.w	#$f,d0
	mulu	d0,d2
	lsr.w	#7,d2
	move.w	$10(a4),d0
	tst.b	$19(a4)
	bmi.s	mt_vibsub
	add.w	d2,d0
	bra.s	mt_vib3
mt_vibsub:
	sub.w	d2,d0
mt_vib3:move.w	d0,6(a5)
	move.b	$18(a4),d0
	lsr.w	#2,d0
	and.w	#$3c,d0
	add.b	d0,$19(a4)
	rts

mt_arplist:
	dc.b	0,1,2,0,1,2,0,1,2,0,1,2,0
	dc.b	1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1

mt_arp:	moveq	#0,d0
	move.b	mt_counter(pc),d0
	move.b	mt_arplist(pc,d0.w),d0
	beq.s	mt_arp0
	cmp.b	#2,d0
	beq.s	mt_arp2
mt_arp1:moveq	#0,d0
	move.b	3(a4),d0
	lsr.b	#4,d0
	bra.s	mt_arpdo
mt_arp2:moveq	#0,d0
	move.b	3(a4),d0
	and.b	#$f,d0
mt_arpdo:
	asl.w	#1,d0
	move.w	$10(a4),d1
	and.w	#$fff,d1
	lea	mt_periods(pc),a0
	moveq	#$24,d2
mt_arp3:cmp.w	(a0)+,d1
	bge.s	mt_arpfound
	dbf	d2,mt_arp3
mt_arp0:move.w	$10(a4),6(a5)
	rts
mt_arpfound:
	move.w	-2(a0,d0.w),6(a5)
	rts

mt_normper:
	move.w	$10(a4),6(a5)
	rts

mt_com:	move.w	2(a4),d0
	and.w	#$fff,d0
	beq.s	mt_normper
	move.b	2(a4),d0
	and.b	#$f,d0
	tst.b	d0
	beq.s	mt_arp
	cmp.b	#1,d0
	beq.s	mt_portup
	cmp.b	#2,d0
	beq.s	mt_portdown
	cmp.b	#3,d0
	beq	mt_port
	cmp.b	#4,d0
	beq	mt_vib
	cmp.b	#5,d0
	beq.s	mt_volport
	cmp.b	#6,d0
	beq.s	mt_volvib
	move.w	$10(a4),6(a5)
	cmp.b	#$a,d0
	beq.s	mt_volslide
	rts

mt_portup:
	moveq	#0,d0
	move.b	3(a4),d0
	sub.w	d0,$10(a4)
	move.w	$10(a4),d0
	cmp.w	#$71,d0
	bpl.s	mt_portup2
	move.w	#$71,$10(a4)
mt_portup2:
	move.w	$10(a4),6(a5)
	rts

mt_portdown:
	moveq	#0,d0
	move.b	3(a4),d0
	add.w	d0,$10(a4)
	move.w	$10(a4),d0
	cmp.w	#$358,d0
	bmi.s	mt_portdown2
	move.w	#$358,$10(a4)
mt_portdown2:
	move.w	$10(a4),6(a5)
	rts

mt_volvib:
	 bsr	mt_vib2
	 bra.s	mt_volslide
mt_volport:
	 bsr	mt_port2

mt_volslide:
	moveq	#0,d0
	move.b	3(a4),d0
	lsr.b	#4,d0
	beq.s	mt_vol3
	add.b	d0,$13(a4)
	cmp.b	#$40,$13(a4)
	bmi.s	mt_vol2
	move.b	#$40,$13(a4)
mt_vol2:moveq	#0,d0
	move.b	$13(a4),d0
	move.w	d0,8(a5)
	rts

mt_vol3:move.b	3(a4),d0
	and.b	#$f,d0
	sub.b	d0,$13(a4)
	bpl.s	mt_vol4
	clr.b	$13(a4)
mt_vol4:moveq	#0,d0
	move.b	$13(a4),d0
	move.w	d0,8(a5)
	rts

mt_com2:move.b	$2(a4),d0
	and.b	#$f,d0
	cmp.b	#$e,d0
	beq.s	mt_filter
	cmp.b	#$d,d0
	beq.s	mt_pattbreak
	cmp.b	#$b,d0
	beq.s	mt_songjmp
	cmp.b	#$c,d0
	beq.s	mt_setvol
	cmp.b	#$f,d0
	beq.s	mt_setspeed
	rts

mt_filter:
	move.b	3(a4),d0
	and.b	#1,d0
	asl.b	#1,d0
	and.b	#$fd,$bfe001
	or.b	d0,$bfe001
	rts

mt_pattbreak:
	move.b	#1,mt_break
	rts

mt_songjmp:
	move.b	#1,mt_break
	move.b	3(a4),d0
	subq.b	#1,d0
	move.b	d0,mt_songpos
	rts

mt_setvol:
	cmp.b	#$40,3(a4)
	bls.s	mt_sv2
	move.b	#$40,3(a4)
mt_sv2:	moveq	#0,d0
	move.b	3(a4),d0
	move.b	d0,$13(a4)
	move.w	d0,8(a5)
	rts

mt_setspeed:
	moveq	#0,d0
	move.b	3(a4),d0
	cmp.b	#$1f,d0
	bls.s	mt_sp2
	moveq	#$1f,d0
mt_sp2:	tst.w	d0
	bne.s	mt_sp3
	moveq	#1,d0
mt_sp3:	move.b	d0,mt_speed
	rts

mt_periods:
	dc.w	$358,$328,$2fa,$2d0,$2a6,$280,$25c,$23a,$21a,$1fc,$1e0
	dc.w	$1c5,$1ac,$194,$17d,$168,$153,$140,$12e,$11d,$10d,$fe
	dc.w	$f0,$e2,$d6,$ca,$be,$b4,$aa,$a0,$97,$8f,$87
	dc.w	$7f,$78,$71,0

mt_speed:	dc.b	6
mt_counter:	dc.b	0
mt_pattpos:	dc.w	0
mt_songpos:	dc.b	0
mt_break:	dc.b	0
mt_dmacon:	dc.w	0
mt_samplestarts:blk.l	$1f,0
mt_voice1:	blk.w	13,0
		dc.w	1
mt_voice2:	blk.w	13,0
		dc.w	2
mt_voice3:	blk.w	13,0
		dc.w	4
mt_voice4:	blk.w	13,0
		dc.w	8
