LZ4 decrunch in Z80 assembly

716 views
Skip to first unread message

Édouard BERGÉ

unread,
Sep 5, 2016, 3:08:38 PM9/5/16
to LZ4c

Hi there
Here is a Z80 assembly version of LZ4 decrunch
It is assumed that files to decrunch are not bigger than 64K
The decruncher is compliant with officiel LZ4 cruncher
Regards

In my test sample, it is two times slower than memcpy to decrunch


note: i may host the source and further revision on my server?

;
; 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



Édouard BERGÉ

unread,
Sep 5, 2016, 5:31:52 PM9/5/16
to LZ4c

;   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!


oups, i mean "do not handle length bigger than 65535" ;) 

Cyan

unread,
Sep 5, 2016, 10:16:19 PM9/5/16
to LZ4c
Looks good !

Do you want to host it somewhere, or do you want to make this post the reference place of your code ?

Édouard BERGÉ

unread,
Sep 6, 2016, 5:33:00 PM9/6/16
to LZ4c
Another people send his Z80 implementation of LZ4 unpack on cpcwiki forum

Here is the link of his source

Cyan

unread,
Sep 7, 2016, 12:13:36 AM9/7/16
to LZ4c
Noted !
added both versions
Reply all
Reply to author
Forward
0 new messages