; MEGA DEPACK v1.91 coded by Mike Watson using DEVPAC 2
; This version finished 26/06/92
; Watch out for my game, coming soon!
; My tabs are set to 10, are yours ?

	opt	O+
begin

; Medium or hi res only folks!

	move.w	#4,-(a7)
	trap	#$E
	addq.w	#2,a7

	cmp.b	#0,d0
	bne.s	.1
	lea	wrong_res(pc),a1
	bsr	print_a1
	bsr	read_key
	clr.l	-(a7)
	trap	#1

; clear out BSS

.1
	lea	bss_start(pc),a1
	move.l	#load_address-bss_start,d0
copy	clr.b	(a1)+
	dbf	d0,copy

	move.l	4(a7),a5		; pointer to basepage
	move.l	a7,old_stack

	move.l	4(a5),d0		; get end of free memory
	sub.l	#load_address,d0
	move.l	d0,unpacked_length	; we want to zap this ram

	lea	my_stack(pc),a7

	bsr	memory_alloc	; allocate ram
	bsr	save_10k

	bsr	hide_cursor	; kill mouse

	bsr	zap_ram		; clear free memory

; what version of the AES have we got then?

	lea	params(PC),a0
	move.l	a0,d1
	move.w	#$C8,d0
	trap	#2

	move.w	ctrl1,d1

	cmp.w	#$140,d1
	bcs.s	l		; only AES between 140 & 200
	cmp.w	#$200,d1		; are capable of showing a
	bcc.s	l		; file selector title

	move.b	#$5b,function	; if valid aes then change f_select
				; parameters

l	move.l	#ctrl0,params	; set up parameter list

loop	lea	fil_extend(PC),a3
	move.l	a3,save_extend
	move.l	#load,title	; we want this as out title
	bsr	file_select
	tst.w	d0
	bne	exit		; cancel pressed ?

	bsr	struct_file	; build proper filename
	bsr	read_file		; load it

	tst.w	rw_error		; was there an error
	beq.s	.dp		; no

; print error message on the screen

	lea	error_mess(pc),a1
	bsr	print_a1
	lea	a_k_t_c(pc),a1
	bsr	print_a1
	bsr	read_key
	bra.s	loop

.dp	bsr	depack		; unpack the file

	move.l	not_packed(pc),d0	; did the file unpack
	beq.s	.1		; yes

; wait for a key, set unpacked length to length read for memory clearing
; purposes.

	move.l	length_read(pc),unpacked_length
	lea	a_k_t_c(pc),a1
	bsr	print_a1
	bsr	read_key
	bra.s	.2

; save the file

.1	lea	fil_extend(PC),a3
	move.l	a3,save_extend
	move.l	#save,title
	bsr	file_select
	tst.w	d0
	bne.s	.2		; cancel pressed ?
	bsr	struct_file	; build new filename
	bsr	write_file	; save it
	tst.w	rw_error		; was there an error
	beq.s	.2		; no

; print error message and wait for a key

	lea	error_mess(pc),a1
	bsr	print_a1
	lea	a_k_t_c(pc),a1
	bsr	print_a1
	bsr	read_key
.2	bsr	zap_ram
	bra	loop		; round again

; leave the program

exit	bsr	show_cursor
	move.l	old_stack(pc),a7
	clr.w	-(a7)
	trap	#1

; memory allocation

memory_alloc
	move.l	#$1000,d0
	add.l	$C(a5),d0
	add.l	$14(a5),d0
	add.l	$1C(a5),d0
	move.l	d0,-(a7)
	pea	(a5)
	clr.w	-(a7)
	move.w	#$4A,-(a7)
	trap	#1
	lea	$C(a7),a7
	rts

save_10k	moveq	#-1,d0
	bsr.s	.1
	subi.l	#$2800,d0
	bmi.s	out_of_mem
.1	move.l	d0,-(a7)
	move.w	#$48,-(a7)
	trap	#1
	addq.w	#6,a7
	rts

out_of_mem
	lea	out_mem(PC),a1
	bsr	print_a1
	bsr	read_key
	bsr	show_cursor
	clr.w	-(a7)
	trap	#1

; call file selector

file_select
	movem.l	a0-a2/D1-d3,-(a7)
	bsr	show_cursor	; mouse must be shown
	bsr.s	get_dir		; get current directory

	lea	adr0(PC),a2
	tst.b	(a2)+
	beq.s	.2
.1	tst.b	(a2)+
	bne.s	.1
.2	move.b	#$5C,-1(a2)
.3	move.b	(a3)+,(a2)+
	bne.s	.3

	lea	params(PC),a0	; parameters for f_select
	move.l	a0,d1
	move.w	#$C8,d0
	trap	#2		; show it

	bsr	hide_cursor
	tst.w	flag
	beq.s	.4
	bsr.s	set_dir
	moveq	#0,d0
	bra.s	.5
.4	moveq	#-1,d0
.5	movem.l	(a7)+,a0-a2/D1-d3
	rts

; set directory

set_dir	lea	adr0(PC),a1
	moveq	#0,d0
	move.b	(a1)+,d0
	pea	(a1)
	subi.w	#$41,d0
	move.w	d0,-(a7)
	move.w	#$E,-(a7)
	trap	#1
	addq.w	#4,a7
	move.l	(a7)+,a1
.1	move.b	(a1)+,d0
	beq.s	.2
	cmp.b	#$2A,d0
	bne.s	.1
	clr.b	-(a1)
.2	pea	adr0(PC)
	move.w	#$3B,-(a7)
	trap	#1
	addq.w	#6,a7
	rts

; get directory

get_dir	movem.l	a0-a2,-(a7)
	lea	adr0(PC),a1
	move.l	a1,a2
	moveq	#$40,d0
.1	clr.b	(a1)+
	dbf	d0,.1
	move.w	#$19,-(a7)
	trap	#1
	addq.w	#2,a7
	add.w	#$41,d0
	move.b	d0,(a2)+
	move.b	#$3A,(a2)+
	clr.w	-(a7)
	pea	(a2)
	move.w	#$47,-(a7)
	trap	#1
	addq.w	#8,a7
	movem.l	(a7)+,a0-a2
	rts

; merge pathname and filename

struct_file
	lea	filename(PC),a0
	lea	adr0(PC),a1
	bsr.s	get_dir
	move.b	(a1)+,(a0)+
	move.b	(a1)+,(a0)+
	tst.b	(a1)
	beq.s	.1
.2	move.b	(a1)+,(a0)+
	bne.s	.2
	subq.w	#1,a0
.1	move.b	#$5C,(a0)+
	lea	adr1(PC),a1
.3	move.b	(a1)+,(a0)+
	bne.s	.3
	rts

; load file

read_file
	lea	loading(pc),a1	; display 'loading'
	bsr	print_a1

	sf	rw_error
	bsr	show_cursor
	move.w	#2,-(a7)
	pea	filename(PC)
	move.w	#$3D,-(a7)
	trap	#1
	addq.w	#8,a7
	tst.w	d0
	bmi.s	.3
	move.w	d0,handle
	pea	load_address(pc)
	moveq	#8,d2
	swap	d2
	move.l	d2,-(a7)
	move.w	d0,-(a7)
	move.w	#$3F,-(a7)
	trap	#1
	lea	$C(a7),a7
	tst.l	d0
	bmi.s	.1
	move.l	d0,length_read
	bra.s	.2
.1	st	rw_error
.2	bra.s	close
	bsr	hide_cursor
	rts
.3	st	rw_error
	rts

; save file

write_file
	lea	saving(pc),a1	; display 'saving'
	bsr	print_a1
	bsr.s	show_cursor
	sf	rw_error
	clr.w	-(a7)
	pea	filename(PC)
	move.w	#$3C,-(a7)
	trap	#1
	addq.w	#8,a7
	tst.w	d0
	bpl.s	.1
	bsr.s	hide_cursor
	moveq	#-1,d0
	bra.s	create_error
.1	move.w	d0,handle
	lea	save_address(pc),a0
	move.l	(a0),a0
	pea	(a0)
	move.l	unpacked_length(pc),-(a7)
	move.w	d0,-(a7)
	move.w	#$40,-(a7)
	trap	#1
	lea	$C(a7),a7
	tst.l	d0
	bpl.s	close
	st	rw_error
close	bsr.s	hide_cursor
	move.w	handle(PC),-(a7)
	move.w	#$3E,-(a7)
	trap	#1
	addq.w	#4,a7
create_error
	tst.w	d0
	bpl.s	.1
	st	rw_error
.1	rts

show_cursor
	movem.l	a0-a2/D0-d2,-(a7)
	dc.w	$A009
	movem.l	(a7)+,a0-a2/D0-d2
	rts

hide_cursor
	movem.l	a0-a2/D0-d2,-(a7)
	dc.w	$A00A
	movem.l	(a7)+,a0-a2/D0-d2
	rts

depack_message
	lea	dpm(pc),a1
	bsr.s	print_a1
	rts

print_a1	movem.l	d0-a6,-(a7)
	pea	(a1)
	move.w	#9,-(a7)
	trap	#1
	addq.w	#6,a7
	movem.l	(a7)+,d0-a6
	rts

read_key	move.w	#7,-(a7)
	trap	#1
	addq.w	#2,a7
	rts

zap_ram	lea	load_address(pc),a0
	move.l	a0,a1
	add.l	unpacked_length(pc),a1
	movem.l	blank(pc),d0-7
.clear	movem.l	d0-7,(a0)
	lea	32(a0),a0
	cmp.l	a0,a1
	bgt.s	.clear
	rts

; Decide how the file was packed

depack	clr.l	not_packed
	lea	load_address(pc),a0
	move.l	a0,save_address

; ATOM packer options

	cmp.l	#'ATOM',(a0)	; is it ATOM v3.3 packed data ?
	beq	a33_DAT

	cmp.l	#'ATOM',538(a0)	; ATOM v3.3 packed prog ?
	beq	a33_prg

	cmp.l	#'ATM5',(a0)	; is it ATOM v3.5 packed data ?
	beq	a35_DAT

	cmp.l	#'3.5 ',634(a0)	; ATOM v3.5 packed prog ?
	beq	a35_prg

; FIRE packer options

	cmp.l	#'FIRE',(a0)	; is it FIRE packed data
	beq	fir_DAT

	cmp.l	#'FIRE',560(a0)	; is it a FIRE packed prog
	beq	fir_prog

; ICE packer options

	cmp.l	#'Ice!',(a0)	; is it pre 2.3 dat?
	beq	pre_23_dat

	cmp.w	#$2e3c,54(a0)	; is it pre 2.3 prog?
	beq	pre_23_prg

	cmp.l	#'ICE!',(a0)	; is it ICE 2.4 packed data ?
	beq	I24_3_DAT

	cmp.l	#'ICE!',442(a0)	; is it ICE 2.4 prog ?
	beq	I24_prg

	cmp.l	#'ICE!',448(a0)	; is it ICE 2.3 prog ?
	beq	I23_prg

; All the JAM packed permutations coming up folks

	cmp.l	#'JAM ',672(a0)	; is it a JAM v1 progfile ?
	beq	JAM1_prg

	cmp.l	#'LSD!',758(a0)	; is it v2 & JAM4 progfile ?
	beq	v2JAM4

	cmp.l	#'LZH!',1170(a0)	; is it LZH & JAM3 progfile ?
	beq	LZHJAM3

	cmp.l	#'LZH!',1194(a0)	; is it LZH & JAM4 progfile ?
	beq	LZHJAM4

	cmp.l	#'LZW!',544(a0)	; is it LZW & JAM4 progfile ?
	beq	LZWJAM4

	cmp.l	#'LSD!',1170(a0)	; is it v2 & JAM3 progfile ?
	beq	v2JAM3

	cmp.l	#'Ice!',712(a0)	; is it Ice & JAM4 progfile
	beq	ICEJAM4

	cmp.l	#'LZH!',(a0)	; is it an LZH datafile
	beq	LZH_DAT_dep

	cmp.l	#'LZW!',(a0)	; is it an LZW datafile
	beq	LZW_DAT

; Automation packed things

	cmp.l	#$2e337200,482(a0)	; is it v2.3r prog file
	beq	aut_23r_prg

	cmp.l	#$2e353100,482(a0)	; is it v2.51 prog file
	beq	aut_251_prg

	cmp.l	#'LSD!',(a0)	; is it an automation data file
	beq	aut_DAT
	cmp.l	#'LSD$',(a0)	; is it an automation data file
	beq	aut_DAT
	cmp.l	#'AUTM',(a0)	; is it an automation data file
	beq	aut_DAT

	cmp.l	#'LSDC',(a0)	; is it a chunk packed file
	beq	aut_chunky

	cmp.l	#'AU5!',836(a0)	; is it v5.01 prog file
	beq	aut_501_prg

	cmp.l	#'AU5!',(a0)	; is it v5.01 data file
	beq	aut_501_dat

; LSD 1.2 prog file

	cmp.l	#'PLEA',638(a0)	; is it an LSD 1.2 prog file
	beq	LSD_12_prg

; JEK 1.2d prog file

	cmp.l	#'JEK ',638(a0)	; is it a JEK 1.2d prog file
	beq	JEK_12d_prg

; bytekiller prog file

	cmp.l	#$7fd00,72(a0)	; is it a bytekiller prog
	beq	bytekiller_prg

; gollum prog file with huffmans encoding

	cmp.l	#$22572c7a,192(a0)	; is it gollum with huff
	beq	gollum_prg_huff

; gollum prog file

	cmp.l	#$42401018,192(a0)	; is it gollum with
	beq	gollum_prg

; DCSquish prog file

	cmp.l	#'Squi',40(a0)	; is it DCSquish
	beq	DCSquish_prg

; 4pak prog file

	cmp.w	#$4ef9,60(a0)	; is it 4pak
	beq	_4pak_prg

; happy prog file

	cmp.l	#'EASY',52(a0)	; is it a happy prog
	beq	happy_prg

; check for files with header at end.

	move.l	a0,a1
	add.l	length_read(pc),a1
	move.l	a1,d0
	and.w	#$fffe,d0
	move.l	d0,a1
	addq	#4,a1
	lea	-20(a1),a2
.loop	cmp.l	a2,a1
	blt.s	.go_on
	cmp.l	#'END!',(a1)
	beq	LSD_dat_dep
	cmp.l	#'JEK!',(a1)
	beq	JEK_dat_dep
	cmp.l	#'POPI',(a1)
	beq	pp_test
	cmp.l	#'PUFF',(a1)
	beq	pp_test
	subq	#2,a1
	bra.s	.loop


.go_on	
; spectrum 512 packed file

	cmp.w	#'SP',(a0)
	beq	spec512

; degas PC1 -3

	cmp.w	#$8000,(a0)
	beq	degas
	cmp.w	#$8001,(a0)
	beq	degas
	cmp.w	#$8002,(a0)
	beq	degas

; Check for unpacked prog file
	cmp.w	#$601a,(a0)	; is it a prog file
	bne.s	.DAT_file
	move.l	a0,a1
	add.l	2(a0),a1
	add.l	6(a0),a1
	lea	$1c(a1),a1
	add.l	length_read(pc),a0
	move.l	a0,d0
	and.w	#$fffe,d0
	move.l	d0,a0
	clr.l	(a0)
	clr.l	2(a0)
	moveq	#0,d0
	move.w	4(a1),d0		; is there a relocation table
	bne.s	.probably_not_packed	; this is not foolproof

	lea	prog_file_mp(pc),a1
	bsr	print_a1
	bra.s	.exit

