Merge pull request #19 from specke/master

Support for -b in Z80 decompressors
This commit is contained in:
Emmanuel Marty 2019-07-24 20:09:48 +02:00 committed by GitHub
commit fd70be918c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 271 additions and 73 deletions

View File

@ -4,16 +4,28 @@
; The data must be compressed using the command line compressor by Emmanuel Marty
; The compression is done as follows:
;
; lzsa.exe -r <sourcefile> <outfile>
; lzsa.exe -f1 -r <sourcefile> <outfile>
;
; where option -r asks for the generation of raw (frame-less) data.
;
; The decompression is done in the standard way:
;
; ld hl,CompressedData
; ld de,WhereToDecompress
; ld hl,FirstByteOfCompressedData
; ld de,FirstByteOfMemoryForDecompressedData
; call DecompressLZSA
;
; Backward compression is also supported; you can compress files backward using:
;
; lzsa.exe -f1 -r -b <sourcefile> <outfile>
;
; and decompress the resulting files using:
;
; ld hl,LastByteOfCompressedData
; ld de,LastByteOfMemoryForDecompressedData
; call DecompressLZSA
;
; (do not forget to uncomment the BACKWARD_DECOMPRESS option in the decompressor).
;
; Of course, LZSA compression algorithm is (c) 2019 Emmanuel Marty,
; see https://github.com/emmanuel-marty/lzsa for more information
;
@ -35,11 +47,43 @@
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
; DEFINE BACKWARD_DECOMPRESS
IFDEF BACKWARD_DECOMPRESS
MACRO NEXT_HL
dec hl
ENDM
MACRO ADD_OFFSET
or a : sbc hl,de
ENDM
MACRO BLOCKCOPY
lddr
ENDM
ELSE
MACRO NEXT_HL
inc hl
ENDM
MACRO ADD_OFFSET
add hl,de
ENDM
MACRO BLOCKCOPY
ldir
ENDM
ENDIF
@DecompressLZSA:
ld b,0 : jr ReadToken
NoLiterals: xor (hl) : inc hl
push de : ld e,(hl) : inc hl : jp m,LongOffset
NoLiterals: xor (hl) : NEXT_HL
push de : ld e,(hl) : NEXT_HL : jp m,LongOffset
; short matches have length 0+3..14+3
ShortOffset: ld d,#FF : add 3 : cp 15+3 : jr nc,LongerMatch
@ -47,8 +91,8 @@ ShortOffset: ld d,#FF : add 3 : cp 15+3 : jr nc,LongerMatch
; placed here this saves a JP per iteration
CopyMatch: ld c,a
.UseC ex (sp),hl : push hl ; BC = len, DE = offset, HL = dest, SP ->[dest,src]
add hl,de : pop de ; BC = len, DE = dest, HL = dest-offset, SP->[src]
ldir : pop hl ; BC = 0, DE = dest, HL = src
ADD_OFFSET : pop de ; BC = len, DE = dest, HL = dest-offset, SP->[src]
BLOCKCOPY : pop hl ; BC = 0, DE = dest, HL = src
ReadToken: ; first a byte token "O|LLL|MMMM" is read from the stream,
; where LLL is the number of literals and MMMM is
@ -58,26 +102,26 @@ ReadToken: ; first a byte token "O|LLL|MMMM" is read from the stream,
cp #70 : jr z,MoreLiterals ; LLL=7 means 7+ literals...
rrca : rrca : rrca : rrca ; LLL<7 means 0..6 literals...
ld c,a : ld a,(hl) : inc hl
ldir
ld c,a : ld a,(hl) : NEXT_HL
BLOCKCOPY
; next we read the first byte of the offset
push de : ld e,(hl) : inc hl
push de : ld e,(hl) : NEXT_HL
; the top bit of token is set if the offset contains two bytes
and #8F : jp p,ShortOffset
LongOffset: ; read second byte of the offset
ld d,(hl) : inc hl
ld d,(hl) : NEXT_HL
add -128+3 : cp 15+3 : jp c,CopyMatch
; MMMM=15 indicates a multi-byte number of literals
LongerMatch: add (hl) : inc hl : jr nc,CopyMatch
LongerMatch: add (hl) : NEXT_HL : jr nc,CopyMatch
; the codes are designed to overflow;
; the overflow value 1 means read 1 extra byte
; and overflow value 0 means read 2 extra bytes
.code1 ld b,a : ld c,(hl) : inc hl : jr nz,CopyMatch.UseC
.code0 ld b,(hl) : inc hl
.code1 ld b,a : ld c,(hl) : NEXT_HL : jr nz,CopyMatch.UseC
.code0 ld b,(hl) : NEXT_HL
; the two-byte match length equal to zero
; designates the end-of-data marker
@ -85,17 +129,17 @@ LongerMatch: add (hl) : inc hl : jr nc,CopyMatch
pop de : ret
MoreLiterals: ; there are three possible situations here
xor (hl) : inc hl : exa
ld a,7 : add (hl) : inc hl : jr c,ManyLiterals
xor (hl) : NEXT_HL : exa
ld a,7 : add (hl) : NEXT_HL : jr c,ManyLiterals
CopyLiterals: ld c,a
.UseC ldir
.UseC BLOCKCOPY
push de : ld e,(hl) : inc hl
push de : ld e,(hl) : NEXT_HL
exa : jp p,ShortOffset : jr LongOffset
ManyLiterals:
.code1 ld b,a : ld c,(hl) : inc hl : jr nz,CopyLiterals.UseC
.code0 ld b,(hl) : inc hl : jr CopyLiterals.UseC
.code1 ld b,a : ld c,(hl) : NEXT_HL : jr nz,CopyLiterals.UseC
.code0 ld b,(hl) : NEXT_HL : jr CopyLiterals.UseC

