*****************************************************************************
**                                                                         **
** Eagleripper: Ben Daglish                                                **
**                                                                         **
** Project: Eagleplayer 2.04                                               **
** Authors: Jan Blumenthal, Henryk Richter, and Mr.Larmer                  **
** 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				      *
*				Ben Daglish				      *
*-----------------------------------------------------------------------------*
Test	= 0
		incdir	include:
		include	misc/eagleplayerripper.i
		include	exec/exec_lib.i
			
		if	Test
		lea	data,a0
		*lea	BD_Tags,a0
		move.l	#datasize,d0
		move.l	a0,a5
		move.l	a0,a4
		move.l	d0,d4
		move.l	d0,d6
wait:		move.l	a5,a1
		move.l	a4,a0
		move.l	d4,d0
		move.l	(a1),d1
		bsr	BD_Ripp1
		tst.l	d0
		beq.s	.Ja
		addq.l	#2,a5
		subq.l	#2,d6
		blt.s	.Err
		btst	#6,$bfe001
		bne.s	wait
.err:		moveq	#-1,d0
.ret:		illegal

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

		RIPPERHEADER	BD_Tags

		dc.b	"Benn Daglish EagleRipper V1.0",10
		dc.b	"done by Eagleeye/DFT & Mr.Larmer/Wanted Team 29.08.97"
		dc.b	0
		even

BD_Tags:	dc.l	RPT_Formatname,.Formatname
		dc.l	RPT_Ripp1,BD_Ripp1
		dc.l	RPT_RequestRipper,1
		dc.l	RPT_Version,1<<16!0
		dc.l	RPT_Creator,.Creator
		dc.l	RPT_Prefix,.Prefix
		dc.l	RPT_Playername,.Playername
		dc.l	0

.Creator:	dc.b	"Benn Daglish, adapted by DEFECT "
		dc.b	"and Mr.Larmer/Wanted Team",0
.Playername:
.Formatname:	dc.b	"Benn Daglish",0
.Prefix:	dc.b	"BD.",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)
*-----------------------------------------------------------------------------*
BD_Ripp1:	lsr.l	#8,d1
		lsr.l	#8,d1
		cmp.w	#$6000,d1
		beq.s	.Ja
		rts

		*--- Jetzt testen, ob es ein korrektes Module sein kann ---*
.Ja:		movem.l	D2-D7/A2-A6,-(A7)
		move.l	A1,A3

		cmpi.w	#$6000,(A1)
		bne.w	.Error
		cmpi.w	#$6000,4(A1)
		bne.w	.Error
		moveq	#0,D2
		move.w	2(A1),D2
		blt.w	.Error
		cmp.l	D0,D2
		bhi.w	.Error
		lea	2(A1,D2.W),A0
		cmpi.l	#$3F006100,(A0)
		bne.w	.Error
		move.w	4(A0),D2
		move.l	D0,D1
		add.w	D2,D1
		cmp.l	D0,D1
		bhi.w	.Error
;		lea	4(A0,D2.W),A0
;		cmpi.l	#$4DF900DF,(A0)
;		bne.s	.Error

		moveq	#$7F,D0
.l6:		cmp.l	#$D040D040,(A0)			; add.w D0,D0 * 2
		beq.b	.ok4
		lea	2(A0),A0
		dbf	D0,.l6
		bra.w	.error

.ok4:		lea	4(A0),A0
		cmp.w	#$D040,(A0)			; add.w D0,D0
		bne.b	.l6
		cmp.w	#$41FA,2(A0)			; lea ..(pc),A0
		bne.b	.l6
		lea	4(A0),A0
;		move.l	A0,A1
;		add.w	(A0),A1				; address subsong table
;
;		moveq	#0,D1
;.l7:	;	moveq	#3,D2
;.l8:	;	move.w	(A1)+,D0
;		and.w	#$FC00,D0
;		tst.w	D0
;		bne.b	.not_subsong
;		dbf	D2,.l8
;		addq.l	#1,D1
;		bra.b	.l7
;.not_subsong
;		subq.l	#1,D1				; last subsong
;		bpl.b	.ok5
;		moveq	#0,D1
;.ok5:	;	lea	InfoBuffer(pc),A1
;		addq.l	#1,D1
;		move.l	D1,SubSongs(A1)
;
		moveq	#$7F,D0
.l9:		cmp.w	#$41FA,(A0)+			; lea ..(pc),A0
		beq.b	.ok6
		dbf	D0,.l9
		bra.w	.error

.ok6:		move.l	A0,A1
		add.w	(A0),A1				; address 1 sample info
		move.l	A1,SampleInfo1

;		move.l	ModulePtr(pc),A0
		move.l	A3,A0
		lea	12(A0),A0
		add.w	(A0),A0
		moveq	#$7F,D0
.l10:		cmp.l	#$D040D040,(A0)			; add.w D0,D0 * 2
		beq.b	.ok7
		lea	2(A0),A0
		dbf	D0,.l10
		bra.b	.ok7a

.ok7:		lea	4(A0),A0
		cmp.w	#$41FA,(A0)			; lea ..(pc),A0
		bne.b	.l10
		lea	2(A0),A0
		add.w	(A0),A0				; address 2 sample info
		tst.w	(A0)
		beq.b	.ok8
.ok7a:		sub.l	A0,A0				; or 0 if samples
.ok8:		move.l	A0,SampleInfo2

		move.l	SampleInfo1(pc),A0
.com1:		move.l	(A0)+,D0
		beq.b	.ok9
		swap	D0
		tst.w	D0
		beq.b	.com1
		subq.l	#4,A0
.ok9:		subq.l	#8,A0
		move.l	A0,D0
		sub.l	SampleInfo1(pc),D0
		lsr.l	#2,D0		; how many samples in first sample info
