-
Notifications
You must be signed in to change notification settings - Fork 2
/
dzx0_fastrom.asm
285 lines (225 loc) · 7.95 KB
/
dzx0_fastrom.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
;
; Speed-optimized ZX0 decompressor by spke, Without self-modified code by uniabis (181 bytes)
;
; ver.00 by spke (27/01-23/03/2021, 191 bytes)
; ver.01 by spke (24/03/2021, 193(+2) bytes - fixed a bug in the initialization)
; ver.01patch2 by uniabis (25/03/2021, 191(-2) bytes - fixed a bug with elias over 8bits)
; ver.01patch5 by uniabis (29/03/2021, 191 bytes - a bit faster)
; ver.01rom by uniabis (07/06/2021, 184(-7) bytes - support for ROM, but slower than the "Turbo")
; ver.01rom5 by uniabis (16/08/2021, 180(-4) bytes - support for ROM, but slower than the "Fast")
; ver.01rom7 by uniabis (10/09/2021, 181(+1) bytes - support for new v2 format)
;
; Original ZX0 decompressors were written by Einar Saukas
;
; This decompressor was written on the basis of "Standard" decompressor by
; Einar Saukas and optimized for speed by spke. This decompressor is
; about 5% faster than the "Turbo" decompressor, which is 128 bytes long.
; It has about the same speed as the 412 bytes version of the "Mega" decompressor.
;
; The decompressor uses AF, BC, DE, HL and IX and IY(optional).
;
; The decompression is done in the standard way:
;
; ld hl,FirstByteOfCompressedData
; ld de,FirstByteOfMemoryForDecompressedData
; call DecompressZX0
;
; Of course, ZX0 compression algorithms are (c) 2021 Einar Saukas,
; see https://github.com/einar-saukas/ZX0 for more information
;
; Drop me an email if you have any comments/ideas/suggestions: [email protected]
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
; +4byte, bit faster
DEFINE AllowUsingIY
; +-0bytes, bit slower, support for hd64180/z180
;DEFINE HD64180
DecompressZX0:
IFDEF AllowUsingIY
ld iy, CopyMatch1
ENDIF
ld ix, $ffff
ld bc, $0001
scf
jr RunOfLiteralsEntry
ShorterOffsets:
IFDEF HD64180
ld b, $ff ; the top byte of the offset is always $FF
ELSE
ld ixh, $ff ; the top byte of the offset is always $FF
ENDIF
ld c, (hl)
inc hl
rr c
IFDEF HD64180
push bc
pop ix
ELSE
ld ixl, c
ENDIF
jr nc, LongerMatch
CopyMatch2: ; the case of matches with len=2
ld bc, 2
; the faster match copying code
CopyMatch1:
push ix ; preserve offset
ex (sp), hl ; preserve source
add hl, de ; HL = dest - offset
ldir
pop hl ; restore source
; after a match you can have either
; 0 + <elias length> = run of literals, or
; 1 + <elias offset msb> + [7-bits of offset lsb + 1-bit of length] + <elias length> = another match
AfterMatch1:
add a, a
jr nc, RunOfLiterals
UsualMatch: ; this is the case of usual match+offset
add a, a
jr nc, LongerOffets
jr nz, ShorterOffsets ; NZ after NC == "confirmed C"
ld a, (hl) ; reload bits
inc hl
rla
jr c, ShorterOffsets
LongerOffets:
ld c, $fe
add a, a ; inline read gamma
rl c
add a, a
jr nc, $-4
call z, ReloadReadGamma
ProcessOffset:
inc c
ret z ; end-of-data marker (only checked for longer offsets)
rr c
IFDEF HD64180
ld b, c
ld c, (hl)
inc hl
rr c
push bc
pop ix
ELSE
ld ixh, c
ld c, (hl)
inc hl
rr c
ld ixl, c
ENDIF
; lowest bit is the first bit of the gamma code for length
jr c, CopyMatch2
LongerMatch:
ld bc, 1
add a, a ; inline read gamma
rl c
add a, a
jr nc, $-4
call z,ReloadReadGamma
CopyMatch3:
push ix ; preserve offset
ex (sp), hl ; preserve source
add hl, de ; HL = dest - offset
; because BC>=3-1, we can do 2 x LDI safely
ldi
ldir
inc c
ldi
pop hl ; restore source
; after a match you can have either
; 0 + <elias length> = run of literals, or
; 1 + <elias offset msb> + [7-bits of offset lsb + 1-bit of length] + <elias length> = another match
AfterMatch3:
add a, a
jr c, UsualMatch
RunOfLiterals:
inc c
add a, a
jr nc, LongerRun
jr nz, CopyLiteral ; NZ after NC == "confirmed C"
RunOfLiteralsEntry:
ld a, (hl) ; reload bits
inc hl
rla
jr c, CopyLiteral
LongerRun:
add a, a ; inline read gamma
rl c
add a, a
jr nc, $-4
jr nz, CopyLiterals
ld a, (hl) ; reload bits
inc hl
rla
call nc, ReadGammaAligned
CopyLiterals:
ldi
CopyLiteral:
ldir
; after a literal run you can have either
; 0 + <elias length> = match using a repeated offset, or
; 1 + <elias offset msb> + [7-bits of offset lsb + 1-bit of length] + <elias length> = another match
add a, a
jr c, UsualMatch
RepMatch:
inc c
add a, a
jr nc, LongerRepMatch
jr nz, CopyMatch1 ; NZ after NC == "confirmed C"
ld a, (hl) ; reload bits
inc hl
rla
jr c, CopyMatch1
LongerRepMatch:
add a, a ; inline read gamma
rl c
add a, a
jr nc, $-4
IFDEF AllowUsingIY
jp nz, CopyMatch1
; this is a crafty equivalent of CALL ReloadReadGamma : JP CopyMatch1
push iy
ELSE
call z, ReloadReadGamma
jr CopyMatch1
ENDIF
; the subroutine for reading the remainder of the partly read Elias gamma code.
; it has two entry points: ReloadReadGamma first refills the bit reservoir in A,
; while ReadGammaAligned assumes that the bit reservoir has just been refilled.
ReloadReadGamma:
ld a, (hl) ; reload bits
inc hl
rla
ret c
ReadGammaAligned:
add a, a
rl c
add a, a
ret c
add a, a
rl c
add a, a
ReadingLongGamma: ; this loop does not need unrolling, as it does not get much use anyway
ret c
add a, a
rl c
rl b
add a, a
jr nz, ReadingLongGamma
ld a, (hl) ; reload bits
inc hl
rla
jr ReadingLongGamma