*****************************************************************************
*   $Id: FreewaY.a,v 1.2 2005/06/23 14:39:16 stoecker Exp $
*   FreewaY 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_FreewaY	
		IFND	XFD_LASTSLAVE
		dc.l	S_FreewaY_END-Xfd68KHeader
		ELSE
		dc.l	0
		ENDIF
		dc.w	2
		dc.w	39
		dc.l	N_FreewaY-Xfd68KHeader
		dc.w	XFDPFF_ADDR
		dc.w	0
		dc.l	RB_FreewaY-Xfd68KHeader
		dc.l	DB_FreewaY-Xfd68KHeader
		dc.l	0
		dc.l	0
		dc.w	XFDID_Freeway07
		dc.w	0
		dc.l	$264+$2c+4

N_FreewaY	dc.b	'FreewaY 0.7',0
		even

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

RB_FreewaY	cmp.l	#$1a3a017c,$f2(a0)
		bne.s	.Exit
		cmp.l	#$1c3a0179,$f6(a0)
		bne.s	.Exit
		cmp.l	#$1a3a0138,$138(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

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

		move.l	$27c(a4),xfdbi_JmpAddress(a5)
		move.l	$274(a4),d0
		add.l	$284(a4),d0
		move.l	d0,xfdbi_DecrAddress(a5)

		move.l	$278(a4),d0
		sub.l	$274(a4),d0
		move.l	$284(a4),d1
		add.l	d1,d0
		move.l	d0,xfdbi_TargetBufLen(a5)
		add.l	d1,d1
		sub.l	d1,d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)

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

		lea	$28c(a4),a0	;data beg
		move.l	a0,-(a7)
		add.l	$26c(a4),a0	;abs data end
		sub.l	$274(a4),a0	;abs data beg -> a0: data end

		move.l	xfdbi_TargetBuffer(a5),a1
		move.l	a1,a2
		add.l	$268(a4),a2
		sub.l	$274(a4),a2
		move.l	a2,a3

		bsr	D_FreewaY

		cmp.l	(a7)+,a0
		bne.s	.Corrupt
		tst.w	d3
		beq.s	.Ok

.Corrupt	move.l	xfdbi_TargetBuffer(a5),a1
		move.l	xfdbi_TargetBufLen(a5),d0
		jsr	-210(a6)
		move.w	#XFDERR_CORRUPTEDDATA,xfdbi_Error(a5)
		moveq	#0,d0
		bra.s	.Exit

.Ok		move.l	a3,a0		;source
		move.l	xfdbi_TargetBuffer(a5),a1
		add.l	$284(a4),a1	;target start
		move.l	a1,a2
		add.l	$264(a4),a2
		sub.l	$274(a4),a2	;target end
		move.l	a2,a3

		move.b	$272(a4),d5
		bsr.b	D_FreewaYB1
		cmp.l	xfdbi_TargetBuffer(a5),a0
		bne.s	.Corrupt

		move.l	a3,a2
		bsr.s	.Copy

		move.l	a3,a0
		sub.l	$284(a4),a0
		move.l	xfdbi_TargetBuffer(a5),a1
		add.l	$284(a4),a1	;target start
		move.l	a1,a2
		add.l	$278(a4),a2
		sub.l	xfdbi_DecrAddress(a5),a2	;target end
		move.l	a2,a3

		move.b	$270(a4),d5
		move.b	$271(a4),d6
		bsr.s	D_FreewaYB2
		cmp.l	xfdbi_TargetBuffer(a5),a0
		bne.s	.Corrupt

		move.l	a3,a2
		bsr.s	.Copy

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

.Copy		move.l	xfdbi_TargetBuffer(a5),a0
		move.l	a0,a1
		add.l	$284(a4),a0
.Copy1		move.b	(a0)+,(a1)+
		cmp.l	a0,a2
		bne.s	.Copy1
		rts

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

D_FreewaYB2	move.b	-(a0),d0
		cmp.b	d5,d0
		bne.s	.Decr2
		move.b	-(a0),d2
		move.b	-(a0),d1
