*****************************************************************************
**                                                                         **
** Eagleripper: Protracker                                                 **
**                                                                         **
** Project: Eagleplayer 2.04                                               **
** Authors: Jan Blumenthal & Henryk Richter                                **
** Start  : 1993/01/09                                                     **
** $Header$                                                                **
**                                                                         **
*****************************************************************************
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program (See the included file COPYING);
** if not, write to the Free Software Foundation, Inc.,
** 675 Mass Ave, Cambridge, MA 02139, USA.
**
***************************************************************************** 


*-----------------------------------------------------------------------------*
*			     Eagleripper V1.0				      *
*				Protracker					      *
*-----------------------------------------------------------------------------*
Test	= 1
Check1 = 1
		incdir	include:
		include	misc/eagleplayerripper.i
		include	exec/exec_lib.i
			
	if	Test

	*--- Pass 1 ---*
haha:
	lea	data+$438,a0
	*lea	ProtrackerTags,a0
	move.l	#datasize,d0
	move.l	a0,a5
	move.l	a0,a4
	move.l	d0,d4
	move.l	d0,d6
.wait1:	move.l	a5,a1
	move.l	a4,a0
	move.l	d4,d0
	move.l	(a1),d1
	bsr	ProtrackerRipp1
	tst.l	d0
	beq.s	.Ja
	addq.l	#2,a5
	subq.l	#2,d6
	blt.s	.All
	btst	#6,$bfe001
	bne.s	.wait1

	*--- Pass 2 ---*
.All:	lea	data+$438,a0
	*lea	ProtrackerTags,a0
	move.l	#datasize,d0
	move.l	a0,a5
	move.l	a0,a4
	move.l	d0,d4
	move.l	d0,d6
.wait2:	move.l	a5,a1
	move.l	a4,a0
	move.l	d4,d0
	move.l	(a1),d1
	bsr	ProtrackerRipp2
	tst.l	d0
	beq.s	.Ja
	addq.l	#2,a5
	subq.l	#2,d6
	blt.s	.All
	btst	#6,$bfe001
	bne.s	.wait2
	moveq	#-1,d0
.ret:	illegal

.ja:	move.l	d1,d0
	bsr	GetModuleName
	bra.s	.Ret
	endc
			
		RIPPERHEADER	ProtrackerTags

	dc.b	"Eagleripper-Tracker (Protracker/Startrekker/...) V1.0",10
	dc.b	"done by Eagleeye/DFT 28.02.97",0
	even

ProtrackerTags:	dc.l	RPT_Formatname,.Formatname
		dc.l	RPT_Ripp1,ProtrackerRipp1
		dc.l	RPT_Ripp2,ProtrackerRipp2
		dc.l	RPT_RequestRipper,1
		dc.l	RPT_Version,1<<16!0
		dc.l	RPT_Creator,.Creator
		dc.l	RPT_Next,StartrekkerTags
		dc.l	RPT_GetModuleName,GetModuleName
		dc.l	RPT_Prefix,.Prefix
		dc.l	0

.Creator:	dc.b	"Lars 'Zap' Hamre, adapted by DEFECT",0
.Formatname:	dc.b	"Protracker",0
.Prefix:	dc.b	"Mod.",0
		even

*-----------------------------------------------------------------------------*
* Input: a0=Adr (start of memory)
*	 d0=Size (size of memory)
*	 a1=current adr
*	 d1=(a1.l)
* Output:d0=Error oder NULL
*	 d1=Size
*	 a0=Startadr (data)
*-----------------------------------------------------------------------------*
ProtrackerRipp1:cmp.l	#"M.K.",d1
		beq.s	.Ja
		rts

.Ja:		bsr	ProTrackerRipp2
.Return:	rts

	ifeq	check1
ProTrackerRipp2:lea	-$438(a1),a0
		move.l	d0,d4

		move.l	$438(a0),d0
		
		cmp.l	#"M!K!",d0		;M.K. da ?
		beq.s	.pro_checkwei
		cmp.l	#"M&K!",d0		;M.K. da ?
		beq.s	.pro_checkwei
		cmp.l	#"M.K.",d0		;M.K. da ?
		bne.w	Check_Error		;nö,Fehler
.pro_checkwei
		moveq	#-1,d0
		lea	$3B8(a0),a1		;Berechne Größe des Songs

		moveq	#$80-1,d3
		moveq	#0,d1
		moveq	#0,d2

.Pro_Check3:	move.b	(a1)+,d1
		cmp.b	d1,d2
		bgt.s	.Pro_Check2
		move.b	d1,d2
