BitBuster decompressor. (last update: 07/07/21)
CODE: xxl
mwa #packed_data BB_INPUT
mwa #destination BB_OUTPUT
jsr unBitBuster
; -------------------------------------
unBitBuster
lda #$80
sta token
ldy #$00
depack_loop
jsr getbits
bcs output_compressed ;if set, we got lz77 compression
jsr GET_BYTE
jsr PUT_BYTE
jmp depack_loop
output_compressed ;handle compressed data
jsr GET_BYTE
sta offsL ;get lowest 7 bits of offset, plus offset extension bit
output_match
sty offsH
asl @
bcc output_match1 ;no need to get extra bits if carry not set
jsr getbits
jsr rlbgetbits
jsr rlbgetbits
jsr rlbgetbits
bcs output_match1 ;since extension mark already makes bit 7 set
asl offsL ;only clear it if the bit should be cleared
lsr offsL
output_match1
inc offsL
bne @+
inc offsH
@ lda #$00 ;return a gamma-encoded value
sta lenH ;initial length to 1
ldx #$01 ;bitcount to 1
stx lenL ;determine number of bits used to encode value
get_gamma_value_size
jsr getbits
bcc get_gamma_value_size_end ;if bit not set, bitlength of remaining is known
inx ;increase bitcount
bne get_gamma_value_size ;repeat...
get_gamma_value_bits
jsr getbits
rol lenL ;insert new bit
rol lenH
get_gamma_value_size_end
dex
bne get_gamma_value_bits ;repeat if more bits to go
get_gamma_value_end
inc lenL
bne @+
inc lenH ;length was stored as length-2 so correct this
@ bcs _ret ; koniec
lda BB_OUTPUT
sec
sbc #$ff
offsL equ *-1
sta copysrc
lda BB_OUTPUT+1
sbc #$ff
offsH equ *-1
sta copysrc+1
cop0 lda $ffff
copysrc equ *-2
inw copysrc
jsr PUT_BYTE
lda #$ff
lenL equ *-1
bne @+
dec lenH
@ dec lenL
bne cop0
lda #$ff
lenH equ *-1
bne cop0
jmp depack_loop
rlbgetbits
rol offsH
getbits
asl token ; bez c
bne _ret
jsr GET_BYTE
rol @ ; c
sta token
_ret rts
token .HE 00
GET_BYTE
lda $ffff
BB_INPUT equ *-2
inw BB_INPUT
rts
PUT_BYTE
sta $ffff
BB_OUTPUT equ *-2
inw BB_OUTPUT
rts
