******************************************************
**     XFD external decruncher for RNC Cruncher     **
**        written by Dexter & Bartman / Abyss       **
**               RNC2 support by WEPL               **
**             modified by Georg Hörmann            **
******************************************************

		OUTPUT	"LIBS:xfd/RNC"

		SECTION	RNCDecrunch,CODE

		INCDIR	"dh0:Include_Asm"	;change this as you need it
		INCLUDE	"dh0:xfd/xfdmaster.i"

; xfdForeman structure MUST be first thing in all external decrunchers

F_RNC		moveq	#-1,d0		;security
		rts
		dc.l	XFDF_ID		;id
		dc.w	1		;version
		dc.w	0
		dc.l	0,0		;private
		dc.l	S_RNC		;first slave

		dc.b	"$VER: External RNC decruncher for xfdmaster.library "
		dc.b	"by Dexter&Bartman/Abyss and WEPL, modified by G.H.",13,10,0
		cnop	0,4

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

; xfdSlave structure: this one doesn't support segment decrunching

S_RNC		dc.l	0		;no more slaves
		dc.w	1		;version
		dc.w	33		;master version
		dc.l	N_RNC		;name
		dc.w	XFDPFF_DATA	;flags
		dc.w	0
		dc.l	RB_RNC		;recog buffer
		dc.l	DB_RNC		;decrunch buffer
		dc.l	0		;recog segment
		dc.l	0		;decrunch segment

N_RNC		dc.b	'Rob Northern (RNC) Cruncher',0
		even

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

; Recog buffer function: receives buffer + length in a0/d0

RB_RNC		cmp.l	#18+2,d0
		ble.s	.Exit
		cmp.l	#"RNC"<<8!1,(a0)
		beq.s	.Ok
		cmp.l	#"RNC"<<8!2,(a0)
		bne.s	.Exit
.Ok		moveq	#1,d0
		rts

.Exit		moveq	#0,d0
		rts

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

;Decrunch buffer function: receives bufferinfo in a0

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

		move.l	4(a2),d0
		move.l	d0,xfdbi_TargetBufSaveLen(a5)
		move.l	d0,xfdbi_TargetBufLen(a5)

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

		move.l	d0,a1
		move.l	a2,a0

		pea	.Cont(pc)
		cmp.b	#1,3(a0)
		beq.s	D_RNC1
		addq.w	#4,a7
		bsr	D_RNC2

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

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

D_RNC1		movem.l	d2-d7/a2-a6,-(a7)
		lea	-$180(a7),a7
		move.l	a7,a2

		lea	18(a0),a3
		move.l	a1,a5
		move.l	4(a0),d0
		lea	(a5,d0.l),a6

		moveq	#0,d7
		move.b	1(a3),d6
		rol.w	#8,d6
		move.b	(a3),d6
		moveq	#2,d0
		moveq	#2,d1
		bsr	.lbc00112c

		moveq	#-1,d5

.Next		move.l	a2,a0
		bsr	.lbc00115c
		lea	$80(a2),a0
		bsr	.lbc00115c
		lea	$100(a2),a0
		bsr	.lbc00115c
		moveq	#-1,d0
		moveq	#16,d1
		bsr.s	.lbc00112c
		move.w	d0,d4
		subq.w	#1,d4
		bra.s	.lbc001088

.lbc001058	lea	$80(a2),a0
		moveq	#0,d0
		bsr.s	.lbc0010f6
		neg.l	d0
		lea	-1(a5,d0.l),a1
		lea	$100(a2),a0
		bsr.s	.lbc0010f6
		move.b	(a1)+,(a5)+
		cmp.l	a6,a5
		beq.s	.Exit
.lbc001072	move.b	(a1)+,(a5)+
		cmp.l	a6,a5
		dbeq	d0,.lbc001072
		cmp.w	d5,d0
		bne.s	.Exit

.lbc001088	movea.l	a2,a0
		bsr.s	.lbc0010f6
		subq.w	#1,d0
		bmi.s	.lbc0010ba