.Decr1		move.b	d1,-(a2)
		cmp.l	a1,a2
		beq.s	.Exit
		subq.b	#1,d2
		bne.s	.Decr1
		bra.s	D_FreewaYB2

.Decr2		cmp.b	d6,d0
		bne.s	.Decr3
		moveq	#0,d1
		move.b	-(a0),d2
		bra.s	.Decr1

.Decr3		move.b	d0,-(a2)
		cmp.l	a1,a2
		bne.s	D_FreewaYB2
.Exit		rts

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

D_FreewaYB1	move.b	-(a0),d1
		cmp.b	d5,d1
		beq.s	.Decr2
.Decr1		move.b	d1,-(a2)
		cmp.l	a1,a2
		bne.s	D_FreewaYB1
.Exit		rts

.Decr2		move.b	-(a0),d0
		beq.s	.Decr1
		move.b	-(a0),d2
		move.l	a0,d3
.Decr3		move.b	d2,d1
		move.l	d3,a0
.Decr4		move.b	-(a0),-(a2)
		cmp.l	a1,a2
		beq.s	.Exit
		subq.b	#1,d1
		bne.s	.Decr4
		subq.b	#1,d0
		bne.s	.Decr3
		bra.s	D_FreewaYB1

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

D_FreewaY	moveq	#-1,d7
		move.l	-(a0),d5

.Decr1		moveq	#3,d4
		bsr.s	.GetBits
		tst.b	d6
		beq	.Decr13

		cmp.b	#2,d6
		bcs.s	.Decr2
		bne.s	.Decr5
		moveq	#1,d3
		bra.s	.Decr8

.Decr2		moveq	#1,d3
.Decr3		moveq	#8,d4
		bsr.s	.GetBits
		addq.w	#2,d6
		bra.s	.Decr9

.Decr4		moveq	#2,d3
		bra.s	.Decr3

.Decr5		cmp.b	#4,d6
		bcs.s	.Decr4
		bne.s	.Decr7
		moveq	#2,d3
		bra.s	.Decr8

.Decr6		moveq	#4,d4
		bsr.s	.GetBits
		move.l	d6,d3
		addq.w	#4,d3
		bra.s	.Decr3

.Decr7		cmp.b	#6,d6
		bcs.s	.Decr6
		bhi.s	.Decr10
		moveq	#4,d4
		bsr.s	.GetBits
		move.l	d6,d3
		addq.w	#4,d3
.Decr8		moveq	#11,d4
		bsr.s	.GetBits
		add.w	#$102,d6
.Decr9		move.b	-1(a2,d6.l),-(a2)
		cmp.l	a1,a2
		dbeq	d3,.Decr9
		bra.s	.Decr16

.GetBits	subq.w	#1,d4
		moveq	#0,d6
.GetBits1	lsl.l	#1,d5
		bne.s	.GetBits2
		move.l	-(a0),d5
		roxl.l	#1,d5
.GetBits2	roxl.w	#1,d6
		dbra	d4,.GetBits1
		rts

.Decr10		moveq	#1,d4
		bsr.s	.GetBits
		tst.b	d6
		bne.s	.Decr11
		moveq	#3,d3
		bra.s	.Decr3

.Decr11		moveq	#1,d4
		bsr.s	.GetBits
		tst.b	d6
		bne.s	.Decr12
		moveq	#3,d3
		bra.s	.Decr8

.Decr12		moveq	#8,d4
		bsr.s	.GetBits
		addq.w	#8,d6
		bra.s	.Decr14

.Decr13		moveq	#3,d4
		bsr.s	.GetBits
.Decr14		move.l	d6,d3
.Decr15		moveq	#8,d4
		bsr.s	.GetBits
		move.b	d6,-(a2)
		cmp.l	a1,a2
		dbeq	d3,.Decr15
.Decr16		cmp.w	d7,d3
		beq	.Decr1
		rts

S_FreewaY_END

