*****************************************************************************
*   $Id: Spike.a,v 1.2 2005/06/23 14:39:16 stoecker Exp $
*   Spike Cruncher 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_Spike15
		IFND	XFD_LASTSLAVE
		dc.l	S_Spike_END-Xfd68KHeader
		ELSE
		dc.l	0
		ENDIF
		dc.w	2
		dc.w	39
		dc.l	N_Spike15-Xfd68KHeader
		dc.w	XFDPFF_RELOC!XFDPFF_RELMODE
		dc.w	0
		dc.l	RB_Spike15-Xfd68KHeader
		dc.l	DB_Spike15-Xfd68KHeader
		dc.l	RS_Spike15-Xfd68KHeader
		dc.l	DS_Spike15-Xfd68KHeader
		dc.w	XFDID_SpikeCruncher
		dc.w	0
		dc.l	$284+4+4

N_Spike15	dc.b	'Spike Cruncher',0
		even

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

RB_Spike15	cmp.l	#$183b0078,$1ac(a0)
		bne.s	.Exit
		cmp.l	#$b27b2076,$1b2(a0)
		bne.s	.Exit
		cmp.l	#$143b0076,$1ba(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

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

		move.l	$32(a3),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		add.l	#$100,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	$284(a3),a0
		move.l	d0,a2
		add.l	xfdbi_TargetBufSaveLen(a5),a2
		move.l	a2,-(a7)
		add.w	#$100,a2
		move.l	a2,a1
		sub.l	$36(a3),a1
		move.l	a1,-(a7)
		lea	$226(a3),a4
		bsr	D_Spike15

		move.w	$3a(a3),d0
		move.l	(a7)+,a1
		move.l	(a7)+,a2
		move.l	xfdbi_TargetBuffer(a5),a0
		bsr	D_Spike15Byte

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

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

RS_Spike15	add.l	a0,a0
		add.l	a0,a0
		cmp.l	#($96*4)+8,-4(a0)
		bne.s	.Exit

		cmp.l	#$183b0078,$18c(a0)
		bne.s	.Exit
		cmp.l	#$b27b2076,$192(a0)
		bne.s	.Exit
		cmp.l	#$143b0076,$19a(a0)
		bne.s	.Exit
		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

DS_Spike15	movem.l	d2-d7/a2-a6,-(a7)
		move.l	a0,a5
		move.l	xfdsi_SegList(a5),a3
		clr.l	xfdsi_SegList(a5)
		add.l	a3,a3
		add.l	a3,a3

		move.l	(a3),a1
		add.l	a1,a1
		add.l	a1,a1
		addq.w	#4,a1
		move.l	a1,a2
		move.l	a1,-(a7)
		move.l	$e(a3),d0
		add.l	d0,a1
		add.l	$12(a3),a2
		move.l	a2,-(a7)
		lea	$100(a2),a2
		lea	$100(a2),a0
.Copy		move.b	-(a1),-(a0)
		subq.l	#1,d0
		bne.s	.Copy
		move.l	a2,a1
		sub.l	$16(a3),a1
		move.l	a1,-(a7)
		lea	$206(a3),a4
		move.l	a6,-(a7)
		bsr.b	D_Spike15
		move.l	(a7)+,a6

		move.w	$1a(a3),d0
		move.l	(a7)+,a1
		move.l	(a7)+,a2
		move.l	(a7),a0
		bsr	D_Spike15Byte

		move.l	(a7)+,a0
		move.l	$12(a3),d0
		lea	xfdsi_SegList(a5),a1
		move.w	xfdsi_RelMode(a5),d1
		jsr	_LVOxfdRelocate(a6)
		moveq	#0,d1
		move.w	d0,xfdsi_Error(a5)
		bne.s	.FreeSeg
		moveq	#1,d1

.FreeSeg	move.l	d1,-(a7)
		move.l	a3,d1
		lsr.l	#2,d1
		move.l	xfdm_DosBase(a6),a6
		jsr	-156(a6)
		move.l	(a7)+,d0

		movem.l	(a7)+,d2-d7/a2-a6
		rts

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

D_Spike15	moveq	#-1,d5
		moveq	#-$80,d3
.Decr1		bsr.s	.Decr4
		bcc.s	.Decr8
		moveq	#0,d2
		bsr.s	.Decr4
		bcc.s	.Decr3
		moveq	#3,d0
		moveq	#8,d2
.Decr2		subq.w	#2,d2
		move.b	(a4,d0.w),d4
		bsr.s	.Decr6
		cmp.w	4(a4,d2.w),d1
		dbne	d0,.Decr2
		move.b	12(a4,d0.w),d2
		add.w	d1,d2
.Decr3		move.b	(a0)+,(a1)+
		cmp.l	a2,a1
		dbeq	d2,.Decr3
		cmp.w	d5,d2
		bne.s	.Exit

.Decr8		moveq	#4,d0
.Decr9		bsr.s	.Decr4
		bcc.s	.Decr10
		subq.w	#1,d0
		bne.s	.Decr9
.Decr10		clr.w	d1
		move.b	16(a4,d0.w),d4
		bmi.s	.Decr11
		bsr.s	.Decr6
.Decr11		move.b	21(a4,d0.w),d0
		add.w	d1,d0
		moveq	#2,d4
		bsr.s	.Decr6
		move.l	d1,d2
		move.b	26(a4,d2.w),d4
		add.w	d2,d2
		bsr.s	.Decr6
		add.w	34(a4,d2.w),d1
		move.l	a1,a6
		sub.l	d1,a6
.Decr12		move.b	(a6)+,(a1)+
		cmp.l	a2,a1
		dbeq	d0,.Decr12
		cmp.w	d5,d0
		beq.s	.Decr1
.Exit		rts

.Decr4		add.b	d3,d3
		bne.s	.Decr5
		move.b	(a0)+,d3
		addx.b	d3,d3
.Decr5		rts

.Decr6		moveq	#0,d1
.Decr7		bsr.s	.Decr4
		addx.w	d1,d1
		subq.b	#1,d4
		bpl.s	.Decr7
		rts

D_Spike15Byte	moveq	#0,d1
.Decr1		move.b	(a1)+,d1
		cmp.w	d0,d1
		bne.s	.Decr3
		moveq	#0,d2
		move.b	(a1)+,d2
		beq.s	.Decr3
		addq.w	#1,d2
		move.b	(a1)+,d1
.Decr2		move.b	d1,(a0)+
		cmp.l	a2,a0
		dbeq	d2,.Decr2
		tst.w	d2
		bpl.s	.Exit
.Decr3		move.b	d1,(a0)+
		cmp.l	a2,a0
		bne.s	.Decr1
.Exit		rts

S_Spike_END
