; Original algorithm by Yann Collet
; This source is based on LZ4 explained
;
; Z80 assembly version by edouard.berge (at) gmail (dot) com
;
;
;
; As the Z80 cannot adress more than 64K in a row
; the decrunching routine DO NOT handle relative offset
; bigger than 65535. You've been warned!
;
;
; hl' compressed data adress
; de' negative lenght of outputed data (set -256 to output 256 bytes)
; de output adress of data
;
LZ4_decrunch
nextsequence
exx
ld a,(hl)
inc hl
ld (lzunpacklength+1),a
exx
and #F0
jr z,lzunpack ; no litteral bytes
srl a
rrca
rrca
rrca
ld b,0
ld c,a
cp 15 ; more bytes for length?
call z,getbytelength
push bc
exx
pop bc
push hl
ex hl,de
add hl,bc ; increase counter
ex hl,de
add hl,bc ; jump over litterals
exx
pop hl
ldir
lzunpack
exx
ld a,d
or e
ret z ; cause last byte is ALWAYS litterals!
; read 2 bytes offset
ld c,(hl)
inc hl
ld b,(hl)
inc hl
push bc
exx
pop hl
push de
ex hl,de
sbc hl,de
pop de
lzunpacklength ld a,#12
and #F
add 4
ld b,0
ld c,a
cp 19 ; more bytes for length?
call z,getbytelength
push bc
ldir
; update counter
exx
ex hl,de
pop bc
add hl,bc
ex hl,de
exx
jr nextsequence
; get additionnal length subroutine
getbytelength
exx
ld a,(hl)
inc hl
exx
cp 255
jr nz,mediumlength
inc b
dec bc
jr getbytelength
mediumlength
add a,c
ld c,a
ld a,b
adc a,0
ld b,a
; bc=length
ret