*****************************************************************************
*   $Id: DefJam.a,v 1.2 2005/06/23 14:39:16 stoecker Exp $
*   DefJam decrunching slave
*
*   XFD library system for cruncher handling
*   Copyright (C) 1994-1998 by Georg Hörmann
*   Copyright (C) 1998 by Dirk Stöcker <soft@dstoecker.de>
*
*   This library is free software; you can redistribute it and/or
*   modify it under the terms of the GNU Lesser General Public
*   License as published by the Free Software Foundation; either
*   version 2.1 of the License, or (at your option) any later version.
*
*   This library 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
*   Lesser General Public License for more details.
*
*   You should have received a copy of the GNU Lesser General Public
*   License along with this library; if not, write to the Free Software
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*****************************************************************************

S_DefJam32T	dc.l	S_DefJam32-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_DefJam32T-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_DefJam32T-Xfd68KHeader
		dc.l	DB_DefJam32T-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_DefJamCruncher32T
		dc.w	0
		dc.l	$2b0+4+4

N_DefJam32T	dc.b	'DefJam Cruncher 3.2T',0
		even

;-------------------------------------------------

RB_DefJam32T	cmp.l	#$43fa028c,$22(a0)
		bne.s	.Exit
		cmp.l	#$49fa015e,$50(a0)
		bne.s	.Exit
		cmp.l	#$b0076610,$18e(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

;-------------------------------------------------

DB_DefJam32T	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a3

		move.l	$188(a3),d0
		sub.l	$182(a3),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)

		move.l	$1ac(a3),xfdbi_JmpAddress(a5)
		move.l	$182(a3),xfdbi_DecrAddress(a5)

		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	_LVOAllocMem(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		lea	$2b0(a3),a0
		add.l	$34(a3),a0	;decr data end

		move.l	d0,a1
		add.l	$4c(a3),a1
		sub.l	$182(a3),a1

		lea	$1b0(a3),a4
		move.b	$17f(a3),d7		

		lea	-10(a7),a7
		move.l	a7,a6
		clr.l	(a6)
		clr.l	4(a6)
		clr.w	8(a6)
		move.b	$ff(a3),1(a6)
		move.b	$12d(a3),3(a6)
		move.b	$14d(a3),5(a6)
		move.b	$15b(a3),7(a6)
		move.b	$161(a3),9(a6)
		bsr	D_DefJam
		lea	10(a7),a7

		moveq	#1,d0
.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

**************************************************

S_DefJam32	dc.l	S_DefJam32Pro-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_DefJam32-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_DefJam32-Xfd68KHeader
		dc.l	DB_DefJam32-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_DefJamCruncher32
		dc.w	0
		dc.l	$2b0+4+4

N_DefJam32	dc.b	'DefJam Cruncher 3.2',0
		even

;-------------------------------------------------

RB_DefJam32	cmp.l	#$43fa028c,$22(a0)
		bne.s	.Exit
		cmp.l	#$49fa015e,$50(a0)
		bne.s	.Exit
		cmp.l	#$b0076610,$18c(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

;-------------------------------------------------

DB_DefJam32	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a3

		move.l	$186(a3),d0
		sub.l	$180(a3),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)

		move.l	$1aa(a3),xfdbi_JmpAddress(a5)
		move.l	$180(a3),xfdbi_DecrAddress(a5)

		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	-198(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		lea	$2b0(a3),a0
		add.l	$34(a3),a0	;decr data end

		move.l	d0,a1
		add.l	$4c(a3),a1
		sub.l	$180(a3),a1

		lea	$1b0(a3),a4
		move.b	$17d(a3),d7		

		lea	-10(a7),a7
		move.l	a7,a6
		clr.l	(a6)
		clr.l	4(a6)
		clr.w	8(a6)
		move.b	$ff(a3),1(a6)
		move.b	$12d(a3),3(a6)
		move.b	$14d(a3),5(a6)
		move.b	$15b(a3),7(a6)
		move.b	$161(a3),9(a6)
		bsr	D_DefJam
		lea	10(a7),a7

		moveq	#1,d0
.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

**************************************************

S_DefJam32Pro
		IFND	XFD_LASTSLAVE
		dc.l	S_DefJam_END-Xfd68KHeader
		ELSE
		dc.l	0
		ENDIF
		dc.w	2
		dc.w	39
		dc.l	N_DefJam32Pro-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_DefJam32Pro-Xfd68KHeader
		dc.l	DB_DefJam32Pro-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_DefJamCruncher32Pro
		dc.w	0
		dc.l	$318+4+4

N_DefJam32Pro	dc.b	'DefJam Cruncher 3.2 Pro',0
		even

;-------------------------------------------------

RB_DefJam32Pro	cmp.l	#$43fa029a,$7c(a0)
		bne.s	.Exit
		cmp.l	#$49fa0126,$f0(a0)
		bne.s	.Exit
		cmp.l	#$b0076610,$a2(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

;-------------------------------------------------

DB_DefJam32Pro	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a3

		move.l	$9c(a3),d0
		sub.l	$96(a3),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)

		move.l	$74(a3),xfdbi_JmpAddress(a5)
		move.l	$96(a3),xfdbi_DecrAddress(a5)

		move.l	xfdbi_TargetBufMemType(a5),d1
		move.l	xfdm_ExecBase(a6),a6
		jsr	-198(a6)
		move.w	#XFDERR_NOMEMORY,xfdbi_Error(a5)
		move.l	d0,xfdbi_TargetBuffer(a5)
		beq.s	.Exit

		lea	$318(a3),a0
		add.l	$d4(a3),a0	;decr data end

		move.l	d0,a1
		add.l	$ec(a3),a1
		sub.l	$96(a3),a1

		lea	$218(a3),a4
		move.b	$93(a3),d7		

		lea	-10(a7),a7
		move.l	a7,a6
		clr.l	(a6)
		clr.l	4(a6)
		clr.w	8(a6)
		move.b	$19f(a3),1(a6)
		move.b	$1cd(a3),3(a6)
		move.b	$1ed(a3),5(a6)
		move.b	$1fb(a3),7(a6)
		move.b	$201(a3),9(a6)
		bsr.b	D_DefJam
		lea	10(a7),a7

		moveq	#1,d0
