-
Notifications
You must be signed in to change notification settings - Fork 2
/
aplib.s
291 lines (231 loc) · 6.4 KB
/
aplib.s
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
; ***************************************************************************
; ***************************************************************************
;
; aplib_6280.s
;
; HuC6280 decompressor for data stored in Jorgen Ibsen's aPLib format.
;
; This code is written for the PCEAS/NECASM assembler in HuC & MagicKit.
;
; 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
;
;
; Assume that we're decompessing from a large multi-bank
; compressed data file and that MPR3 (and MPR4) are used
; as a window into that file?
;
APL_FROM_MPR3 = 0
;
; Macro to increment the source pointer to the next page.
;
if APL_FROM_MPR3
APL_INC_PAGE macro
bsr .next_page
endm
else
APL_INC_PAGE macro
inc <apl_srcptr + 1
endm
endif
;
; Macros to read a byte/bit from the compressed source data.
;
if 1
APL_GET_SRC macro
lda [apl_srcptr],y
iny
bne .skip\@
APL_INC_PAGE
.skip\@:
endm
else
APL_GET_SRC macro
lda [apl_srcptr] ; Can be used if you *really*
inc <apl_srcptr + 0 ; don't want to map MPR4.
bne .skip\@
APL_INC_PAGE
.skip\@:
endm
endif
; ***************************************************************************
; ***************************************************************************
;
; Data usage is pretty-much all of the temporary System Card locations.
;
; Note that the TII instruction overlaps the bottom 2 bytes of the stack.
;
apl_bitbuf = __bp ; 1 byte.
apl_srcptr = __si ; 1 word.
apl_offset = __di ; 1 word.
apl_winptr = __ah ; Part of TII instruction.
apl_dstptr = __bh ; Part of TII instruction.
apl_length = __ch ; Part of TII instruction.
; ***************************************************************************
; ***************************************************************************
;
; 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: __bp, __si, __di, __ax, __bx, __cx, __dx
;
; If compiled with APL_FROM_MPR3, then apl_srcptr should be within MPR3, i.e.
; in the range $6000..$7FFF, and both MPR3 & MPR4 should be set.
;
; 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 has
; been commented-out, since these don't occur in typical PC Engine usage.
;
apl_decompress: lda #$73 ; TII instruction.
sta <__al
tii .tii_end, __dh, 3 ; TII ends with JMP.
lda #$80 ; Initialize an empty
sta <apl_bitbuf ; bit-buffer.
cly ; Initialize source index.
;
; 0 bbbbbbbb - One byte from compressed data, i.e. a "literal".
;
.literal: APL_GET_SRC
.write_byte: clx ; LWM=0.
sta [apl_dstptr] ; Write the byte directly to
inc <apl_dstptr + 0 ; the output.
bne .next_tag
inc <apl_dstptr + 1
.next_tag: asl <apl_bitbuf ; 1 0 <offset> <length>
bcc .literal
bne .skip1
bsr .load_bit
bcc .literal
.skip1: asl <apl_bitbuf
bne .skip2
bsr .load_bit
.skip2: bcc .code_pair
asl <apl_bitbuf ; 1 1 0 dddddddn
bne .skip3
bsr .load_bit
.skip3: bcc .copy_two_three
; 1 1 1 dddd - Copy 1 byte within 15 bytes (or zero).
.copy_one: lda #$10
.nibble_loop: asl <apl_bitbuf
bne .skip4
bsr .load_bit
.skip4: rol a
bcc .nibble_loop
beq .write_byte ; Offset=0 means write zero.
eor #$FF ; CS from previous ROL.
adc <apl_dstptr + 0
sta <apl_winptr + 0
lda #$FF
adc <apl_dstptr + 1
sta <apl_winptr + 1
lda [apl_winptr]
bra .write_byte
;
; Subroutines for byte & bit handling.
;
if APL_FROM_MPR3
.next_page: inc <apl_srcptr + 1 ; Increment source page, and
bmi .next_bank ; check if changed bank.
rts
.next_bank: pha ; Increment source bank view
tma3 ; within a large data file.
inc a
tam3
inc a
tam4
lda #$60
sta <apl_srcptr + 1
pla
rts
endif
.load_bit: pha ; Reload an empty bit-buffer
APL_GET_SRC ; from the compressed source.
rol a
sta <apl_bitbuf
pla
rts
.get_gamma: lda #1 ; Get a gamma-coded value.
.gamma_loop: asl <apl_bitbuf
bne .skip5
bsr .load_bit
.skip5: rol a
rol <apl_length + 1
asl <apl_bitbuf
bne .skip6
bsr .load_bit
.skip6: bcs .gamma_loop
rts
.match_done: ldx #1 ; LWM=1.
clc ; Update destination address.
lda <apl_length + 0
adc <apl_dstptr + 0
sta <apl_dstptr + 0
lda <apl_length + 1
adc <apl_dstptr + 1
sta <apl_dstptr + 1
bra .next_tag
;
; 1 1 0 dddddddn - Copy 2 or 3 within 128 bytes.
;
.copy_two_three:APL_GET_SRC ; 1 1 0 dddddddn
lsr a
beq .finished ; offset 0 == EOF.
sta <apl_offset + 0 ; Preserve offset.
stz <apl_offset + 1
cla
adc #2
stz <apl_length + 1
bra .do_match
.finished: rts ; All decompressed!
;
; 1 0 <offset> <length> - gamma-coded LZSS pair.
;
.code_pair: bsr .get_gamma ; Bits 8..15 of offset (min 2).
stz <apl_length + 1
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.
bsr .get_gamma ; Get Length (lo-byte in A).
bra .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.
bsr .get_gamma ; Get length (lo-byte in A).
ldx <apl_offset + 1 ; If offset < 256.
beq .lt256
; cpx #$7D ; If offset >= 32000, length += 2.
; bcs .match_plus2
cpx #$05 ; If offset >= 1280, length += 1.
bcs .match_plus1
bra .do_match
.lt256: ldx <apl_offset + 0 ; If offset < 128, length += 2.
bmi .do_match
.match_plus2: inc a ; Increment length.
bne .match_plus1
inc <apl_length + 1
.match_plus1: inc a ; Increment length.
bne .do_match
inc <apl_length + 1
.do_match: sta <apl_length + 0
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
jmp __ax
.tii_end: jmp .match_done