;	INCLUDE '../mem_map.inc'

;	ORG   Depacker_start

; DeHRUSTer v1.2
;	HL - начало архива
;	DE - адрес распаковки

;--------------[DEPACKER]---------------
DEHRUST:
	PUSH DE
	PUSH HL
	INC HL
	INC HL
	LD C,(HL)
	INC HL
	LD B,(HL)
	INC HL
	DEC BC
	EX DE,HL
	ADD HL,BC
	EX DE,HL
	LD C,(HL)
	INC HL
	LD B,(HL)
	DEC BC
	POP HL
	ADD HL,BC
	SBC HL,DE
	ADD HL,DE
	JR C,.LL4019
	LD D,H
	LD E,L
.LL4019:
	LDDR 
	EX DE,HL
	POP DE
	LD C,#0C
	ADD HL,BC
	PUSH HL
	POP IX
	LD A,#03
.LL4025:
	DEC HL
	LD B,(HL)
	DEC HL
	LD C,(HL)
	PUSH BC
	DEC A
	JR NZ,.LL4025
	LD B,A
	EXX 
	LD D,#BF
	LD C,#10
	CALL .LL4115
.LL4036:
	LD A,(IX+#00)
	INC IX
	EXX 
.LL403C:
	LD (DE),A
	INC DE
.LL403E:
	EXX 
.LL403F:
	ADD HL,HL
	DJNZ .LL4045
	CALL .LL4115
.LL4045:
	JR C,.LL4036
	LD E,#01
.LL4049:
	LD A,#80
.LL404B:
	ADD HL,HL
	DJNZ .LL4051
	CALL .LL4115
.LL4051:
	RLA 
	JR C,.LL404B
	CP #03
	JR C,.LL405D
	ADD A,E
	LD E,A
	XOR C
	JR NZ,.LL4049
.LL405D:
	ADD A,E
	CP #04
	JR Z,.LL40C4
	ADC A,#FF
	CP #02
	EXX 
.LL4067:
	LD C,A
.LL4068:
	EXX 
	LD A,#BF
	JR C,.LL4082
.LL406D:
	ADD HL,HL
	DJNZ .LL4073
	CALL .LL4115
.LL4073:
	RLA 
	JR C,.LL406D
	JR Z,.LL407D
	INC A
	ADD A,D
	JR NC,.LL4084
	SUB D
.LL407D:
	INC A
	JR NZ,.LL408D
	LD A,#EF
.LL4082:
	RRCA 
	CP A
.LL4084:
	ADD HL,HL
	DJNZ .LL408A
	CALL .LL4115
.LL408A:
	RLA 
	JR C,.LL4084
.LL408D:
	EXX 
	LD H,#FF
	JR Z,.LL409B
	LD H,A
	INC A
	LD A,(IX+#00)
	INC IX
	JR Z,.LL40A6
.LL409B:
	LD L,A
	ADD HL,DE
	LDIR 
.LL409F:
	JR .LL403E
.LL40A1:
	EXX 
	RRC D
	JR .LL403F
.LL40A6:
	CP #E0
	JR C,.LL409B
	RLCA 
	XOR C
	INC A
	JR Z,.LL40A1
	SUB #10
.LL40B1:
	LD L,A
	LD C,A
	LD H,#FF
	ADD HL,DE
	LDI 
	LD A,(IX+#00)
	INC IX
	LD (DE),A
	INC HL
	INC DE
	LD A,(HL)
	JP .LL403C
.LL40C4:
	LD A,#80
.LL40C6:
	ADD HL,HL
	DJNZ .LL40CC
	CALL .LL4115
.LL40CC:
	ADC A,A
	JR NZ,.LL40F3
	JR C,.LL40C6
	LD A,#FC
	JR .LL40F6
.LL40D5:
	LD B,A
	LD C,(IX+#00)
	INC IX
	CCF 
	JR .LL4068
.LL40DE:
	CP #0F
	JR C,.LL40D5
	JR NZ,.LL4067
	LD B,#03
	EX DE,HL
.LL40E7:
	POP DE
	LD (HL),E
	INC HL
	LD (HL),D
	INC HL
	DJNZ .LL40E7
	LD HL,#2758
	EXX 
	RET 
.LL40F3:
	SBC A,A
	LD A,#EF
.LL40F6:
	ADD HL,HL
	DJNZ .LL40FC
	CALL .LL4115
.LL40FC:
	RLA 
	JR C,.LL40F6
	EXX 
	JR NZ,.LL40B1
	BIT 7,A
	JR Z,.LL40DE
	SUB #EA
	ADD A,A
	LD B,A
.LL410A:
	LD A,(IX+#00)
	INC IX
	LD (DE),A
	INC DE
	DJNZ .LL410A
	JR .LL409F
.LL4115:
	LD B,C
	LD L,(IX+#00)
	INC IX
	LD H,(IX+#00)
	INC IX
	RET
Length		EQU	$-DEHRUST

	in a,(#FF)

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

;---------------[MAIN.HR]---------------
;Depacker.packcode	EQU	$$$
;PackedMAIN			EQU	$
;		INCBIN 'Build/Bin/temp/MAIN.HR'
;---------------------------------------
;