.lbc001090	move.b	(a3)+,(a5)+
		cmp.l	a6,a5
		dbeq	d0,.lbc001090
		cmp.w	d5,d0
		bne.s	.Exit

		move.b	1(a3),d0
		rol.w	#8,d0
		move.b	(a3),d0
		lsl.l	d7,d0
		moveq	#1,d1
		lsl.w	d7,d1
		subq.w	#1,d1
		and.l	d1,d6
		or.l	d0,d6
.lbc0010ba	dbf	d4,.lbc001058
		bra.s	.Next

.Exit		lea	$180(a7),a7
		movem.l	(a7)+,d2-d7/a2-a6
		rts

.lbc00112c	and.w	d6,d0
		sub.b	d1,d7
		bge.s	.lbc001134
		bsr.s	.lbc001138
.lbc001134	lsr.l	d1,d6
		rts

.lbc0010f6	move.w	(a0)+,d0
		and.w	d6,d0
		sub.w	(a0)+,d0
		bne.s	.lbc0010f6
		move.b	$3c(a0),d1
		sub.b	d1,d7
		bge.s	.lbc001108
		bsr.s	.lbc001138
.lbc001108	lsr.l	d1,d6
		move.b	$3d(a0),d0
		cmp.b	#2,d0
		blt.s	.lbc00112a
		subq.b	#1,d0
		move.b	d0,d1
		move.b	d0,d2
		move.w	$3e(a0),d0
		and.w	d6,d0
		sub.b	d1,d7
		bge.s	.lbc001126
		bsr.s	.lbc001138
.lbc001126	lsr.l	d1,d6
		bset	d2,d0
.lbc00112a	rts

.lbc001138	add.b	d1,d7
		lsr.l	d7,d6
		swap	d6
		addq.w	#4,a3
		move.b	-(a3),d6
		rol.w	#8,d6
		move.b	-(a3),d6
		swap	d6
		sub.b	d7,d1
		moveq	#16,d7
		sub.b	d1,d7
		rts

.lbc00115c	moveq	#$1f,d0
		moveq	#5,d1
		bsr.s	.lbc00112c
		subq.w	#1,d0
		bmi.s	.lbc001202

		move.w	d0,d2
		move.w	d0,d3
		lea	-16(a7),a7
		move.l	a7,a1

.lbc001172	moveq	#$f,d0
		moveq	#4,d1
		bsr.s	.lbc00112c
		move.b	d0,(a1)+
		dbf	d2,.lbc001172

		moveq	#1,d0
		ror.l	#1,d0
		moveq	#1,d1
		moveq	#0,d2
		movem.l	d5-d7,-(a7)
.lbc00119a	move.w	d3,d4
		lea	12(a7),a1
.lbc0011a0	cmp.b	(a1)+,d1
		bne.s	.lbc0011ec
		moveq	#1,d5
		lsl.w	d1,d5
		subq.w	#1,d5
		move.w	d5,(a0)+
		move.l	d2,d5
		swap	d5
		move.w	d1,d7
		subq.w	#1,d7
.lbc0011c2	roxl.w	#1,d5
		roxr.w	#1,d6
		dbra	d7,.lbc0011c2

		moveq	#16,d5
		sub.b	d1,d5
		lsr.w	d5,d6
		move.w	d6,(a0)+
		move.b	d1,$3c(a0)
		move.b	d3,d5
		sub.b	d4,d5
		move.b	d5,$3d(a0)
		moveq	#1,d6
		subq.b	#1,d5
		lsl.w	d5,d6
		subq.w	#1,d6
		move.w	d6,$3e(a0)
		add.l	d0,d2
.lbc0011ec	dbf	d4,.lbc0011a0

		lsr.l	#1,d0
		addq.b	#1,d1
		cmp.b	#17,d1
		bne.s	.lbc00119a

		movem.l	(a7)+,d5-d7
		lea	16(a7),a7
