mirror of https://github.com/wwarthen/RomWBW.git
26 changed files with 2368 additions and 638 deletions
@ -0,0 +1,281 @@ |
|||||
|
; ----------------------------------------------------------------------------- |
||||
|
; Decompress raw LZSA1 block. Create one with lzsa -r <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: |
||||
|
; * LZSA_SRC_LO/LZSA_SRC_HI/LZSA_SRC_BANK contain the compressed raw block address |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/LZSA_DST_BANK contain the destination buffer address |
||||
|
; |
||||
|
; out: |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/LZSA_DST_BANK contain the last decompressed byte address, +1 |
||||
|
; |
||||
|
; ----------------------------------------------------------------------------- |
||||
|
; Backward decompression is also supported, use lzsa -r -b <original_file> <compressed_file> |
||||
|
; To use it, also define BACKWARD_DECOMPRESS=1 before including this code! |
||||
|
; |
||||
|
; in: |
||||
|
; * LZSA_SRC_LO/LZSA_SRC_HI/LZSA_SRC_BANK must contain the address of the last byte of compressed data |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/LZSA_DST_BANK must contain the address of the last byte of the destination buffer |
||||
|
; |
||||
|
; out: |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/BANK contain the last decompressed byte address, -1 |
||||
|
; |
||||
|
; ----------------------------------------------------------------------------- |
||||
|
; |
||||
|
; Copyright (C) 2019-2020 Emmanuel Marty, Peter Ferrie |
||||
|
; |
||||
|
; 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. |
||||
|
; ----------------------------------------------------------------------------- |
||||
|
|
||||
|
!cpu 65816 |
||||
|
DECOMPRESS_LZSA1 |
||||
|
SEP #$30 |
||||
|
!as |
||||
|
!rs |
||||
|
LDY #$00 |
||||
|
|
||||
|
DECODE_TOKEN |
||||
|
JSR GETSRC ; read token byte: O|LLL|MMMM |
||||
|
PHA ; preserve token on stack |
||||
|
|
||||
|
AND #$70 ; isolate literals count |
||||
|
BEQ NO_LITERALS ; skip if no literals to copy |
||||
|
CMP #$70 ; LITERALS_RUN_LEN? |
||||
|
BNE PREPARE_COPY_LITERALS ; if not, count is directly embedded in token |
||||
|
|
||||
|
JSR GETSRC ; get extra byte of variable literals count |
||||
|
; the carry is always set by the CMP above |
||||
|
; GETSRC doesn't change it |
||||
|
SBC #$F9 ; (LITERALS_RUN_LEN) |
||||
|
BCC PREPARE_COPY_LITERALS_DIRECT |
||||
|
BEQ LARGE_VARLEN_LITERALS ; if adding up to zero, go grab 16-bit count |
||||
|
|
||||
|
JSR GETSRC ; get single extended byte of variable literals count |
||||
|
INY ; add 256 to literals count |
||||
|
BCS PREPARE_COPY_LITERALS_DIRECT ; (*like JMP PREPARE_COPY_LITERALS_DIRECT but shorter) |
||||
|
|
||||
|
LARGE_VARLEN_LITERALS ; handle 16 bits literals count |
||||
|
; literals count = directly these 16 bits |
||||
|
JSR GETLARGESRC ; grab low 8 bits in X, high 8 bits in A |
||||
|
TAY ; put high 8 bits in Y |
||||
|
TXA |
||||
|
BCS PREPARE_COPY_LARGE_LITERALS ; (*like JMP PREPARE_COPY_LITERALS_DIRECT but shorter) |
||||
|
|
||||
|
PREPARE_COPY_LITERALS |
||||
|
TAX |
||||
|
LDA SHIFT_TABLE-1,X ; shift literals length into place |
||||
|
; -1 because position 00 is reserved |
||||
|
PREPARE_COPY_LITERALS_DIRECT |
||||
|
TAX |
||||
|
|
||||
|
PREPARE_COPY_LARGE_LITERALS |
||||
|
BEQ COPY_LITERALS |
||||
|
INY |
||||
|
|
||||
|
COPY_LITERALS |
||||
|
JSR GETPUT ; copy one byte of literals |
||||
|
DEX |
||||
|
BNE COPY_LITERALS |
||||
|
DEY |
||||
|
BNE COPY_LITERALS |
||||
|
|
||||
|
NO_LITERALS |
||||
|
PLA ; retrieve token from stack |
||||
|
PHA ; preserve token again |
||||
|
BMI GET_LONG_OFFSET ; $80: 16 bit offset |
||||
|
|
||||
|
JSR GETSRC ; get 8 bit offset from stream in A |
||||
|
TAX ; save for later |
||||
|
LDA #$FF ; high 8 bits |
||||
|
BNE GOT_OFFSET ; go prepare match |
||||
|
; (*like JMP GOT_OFFSET but shorter) |
||||
|
|
||||
|
SHORT_VARLEN_MATCHLEN |
||||
|
JSR GETSRC ; get single extended byte of variable match len |
||||
|
INY ; add 256 to match length |
||||
|
|
||||
|
PREPARE_COPY_MATCH |
||||
|
TAX |
||||
|
PREPARE_COPY_MATCH_Y |
||||
|
TXA |
||||
|
BEQ COPY_MATCH_LOOP |
||||
|
INY |
||||
|
|
||||
|
COPY_MATCH_LOOP |
||||
|
LDA $AAAAAA ; get one byte of backreference |
||||
|
JSR PUTDST ; copy to destination |
||||
|
|
||||
|
REP #$20 |
||||
|
!ifdef BACKWARD_DECOMPRESS { |
||||
|
|
||||
|
; Backward decompression -- put backreference bytes backward |
||||
|
|
||||
|
DEC COPY_MATCH_LOOP+1 |
||||
|
|
||||
|
} else { |
||||
|
|
||||
|
; Forward decompression -- put backreference bytes forward |
||||
|
|
||||
|
INC COPY_MATCH_LOOP+1 |
||||
|
|
||||
|
} |
||||
|
SEP #$20 |
||||
|
|
||||
|
DEX |
||||
|
BNE COPY_MATCH_LOOP |
||||
|
DEY |
||||
|
BNE COPY_MATCH_LOOP |
||||
|
BEQ DECODE_TOKEN ; (*like JMP DECODE_TOKEN but shorter) |
||||
|
|
||||
|
GET_LONG_OFFSET ; handle 16 bit offset: |
||||
|
JSR GETLARGESRC ; grab low 8 bits in X, high 8 bits in A |
||||
|
|
||||
|
GOT_OFFSET |
||||
|
|
||||
|
!ifdef BACKWARD_DECOMPRESS { |
||||
|
|
||||
|
; Backward decompression - substract match offset |
||||
|
|
||||
|
STA OFFSHI ; store high 8 bits of offset |
||||
|
STX OFFSLO |
||||
|
|
||||
|
SEC ; substract dest - match offset |
||||
|
REP #$20 |
||||
|
!al |
||||
|
LDA PUTDST+1 |
||||
|
OFFSLO = *+1 |
||||
|
OFFSHI = *+2 |
||||
|
SBC #$AAAA ; 16 bits |
||||
|
STA COPY_MATCH_LOOP+1 ; store back reference address |
||||
|
SEP #$20 |
||||
|
!as |
||||
|
SEC |
||||
|
|
||||
|
} else { |
||||
|
|
||||
|
; Forward decompression - add match offset |
||||
|
|
||||
|
STA OFFSHI ; store high 8 bits of offset |
||||
|
TXA |
||||
|
|
||||
|
CLC ; add dest + match offset |
||||
|
ADC PUTDST+1 ; low 8 bits |
||||
|
STA COPY_MATCH_LOOP+1 ; store back reference address |
||||
|
OFFSHI = *+1 |
||||
|
LDA #$AA ; high 8 bits |
||||
|
|
||||
|
ADC PUTDST+2 |
||||
|
STA COPY_MATCH_LOOP+2 ; store high 8 bits of address |
||||
|
|
||||
|
} |
||||
|
|
||||
|
LDA PUTDST+3 ; bank |
||||
|
STA COPY_MATCH_LOOP+3 ; store back reference address |
||||
|
|
||||
|
PLA ; retrieve token from stack again |
||||
|
AND #$0F ; isolate match len (MMMM) |
||||
|
ADC #$02 ; plus carry which is always set by the high ADC |
||||
|
CMP #$12 ; MATCH_RUN_LEN? |
||||
|
BCC PREPARE_COPY_MATCH ; if not, count is directly embedded in token |
||||
|
|
||||
|
JSR GETSRC ; get extra byte of variable match length |
||||
|
; the carry is always set by the CMP above |
||||
|
; GETSRC doesn't change it |
||||
|
SBC #$EE ; add MATCH_RUN_LEN and MIN_MATCH_SIZE to match length |
||||
|
BCC PREPARE_COPY_MATCH |
||||
|
BNE SHORT_VARLEN_MATCHLEN |
||||
|
|
||||
|
; Handle 16 bits match length |
||||
|
JSR GETLARGESRC ; grab low 8 bits in X, high 8 bits in A |
||||
|
TAY ; put high 8 bits in Y |
||||
|
; large match length with zero high byte? |
||||
|
BNE PREPARE_COPY_MATCH_Y ; if not, continue |
||||
|
|
||||
|
DECOMPRESSION_DONE |
||||
|
RTS |
||||
|
|
||||
|
SHIFT_TABLE |
||||
|
!BYTE $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 |
||||
|
!BYTE $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01 |
||||
|
!BYTE $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02 |
||||
|
!BYTE $03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03 |
||||
|
!BYTE $04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04 |
||||
|
!BYTE $05,$05,$05,$05,$05,$05,$05,$05,$05,$05,$05,$05,$05,$05,$05,$05 |
||||
|
!BYTE $06,$06,$06,$06,$06,$06,$06,$06,$06,$06,$06,$06,$06,$06,$06,$06 |
||||
|
!BYTE $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07 |
||||
|
|
||||
|
!ifdef BACKWARD_DECOMPRESS { |
||||
|
|
||||
|
; Backward decompression -- get and put bytes backward |
||||
|
|
||||
|
GETPUT |
||||
|
JSR GETSRC |
||||
|
PUTDST |
||||
|
LZSA_DST_LO = *+1 |
||||
|
LZSA_DST_HI = *+2 |
||||
|
LZSA_DST_BANK = *+3 |
||||
|
STA $AAAAAA |
||||
|
REP #$20 |
||||
|
DEC PUTDST+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
|
||||
|
GETLARGESRC |
||||
|
JSR GETSRC ; grab low 8 bits |
||||
|
TAX ; move to X |
||||
|
; fall through grab high 8 bits |
||||
|
|
||||
|
GETSRC |
||||
|
LZSA_SRC_LO = *+1 |
||||
|
LZSA_SRC_HI = *+2 |
||||
|
LZSA_SRC_BANK = *+3 |
||||
|
LDA $AAAAAA |
||||
|
REP #$20 |
||||
|
DEC GETSRC+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
|
||||
|
} else { |
||||
|
|
||||
|
; Forward decompression -- get and put bytes forward |
||||
|
|
||||
|
GETPUT |
||||
|
JSR GETSRC |
||||
|
PUTDST |
||||
|
LZSA_DST_LO = *+1 |
||||
|
LZSA_DST_HI = *+2 |
||||
|
LZSA_DST_BANK = *+3 |
||||
|
STA $AAAAAA |
||||
|
REP #$20 |
||||
|
INC PUTDST+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
|
||||
|
GETLARGESRC |
||||
|
JSR GETSRC ; grab low 8 bits |
||||
|
TAX ; move to X |
||||
|
; fall through grab high 8 bits |
||||
|
|
||||
|
GETSRC |
||||
|
LZSA_SRC_LO = *+1 |
||||
|
LZSA_SRC_HI = *+2 |
||||
|
LZSA_SRC_BANK = *+3 |
||||
|
LDA $AAAAAA |
||||
|
REP #$20 |
||||
|
INC GETSRC+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
} |
||||
@ -0,0 +1,338 @@ |
|||||
|
; ----------------------------------------------------------------------------- |
||||
|
; Decompress raw LZSA2 block. |
||||
|
; Create one with lzsa -r -f2 <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: |
||||
|
; * LZSA_SRC_LO/LZSA_SRC_HI/LZSA_SRC_BANK contain the compressed raw block address |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/LZSA_DST_BANK contain the destination buffer address |
||||
|
; |
||||
|
; out: |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/LZSA_DST_BANK contain the last decompressed byte address, +1 |
||||
|
; |
||||
|
; ----------------------------------------------------------------------------- |
||||
|
; Backward decompression is also supported, use lzsa -r -b -f2 <original_file> <compressed_file> |
||||
|
; To use it, also define BACKWARD_DECOMPRESS=1 before including this code! |
||||
|
; |
||||
|
; in: |
||||
|
; * LZSA_SRC_LO/LZSA_SRC_HI/LZSA_SRC_BANK must contain the address of the last byte of compressed data |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/LZSA_DST_BANK must contain the address of the last byte of the destination buffer |
||||
|
; |
||||
|
; out: |
||||
|
; * LZSA_DST_LO/LZSA_DST_HI/BANK contain the last decompressed byte address, -1 |
||||
|
; |
||||
|
; ----------------------------------------------------------------------------- |
||||
|
; |
||||
|
; Copyright (C) 2019-2020 Emmanuel Marty, Peter Ferrie |
||||
|
; |
||||
|
; 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. |
||||
|
; ----------------------------------------------------------------------------- |
||||
|
|
||||
|
!cpu 65816 |
||||
|
NIBCOUNT = $FC ; zero-page location for temp offset |
||||
|
|
||||
|
DECOMPRESS_LZSA2 |
||||
|
SEP #$30 |
||||
|
!as |
||||
|
!rs |
||||
|
LDY #$00 |
||||
|
STY NIBCOUNT |
||||
|
|
||||
|
DECODE_TOKEN |
||||
|
JSR GETSRC ; read token byte: XYZ|LL|MMM |
||||
|
PHA ; preserve token on stack |
||||
|
|
||||
|
AND #$18 ; isolate literals count (LL) |
||||
|
BEQ NO_LITERALS ; skip if no literals to copy |
||||
|
CMP #$18 ; LITERALS_RUN_LEN_V2? |
||||
|
BCC PREPARE_COPY_LITERALS ; if less, count is directly embedded in token |
||||
|
|
||||
|
JSR GETNIBBLE ; get extra literals length nibble |
||||
|
; add nibble to len from token |
||||
|
ADC #$02 ; (LITERALS_RUN_LEN_V2) minus carry |
||||
|
CMP #$12 ; LITERALS_RUN_LEN_V2 + 15 ? |
||||
|
BCC PREPARE_COPY_LITERALS_DIRECT ; if less, literals count is complete |
||||
|
|
||||
|
JSR GETSRC ; get extra byte of variable literals count |
||||
|
; the carry is always set by the CMP above |
||||
|
; GETSRC doesn't change it |
||||
|
SBC #$EE ; overflow? |
||||
|
BRA PREPARE_COPY_LITERALS_DIRECT |
||||
|
|
||||
|
PREPARE_COPY_LITERALS_LARGE |
||||
|
; handle 16 bits literals count |
||||
|
; literals count = directly these 16 bits |
||||
|
JSR GETLARGESRC ; grab low 8 bits in X, high 8 bits in A |
||||
|
TAY ; put high 8 bits in Y |
||||
|
BCS PREPARE_COPY_LITERALS_HIGH ; (*same as JMP PREPARE_COPY_LITERALS_HIGH but shorter) |
||||
|
|
||||
|
PREPARE_COPY_LITERALS |
||||
|
LSR ; shift literals count into place |
||||
|
LSR |
||||
|
LSR |
||||
|
|
||||
|
PREPARE_COPY_LITERALS_DIRECT |
||||
|
TAX |
||||
|
BCS PREPARE_COPY_LITERALS_LARGE ; if so, literals count is large |
||||
|
|
||||
|
PREPARE_COPY_LITERALS_HIGH |
||||
|
TXA |
||||
|
BEQ COPY_LITERALS |
||||
|
INY |
||||
|
|
||||
|
COPY_LITERALS |
||||
|
JSR GETPUT ; copy one byte of literals |
||||
|
DEX |
||||
|
BNE COPY_LITERALS |
||||
|
DEY |
||||
|
BNE COPY_LITERALS |
||||
|
|
||||
|
NO_LITERALS |
||||
|
PLA ; retrieve token from stack |
||||
|
PHA ; preserve token again |
||||
|
ASL |
||||
|
BCS REPMATCH_OR_LARGE_OFFSET ; 1YZ: rep-match or 13/16 bit offset |
||||
|
|
||||
|
ASL ; 0YZ: 5 or 9 bit offset |
||||
|
BCS OFFSET_9_BIT |
||||
|
|
||||
|
; 00Z: 5 bit offset |
||||
|
|
||||
|
LDX #$FF ; set offset bits 15-8 to 1 |
||||
|
|
||||
|
JSR GETCOMBINEDBITS ; rotate Z bit into bit 0, read nibble for bits 4-1 |
||||
|
ORA #$E0 ; set bits 7-5 to 1 |
||||
|
BNE GOT_OFFSET_LO ; go store low byte of match offset and prepare match |
||||
|
|
||||
|
OFFSET_9_BIT ; 01Z: 9 bit offset |
||||
|
;;ASL ; shift Z (offset bit 8) in place |
||||
|
ROL |
||||
|
ROL |
||||
|
AND #$01 |
||||
|
EOR #$FF ; set offset bits 15-9 to 1 |
||||
|
BNE GOT_OFFSET_HI ; go store high byte, read low byte of match offset and prepare match |
||||
|
; (*same as JMP GOT_OFFSET_HI but shorter) |
||||
|
|
||||
|
REPMATCH_OR_LARGE_OFFSET |
||||
|
ASL ; 13 bit offset? |
||||
|
BCS REPMATCH_OR_16_BIT ; handle rep-match or 16-bit offset if not |
||||
|
|
||||
|
; 10Z: 13 bit offset |
||||
|
|
||||
|
JSR GETCOMBINEDBITS ; rotate Z bit into bit 8, read nibble for bits 12-9 |
||||
|
ADC #$DE ; set bits 15-13 to 1 and substract 2 (to substract 512) |
||||
|
BNE GOT_OFFSET_HI ; go store high byte, read low byte of match offset and prepare match |
||||
|
; (*same as JMP GOT_OFFSET_HI but shorter) |
||||
|
|
||||
|
REPMATCH_OR_16_BIT ; rep-match or 16 bit offset |
||||
|
;;ASL ; XYZ=111? |
||||
|
BMI REP_MATCH ; reuse previous offset if so (rep-match) |
||||
|
|
||||
|
; 110: handle 16 bit offset |
||||
|
JSR GETSRC ; grab high 8 bits |
||||
|
GOT_OFFSET_HI |
||||
|
TAX |
||||
|
JSR GETSRC ; grab low 8 bits |
||||
|
GOT_OFFSET_LO |
||||
|
STA OFFSLO ; store low byte of match offset |
||||
|
STX OFFSHI ; store high byte of match offset |
||||
|
|
||||
|
REP_MATCH |
||||
|
!ifdef BACKWARD_DECOMPRESS { |
||||
|
|
||||
|
; Backward decompression - substract match offset |
||||
|
|
||||
|
SEC ; add dest + match offset |
||||
|
REP #$20 |
||||
|
!al |
||||
|
LDA PUTDST+1 ; 16 bits |
||||
|
OFFSLO = *+1 |
||||
|
OFFSHI = *+2 |
||||
|
SBC #$AAAA |
||||
|
STA COPY_MATCH_LOOP+1 ; store back reference address |
||||
|
SEP #$20 |
||||
|
!as |
||||
|
SEC |
||||
|
|
||||
|
} else { |
||||
|
|
||||
|
; Forward decompression - add match offset |
||||
|
|
||||
|
CLC ; add dest + match offset |
||||
|
REP #$20 |
||||
|
!al |
||||
|
LDA PUTDST+1 ; 16 bits |
||||
|
OFFSLO = *+1 |
||||
|
OFFSHI = *+2 |
||||
|
ADC #$AAAA |
||||
|
STA COPY_MATCH_LOOP+1 ; store back reference address |
||||
|
SEP #$20 |
||||
|
!as |
||||
|
} |
||||
|
|
||||
|
LDA PUTDST+3 ; bank |
||||
|
STA COPY_MATCH_LOOP+3 ; store back reference address |
||||
|
|
||||
|
PLA ; retrieve token from stack again |
||||
|
AND #$07 ; isolate match len (MMM) |
||||
|
ADC #$01 ; add MIN_MATCH_SIZE_V2 and carry |
||||
|
CMP #$09 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2? |
||||
|
BCC PREPARE_COPY_MATCH ; if less, length is directly embedded in token |
||||
|
|
||||
|
JSR GETNIBBLE ; get extra match length nibble |
||||
|
; add nibble to len from token |
||||
|
ADC #$08 ; (MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2) minus carry |
||||
|
CMP #$18 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15? |
||||
|
BCC PREPARE_COPY_MATCH ; if less, match length is complete |
||||
|
|
||||
|
JSR GETSRC ; get extra byte of variable match length |
||||
|
; the carry is always set by the CMP above |
||||
|
; GETSRC doesn't change it |
||||
|
SBC #$E8 ; overflow? |
||||
|
|
||||
|
PREPARE_COPY_MATCH |
||||
|
TAX |
||||
|
BCC PREPARE_COPY_MATCH_Y ; if not, the match length is complete |
||||
|
BEQ DECOMPRESSION_DONE ; if EOD code, bail |
||||
|
|
||||
|
; Handle 16 bits match length |
||||
|
JSR GETLARGESRC ; grab low 8 bits in X, high 8 bits in A |
||||
|
TAY ; put high 8 bits in Y |
||||
|
|
||||
|
PREPARE_COPY_MATCH_Y |
||||
|
TXA |
||||
|
BEQ COPY_MATCH_LOOP |
||||
|
INY |
||||
|
|
||||
|
COPY_MATCH_LOOP |
||||
|
LDA $AAAAAA ; get one byte of backreference |
||||
|
JSR PUTDST ; copy to destination |
||||
|
|
||||
|
REP #$20 |
||||
|
!ifdef BACKWARD_DECOMPRESS { |
||||
|
|
||||
|
; Backward decompression -- put backreference bytes backward |
||||
|
|
||||
|
DEC COPY_MATCH_LOOP+1 |
||||
|
|
||||
|
} else { |
||||
|
|
||||
|
; Forward decompression -- put backreference bytes forward |
||||
|
|
||||
|
INC COPY_MATCH_LOOP+1 |
||||
|
|
||||
|
} |
||||
|
SEP #$20 |
||||
|
|
||||
|
DEX |
||||
|
BNE COPY_MATCH_LOOP |
||||
|
DEY |
||||
|
BNE COPY_MATCH_LOOP |
||||
|
JMP DECODE_TOKEN |
||||
|
|
||||
|
GETCOMBINEDBITS |
||||
|
EOR #$80 |
||||
|
ASL |
||||
|
PHP |
||||
|
|
||||
|
JSR GETNIBBLE ; get nibble into bits 0-3 (for offset bits 1-4) |
||||
|
PLP ; merge Z bit as the carry bit (for offset bit 0) |
||||
|
COMBINEDBITZ |
||||
|
ROL ; nibble -> bits 1-4; carry(!Z bit) -> bit 0 ; carry cleared |
||||
|
DECOMPRESSION_DONE |
||||
|
RTS |
||||
|
|
||||
|
GETNIBBLE |
||||
|
NIBBLES = *+1 |
||||
|
LDA #$AA |
||||
|
LSR NIBCOUNT |
||||
|
BCC NEED_NIBBLES |
||||
|
AND #$0F ; isolate low 4 bits of nibble |
||||
|
RTS |
||||
|
|
||||
|
NEED_NIBBLES |
||||
|
INC NIBCOUNT |
||||
|
JSR GETSRC ; get 2 nibbles |
||||
|
STA NIBBLES |
||||
|
LSR |
||||
|
LSR |
||||
|
LSR |
||||
|
LSR |
||||
|
SEC |
||||
|
RTS |
||||
|
|
||||
|
!ifdef BACKWARD_DECOMPRESS { |
||||
|
|
||||
|
; Backward decompression -- get and put bytes backward |
||||
|
|
||||
|
GETPUT |
||||
|
JSR GETSRC |
||||
|
PUTDST |
||||
|
LZSA_DST_LO = *+1 |
||||
|
LZSA_DST_HI = *+2 |
||||
|
LZSA_DST_BANK = *+3 |
||||
|
STA $AAAAAA |
||||
|
REP #$20 |
||||
|
DEC PUTDST+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
|
||||
|
GETLARGESRC |
||||
|
JSR GETSRC ; grab low 8 bits |
||||
|
TAX ; move to X |
||||
|
; fall through grab high 8 bits |
||||
|
|
||||
|
GETSRC |
||||
|
LZSA_SRC_LO = *+1 |
||||
|
LZSA_SRC_HI = *+2 |
||||
|
LZSA_SRC_BANK = *+3 |
||||
|
LDA $AAAAAA |
||||
|
REP #$20 |
||||
|
DEC GETSRC+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
|
||||
|
} else { |
||||
|
|
||||
|
; Forward decompression -- get and put bytes forward |
||||
|
|
||||
|
GETPUT |
||||
|
JSR GETSRC |
||||
|
PUTDST |
||||
|
LZSA_DST_LO = *+1 |
||||
|
LZSA_DST_HI = *+2 |
||||
|
LZSA_DST_BANK = *+3 |
||||
|
STA $AAAAAA |
||||
|
REP #$20 |
||||
|
INC PUTDST+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
|
||||
|
GETLARGESRC |
||||
|
JSR GETSRC ; grab low 8 bits |
||||
|
TAX ; move to X |
||||
|
; fall through grab high 8 bits |
||||
|
|
||||
|
GETSRC |
||||
|
LZSA_SRC_LO = *+1 |
||||
|
LZSA_SRC_HI = *+2 |
||||
|
LZSA_SRC_BANK = *+3 |
||||
|
LDA $AAAAAA |
||||
|
REP #$20 |
||||
|
INC GETSRC+1 |
||||
|
SEP #$20 |
||||
|
RTS |
||||
|
} |
||||
@ -0,0 +1,90 @@ |
|||||
|
; unlzsa1-6309.s - Hitachi 6309 decompression routine for raw LZSA1 - 92 bytes |
||||
|
; compress with lzsa -f1 -r <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = start of compressed data |
||||
|
; y = start of decompression buffer |
||||
|
; out: y = end of decompression buffer + 1 |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty, Doug Masten |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa1 equ lz1token |
||||
|
|
||||
|
lz1bigof lda ,x+ ; O set: load MSB 16-bit (negative, signed) offest |
||||
|
lz1gotof leau d,y ; put backreference start address in U (dst+offset) |
||||
|
|
||||
|
ldd #$000f ; clear MSB match length and set mask for MMMM |
||||
|
andb ,s+ ; isolate MMMM (embedded match length) in token |
||||
|
addb #$03 ; add MIN_MATCH_SIZE |
||||
|
cmpb #$12 ; MATCH_RUN_LEN? |
||||
|
bne lz1gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
addb ,x+ ; add extra match length byte + MIN_MATCH_SIZE + MATCH_RUN_LEN |
||||
|
bcc lz1gotln ; if no overflow, we have the full length |
||||
|
bne lz1midln |
||||
|
|
||||
|
ldb ,x+ ; load 16-bit len in D (low part in B, high in A) |
||||
|
lda ,x+ ; (little endian) |
||||
|
bne lz1gotln ; check if we hit EOD (16-bit length = 0) |
||||
|
tstb |
||||
|
bne lz1gotln ; go copy matched bytes if not |
||||
|
|
||||
|
rts ; done, bail |
||||
|
|
||||
|
lz1midln tfr b,a ; copy high part of len into A |
||||
|
ldb ,x+ ; grab low 8 bits of len in B |
||||
|
|
||||
|
lz1gotln tfr d,w ; set W with match length for TFM instruction |
||||
|
tfm u+,y+ ; copy match bytes |
||||
|
|
||||
|
lz1token ldb ,x+ ; load next token into B: O|LLL|MMMM |
||||
|
pshs b ; save it |
||||
|
|
||||
|
andb #$70 ; isolate LLL (embedded literals count) in B |
||||
|
beq lz1nolt ; skip if no literals |
||||
|
cmpb #$70 ; LITERALS_RUN_LEN? |
||||
|
bne lz1declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
ldb ,x+ ; load extra literals count byte |
||||
|
addb #$07 ; add LITERALS_RUN_LEN |
||||
|
bcc lz1gotla ; if no overflow, we got the complete count, copy |
||||
|
bne lz1midlt |
||||
|
|
||||
|
ldb ,x+ ; load low 8 bits of little-endian literals count |
||||
|
lda ,x+ ; load high 8 bits of literal count |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1midlt tfr b,a ; copy high part of literals count into A |
||||
|
ldb ,x+ ; load low 8 bits of literals count |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
|
||||
|
lz1gotla clra ; clear A (high part of literals count) |
||||
|
lz1gotlt tfr d,w ; set W with literals count for TFM instruction |
||||
|
tfm x+,y+ ; copy literal bytes |
||||
|
|
||||
|
lz1nolt ldb ,x+ ; load either 8-bit or LSB 16-bit offset (negative, signed) |
||||
|
lda ,s ; get token again, don't pop it from the stack |
||||
|
bmi lz1bigof ; test O bit (small or large offset) |
||||
|
|
||||
|
lda #$ff ; set high 8 bits |
||||
|
bra lz1gotof |
||||
@ -0,0 +1,102 @@ |
|||||
|
; unlzsa1.s - 6809 decompression routine for raw LZSA1 - 110 bytes |
||||
|
; compress with lzsa -r <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = start of compressed data |
||||
|
; y = start of decompression buffer |
||||
|
; out: y = end of decompression buffer + 1 |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa1 equ lz1token |
||||
|
|
||||
|
lz1bigof lda ,x+ ; O set: load MSB 16-bit (negative, signed) offest |
||||
|
lz1gotof leau d,y ; put backreference start address in U (dst+offset) |
||||
|
|
||||
|
ldd #$000f ; clear MSB match length and set mask for MMMM |
||||
|
andb ,s+ ; isolate MMMM (embedded match length) in token |
||||
|
addb #$03 ; add MIN_MATCH_SIZE |
||||
|
cmpb #$12 ; MATCH_RUN_LEN? |
||||
|
bne lz1gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
addb ,x+ ; add extra match length byte + MIN_MATCH_SIZE + MATCH_RUN_LEN |
||||
|
bcc lz1gotln ; if no overflow, we have the full length |
||||
|
bne lz1midln |
||||
|
|
||||
|
ldb ,x+ ; load 16-bit len in D (low part in B, high in A) |
||||
|
lda ,x+ ; (little endian) |
||||
|
bne lz1gotln ; check if we hit EOD (16-bit length = 0) |
||||
|
tstb |
||||
|
bne lz1gotln ; go copy matched bytes if not |
||||
|
|
||||
|
rts ; done, bail |
||||
|
|
||||
|
lz1midln tfr b,a ; copy high part of len into A |
||||
|
ldb ,x+ ; grab low 8 bits of len in B |
||||
|
|
||||
|
lz1gotln pshs x ; save source compressed data pointer |
||||
|
tfr d,x ; copy match length to X |
||||
|
|
||||
|
lz1cpymt lda ,u+ ; copy matched byte |
||||
|
sta ,y+ |
||||
|
leax -1,x ; decrement X |
||||
|
bne lz1cpymt ; loop until all matched bytes are copied |
||||
|
|
||||
|
puls x ; restore source compressed data pointer |
||||
|
|
||||
|
lz1token ldb ,x+ ; load next token into B: O|LLL|MMMM |
||||
|
pshs b ; save it |
||||
|
|
||||
|
andb #$70 ; isolate LLL (embedded literals count) in B |
||||
|
beq lz1nolt ; skip if no literals |
||||
|
cmpb #$70 ; LITERALS_RUN_LEN? |
||||
|
bne lz1declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
ldb ,x+ ; load extra literals count byte |
||||
|
addb #$07 ; add LITERALS_RUN_LEN |
||||
|
bcc lz1gotla ; if no overflow, we got the complete count, copy |
||||
|
bne lz1midlt |
||||
|
|
||||
|
ldb ,x+ ; load low 8 bits of little-endian literals count |
||||
|
lda ,x+ ; load high 8 bits of literal count |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1midlt tfr b,a ; copy high part of literals count into A |
||||
|
ldb ,x+ ; load low 8 bits of literals count |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lz1gotla clra ; clear A (high part of literals count) |
||||
|
|
||||
|
lz1gotlt leau ,x |
||||
|
tfr d,x ; transfer 16-bit count into X |
||||
|
lz1cpylt lda ,u+ ; copy literal byte |
||||
|
sta ,y+ |
||||
|
leax -1,x ; decrement X and update Z flag |
||||
|
bne lz1cpylt ; loop until all literal bytes are copied |
||||
|
leax ,u |
||||
|
|
||||
|
lz1nolt ldb ,x+ ; load either 8-bit or LSB 16-bit offset (negative, signed) |
||||
|
lda ,s ; get token again, don't pop it from the stack |
||||
|
bmi lz1bigof ; test O bit (small or large offset) |
||||
|
|
||||
|
lda #$ff ; set high 8 bits |
||||
|
bra lz1gotof |
||||
@ -0,0 +1,92 @@ |
|||||
|
; unlzsa1-6309.s - H6309 backward decompressor for raw LZSA1 - 97 bytes |
||||
|
; compress with lzsa -f1 -r -b <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = last byte of compressed data |
||||
|
; y = last byte of decompression buffer |
||||
|
; out: y = first byte of decompressed data |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty, Doug Masten |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa1 |
||||
|
leax 1,x |
||||
|
bra lz1token |
||||
|
|
||||
|
lz1bigof ldd ,--x ; O set: load long 16-bit (negative, signed) offest |
||||
|
lz1gotof negd ; reverse sign of offset in D |
||||
|
leau d,y ; put backreference start address in U (dst+offset) |
||||
|
|
||||
|
ldd #$000f ; clear MSB match length and set mask for MMMM |
||||
|
andb ,s+ ; isolate MMMM (embedded match length) in token |
||||
|
addb #$03 ; add MIN_MATCH_SIZE |
||||
|
cmpb #$12 ; MATCH_RUN_LEN? |
||||
|
bne lz1gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
addb ,-x ; add extra match length byte + MIN_MATCH_SIZE + MATCH_RUN_LEN |
||||
|
bcc lz1gotln ; if no overflow, we have the full length |
||||
|
bne lz1midln |
||||
|
|
||||
|
ldd ,--x ; load 16-bit len in D (low part in B, high in A) |
||||
|
bne lz1gotln ; check if we hit EOD (16-bit length = 0) |
||||
|
|
||||
|
leay 1,y ; adjust pointer to first byte of decompressed data |
||||
|
rts ; done, bail |
||||
|
|
||||
|
lz1midln tfr b,a ; copy high part of len into A |
||||
|
ldb ,-x ; grab low 8 bits of len in B |
||||
|
|
||||
|
lz1gotln tfr d,w ; set W with match length for TFM instruction |
||||
|
tfm u-,y- ; copy match bytes |
||||
|
|
||||
|
lz1token ldb ,-x ; load next token into B: O|LLL|MMMM |
||||
|
pshs b ; save it |
||||
|
|
||||
|
andb #$70 ; isolate LLL (embedded literals count) in B |
||||
|
beq lz1nolt ; skip if no literals |
||||
|
cmpb #$70 ; LITERALS_RUN_LEN? |
||||
|
bne lz1declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
ldb ,-x ; load extra literals count byte |
||||
|
addb #$07 ; add LITERALS_RUN_LEN |
||||
|
bcc lz1gotla ; if no overflow, we got the complete count, copy |
||||
|
bne lz1midlt |
||||
|
|
||||
|
ldd ,--x ; load 16 bit count in D (low part in B, high in A) |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1midlt tfr b,a ; copy high part of literals count into A |
||||
|
ldb ,-x ; load low 8 bits of literals count |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
|
||||
|
lz1gotla clra ; clear A (high part of literals count) |
||||
|
lz1gotlt tfr d,w ; set W with literals count for TFM instruction |
||||
|
leax -1,x ; tfm is post-decrement |
||||
|
tfm x-,y- ; copy literal bytes |
||||
|
leax 1,x |
||||
|
|
||||
|
lz1nolt ldb ,s ; get token again, don't pop it from the stack |
||||
|
bmi lz1bigof ; test O bit (small or large offset) |
||||
|
|
||||
|
ldb ,-x ; load either 8-bit or LSB 16-bit offset (negative, signed) |
||||
|
lda #$ff ; set high 8 bits |
||||
|
bra lz1gotof |
||||
@ -0,0 +1,105 @@ |
|||||
|
; unlzsa1b.s - 6809 backward decompression routine for raw LZSA1 - 113 bytes |
||||
|
; compress with lzsa -r -b <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = last byte of compressed data |
||||
|
; y = last byte of decompression buffer |
||||
|
; out: y = first byte of decompressed data |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa1 |
||||
|
leax 1,x |
||||
|
leay 1,y |
||||
|
bra lz1token |
||||
|
|
||||
|
lz1bigof ldd ,--x ; O set: load long 16 bit (negative, signed) offset |
||||
|
lz1gotof nega ; reverse sign of offset in D |
||||
|
negb |
||||
|
sbca #0 |
||||
|
leau d,y ; put backreference start address in U (dst+offset) |
||||
|
|
||||
|
ldd #$000f ; clear MSB match length and set mask for MMMM |
||||
|
andb ,s+ ; isolate MMMM (embedded match length) in token |
||||
|
|
||||
|
addb #$03 ; add MIN_MATCH_SIZE |
||||
|
cmpb #$12 ; MATCH_RUN_LEN? |
||||
|
bne lz1gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
addb ,-x ; add extra match length byte + MIN_MATCH_SIZE + MATCH_RUN_LEN |
||||
|
bcc lz1gotln ; if no overflow, we have the full length |
||||
|
bne lz1midln |
||||
|
|
||||
|
ldd ,--x ; load 16-bit len in D (low part in B, high in A) |
||||
|
bne lz1gotln ; check if we hit EOD (16-bit length = 0) |
||||
|
|
||||
|
rts ; done, bail |
||||
|
|
||||
|
lz1midln tfr b,a ; copy high part of len into A |
||||
|
ldb ,-x ; grab low 8 bits of len in B |
||||
|
|
||||
|
lz1gotln pshs x ; save source compressed data pointer |
||||
|
tfr d,x ; copy match length to X |
||||
|
|
||||
|
lz1cpymt lda ,-u ; copy matched byte |
||||
|
sta ,-y |
||||
|
leax -1,x ; decrement X |
||||
|
bne lz1cpymt ; loop until all matched bytes are copied |
||||
|
|
||||
|
puls x ; restore source compressed data pointer |
||||
|
|
||||
|
lz1token ldb ,-x ; load next token into B: O|LLL|MMMM |
||||
|
pshs b ; save it |
||||
|
|
||||
|
andb #$70 ; isolate LLL (embedded literals count) in B |
||||
|
beq lz1nolt ; skip if no literals |
||||
|
cmpb #$70 ; LITERALS_RUN_LEN? |
||||
|
bne lz1declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
ldb ,-x ; load extra literals count byte |
||||
|
addb #$07 ; add LITERALS_RUN_LEN |
||||
|
bcc lz1gotla ; if no overflow, we got the complete count, copy |
||||
|
bne lz1midlt |
||||
|
|
||||
|
ldd ,--x ; load 16 bit count in D (low part in B, high in A) |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1midlt tfr b,a ; copy high part of literals count into A |
||||
|
ldb ,-x ; load low 8 bits of literals count |
||||
|
bra lz1gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz1declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
|
||||
|
lz1gotla clra ; clear A (high part of literals count) |
||||
|
lz1gotlt leau ,x |
||||
|
tfr d,x ; transfer 16-bit count into X |
||||
|
lz1cpylt lda ,-u ; copy literal byte |
||||
|
sta ,-y |
||||
|
leax -1,x ; decrement X and update Z flag |
||||
|
bne lz1cpylt ; loop until all literal bytes are copied |
||||
|
leax ,u |
||||
|
|
||||
|
lz1nolt ldb ,s ; get token again, don't pop it from the stack |
||||
|
bmi lz1bigof ; test O bit (small or large offset) |
||||
|
|
||||
|
ldb ,-x ; O clear: load 8 bit (negative, signed) offset |
||||
|
lda #$ff ; set high 8 bits |
||||
|
bra lz1gotof |
||||
@ -0,0 +1,129 @@ |
|||||
|
; unlzsa2-6309.s - Hitachi 6309 decompression routine for raw LZSA2 - 150 bytes |
||||
|
; compress with lzsa -f2 -r <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = start of compressed data |
||||
|
; y = start of decompression buffer |
||||
|
; out: y = end of decompression buffer + 1 |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty, Doug Masten |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa2 |
||||
|
clr lz2nibct ; reset nibble available flag |
||||
|
bra lz2token |
||||
|
|
||||
|
lz2nibct fcb 0 ; nibble ready flag |
||||
|
|
||||
|
lz2replg lslb ; push token's Y flag bit into carry |
||||
|
bcs lz2rep16 ; if token's Y bit is set, rep or 16 bit offset |
||||
|
|
||||
|
sex ; push token's Z flag bit into reg A |
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; push token's Z flag bit into carry |
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 13-15 of offset, reverse bit 8 |
||||
|
tfr b,a ; copy bits 8-15 of offset into A |
||||
|
suba #$02 ; substract 512 from offset |
||||
|
ldb ,x+ ; load low 8 bits of (negative, signed) offset |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2rep16 bmi lz2repof ; if token's Z flag bit is set, rep match |
||||
|
ldd ,x++ ; load high then low 8 bits of offset |
||||
|
|
||||
|
lz2gotof std lz2moff+2 ; store match offset |
||||
|
|
||||
|
lz2repof ldd #$0007 ; clear MSB match length and set mask for MMM |
||||
|
andb ,u ; isolate MMM (embedded match length) in token |
||||
|
lz2moff leau $aaaa,y ; put backreference start address in U (dst+offset) |
||||
|
addb #$02 ; add MIN_MATCH_SIZE_V2 |
||||
|
cmpb #$09 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2? |
||||
|
bne lz2gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
addb #$09 ; add MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 |
||||
|
cmpb #$18 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15? |
||||
|
bne lz2gotln ; if not, we have the full match length, go copy |
||||
|
|
||||
|
addb ,x+ ; add extra length byte + MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15 |
||||
|
bcc lz2gotln ; if no overflow, we have the full length |
||||
|
beq lz2done ; detect EOD code |
||||
|
|
||||
|
ldb ,x+ ; load 16-bit len in D (low part in B, high in A) |
||||
|
lda ,x+ ; (little endian) |
||||
|
|
||||
|
lz2gotln tfr d,w ; set W with match count for TFM instruction |
||||
|
tfm u+,y+ ; copy match bytes |
||||
|
|
||||
|
lz2token tfr x,u ; save token address |
||||
|
ldb ,x+ ; load next token into B: XYZ|LL|MMM |
||||
|
andb #$18 ; isolate LL (embedded literals count) in B |
||||
|
beq lz2nolt ; skip if no literals |
||||
|
cmpb #$18 ; LITERALS_RUN_LEN_V2? |
||||
|
bne lz2declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
bsr lz2nibl ; get extra literals length nibble in B |
||||
|
addb #$03 ; add LITERALS_RUN_LEN_V2 |
||||
|
cmpb #$12 ; LITERALS_RUN_LEN_V2 + 15 ? |
||||
|
bne lz2gotla ; if not, we have the full literals count, go copy |
||||
|
|
||||
|
addb ,x+ ; add extra literals count byte + LITERALS_RUN_LEN + 15 |
||||
|
bcc lz2gotla ; if no overflow, we got the complete count, copy |
||||
|
|
||||
|
ldb ,x+ ; load low 8 bits of little-endian literals count |
||||
|
lda ,x+ ; load high 8 bits of literal count |
||||
|
bra lz2gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz2declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lz2gotla clra ; clear A (high part of literals count) |
||||
|
|
||||
|
lz2gotlt tfr d,w ; set W with literals count for TFM instruction |
||||
|
tfm x+,y+ ; copy literal bytes |
||||
|
|
||||
|
lz2nolt ldb ,u ; get token again |
||||
|
lslb ; push token's X flag bit into carry |
||||
|
bcs lz2replg ; if token's X bit is set, rep or large offset |
||||
|
|
||||
|
lslb ; push token's Y flag bit into carry |
||||
|
sex ; push token's Z flag bit into reg A (carry flag is not effected) |
||||
|
bcs lz2offs9 ; if token's Y bit is set, 9 bits offset |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; retrieve token's Z flag bit and push into carry |
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 5-7 of offset, reverse bit 0 |
||||
|
sex ; set bits 8-15 of offset to $FF |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2offs9 deca ; set bits 9-15 of offset, reverse bit 8 |
||||
|
ldb ,x+ ; load low 8 bits of (negative, signed) offset |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2nibl ldb #$aa |
||||
|
com lz2nibct ; nibble ready? |
||||
|
bpl lz2gotnb |
||||
|
|
||||
|
ldb ,x+ ; load two nibbles |
||||
|
stb lz2nibl+1 ; store nibble for next time (low 4 bits) |
||||
|
lsrb ; shift 4 high bits of nibble down |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lz2gotnb andb #$0f ; only keep low 4 bits |
||||
|
lz2done rts |
||||
@ -0,0 +1,146 @@ |
|||||
|
; unlzsa2.s - 6809 decompression routine for raw LZSA2 - 169 bytes |
||||
|
; compress with lzsa -f2 -r <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = start of compressed data |
||||
|
; y = start of decompression buffer |
||||
|
; out: y = end of decompression buffer + 1 |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa2 |
||||
|
clr <lz2nibct,pcr ; reset nibble available flag |
||||
|
|
||||
|
lz2token ldb ,x+ ; load next token into B: XYZ|LL|MMM |
||||
|
pshs b ; save it |
||||
|
|
||||
|
andb #$18 ; isolate LL (embedded literals count) in B |
||||
|
beq lz2nolt ; skip if no literals |
||||
|
cmpb #$18 ; LITERALS_RUN_LEN_V2? |
||||
|
bne lz2declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
bsr lz2nibl ; get extra literals length nibble in B |
||||
|
addb #$03 ; add LITERALS_RUN_LEN_V2 |
||||
|
cmpb #$12 ; LITERALS_RUN_LEN_V2 + 15 ? |
||||
|
bne lz2gotla ; if not, we have the full literals count, go copy |
||||
|
|
||||
|
addb ,x+ ; add extra literals count byte + LITERALS_RUN_LEN + 15 |
||||
|
bcc lz2gotla ; if no overflow, we got the complete count, copy |
||||
|
|
||||
|
ldb ,x+ ; load low 8 bits of little-endian literals count |
||||
|
lda ,x+ ; load high 8 bits of literal count |
||||
|
bra lz2gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz2declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lz2gotla clra ; clear A (high part of literals count) |
||||
|
|
||||
|
lz2gotlt leau ,x |
||||
|
tfr d,x ; transfer 16-bit count into X |
||||
|
lz2cpylt lda ,u+ ; copy literal byte |
||||
|
sta ,y+ |
||||
|
leax -1,x ; decrement X and update Z flag |
||||
|
bne lz2cpylt ; loop until all literal bytes are copied |
||||
|
leax ,u |
||||
|
|
||||
|
lz2nolt ldb ,s ; get token again, don't pop it from the stack |
||||
|
|
||||
|
lslb ; push token's X flag bit into carry |
||||
|
bcs lz2replg ; if token's X bit is set, rep or large offset |
||||
|
|
||||
|
lslb ; push token's Y flag bit into carry |
||||
|
sex ; push token's Z flag bit into reg A (carry flag is not effected) |
||||
|
bcs lz2offs9 ; if token's Y bit is set, 9 bits offset |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; retrieve token's Z flag bit and push into carry |
||||
|
|
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 5-7 of offset, reverse bit 0 |
||||
|
sex ; set bits 8-15 of offset to $FF |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2offs9 deca ; set bits 9-15 of offset, reverse bit 8 |
||||
|
ldb ,x+ ; load low 8 bits of (negative, signed) offset |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2nibct fcb $00 ; nibble ready flag |
||||
|
|
||||
|
lz2nibl ldb #$aa |
||||
|
com <lz2nibct,pcr ; toggle nibble ready flag and check |
||||
|
bpl lz2gotnb |
||||
|
|
||||
|
ldb ,x+ ; load two nibbles |
||||
|
stb <lz2nibl+1,pcr ; store nibble for next time (low 4 bits) |
||||
|
|
||||
|
lsrb ; shift 4 high bits of nibble down |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
|
||||
|
lz2gotnb andb #$0f ; only keep low 4 bits |
||||
|
lz2done rts |
||||
|
|
||||
|
lz2replg lslb ; push token's Y flag bit into carry |
||||
|
bcs lz2rep16 ; if token's Y bit is set, rep or 16 bit offset |
||||
|
|
||||
|
sex ; push token's Z flag bit into reg A |
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; push token's Z flag bit into carry |
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 13-15 of offset, reverse bit 8 |
||||
|
tfr b,a ; copy bits 8-15 of offset into A |
||||
|
suba #$02 ; substract 512 from offset |
||||
|
ldb ,x+ ; load low 8 bits of (negative, signed) offset |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2rep16 bmi lz2repof ; if token's Z flag bit is set, rep match |
||||
|
ldd ,x++ ; load high then low 8 bits of offset |
||||
|
|
||||
|
lz2gotof std <lz2repof+2,pcr ; store match offset |
||||
|
lz2repof leau $aaaa,y ; put backreference start address in U (dst+offset) |
||||
|
|
||||
|
ldd #$0007 ; clear MSB match length and set mask for MMM |
||||
|
andb ,s+ ; isolate MMM (embedded match length) in token |
||||
|
addb #$02 ; add MIN_MATCH_SIZE_V2 |
||||
|
cmpb #$09 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2? |
||||
|
bne lz2gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
addb #$09 ; add MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 |
||||
|
cmpb #$18 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15? |
||||
|
bne lz2gotln ; if not, we have the full match length, go copy |
||||
|
|
||||
|
addb ,x+ ; add extra length byte + MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15 |
||||
|
bcc lz2gotln ; if no overflow, we have the full length |
||||
|
beq lz2done ; detect EOD code |
||||
|
|
||||
|
ldb ,x+ ; load 16-bit len in D (low part in B, high in A) |
||||
|
lda ,x+ ; (little endian) |
||||
|
|
||||
|
lz2gotln pshs x ; save source compressed data pointer |
||||
|
tfr d,x ; copy match length to X |
||||
|
|
||||
|
lz2cpymt lda ,u+ ; copy matched byte |
||||
|
sta ,y+ |
||||
|
leax -1,x ; decrement X |
||||
|
bne lz2cpymt ; loop until all matched bytes are copied |
||||
|
|
||||
|
puls x ; restore source compressed data pointer |
||||
|
lbra lz2token ; go decode next token |
||||
@ -0,0 +1,133 @@ |
|||||
|
; unlzsa2b-6309.s - H6309 backward decompressor for raw LZSA2 - 155 bytes |
||||
|
; compress with lzsa -f2 -r -b <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = last byte of compressed data |
||||
|
; y = last byte of decompression buffer |
||||
|
; out: y = first byte of decompressed data |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty, Doug Masten |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa2 |
||||
|
clr lz2nibct ; reset nibble available flag |
||||
|
leax 1,x ; adjust compressed data pointer |
||||
|
bra lz2token |
||||
|
|
||||
|
lz2nibct fcb 0 ; nibble ready flag |
||||
|
|
||||
|
lz2replg lslb ; push token's Y flag bit into carry |
||||
|
bcs lz2rep16 ; if token's Y bit is set, rep or 16 bit offset |
||||
|
|
||||
|
sex ; push token's Z flag bit into reg A |
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; push token's Z flag bit into carry |
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 13-15 of offset, reverse bit 8 |
||||
|
tfr b,a ; copy bits 8-15 of offset into A |
||||
|
suba #$02 ; substract 512 from offset |
||||
|
bra lz2lowof |
||||
|
|
||||
|
lz2rep16 bmi lz2repof ; if token's Z flag bit is set, rep match |
||||
|
lda ,-x ; load high 8 bits of (negative, signed) offset |
||||
|
lz2lowof ldb ,-x ; load low 8 bits of offset |
||||
|
|
||||
|
lz2gotof negd ; reverse sign of offset in D |
||||
|
std lz2moff+2 ; store match offset |
||||
|
|
||||
|
lz2repof ldd #$0007 ; clear MSB match length and set mask for MMM |
||||
|
andb ,u ; isolate MMM (embedded match length) in token |
||||
|
lz2moff leau $aaaa,y ; put backreference start address in U (dst+offset) |
||||
|
addb #$02 ; add MIN_MATCH_SIZE_V2 |
||||
|
cmpb #$09 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2? |
||||
|
bne lz2gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
addb #$09 ; add MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 |
||||
|
cmpb #$18 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15? |
||||
|
bne lz2gotln ; if not, we have the full match length, go copy |
||||
|
|
||||
|
addb ,-x ; add extra length byte + MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15 |
||||
|
bcc lz2gotln ; if no overflow, we have the full length |
||||
|
beq lz2done ; detect EOD code |
||||
|
|
||||
|
ldd ,--x ; load 16-bit len in D (low part in B, high in A) |
||||
|
|
||||
|
lz2gotln tfr d,w ; set W with match count for TFM instruction |
||||
|
tfm u-,y- ; copy match bytes |
||||
|
|
||||
|
lz2token ldb ,-x ; load next token into B: XYZ|LL|MMM |
||||
|
tfr x,u ; save token address |
||||
|
andb #$18 ; isolate LL (embedded literals count) in B |
||||
|
beq lz2nolt ; skip if no literals |
||||
|
cmpb #$18 ; LITERALS_RUN_LEN_V2? |
||||
|
bne lz2declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
bsr lz2nibl ; get extra literals length nibble in B |
||||
|
addb #$03 ; add LITERALS_RUN_LEN_V2 |
||||
|
cmpb #$12 ; LITERALS_RUN_LEN_V2 + 15 ? |
||||
|
bne lz2gotla ; if not, we have the full literals count, go copy |
||||
|
|
||||
|
addb ,-x ; add extra literals count byte + LITERALS_RUN_LEN + 15 |
||||
|
bcc lz2gotla ; if no overflow, we got the complete count, copy |
||||
|
|
||||
|
ldd ,--x ; load 16 bit count in D (low part in B, high in A) |
||||
|
bra lz2gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz2nibl com lz2nibct ; nibble ready? |
||||
|
bpl lz2gotnb |
||||
|
|
||||
|
ldb ,-x ; load two nibbles |
||||
|
stb lz2gotnb+1 ; store nibble for next time (low 4 bits) |
||||
|
lsrb ; shift 4 high bits of nibble down |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
rts |
||||
|
|
||||
|
lz2declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lz2gotla clra ; clear A (high part of literals count) |
||||
|
|
||||
|
lz2gotlt tfr d,w ; set W with literals count for TFM instruction |
||||
|
leax -1,x ; tfm is post-decrement |
||||
|
tfm x-,y- ; copy literal bytes |
||||
|
leax 1,x |
||||
|
|
||||
|
lz2nolt ldb ,u ; get token again |
||||
|
lslb ; push token's X flag bit into carry |
||||
|
bcs lz2replg ; if token's X bit is set, rep or large offset |
||||
|
|
||||
|
lslb ; push token's Y flag bit into carry |
||||
|
sex ; push token's Z flag bit into reg A (carry flag is not effected) |
||||
|
bcs lz2offs9 ; if token's Y bit is set, 9 bits offset |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; retrieve token's Z flag bit and push into carry |
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 5-7 of offset, reverse bit 0 |
||||
|
sex ; set bits 8-15 of offset to $FF |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2offs9 deca ; set bits 9-15 of offset, reverse bit 8 |
||||
|
bra lz2lowof |
||||
|
|
||||
|
lz2done leay 1,y ; adjust pointer to first byte of decompressed data and then exit |
||||
|
lz2gotnb ldb #$aa ; load nibble |
||||
|
andb #$0f ; only keep low 4 bits |
||||
|
rts |
||||
@ -0,0 +1,152 @@ |
|||||
|
; unlzsa2b.s - 6809 backward decompression routine for raw LZSA2 - 171 bytes |
||||
|
; compress with lzsa -f2 -r -b <original_file> <compressed_file> |
||||
|
; |
||||
|
; in: x = last byte of compressed data |
||||
|
; y = last byte of decompression buffer |
||||
|
; out: y = first byte of decompressed data |
||||
|
; |
||||
|
; Copyright (C) 2020 Emmanuel Marty |
||||
|
; |
||||
|
; 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. |
||||
|
|
||||
|
decompress_lzsa2 |
||||
|
clr <lz2nibct,pcr ; reset nibble available flag |
||||
|
leax 1,x |
||||
|
leay 1,y |
||||
|
|
||||
|
lz2token ldb ,-x ; load next token into B: XYZ|LL|MMM |
||||
|
pshs b ; save it |
||||
|
|
||||
|
andb #$18 ; isolate LLL (embedded literals count) in B |
||||
|
beq lz2nolt ; skip if no literals |
||||
|
cmpb #$18 ; LITERALS_RUN_LEN_V2? |
||||
|
bne lz2declt ; if not, we have the complete count, go unshift |
||||
|
|
||||
|
bsr lz2nibl ; get extra literals length nibble in B |
||||
|
addb #$03 ; add LITERALS_RUN_LEN_V2 |
||||
|
cmpb #$12 ; LITERALS_RUN_LEN_V2 + 15 ? |
||||
|
bne lz2gotla ; if not, we have the full literals count, go copy |
||||
|
|
||||
|
addb ,-x ; add extra literals count byte + LITERALS_RUN_LEN + 15 |
||||
|
bcc lz2gotla ; if no overflow, we got the complete count, copy |
||||
|
|
||||
|
ldd ,--x ; load 16 bit count in D (low part in B, high in A) |
||||
|
bra lz2gotlt ; we now have the complete count, go copy |
||||
|
|
||||
|
lz2declt lsrb ; shift literals count into place |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lz2gotla clra ; clear A (high part of literals count) |
||||
|
|
||||
|
lz2gotlt leau ,x |
||||
|
tfr d,x ; transfer 16-bit count into X |
||||
|
lz2cpylt lda ,-u ; copy literal byte |
||||
|
sta ,-y |
||||
|
leax -1,x ; decrement X and update Z flag |
||||
|
bne lz2cpylt ; loop until all literal bytes are copied |
||||
|
leax ,u |
||||
|
|
||||
|
lz2nolt ldb ,s ; get token again, don't pop it from the stack |
||||
|
|
||||
|
lslb ; push token's X flag bit into carry |
||||
|
bcs lz2replg ; if token's X bit is set, rep or large offset |
||||
|
|
||||
|
lslb ; push token's Y flag bit into carry |
||||
|
sex ; push token's Z flag bit into reg A (carry flag is not effected) |
||||
|
bcs lz2offs9 ; if token's Y bit is set, 9 bits offset |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; retrieve token's Z flag bit and push into carry |
||||
|
|
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 5-7 of offset, reverse bit 0 |
||||
|
sex ; set bits 8-15 of offset to $FF |
||||
|
bra lz2gotof |
||||
|
|
||||
|
lz2offs9 deca ; set bits 9-15 of offset, reverse bit 8 |
||||
|
bra lz2lowof |
||||
|
|
||||
|
lz2nibct fcb $00 ; nibble ready flag |
||||
|
|
||||
|
lz2nibl ldb #$aa |
||||
|
com <lz2nibct,pcr ; toggle nibble ready flag and check |
||||
|
bpl lz2gotnb |
||||
|
|
||||
|
ldb ,-x ; load two nibbles |
||||
|
stb <lz2nibl+1,pcr ; store nibble for next time (low 4 bits) |
||||
|
|
||||
|
lsrb ; shift 4 high bits of nibble down |
||||
|
lsrb |
||||
|
lsrb |
||||
|
lsrb |
||||
|
|
||||
|
lz2gotnb andb #$0f ; only keep low 4 bits |
||||
|
lz2done rts |
||||
|
|
||||
|
lz2replg lslb ; push token's Y flag bit into carry |
||||
|
bcs lz2rep16 ; if token's Y bit is set, rep or 16 bit offset |
||||
|
|
||||
|
sex ; push token's Z flag bit into reg A |
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
lsla ; retrieve token's Z flag bit and push into carry |
||||
|
|
||||
|
rolb ; shift Z flag from carry into bit 0 of B |
||||
|
eorb #$e1 ; set bits 13-15 of offset, reverse bit 8 |
||||
|
tfr b,a ; copy bits 8-15 of offset into A |
||||
|
suba #$02 ; substract 512 from offset |
||||
|
bra lz2lowof |
||||
|
|
||||
|
lz2rep16 bmi lz2repof ; if token's Z flag bit is set, rep match |
||||
|
|
||||
|
lda ,-x ; load high 8 bits of (negative, signed) offset |
||||
|
lz2lowof ldb ,-x ; load low 8 bits of offset |
||||
|
|
||||
|
lz2gotof nega ; reverse sign of offset in D |
||||
|
negb |
||||
|
sbca #0 |
||||
|
std <lz2repof+2,pcr ; store match offset |
||||
|
|
||||
|
lz2repof leau $aaaa,y ; put backreference start address in U (dst+offset) |
||||
|
|
||||
|
ldd #$0007 ; clear MSB match length and set mask for MMM |
||||
|
andb ,s+ ; isolate MMM (embedded match length) in token |
||||
|
|
||||
|
addb #$02 ; add MIN_MATCH_SIZE_V2 |
||||
|
cmpb #$09 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2? |
||||
|
bne lz2gotln ; no, we have the full match length, go copy |
||||
|
|
||||
|
bsr lz2nibl ; get offset nibble in B |
||||
|
addb #$09 ; add MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 |
||||
|
cmpb #$18 ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15? |
||||
|
bne lz2gotln ; if not, we have the full match length, go copy |
||||
|
|
||||
|
addb ,-x ; add extra length byte + MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15 |
||||
|
bcc lz2gotln ; if no overflow, we have the full length |
||||
|
beq lz2done ; detect EOD code |
||||
|
|
||||
|
ldd ,--x ; load 16-bit len in D (low part in B, high in A) |
||||
|
|
||||
|
lz2gotln pshs x ; save source compressed data pointer |
||||
|
tfr d,x ; copy match length to X |
||||
|
|
||||
|
lz2cpymt lda ,-u ; copy matched byte |
||||
|
sta ,-y |
||||
|
leax -1,x ; decrement X |
||||
|
bne lz2cpymt ; loop until all matched bytes are copied |
||||
|
|
||||
|
puls x ; restore source compressed data pointer |
||||
|
lbra lz2token ; go decode next token |
||||
File diff suppressed because it is too large
Loading…
Reference in new issue