.Pro_Check2:	dbf	d3,.Pro_Check3

		addq.b	#1,d2
		asl.l	#8,d2
		move	d2,d3
		asl.l	#2,d3
		add.l	#$43C,d3

	cmp.l	d4,d3	;ist geladenes File größer als der Song
					;allein ??
	bhi.w	Check_Error		;nein,Fehler

		lea	$43c(a0),a1
		moveq	#0,d3
		moveq	#0,d4
;		move.w	#64*4,d2
		subq	#1,d2
.Testfirstpatt:	move.w	(a1)+,d1
		and.w	#$fff,d1
		beq.s	.wei
		lea	ProPC_Periods,a2	;Periodentabelle
		addq	#1,d3			;Num Notes
.lop2		move.w	(a2)+,d0
		beq.s	.wei
		cmp.w	d1,d0
		bne.s	.lop2
		addq	#1,d4		;Num Valid
.wei:
		addq.l	#2,a1
		dbf	d2,.testfirstpatt
		tst	d3
		beq.s	Pro_CheckRet

		moveq	#-1,d0
		mulu	#100,d4
		divu	d3,d4
		cmp	#40,d4			;40% aller Noten müssen korrekt
						;sein !!!
		blo.s	Pro_CheckRet
		bra.s	checkvolsnlengths
Pro_CheckRet:	rts


Checkvolsnlengths:
		lea	42(A0),a2		;Zeiger auf 1. Samplelänge
		moveq	#$1e,d3
.loop:		moveq	#0,d1
		moveq	#0,d2
		moveq	#0,d4

		move.w	(a2),d1			;Länge
		beq.s	.next
		addq.w	#8,d1			;zur Sicherheit
		bhi.s	.ok1
		subq.w	#8,d1
.ok1:		add.l	d1,d1

		move.w	4(a2),d2		;Repeatstart
		cmp.l	d2,d1
		blo.s	Check_Error

		move.w	6(a2),d4		;Repeatlänge
		cmp.w	#-1,d4
		beq.s	.TestVol
		cmp.l	d4,d1
		beq.s	.TestVol
		add.l	d4,d4
		cmp.l	d4,d1
		blo.s	Check_Error

	;	add.l	d2,d4
	;	cmp.l	d4,d1
	;	blo.s	Check_Error

.TestVol:	cmp.b	#64,3(A2)
		bhi.s	Check_Error

		move.b	2(A2),d1
		and.b	#$f0,d1
		cmp.b	#$80,d1
		beq	Check_Error
.next
		add.l	#30,a2
		dbf	d3,.loop

Pro_Check2:	moveq	#0,d0
		rts

Check_Error:	moveq	#-1,d0
		rts

	else


*-----------------------------------------------------------------------------*
ProTrackerRipp2:move.l	d3,-(a7)
		lea	-$438(a1),a0
		cmp.l	#$3b8+$7f+1024,d0		*Enforcer
		bls.w	.Check_Error


	*----- Patternliste testen -----*
		lea	$3B8(a0),a1		;Berechne Größe des Songs
		moveq	#$7F,d3
		moveq	#0,d1
.Pro_Check2:	move.l	d1,d2
		subq.w	#1,d3
.Pro_Check3:	move.b	(a1)+,d1
	cmp.b	#$3f,d1
	bhi.w	.Check_Error
		cmp.b	d2,d1
		bgt.s	.Pro_Check2
		dbra	d3,.Pro_Check3
		addq.b	#1,d2



*		move.l	$438(a0),d1
*		cmp.l	#"M.K.",d1		* Wenn keine Kennung, dann
*		beq.s	.KennungDa		* aus Sicherheitsgründen
*		cmp.l	#"EXO4",d1		* den Check da unten machen,
*		beq	.KennungDa		* da sonst zu viel als Module
*		cmp.l	#"FLT4",d1		* erkannt wird.
*		beq	.KennungDa
*		cmp.l	#"M.K.",d1	
*		beq	.KennungDa
*		cmp.l	#"M&K!",d1	
*		beq	.KennungDa
*		cmp.l	#"NORI",d1	
*		beq.s	.KennungDa

		move.b	$3b6(a0),d1
		cmp.b	d2,d1			* jetzt total auf
		blt.w	.Check_Error		* Sicherheit
		cmp.b	#$7f,$3b7(a0)
		bhi.w	.Check_Error
		tst.b	$3b7(a0)
		beq.w	.Check_Error

