From fcfba056d2f00d275e918d498b6fef8e6c0677bc Mon Sep 17 00:00:00 2001 From: introspec <31136975+specke@users.noreply.github.com> Date: Wed, 24 Jul 2019 17:28:39 +0100 Subject: [PATCH] Add files via upload LZSA2 decompressors with support for -b option. --- asm/z80/unlzsa2_fast_v1.asm | 107 ++++++++++++++++++++++++++--------- asm/z80/unlzsa2_small_v1.asm | 84 +++++++++++++++++++++++---- 2 files changed, 151 insertions(+), 40 deletions(-) diff --git a/asm/z80/unlzsa2_fast_v1.asm b/asm/z80/unlzsa2_fast_v1.asm index b6a1d31..7274e68 100644 --- a/asm/z80/unlzsa2_fast_v1.asm +++ b/asm/z80/unlzsa2_fast_v1.asm @@ -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 +; +; 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 diff --git a/asm/z80/unlzsa2_small_v1.asm b/asm/z80/unlzsa2_small_v1.asm index 99df8bb..a2697e7 100644 --- a/asm/z80/unlzsa2_small_v1.asm +++ b/asm/z80/unlzsa2_small_v1.asm @@ -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 +; +; 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