-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlz4vram.s
349 lines (317 loc) · 4.1 KB
/
lz4vram.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
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
;
; Lauri Kasanen, 6 Jun 2017
; (C) Mega Cat Studios
; An optimized LZ4 decompressor
;
.importzp sp, sreg, regsave, regbank
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
.macpack longbranch
.import pushax,popax,_vram_write
.export _vram_unlz4
_out = regsave
_written = regsave + 2
_tmp = tmp1
_token = tmp2
_offset = ptr3
_in = sreg
_outlen = ptr4
PPU_ADDR = $2006
PPU_DATA = $2007
; ---------------------------------------------------------------
; void decompress_lz4 (const u8 *in, u8 * const out, const u16 outlen)
; ---------------------------------------------------------------
.segment "CODE"
; size in ptr3, src in ptr1, dest in ptr2
; don't touch size
memcpy_r2v:
lda ptr2+1
sta PPU_ADDR
lda ptr2
sta PPU_ADDR
lda ptr1
ldx ptr1+1
jsr pushax
lda ptr3
ldx ptr3+1
jmp _vram_write
memcpy_v2v:
lda #0
sta tmp3
sta tmp4
jmp @check
@loop:
; read source byte
lda ptr1+1
sta PPU_ADDR
lda ptr1
sta PPU_ADDR
ldx PPU_DATA
ldx PPU_DATA
inc ptr1
bne @nosrcinc
inc ptr1+1
@nosrcinc:
; write dst byte
lda ptr2+1
sta PPU_ADDR
lda ptr2
sta PPU_ADDR
stx PPU_DATA
inc ptr2
bne @nodstinc
inc ptr2+1
@nodstinc:
; increase counter
inc tmp3
bne @noinc
inc tmp4
@noinc:
@check:
lda tmp3
cmp ptr3
lda tmp4
sbc ptr3+1
bcc @loop
rts
.proc _vram_unlz4: near
sta _outlen
stx _outlen+1
jsr popax
sta _out
stx _out+1
jsr popax
sta _in
stx _in+1
;
; written = 0;
;
lda #$00
sta _written
;
; while (written < outlen) {
;
jmp L0046
;
; token = *in++;
;
L0004: ldy #0
lda (_in),y
sta _token
inc _in
bne L000A
inc _in+1
L000A:
;
; offset = token >> 4;
;
ldx #$00
lsr a
lsr a
lsr a
lsr a
sta _offset
stx _offset+1
;
; token &= 0xf;
; token += 4; // Minmatch
;
lda _token
and #$0F
clc
adc #4
sta _token
;
; if (offset == 15) {
;
lda _offset
cmp #$0F
L0013: bne L001A
;
; tmp = *in++;
;
ldy #0
lda (_in),y
sta _tmp
inc _in
bne L0017
inc _in+1
L0017:
;
; offset += tmp;
;
clc
adc _offset
sta _offset
lda #$00
adc _offset+1
sta _offset+1
;
; if (tmp == 255)
;
lda _tmp
cmp #$FF
;
; goto moreliterals;
;
jmp L0013
;
; if (offset) {
;
L001A: lda _offset
ora _offset+1
beq L001C
;
; memcpy(&out[written], in, offset);
;
lda _out
clc
adc _written
sta ptr2
lda _out+1
adc _written+1
tax
stx ptr2+1
lda _in
ldx _in+1
sta ptr1
stx ptr1+1
jsr memcpy_r2v
;
; written += offset;
;
lda _offset
clc
adc _written
sta _written
lda _offset+1
adc _written+1
sta _written+1
;
; in += offset;
;
lda _offset
clc
adc _in
sta _in
lda _offset+1
adc _in+1
sta _in+1
;
; if (written >= outlen)
;
L001C: lda _written
cmp _outlen
lda _written+1
sbc _outlen+1
;
; return;
;
bcc L0047
rts
;
; memcpy(&offset, in, 2);
;
L0047: ldy #0
lda (_in),y
sta _offset
iny
lda (_in),y
sta _offset+1
;
; in += 2;
;
lda #$02
clc
adc _in
sta _in
bcc L002F
inc _in+1
;
; copysrc = out + written - offset;
;
L002F: lda _out
clc
adc _written
pha
lda _out+1
adc _written+1
tax
pla
sec
sbc _offset
sta ptr1
txa
sbc _offset+1
sta ptr1+1
;
; offset = token;
;
lda #$00
sta _offset+1
lda _token
sta _offset
;
; if (token == 19) {
;
cmp #$13
L0045: bne L003C
;
; tmp = *in++;
;
ldy #0
lda (_in),y
sta _tmp
inc _in
bne L0039
inc _in+1
L0039:
;
; offset += tmp;
;
clc
adc _offset
sta _offset
tya
adc _offset+1
sta _offset+1
;
; if (tmp == 255)
;
lda _tmp
cmp #$FF
;
; goto morematches;
;
jmp L0045
;
; memcpy(&out[written], copysrc, offset);
;
L003C: lda _out
clc
adc _written
sta ptr2
lda _out+1
adc _written+1
tax
stx ptr2+1
jsr memcpy_v2v
;
; written += offset;
;
lda _offset
clc
adc _written
sta _written
lda _offset+1
adc _written+1
L0046: sta _written+1
;
; while (written < outlen) {
;
lda _written
cmp _outlen
lda _written+1
sbc _outlen+1
jcc L0004
rts
.endproc