.lbc001202	rts

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

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

		lea	18(a0),a3
		move.l	a1,a5
		move.l	4(a0),d0
		lea	(a5,d0.l),a6

		moveq	#-$80,d7
		add.b	d7,d7
		move.b	(a3)+,d7
		addx.b	d7,d7
		add.b	d7,d7
		bra.w	.350

.298		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.2ce

.29e		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.2e8

.2a4		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.2ee

.2aa		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.2f6

.2b0		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.302

.2b6		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.308

.2bc		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.30e

.2c2		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.31a

.2c8		moveq	#3,d5
.2ca		add.b	d7,d7
		beq.b	.298
.2ce		addx.w	d6,d6
		dbra	d5,.2ca
		addq.w	#2,d6
.2d6		move.b	(a3)+,(a5)+
		move.b	(a3)+,(a5)+
		move.b	(a3)+,(a5)+
		move.b	(a3)+,(a5)+
		dbra	d6,.2d6
		bra.b	.350

.2e4		add.b	d7,d7
		beq.b	.29e
.2e8		addx.w	d5,d5
		add.b	d7,d7
		beq.b	.2a4
.2ee		bcc.b	.2fe
		subq.w	#1,d5
		add.b	d7,d7
		beq.b	.2aa
.2f6		addx.w	d5,d5
		cmpi.b	#9,d5
		beq.b	.2c8
.2fe		add.b	d7,d7
		beq.b	.2b0
.302		bcc.b	.31e
		add.b	d7,d7
		beq.b	.2b6
.308		addx.w	d6,d6
		add.b	d7,d7
		beq.b	.2bc
.30e		bcs.b	.382
		tst.w	d6
		bne.b	.31c
		addq.w	#1,d6
.316		add.b	d7,d7
		beq.b	.2c2
.31a		addx.w	d6,d6
.31c		rol.w	#8,d6
.31e		move.b	(a3)+,d6
		movea.l	a5,a0
		suba.w	d6,a0
		subq.w	#1,a0
		lsr.w	#1,d5
		bcc.b	.32c
		move.b	(a0)+,(a5)+
.32c		subq.w	#1,d5
		tst.w	d6
		bne.b	.33e
		move.b	(a0),d6
.334		move.b	d6,(a5)+
		move.b	d6,(a5)+
		dbra	d5,.334
		bra.b	.350

.33e		move.b	(a0)+,(a5)+
		move.b	(a0)+,(a5)+
		dbra	d5,.33e
		bra.b	.350

.348		move.b	(a3)+,d7
		addx.b	d7,d7
		bcs.b	.35c
.34e		move.b	(a3)+,(a5)+
.350		add.b	d7,d7
		bcs.b	.35a
		move.b	(a3)+,(a5)+
		add.b	d7,d7
		bcc.b	.34e
.35a		beq.b	.348
.35c		moveq	#2,d5
		moveq	#0,d6
		add.b	d7,d7
		beq.b	.398
.364		bcc.w	.2e4
		add.b	d7,d7
		beq.b	.39e
.36c		bcc.b	.31e
		addq.w	#1,d5
		add.b	d7,d7
		beq.b	.3a4
.374		bcc.w	.2fe
		move.b	(a3)+,d5
		beq.b	.3b6
		addq.w	#8,d5
		bra.w	.2fe

.382		add.b	d7,d7
		beq.b	.3aa
.386		addx.w	d6,d6
		ori.w	#4,d6
		add.b	d7,d7
		beq.b	.3b0
.390		bcs.s	.31c
		bra.s	.316

.398		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.364

.39e		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.36c

.3a4		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.374

.3aa		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.386

.3b0		move.b	(a3)+,d7
		addx.b	d7,d7
		bra.b	.390

.3b6		add.b	d7,d7
		bne.b	.3be
		move.b	(a3)+,d7
		addx.b	d7,d7
.3be		bcs.b	.350

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

		END