lzsa/asm/z80/unlzsa2_small_v1.asm

182 lines
4.3 KiB
NASM
Raw Normal View History

2019-06-10 11:24:15 +00:00
;
; Size-optimized LZSA2 decompressor by spke (v.1 02-09/06/2019, 140 bytes);
; with improvements by uniabis (30/07/2019, -1 byte, +3% speed and support for Hitachi HD64180).
2019-06-10 11:24:15 +00:00
;
2019-06-21 15:39:50 +00:00
; The data must be compressed using the command line compressor by Emmanuel Marty
2019-06-10 11:24:15 +00:00
; 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:
;
; ld hl,FirstByteOfCompressedData
; ld de,FirstByteOfMemoryForDecompressedData
2019-06-10 11:24:15 +00:00
; 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 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
;
; 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 ; uncomment for data compressed with option -b
; DEFINE HD64180 ; uncomment for systems using Hitachi HD64180
IFNDEF BACKWARD_DECOMPRESS
MACRO NEXT_HL
inc hl
ENDM
MACRO ADD_OFFSET
ex de,hl : add hl,de
ENDM
MACRO BLOCKCOPY
ldir
ENDM
ELSE
MACRO NEXT_HL
dec hl
ENDM
MACRO ADD_OFFSET
push hl : or a : sbc hl,de : pop de ; 11+4+15+10 = 40t / 5 bytes
ENDM
MACRO BLOCKCOPY
lddr
ENDM
ENDIF
2019-06-10 11:24:15 +00:00
IFNDEF HD64180
MACRO LD_IY_DE
ld iyl,e : ld iyh,d
ENDM
MACRO LD_DE_IY
ld e,iyl : ld d,iyh
ENDM
ELSE
MACRO LD_IY_DE
push de : pop iy
ENDM
MACRO LD_DE_IY
push iy : pop de
ENDM
ENDIF
2019-06-10 11:24:15 +00:00
@DecompressLZSA2:
xor a : ld b,a : exa : jr ReadToken
CASE00x: call ReadNibble
ld e,a : ld a,c
cp %00100000 : rl e : jr SaveOffset
2019-06-10 11:24:15 +00:00
CASE0xx ld d,#FF : cp %01000000 : jr c,CASE00x
CASE01x: cp %01100000 : rl d
OffsetReadE: ld e,(hl) : NEXT_HL
SaveOffset: LD_IY_DE
2019-06-10 11:24:15 +00:00
MatchLen: and %00000111 : add 2 : cp 9 : call z,ExtendedCode
CopyMatch: ld c,a
ex (sp),hl ; BC = len, DE = -offset, HL = dest, SP -> [src]
ADD_OFFSET ; BC = len, DE = dest, HL = dest+(-offset), SP -> [src]
BLOCKCOPY ; BC = 0, DE = dest
pop hl ; HL = src
2019-06-10 11:24:15 +00:00
ReadToken: ld a,(hl) : NEXT_HL : push af
2019-06-10 11:24:15 +00:00
and %00011000 : jr z,NoLiterals
rrca : rrca : rrca
call pe,ExtendedCode
ld c,a
BLOCKCOPY
2019-06-10 11:24:15 +00:00
NoLiterals: pop af : push de
2019-06-10 11:24:15 +00:00
or a : jp p,CASE0xx
CASE1xx cp %11000000 : jr nc,CASE11x
CASE10x: call ReadNibble
ld d,a : ld a,c
cp %10100000 : rl d
dec d : dec d : DB #CA ; jr OffsetReadE ; #CA is JP Z,.. to skip all commands in CASE110 before jr OffsetReadE
2019-06-10 11:24:15 +00:00
CASE110: ld d,(hl) : NEXT_HL : jr OffsetReadE
2019-06-10 11:24:15 +00:00
CASE11x cp %11100000 : jr c,CASE110
CASE111: LD_DE_IY : jr MatchLen
2019-06-10 11:24:15 +00:00
ExtendedCode: call ReadNibble : inc a : jr z,ExtraByte
sub #F0+1 : add c : ret
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 is not needed, because RET from ReadNibble is sufficient
2019-06-10 11:24:15 +00:00
ReadNibble: ld c,a : xor a : exa : ret m
UpdateNibble ld a,(hl) : or #F0 : exa
ld a,(hl) : NEXT_HL : or #0F
2019-06-10 11:24:15 +00:00
rrca : rrca : rrca : rrca : ret