.probably_not_packed
	lea	prog_file_np(pc),a1
	bsr	print_a1
	bra.s	.exit

; check for unpacked dat file

.DAT_file	lea	DAT_file(pc),a1
	bsr	print_a1
	moveq	#3,d1
	lea	header(pc),a1

.loop1	move.b	(a0)+,d0		; get header
	cmp.b	#' ',d0		; we want to exit if
	blt.s	.not_ascii	; not ascii
	cmp.b	#'z',d0
	bgt.s	.not_ascii
	move.b	d0,(a1)+
	dbf	d1,.loop1

	moveq	#3,d1
.loop2	move.b	(a0)+,d0		; get next 4 bytes
	cmp.b	#' ',d0		; we want to exit
	blt.s	.ascii		; if not ascii
	cmp.b	#'z',d0
	bgt.s	.ascii
	move.b	d0,(a1)+
	dbf	d1,.loop2

	bra.s	.not_ascii

.ascii	lea	ascii_header(pc),a1
	bsr	print_a1
	bra.s	.exit

.not_ascii
	lea	not_ascii(pc),a1
	bsr	print_a1

.exit	move.l	#-1,not_packed
	rts

; depack ATOM v3.5 packed prog file

a35_prg	lea	ATOM_v3.5_packed_prog(PC),a1
	bsr	print_a1
	bsr	depack_message

	move.w	#$4e75,86(a0)	; modify depack code

	move.l	656(a0),unpacked_length	; get length
	movem.l	d0-a5,-(a7)	; jump into its depacker
	jsr	76(a0)
	move.l	a0,save_address	; get address
	movem.l	(a7)+,d0-a5

	rts

; Depack ATOM v3.3 packed program file

a33_prg	lea	564(a0),a0	; file starts here
	move.l	#'ATOM',(a0)	; build pack file header
	move.l	a0,save_address	; new address to save from
	lea	ATOM_v3.3_packed_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	ATOM_depack_v3.3
	rts

; Depack ATOM v3.3 packed data

