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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
; ***************************************************************************
; ***************************************************************************
;
; aplib_6502.s
;
; NMOS 6502 decompressor for data stored in Jorgen Ibsen's aPLib format.
;
; Includes support for Emmanuel Marty's enhancements to the aPLib format.
;
; The code is 252 bytes long for standard format, 270 for enhanced format.
;
; This code is written for the ACME assembler.
;
; Copyright John Brandwood 2019.
;
; Distributed under the Boost Software License, Version 1.0.
; (See accompanying file LICENSE_1_0.txt or copy at
; http://www.boost.org/LICENSE_1_0.txt)
;
; ***************************************************************************
; ***************************************************************************
; ***************************************************************************
; ***************************************************************************
;
; Decompression Options & Macros
;
;
; Use the enhanced format from Emmanuel Marty's APULTRA?
;
; The enhancements speed up decompression on an 8-bit CPU.
;
; This gives an 11% improvement in decompresison speed, but
; breaks compatibility with standard aPLib encoders.
;
APL_ENHANCED = 0
;
; Assume that we're decompessing from a large multi-bank
; compressed data file, and that the next bank may need to
; paged in when a page-boundary is crossed.
;
APL_FROM_BANK = 0
;
; Macro to increment the source pointer to the next page.
;
!if APL_FROM_BANK {
!macro APL_INC_PAGE {
jsr .next_page
}
} else {
!macro APL_INC_PAGE {
inc <apl_srcptr + 1
}
}
;
; Macro to read a byte from the compressed source data.
;
!macro APL_GET_SRC {
lda (apl_srcptr),y
inc <apl_srcptr + 0
bne .skip
+APL_INC_PAGE
.skip:
}
; ***************************************************************************
; ***************************************************************************
;
; Data usage is last 12 bytes of zero-page.
;
!if APL_ENHANCED {
apl_nibflg = $F4 ; 1 byte.
apl_nibble = $F5 ; 1 byte.
apl_egamma = $F6 ; 1 byte.
}
apl_bitbuf = $F7 ; 1 byte.
apl_offset = $F8 ; 1 word.
apl_winptr = $FA ; 1 word.
apl_srcptr = $FC ; 1 word.
apl_dstptr = $FE ; 1 word.
apl_length = apl_winptr
; ***************************************************************************
; ***************************************************************************
;
; apl_decompress - Decompress data stored in Jorgen Ibsen's aPLib format.
;
; Args: apl_srcptr = ptr to compessed data
; Args: apl_dstptr = ptr to output buffer
; Uses: lots!
;
; If compiled with APL_FROM_BANK, then apl_srcptr should be within the bank
; window range.
;
; As an optimization, the code to handle window offsets > 64768 bytes has
; been removed, since these don't occur with a 16-bit address range.
;
; As an optimization, the code to handle window offsets > 32000 bytes can
; be commented-out, since these don't occur in typical 8-bit computer usage.
;
apl_decompress: ldy #0 ; Initialize source index.
lda #$80 ; Initialize an empty
sta <apl_bitbuf ; bit-buffer.
!if APL_ENHANCED {
sta <apl_egamma ; Bit-buffer for gamma pairs.
sty <apl_nibflg ; Reset the flag.
}
;
; 0 bbbbbbbb - One byte from compressed data, i.e. a "literal".
;
.literal: +APL_GET_SRC
.write_byte: ldx #0 ; LWM=0.
sta (apl_dstptr),y ; Write the byte directly to
inc <apl_dstptr + 0 ; the output.
bne .next_tag
inc <apl_dstptr + 1
.next_tag: asl <apl_bitbuf ; 0 bbbbbbbb
bne .skip0
jsr .load_bit
.skip0: bcc .literal
.skip1: asl <apl_bitbuf ; 1 0 <offset> <length>
bne .skip2
jsr .load_bit
.skip2: bcc .copy_large
asl <apl_bitbuf ; 1 1 0 dddddddn
bne .skip3
jsr .load_bit
.skip3: bcc .copy_normal
; 1 1 1 dddd - Copy 1 byte within 15 bytes (or zero).
!if APL_ENHANCED {
.copy_short: lsr <apl_nibflg ; Is there a nibble waiting?
lda <apl_nibble ; Extract the lo-nibble.
bcs .skip4
inc <apl_nibflg ; Reset the flag.
+APL_GET_SRC
sta <apl_nibble ; Preserve for next time.
lsr ; Extract the hi-nibble.
lsr
lsr
lsr
.skip4: and #$0F ; Current nibble.
beq .write_byte ; Offset=0 means write zero.
} else {
.copy_short: lda #$10
.nibble_loop: asl <apl_bitbuf
bne .skip4
pha
jsr .load_bit
pla
.skip4: rol
bcc .nibble_loop
beq .write_byte ; Offset=0 means write zero.
}
eor #$FF ; Read the byte directly from
tay ; the destination window.
iny
dec <apl_dstptr + 1
lda (apl_dstptr),y
inc <apl_dstptr + 1
ldy #0
beq .write_byte
;
; 1 1 0 dddddddn - Copy 2 or 3 within 128 bytes.
;
.copy_normal: +APL_GET_SRC ; 1 1 0 dddddddn
lsr
beq .finished ; Offset 0 == EOF.
sta <apl_offset + 0 ; Preserve offset.
sty <apl_offset + 1
tya ; Y == 0.
tax ; Bits 8..15 of length.
adc #2 ; Bits 0...7 of length.
bne .do_match ; NZ from previous ADC.
;
; Subroutines for byte & bit handling.
;
!if APL_ENHANCED {
.get_gamma: lda #1 ; Get a gamma-coded value.
.gamma_loop: asl <apl_egamma
bne .rotate_gamma
pha
+APL_GET_SRC ; Reload an empty bit-buffer
rol ; from the compressed source.
sta <apl_egamma
pla
.rotate_gamma: rol
bcs .big_gamma ; Got 8 bits, now read rest.
asl <apl_egamma
bcc .gamma_loop
rts ; Always returns CS.
.big_gamma: pha ; Read remaining bits of length
tya ; larger than 255. This is very
jsr .rotate_gamma ; rare, so it saves cycles on
tax ; the 6502 to do it this way.
pla
.finished: rts ; All decompressed!
} else {
.get_gamma: lda #1 ; Get a gamma-coded value.
.gamma_loop: asl <apl_bitbuf
bne .skip5
pha
jsr .load_bit
pla
.skip5: rol
rol <apl_length + 1
asl <apl_bitbuf
bne .skip6
pha
jsr .load_bit
pla
.skip6: bcs .gamma_loop
.finished: rts ; All decompressed!
}
;
; 1 0 <offset> <length> - gamma-coded LZSS pair.
;
!if APL_ENHANCED {
.copy_large: jsr .get_gamma ; Bits 8..15 of offset (min 2).
cpx #1 ; CC if LWM==0, CS if LWM==1.
ldx #0 ; Clear hi-byte of length.
sbc #2 ; -3 if LWM==0, -2 if LWM==1.
bcs .normal_pair ; CC if LWM==0 && offset==2.
jsr .get_gamma ; Get length (A=lo-byte & CS).
bcs .do_match ; Use previous Offset.
.normal_pair: sta <apl_offset + 1 ; Save bits 8..15 of offset.
+APL_GET_SRC
sta <apl_offset + 0 ; Save bits 0...7 of offset.
jsr .get_gamma ; Get length (A=lo-byte & CS).
} else {
.copy_large: jsr .get_gamma ; Bits 8..15 of offset (min 2).
sty <apl_length + 1 ; Clear hi-byte of length.
cpx #1 ; CC if LWM==0, CS if LWM==1.
sbc #2 ; -3 if LWM==0, -2 if LWM==1.
bcs .normal_pair ; CC if LWM==0 && offset==2.
jsr .get_gamma ; Get length (A=lo-byte & CC).
ldx <apl_length + 1
bcc .do_match ; Use previous Offset.
.normal_pair: sta <apl_offset + 1 ; Save bits 8..15 of offset.
+APL_GET_SRC
sta <apl_offset + 0 ; Save bits 0...7 of offset.
jsr .get_gamma ; Get length (A=lo-byte & CC).
ldx <apl_length + 1
}
ldy <apl_offset + 1 ; If offset < 256.
beq .lt256
cpy #$7D ; If offset >= 32000, length += 2.
bcs .match_plus2
cpy #$05 ; If offset >= 1280, length += 1.
bcs .match_plus1
bcc .do_match
.lt256: ldy <apl_offset + 0 ; If offset < 128, length += 2.
bmi .do_match
!if APL_ENHANCED {
} else {
sec ; aPLib gamma returns with CC.
}
.match_plus2: adc #1 ; CS, so ADC #2.
bcs .match_plus256
.match_plus1: adc #0 ; CS, so ADC #1, or CC if fall
bcc .do_match ; through from .match_plus2.
.match_plus256: inx
.do_match: eor #$FF ; Negate the lo-byte of length
tay ; and check for zero.
iny
beq .calc_addr
eor #$FF
inx ; Increment # of pages to copy.
clc ; Calc destination for partial
adc <apl_dstptr + 0 ; page.
sta <apl_dstptr + 0
bcs .calc_addr
dec <apl_dstptr + 1
.calc_addr: sec ; Calc address of match.
lda <apl_dstptr + 0
sbc <apl_offset + 0
sta <apl_winptr + 0
lda <apl_dstptr + 1
sbc <apl_offset + 1
sta <apl_winptr + 1
.copy_page: lda (apl_winptr),y
sta (apl_dstptr),y
iny
bne .copy_page
inc <apl_winptr + 1
inc <apl_dstptr + 1
dex ; Any full pages left to copy?
bne .copy_page
inx ; LWM=1.
jmp .next_tag
;
; Subroutines for byte & bit handling.
;
.load_bit: +APL_GET_SRC ; Reload an empty bit-buffer
rol ; from the compressed source.
sta <apl_bitbuf
rts
|