*****************************************************************************
*   $Id: BytePacker.a,v 1.2 2005/06/23 14:39:16 stoecker Exp $
*   BytePacker and related types 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_TSK		dc.l	S_LPv1-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_TSK-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_TSK-Xfd68KHeader
		dc.l	DB_TSK-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_TSKCruncher
		dc.w	0
		dc.l	$114+4+$20

N_TSK		dc.b	'TSK Cruncher',0
		even

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

RB_TSK		cmp.l	#$d1ed0004,$34(a0)
		bne.s	.Exit
		cmp.l	#$08800007,$96(a0)
		bne.s	.Exit
		cmp.l	#$08000007,$82(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

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

		move.b	$4d(a3),d6
		move.b	$55(a3),d7

		lea	$114(a3),a0
		move.l	a0,a1
		add.l	4(a1),a0
		addq.w	#1,a0
		add.w	#12,a1
		moveq	#0,d2

.C_000024	cmp.l	a1,a0
		beq.s	.C_0000E0
		cmp.b	-(a0),d6
		beq.s	.C_000046
		cmp.b	(a0),d7
		beq.s	.C_000094
		addq.l	#1,d2
		bra.s	.C_000024

.C_000046	tst.b	-(a0)
		bne.s	.C_000054
		addq.l	#1,d2
		bra.s	.C_000024

.C_000054	moveq	#0,d0
		move.b	(a0),d0
		moveq	#0,d1
		btst	#7,d0
		beq.s	.C_000070
		move.b	-(a0),d1
		bra.s	.C_000076

.C_000070	move.b	-(a0),d1
		lsl.w	#8,d1
		move.b	-(a0),d1
.C_000076	bclr	#7,d0
.C_000080	add.l	d0,d2
		dbra	d1,.C_000080
		sub.w	d0,a0
		bra.s	.C_000024

.C_000094	tst.b	-(a0)
		bne.s	.C_0000A2
		addq.l	#1,d2
		bra.s	.C_000024

.C_0000A2	moveq	#0,d0
		moveq	#0,d1
		move.b	(a0),d0
		btst	#7,d0
		beq.s	.C_0000BE
		move.b	-(a0),d1
		bra.s	.C_0000C4

.C_0000BE	move.b	-(a0),d1
		lsl.w	#8,d1
		move.b	-(a0),d1
.C_0000C4	bclr	#7,d0
		add.l	d0,d2
		bra.s	.C_000024