View File

@ -4,16 +4,28 @@
; The data must be compressed using the command line compressor by Emmanuel Marty
; The compression is done as follows:
;
; lzsa.exe -r <sourcefile> <outfile>
; lzsa.exe -f1 -r <sourcefile> <outfile>
;
; where option -r asks for the generation of raw (frame-less) data.
;
; The decompression is done in the standard way:
;
; ld hl,CompressedData
; ld de,WhereToDecompress
; ld hl,FirstByteOfCompressedData
; ld de,FirstByteOfMemoryForDecompressedData
; call DecompressLZSA
;
; Backward compression is also supported; you can compress files backward using:
;
; lzsa.exe -f1 -r -b <sourcefile> <outfile>
;
; and decompress the resulting files using:
;
; ld hl,LastByteOfCompressedData
; ld de,LastByteOfMemoryForDecompressedData
; call DecompressLZSA
;
; (do not forget to uncomment the BACKWARD_DECOMPRESS option in the decompressor).
;
; Of course, LZSA compression algorithm is (c) 2019 Emmanuel Marty,
; see https://github.com/emmanuel-marty/lzsa for more information
;
@ -34,7 +46,38 @@
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
;
; DEFINE BACKWARD_DECOMPRESS
IFDEF BACKWARD_DECOMPRESS
MACRO NEXT_HL
dec hl
ENDM
MACRO ADD_OFFSET
or a : sbc hl,de
ENDM
MACRO BLOCKCOPY
lddr
ENDM
ELSE
MACRO NEXT_HL
inc hl
ENDM
MACRO ADD_OFFSET
add hl,de
ENDM
MACRO BLOCKCOPY
ldir
ENDM
ENDIF
@DecompressLZSA:
ld b,0
@ -42,21 +85,21 @@
; first a byte token "O|LLL|MMMM" is read from the stream,
; where LLL is the number of literals and MMMM is
; a length of the match that follows after the literals
ReadToken: ld a,(hl) : exa : ld a,(hl) : inc hl
ReadToken: ld a,(hl) : exa : ld a,(hl) : NEXT_HL
and #70 : jr z,NoLiterals
rrca : rrca : rrca : rrca ; LLL<7 means 0..6 literals...
cp #07 : call z,ReadLongBA ; LLL=7 means 7+ literals...
ld c,a : ldir
ld c,a : BLOCKCOPY
; next we read the low byte of the -offset
NoLiterals: push de : ld e,(hl) : inc hl : ld d,#FF
NoLiterals: push de : ld e,(hl) : NEXT_HL : ld d,#FF
; the top bit of token is set if
; the offset contains the high byte as well
exa : or a : jp p,ShortOffset
LongOffset: ld d,(hl) : inc hl
LongOffset: ld d,(hl) : NEXT_HL
; last but not least, the match length is read
ShortOffset: and #0F : add 3 ; MMMM<15 means match lengths 0+3..14+3
@ -64,19 +107,19 @@ ShortOffset: and #0F : add 3 ; MMMM<15 means match lengths 0+3..14+3
ld c,a
ex (sp),hl : push hl ; BC = len, DE = -offset, HL = dest, SP ->[dest,src]
add hl,de : pop de ; BC = len, DE = dest, HL = dest+(-offset), SP->[src]
ldir : pop hl ; BC = 0, DE = dest, HL = src
ADD_OFFSET : pop de ; BC = len, DE = dest, HL = dest+(-offset), SP->[src]
BLOCKCOPY : pop hl ; BC = 0, DE = dest, HL = src
jr ReadToken
; a standard routine to read extended codes
; into registers B (higher byte) and A (lower byte).
ReadLongBA: add (hl) : inc hl : ret nc
ReadLongBA: add (hl) : NEXT_HL : ret nc
; the codes are designed to overflow;
; the overflow value 1 means read 1 extra byte
; and overflow value 0 means read 2 extra bytes
.code1: ld b,a : ld a,(hl) : inc hl : ret nz
.code0: ld c,a : ld b,(hl) : inc hl
.code1: ld b,a : ld a,(hl) : NEXT_HL : ret nz
.code0: ld c,a : ld b,(hl) : NEXT_HL
; the two-byte match length equal to zero
; designates the end-of-data marker