.KennungDa:	asl.l	#8,d2
		move	d2,d3
		asl.l	#2,d3
		add.l	#$43C,d3

		cmp.l	d0,d3
		bhi.w	.Check_Error		;nein,Fehler


	*-------- Checkvolsnlengths --------------*
		movem.l	d2/d4/d5,-(a7)
		moveq	#0,d5
		lea	42(A0),a1		;Zeiger auf 1. Samplelänge
		moveq	#$1e,d3
.loop:		moveq	#0,d1
		moveq	#0,d2
		moveq	#0,d4

		move.w	(a1),d1			;Länge
		beq.s	.next
		addq.l	#1,d5			;Anz der verf. Samples
		addq.w	#8,d1			;zur Sicherheit
		bhi.s	.ok1
		subq.w	#8,d1
.ok1:		add.l	d1,d1

		move.w	4(a1),d2		;Repeatstart
		cmp.l	d2,d1
		blo.w	.Error

		move.w	6(a1),d4		;Repeatlänge
		cmp.w	#-1,d4
		beq.s	.TestVol
		cmp.l	d4,d1
		beq.s	.TestVol
		add.l	d4,d4
		cmp.l	d4,d1
		blo.w	.Error

	;	add.l	d2,d4
	;	cmp.l	d4,d1
	;	blo.s	.Error

.TestVol:	cmp.b	#64,3(A1)
		bhi.w	.Error

		move.b	2(A1),d1
		and.b	#$f0,d1
		cmp.b	#$80,d1
		beq	.Error
.next
		lea	30(a1),a1
		dbf	d3,.loop

		tst.l	d5
		beq.w	.Error

	*--- Erstes Pattern testen ---*
		move.l	(a7),d2
		lea	$43c(a0),a1
		*moveq	#0,d5
		moveq	#0,d3
		moveq	#0,d1
		;move.w	#64*4,d2
		subq	#1,d2
.Testfirstpatt:	move.w	(a1)+,d4
		*bne.s	.NoZero
		*addq.l	#1,d5
.NoZero		and.w	#$fff,d4
		beq.s	.wei
		lea	ProPC_Periods(pc),a2	;Periodentabelle
		addq	#1,d3			;Num Notes
.lop2		move.w	(a2)+,d0
		beq.s	.wei
		cmp.w	d4,d0
		bne.s	.lop2
		addq	#1,d1		;Num Valid
.Wei:		lea	2(a1),a1
		dbf	d2,.testfirstpatt

		*cmp.l	d5,d2			;lauter Nullen
		*beq.s	.Error

		*-- Würde heissen, dass keine Noten da sind --*
		tst.l	d3
		*beq.s	.GleichTest
		beq.s	.Error

		moveq	#-1,d0
		mulu	#100,d1
		divu	d3,d1
		cmp	#40,d1			;40% aller Noten müssen korrekt
						;sein !!!
		bls.w	.Error


	*-- Testen, ob Module am Anfang nur Nullen hat --*
.GleichTest:	move.l	a0,a1
		moveq	#0,d4
		moveq	#0,d5
		move.w	#1084-1,d2
.NextB:		move.b	(a1)+,d0
		beq.w	.PT_Zero
		cmp.b	#255,d0
		beq.w	.PT_F
.Pt_Weiter:	dbf	d2,.NextB

		cmp.l	#1084-5,d4
		bhi.w	.Error
		cmp.l	#1084-10,d5
		bhi.s	.Error

		add.l	d5,d4
		mulu	#100,d4
		divu	#1084,d4
		cmp.w	#90,d4
		bhi.s	.Error

		move.l	#"M.K.",$438(a0)

.ModOk:		bsr	PT_BerechSize
		movem.l	(a7)+,d2/d4/d5
		move.l	(a7)+,d3
		moveq	#0,d0
		rts


.PT_Zero:	addq.l	#1,d4
		bra.w	.Pt_Weiter
.PT_F:		addq.l	#1,d5
		bra.w	.Pt_Weiter


.Error:		movem.l	(a7)+,d2/d4/d5
.Check_Error:	move.l	(a7)+,d3
		moveq	#1,d0
		rts
	endc

*-----------------------------------------------------------------------------*
ProPC_Periods:	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	$6b,$5f,$55,$50,$47,0



*#############################################################################*
*#############################################################################*
*#############################################################################*
StartrekkerTags:dc.l	RPT_Formatname,.Formatname
		dc.l	RPT_Ripp1,StartrekkerRipp1
		* Kein Pass 2, da PT das miterledigt !!!
		dc.l	RPT_RequestRipper,1
		dc.l	RPT_Version,1<<16!0
		dc.l	RPT_Creator,.Creator
		*dc.l	RPT_Next,WantonpackerTags
		dc.l	RPT_GetModuleName,GetModuleName
		dc.l	0

