Skip to content

Commit 2041022

Browse files
committed
Add MegaLZ bas library
1 parent 518dccc commit 2041022

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

docs/library/megalz.bas.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#MegaLZ.bas
2+
3+
# megaLZDepack.bas
4+
5+
This routine takes a block of data compressed with the [MegaLZ](http://lvd.nm.ru/MegaLZ/) compression
6+
algorithm at SOURCE location and decompresses it to DESTINATIOn location.
7+
You should probably save compressed files as direct binaries, and use asm mode incbin commands
8+
to include the binary into your project.
9+
10+
THIS METHOD IS NOW INCLUDED IN THE ZX BASIC EXTERNAL LIBRARY (the library folder),
11+
so can and should be included with `#include <megalz.bas>` for the latest code.
12+
13+
```
14+
SUB megaLZDepack (source as uInteger, dest as uInteger)
15+
ASM
16+
LD E,(IX+6)
17+
LD D,(IX+7)
18+
19+
;Z80 depacker for megalz V4 packed files (C) fyrex^mhm
20+
21+
; DESCRIPTION:
22+
;
23+
; Depacker is fully relocatable, not self-modifying,
24+
;it's length is 110 bytes starting from DEC40.
25+
;Register usage: AF,AF',BC,DE,HL. Must be CALL'ed, return is done by RET.
26+
;Provide extra stack location for store 2 bytes (1 word). Depacker does not
27+
;disable or enable interrupts, as well as could be interrupted at any time
28+
;(no f*cking wicked stack usage :).
29+
30+
; USAGE:
31+
;
32+
; - put depacker anywhere you want,
33+
; - put starting address of packed block in HL,
34+
; - put location where you want data to be depacked in DE,
35+
; (much like LDIR command, but without BC)
36+
; - make CALL to depacker (DEC40).
37+
; - enjoy! ;)
38+
39+
; PRECAUTIONS:
40+
;
41+
; Be very careful if packed and depacked blocks coincide somewhere in memory.
42+
;Here are some advices:
43+
;
44+
; 1. put packed block to the highest addresses possible.
45+
; Best if last byte of packed block has address #FFFF.
46+
;
47+
; 2. Leave some gap between ends of packed and depacked block.
48+
; For example, last byte of depacked block at #FF00,
49+
; last byte of packed block at #FFFF.
50+
;
51+
; 3. Place nonpackable data to the end of block.
52+
;
53+
; 4. Always check whether depacking occurs OK and neither corrupts depacked data
54+
; nor hangs computer.
55+
;
56+
57+
;DEC40
58+
59+
LD A,80h
60+
EX AF,AF'
61+
MS: LDI
62+
M0: LD BC,2FFh
63+
M1: EX AF,AF'
64+
M1X: ADD A,A
65+
JR NZ,M2
66+
LD A,(HL)
67+
INC HL
68+
RLA
69+
M2: RL C
70+
JR NC,M1X
71+
EX AF,AF'
72+
DJNZ X2
73+
LD A,2
74+
SRA C
75+
JR C,N1
76+
INC A
77+
INC C
78+
JR Z,N2
79+
LD BC,33Fh
80+
JR M1
81+
82+
X2: DJNZ X3
83+
SRL C
84+
JR C,MS
85+
INC B
86+
JR M1
87+
X6:
88+
ADD A,C
89+
N2:
90+
LD BC,4FFh
91+
JR M1
92+
N1:
93+
INC C
94+
JR NZ,M4
95+
EX AF,AF'
96+
INC B
97+
N5: RR C
98+
JP C, END_DEC40
99+
RL B
100+
ADD A,A
101+
JR NZ,N6
102+
LD A,(HL)
103+
INC HL
104+
RLA
105+
N6: JR NC,N5
106+
EX AF,AF'
107+
ADD A,B
108+
LD B,6
109+
JR M1
110+
X3:
111+
DJNZ X4
112+
LD A,1
113+
JR M3
114+
X4: DJNZ X5
115+
INC C
116+
JR NZ,M4
117+
LD BC,51Fh
118+
JR M1
119+
X5:
120+
DJNZ X6
121+
LD B,C
122+
M4: LD C,(HL)
123+
INC HL
124+
M3: DEC B
125+
PUSH HL
126+
LD L,C
127+
LD H,B
128+
ADD HL,DE
129+
LD C,A
130+
LD B,0
131+
LDIR
132+
POP HL
133+
JR M0
134+
135+
END_DEC40:
136+
END ASM
137+
END SUB
138+
```
139+
140+
141+
## Usage
142+
Example:
143+
```
144+
megaLZDepack (32768,16384)
145+
```

0 commit comments

Comments
 (0)