.C_0000E0	move.l	$114+0(a3),d0
		addq.l	#1,d0
		sub.l	d2,d0
		move.l	d0,xfdbi_DecrAddress(a5)
		move.l	$114+8(a3),xfdbi_JmpAddress(a5)

		move.l	d2,d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		ADDQ.L	#1,D0
		move.l	d0,xfdbi_TargetBufLen(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	.Exit

		move.l	d0,a1
		addq.w	#1,a1
		add.l	d2,a1
		lea	$114(a3),a0
		move.l	a0,a2
		add.l	4(a2),a0
		addq.w	#1,a0
		add.w	#12,a2

.D_000024	cmp.l	a2,a0
		beq.s	.D_0000E0
		cmp.b	-(a0),d6
		beq.s	.D_000046
		cmp.b	(a0),d7
		beq.s	.D_000094
		move.b	(a0),-(a1)
		bra.s	.D_000024

.D_000046	tst.b	-(a0)
		bne.s	.D_000054
		move.b	d6,-(a1)
		bra.s	.D_000024

.D_000054	moveq	#0,d0
		move.b	(a0),d0
		moveq	#0,d1
		btst	#7,d0
		beq.s	.D_000070
		move.b	-(a0),d1
		bra.s	.D_000076

.D_000070	move.b	-(a0),d1
		lsl.w	#8,d1
		move.b	-(a0),d1
.D_000076	bclr	#7,d0
		subq.w	#1,d0
		movem.l	d0/a0,-(a7)
.D_000080	move.b	-(a0),-(a1)
		dbra	d0,.D_000080
		movem.l	(a7),d0/a0
		dbra	d1,.D_000080
		addq.w	#8,a7
		sub.w	d0,a0
		subq.w	#1,a0
		bra.s	.D_000024

.D_000094	tst.b	-(a0)
		bne.s	.D_0000A2
		move.b	d7,-(a1)
		bra.s	.D_000024

.D_0000A2	moveq	#0,d0
		moveq	#0,d1
		move.b	(a0),d0
		btst	#7,d0
		beq.s	.D_0000BE
		move.b	-(a0),d1
		bra.s	.D_0000C4

.D_0000BE	move.b	-(a0),d1
		lsl.w	#8,d1
		move.b	-(a0),d1
.D_0000C4	bclr	#7,d0
		subq.w	#1,d0
.D_0000CE	move.b	-1(a1,d1.w),-(a1)
		dbra	d0,.D_0000CE
		bra.s	.D_000024

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

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

S_LPv1		dc.l	S_CrMaster10-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_LPv1-Xfd68KHeader
		dc.w	XFDPFF_DATA|XFDPFF_RECOGLEN|XFDPFF_USERTARGET
		dc.w	0
		dc.l	RB_LPv1-Xfd68KHeader
		dc.l	DB_LPv1-Xfd68KHeader
		dc.l	SD_LPv1-Xfd68KHeader
		dc.l	VD_LPv1-Xfd68KHeader
		dc.w	XFDID_LightPack15
		dc.w	0
		dc.l	12+4

N_LPv1		dc.b	'LightPack 1.5',0
		even

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

RB_LPv1		moveq	#0,d0
		cmp.l	#'LPv1',(a0)
		bne.s	.Exit
		MOVE.L	4(A0),D1
		MOVE.L	D1,xfdrr_MinTargetLen(A1)
		MOVE.L	D1,xfdrr_FinalTargetLen(A1)
		MOVEQ	#$C,D1
		ADD.L	8(A0),D1
		MOVE.L	D1,xfdrr_MinSourceLen(A1)
		moveq	#1,d0
.Exit		rts

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

SD_LPv1		moveq	#0,d0
		cmp.l	#'LPv1',(a0)
		bne.s	.Exit
		moveq	#1,d0
.Exit		rts

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

VD_LPv1		moveq	#$c,d1
		add.l	8(a0),d1	;crlen
		cmp.l	d0,d1		;crlen > buflen ??
		bgt.s	.Exit
		move.l	d1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DB_LPv1		movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdbi_SourceBuffer(a5),a2
		move.l	xfdbi_UserTargetBuf(A5),A1
		addq.w	#4,a2
		move.l	(a2)+,d2
		addq.w	#4,a2
		move.b	#$97,d3

.Next		cmp.b	(a2),d3
		beq.s	.UnPack
		move.b	(a2)+,(a1)+
		subq.l	#1,d2
		bne.s	.Next
		bra.s	.Ok

.UnPack		move.b	1(a2),d0
		lsl.w	#8,d0
		move.b	2(a2),d0
		move.b	3(a2),d1
		subq.w	#1,d0
.Copy		move.b	d1,(a1)+
		subq.l	#1,d2
		beq.s	.Ok
		dbf	d0,.Copy
		addq.w	#4,a2
		bra.s	.Next

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

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

S_CrMaster10	dc.l	S_HQCComp-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_CrMaster10-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_CrMaster10-Xfd68KHeader
		dc.l	DB_CrMaster10-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_CrunchMaster10
		dc.w	0
		dc.l	$aa+4+24

N_CrMaster10	dc.b	'Crunch Master 1.0',0
		even

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

RB_CrMaster10	cmp.l	#$0c505456,$46(a0)
		bne.s	.Exit
		cmp.l	#$0c685456,$4e(a0)
		bne.s	.Exit
		cmp.l	#$45fa007a,$26(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

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

		lea	$aa(a3),a0
		move.l	a0,a2
		add.l	$a2(a3),a0
		subq.w	#6,a0

		moveq	#0,d0		;length
		move.w	#'TV',d2
		moveq	#0,d3

.Len1		subq.w	#1,d3
		cmp.w	(a0),d2
		bne.s	.Len3
		cmp.w	6(a0),d2
		bne.s	.Len3
		tst.w	d3
		beq.s	.Len3
		moveq	#3,d3
		move.w	2(a0),d1
		subq.l	#6,d0
.Len2		addq.l	#2,d0
		dbra	d1,.Len2
		bra.s	.Len4

.Len3		addq.l	#2,d0
.Len4		subq.w	#2,a0
		cmp.l	a2,a0
		bcc.s	.Len1

		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(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

		moveq	#0,d4
		lea	$aa(a3),a0
		move.l	a0,a2
		add.l	$a2(a3),a0
		subq.w	#6,a0
		move.l	d0,a1
		add.l	xfdbi_TargetBufSaveLen(a5),a1

.Decr1		subq.w	#1,d4
		cmp.w	(a0),d2
		bne.s	.Decr3
		cmp.w	6(a0),d2
		bne.s	.Decr3
		tst.w	d4
		beq.s	.Decr3
		moveq	#3,d4
		move.w	2(a0),d0
		move.w	4(a0),d1
		addq.w	#6,a1
.Decr2		move.w	d1,-(a1)
		dbra	d0,.Decr2
		bra.s	.Decr4

.Decr3		move.w	(a0),-(a1)
.Decr4		subq.w	#2,a0
		cmp.l	a2,a0
		bcc.s	.Decr1

		move.l	$3c(a3),d0
		sub.l	xfdbi_TargetBufSaveLen(a5),d0
		move.l	d0,xfdbi_DecrAddress(a5)
		add.l	$a6(a3),d0
		move.l	d0,xfdbi_JmpAddress(a5)

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

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

S_HQCComp	dc.l	S_FlashSpeed10-Xfd68KHeader
		dc.w	2
		dc.w	39
		dc.l	N_HQCComp-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_HQCComp-Xfd68KHeader
		dc.l	DB_HQCComp-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_HQCCompressor100
		dc.w	0
		dc.l	$298+20+4

N_HQCComp	dc.b	'HQC Compressor 1.00',0
		even

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

RB_HQCComp	cmp.l	#$c0fc0004,$38(a0)
		bne.s	.Exit
		cmp.l	#$4bfa0212,$4c(a0)
		bne.s	.Exit
		cmp.l	#$b0ad0008,$94(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

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

		move.l	$298+0(a2),d0
		move.l	d0,xfdbi_DecrAddress(a5)
		move.l	d0,xfdbi_JmpAddress(a5)

		move.l	$298+12(a2),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(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	$298+16(a2),a0
		move.l	d0,a1
		move.l	a0,a3
		add.l	$298+4(a2),a3
		move.l	a1,a4
		add.l	$298+12(a2),a4
		move.b	$298+11(a2),d2

.Next		move.b	(a0)+,d0
		cmp.b	d2,d0
		bne.s	.Skip
		moveq	#0,d1
		move.b	(a0)+,d1
		beq.s	.Skip
		move.b	(a0)+,d0
		subq.w	#2,d1
.Copy		move.b	d0,(a1)+
		cmp.l	a4,a1
		beq.s	.Ok
		dbf	d1,.Copy
.Skip		move.b	d0,(a1)+
		cmp.l	a4,a1
		beq.s	.Ok
		cmp.l	a3,a0
		bls.s	.Next

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

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

S_FlashSpeed10
		IFND	XFD_LASTSLAVE
		dc.l	S_BytePacker_END-Xfd68KHeader
		ELSE
		dc.l	0
		ENDIF
		dc.w	2
		dc.w	39
		dc.l	N_FlashSpeed10-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_FlashSpeed10-Xfd68KHeader
		dc.l	DB_FlashSpeed10-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_FlashSpeed10
		dc.w	0
		dc.l	$70+4+4

N_FlashSpeed10	dc.b	'FlashSpeed 1.0',0
		even

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

RB_FlashSpeed10	cmp.l	#$0c180001,$36(a0)
		bne.s	.Exit
		cmp.l	#$0c100001,$3c(a0)
		bne.s	.Exit
		cmp.l	#$0c280001,$42(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

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

		lea	$70(a3),a0

		move.l	$2a(a3),xfdbi_JmpAddress(a5)
		move.l	$2a(a3),xfdbi_DecrAddress(a5)

		moveq	#0,d0
		moveq	#0,d1
		moveq	#1,d2
.Len1		addq.l	#1,d0
		cmp.b	(a0)+,d2
		bne.s	.Len1
		cmp.b	(a0),d2
		bne.s	.Len1
		cmp.b	1(a0),d2
		beq.s	.Len2
		subq.l	#1,d0
		addq.w	#1,a0
		bra.s	.Len3

.Len2		addq.w	#2,a0
.Len3		move.b	(a0)+,d1
		beq.s	.Len4
		addq.w	#1,a0
		add.l	d1,d0
		bra.s	.Len1

.Len4		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		addq.l	#1,D0
		move.l	d0,xfdbi_TargetBufLen(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	$70(a3),a0
		move.l	d0,a1

		moveq	#0,d0
		moveq	#0,d1
		moveq	#1,d2
.Decr1		move.b	(a0),(a1)+
		cmp.b	(a0)+,d2
		bne.s	.Decr1
		cmp.b	(a0),d2
		bne.s	.Decr1
		cmp.b	1(a0),d2
		beq.s	.Decr2
		subq.w	#1,a1
		addq.w	#1,a0
		bra.s	.Decr3

.Decr2		addq.w	#2,a0
.Decr3		move.b	(a0)+,d1
		beq.s	.Decr5
		move.b	(a0)+,d0
		subq.w	#1,d1
.Decr4		move.b	d0,(a1)+
		dbra	d1,.Decr4
		moveq	#0,d1
		bra.s	.Decr1

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

S_BytePacker_END