View File

@ -10,15 +10,74 @@
;
; The decompression is done in the standard way:
;
; ld hl,CompressedData
; ld de,WhereToDecompress
; ld hl,FirstByteOfCompressedData
; ld de,FirstByteOfMemoryForDecompressedData
; call DecompressLZSA2
;
; Backward compression is also supported; you can compress files backward using:
;
; lzsa.exe -f2 -r -b <sourcefile> <outfile>
;
; and decompress the resulting files using:
;
; ld hl,LastByteOfCompressedData
; ld de,LastByteOfMemoryForDecompressedData
; call DecompressLZSA2
;
; (do not forget to uncomment the BACKWARD_DECOMPRESS option in the decompressor).
;
; Of course, LZSA2 compression algorithm is (c) 2019 Emmanuel Marty,
; see https://github.com/emmanuel-marty/lzsa for more information
;
; Drop me an email if you have any comments/ideas/suggestions: zxintrospec@gmail.com
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
; DEFINE BACKWARD_DECOMPRESS
IFDEF BACKWARD_DECOMPRESS
MACRO NEXT_HL
dec hl
ENDM
MACRO ADD_OFFSET
or a : sbc hl,de
ENDM
MACRO BLOCKCOPY
lddr
ENDM
ELSE
MACRO NEXT_HL
inc hl
ENDM
MACRO ADD_OFFSET
add hl,de
ENDM
MACRO BLOCKCOPY
ldir
ENDM
ENDIF
@DecompressLZSA2:
; A' stores next nibble as %1111.... or assumed to contain trash
@ -32,16 +91,16 @@
LongerMatch: exa : jp m,.noUpdate
ld a,(hl) : or #F0 : exa
ld a,(hl) : inc hl : or #0F
ld a,(hl) : NEXT_HL : or #0F
rrca : rrca : rrca : rrca
.noUpdate sub #F0-9 : cp 15+9 : jr c,CopyMatch
;inc a : jr z,LongMatch : sub #F0-9+1 : jp CopyMatch
LongMatch: ;ld a,24 :
add (hl) : inc hl : jr nc,CopyMatch
ld c,(hl) : inc hl
ld b,(hl) : inc hl
add (hl) : NEXT_HL : jr nc,CopyMatch
ld c,(hl) : NEXT_HL
ld b,(hl) : NEXT_HL
jr nz,CopyMatch.useC
pop de : ret
@ -49,19 +108,19 @@ LongMatch: ;ld a,24 :
ManyLiterals: ld a,18 :
add (hl) : inc hl : jr nc,CopyLiterals
ld c,(hl) : inc hl
add (hl) : NEXT_HL : jr nc,CopyLiterals
ld c,(hl) : NEXT_HL
ld a,b : ld b,(hl) : inc hl
jr CopyLiterals.useBC
MoreLiterals: ld b,(hl) : inc hl
MoreLiterals: ld b,(hl) : NEXT_HL
exa : jp m,.noUpdate
ld a,(hl) : or #F0 : exa
ld a,(hl) : inc hl : or #0F
ld a,(hl) : NEXT_HL : or #0F
rrca : rrca : rrca : rrca
.noUpdate ;sub #F0-3 : cp 15+3 : jr z,ManyLiterals
@ -69,7 +128,7 @@ MoreLiterals: ld b,(hl) : inc hl
CopyLiterals: ld c,a
.useC ld a,b : ld b,0
.useBC ldir
.useBC BLOCKCOPY
push de : or a : jp p,CASE0xx : jr CASE1xx
@ -78,7 +137,7 @@ CopyLiterals: ld c,a
; if "LL" of the byte token is equal to 0,
; there are no literals to copy
NoLiterals: xor (hl) : inc hl
NoLiterals: xor (hl) : NEXT_HL
push de : jp m,CASE1xx
; short (5 or 9 bit long) offsets
@ -87,7 +146,7 @@ CASE0xx ld d,#FF : cp %01000000 : jr c,CASE00x
; "01x": the case of the 9-bit offset
CASE01x: cp %01100000 : rl d
ReadOffsetE: ld e,(hl) : inc hl
ReadOffsetE: ld e,(hl) : NEXT_HL
SaveOffset: ld ixl,e : ld ixh,d
@ -95,8 +154,8 @@ MatchLen: inc a : and %00000111 : jr z,LongerMatch : inc a
CopyMatch: ld c,a
.useC ex (sp),hl : push hl ; BC = len, DE = offset, HL = dest, SP ->[dest,src]
add hl,de : pop de ; BC = len, DE = dest, HL = dest-offset, SP->[src]
ldir : pop hl
ADD_OFFSET : pop de ; BC = len, DE = dest, HL = dest-offset, SP->[src]
BLOCKCOPY : pop hl
; compressed data stream contains records
; each record begins with the byte token "XYZ|LL|MMM"
@ -105,8 +164,8 @@ ReadToken: ld a,(hl) : and %00011000 : jr z,NoLiterals
jp pe,MoreLiterals ; 00 has already been processed; this identifies the case of 11
rrca : rrca : rrca
ld c,a : ld a,(hl) : inc hl ; token is re-read for further processing
ldir
ld c,a : ld a,(hl) : NEXT_HL ; token is re-read for further processing
BLOCKCOPY
; the token and literals are followed by the offset
push de : or a : jp p,CASE0xx
@ -118,7 +177,7 @@ CASE10x: ld c,a : xor a
exa : jp m,.noUpdate
ld a,(hl) : or #F0 : exa
ld a,(hl) : inc hl : or #0F
ld a,(hl) : NEXT_HL : or #0F
rrca : rrca : rrca : rrca
.noUpdate ld d,a : ld a,c
@ -130,7 +189,7 @@ CASE00x: ld c,a : xor a
exa : jp m,.noUpdate
ld a,(hl) : or #F0 : exa
ld a,(hl) : inc hl : or #0F
ld a,(hl) : NEXT_HL : or #0F
rrca : rrca : rrca : rrca
.noUpdate ld e,a : ld a,c
@ -143,15 +202,7 @@ CASE11x cp %11100000 : jr c,CASE110
CASE111: ld e,ixl : ld d,ixh : jr MatchLen
; "110": 16-bit offset
CASE110: ld d,(hl) : inc hl : jr ReadOffsetE
;ReadNibble: ; 17 bytes, 44 t-state per nibble
; exa : ret m ; 4+11 = 15t
;UpdateNibble:
; ld a,(hl) : or #F0 : exa
; ld a,(hl) : inc hl : or #0F
; rrca : rrca : rrca : rrca : ret ; 4+5 + 7+7+4+7+6+7+4+4+4+4+10 = 73t
CASE110: ld d,(hl) : NEXT_HL : jr ReadOffsetE

