;V2 Depack routine for JAM Packer V2.X/3.0/4.0

DEPACK:
		ADDA.L	#$4,A0	;ALLOW FOR HEADER
		MOVE.W	#$FE,D7
		MOVE.L	A0,A6
		MOVE.L	(A0)+,D5
		ADDA.L	D5,A1
		ADDA.L	(A0),A0
		SUBA.L	#4,A0
		TST.W	-(A0)
		BPL.S	SKIP
		SUBQ.L	#1,A0
SKIP		MOVE.B	-(A0),D0

TOP		ROL.W	D7
		MOVE.W	D7,$FFFF8240.W
		LSL.B	#1,D0
		BNE.S	NOTE
		MOVE.B	-(A0),D0
		ROXL.B	#1,D0
NOTE		BCC.S	SHEP
		CLR.W	D1
		LSL.B	#1,D0
		BNE.S	KLF
		MOVE.B	-(A0),D0
		ROXL.B	#1,D0
KLF		BCC.S	PET
		LEA	TABL1(PC),A3
		MOVEQ	#3,D3
STE		CLR.W	D1
		MOVE.B	0(A3,D3.W),D2
		EXT.W	D2
		MOVEQ	#-1,D4
		LSL.W	D2,D4
		NOT.W	D4
		SUBQ.W	#1,D2
ARJ		LSL.B	#1,D0
		BNE.S	BOYS
		MOVE.B	-(A0),D0
		ROXL.B	#1,D0
BOYS		ROXL.W	#1,D1
		DBF	D2,ARJ
		TST.W	D3
		BEQ.S	SHOP
		CMP.W	D1,D4
		DBNE	D3,STE
SHOP		MOVE.B	4(A3,D3.W),D2
		EXT.W	D2
		ADD.W	D2,D1
PET		MOVE.B	-(A0),-(A1)
		DBF	D1,PET
SHEP		MOVE.L	A6,A3
		ADDQ.L	#$8,A3
		CMPA.L	A3,A0
		BLE	EXIT
		LEA	TABL2(PC),A3
		MOVEQ	#3,D2
CATHY		LSL.B	#1,D0
		BNE.S	ALSA
		MOVE.B	-(A0),D0
		ROXL.B	#1,D0
ALSA		BCC.S	FDC
		DBF	D2,CATHY
FDC		CLR.W	D1
		ADDQ.W	#1,D2
		MOVE.B	0(A3,D2.W),D3
		BEQ.S	VAN
		EXT.W	D3
		SUBQ.W	#1,D3
DE		LSL.B	#1,D0
		BNE.S	VE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