.import memcpy_upwards,pushax,popax
.export _decompress_lz4
-_out = regsave
-_written = regsave + 2
-_tmp = tmp1
-_token = tmp2
-_offset = ptr3
-_in = sreg
-_outlen = ptr4
+out = regsave
+written = regsave + 2
+tmp = tmp1
+token = tmp2
+offset = ptr3
+in = sreg
+outlen = ptr4
; ---------------------------------------------------------------
; void decompress_lz4 (const u8 *in, u8 * const out, const u16 outlen)
.proc _decompress_lz4: near
- sta _outlen
- stx _outlen+1
+ sta outlen
+ stx outlen+1
jsr popax
- sta _out
- stx _out+1
+ sta out
+ stx out+1
jsr popax
- sta _in
- stx _in+1
+ sta in
+ stx in+1
;
; written = 0;
;
lda #$00
- sta _written
+ sta written
;
; while (written < outlen) {
;
; token = *in++;
;
L0004: ldy #0
- lda (_in),y
- sta _token
+ lda (in),y
+ sta token
- inc _in
+ inc in
bne L000A
- inc _in+1
+ inc in+1
L000A:
;
; offset = token >> 4;
lsr a
lsr a
lsr a
- sta _offset
- stx _offset+1
+ sta offset
+ stx offset+1
;
; token &= 0xf;
; token += 4; // Minmatch
;
- lda _token
+ lda token
and #$0F
clc
adc #4
- sta _token
+ sta token
;
; if (offset == 15) {
;
- lda _offset
+ lda offset
cmp #$0F
L0013: bne L001A
;
; tmp = *in++;
;
ldy #0
- lda (_in),y
- sta _tmp
+ lda (in),y
+ sta tmp
- inc _in
+ inc in
bne L0017
- inc _in+1
+ inc in+1
L0017:
;
; offset += tmp;
;
clc
- adc _offset
- sta _offset
+ adc offset
+ sta offset
lda #$00
- adc _offset+1
- sta _offset+1
+ adc offset+1
+ sta offset+1
;
; if (tmp == 255)
;
- lda _tmp
+ lda tmp
cmp #$FF
;
; goto moreliterals;
;
; if (offset) {
;
-L001A: lda _offset
- ora _offset+1
+L001A: lda offset
+ ora offset+1
beq L001C
;
; memcpy(&out[written], in, offset);
;
- lda _out
+ lda out
clc
- adc _written
+ adc written
sta ptr2
- lda _out+1
- adc _written+1
+ lda out+1
+ adc written+1
tax
lda ptr2
stx ptr2+1
jsr pushax
- lda _in
- ldx _in+1
+ lda in
+ ldx in+1
sta ptr1
stx ptr1+1
ldy #0
;
; written += offset;
;
- lda _offset
+ lda offset
clc
- adc _written
- sta _written
- lda _offset+1
- adc _written+1
- sta _written+1
+ adc written
+ sta written
+ lda offset+1
+ adc written+1
+ sta written+1
;
; in += offset;
;
- lda _offset
+ lda offset
clc
- adc _in
- sta _in
- lda _offset+1
- adc _in+1
- sta _in+1
+ 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
+L001C: lda written
+ cmp outlen
+ lda written+1
+ sbc outlen+1
;
; return;
;
; memcpy(&offset, in, 2);
;
L0047: ldy #0
- lda (_in),y
- sta _offset
+ lda (in),y
+ sta offset
iny
- lda (_in),y
- sta _offset+1
+ lda (in),y
+ sta offset+1
;
; in += 2;
;
lda #$02
clc
- adc _in
- sta _in
+ adc in
+ sta in
bcc L002F
- inc _in+1
+ inc in+1
;
; copysrc = out + written - offset;
;
-L002F: lda _out
+L002F: lda out
clc
- adc _written
+ adc written
pha
- lda _out+1
- adc _written+1
+ lda out+1
+ adc written+1
tax
pla
sec
- sbc _offset
+ sbc offset
sta ptr1
txa
- sbc _offset+1
+ sbc offset+1
sta ptr1+1
;
; offset = token;
;
lda #$00
- sta _offset+1
- lda _token
- sta _offset
+ sta offset+1
+ lda token
+ sta offset
;
; if (token == 19) {
;
; tmp = *in++;
;
ldy #0
- lda (_in),y
- sta _tmp
+ lda (in),y
+ sta tmp
- inc _in
+ inc in
bne L0039
- inc _in+1
+ inc in+1
L0039:
;
; offset += tmp;
;
clc
- adc _offset
- sta _offset
+ adc offset
+ sta offset
tya
- adc _offset+1
- sta _offset+1
+ adc offset+1
+ sta offset+1
;
; if (tmp == 255)
;
- lda _tmp
+ lda tmp
cmp #$FF
;
; goto morematches;
;
; memcpy(&out[written], copysrc, offset);
;
-L003C: lda _out
+L003C: lda out
clc
- adc _written
+ adc written
sta ptr2
- lda _out+1
- adc _written+1
+ lda out+1
+ adc written+1
tax
lda ptr2
stx ptr2+1
;
; written += offset;
;
- lda _offset
+ lda offset
clc
- adc _written
- sta _written
- lda _offset+1
- adc _written+1
-L0046: sta _written+1
+ 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
+ lda written
+ cmp outlen
+ lda written+1
+ sbc outlen+1
jcc L0004
rts