View File

@ -10,15 +10,75 @@
;
; The decompression is done in the standard way:
;
; ld hl,CompressedData
; ld de,WhereToDecompress
; ld hl,FirstByteOfCompressedData
; ld de,FirstByteOfMemoryForDecompressedData
; call DecompressLZSA2
;
; Backward compression is also supported; you can compress files backward using:
;
; lzsa.exe -f2 -r -b <sourcefile> <outfile>
;
; and decompress the resulting files using:
;
; ld hl,LastByteOfCompressedData
; ld de,LastByteOfMemoryForDecompressedData
; call DecompressLZSA2
;
; (do not forget to uncomment the BACKWARD_DECOMPRESS option in the decompressor).
;
; Of course, LZSA2 compression algorithm is (c) 2019 Emmanuel Marty,
; see https://github.com/emmanuel-marty/lzsa for more information
;
; Drop me an email if you have any comments/ideas/suggestions: zxintrospec@gmail.com
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Permission is granted to anyone to use this software for any purpose,
; including commercial applications, and to alter it and redistribute it
; freely, subject to the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software
; in a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
; 3. This notice may not be removed or altered from any source distribution.
;
; DEFINE BACKWARD_DECOMPRESS
IFDEF BACKWARD_DECOMPRESS
MACRO NEXT_HL
dec hl
ENDM
MACRO ADD_OFFSET
or a : sbc hl,de
ENDM
MACRO BLOCKCOPY
lddr
ENDM
ELSE
MACRO NEXT_HL
inc hl
ENDM
MACRO ADD_OFFSET
add hl,de
ENDM
MACRO BLOCKCOPY
ldir
ENDM
ENDIF
@DecompressLZSA2:
xor a : ld b,a : exa : jr ReadToken
@ -27,7 +87,7 @@ CASE0xx ld d,#FF : cp %01000000 : jr c,CASE00x
CASE01x: cp %01100000 : rl d
OffsetReadE: ld e,(hl) : inc hl
OffsetReadE: ld e,(hl) : NEXT_HL
SaveOffset: ld iyl,e : ld iyh,d
@ -35,17 +95,17 @@ MatchLen: and %00000111 : add 2 : cp 9 : call z,ExtendedCode
CopyMatch: ld c,a
ex (sp),hl : push hl ; BC = len, DE = offset, HL = dest, SP ->[dest,src]
add hl,de : pop de ; BC = len, DE = dest, HL = dest-offset, SP->[src]
ldir : pop hl
ADD_OFFSET : pop de ; BC = len, DE = dest, HL = dest-offset, SP->[src]
BLOCKCOPY : pop hl
ReadToken: ld a,(hl) : ld ixl,a : inc hl
ReadToken: ld a,(hl) : ld ixl,a : NEXT_HL
and %00011000 : jr z,NoLiterals
rrca : rrca : rrca
call pe,ExtendedCode
ld c,a
ldir
BLOCKCOPY
NoLiterals: push de : ld a,ixl
or a : jp p,CASE0xx
@ -65,18 +125,18 @@ CASE11x cp %11100000 : jr c,CASE110
CASE111: ld e,iyl : ld d,iyh : jr MatchLen
CASE110: ld d,(hl) : inc hl : jr OffsetReadE
CASE110: ld d,(hl) : NEXT_HL : jr OffsetReadE
ExtendedCode: call ReadNibble : inc a : jr z,ExtraByte
sub #F0+1 : add c : ret
ExtraByte ld a,15 : add c : add (hl) : inc hl : ret nc
ld a,(hl) : inc hl
ld b,(hl) : inc hl : ret nz
ExtraByte ld a,15 : add c : add (hl) : NEXT_HL : ret nc
ld a,(hl) : NEXT_HL
ld b,(hl) : NEXT_HL : ret nz
pop de : pop de : ret
ReadNibble: ld c,a : xor a : exa : ret m
UpdateNibble ld a,(hl) : or #F0 : exa
ld a,(hl) : inc hl : or #0F
ld a,(hl) : NEXT_HL : or #0F
rrca : rrca : rrca : rrca : ret