.Exit		movem.l	(a7)+,d2-d7/a2-a6
		rts

;=================================================

* a0: source end
* a1: target begin
* d5-d7/a4/a6: parameters

D_DefJam	moveq	#-1,d6
		move.l	-(a0),a2
		add.l	a1,a2
		move.l	-(a0),d0
.Decr1		moveq	#3,d1
		bsr	.Decr20
		tst.w	d2
		beq.b	.Decr15
		cmp.b	#7,d2
		bne.s	.Decr6
		bsr	.Decr17
		bcs.s	.Decr3
		moveq	#2,d1
		bsr	.Decr20
		addq.w	#7,d2
		bra.s	.Decr6

.Decr3		moveq	#8,d1
		bsr	.Decr20
		tst.w	d2
		beq.s	.Decr4
		add.w	#10,d2
		bra.s	.Decr6

.Decr4		moveq	#12,d1
		bsr	.Decr20
		tst.w	d2
		beq.s	.Decr5
		add.w	#$109,d2
		bra.s	.Decr6

.Decr5		moveq	#15,d1
		bsr.b	.Decr20
		add.l	#$110B,d2
.Decr6		subq.w	#1,d2
		move.w	d2,d3
.Decr7		bsr.s	.Decr17
		bcs.s	.Decr9
		moveq	#8,d5
		moveq	#6,d1
		bra.s	.Decr14

.Decr9		bsr.s	.Decr17
		bcs.s	.Decr11
		moveq	#0,d5
		moveq	#3,d1
		bra.s	.Decr14

.Decr11		bsr.s	.Decr17
		bcs.s	.Decr13
		moveq	#$48,d5
		moveq	#6,d1
		bra.s	.Decr14

.Decr13		move.w	#$88,d5
		moveq	#7,d1
.Decr14		bsr.s	.Decr20
		add.w	d5,d2
		move.b	(a4,d2.w),-(a2)
		cmp.l	a1,a2
		dbeq	d3,.Decr7
		cmp.w	d6,d3
		bne.s	.Decr29

.Decr15		bsr.s	.Decr17
		bcc.s	.Decr24
		moveq	#2,d1
		bsr.s	.Decr20
		move.w	(a6),d1			;<--
		moveq	#3,d3
		tst.b	d2
		beq.s	.Decr27
		cmp.b	d2,d3
		beq.s	.Decr23
		cmp.b	#1,d2
		beq.s	.Decr18
		moveq	#6,d3
		moveq	#3,d1
		bra.s	.Decr19

.Decr17		lsr.l	#1,d0
		bne.s	.Decr16
		move.l	-(a0),d0
		move.b	#$10,ccr
		roxr.l	#1,d0
.Decr16		rts

.Decr18		moveq	#4,d3
		moveq	#1,d1
.Decr19		bsr.s	.Decr20
		add.w	d2,d3
		move.w	2(a6),d1			;<--
		bra.s	.Decr27

.Decr20		subq.w	#1,d1
		moveq	#0,d2
.Decr21		bsr.s	.Decr17
		addx.l	d2,d2
		dbra	d1,.Decr21
		rts

.Decr23		moveq	#8,d1
		bsr.s	.Decr20
		move.w	d2,d3
		add.w	#14,d3
		move.w	4(a6),d1			;<--
		bra.s	.Decr27

.Decr24		bsr.s	.Decr17
		bcs.s	.Decr26
		moveq	#2,d3
		move.w	6(a6),d1			;<--
		bra.s	.Decr27

.Decr26		moveq	#1,d3
		move.w	8(a6),d1			;<--
.Decr27		bsr.s	.Decr20
.Decr28		move.b	-1(a2,d2.w),-(a2)
		cmp.l	a1,a2
		dbeq	d3,.Decr28
		cmp.w	d6,d3
		beq	.Decr1

.Decr29		move.l	xfdbi_TargetBuffer(a5),a0
		move.l	a0,a2
		add.l	xfdbi_TargetBufLen(a5),a2
.Decr30		move.b	(a1)+,d0
		cmp.b	d7,d0		;<--
		bne.s	.Decr32
		moveq	#0,d1
		move.b	(a1)+,d1
		beq.s	.Decr32
		move.b	(a1)+,d0
		addq.w	#1,d1
.Decr31		move.b	d0,(a0)+
		cmp.l	a2,a0
		dbeq	d1,.Decr31
		cmp.w	d6,d1
		bne.s	.Exit
.Decr32		move.b	d0,(a0)+
		cmp.l	a2,a0
		bne.s	.Decr30
.Exit		rts

S_DefJam_END