;		lea	InfoBuffer(pc),A0
;		move.l	D0,Samples(A0)
		move.l	D0,SmpIn1SmpInfo

		move.l	SampleInfo2(pc),D0
		move.l	D0,A0
		beq.b	.one_smp_info
.com2:		move.l	(A0)+,D0
		beq.b	.ok10
		swap	D0
		tst.w	D0
		beq.b	.com2
		addq.l	#4,A0
.ok10:		subq.l	#8,A0
		move.l	A0,D0
		sub.l	SampleInfo2(pc),D0
		lsr.l	#2,D0		; how many samples in second sample info
.one_smp_info
;		lea	InfoBuffer(pc),A0
;		add.l	D0,Samples(A0)
		move.l	D0,SmpIn2SmpInfo

		move.l	SampleInfo1(pc),A0
		move.l	SmpIn1SmpInfo(pc),D3
		lsl.l	#2,D3
		moveq	#0,D2

		moveq	#0,D0
		move.l	A0,A2
		add.l	(A0,D0.w),A2
		move.l	(A2),D1
		move.w	8(A2),D2
.l11:		addq.l	#4,D0

		cmp.l	D0,D3
		beq.b	.ok11

		move.l	A0,A2
		add.l	(A0,D0.w),A2
		move.l	(A2),D4
		cmp.l	D1,D4
		bcs.b	.l11
		cmp.l	D1,D4
		bne.b	.l111
		move.w	8(A2),D5
		cmp.l	D2,D5
		bcs.b	.l11
		bra.b	.l112
.l111:		move.w	8(A2),D5
.l112:		move.l	D4,D1
		move.w	D5,D2
		bra.b	.l11
.ok11:		move.l	SampleInfo1(pc),A1
		add.l	D1,A1
		lsl.l	#1,D2
		add.l	D2,A1
;		sub.l	ModulePtr(pc),A1
		sub.l	A3,A1
;		lea	InfoBuffer(pc),A0
;		move.l	A1,Calcsize(A0)
		move.l	A1,Calcsize

		move.l	SampleInfo2(pc),D0
		move.l	D0,A0
		beq.b	.ok12
		move.l	SmpIn2SmpInfo(pc),D3
		lsl.l	#2,D3
		moveq	#0,D2

		moveq	#0,D0
		move.l	A0,A2
		add.l	(A0,D0.w),A2
		move.l	(A2),D1
		move.w	8(A2),D2
.l12:		addq.l	#4,D0

		cmp.l	D0,D3
		beq.b	.ok13

		move.l	A0,A2
		add.l	(A0,D0.w),A2
		move.l	(A2),D4
		cmp.l	D1,D4
		bcs.b	.l12
		cmp.l	D1,D4
		bne.b	.l113
		move.w	8(A2),D5
		cmp.l	D2,D5
		bcs.b	.l12
		bra.b	.l114
.l113:		move.w	8(A2),D5
.l114:		move.l	D4,D1
		move.w	D5,D2
		bra.b	.l12

.ok13:		move.l	SampleInfo1(pc),A1
		add.l	D1,A1
		lsl.l	#1,D2
		add.l	D2,A1
;		sub.l	ModulePtr(pc),A1
		sub.l	A3,A1
;		lea	InfoBuffer(pc),A0
;		cmp.l	Calcsize(A0),A1
		cmp.l	Calcsize(pc),A1
		bcs.b	.ok12
;		move.l	A1,Calcsize(A0)
		move.l	A1,Calcsize
.ok12:
;		movea.l	A1,A0
		movea.l	A3,A0
		moveq	#0,D0
;		moveq	#-1,D1
		move.l	Calcsize(pc),D1
.Error:		movem.l	(A7)+,D2-D7/A2-A6
		rts

Calcsize:	dc.l	0
SampleInfo1:	dc.l	0
SmpIn1SmpInfo:	dc.l	0
SampleInfo2:	dc.l	0
SmpIn2SmpInfo:	dc.l	0


*		cmp.w	#$6000,(a1)
*		bne.w	.Return
*		cmp.w	#$6000,4(a1)
*		bne.s	.Return
*
*		*--- ersten Jump testen ---*
*		moveq	#0,d2
*		move.w	2(a1),d2			;Offset negativ ?
*		blt.s	.Return
*		cmp.l	d0,d2
*		bhi.s	.Return
*		lea	2(a1,d2.w),a0
*		cmp.l	#$3f006100,(a0)
*		bne.s	.Return
*
*		move.w	4(a0),d2			;Offset negativ ?
*		move.l	d0,d1
*		add.w	d2,d1
*		cmp.l	d0,d1
*		bhi.s	.Return
*		lea	4(a0,d2.w),a0
*		cmp.l	#$4df900df,(a0)
*		bne.s	.Return
*
*.Ja2:		move.l	a1,a0
*		moveq	#0,d0
*		moveq	#-1,d1
*.Return:	rts
*
*


	if	test
data:		incdir	"Modules:Verschiedenes/"
	*	ds.b	200

		incdir	"Modules:Verschiedenes/"
		*incbin	"BD.Blasteroids 1"              
		*incbin	"BD.Blasteroids 2"              
		*incbin	"BD.Deflektor"                  
		*incbin	"BD.HotRod"                     
		*incbin	"BD.Motorhead 1"
		*incbin	"BD.Motorhead 2"                
		*incbin	"BD.Motorhead 3"                
		*incbin	"BD.PassingShot"                
		*incbin	"BD.RickDangerous2_Patched"     
		*incbin	"BD.SuperCars"                  
		incbin	"BD.Switchblade"                





datasize	= *-data
	endc