a33_DAT	lea	ATOM_v3.3_packed(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	ATOM_depack_v3.3
	rts

; Depack ATOM v3.5 packed data

a35_DAT	lea	ATOM_v3.5_packed(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	ATOM_depack_v3.5
	rts

; Depack FIRE packed prog

fir_prog	lea	560(a0),a0	; file starts here
	move.l	a0,save_address	; new save address
	lea	FIRE_packed_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	FIRE_depack
	rts

; depack FIRE packed data

fir_DAT	lea	FIRE_packed(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	FIRE_depack
	rts

; Depack pre ICE 2.3 data

pre_23_dat
	lea	olde_ICE_packed_dat(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	olde_ice_depack
	move.l	d0,unpacked_length
	rts

; Depack pre 2.3 ICE packed prog

pre_23_prg
	lea	olde_ICE_packed_prog(PC),a1
	bsr	print_a1
	bsr	depack_message

	move.w	#$4e75,64(a0)	; modify depack code
	move.l	56(a0),unpacked_length	; get length from code

	movem.l	d0-a5,-(a7)	; jump into its depacker
	jsr	(a0)
	movem.l	(a7)+,d0-a5

	move.l	a6,save_address	; a6 returns pack address
	rts

; Depack ICE 2.4 packed prog

I24_prg	lea	442(a0),a0	; packed file starts here
	lea	ICE_24_packed_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.l	a0,a5
	move.l	a0,a6
	add.l	4(a0),a5
	add.l	8(a0),a6
	lea	120(a6),a6
	lea	120(a0),a4
	move.l	8(a0),unpacked_length
	bsr	ICE_24_prog_depack
	move.l	a4,save_address
	rts

; Depack ICE 2.4 packed prog

I23_prg	lea	448(a0),a0	; packed file starts here
	lea	ICE_23_packed_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.l	a0,a5
	move.l	a0,a6
	add.l	4(a0),a5
	add.l	8(a0),a6
	lea	120(a6),a6
	lea	120(a0),a4
	move.l	8(a0),unpacked_length
	bsr	ICE_23_prog_depack
	move.l	a4,save_address
	rts

; Depack ICE 2.4/3 data

I24_3_DAT	lea	ICE_packed(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	ICE_depack_23_24
	rts

; unpack JAMpack v1 prog file

JAM1_prg	lea	JAM_1_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bra	go_on

; unpack JAMpack 3 using v2 compression prog file

v2JAM3	lea	JAM3_v2_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.w	#$4e75,166(a0)
	jsr	102(a0)
	move.l	a3,save_address
	move.l	a6,unpacked_length
	rts

; unpack JAMpack 3 using LZH compression prog file

LZHJAM3	lea	JAM3_LZH_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.w	#$4e75,166(a0)	; modify depack code
	jsr	102(a0)
	move.l	a3,save_address
	move.l	a6,unpacked_length
	rts

; unpack JAMpack 4 using v2 compression prog file

v2JAM4	lea	JAM4_v2_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.w	#$4e75,166(a0)	; modify depack code
	jsr	106(a0)
	move.l	a3,save_address
	move.l	a6,unpacked_length
	rts

; unpack JAMpack 4 using LZH compression prog file

LZHJAM4	lea	JAM4_LZH_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.w	#$4e75,166(a0)	; modify depack code
	jsr	106(a0)
	move.l	a3,save_address
	move.l	a6,unpacked_length
	rts

; unpack JAMpack 4 using LZW compression prog file

LZWJAM4	lea	JAM4_LZW_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.w	#$4e75,166(a0)	; modify depack code
	jsr	106(a0)
	move.l	a3,save_address
	move.l	a6,unpacked_length
	rts

; depack JAMpack 4 using ICE compression prog file

ICEJAM4	lea	JAM4_ICE_prog(pc),a1
	bsr	print_a1
	bsr	depack_message
	move.l	720(a0),unpacked_length
	move.w	#$4e75,166(a0)	; modify depack code
	jsr	106(a0)
	move.l	a3,save_address
	rts

; depack LZH packed datafile

LZH_DAT_dep
	lea	LZH_DAT(pc),a1
	bsr	print_a1
	bsr	depack_message
	move.l	4(a0),unpacked_length
	lea	$400(a0),a1
	move.l	a1,save_address
	bsr	UNPACK
	rts

; depack LZW packed datafile

LZW_DAT_dep
	lea	LZW_DAT(pc),a1
	bsr	print_a1
	bsr	depack_message
	move.l	4(a0),unpacked_length
	bsr	dp_LZW
	move.l	a1,save_address
	rts

; unpack automation dat file

aut_DAT	lea	auto_DAT_file,a1
	bsr	print_a1
	bsr	depack_message
	bra.s	dp_DAT

; unpack automation prog file

aut_23r_prg
	lea	auto_23r_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bra.s	dpboth

buf_size	equ	$c0

aut_251_prg
	lea	auto_251_prog(PC),a1
	bsr	print_a1
	bsr	depack_message

dpboth	lea	822(a0),a0	; address of packed data
dp_DAT	move.l	a0,save_address
	move.l	4(a0),unpacked_length
	move.l	a0,a1
	lea	-buf_size(a1),a0
	move.l	a0,a2
	move.l	a0,a4
	move.l	a1,a3
	lea	buf_adr,a5
	move.w	#buf_size-1,d0
.1	move.b	(a4)+,(a5)+
	dbf	d0,.1
	move.l	8(a3),d0
	moveq	#12,d1
	add.l	d1,d0
.2	move.b	(a3)+,(a2)+
	subq.l	#1,d0
	bne.s	.2
	move.l	a0,-(a7)
	bsr	depack_aut_23r_251
	move.l	(a7)+,a0
	lea	buf_adr,a1
	move.w	#buf_size-1,d0
.3	move.b	(a1)+,(a0)+
	dbf	d0,.3

	rts

; unpack automation chunk packed file

aut_chunky
	lea	auto_chunky(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.l	a0,a6
	add.l	length_read(pc),a0
	move.l	a0,d0		; set up buffer address
	and.w	#$fffe,d0
	addq.l	#8,d0
	move.l	d0,a1
	move.l	a1,save_address
	move.l	4(a6),unpacked_length
	lea	12(a6),a6		; start of offsets
.again	lea	load_address(pc),a0	; start of file
	move.l	(a6)+,d0		; next offset
	cmp.l	#'LSD!',d0	; finished ?
	beq.s	.fin
	add.l	d0,a0		; get next chunk
	movem.l	a0-a6,-(a7)
	bsr	depack_aut_23r_251
	movem.l	(a7)+,a0-a6
	add.l	4(a0),a1		; depack to here next time
	bra.s	.again
.fin	rts

; Depack automation v5.01 packed program file

aut_501_prg
	lea	836(a0),a0	; file starts here
	move.l	a0,save_address	; new address to save from
	lea	auto_501_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	aut501_depack
	rts

; Depack automation v5.01 data

aut_501_dat
	lea	auto_501_dat(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	aut501_depack	; with a gem shell on the packer
	rts

; depack LSD packed datafile

LSD_dat_dep
	move.l	a1,-(a7)
	lea	LSD_dat(pc),a1
	bsr	print_a1
	bsr	depack_message
	move.l	(a7)+,a1
	move.l	-4(a1),unpacked_length
	bra.s	datafil

; depack JEK packed datafile

JEK_dat_dep
	move.l	a1,-(a7)
	lea	JEK_dat(pc),a1
	bsr	print_a1
	bsr	depack_message
	move.l	(a7)+,a1
	move.l	-4(a1),unpacked_length
	bra.s	datafil

; depack LSD & JEK packed program

LSD_12_prg
	lea	LSD_12_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bra.s	go_on
JEK_12d_prg
	lea	JEK_12d_prog(PC),a1
	bsr	print_a1
	bsr	depack_message

go_on	move.l	a0,a1
	add.l	length_read(pc),a1
	move.l	a1,d0
	and.w	#$fffe,d0
	move.l	d0,a1
	addq	#4,a1
.again	cmp.w	#0,(a1)
	bne.s	.at_end
	subq	#2,a1
	bra.s	.again
.at_end	move.l	-2(a1),unpacked_length
	lea	2(a1),a1
datafil	move.l	a1,store_end
	lea	926(a0),a1
	move.l	a1,save_add
	move.l	a1,save_address
	bsr	LSD_depack
	rts

	rts

; is it a pompey prog or dat file

pp_test	cmp.w	#$601a,(a0)
	bne.s	.pp_dat

; depack pp prog file

	move.l	a1,-(a7)
	lea	pp_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.l	(a7)+,a1
	move.l	a0,a2
	lea	40(a2),a2		; get start of packed data
	add.l	(a2),a0
	lea	$1c(a0),a0	; add prog header
	move.l	a0,save_address
	lea	4(a1),a1		; end of file
	move.l	a0,a2		; juggle the registers!
	move.l	a1,a0
	bsr	pp_depack
	rts

; depack pp dat file

.pp_dat	move.l	a1,-(a7)
	lea	pp_dat(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.l	a0,save_address
	move.l	(a7)+,a1
	lea	4(a1),a1
	move.l	a0,a2
	move.l	a1,a0
	bsr	pp_depack
	rts

; Depack DCSquish packed program file

DCSquish_prg
	lea	DCS_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	move.l	30(a0),unpacked_length
	lea	900(a0),a3
	move.l	a3,a5
	add.l	length_read(pc),a5
	move.l	a5,save_address
	bsr	DCsquish_unpack
	rts

; Depack 4pak packed program file

_4pak_prg
	lea	_4pak_prog(PC),a1
	bsr	print_a1
	bsr	depack_message

	move.l	a0,a3
	lea	$1c(a3),a0
	lea	$1c(a3),a1
	add.l	30(a3),a0
	add.l	36(a3),a1
	move.w	46(a3),d1
	move.w	58(a3),.wibble
	moveq	#0,d0
.1	move.l	-(a0),-(a1)
	dbf	d1,.1
	dbf	d0,.1
.wibble	nop
	move.l	62(a3),a4
	lea	$1c(a3),a0
	add.l	a0,a4
	move.l	64(a4),2(a3)
	move.l	78(a4),6(a3)
	move.l	92(a4),10(a3)
	bsr	_4pak_depack
	lea	$1c(a3),a2
	add.l	2(a3),a2
	add.l	6(a3),a2

	move.b	(a2)+,d0
	lsl.w	#8,d0
	move.b	(a2)+,d0
	swap	d0
	move.b	(a2)+,d0
	lsl.w	#8,d0
	move.b	(a2)+,d0
	moveq	#1,d2
	tst.l	d0
	beq.s	.2
	moveq	#0,d0
.3	move.b	(a2)+,d0
	cmp.b	d2,d0
	bhi.s	.3
	beq.s	.3
.2	sub.l	a3,a2		; unpacked length
	move.l	a2,unpacked_length
	rts

; Depack bytekiller packed program file

bytekiller_prg
	lea	426(a0),a6
	move.l	4(a6),unpacked_length
	lea	36(a6),a5
	move.l	a5,save_address
	lea	bkill_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	bytekiller_depack
	rts

; Depack gollum packed program file with huffing

gollum_prg_huff
	lea	gollum_prog_huff(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	gollum_depacker_huff
	rts

; Depack gollum packed program file

gollum_prg
	lea	gollum_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	gollum_depacker
	move.l	d2,save_address
	rts

; Depack happy packed program file

happy_prg
	lea	happy_prog(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	happy_depacker
	rts

; Depack spectrum 512 picture file

spec512	lea	spec512_pic(PC),a1
	bsr	print_a1
	bsr	depack_message
	bsr	spec_depack
	rts

; Depack DEGAS file

degas	lea	degas_pic(pc),a1
	bsr	print_a1
	bsr	depack_message
	bsr	degas_depack
	rts


***********************************************************************
* Depack code follows                                                 *
***********************************************************************

; Depack ATOM v3.5

ATOM_depack_v3.5
	move.l	4(a0),unpacked_length
	movem.l	d1-a6,-(a7)
	cmp.l	#$41544D35,(a0)
	bne	.exit
	lea	4(a0),a1
	move.l	4(a1),a2
	lea	8(a1,a2.L),a2
	move.l	a0,a3
.1	move.l	(a1)+,(a3)+
	move.l	(a1)+,(a3)+
	cmp.l	a1,a2
	bgt.s	.1
	link	a2,#-$1C
	move.l	(a0)+,d0
	lea	4(a0,d0.L),a5
	move.l	d0,-(a7)
	move.l	a5,a4
	lea	$1000(a4),a5
	subq.w	#8,a4
	move.l	(a0)+,d0
	move.l	a0,a6
	add.l	d0,a6
	clr.w	-2(a2)
	move.b	.AT277(PC),d0
	bne.s	.AT145
	moveq	#0,d0
	move.b	-(a6),d0
	move.w	d0,-2(a2)
	lsl.w	#2,d0
	suba.w	d0,a4
.AT145	sf	.AT277
	pea	(a4)
	pea	(a5)
	move.b	-(a6),d7
	bra	.AT166
.AT146	move.w	d3,d5
.AT147	add.b	d7,d7
.AT148	dbcs	d5,.AT147
	beq.s	.AT14B
	bcc.s	.AT149
	sub.w	d3,d5
	neg.w	d5
	bra.s	.AT14E
.AT149	moveq	#3,d6
	bsr.s	.AT154
	beq.s	.AT14A
	bra.s	.AT14D
.AT14A	moveq	#7,d6
	bsr.s	.AT154
	beq.s	.AT14C
	add.w	#$F,d5
	bra.s	.AT14D
.AT14B	move.b	-(a6),d7
	addx.b	d7,d7
	bra.s	.AT148
.AT14C	moveq	#$D,d6
	bsr.s	.AT154
	add.w	#$10E,d5
.AT14D	add.w	d3,d5
.AT14E	lea	.AT170(PC),a4
	move.w	d5,d2
	bne.s	.AT158
	add.b	d7,d7
	bne.s	.AT14F
	move.b	-(a6),d7
	addx.b	d7,d7
.AT14F	bcs.s	.AT150
	moveq	#1,d6
	bra.s	.AT159
.AT150	moveq	#3,d6
	bsr.s	.AT154
	tst.b	-$1C(a2)
	beq.s	.AT151
	move.b	-$12(a2,d5.W),-(a5)
	bra	.AT164
.AT151	move.b	(a5),d0
	btst	#3,d5
	bne.s	.AT152
	bra.s	.AT153
.AT152	add.b	#-$10,d5
.AT153	sub.b	d5,d0
	move.b	d0,-(a5)
	bra	.AT164
.AT154	clr.w	d5
.AT155	add.b	d7,d7
	beq.s	.AT157
.AT156	addx.w	d5,d5
	dbf	d6,.AT155
	tst.w	d5
	rts
.AT157	move.b	-(a6),d7
	addx.b	d7,d7
	bra.s	.AT156
.AT158	moveq	#2,d6
.AT159	bsr.s	.AT154
	move.w	d5,d4
	move.b	$E(a4,d4.W),d6
	ext.w	d6
	tst.b	-$1B(a2)
	bne.s	.AT15A
	addq.w	#4,d6
	bra.s	.AT15E
.AT15A	bsr.s	.AT154
	move.w	d5,d1
	lsl.w	#4,d1
	moveq	#2,d6
	bsr.s	.AT154
	cmp.b	#7,d5
	blt.s	.AT15C
	moveq	#0,d6
	bsr.s	.AT154
	beq.s	.AT15B
	moveq	#2,d6
	bsr.s	.AT154
	add.w	d5,d5
	or.w	d1,d5
	bra.s	.AT15F
.AT15B	or.b	-$1A(a2),d1
	bra.s	.AT15D
.AT15C	or.b	-$19(a2,d5.W),d1
.AT15D	move.w	d1,d5
	bra.s	.AT15F
.AT15E	bsr.s	.AT154
.AT15F	add.w	d4,d4
	beq.s	.AT160
	add.w	-2(a4,d4.W),d5
.AT160	lea	1(a5,d5.W),a4
	move.b	-(a4),-(a5)
.AT161	move.b	-(a4),-(a5)
	dbf	d2,.AT161
	bra.s	.AT164
.AT162	add.b	d7,d7
	bne.s	.AT163
	move.b	-(a6),d7
	addx.b	d7,d7
.AT163	bcs.s	.AT16E
	move.b	-(a6),-(a5)
.AT164	cmp.l	a5,a3
.AT165	bne.s	.AT162
	cmp.l	a6,a0
	beq.s	.AT16F
.AT166	moveq	#0,d6
	bsr	.AT154
	beq.s	.AT169
	move.b	-(a6),d0
	lea	-$1A(a2),a1
	move.b	d0,(a1)+
	moveq	#1,d1
	moveq	#6,d2
.AT167	cmp.b	d0,d1
	bne.s	.AT168
	addq.w	#2,d1
.AT168	move.b	d1,(a1)+
	addq.w	#2,d1
	dbf	d2,.AT167
	st	-$1B(a2)
	bra.s	.AT16A
.AT169	sf	-$1B(a2)
.AT16A	moveq	#0,d6
	bsr	.AT154
	beq.s	.AT16C
	lea	-$12(a2),a1
	moveq	#$F,d0
.AT16B	move.b	-(a6),(a1)+
	dbf	d0,.AT16B
	st	-$1C(a2)
	bra.s	.AT16D
.AT16C	sf	-$1C(a2)
.AT16D	clr.w	d3
	move.b	-(a6),d3
	move.b	-(a6),d0
	lsl.w	#8,d0
	move.b	-(a6),d0
	move.l	a5,a3
	suba.w	d0,a3
	bra.s	.AT162
.AT16E	bra	.AT146
.AT16F	move.l	(a7)+,a0
	pea	(a2)
	bsr.s	.AT171
	move.l	(a7)+,a2
	move.l	(a7)+,d1
	move.l	(a7)+,d0
	bsr.s	.AT177
	unlk	a2
	move.l	d1,d0
.exit	movem.l	(a7)+,d1-a6
	rts
.AT170	dc.b	$00,' ',$00,'`',$01,'`',$03,'`'
	dc.b	$07,'`',$0F,'`',$1F,'`',$00,$01
	dc.b	$03,$04,$05,$06,$07,$08
.AT171	move.w	-2(a2),d7
.AT172	dbf	d7,.AT173
	rts
.AT173	move.l	-(a0),d0
	lea	0(a5,d0.L),a1
	lea	$7d00(a1),a2
.AT174	moveq	#3,d6
.AT175	move.w	(a1)+,d0
	moveq	#3,d5
.AT176	add.w	d0,d0
	addx.w	d1,d1
	add.w	d0,d0
	addx.w	d2,d2
	add.w	d0,d0
	addx.w	d3,d3
	add.w	d0,d0
	addx.w	d4,d4
	dbf	d5,.AT176
	dbf	d6,.AT175
	movem.w	d1-d4,-8(a1)
	cmp.l	a1,a2
	bne.s	.AT174
	bra.s	.AT172
.AT177	lsr.l	#4,d0
	subq.w	#8,a6
	sub.l	a6,d1
.AT178	move.l	(a5)+,(a6)+
	move.l	(a5)+,(a6)+
	move.l	(a5)+,(a6)+
	move.l	(a5)+,(a6)+
	dbf	d0,.AT178
	rts
.AT277	dc.b	$00
	even

; Depack ATOM v3.3

DEC_MARGE	equ	16
RESTORE_M	equ	1
PIC_ALGO	equ	0
col_flash	equ	0

ATOM_depack_v3.3
	move.l	4(a0),unpacked_length
	movem.l	d0-a6,-(a7)
	cmp.l	#"ATOM",(a0)+
	bne	no_crunched
	move.l	(a0)+,d0
	move.l	d0,-(a7)
	lea	DEC_MARGE(a0,d0.l),a5
	ifne	RESTORE_M
	move.l	a5,a4
	lea	buff_marg(pc),a3
	moveq	#DEC_MARGE+9,d0
.save_m	move.b	-(a4),(a3)+
	dbf	d0,.save_m
	movem.l	a3-a4,-(a7)
	endc
	ifne	PIC_ALGO
	pea	(a5)
	endc
	move.l	(a0)+,d0
	lea	0(a0,d0.l),a6
	move.b	-(a6),d7
	bra	make_jnk
tablus	lea	tablus_table(pc),a4
	moveq	#1,d6
	bsr.s	get_bit2
	bra.s	tablus2
decrunch	moveq	#6,d6
take_lenght
	add.b	d7,d7
	beq.s	.empty1
.cont_copy
	dbcc	d6,take_lenght
	bcs.s	.next_cod
	moveq	#6,d5
	sub	d6,d5
	bra.s	.do_copy
.next_cod	moveq	#3,d6
	bsr.s	get_bit2
	beq.s	.next_cod1
	addq	#6,d5
	bra.s	.do_copy
.next_cod1
	moveq	#7,d6
	bsr.s	get_bit2
	beq.s	.next_cod2
	add	#15+6,d5
	bra.s	.do_copy
.empty1	move.b	-(a6),d7
	addx.b	d7,d7
	bra.s	.cont_copy
.next_cod2
	moveq	#13,d6
	bsr.s	get_bit2
	add	#255+15+6,d5
.do_copy	move	d5,-(a7)
	bne.s	bigger
	lea	decrun_table2(pc),a4
	moveq	#2,d6
	bsr.s	get_bit2
	cmp	#5,d5
	blt.s	contus
	addq	#2,a7
	subq	#6,d5
	bgt.s	tablus
	move.l	a5,a4
	blt.s	.first4
	addq	#4,a4
.first4	moveq	#1,d6
	bsr.s	get_bit2
tablus2	move.b	0(a4,d5.w),-(a5)
	bra.s	make_jnk
get_bit2	clr	d5
.get_bits	add.b	d7,d7
	beq.s	.empty
.cont	addx	d5,d5
	dbf	d6,.get_bits
	tst	d5
	rts
.empty	move.b	-(a6),d7
	addx.b	d7,d7
	bra.s	.cont
bigger	lea	decrun_table(pc),a4
cont	moveq	#2,d6
	bsr.s	get_bit2
contus	move	d5,d4
	move.b	14(a4,d4.w),d6
	ext	d6
	bsr.s	get_bit2
	add	d4,d4
	beq.s	.first
	add	-2(a4,d4.w),d5
.first	lea	1(a5,d5.w),a4
	move	(a7)+,d5
	move.b	-(a4),-(a5)
.copy_same
	move.b	-(a4),-(a5)
	dbf	d5,.copy_same
make_jnk	moveq	#11,d6
	moveq	#11,d5
take_jnk	add.b	d7,d7
	beq.s	empty
cont_jnk	dbcc	d6,take_jnk
	bcs.s	next_cod
	sub	d6,d5
	bra.s	copy_jnk1
next_cod	moveq	#7,d6
	bsr.s	get_bit2
	beq.s	.next_cod1
	addq	#8,d5
	addq	#3,d5
	bra.s	copy_jnk1
.next_cod1
	moveq	#2,d6
	bsr.s	get_bit2
	swap	d5
	moveq	#15,d6
	bsr.s	get_bit2
	addq.l	#8,d5
	addq.l	#3,d5
copy_jnk1	subq	#1,d5
	bmi.s	.end_word
	moveq	#1,d6
	swap	d6
.copy_jnk	move.b	-(a6),-(a5)

	dbf	d5,.copy_jnk

	sub.l	d6,d5
	bpl.s	.copy_jnk
.end_word	cmp.l	a6,a0
.decrunch	bne	decrunch
	cmp.b	#$80,d7
	bne.s	.decrunch
	ifne	PIC_ALGO
	move.l	(a7)+,a0
	bsr	decod_picture
	endc
	ifne	RESTORE_M
	movem.l	(a7)+,a3-a4
	endc
	move.l	(a7)+,d0
	bsr.s	copy_decrun
	ifne	RESTORE_M
	moveq	#DEC_MARGE+9,d0
.restore_m
	move.b	-(a3),(a4)+
	dbf	d0,.restore_m
	endc
no_crunched
	movem.l	(a7)+,d0-a6
	rts
empty	move.b	-(a6),d7
	addx.b	d7,d7
	bra.s	cont_jnk
decrun_table
	dc.w	32,32+64,32+64+256,32+64+256+512,32+64+256+512+1024
	dc.w	32+64+256+512+1024+2048,32+64+256+512+1024+2048+4096
	dc.b	4,5,7,8,9,10,11,12
decrun_table2
	dc.w	32,32+64,32+64+128,32+64+128+256
	dc.w	32+64+128+256+512,32+64+128+256+512*2
	dc.w	32+64+128+256+512*3
	dc.b	4,5,6,7,8,8
tablus_table
	dc.b	$60,$20,$10,$8
	ifne	PIC_ALGO
decod_picture
	move	-(a0),d7
	clr	(a0)
.next_picture
	dbf	d7,.decod_algo
	rts
.decod_algo
	move.l	-(a0),d0
	clr.l	(a0)
	lea	0(a5,d0.l),a1
	lea	$7d00(a1),a2
.next_planes	moveq	#3,d6
.next_word
	move	(a1)+,d0
	moveq	#3,d5
.next_bits
	add	d0,d0
	addx	d1,d1
	add	d0,d0
	addx	d2,d2
	add	d0,d0
	addx	d3,d3
	add	d0,d0
	addx	d4,d4
	dbf	d5,.next_bits
	dbf	d6,.next_word
	movem	d1-d4,-8(a1)
	cmp.l	a1,a2
	bne.s	.next_planes
	bra.s	.next_picture
	endc
copy_decrun
	lsr.l	#4,d0
	lea	-12(a6),a6
.copy_decrun
	rept	4
	move.l	(a5)+,(a6)+
	endr
	dbf	d0,.copy_decrun
	rts

	ifne	RESTORE_M
buff_marg	dcb.b	DEC_MARGE+10,0
	endc

; Depack FIRE

FIRE_depack
	move.l	8(a0),unpacked_length
	link	a3,#-120
	movem.l	d0-a6,-(a7)
	lea	120(a0),a4
	move.l	a4,a6
	bsr.s	.getinfo
	cmp.l	#'FIRE',d0
	bne.s	.not_packed
	bsr.s	.getinfo
	lea.l	-8(a0,d0.l),a5
	bsr.s	.getinfo
	move.l	d0,(a7)
	add.l	d0,a6
	move.l	a6,a1
	moveq	#119,d0
.save	move.b	-(a1),-(a3)
	dbf	d0,.save
	move.l	a6,a3
	move.b	-(a5),d7
	lea	.tabellen(pc),a2
	moveq	#1,d6
	swap	d6
	moveq	#0,d5
.normal_bytes
	bsr.s	.get_1_bit
	bcc.s	.test_if_end
	moveq	#0,d1
	bsr.s	.get_1_bit
	bcc.s	.copy_direkt
;       lea.l   .direkt_tab+16-.tabellen(a2),a0
	move.l	a2,a0
	moveq	#3,d3
.nextgb	move.l	-(a0),d0
	bsr.s	.get_d0_bits
	swap	d0
	cmp.w	d0,d1
	dbne	d3,.nextgb
.no_more	add.l	16(a0),d1
.copy_direkt
	move.b	-(a5),-(a6)
	dbf	d1,.copy_direkt
.test_if_end
	cmp.l	a4,a6
	bgt.s	.strings
	movem.l	(a7),d0-a2/a5
.move	move.b	(a4)+,(a0)+
	subq.l	#1,d0
	bne.s	.move
	moveq	#119,d0
.rest	move.b	-(a5),-(a3)
	dbf	d0,.rest
.not_packed
	movem.l	(a7)+,d0-a6
	unlk	a3
	rts
.getinfo
	moveq	#3,d1
.glw	rol.l	#8,d0
	move.b	(a0)+,d0
	dbf	d1,.glw
	rts
.get_1_bit
	add.b	d7,d7
	beq.s	.no_bit_found
	rts
.no_bit_found
	move.b	-(a5),d7
	addx.b	d7,d7
	rts
.get_d0_bits
	moveq	#0,d1
.hole_bit_loop
	add.b	d7,d7
	beq.s	.not_found
.on_d0	addx.w	d1,d1
	dbf	d0,.hole_bit_loop
	rts
.not_found
	move.b	-(a5),d7
	addx.b	d7,d7
	bra.s	.on_d0
.strings
	moveq	#1,d0
	bsr.s	.get_d0_bits
	subq.w	#1,d1
	bmi.s	.gleich_morestring
	beq.s	.length_2
	subq.w	#1,d1
	beq.s	.length_3
	bsr.s	.get_1_bit
	bcc.s	.bitset
	bsr.s	.get_1_bit
	bcc.s	.length_4
	bra.s	.length_5

.get_short_offset
	moveq	#1,d0
	bsr.s	.get_d0_bits
	subq.w	#1,d1
	bpl.s	.contoffs
	moveq	#0,d0
	rts
.get_long_offset
	moveq	#1,d0
	bsr.s	.get_d0_bits
.contoffs	add.w	d1,d1
	add.w	d1,d1
	movem.w	.offset_table-.tabellen(a2,d1),d0/d5
	bsr.s	.get_d0_bits
	add.l	d5,d1
	rts
.gleich_morestring
	moveq	#1,d0
	bsr.s	.get_d0_bits
	subq.w	#1,d1
	bmi.s	.gleich_string
	add.w	d1,d1
	add.w	d1,d1
	movem.w	.more_table-.tabellen(a2,d1),d0/d2
	bsr.s	.get_d0_bits
	add.w	d1,d2
	bsr.s	.get_long_offset
	move.w	d2,d0
	bra.s	.copy_longstring
.bitset	moveq	#2,d0
	bsr.s	.get_d0_bits
	moveq	#0,d0
	bset	d1,d0
	bra.s	.put_d0
.length_2
	moveq	#7,d0
	bsr.s	.get_d0_bits
	moveq	#2-2,d0
	bra.s	.copy_string
.length_3
	bsr.s	.get_short_offset
	tst.w	d0
	beq.s	.put_d0
	moveq	#3-2,d0
	bra.s	.copy_string
.length_4
	bsr.s	.get_short_offset
	tst.w	d0
	beq.s	.vorgnger_kopieren
	moveq	#4-2,d0
	bra.s	.copy_string
.length_5
	bsr.s	.get_short_offset
	tst.w	d0
	beq.s	.put_ff
	moveq	#5-2,d0
	bra.s	.copy_string
.put_ff	moveq	#-1,d0
	bra.s	.put_d0
.vorgnger_kopieren
	move.b	(a6),d0
;       bra.s   .put_d0
.put_d0	move.b	d0,-(a6)
	bra.s	.backmain
.gleich_string
	bsr.s	.get_long_offset
	beq.s	.backmain
	move.b	(a6),d0
.copy_gl	move.b	d0,-(a6)
	dbf	d1,.copy_gl
	sub.l	d6,d1
	bmi.s	.backmain
	bra.s	.copy_gl
.copy_longstring
	subq.w	#2,d0
.copy_string
	lea.l	2(a6,d1.l),a0
	add.w	d0,a0
	move.b	-(a0),-(a6)
.dep_b	move.b	-(a0),-(a6)
	dbf	d0,.dep_b
.backmain	bra	.normal_bytes
.direkt_tab
	dc.l	$03ff0009,$00070002,$00030001,$00030001
.tabellen	dc.l	15-1,8-1,5-1,2-1
.offset_table
	dc.w	3,0,7,16+0,11,256+16+0,15,4096+256+16+0
.more_table
	dc.w	3,5,5,16+5,7,64+16+5

; Old style ice depacker

olde_ice_depack
	movem.l	a0-a6/D0-d7,-(a7)
	cmp.l	#$49636521,(a0)+
	bne.s	.olde03
	move.l	(a0)+,d0
	lea	-8(a0,d0.L),a5
	move.l	(a0)+,(a7)
	lea	$6C(a0),a4
	move.l	a4,a6
	add.l	(a7),a6
	move.l	a6,a3
	move.l	a6,a1
	lea	.olde1D(PC),a2
	moveq	#$77,d0
.olde00	move.b	-(a1),-(a2)
	dbf	d0,.olde00
	bsr.s	.olde08
	bsr.s	.olde04
	move.l	(a7),d0
	lea	-$78(a4),a1
.olde01	move.b	(a4)+,(a1)+
	dbf	d0,.olde01
	subi.l	#$10000,d0
	bpl.s	.olde01
	moveq	#$77,d0
	lea	.olde1D(PC),a2
.olde02	move.b	-(a2),-(a3)
	dbf	d0,.olde02
.olde03	movem.l	(a7)+,a0-a6/D0-d7
	rts
.olde04	bsr.s	.olde0C
	bcc.s	.olde07
	moveq	#0,d1
	bsr.s	.olde0C
	bcc.s	.olde06
	lea	.olde1A(PC),a1
	moveq	#4,d3
.olde05	move.l	-(a1),d0
	bsr.s	.olde0F
	swap	d0
	cmp.w	d0,d1
	dbne	d3,.olde05
	add.l	$14(a1),d1
.olde06	move.b	-(a5),-(a6)
	dbf	d1,.olde06
.olde07	cmp.l	a4,a6
	bgt.s	.olde12
	rts
.olde08	moveq	#3,d0
.olde09	move.b	-(a5),d7
	ror.l	#8,d7
	dbf	d0,.olde09
	rts
.olde0A	move.w	a5,d7
	btst	#0,d7
	bne.s	.olde0B
	move.l	-(a5),d7
	addx.l	d7,d7
	bra.s	.olde11
.olde0B	move.l	-5(a5),d7
	lsl.l	#8,d7
	move.b	-(a5),d7
	subq.l	#3,a5
	add.l	d7,d7
	bset	#0,d7
	bra.s	.olde11
.olde0C	add.l	d7,d7
	beq.s	.olde0D
	rts
.olde0D	move.w	a5,d7
	btst	#0,d7
	bne.s	.olde0E
	move.l	-(a5),d7
	addx.l	d7,d7
	rts
.olde0E	move.l	-5(a5),d7
	lsl.l	#8,d7
	move.b	-(a5),d7
	subq.l	#3,a5
	add.l	d7,d7
	bset	#0,d7
	rts
.olde0F	moveq	#0,d1
.olde10	add.l	d7,d7
	beq.s	.olde0A
.olde11	addx.w	d1,d1
	dbf	d0,.olde10
	rts
.olde12	lea	.olde1B(PC),a1
	moveq	#3,d2
.olde13	bsr.s	.olde0C
	dbcc	d2,.olde13
	moveq	#0,d4
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bmi.s	.olde14
	bsr.s	.olde0F
.olde14	move.b	6(a1,d2.W),d4
	add.w	d1,d4
	beq.s	.olde16
	lea	.olde1C(PC),a1
	moveq	#1,d2
.olde15	bsr.s	.olde0C
	dbcc	d2,.olde15
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bsr.s	.olde0F
	add.w	d2,d2
	add.w	6(a1,d2.W),d1
	bra.s	.olde18
.olde16	moveq	#0,d1
	moveq	#5,d0
	moveq	#0,d2
	bsr.s	.olde0C
	bcc.s	.olde17
	moveq	#8,d0
	moveq	#$40,d2
.olde17	bsr.s	.olde0F
	add.w	d2,d1
.olde18	lea	2(a6,d4.W),a1
	add.w	d1,a1
	move.b	-(a1),-(a6)
.olde19	move.b	-(a1),-(a6)
	dbf	d4,.olde19
	bra	.olde04
	dc.b	$7F,$FF,$00,$0E,$00,$FF,$00,$07
	dc.b	$00,$07,$00,$02,$00,$03,$00,$01
	dc.b	$00,$03,$00,$01
.olde1A	dc.b	$00,$00,$01,$0D,$00,$00,$00,$0E
	dc.b	$00,$00,$00,$07,$00,$00,$00,$04
	dc.b	$00,$00,$00,$01
.olde1B	dc.b	$09,$01,$00,$FF,$FF,$08,$04,$02
	dc.b	$01,$00
.olde1C	dc.b	$0B,$04,$07,$00,$01,' ',$00,$00
	dc.b	$00,' ',$00,$00,$00,$00,$00,$00
	dcb.w	57,0
.olde1D	ds.l	1

; ICE depack v 2.3/2.4

ICE_depack_23_24
	move.l	8(a0),unpacked_length
	link	a3,#-120
	movem.l	d0-a6,-(a7)
	lea	120(a0),a4
	move.l	a4,a6
	bsr.s	.getinfo
	cmp.l	#'ICE!',d0
	bne.s	.not_packed
	bsr.s	.getinfo
	lea.l	-8(a0,d0.l),a5
	bsr.s	.getinfo
	move.l	d0,(a7)
	add.l	d0,a6
	move.l	a6,a1
	moveq	#119,d0
.save	move.b	-(a1),-(a3)
	dbf	d0,.save
	move.l	a6,a3
	move.b	-(a5),d7
	bsr.s	.normal_bytes
	move.l	a3,a5
	bsr	.get_1_bit
	bcc.s	.no_picture
	move.w	#$0f9f,d7
.ICE_00	moveq	#3,d6
.ICE_01	move.w	-(a3),d4
	moveq	#3,d5
.ICE_02	add.w	d4,d4
	addx.w	d0,d0
	add.w	d4,d4
	addx.w	d1,d1
	add.w	d4,d4
	addx.w	d2,d2
	add.w	d4,d4
	addx.w	d3,d3
	dbra	d5,.ICE_02
	dbra	d6,.ICE_01
	movem.w	d0-d3,(a3)
	dbra	d7,.ICE_00
.no_picture
	movem.l	(a7),d0-a3
.move	move.b	(a4)+,(a0)+
	subq.l	#1,d0
	bne.s	.move
	moveq	#119,d0
.rest	move.b	-(a3),-(a5)
	dbf	d0,.rest
.not_packed
	movem.l	(a7)+,d0-a6
	unlk	a3
	rts
.getinfo	moveq	#3,d1
.getbytes	lsl.l	#8,d0
	move.b	(a0)+,d0
	dbf	d1,.getbytes
	rts
.normal_bytes
	bsr.s	.get_1_bit
	bcc.s	.test_if_end
	moveq.l	#0,d1
	bsr.s	.get_1_bit
	bcc.s	.copy_direkt
	lea.l	.direkt_tab+20(pc),a1
	moveq.l	#4,d3
.nextgb	move.l	-(a1),d0
	bsr.s	.get_d0_bits
	swap.w	d0
	cmp.w	d0,d1
	dbne	d3,.nextgb
.no_more	add.l	20(a1),d1
.copy_direkt
	move.b	-(a5),-(a6)
	dbf	d1,.copy_direkt
.test_if_end
	cmp.l	a4,a6
	bgt.s	.strings
	rts
.get_1_bit
	add.b	d7,d7
	bne.s	.bitfound
	move.b	-(a5),d7
	addx.b	d7,d7
.bitfound
	rts
.get_d0_bits
	moveq.l	#0,d1
.hole_bit_loop
	add.b	d7,d7
	bne.s	.on_d0
	move.b	-(a5),d7
	addx.b	d7,d7
.on_d0	addx.w	d1,d1
	dbf	d0,.hole_bit_loop
	rts
.strings	lea.l	.length_tab(pc),a1
	moveq.l	#3,d2
.get_length_bit
	bsr.s	.get_1_bit
	dbcc	d2,.get_length_bit
.no_length_bit
	moveq.l	#0,d4
	moveq.l	#0,d1
	move.b	1(a1,d2.w),d0
	ext.w	d0
	bmi.s	.no_ber
.get_ber
	bsr.s	.get_d0_bits
.no_ber	move.b	6(a1,d2.w),d4
	add.w	d1,d4
	beq.s	.get_offset_2
	lea.l	.more_offset(pc),a1
	moveq.l	#1,d2
.getoffs	bsr.s	.get_1_bit
	dbcc	d2,.getoffs
	moveq.l	#0,d1
	move.b	1(a1,d2.w),d0
	ext.w	d0
	bsr.s	.get_d0_bits
	add.w	d2,d2
	add.w	6(a1,d2.w),d1
	bpl.s	.depack_bytes
	sub.w	d4,d1
	bra.s	.depack_bytes
.get_offset_2
	moveq.l	#0,d1
	moveq.l	#5,d0
	moveq.l	#-1,d2
	bsr.s	.get_1_bit
	bcc.s	.less_40
	moveq.l	#8,d0
	moveq.l	#$3f,d2
.less_40	bsr.s	.get_d0_bits
	add.w	d2,d1
.depack_bytes
	lea.l	2(a6,d4.w),a1
	add.w	d1,a1
	move.b	-(a1),-(a6)
.dep_b	move.b	-(a1),-(a6)
	dbf	d4,.dep_b
	bra	.normal_bytes
.direkt_tab
	dc.l	$7fff000e,$00ff0007,$00070002,$00030001,$00030001
	dc.l	270-1,15-1,8-1,5-1,2-1
.length_tab
	dc.b	9,1,0,-1,-1
	dc.b	8,4,2,1,0
.more_offset
	dc.b	11,4,7,0
	dc.w	$11f,-1,$1f

; depack ice 2.3 prog file

ICE_23_prog_depack
	move.b	-(a5),d7
.I23pA	bsr.s	.I23pE
	bcc.s	.I23pD
	moveq	#0,d1
	bsr.s	.I23pE
	bcc.s	.I23pC
	lea	.I23p1B(PC),a1
	moveq	#4,d3
.I23pB	move.l	-(a1),d0
	bsr.s	.I23p10
	swap	d0
	cmp.w	d0,d1
	dbne	d3,.I23pB
	add.l	$14(a1),d1
.I23pC	move.b	-(a5),-(a6)
	dbf	d1,.I23pC
.I23pD	cmp.l	a4,a6
	bgt.s	.I23p13
	rts
.I23pE	add.b	d7,d7
	bne.s	.I23pF
	move.b	-(a5),d7
	addx.b	d7,d7
.I23pF	rts
.I23p10	moveq	#0,d1
.I23p11	add.b	d7,d7
	bne.s	.I23p12
	move.b	-(a5),d7
	addx.b	d7,d7
.I23p12	addx.w	d1,d1
	dbf	d0,.I23p11
	rts
.I23p13	lea	.I23p1C(PC),a1
	moveq	#3,d2
.I23p14	bsr.s	.I23pE
	dbcc	d2,.I23p14
	moveq	#0,d4
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bmi.s	.I23p15
	bsr.s	.I23p10
.I23p15	move.b	6(a1,d2.W),d4
	add.w	d1,d4
	beq.s	.I23p17
	lea	.I23p1D(PC),a1
	moveq	#1,d2
.I23p16	bsr.s	.I23pE
	dbcc	d2,.I23p16
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bsr.s	.I23p10
	add.w	d2,d2
	add.w	6(a1,d2.W),d1
	bpl.s	.I23p19
	sub.w	d4,d1
	bra.s	.I23p19
.I23p17	moveq	#0,d1
	moveq	#5,d0
	moveq	#-1,d2
	bsr.s	.I23pE
	bcc.s	.I23p18
	moveq	#8,d0
	moveq	#$3F,d2
.I23p18	bsr.s	.I23p10
	add.w	d2,d1
.I23p19	lea	2(a6,d4.W),a1
	add.w	d1,a1
	move.b	-(a1),-(a6)
.I23p1A	move.b	-(a1),-(a6)
	dbf	d4,.I23p1A
	bra	.I23pA
	dc.b	$7F,$FF,$00,$0E,$00,$FF,$00,$07
	dc.b	$00,$07,$00,$02,$00,$03,$00,$01
	dc.b	$00,$03,$00,$01
.I23p1B	dc.b	$00,$00,$01,$0D,$00,$00,$00,$0E
	dc.b	$00,$00,$00,$07,$00,$00,$00,$04
	dc.b	$00,$00,$00,$01
.I23p1C	dc.b	$09,$01,$00,$FF,$FF,$08,$04,$02
	dc.b	$01,$00
.I23p1D	dc.b	$0B,$04,$07,$00,$01,$1F,$FF,$FF
	dc.b	$00,$1F

; depack ice v2.4 packed programs

ICE_24_prog_depack
	move.b	-(a5),d7
.I24p0A	bsr.s	.I24p0E
	bcc.s	.I24p0D
	moveq	#0,d1
	bsr.s	.I24p0E
	bcc.s	.I24p0C
	lea	.I24p1B(PC),a1
	moveq	#4,d3
.I24p0B	move.l	-(a1),d0
	bsr.s	.I24p10
	swap	d0
	cmp.w	d0,d1
	dbne	d3,.I24p0B
	add.l	$14(a1),d1
.I24p0C	move.b	-(a5),-(a6)
	dbf	d1,.I24p0C
.I24p0D	cmpa.l	a4,a6
	bgt.s	.I24p13
	rts
.I24p0E	add.b	d7,d7
	bne.s	.I24p0F
	move.b	-(a5),d7
	addx.b	d7,d7
.I24p0F	rts
.I24p10	moveq	#0,d1
.I24p11	add.b	d7,d7
	bne.s	.I24p12
	move.b	-(a5),d7
	addx.b	d7,d7
.I24p12	addx.w	d1,d1
	dbf	d0,.I24p11
	rts
.I24p13	lea	.I24p1C(PC),a1
	moveq	#3,d2
.I24p14	bsr.s	.I24p0E
	dbcc	d2,.I24p14
	moveq	#0,d4
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bmi.s	.I24p15
	bsr.s	.I24p10
.I24p15	move.b	6(a1,d2.W),d4
	add.w	d1,d4
	beq.s	.I24p17
	lea	.I24p1D(PC),a1
	moveq	#1,d2
.I24p16	bsr.s	.I24p0E
	dbcc	d2,.I24p16
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bsr.s	.I24p10
	add.w	d2,d2
	add.w	6(a1,d2.W),d1
	bpl.s	.I24p19
	sub.w	d4,d1
	bra.s	.I24p19
.I24p17	moveq	#0,d1
	moveq	#5,d0
	moveq	#-1,d2
	bsr.s	.I24p0E
	bcc.s	.I24p18
	moveq	#8,d0
	moveq	#$3F,d2
.I24p18	bsr.s	.I24p10
	add.w	d2,d1
.I24p19	lea	2(a6,d4.W),a1
	adda.w	d1,a1
	move.b	-(a1),-(a6)
.I24p1A	move.b	-(a1),-(a6)
	dbf	d4,.I24p1A
	bra	.I24p0A
	dc.b	$7F,$FF,$00,$0E,$00,$FF,$00,$07
	dc.b	$00,$07,$00,$02,$00,$03,$00,$01
	dc.b	$00,$03,$00,$01
.I24p1B	dc.b	$00,$00,$01,$0D,$00,$00,$00,$0E
	dc.b	$00,$00,$00,$07,$00,$00,$00,$04
	dc.b	$00,$00,$00,$01
.I24p1C	dc.b	$09,$01,$00,$FF,$FF,$08,$04,$02
	dc.b	$01,$00
.I24p1D	dc.b	$0B,$04,$07,$00,$01,$1F,$FF,$FF
	dc.b	$00,$1F

; depack LZH (jam3/4)

RESET	movem.w	d0-d1/D3/D5,-(a7)
	move.l	a1,a2
	moveq	#$00,d2
	moveq	#-$02,d4
	moveq	#$00,d1
	move.w	#$0139,d5
OUTER	addq.w	#2,d4
	cmp.w	(a2)+,d7
	bhi.s	OUTER
	move.w	$00(a3,d4.W),d3
	addq.w	#1,d3
	lsr.w	#1,d3
	cmp.w	-$02(a3,d2.W),d3
	bls.s	BIDDLE
	move.w	$00(a3,d1.W),d0
	add.w	$02(a3,d1.W),d0
	cmp.w	d0,d3
	bls.s	BIDDLE
BITH	move.w	d0,$00(a3,d2.W)
	move.w	d1,$00(a1,d2.W)
	move.w	d2,$00(a0,d1.W)
	move.w	d2,$02(a0,d1.W)
	addq.w	#2,d2
	addq.l	#4,d1
	move.w	$00(a3,d1.W),d0
	add.w	$02(a3,d1.W),d0
	cmp.w	d0,d3
	bhi.s	BITH
BIDDLE	move.w	d3,$00(a3,d2.W)
	move.w	$00(a1,d4.W),d0
	move.w	d0,$00(a1,d2.W)
	move.w	d2,$00(a0,d0.W)
	addq.w	#2,d2
	dbf	d5,OUTER
MAKETABLE2
	move.w	$00(a3,d1.W),d0
	add.w	$02(a3,d1.W),d0
	move.w	d0,$00(a3,d2.W)
	move.w	d1,$00(a1,d2.W)
	move.w	d2,$00(a0,d1.W)
	move.w	d2,$02(a0,d1.W)
	addq.w	#2,d2
	addq.l	#4,d1
	cmp.w	d7,d2
	bne.s	MAKETABLE2
	movem.w	(a7)+,d0-d1/D3/D5
	rts
CREATE	move.l	TABLE7(PC),a0
	lea	TDATA1(PC),a1
	moveq	#$00,d0
	moveq	#$1F,d1
	moveq	#$00,d2
LOOP1	move.b	d0,(a0)+
	dbf	d1,LOOP1
	dbf	d2,EXIT1
	move.w	(a1)+,d3
	moveq	#$03,d2
EXIT1	rol.w	#4,d3
	move.w	d3,d1
	andi.w	#$000F,d1
	addq.b	#4,d0
	bcc.s	LOOP1
	move.l	TABLE8(PC),a0
	lea	TDATA2(PC),a1
	moveq	#$05,d0
	moveq	#$03,d1
LOOP2	move.b	$00(a1,d0.W),d2
	ext.w	d2
LOOP3	move.b	d1,(a0)+
	dbf	d2,LOOP3
	addq.w	#1,d1
	dbf	d0,LOOP2
	rts
CREATE2	move.l	TABLE1(PC),a0
	move.l	TABLE5(PC),a1
	move.l	TABLE4(PC),a2
	moveq	#$01,d1
	move.w	#$04E6,d2
	moveq	#$00,d4
	move.w	#$0139,d0
CONTINUE
	move.w	d1,(a0)+
	move.w	d2,(a1)+
	move.w	d4,(a2)+
	addq.w	#2,d2
	addq.w	#2,d4
	dbf	d0,CONTINUE
	move.l	TABLE1(PC),a0
	move.l	TABLE2(PC),a3
	move.l	TABLE6(PC),a1
	move.l	TABLE3(PC),a2
	move.w	#$0274,d2
	moveq	#$00,d4
	move.w	#$0138,d0
DOMORE	move.w	(a0)+,d1
	add.w	(a0)+,d1
	move.w	d1,(a3)+
	move.w	d4,(a1)+
	move.w	d2,(a2)+
	move.w	d2,(a2)+
	addq.w	#4,d4
	addq.w	#2,d2
	dbf	d0,DOMORE
	move.w	#$FFFF,(a3)
	clr.w	(a2)
	rts
RESTORE	bsr	RESET
	bra	BACK
TABLE1	dc.l	00
TABLE2	dc.l	00
TABLE3	dc.l	00
TABLE4	dc.l	00
TABLE5	dc.l	00
TABLE6	dc.l	00
TABLE7	dc.l	00
TABLE8	dc.l	00
EXIT
	addq.l	#4,a7
	rts
UNPACK
	move.l	a0,a5
	move.l	a1,a6
	lea	TABLE1(PC),a0
	move.l	#0,(a0)
	move.l	#$274,4(a0)
	move.l	#$4E8,8(a0)
	move.l	#$9CE,12(a0)
	move.l	#$C42,16(a0)
	move.l	#$EB6,20(a0)
	move.l	#$112A,24(a0)
	move.l	#$122A,28(a0)
	move.l	a7,d1
	sub.l	#$1500,d1
	move.w	#7,d0
TABLOOP
	add.l	d1,(a0)+
	dbf	d0,TABLOOP
	bsr	CREATE
	bsr	CREATE2
	move.l	TABLE3(PC),a0
	move.l	TABLE5(PC),a1
	move.l	TABLE1(PC),a3
	move.l	a6,-(a7)
	add.l	4(a5),a6
	add.l	8(a5),a5
	add.l	#$0C,a5
	move.l	a6,a4
	move.w	#59,d0
SPACES	move.b	#$20,(a4)+
	dbf	d0,SPACES
	moveq	#$08,d6
	move.b	-(a5),d5
	move.w	#$04E6,d7
	move.l	TABLE8(PC),a4
TOP	cmp.l	(a7),a6
	ble	EXIT
	move.w	$04E4(a1),d0
MORE	dbf	d6,NOTEMPTY
	moveq	#$07,d6
	move.b	-(a5),d5
NOTEMPTY
	add.b	d5,d5
	bcc.s	ZERO
	addq.w	#2,d0
ZERO	move.w	$00(a1,d0.W),d0
	cmp.w	d7,d0
	blt.s	MORE
	move.w	d0,d1
	sub.w	d7,d0
	move.w	$04E4(a3),d4
	bmi	RESTORE
BACK	move.w	$00(a0,d1.W),d1
SCAN	lea	$00(a3,d1.W),a2
	addq.w	#1,(a2)
	cmpm.w	(a2)+,(a2)+
	bcs.s	FRSTGTR
	move.w	$00(a0,d1.W),d1
	bne.s	SCAN
	lsr.w	#1,d0
	cmp.w	#$0100,d0
	bge.s	GTE256
WRITE	move.b	d0,-(a6)
	bra.s	TOP
FRSTGTR	subq.w	#1,-$0004(a2)
	move.w	-$0004(a2),d4
SAME	cmp.w	(a2)+,d4
	beq.s	SAME
	subq.l	#4,a2
	addq.w	#1,(a2)
	suba.l	a3,a2
	move.w	$00(a1,d1.W),d4
	move.w	a2,$00(a0,d4.W)
	cmp.w	d7,d4
	bcc.s	SKIP
	move.w	a2,$02(a0,d4.W)
SKIP	move.w	$00(a1,a2.W),d2
	move.w	d4,$00(a1,a2.W)
	move.w	d1,$00(a0,d2.W)
	cmp.w	d7,d2
	bcc.s	SKIP2
	move.w	d1,$02(a0,d2.W)
SKIP2	move.w	d2,$00(a1,d1.W)
	move.w	$00(a0,a2.W),d1
	bne.s	SCAN
	lsr.w	#1,d0
	cmp.w	#$0100,d0
	blt.s	WRITE
GTE256	move.b	-1(a5),d1
	moveq	#$00,d2
	lsr.b	d6,d1
	or.b	d5,d1
	move.b	$00(a4,d1.W),d2
	sub.w	d2,d6
	bpl.s	POSITIVE
	move.b	-(a5),d5
	move.b	d5,d4
	addq.w	#2,d6
	bmi.s	OVERFLOW
	beq.s	MODD4
	lsr.b	#1,d4
	lsl.b	#7,d5
	bra.s	MODD4
READONE	move.b	-(a5),d5
	move.b	d5,d4
	lsl.b	#6,d5
	moveq	#$02,d6
	lsr.b	d6,d4
	bra.s	DOCOPY
POSITIVE
	beq.s	READONE
	move.b	(a5),d5
	subq.w	#6,d6
OVERFLOW
	neg.w	d6
	lsl.b	d6,d5
	move.b	d5,d4
	move.b	-(a5),d5
	move.b	d5,d2
	lsl.b	d6,d5
	subq.w	#8,d6
	neg.w	d6
	lsr.b	d6,d2
	or.b	d2,d4
MODD4	andi.w	#$003F,d4
DOCOPY	move.l	TABLE7(PC),a0
	move.b	(a0,d1.W),d2
	lsl.w	#4,d2
	or.b	d4,d2
	;NEG.W   D2
	lea	$01(a6,d2.W),a0
	subi.w	#$00FE,d0
	move.w	d0,d1
COPY	move.b	-(a0),d0
	move.b	d0,-(a6)
	dbf	d1,COPY
	move.l	TABLE3(PC),a0
	bra	TOP

TDATA2	btst	d7,$2F3F(a7)
	move.l	(a7)+,-(a7)
TDATA1
	dc.w	$FFF7
	dc.w	$7777
	dc.w	$7773
	dc.w	$3333
	dc.w	$3333
	dc.w	$3331
	move.b	(a1),-(a0)
	move.b	(a1),-(a0)
	move.b	(a1),-(a0)
	move.b	(a1),-(a0)
	move.b	(a1),-(a0)
	move.b	(a0),-(a0)
	ori.b	#$00,d0
	ori.b	#$00,d0

; depack LZW file

dp_LZW	move.l	a0,a1
	add.l	#$100,a1
.LZW0D	move.l	a0,a4
	move.l	a1,a6
	add.l	8(a0),a4
	add.l	4(a0),a6
	move.l	a6,a5
	move.l	4(a0),d4
	move.b	#$20,d3
	move.w	#$FED,d5
	move.l	a6,a3
	moveq	#0,d7
.LZW0E	dbf	d7,.LZW0F
	move.b	-(a4),d6
	moveq	#7,d7
.LZW0F	lsr.b	#1,d6
	bcc.s	.LZW10
	move.b	-(a4),-(a6)
	addq.w	#1,d5
	subq.l	#1,d4
	bgt.s	.LZW0E
	bra.s	.LZW13
.LZW10	move.b	-2(a4),d0
	lsl.w	#4,d0
	move.b	-(a4),d0
	sub.w	d5,d0
	neg.w	d0
	and.w	#$FFF,d0
	lea	1(a6,d0.W),a0
	moveq	#$F,d1
	and.b	-(a4),d1
	addq.w	#2,d1
	moveq	#1,d0
	add.w	d1,d0
	cmp.l	a5,a0
	bgt.s	.LZW14
.LZW11	move.b	-(a0),-(a6)
	dbf	d1,.LZW11
.LZW12	add.l	d0,d5
	sub.l	d0,d4
	bgt.s	.LZW0E
.LZW13	rts
.LZW14	cmp.l	a5,a0
	ble.s	.LZW11
	move.b	d3,-(a6)
	subq.l	#1,a0
	dbf	d1,.LZW14
	bra.s	.LZW12

; depack automation v 2.3r / 2.51

depack_aut_23r_251
	addq.l	#4,a0
	move.l	a0,a4
	move.l	(a0)+,d5
	add.l	d5,a1
	add.l	(a0),a0
	suba.l	#4,a0
	tst.w	-(a0)
	bpl.s	.AUT1
	subq.l	#1,a0
.AUT1	move.b	-(a0),d0
.AUT2	lsl.b	#1,d0
	bne.s	.AUT3
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT3	bcc.s	.AUT10
	clr.w	d1
	lsl.b	#1,d0
	bne.s	.AUT4
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT4	bcc.s	.AUT9
	lea	.nums(PC),a3
	moveq	#3,d3
.AUT5	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
.AUT6	lsl.b	#1,d0
	bne.s	.AUT7
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT7	roxl.w	#1,d1
	dbf	d2,.AUT6
	tst.w	d3
	beq.s	.AUT8
	cmp.w	d1,d4
	dbne	d3,.AUT5
.AUT8	move.b	4(a3,d3.W),d2
	ext.w	d2
	add.w	d2,d1
	bra.s	.AUT9

.nums	dc.b	$0A,$03,$02,$02,$0E,$07,$04,$01

.AUT9	move.b	-(a0),-(a1)
	dbf	d1,.AUT9
.AUT10	move.l	a4,a3
	addq.l	#$8,a3
	cmp.l	a3,a0
	ble	.BYENOW
	lea	.nums2(PC),a3
	moveq	#3,d2
.AUT11	lsl.b	#1,d0
	bne.s	.AUT12
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT12	bcc.s	.AUT13
	dbf	d2,.AUT11
.AUT13	clr.w	d1
	addq.w	#1,d2
	move.b	0(a3,d2.W),d3
	beq.s	.AUT16
	ext.w	d3
	subq.w	#1,d3
.AUT14	lsl.b	#1,d0
	bne.s	.AUT15
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT15	roxl.w	#1,d1
	dbf	d3,.AUT14
.AUT16	move.b	5(a3,d2.W),d3
	ext.w	d3
	add.w	d3,d1
	bra.s	.AUT161
.nums2	dc.b	$0A,$02,$01,$00,$00,$0A,$06,$04,$03,$02
.AUT161	cmp.w	#2,d1
	beq.s	.AUT22
	lea	.nums3(PC),a3
	moveq	#1,d3
.AUT17	lsl.b	#1,d0
	bne.s	.AUT18
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT18	bcc.s	.AUT19
	dbf	d3,.AUT17
.AUT19	addq.w	#1,d3
	clr.w	d2
	move.b	0(a3,d3.W),d4
	ext.w	d4
.AUT20	lsl.b	#1,d0
	bne.s	.AUT21
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT21	roxl.w	#1,d2
	dbf	d4,.AUT20
	lsl.w	#1,d3
	add.w	4(a3,d3.W),d2
	bra.s	.AUT26

.nums3	dc.b	$0B,$04,$07,$00,$01,$20,$00,$00
	dc.b	$00,$20,$00,$00

.AUT22	clr.w	d2
	moveq	#5,d3
	clr.w	d4
	lsl.b	#1,d0
	bne.s	.AUT23
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT23	bcc.s	.AUT24
	moveq	#8,d3
	moveq	#$40,d4
.AUT24	lsl.b	#1,d0
	bne.s	.AUT25
	move.b	-(a0),d0
	roxl.b	#1,d0
.AUT25	roxl.w	#1,d2
	dbf	d3,.AUT24
	add.w	d4,d2
.AUT26	lea	0(a1,d2.W),a2
	ext.l	d1
	add.l	d1,a2
	subq.w	#1,d1
.AUT27	move.b	-(a2),-(a1)
	dbf	d1,.AUT27
	bra	.AUT2
.BYENOW	rts

; LSD depacker

LSD_depack
	move.l	store_end(PC),a0
	move.l	save_add(PC),a1
	move.l	-(a0),a2
	lea	upk_l(PC),a3
	move.l	a2,(a3)
	add.l	a1,a2
	lea	upk_end(PC),a3
	move.l	a2,(a3)
	move.l	-(a0),d5
	move.l	-(a0),d0
	eor.l	d0,d5
.LSD0B	lsr.l	#1,d0
	bne.s	.LSD0C
	bsr.s	.LSD18
.LSD0C	bcs.s	.LSD13
	moveq	#8,d1
	moveq	#1,d3
	lsr.l	#1,d0
	bne.s	.LSD0D
	bsr.s	.LSD18
.LSD0D	bcs.s	.LSD15
	moveq	#3,d1
	clr.w	d4
.LSD0E	bsr.s	.LSD19
	move.w	d2,d3
	add.w	d4,d3
.LSD0F	moveq	#7,d1
.LSD10	lsr.l	#1,d0
	bne.s	.LSD11
	bsr.s	.LSD18
.LSD11	roxl.l	#1,d2
	dbf	d1,.LSD10
	move.b	d2,-(a2)
	dbf	d3,.LSD0F
	bra.s	.LSD17
.LSD12	moveq	#8,d1
	moveq	#8,d4
	bra.s	.LSD0E
.LSD13	moveq	#2,d1
	bsr.s	.LSD19
	cmp.b	#2,d2
	blt.s	.LSD14
	cmp.b	#3,d2
	beq.s	.LSD12
	moveq	#8,d1
	bsr.s	.LSD19
	move.w	d2,d3
	move.w	#$C,d1
	bra.s	.LSD15
.LSD14	move.w	#9,d1
	add.w	d2,d1
	addq.w	#2,d2
	move.w	d2,d3
.LSD15	bsr.s	.LSD19
.LSD16	subq.w	#1,a2
	move.b	0(a2,d2.W),(a2)
	dbf	d3,.LSD16
.LSD17	cmp.l	a2,a1
	blt.s	.LSD0B
	rts
.LSD18	move.l	-(a0),d0
	eor.l	d0,d5
	move	#$10,CCR
	roxr.l	#1,d0
	rts
.LSD19	subq.w	#1,d1
	clr.w	d2
.LSD1A	lsr.l	#1,d0
	bne.s	.LSD1B
	move.l	-(a0),d0
	eor.l	d0,d5
	move	#$10,CCR
	roxr.l	#1,d0
.LSD1B	roxl.l	#1,d2
	dbf	d1,.LSD1A
	rts

store_end	dcb.w	2,0
save_add	dcb.w	2,0
upk_l	dcb.w	2,0
store_start
	dcb.w	2,0
upk_end	dcb.w	2,0

; automation 501 depacker
aut501_depack
	move.l	8(a0),unpacked_length
	link	a3,#-$78
	movem.l	a0-a6/D0-d7,-(a7)
	lea	$78(a0),a4
	movea.l	a4,a6
	bsr.s	.AU516
	cmpi.l	#$41553521,d0
	bne.s	.AU515
	bsr.s	.AU516
	lea	-8(a0,d0.L),a5
	bsr.s	.AU516
	move.l	d0,(a7)
	adda.l	d0,a6
	movea.l	a6,a1
	moveq	#$77,d0
.AU50E	move.b	-(a1),-(a3)
	dbf	d0,.AU50E
	movea.l	a6,a3
	move.b	-(a5),d7
	bsr.s	.AU518
	movea.l	a3,a5
	bsr	.AU51C
	bcc.s	.AU512
	move.w	#$F9F,d7
.AU50F	moveq	#3,d6
.AU510	move.w	-(a3),d4
	moveq	#3,d5
.AU511	add.w	d4,d4
	addx.w	d0,d0
	add.w	d4,d4
	addx.w	d1,d1
	add.w	d4,d4
	addx.w	d2,d2
	add.w	d4,d4
	addx.w	d3,d3
	dbf	d5,.AU511
	dbf	d6,.AU510
	movem.w	d0-d3,(a3)
	dbf	d7,.AU50F
.AU512	movem.l	(a7),a0-a3/D0-d7
.AU513	move.b	(a4)+,(a0)+
	subq.l	#1,d0
	bne.s	.AU513
	moveq	#$77,d0
.AU514	move.b	-(a3),-(a5)
	dbf	d0,.AU514
.AU515	movem.l	(a7)+,a0-a6/D0-d7
	unlk	a3
	rts
.AU516	moveq	#3,d1
.AU517	lsl.l	#8,d0
	move.b	(a0)+,d0
	dbf	d1,.AU517
	rts
.AU518	bsr.s	.AU51C
	bcc.s	.AU51B
	moveq	#0,d1
	bsr.s	.AU51C
	bcc.s	.AU51A
	lea	.AU529(PC),a1
	moveq	#4,d3
.AU519	move.l	-(a1),d0
	bsr.s	.AU51E
	swap	d0
	cmp.w	d0,d1
	dbne	d3,.AU519
	add.l	$14(a1),d1
.AU51A	move.b	-(a5),-(a6)
	dbf	d1,.AU51A
.AU51B	cmpa.l	a4,a6
	bgt.s	.AU521
	rts
.AU51C	add.b	d7,d7
	bne.s	.AU51D
	move.b	-(a5),d7
	addx.b	d7,d7
.AU51D	rts
.AU51E	moveq	#0,d1
.AU51F	add.b	d7,d7
	bne.s	.AU520
	move.b	-(a5),d7
	addx.b	d7,d7
.AU520	addx.w	d1,d1
	dbf	d0,.AU51F
	rts
.AU521	lea	.AU52A(PC),a1
	moveq	#3,d2
.AU522	bsr.s	.AU51C
	dbcc	d2,.AU522
	moveq	#0,d4
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bmi.s	.AU523
	bsr.s	.AU51E
.AU523	move.b	6(a1,d2.W),d4
	add.w	d1,d4
	beq.s	.AU525
	lea	.AU52B(PC),a1
	moveq	#1,d2
.AU524	bsr.s	.AU51C
	dbcc	d2,.AU524
	moveq	#0,d1
	move.b	1(a1,d2.W),d0
	ext.w	d0
	bsr.s	.AU51E
	add.w	d2,d2
	add.w	6(a1,d2.W),d1
	bpl.s	.AU527
	sub.w	d4,d1
	bra.s	.AU527
.AU525	moveq	#0,d1
	moveq	#5,d0
	moveq	#-1,d2
	bsr.s	.AU51C
	bcc.s	.AU526
	moveq	#8,d0
	moveq	#$3F,d2
.AU526	bsr.s	.AU51E
	add.w	d2,d1
.AU527	lea	2(a6,d4.W),a1
	adda.w	d1,a1
	move.b	-(a1),-(a6)
.AU528	move.b	-(a1),-(a6)
	dbf	d4,.AU528
	bra	.AU518
	dc.b	$7F,$FF,$00,$0E,$00,$FF,$00,$07
	dc.b	$00,$07,$00,$02,$00,$03,$00,$01
	dc.b	$00,$03,$00,$01
.AU529	dc.b	$00,$00,$01,$0D,$00,$00,$00,$0E
	dc.b	$00,$00,$00,$07,$00,$00,$00,$04
	dc.b	$00,$00,$00,$01
.AU52A	dc.b	$09,$01,$00,$FF,$FF,$08,$04,$02
	dc.b	$01,$00
.AU52B	dc.b	$0B,$04,$07,$00,$01,$1F,$FF,$FF
	dc.b	$00,$1F


; Pompey pirates depacker

pp_depack	move.l	a2,a3		; save start
	subq.l	#4,a0		; point to POPI
	add.l	-(a0),a2		; add upkd length to start
	move.l	(a0),unpacked_length
	move.l	a2,a4		; save it
	subq.l	#4,a0		; point to packed length
.PP01	move.b	-(a0),d0
	beq.s	.PP01
.PP02	moveq	#0,d2
	bsr.s	.PP0F
	bcs.s	.PP05
	move.b	-(a0),d2
.PP03	move.b	d2,-(a2)
	bra.s	.PP07
.PP04	moveq	#$F,d4
	moveq	#2,d3
	bsr.s	.PP12
	bne.s	.PP0D
	move.w	d4,d2
	bra.s	.PP03
.PP05	bsr.s	.PP0F
	bcc.s	.PP09
	bsr.s	.PP0F
	bcc.s	.PP04
	bsr.s	.PP0F
	bcs.s	.PP06
	moveq	#2,d1
	bsr	.PP15
	exg	d2,d1
	addq.w	#1,d2
	bset	d1,d2
	bra.s	.PP03
.PP06	bsr.s	.PP0F
	bcs.s	.PP08
	moveq	#$F,d4
	move.w	#3,d3
	bsr.s	.PP12
	bne.s	.PP0D
	move.b	(a2),-(a2)
.PP07	cmp.l	a2,a3
	blt.s	.PP02
	rts			; finished
.PP08	moveq	#4,d3
	bra.s	.PP0C
.PP09	bsr.s	.PP0F
	bcs.s	.PP19
	moveq	#1,d1
.PP0A	lsr.b	#1,d0
	bne.s	.PP0B
	move.b	-(a0),d0
	roxr.b	#1,d0
.PP0B	addx.w	d2,d2
	dbf	d1,.PP0A
	move.w	d2,d1
	beq.s	.PP18
	addq.w	#1,d1
	add.w	d1,d1
	subq.w	#1,d1
	bsr.s	.PP15
	move.w	d2,d3
.PP0C	bsr.s	.PP11
.PP0D	move.l	a2,a1
	add.l	d2,a1
.PP0E	move.b	-(a1),-(a2)
	dbf	d3,.PP0E
	bra.s	.PP07
.PP0F	lsr.b	#1,d0
	bne.s	.PP10
	move.b	-(a0),d0
	roxr.b	#1,d0
.PP10	rts
.PP11	moveq	#0,d4
.PP12	moveq	#1,d1
	moveq	#0,d2
.PP13	lsr.b	#1,d0
	bne.s	.PP14
	move.b	-(a0),d0
	roxr.b	#1,d0
.PP14	addx.w	d2,d2
	dbf	d1,.PP13
	move.w	d2,d1
	addq.w	#1,d1
	lsl.w	#2,d1
	subq.w	#1,d1
	eor.b	d1,d4
	bne.s	.PP15
	rts
	moveq	#7,d1
.PP15	moveq	#0,d2
.PP16	lsr.b	#1,d0
	bne.s	.PP17
	move.b	-(a0),d0
	roxr.b	#1,d0
.PP17	addx.w	d2,d2
	dbf	d1,.PP16
	rts
.PP18	bsr.s	.PP11
	move.w	d2,d3
	moveq	#1,d2
	bra.s	.PP0D
.PP19	moveq	#1,d3
	move.b	-(a0),d2
	bra.s	.PP0D

; DCSquish depacker

DCsquish_unpack
	move.l	a5,a2
	move.w	#0,d0
	beq.s	.squish1C
.squish1B	move.w	(a3)+,(a2)+
	dbf	d0,.squish1B
.squish1C	moveq	#3,d2
	moveq	#5,d3
	moveq	#6,d4
	moveq	#7,d7
.squish1D	moveq	#0,d0
	move.b	(a3)+,d0
	bmi.s	.squish20
	beq.s	.exit
	bclr	d4,d0
	beq.s	.squish1F
	move.w	d0,d1
	lsl.w	d2,d1
	bclr	d3,d0
	addq.w	#2,d0
	bra.s	.squish22
.squish1E	move.b	(a3)+,(a2)+
.squish1F	dbf	d0,.squish1E
	bra.s	.squish1D
.squish20	bclr	d7,d0
	move.w	d0,d1
	lsl.w	d3,d1
	and.b	d7,d0
	bne.s	.squish21
	move.b	(a3)+,d0
	addq.w	#8,d0
.squish21	addq.w	#1,d0
.squish22	move.b	(a3)+,d1
	move.l	a2,a0
	suba.w	d1,a0
.squish23	move.b	(a0)+,(a2)+
	dbf	d0,.squish23
	bra.s	.squish1D
.exit	rts

; 4pak depacker

_4pak_depack
	bra.s	._4p05
._4p02	moveq	#$1F,d1
	and.w	d0,d1
	lsl.w	#3,d0
	move.b	(a1)+,d0
	lea	-$800(a0),a2
	add.w	d0,a2
	move.b	(a2)+,(a0)+
	move.b	(a2)+,(a0)+
._4p03	move.b	(a2)+,(a0)+
	dbf	d1,._4p03
	bra.s	._4p05
._4p04	move.b	(a1)+,(a0)+
	dbf	d0,._4p04
._4p05	moveq	#0,d0
	move.b	(a1)+,d0
	blt.s	._4p02
	dbeq	d0,._4p04
	rts

; bytekiller depacker

bytekiller_depack
	move.l	a6,a0
	move.l	a5,a1
	move.l	(a0)+,d0
	move.l	(a0)+,d1
	move.l	(a0)+,d5
	move.l	a1,a2
	add.l	d0,a0
	add.l	d1,a2
	move.l	-(a0),d0
	eor.l	d0,d5
.BYT00	lsr.l	#1,d0
	bne.s	.BYT01
	bsr.s	.BYT0E
.BYT01	bcs.s	.BYT08
	moveq	#8,d1
	moveq	#1,d3
	lsr.l	#1,d0
	bne.s	.BYT02
	bsr.s	.BYT0E
.BYT02	bcs.s	.BYT0A
	moveq	#3,d1
	clr.w	d4
.BYT03	bsr.s	.BYT0F
	move.w	d2,d3
	add.w	d4,d3
.BYT04	moveq	#7,d1
.BYT05	lsr.l	#1,d0
	bne.s	.BYT06
	bsr.s	.BYT0E
.BYT06	roxl.l	#1,d2
	dbf	d1,.BYT05
	move.b	d2,-(a2)
	dbf	d3,.BYT04
	bra.s	.BYT0C
.BYT07	moveq	#8,d1
	moveq	#8,d4
	bra.s	.BYT03
.BYT08	moveq	#2,d1
	bsr.s	.BYT0F
	cmp.b	#2,d2
	blt.s	.BYT09
	cmp.b	#3,d2
	beq.s	.BYT07
	moveq	#8,d1
	bsr.s	.BYT0F
	move.w	d2,d3
	move.w	#$C,d1
	bra.s	.BYT0A
.BYT09	move.w	#9,d1
	add.w	d2,d1
	addq.w	#2,d2
	move.w	d2,d3
.BYT0A	bsr.s	.BYT0F
.BYT0B	subq.w	#1,a2
	move.b	0(a2,d2.W),(a2)
	dbf	d3,.BYT0B
.BYT0C	cmp.l	a2,a1
	blt.s	.BYT00
	tst.l	d5
	beq.s	.BYT0D
	nop
.BYT0D	rts
.BYT0E	move.l	-(a0),d0
	eor.l	d0,d5
	move	#$10,CCR
	roxr.l	#1,d0
	rts
.BYT0F	subq.w	#1,d1
	clr.w	d2
.BYT10	lsr.l	#1,d0
	bne.s	.BYT11
	move.l	-(a0),d0
	eor.l	d0,d5
	move	#$10,CCR
	roxr.l	#1,d0
.BYT11	roxl.l	#1,d2
	dbf	d1,.BYT10
	rts

; Gollum depacker

gollum_depacker_huff
.GHF01	bra.s	.GHF05
.GHF03	dcb.w	2,0
.upl	dc.l	0
.GHF05	pea	.GHF01(PC)
	lea	698(a0),a1
	move.l	a1,save_address
	move.l	144(a0),unpacked_length
	move.l	144(a0),.upl
	move.l	length_read(pc),.GHF00
	lea	698(a0),a0
	move.l	a0,-(a7)
	move.l	.upl(PC),d0
	addq.l	#8,d0
	add.l	d0,a0
	move.l	a0,-(a7)
	add.l	d0,a0
	lea	.GHF03(PC),a1
	move.l	a0,(a1)
	move.l	4(a7),a4
	lea	$100(a4),a2
	lea	$300(a4),a0
	move.l	(a7),a1
	move.l	.GHF03(PC),a6
	moveq	#0,d0
	moveq	#0,d2
	moveq	#0,d1
.GHF06	move.w	(a2)+,d1
	moveq	#$10,d2
	sub.b	(a4)+,d2
	bmi.s	.GHF08
	moveq	#1,d3
	lsl.l	d2,d3
	lea	0(a6,d1.L),a5
.GHF07	move.b	d0,(a5)+
	subq.l	#1,d3
	bne.s	.GHF07
.GHF08	addq.w	#1,d0
	cmp.w	#$100,d0
	bne.s	.GHF06
	move.l	.GHF03(PC),a5
	move.l	4(a7),a4
	lea	$100(a4),a2
	move.l	(a7),a1
	lea	$300(a4),a0
	move.l	4(a7),a6
	move.l	.GHF00(PC),d0
	add.l	d0,a6
	moveq	#0,d0
	moveq	#0,d1
	moveq	#0,d3
	move.w	(a0),d0
.GHF09	move.b	0(a5,d0.L),d1
	move.b	d1,(a1)+
	add.b	0(a4,d1.W),d3
	cmp.w	#$10,d3
	bcs.s	.GHF0A
	addq.l	#2,a0
	sub.w	#$10,d3
.GHF0A	move.l	(a0),d0
	lsl.l	d3,d0
	clr.w	d0
	swap	d0
	cmp.l	a6,a0
	bcs.s	.GHF09
	move.l	4(a7),d0
	move.l	(a7),4(a7)
	move.l	d0,(a7)
	move.l	4(a7),a0
	move.l	(a7),a1
	lea	.GHF11(PC),a3
	move.l	a1,d2
.GHF0B	clr.w	d0
	move.b	(a0)+,d0
	move.w	d0,d1
	subq.w	#1,d0
	bmi.s	.GHF0D
.GHF0C	move.b	(a0)+,(a1)+
	dbf	d0,.GHF0C
	cmp.w	#$FF,d1
	beq.s	.GHF0B
.GHF0D	moveq	#0,d0
	move.b	(a0)+,(a3)
	bmi.s	.GHF0F
	move.b	(a0)+,1(a3)
	move.w	(a3),d0
	lsr.w	#5,d0
	tst.w	d0
	beq.s	.EXIT
	move.l	a1,a4
	suba.w	d0,a4
	move.w	(a3),d0
	andi.w	#$1F,d0
	move.b	(a4)+,(a1)+
	move.b	(a4)+,(a1)+
	move.b	(a4)+,(a1)+
.GHF0E	move.b	(a4)+,(a1)+
	dbf	d0,.GHF0E
	bra.s	.GHF0B
.GHF0F	move.b	(a0)+,1(a3)
	move.w	(a3),d0
	andi.w	#$7FFF,d0
	move.l	a1,a4
	suba.w	d0,a4
	moveq	#0,d0
	move.b	(a0)+,d0
	move.b	(a4)+,(a1)+
	move.b	(a4)+,(a1)+
	move.b	(a4)+,(a1)+
.GHF10	move.b	(a4)+,(a1)+
	dbf	d0,.GHF10
	bra.s	.GHF0B
.GHF11	dcb.w	2,0
.EXIT	lea	12(a7),a7
	rts
.GHF00	dc.l	0

; Gollum depacker

gollum_depacker
.GOL00	bra.s	.GOL04
.GOL02	dcb.w	2,0
.GOL03	dc.l	0

.GOL04	move.l	144(a0),.GOL03
	move.l	144(a0),unpacked_length
	lea	564(a0),a0
	pea	.GOL00(PC)
	move.l	a0,-(a7)
	move.l	.GOL03(PC),d0
	addq.l	#8,d0
	add.l	d0,a0
	move.l	a0,-(a7)
	add.l	d0,a0
	lea	.GOL02(PC),a1
	move.l	a0,(a1)
	move.l	4(a7),a0
	move.l	(a7),a1
	lea	.GOL0C(PC),a3
	move.l	a1,d2
.GOL05	clr.w	d0
	move.b	(a0)+,d0
	move.w	d0,d1
	subq.w	#1,d0
	bmi.s	.GOL07
.GOL06	move.b	(a0)+,(a1)+
	dbf	d0,.GOL06
	cmp.w	#$FF,d1
	beq.s	.GOL05
.GOL07	moveq	#0,d0
	move.b	(a0)+,(a3)
	bmi.s	.GOL09
	move.b	(a0)+,1(a3)
	move.w	(a3),d0
	lsr.w	#5,d0
	tst.w	d0
	beq.s	.exit
	move.l	a1,a4
	suba.w	d0,a4
	move.w	(a3),d0
	andi.w	#$1F,d0
	move.b	(a4)+,(a1)+
	move.b	(a4)+,(a1)+
	move.b	(a4)+,(a1)+
.GOL08	move.b	(a4)+,(a1)+
	dbf	d0,.GOL08
	bra.s	.GOL05
.GOL09	move.b	(a0)+,1(a3)
	move.w	(a3),d0
	andi.w	#$7FFF,d0
	move.l	a1,a4
	suba.w	d0,a4
	moveq	#0,d0
	move.b	(a0)+,d0
	move.b	(a4)+,(a1)+
.GOL0A	move.b	(a4)+,(a1)+
	move.b	(a4)+,(a1)+
.GOL0B	move.b	(a4)+,(a1)+
	dbf	d0,.GOL0B
	bra.s	.GOL05
.GOL0C	dcb.w	2,0
.exit	lea	12(a7),a7
	rts

; Happy depacker
; I hate this, it was pulling so many values
; from the basepage that in the end I just
; build a dummy basepage for it to use. Oh and
; each program packed has a slightly different
; depacker which is very annoying.

happy_depacker
	move.l	#dat,save_address

	movem.l	776(a0),d0-1
	movem.l	d0-1,.HAP19
	lea	1456(a0),a1
	lea	.HAP34,a2
	move.l	#400-1,d0
.1	move.b	(a1)+,(a2)+
	dbf	d0,.1
	move.l	1430(a0),.upl
	move.l	1430(a0),unpacked_length
	move.l	1434(a0),.pkl
	lea	$1c(a0),a1
	move.l	a1,.p_start
	move.l	2(a0),.p_size
	add.l	2(a0),a1
	move.l	a1,.d_start
	move.l	6(a0),.d_size
	add.l	6(a0),a1
	move.l	a1,.b_start
	move.l	10(a0),.b_size
	bra.s	.begin

.basepage	dc.l	.basepage
.b1	dc.l	0
.p_start	dc.l	0
.p_size	dc.l	0
.d_start	dc.l	0
.d_size	dc.l	0
.b_start	dc.l	0
.b_size	dc.l	0

.begin	move.l	a7,a6
	lea	.basepage(pc),a5
	move.l	$18(a5),d0
.HAP07	bsr	.HAP22
	add.l	$1C(a5),d0
	add.l	#$100,d0
	addq.l	#1,d0
.HAP08	move.l	d0,a3
	move.l	d0,d7
	move.l	$18(a5),a4
	move.l	$14(a5),d0
.HAP09	move.b	-(a4),-(a3)
	subq.l	#1,d0
	bne.s	.HAP09
	move.l	$10(a5),a4
	lea	.HAP19(PC),a2
.HAP0A	move.b	(a3),d0
	cmp.b	1(a2),d0
	beq.s	.HAP0C
	cmp.b	3(a2),d0
	beq.s	.HAP0E
	cmp.b	5(a2),d0
	beq.s	.HAP10
	move.b	(a3)+,(a4)+
.HAP0B	cmp.l	a3,d7
	bcs.s	.HAP13
	bra.s	.HAP0A
.HAP0C	addq.l	#1,a3
	clr.w	d1
	move.b	(a3)+,d2
	move.b	(a3)+,d1
	subq.w	#1,d1
.HAP0D	move.b	d2,(a4)+
	dbf	d1,.HAP0D
	bra.s	.HAP0B
.HAP0E	addq.l	#1,a3
	clr.w	d1
	move.b	(a3)+,d1
	move.b	7(a2),d2
	subq.w	#1,d1
.HAP0F	move.b	d2,(a4)+
	dbf	d1,.HAP0F
	bra.s	.HAP0B
.HAP10	addq.l	#1,a3
	clr.l	d6
	clr.l	d5
	move.b	1(a3),d6
	subq.b	#1,d6
.HAP11	clr.w	d5
	move.b	(a3),d5
	subq.b	#1,d5
	lea	2(a3),a1
.HAP12	move.b	(a1)+,(a4)+
	dbf	d5,.HAP12
	dbf	d6,.HAP11
	clr.l	d6
	move.b	(a3),d6
	lea	2(a3),a3
	add.l	d6,a3
	bra.s	.HAP0B
.HAP13	lea	.HAP2A(PC),a0
.exit	rts

.HAP19	dc.b	$00,$0F,$00,$13,$00,$17,$00,$00

.HAP22	movem.l	a0-a6/D0-d7,-(a7)
	lea	.HAP2B(PC),a6
	move.l	d0,(a6)
	lea	.HAP2C(PC),a6
	lea	.HAP42(PC),a6
	move.l	a5,(a6)
	lea	.HAP2F(PC),a0
	move.l	a7,(a0)
	move.l	$18(a5),a0
	lea	.HAP31(PC),a6
	move.l	$14(a5),(a6)
	add.l	$1C(a5),a0
	lea	$2000(a0),a0
	lea	-$1388(a7),a7
	lea	.HAP32(PC),a6
	move.l	a7,(a6)
	lea	-$A(a7),a7
.HAP23	moveq	#7,d7
	lea	.HAP34(PC),a5
	move.w	#0,-(a7)
	lea	.HAP33(PC),a6
	clr.w	(a6)
	bsr	.HAP37
	addq.l	#2,a7
	bsr	.HAP40
	lea	.HAP30(PC),a6
	move.l	a2,(a6)
	move.l	a2,d0
	lea	.HAP31(PC),a6
	add.l	d0,(a6)
	addq.l	#2,(a6)
	moveq	#7,d7
	move.l	a2,a5
	move.l	.HAP32(PC),a2
	lea	dat(PC),a4
	lea	.HAP31(PC),a6
	move.l	(a6),a3
.HAP24	cmp.l	a5,a3
	bls.s	.HAP29
	move.l	a2,a0
.HAP25	cmp.w	#0,(a0)
	beq.s	.HAP28
	bsr	.HAP3C
	tst.w	d0
	beq.s	.HAP26
	move.w	4(a0),d1
	bra.s	.HAP27
.HAP26	move.w	2(a0),d1
.HAP27	and.l	#$FFFF,d1
	lsl.l	#3,d1
	move.l	a2,a0
	add.l	d1,a0
	bra.s	.HAP25
.HAP28	move.b	7(a0),(a4)+
	bra.s	.HAP24
.HAP29	lea	.HAP2C(PC),a6
	lea	.HAP2B(PC),a6
	lea	.HAP42(PC),a1
	move.l	(a1),a0
	move.l	$10(a0),d0
	lea	.pkl(PC),a6
	move.l	(a6),$14(a0)
	add.l	$14(a0),d0
	move.l	d0,$18(a0)
	lea	.HAP2F(PC),a6
	move.l	(a6),a7
	movem.l	(a7)+,a0-a6/D0-d7
	move.l	$18(a5),d0
	rts

.HAP2A	dc.b	$00,$00
.HAP2B	dcb.w	2,0
.HAP2C	dc.b	$00,$00
.upl	dc.b	$00,$00,$09,$C5
.pkl	dc.b	$00,$00,$08,$B4
.HAP2F	dcb.w	2,0
.HAP30	dcb.w	2,0
.HAP31	dcb.w	2,0
.HAP32	dcb.w	2,0
.HAP33	dc.b	$00,$00

.HAP34	ds.b	400

.HAP37	bsr.s	.HAP3C
	tst.b	d0
	beq.s	.HAP38
	lea	.HAP32(PC),a6
	move.l	(a6),a0
	move.w	4(a7),d0
	and.l	#$FFFF,d0
	lsl.l	#3,d0
	add.l	d0,a0
	move.w	#-1,(a0)
	lea	.HAP33(PC),a6
	addq.w	#1,(a6)
	move.w	(a6),2(a0)
	move.l	a0,-(a7)
	move.w	2(a0),-(a7)
	bsr.s	.HAP37
	addq.l	#2,a7
	move.l	(a7)+,a0
	lea	.HAP33(PC),a6
	addq.w	#1,(a6)
	move.w	(a6),4(a0)
	move.w	4(a0),-(a7)
	bsr.s	.HAP37
	addq.l	#2,a7
	rts
.HAP38	lea	.HAP32(PC),a6
	move.l	(a6),a0
	move.w	4(a7),d0
	and.l	#$FFFF,d0
	lsl.l	#3,d0
	add.l	d0,a0
	move.w	#0,(a0)
	moveq	#7,d1
	clr.w	d2
.HAP39	bsr.s	.HAP3C
	tst.w	d0
	beq.s	.HAP3A
	bset	d1,d2
	bra.s	.HAP3B
.HAP3A	bclr	d1,d2
.HAP3B	subq.w	#1,d1
	cmp.w	#-1,d1
	bne.s	.HAP39
	move.w	d2,6(a0)
	rts
.HAP3C	btst	d7,(a5)
	beq.s	.HAP3D
	move.w	#-1,d0
	bra.s	.HAP3E
.HAP3D	clr.w	d0
.HAP3E	subq.w	#1,d7
	cmp.w	#-1,d7
	beq.s	.HAP3F
	rts

.HAP3F	moveq	#7,d7
	addq.l	#1,a5
	rts

.HAP40	lea	.HAP42(PC),a0
	move.l	(a0),a1
	move.l	a1,a0
	move.l	$10(a0),a1
	move.l	a1,a2
	add.l	$14(a0),a1
	move.l	$14(a0),d0
	lea	.upl(PC),a0
	add.l	(a0),a2
	move.l	(a0),d1
	lsr.l	#3,d1
	add.l	d1,a2
	addq.l	#1,a2
	addq.l	#1,a1
	addq.l	#1,d0
.HAP41	move.b	-(a1),-(a2)
	subq.l	#1,d0
	bne.s	.HAP41
	rts

.HAP42	dcb.w	2,0

; spectrum 512 picture depacker

spec_depack
	lea	load_address+12,a0
	move.l	a0,a1
	add.l	length_read,a1
	lea	2000(a1),a1
	move.l	a1,d0
	clr.b	d0
	move.l	d0,a1
	move.l	a1,save_address
	move.l	#51104,unpacked_length
	move.l	a1,a5
	move.w	#1,d1
	move.w	#7,d7
	lea	$7D00(a1),a2
	lea	8(a2),a3
.spec00	lea	-$7C60(a2),a1
.spec01	move.b	(a0)+,d6
	ext.w	d6
	bmi.s	.spec03
.spec02	move.b	(a0)+,(a1)
	adda.w	d1,a1
	exg	d7,d1
	dbf	d6,.spec02
	bra.s	.spec05
.spec03	neg.w	d6
	addq.w	#1,d6
	move.b	(a0)+,d0
.spec04	move.b	d0,(a1)
	adda.w	d1,a1
	exg	d7,d1
	dbf	d6,.spec04
.spec05	cmpa.l	a2,a1
	bcs.s	.spec01
	bne.s	.exit
	addq.l	#2,a2
	cmpa.l	a3,a2
	bcs.s	.spec00

	lea	load_address+12,a0
	add.l	load_address+4,a0
	move.l	a5,a1
	lea	32000(a1),a1
	move.w	#$254,d7
	clr.w	d0
.spec06	move.w	#$D,d6
	move.w	(a0)+,d1
	lsr.w	#1,d1
	move.w	d0,(a1)+
.spec07	lsr.w	#1,d1
	bcc.s	.spec08
	move.w	(a0)+,(a1)+
	dbf	d6,.spec07
	bra.s	.spec09
.spec08	move.w	d0,(a1)+
	dbf	d6,.spec07
.spec09	move.w	d0,(a1)+
	dbf	d7,.spec06
.exit	rts

; degas unpacker

degas_depack
	move.w	(a0),d0
	and.w	#$00ff,d0
	move.w	d0,res
	lea	34(a0),a1		; start of file
	add.l	length_read(pc),a0
	lea	320(a0),a4
	move.l	a4,d0
	clr.b	d0
	move.l	d0,a4		; depack to here
	move.l	a4,-(a7)
	bsr.s	.dp_degas
	moveq	#21,d0
.1	move.b	(a1)+,(a4)+	; copy colour cycle info
	dbf	d0,.1
	move.l	(a7)+,a4
	move.l	#32066,unpacked_length
	lea	load_address+34(pc),a1
	moveq	#16,d0
.copy	move.w	-(a1),-(a4)	; copy in pallete
	dbf	d0,.copy
	clr.b	(a4)		; set up header
	move.l	a4,save_address
	rts

.dp_degas	move.l	#399,d1
	cmp.w	#2,res
	beq.s	.loop
	move.l	#199,d1		; 200 scanlines

.loop	lea	buffer(pc),a0
	moveq	#-$80,d5
	cmp.w	#2,res
	bge.s	.dep00
	move.w	#$A0,d4
	bra.s	.dep01
.dep00	moveq	#$50,d4
.dep01	bra.s	.dep06
.dep02	move.b	(a1)+,d7
	tst.b	d7
	blt.s	.dep04
	addq.b	#1,d7
.dep03	move.b	(a1)+,(a0)+
	subq.w	#1,d4
	subq.b	#1,d7
	bne.s	.dep03
	bra.s	.dep06
.dep04	move.b	d7,d0
	ext.w	d0
	cmp.w	d5,d0
	beq.s	.dep06
	move.b	d7,d0
	ext.w	d0
	neg.w	d0
	move.b	d0,d6
	addq.b	#1,d6
	move.b	(a1)+,d7
.dep05	move.b	d7,(a0)+
	subq.w	#1,d4
	subq.b	#1,d6
	bne.s	.dep05
.dep06	tst.w	d4
	bgt.s	.dep02
	movem.l	a0-a1,-(a7)
	bsr.s	biddle
	movem.l	(a7)+,a0-a1
	dbf	d1,.loop
	rts

biddle	lea	buffer(pc),a6	; rearrange a scanline
	tst.w	res
	bne.s	.rear02
	move.l	a6,b_a_store
	lea	$28(a6),a2
	lea	$50(a6),a3
	lea	$78(a6),a5
	moveq	#0,d7
	bra.s	.rear01
.rear00	move.l	a4,a0
	move.l	b_a_store(pc),a1
	move.w	(a1),(a0)
	addq.l	#2,b_a_store
	addq.l	#2,a4
	move.l	a4,a0
	move.l	a2,a1
	move.w	(a1),(a0)
	addq.l	#2,a2
	addq.l	#2,a4
	move.l	a4,a0
	move.l	a3,a1
	move.w	(a1),(a0)
	addq.l	#2,a3
	addq.l	#2,a4
	move.l	a4,a0
	move.l	a5,a1
	move.w	(a1),(a0)
	addq.l	#2,a5
	addq.l	#2,a4
	addq.w	#1,d7
.rear01	cmp.w	#$14,d7
	blt.s	.rear00
	bra.s	.exit
.rear02	cmp.w	#1,res
	bne.s	.rear05
	move.l	a6,b_a_store
	lea	$50(a6),a2
	clr.w	d7
	bra.s	.rear04
.rear03	move.l	a4,a0
	move.l	b_a_store(pc),a1
	move.w	(a1),(a0)
	addq.l	#2,b_a_store
	addq.l	#2,a4
	move.l	a4,a0
	move.l	a2,a1
	move.w	(a1),(a0)
	addq.l	#2,a2
	addq.l	#2,a4
	addq.w	#1,d7
.rear04	cmp.w	#$28,d7
	blt.s	.rear03
	bra.s	.exit
.rear05	clr.w	d7
	bra.s	.rear07
.rear06	move.l	a4,a0
	move.l	a6,a1
	move.w	(a1),(a0)
	addq.l	#2,a6
	addq.l	#2,a4
	addq.w	#1,d7
.rear07	cmp.w	#$28,d7
	blt.s	.rear06
.exit	rts

**********************************************************************

	data
nul	dc.w	0
old_stack	dc.l	0
adr0	dcb.w	39,0
	dc.b	0

adr1	dc.b	0,0,0,0,0,0,0,0,0
	dcb.w	28,0

params	dc.l	ctrl_test
	dc.l	ctrl1
	dc.l	ctrl2
	dc.l	ctrl3
	dc.l	ctrl4
	dc.l	ctrl5

load	dc.b	'Select file to depack',0
save	dc.b	'Select filename to save to',0
	even
ctrl_test	dc.b	0,$0a,0,0,0,$01,0,$0
function	equ	*+1
ctrl0	dc.b	0,$5a,0,0,0,$02,0,$03
ctrl1	dc.b	$01,' ',0,$01
ctrl2	dc.b	0,0
ctrl3	dc.b	0,$01
flag	dcb.w	2,0
ctrl4	dc.l	adr0
	dc.l	adr1
title	dc.l	0
ctrl5	dc.w	0
	ds.l	4
fil_extend
	dc.b	'*.*',0
save_extend
	dcb.w	2,0

; pack files info

ATOM_v3.3_packed
	dc.b	'This is an ATOM v3.3 packed file',13,10,0
ATOM_v3.5_packed
	dc.b	'This is an ATOM v3.5 packed file',13,10,0
ATOM_v3.3_packed_prog
	dc.b	'This is an ATOM v3.3 packed program file',13,10,0
ATOM_v3.5_packed_prog
	dc.b	'This is an ATOM v3.5 packed program file',13,10,0
FIRE_packed
	dc.b	'This is a FIRE packed file',13,10,0
FIRE_packed_prog
	dc.b	'This is a FIRE packed program file',13,10,0
ICE_packed
	dc.b	'This is an ICE 2.3/2.4 packed file',13,10,0
ICE_23_packed_prog
	dc.b	'This is an ICE 2.3 packed program file',13,10,0
ICE_24_packed_prog
	dc.b	'This is an ICE 2.4 packed program file',13,10,0
olde_ICE_packed_prog
	dc.b	'This is a pre ICE 2.3 packed program file',13,10,0
olde_ICE_packed_dat
	dc.b	'This is a pre ICE 2.3 packed datafile',13,10,0
JAM_1_prog
	dc.b	'This is a JAM packer 1 packed program',13,10,0
JAM4_v2_prog
	dc.b	'This is a JAM packer 4 program packed with v2 method',13,10,0
JAM3_v2_prog
	dc.b	'This is a JAM packer 3 program packed with v2 method',13,10,0
JAM3_LZH_prog
	dc.b	'This is a JAM packer 3 program packed with LZH method',13,10,0
JAM4_LZH_prog
	dc.b	'This is a JAM packer 4 program packed with LZH method',13,10,0
JAM4_ICE_prog
	dc.b	'This is a JAM packer 4 program packed with ICE method',13,10,0
LZH_DAT
	dc.b	'This is a JAM packer 3/4 datafile packed with LZH method',13,10,0
LZW_DAT
	dc.b	'This is a JAM packer 4 datafile packed with LZW method',13,10,0
JAM4_LZW_prog
	dc.b	'This is a JAM packer 4 program packed with LZW method',13,10,0
auto_23r_prog
	dc.b	'This is an automation v2.3r packed program',13,10,0
auto_251_prog
	dc.b	'This is an automation v2.51 packed program',13,10,0
auto_chunky
	dc.b	'This is an automation chunk packed file',13,10,0
auto_501_prog
	dc.b	'This is an automation v5.01 packed program',13,10,0
auto_501_dat
	dc.b	'This is an automation v5.01 packed datafile',13,10,0
auto_DAT_file
	dc.b	'This is an automation v2.3-2.5 packed datafile',13,10,0
LSD_12_prog
	dc.b	'This is an LSD v1.2 packed program',13,10,0
JEK_12d_prog
	dc.b	'This is a JEK v1.2d packed program',13,10,0
LSD_dat
	dc.b	'This is an LSD packed datafile',13,10,0
JEK_dat
	dc.b	'This is a JEK/JAM v1 packed datafile',13,10,0
pp_dat
	dc.b	'This is a Pompey packed datafile',13,10,0
pp_prog
	dc.b	'This is a Pompey packed progfile',13,10,0
bkill_prog
	dc.b	'This is a bytekiller packed program',13,10,0
DCS_prog
	dc.b	'This is a DCSquish packed program',13,10,0
_4pak_prog
	dc.b	'This is a 4pak packed program',13,10,0
gollum_prog_huff
	dc.b	'This is a gollum packed program with huffmans encoding',13,10,0
gollum_prog
	dc.b	'This is a gollum packed program',13,10,0
happy_prog
	dc.b	'This is a happy packed program',13,10,0
spec512_pic
	dc.b	'This is a spectrum512 packed picture file',13,10,0
degas_pic
	dc.b	'This is a degas packed picture file',13,10,0
prog_file_np
	dc.b	'This is a program file and is most likely unpacked',13,10,0
prog_file_mp
	dc.b	'This is a program file and might be packed',13,10,0
DAT_file	dc.b	'This is a datafile but is either unpacked or',13,10
	dc.b	$1B,'Lhas been packed with an unknown packer',13,10,0

ascii_header
	dc.b	$1B,'Lfile header is ""'
header	ds.b	4

	dc.b	'" which indicates file is possibly packed',13,10,0

not_ascii
	dc.b	$1b,'LFile header indicates that it is probably unpacked',13,10,0

; assorted messages

out_mem	dc.b	$1B,'LSorry, out of memory!',13,10,0
dpm	dc.b	$1B,'LPlease wait, depacking',13,10,0
loading	dc.b	$1B,'EPlease wait, loading',13,10,0
saving	dc.b	'Please wait, saving',13,10,0
a_k_t_c	dc.b	$1B,'LPress any key to continue',0
error_mess
	dc.b	$1B,'LOOPS! file error!',13,10,0
wrong_res	dc.b	$1B,'ESorry,medium or high res only!',0
	even
	bss
bss_start
blank	ds.l	8
save_address
	ds.l	1
length_read
	ds.l	1
not_packed
	ds.l	1
unpacked_length
	ds.l	1
rw_error	ds.b	2
filename	ds.w	33
handle	ds.b	2

	ds.l	$2000/4		; space required 'coz some depackers
	; use a lot of stack space esp jam3
my_stack	ds.l	2
	ds.l	100
buf_adr	ds.l	$100
buffer	ds.l	160/4
c2	ds.l	1
dps	ds.l	1
res	ds.w	1
b_a_store	ds.l	1
dat	equ	*+$898
load_address
	ds.l	8000