.Creator:	dc.b	"Exolon/Fairlight, adapted by DEFECT",0
.Formatname:	dc.b	"Startrekker",0
		even

*-----------------------------------------------------------------------------*
* Input: a0=Adr (start of memory)
*	 d0=Size (size of memory)
*	 a1=current adr
*	 d1=(a1.l)
* Output:d0=Error oder NULL
*	 d1=Size
*	 a0=Startadr (data)
*-----------------------------------------------------------------------------*
StartrekkerRipp1:
		cmp.l	#"FLT4",d1
		beq.s	.Ja
		cmp.l	#"EXO4",d1
		beq.s	.ja
		rts

.Ja:		bsr	ProTrackerRipp2
.Return:	rts




**#############################################################################*
**#############################################################################*
**#############################################################################*
*WantonPackerTags:dc.l	RPT_Formatname,.Formatname
*		dc.l	RPT_Ripp1,WantonpackerRipp1
*		dc.l	RPT_RequestRipper,1
*		dc.l	RPT_Version,1<<16!0
*		dc.l	RPT_Creator,.Creator
*		dc.l	RPT_GetModuleName,GetModuleName
*		dc.l	0
*
*.Formatname:	dc.b	"Wantonpacker",0
*.Creator:	dc.b	"Wanton of Bloodsuckers,",10
*		dc.b	"adapted by DEFECT",0
*		even
*
**-----------------------------------------------------------------------------*
** Input: a0=Adr (start of memory)
**	 d0=Size (size of memory)
**	 a1=current adr
**	 d1=(a1.l)
** Output:d0=Error oder NULL
**	 d1=Size
**	 a0=Startadr (data)
**-----------------------------------------------------------------------------*
*WantonPackerRipp1:
*		lsr.l	#8,d1
*		cmpi.w	#"WN",d1		;"WN" da ?
*		beq.s	.Ja
*		rts
*
*.Ja:		lea	-$438(a1),a0
*		lea	$3b8(A0),a1
*		moveq	#$7f,d1
*		moveq	#0,d2
*.Wn_Calc:	cmp.b	(A1)+,d2
*		bge.s	.WN_NoThis
*		move.b	-1(A1),d2
*.Wn_Nothis:	dbf	d1,.wn_calc
*		addq.b	#1,d2
*		cmp.w	$43a(A0),d2
*		bne.s	.nown
*		bsr	PT_BerechSize
*		moveq	#0,d0
*.NoWn:		rts
*
*
*
*


*-----------------------------------------------------------------------------*
*-----------------------------------------------------------------------------*
*-----------------------------------------------------------------------------*
*-----------------------------------------------------------------------------*
PT_BerechSize:	move.l	a0,a1
		lea	952(a1),a1
		moveq	#127,d0
		moveq	#0,d2
.mtloop		move.l	d2,d1
		subq.w	#1,d0
.mtloop2	move.b	(a1)+,d2
		cmp.b	d1,d2
		bgt.s	.mtloop
		dbra	d0,.mtloop2
		addq.b	#1,d1
			
		move.l	a0,a1
		asl.l	#8,d1
		asl.l	#2,d1
		add.l	#1084,d1
		moveq	#30,d0
.mtloop3:	moveq	#0,d2
		move.w	42(a1),d2
		asl.l	#1,d2
		add.l	d2,d1
		add.l	#30,a1
		dbra	d0,.mtloop3
		rts

*-----------------------------------------------------------------------------*
* Input: a0=Start of File
*	 d0=Size
* Output:d0=Ptr to Name or NULL
*-----------------------------------------------------------------------------*
GetModuleName:	moveq	#0,d0
		tst.b	(a0)
		beq.s	.Return
		move.l	a0,d0
.Return:	rts


	if	test
data:		incdir	"Modules:"
	*	ds.b	200
		*incbin	Mod.Agnostica                 
		*incbin	Mod.Agnostica.EXA2            
		*incbin	Mod.Agnostica.EXAM            
		*incbin	Mod.Agnostica.xpk             
		*incbin	Mod.Musikertest
	*	incbin	Mod.Unbekannt
		*incbin	Mod.Unbekannt2
		*incbin	Mod.Unbekannt3
		*incbin	Mod.Unbekannt4

		incdir	"Modules:Trackerclones/Magnetic_Fields/"
		incbin	mod.test
datasize	= *-data

	endc
