2019-06-10 11:24:15 +00:00
|
|
|
;
|
2019-09-25 06:09:18 +00:00
|
|
|
; Speed-optimized LZSA2 decompressor by spke (v.2 02-27/08/2019, 216 bytes);
|
2019-08-01 14:07:14 +00:00
|
|
|
; with improvements by uniabis (30/07/2019, -5 bytes, +3% speed and support for Hitachi HD64180).
|
2019-06-10 11:24:15 +00:00
|
|
|
;
|
|
|
|
; The data must be compressed using the command line compressor by Emmanuel Marty
|
|
|
|
; The compression is done as follows:
|
|
|
|
;
|
|
|
|
; lzsa.exe -f2 -r <sourcefile> <outfile>
|
|
|
|
;
|
|
|
|
; where option -r asks for the generation of raw (frame-less) data.
|
|
|
|
;
|
|
|
|
; The decompression is done in the standard way:
|
|
|
|
;
|
2019-07-24 16:28:39 +00:00
|
|
|
; ld hl,FirstByteOfCompressedData
|
|
|
|
; ld de,FirstByteOfMemoryForDecompressedData
|
2019-06-10 11:24:15 +00:00
|
|
|
; call DecompressLZSA2
|
|
|
|
;
|
2019-07-24 16:28:39 +00:00
|
|
|
; 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).
|
|
|
|
;
|
2019-08-01 14:07:14 +00:00
|
|
|
; Of course, LZSA2 compression algorithms are (c) 2019 Emmanuel Marty,
|
2019-06-10 11:24:15 +00:00
|
|
|
; see https://github.com/emmanuel-marty/lzsa for more information
|
|
|
|
;
|
|
|
|
; Drop me an email if you have any comments/ideas/suggestions: zxintrospec@gmail.com
|
|
|
|
;
|
2019-07-24 16:28:39 +00:00
|
|
|
; 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.
|
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
; DEFINE BACKWARD_DECOMPRESS ; uncomment for data compressed with option -b
|
|
|
|
; DEFINE HD64180 ; uncomment for systems using Hitachi HD64180
|
2019-07-24 16:28:39 +00:00
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
IFNDEF BACKWARD_DECOMPRESS
|
2019-07-24 16:28:39 +00:00
|
|
|
|
|
|
|
MACRO NEXT_HL
|
2019-08-01 14:07:14 +00:00
|
|
|
inc hl
|
2019-07-24 16:28:39 +00:00
|
|
|
ENDM
|
|
|
|
|
|
|
|
MACRO ADD_OFFSET
|
2019-08-01 14:07:14 +00:00
|
|
|
ex de,hl : add hl,de
|
2019-07-24 16:28:39 +00:00
|
|
|
ENDM
|
|
|
|
|
|
|
|
MACRO BLOCKCOPY
|
2019-08-01 14:07:14 +00:00
|
|
|
ldir
|
2019-07-24 16:28:39 +00:00
|
|
|
ENDM
|
|
|
|
|
2019-09-25 06:09:18 +00:00
|
|
|
MACRO COPY_MATCH
|
|
|
|
ldi : ldir
|
|
|
|
ENDM
|
|
|
|
|
2019-07-24 16:28:39 +00:00
|
|
|
ELSE
|
|
|
|
|
|
|
|
MACRO NEXT_HL
|
2019-08-01 14:07:14 +00:00
|
|
|
dec hl
|
2019-07-24 16:28:39 +00:00
|
|
|
ENDM
|
|
|
|
|
|
|
|
MACRO ADD_OFFSET
|
2019-08-01 14:07:14 +00:00
|
|
|
ex de,hl : ld a,e : sub l : ld l,a
|
|
|
|
ld a,d : sbc h : ld h,a ; 4*4+3*4 = 28t / 7 bytes
|
2019-07-24 16:28:39 +00:00
|
|
|
ENDM
|
|
|
|
|
|
|
|
MACRO BLOCKCOPY
|
2019-08-01 14:07:14 +00:00
|
|
|
lddr
|
2019-07-24 16:28:39 +00:00
|
|
|
ENDM
|
|
|
|
|
2019-09-25 06:09:18 +00:00
|
|
|
MACRO COPY_MATCH
|
|
|
|
ldd : lddr
|
|
|
|
ENDM
|
|
|
|
|
2019-07-24 16:28:39 +00:00
|
|
|
ENDIF
|
2019-06-10 11:24:15 +00:00
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
IFNDEF HD64180
|
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
MACRO LD_IX_DE
|
2019-08-01 14:07:14 +00:00
|
|
|
ld ixl,e : ld ixh,d
|
2019-07-30 16:39:27 +00:00
|
|
|
ENDM
|
2019-08-01 14:07:14 +00:00
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
MACRO LD_DE_IX
|
2019-08-01 14:07:14 +00:00
|
|
|
ld e,ixl : ld d,ixh
|
2019-07-30 16:39:27 +00:00
|
|
|
ENDM
|
2019-08-01 14:07:14 +00:00
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
ELSE
|
2019-08-01 14:07:14 +00:00
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
MACRO LD_IX_DE
|
2019-08-01 14:07:14 +00:00
|
|
|
push de : pop ix
|
2019-07-30 16:39:27 +00:00
|
|
|
ENDM
|
2019-08-01 14:07:14 +00:00
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
MACRO LD_DE_IX
|
2019-08-01 14:07:14 +00:00
|
|
|
push ix : pop de
|
2019-07-30 16:39:27 +00:00
|
|
|
ENDM
|
2019-08-01 14:07:14 +00:00
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
ENDIF
|
|
|
|
|
2019-06-10 11:24:15 +00:00
|
|
|
@DecompressLZSA2:
|
|
|
|
; A' stores next nibble as %1111.... or assumed to contain trash
|
|
|
|
; B is assumed to be 0
|
2019-08-01 14:07:14 +00:00
|
|
|
ld b,0 : scf : exa : jr ReadToken
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
ManyLiterals: ld a,18 : add (hl) : NEXT_HL : jr nc,CopyLiterals
|
2019-07-24 16:28:39 +00:00
|
|
|
ld c,(hl) : NEXT_HL
|
2019-07-30 16:39:27 +00:00
|
|
|
ld a,b : ld b,(hl)
|
2019-08-01 14:07:14 +00:00
|
|
|
jr ReadToken.NEXTHLuseBC
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-24 16:28:39 +00:00
|
|
|
MoreLiterals: ld b,(hl) : NEXT_HL
|
2019-07-30 16:39:27 +00:00
|
|
|
scf : exa : jr nc,.noUpdate
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
ld a,(hl) : or #F0 : exa
|
2019-07-24 16:28:39 +00:00
|
|
|
ld a,(hl) : NEXT_HL : or #0F
|
2019-06-10 11:24:15 +00:00
|
|
|
rrca : rrca : rrca : rrca
|
|
|
|
|
|
|
|
.noUpdate ;sub #F0-3 : cp 15+3 : jr z,ManyLiterals
|
|
|
|
inc a : jr z,ManyLiterals : sub #F0-3+1
|
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
CopyLiterals: ld c,a : ld a,b : ld b,0
|
|
|
|
BLOCKCOPY
|
|
|
|
push de : or a : jp p,CASE0xx ;: jr CASE1xx
|
2019-06-10 11:24:15 +00:00
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
cp %11000000 : jr c,CASE10x
|
2019-06-10 11:24:15 +00:00
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
CASE11x cp %11100000 : jr c,CASE110
|
2019-06-10 11:24:15 +00:00
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
; "111": repeated offset
|
|
|
|
CASE111: LD_DE_IX : jr MatchLen
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
Literals0011: jr nz,MoreLiterals
|
|
|
|
|
2019-06-10 11:24:15 +00:00
|
|
|
; if "LL" of the byte token is equal to 0,
|
|
|
|
; there are no literals to copy
|
2019-08-01 14:07:14 +00:00
|
|
|
NoLiterals: or (hl) : NEXT_HL
|
2019-06-10 11:24:15 +00:00
|
|
|
push de : jp m,CASE1xx
|
|
|
|
|
|
|
|
; short (5 or 9 bit long) offsets
|
|
|
|
CASE0xx ld d,#FF : cp %01000000 : jr c,CASE00x
|
|
|
|
|
|
|
|
; "01x": the case of the 9-bit offset
|
|
|
|
CASE01x: cp %01100000 : rl d
|
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
ReadOffsetE ld e,(hl) : NEXT_HL
|
2019-06-10 11:24:15 +00:00
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
SaveOffset: LD_IX_DE
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
MatchLen: inc a : and %00000111 : jr z,LongerMatch : inc a
|
|
|
|
|
|
|
|
CopyMatch: ld c,a
|
2019-08-01 14:07:14 +00:00
|
|
|
.useC ex (sp),hl ; BC = len, DE = offset, HL = dest, SP ->[dest,src]
|
2019-07-30 16:39:27 +00:00
|
|
|
ADD_OFFSET ; BC = len, DE = dest, HL = dest-offset, SP->[src]
|
2019-09-25 06:09:18 +00:00
|
|
|
COPY_MATCH : pop hl
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
; compressed data stream contains records
|
|
|
|
; each record begins with the byte token "XYZ|LL|MMM"
|
2019-09-25 06:09:18 +00:00
|
|
|
ReadToken: ld a,(hl) : and %00011000 : jp pe,Literals0011 ; process the cases 00 and 11 separately
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
rrca : rrca : rrca
|
|
|
|
|
2019-07-30 16:39:27 +00:00
|
|
|
ld c,a : ld a,(hl) ; token is re-read for further processing
|
2019-08-01 14:07:14 +00:00
|
|
|
.NEXTHLuseBC NEXT_HL
|
2019-07-24 16:28:39 +00:00
|
|
|
BLOCKCOPY
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
; the token and literals are followed by the offset
|
|
|
|
push de : or a : jp p,CASE0xx
|
|
|
|
|
|
|
|
CASE1xx cp %11000000 : jr nc,CASE11x
|
|
|
|
|
|
|
|
; "10x": the case of the 5-bit offset
|
2019-08-01 14:07:14 +00:00
|
|
|
CASE10x: ld c,a : exa : jr nc,.noUpdate
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
ld a,(hl) : or #F0 : exa
|
2019-07-24 16:28:39 +00:00
|
|
|
ld a,(hl) : NEXT_HL : or #0F
|
2019-06-10 11:24:15 +00:00
|
|
|
rrca : rrca : rrca : rrca
|
|
|
|
|
|
|
|
.noUpdate ld d,a : ld a,c
|
|
|
|
cp %10100000 : rl d
|
|
|
|
dec d : dec d : jr ReadOffsetE
|
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
; "110": 16-bit offset
|
|
|
|
CASE110: ld d,(hl) : NEXT_HL : jr ReadOffsetE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-10 11:24:15 +00:00
|
|
|
; "00x": the case of the 5-bit offset
|
2019-08-01 14:07:14 +00:00
|
|
|
CASE00x: ld c,a : exa : jr nc,.noUpdate
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
ld a,(hl) : or #F0 : exa
|
2019-07-24 16:28:39 +00:00
|
|
|
ld a,(hl) : NEXT_HL : or #0F
|
2019-06-10 11:24:15 +00:00
|
|
|
rrca : rrca : rrca : rrca
|
|
|
|
|
|
|
|
.noUpdate ld e,a : ld a,c
|
2019-08-01 14:07:14 +00:00
|
|
|
cp %00100000 : rl e : jr SaveOffset
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-01 14:07:14 +00:00
|
|
|
LongerMatch: scf : exa : jr nc,.noUpdate
|
|
|
|
|
|
|
|
ld a,(hl) : or #F0 : exa
|
|
|
|
ld a,(hl) : NEXT_HL : or #0F
|
|
|
|
rrca : rrca : rrca : rrca
|
|
|
|
|
|
|
|
.noUpdate sub #F0-9 : cp 15+9 : jr c,CopyMatch
|
|
|
|
|
|
|
|
LongMatch: add (hl) : NEXT_HL : jr nc,CopyMatch
|
|
|
|
ld c,(hl) : NEXT_HL
|
|
|
|
ld b,(hl) : NEXT_HL : jr nz,CopyMatch.useC
|
|
|
|
pop de : ret
|
2019-06-10 11:24:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|