2001-11-23 11:56:07 +00:00
|
|
|
;
|
2003-09-23 20:05:32 +00:00
|
|
|
; Piotr Fusik, 21.09.2003
|
2001-11-23 11:56:07 +00:00
|
|
|
;
|
|
|
|
; unsigned __fastcall__ inflatemem (char* dest, const char* source);
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _inflatemem
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.import incsp2
|
|
|
|
.importzp sp, sreg, ptr1, ptr2, ptr3, ptr4, tmp1
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Constants
|
|
|
|
;
|
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Maximum length of a Huffman code.
|
2013-05-09 11:56:54 +00:00
|
|
|
MAX_BITS = 15
|
2003-09-23 20:05:32 +00:00
|
|
|
|
|
|
|
; All Huffman trees are stored in the bitsCount, bitsPointer_l
|
|
|
|
; and bitsPointer_h arrays. There may be two trees: the literal/length tree
|
|
|
|
; and the distance tree, or just one - the temporary tree.
|
|
|
|
|
|
|
|
; Index in the mentioned arrays for the beginning of the literal/length tree
|
|
|
|
; or the temporary tree.
|
2013-05-09 11:56:54 +00:00
|
|
|
PRIMARY_TREE = 0
|
2003-09-23 20:05:32 +00:00
|
|
|
|
|
|
|
; Index in the mentioned arrays for the beginning of the distance tree.
|
2013-05-09 11:56:54 +00:00
|
|
|
DISTANCE_TREE = MAX_BITS
|
2003-09-23 20:05:32 +00:00
|
|
|
|
|
|
|
; Size of each array.
|
2013-05-09 11:56:54 +00:00
|
|
|
TREES_SIZE = 2*MAX_BITS
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
|
2001-11-23 11:56:07 +00:00
|
|
|
; --------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Page zero
|
|
|
|
;
|
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Pointer to the compressed data.
|
2013-05-09 11:56:54 +00:00
|
|
|
inputPointer = ptr1 ; 2 bytes
|
2003-09-23 20:05:32 +00:00
|
|
|
|
|
|
|
; Pointer to the uncompressed data.
|
2013-05-09 11:56:54 +00:00
|
|
|
outputPointer = ptr2 ; 2 bytes
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Local variables.
|
|
|
|
; As far as there is no conflict, same memory locations are used
|
|
|
|
; for different variables.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
inflateDynamicBlock_cnt = ptr3 ; 1 byte
|
|
|
|
inflateCodes_src = ptr3 ; 2 bytes
|
|
|
|
buildHuffmanTree_src = ptr3 ; 2 bytes
|
|
|
|
getNextLength_last = ptr3 ; 1 byte
|
|
|
|
getNextLength_index = ptr3+1 ; 1 byte
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
buildHuffmanTree_ptr = ptr4 ; 2 bytes
|
|
|
|
fetchCode_ptr = ptr4 ; 2 bytes
|
|
|
|
getBits_tmp = ptr4 ; 1 byte
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
moveBlock_len = sreg ; 2 bytes
|
|
|
|
inflateDynamicBlock_np = sreg ; 1 byte
|
|
|
|
inflateDynamicBlock_nd = sreg+1 ; 1 byte
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
getBit_hold = tmp1 ; 1 byte
|
2003-09-23 20:05:32 +00:00
|
|
|
|
|
|
|
|
2001-11-23 11:56:07 +00:00
|
|
|
; --------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Code
|
|
|
|
;
|
|
|
|
|
|
|
|
_inflatemem:
|
|
|
|
|
|
|
|
; inputPointer = source
|
2013-05-09 11:56:54 +00:00
|
|
|
sta inputPointer
|
|
|
|
stx inputPointer+1
|
2001-11-23 11:56:07 +00:00
|
|
|
; outputPointer = dest
|
|
|
|
.ifpc02
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (sp)
|
|
|
|
ldy #1
|
2001-11-23 11:56:07 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #0
|
|
|
|
lda (sp),y
|
|
|
|
iny
|
2001-11-23 11:56:07 +00:00
|
|
|
.endif
|
2013-05-09 11:56:54 +00:00
|
|
|
sta outputPointer
|
|
|
|
lda (sp),y
|
|
|
|
sta outputPointer+1
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
; ldy #1
|
|
|
|
sty getBit_hold
|
2001-11-23 11:56:07 +00:00
|
|
|
inflatemem_1:
|
|
|
|
; Get a bit of EOF and two bits of block type
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #3
|
|
|
|
lda #0
|
|
|
|
jsr getBits
|
|
|
|
lsr a
|
2003-09-23 20:05:32 +00:00
|
|
|
; A and Z contain block type, C contains EOF flag
|
2001-11-23 11:56:07 +00:00
|
|
|
; Save EOF flag
|
2013-05-09 11:56:54 +00:00
|
|
|
php
|
2001-11-23 11:56:07 +00:00
|
|
|
; Go to the routine decompressing this block
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr callExtr
|
|
|
|
plp
|
|
|
|
bcc inflatemem_1
|
2001-11-23 11:56:07 +00:00
|
|
|
; C flag is set!
|
|
|
|
|
|
|
|
; return outputPointer - dest;
|
2013-05-09 11:56:54 +00:00
|
|
|
lda outputPointer
|
2001-11-23 11:56:07 +00:00
|
|
|
.ifpc02
|
2013-05-09 11:56:54 +00:00
|
|
|
sbc (sp) ; C flag is set
|
|
|
|
ldy #1
|
2001-11-23 11:56:07 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #0
|
|
|
|
sbc (sp),y ; C flag is set
|
|
|
|
iny
|
2001-11-23 11:56:07 +00:00
|
|
|
.endif
|
2013-05-09 11:56:54 +00:00
|
|
|
pha
|
|
|
|
lda outputPointer+1
|
|
|
|
sbc (sp),y
|
|
|
|
tax
|
|
|
|
pla
|
2001-11-23 11:56:07 +00:00
|
|
|
; pop dest
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp incsp2
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Go to proper block decoding routine.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
callExtr:
|
2013-05-09 11:56:54 +00:00
|
|
|
bne inflateCompressedBlock
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Decompress a 'stored' data block.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
inflateCopyBlock:
|
|
|
|
; Ignore bits until byte boundary
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #1
|
|
|
|
sty getBit_hold
|
2001-11-23 11:56:07 +00:00
|
|
|
; Get 16-bit length
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #inputPointer
|
|
|
|
lda (0,x)
|
|
|
|
sta moveBlock_len
|
|
|
|
lda (inputPointer),y
|
|
|
|
sta moveBlock_len+1
|
2003-09-23 20:05:32 +00:00
|
|
|
; Skip the length and one's complement of it
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #4
|
|
|
|
clc
|
|
|
|
adc inputPointer
|
|
|
|
sta inputPointer
|
|
|
|
bcc moveBlock
|
|
|
|
inc inputPointer+1
|
|
|
|
; jmp moveBlock
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Copy block of length moveBlock_len from (0,x) to the output.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
moveBlock:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy moveBlock_len
|
|
|
|
beq moveBlock_1
|
2001-11-23 11:56:07 +00:00
|
|
|
.ifpc02
|
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #0
|
2001-11-23 11:56:07 +00:00
|
|
|
.endif
|
2013-05-09 11:56:54 +00:00
|
|
|
inc moveBlock_len+1
|
2001-11-23 11:56:07 +00:00
|
|
|
moveBlock_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (0,x)
|
2001-11-23 11:56:07 +00:00
|
|
|
.ifpc02
|
2013-05-09 11:56:54 +00:00
|
|
|
sta (outputPointer)
|
2001-11-23 11:56:07 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
sta (outputPointer),y
|
2001-11-23 11:56:07 +00:00
|
|
|
.endif
|
2013-05-09 11:56:54 +00:00
|
|
|
inc 0,x
|
|
|
|
bne moveBlock_2
|
|
|
|
inc 1,x
|
2001-11-23 11:56:07 +00:00
|
|
|
moveBlock_2:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc outputPointer
|
|
|
|
bne moveBlock_3
|
|
|
|
inc outputPointer+1
|
2001-11-23 11:56:07 +00:00
|
|
|
moveBlock_3:
|
|
|
|
.ifpc02
|
2013-05-09 11:56:54 +00:00
|
|
|
dey
|
2001-11-23 11:56:07 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
dec moveBlock_len
|
2001-11-23 11:56:07 +00:00
|
|
|
.endif
|
2013-05-09 11:56:54 +00:00
|
|
|
bne moveBlock_1
|
|
|
|
dec moveBlock_len+1
|
|
|
|
bne moveBlock_1
|
|
|
|
rts
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Decompress a Huffman-coded data block
|
|
|
|
; (A = 1: fixed, A = 2: dynamic).
|
|
|
|
|
|
|
|
inflateCompressedBlock:
|
2013-05-09 11:56:54 +00:00
|
|
|
lsr a
|
|
|
|
bne inflateDynamicBlock
|
2003-09-23 20:05:32 +00:00
|
|
|
; Note: inflateDynamicBlock may assume that A = 1
|
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
|
|
|
; Decompress a Huffman-coded data block with default Huffman trees
|
|
|
|
; (defined by the DEFLATE format):
|
2001-11-23 11:56:07 +00:00
|
|
|
; literalCodeLength: 144 times 8, 112 times 9
|
2003-09-23 20:05:32 +00:00
|
|
|
; endCodeLength: 7
|
|
|
|
; lengthCodeLength: 23 times 7, 6 times 8
|
2001-11-23 11:56:07 +00:00
|
|
|
; distanceCodeLength: 30 times 5+DISTANCE_TREE, 2 times 8
|
2003-09-23 20:05:32 +00:00
|
|
|
; (two 8-bit codes from the primary tree are not used).
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
inflateFixedBlock:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #159
|
|
|
|
stx distanceCodeLength+32
|
|
|
|
lda #8
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateFixedBlock_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta literalCodeLength-1,x
|
|
|
|
sta literalCodeLength+159-1,x
|
|
|
|
dex
|
|
|
|
bne inflateFixedBlock_1
|
|
|
|
ldx #112
|
|
|
|
; lda #9
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateFixedBlock_2:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc literalCodeLength+144-1,x ; sta
|
|
|
|
dex
|
|
|
|
bne inflateFixedBlock_2
|
|
|
|
ldx #24
|
|
|
|
; lda #7
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateFixedBlock_3:
|
2013-05-09 11:56:54 +00:00
|
|
|
dec endCodeLength-1,x ; sta
|
|
|
|
dex
|
|
|
|
bne inflateFixedBlock_3
|
|
|
|
ldx #30
|
|
|
|
lda #5+DISTANCE_TREE
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateFixedBlock_4:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta distanceCodeLength-1,x
|
|
|
|
dex
|
|
|
|
bne inflateFixedBlock_4
|
|
|
|
beq inflateCodes ; branch always
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Decompress a Huffman-coded data block, reading Huffman trees first.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
inflateDynamicBlock:
|
|
|
|
; numberOfPrimaryCodes = 257 + getBits(5)
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #5
|
|
|
|
; lda #1
|
|
|
|
jsr getBits
|
|
|
|
sta inflateDynamicBlock_np
|
2001-11-23 11:56:07 +00:00
|
|
|
; numberOfDistanceCodes = 1 + getBits(5)
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #5
|
|
|
|
lda #1+29+1
|
|
|
|
jsr getBits
|
|
|
|
sta inflateDynamicBlock_nd
|
2001-11-23 11:56:07 +00:00
|
|
|
; numberOfTemporaryCodes = 4 + getBits(4)
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #4
|
|
|
|
tax
|
|
|
|
jsr getBits
|
|
|
|
sta inflateDynamicBlock_cnt
|
2003-09-23 20:05:32 +00:00
|
|
|
; Get lengths of temporary codes in the order stored in tempCodeLengthOrder
|
2013-05-09 11:56:54 +00:00
|
|
|
txa ; lda #0
|
|
|
|
tay
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateDynamicBlock_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #3 ; A = 0
|
|
|
|
jsr getBits ; does not change Y
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateDynamicBlock_2:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx tempCodeLengthOrder,y
|
|
|
|
sta literalCodeLength,x
|
|
|
|
lda #0
|
|
|
|
iny
|
|
|
|
cpy inflateDynamicBlock_cnt
|
|
|
|
bcc inflateDynamicBlock_1
|
|
|
|
cpy #19
|
|
|
|
bcc inflateDynamicBlock_2
|
|
|
|
ror literalCodeLength+19 ; C flag is set, so this will set b7
|
2003-09-23 20:05:32 +00:00
|
|
|
; Build the tree for temporary codes
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr buildHuffmanTree
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Use temporary codes to get lengths of literal/length and distance codes
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #0
|
|
|
|
ldy #1
|
|
|
|
stx getNextLength_last
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateDynamicBlock_3:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr getNextLength
|
|
|
|
sta literalCodeLength,x
|
|
|
|
inx
|
|
|
|
bne inflateDynamicBlock_3
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateDynamicBlock_4:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr getNextLength
|
2003-09-23 20:05:32 +00:00
|
|
|
inflateDynamicBlock_5:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta endCodeLength,x
|
|
|
|
inx
|
|
|
|
cpx inflateDynamicBlock_np
|
|
|
|
bcc inflateDynamicBlock_4
|
|
|
|
lda #0
|
|
|
|
cpx #1+29
|
|
|
|
bcc inflateDynamicBlock_5
|
2003-09-23 20:05:32 +00:00
|
|
|
inflateDynamicBlock_6:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr getNextLength
|
|
|
|
cmp #0
|
|
|
|
beq inflateDynamicBlock_7
|
|
|
|
adc #DISTANCE_TREE-1 ; C flag is set
|
2003-09-23 20:05:32 +00:00
|
|
|
inflateDynamicBlock_7:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta endCodeLength,x
|
|
|
|
inx
|
|
|
|
cpx inflateDynamicBlock_nd
|
|
|
|
bcc inflateDynamicBlock_6
|
|
|
|
ror endCodeLength,x ; C flag is set, so this will set b7
|
|
|
|
; jmp inflateCodes
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Decompress a data block basing on given Huffman trees.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
inflateCodes:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr buildHuffmanTree
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateCodes_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr fetchPrimaryCode
|
|
|
|
bcs inflateCodes_2
|
2001-11-23 11:56:07 +00:00
|
|
|
; Literal code
|
|
|
|
.ifpc02
|
2013-05-09 11:56:54 +00:00
|
|
|
sta (outputPointer)
|
2001-11-23 11:56:07 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #0
|
|
|
|
sta (outputPointer),y
|
2001-11-23 11:56:07 +00:00
|
|
|
.endif
|
2013-05-09 11:56:54 +00:00
|
|
|
inc outputPointer
|
|
|
|
bne inflateCodes_1
|
|
|
|
inc outputPointer+1
|
|
|
|
bcc inflateCodes_1 ; branch always
|
2001-11-23 11:56:07 +00:00
|
|
|
; End of block
|
|
|
|
inflateCodes_ret:
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2001-11-23 11:56:07 +00:00
|
|
|
inflateCodes_2:
|
2013-05-09 11:56:54 +00:00
|
|
|
beq inflateCodes_ret
|
2003-09-23 20:05:32 +00:00
|
|
|
; Restore a block from the look-behind buffer
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr getValue
|
|
|
|
sta moveBlock_len
|
|
|
|
tya
|
|
|
|
jsr getBits
|
|
|
|
sta moveBlock_len+1
|
|
|
|
ldx #DISTANCE_TREE
|
|
|
|
jsr fetchCode
|
|
|
|
jsr getValue
|
|
|
|
sec
|
|
|
|
eor #$ff
|
|
|
|
adc outputPointer
|
|
|
|
sta inflateCodes_src
|
|
|
|
php
|
|
|
|
tya
|
|
|
|
jsr getBits
|
|
|
|
plp
|
|
|
|
eor #$ff
|
|
|
|
adc outputPointer+1
|
|
|
|
sta inflateCodes_src+1
|
|
|
|
ldx #inflateCodes_src
|
|
|
|
jsr moveBlock
|
|
|
|
beq inflateCodes_1 ; branch always
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Build Huffman trees basing on code lengths (in bits).
|
|
|
|
; stored in the *CodeLength arrays.
|
|
|
|
; A byte with its highest bit set marks the end.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
buildHuffmanTree:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<literalCodeLength
|
|
|
|
sta buildHuffmanTree_src
|
|
|
|
lda #>literalCodeLength
|
|
|
|
sta buildHuffmanTree_src+1
|
2001-11-23 11:56:07 +00:00
|
|
|
; Clear bitsCount and bitsPointer_l
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #2*TREES_SIZE+1
|
|
|
|
lda #0
|
2001-11-23 11:56:07 +00:00
|
|
|
buildHuffmanTree_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta bitsCount-1,y
|
|
|
|
dey
|
|
|
|
bne buildHuffmanTree_1
|
|
|
|
beq buildHuffmanTree_3 ; branch always
|
2001-11-23 11:56:07 +00:00
|
|
|
; Count number of codes of each length
|
|
|
|
buildHuffmanTree_2:
|
2013-05-09 11:56:54 +00:00
|
|
|
tax
|
|
|
|
inc bitsPointer_l,x
|
|
|
|
iny
|
|
|
|
bne buildHuffmanTree_3
|
|
|
|
inc buildHuffmanTree_src+1
|
2001-11-23 11:56:07 +00:00
|
|
|
buildHuffmanTree_3:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (buildHuffmanTree_src),y
|
|
|
|
bpl buildHuffmanTree_2
|
2003-09-23 20:05:32 +00:00
|
|
|
; Calculate a pointer for each length
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #0
|
|
|
|
lda #<sortedCodes
|
|
|
|
ldy #>sortedCodes
|
|
|
|
clc
|
2001-11-23 11:56:07 +00:00
|
|
|
buildHuffmanTree_4:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta bitsPointer_l,x
|
|
|
|
tya
|
|
|
|
sta bitsPointer_h,x
|
|
|
|
lda bitsPointer_l+1,x
|
|
|
|
adc bitsPointer_l,x ; C flag is zero
|
|
|
|
bcc buildHuffmanTree_5
|
|
|
|
iny
|
2001-11-23 11:56:07 +00:00
|
|
|
buildHuffmanTree_5:
|
2013-05-09 11:56:54 +00:00
|
|
|
inx
|
|
|
|
cpx #TREES_SIZE
|
|
|
|
bcc buildHuffmanTree_4
|
|
|
|
lda #>literalCodeLength
|
|
|
|
sta buildHuffmanTree_src+1
|
|
|
|
ldy #0
|
|
|
|
bcs buildHuffmanTree_9 ; branch always
|
2001-11-23 11:56:07 +00:00
|
|
|
; Put codes into their place in sorted table
|
|
|
|
buildHuffmanTree_6:
|
2013-05-09 11:56:54 +00:00
|
|
|
beq buildHuffmanTree_7
|
|
|
|
tax
|
|
|
|
lda bitsPointer_l-1,x
|
|
|
|
sta buildHuffmanTree_ptr
|
|
|
|
lda bitsPointer_h-1,x
|
|
|
|
sta buildHuffmanTree_ptr+1
|
|
|
|
tya
|
|
|
|
ldy bitsCount-1,x
|
|
|
|
inc bitsCount-1,x
|
|
|
|
sta (buildHuffmanTree_ptr),y
|
|
|
|
tay
|
2001-11-23 11:56:07 +00:00
|
|
|
buildHuffmanTree_7:
|
2013-05-09 11:56:54 +00:00
|
|
|
iny
|
|
|
|
bne buildHuffmanTree_9
|
|
|
|
inc buildHuffmanTree_src+1
|
|
|
|
ldx #MAX_BITS-1
|
2001-11-23 11:56:07 +00:00
|
|
|
buildHuffmanTree_8:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda bitsCount,x
|
|
|
|
sta literalCount,x
|
|
|
|
dex
|
|
|
|
bpl buildHuffmanTree_8
|
2001-11-23 11:56:07 +00:00
|
|
|
buildHuffmanTree_9:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (buildHuffmanTree_src),y
|
|
|
|
bpl buildHuffmanTree_6
|
|
|
|
rts
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Decode next code length using temporary codes.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
getNextLength:
|
2013-05-09 11:56:54 +00:00
|
|
|
stx getNextLength_index
|
|
|
|
dey
|
|
|
|
bne getNextLength_1
|
2001-11-23 11:56:07 +00:00
|
|
|
; Fetch a temporary code
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr fetchPrimaryCode
|
2001-11-23 11:56:07 +00:00
|
|
|
; Temporary code 0..15: put this length
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #1
|
|
|
|
cmp #16
|
|
|
|
bcc getNextLength_2
|
2001-11-23 11:56:07 +00:00
|
|
|
; Temporary code 16: repeat last length 3 + getBits(2) times
|
|
|
|
; Temporary code 17: put zero length 3 + getBits(3) times
|
|
|
|
; Temporary code 18: put zero length 11 + getBits(7) times
|
2013-05-09 11:56:54 +00:00
|
|
|
tay
|
|
|
|
ldx tempExtraBits-16,y
|
|
|
|
lda tempBaseValue-16,y
|
|
|
|
jsr getBits
|
|
|
|
cpy #17
|
|
|
|
tay
|
|
|
|
txa ; lda #0
|
|
|
|
bcs getNextLength_2
|
2001-11-23 11:56:07 +00:00
|
|
|
getNextLength_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda getNextLength_last
|
2001-11-23 11:56:07 +00:00
|
|
|
getNextLength_2:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta getNextLength_last
|
|
|
|
ldx getNextLength_index
|
|
|
|
rts
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Read a code basing on the primary tree.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
fetchPrimaryCode:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #PRIMARY_TREE
|
|
|
|
; jmp fetchCode
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Read a code from input basing on the tree specified in X.
|
|
|
|
; Return low byte of this code in A.
|
|
|
|
; For the literal/length tree, the C flag is set if the code is non-literal.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
fetchCode:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #0
|
2001-11-23 11:56:07 +00:00
|
|
|
fetchCode_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr getBit
|
|
|
|
rol a
|
|
|
|
inx
|
|
|
|
sec
|
|
|
|
sbc bitsCount-1,x
|
|
|
|
bcs fetchCode_1
|
|
|
|
adc bitsCount-1,x ; C flag is zero
|
|
|
|
cmp literalCount-1,x
|
|
|
|
sta fetchCode_ptr
|
|
|
|
ldy bitsPointer_l-1,x
|
|
|
|
lda bitsPointer_h-1,x
|
|
|
|
sta fetchCode_ptr+1
|
|
|
|
lda (fetchCode_ptr),y
|
|
|
|
rts
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Decode low byte of a value (length or distance), basing on the code in A.
|
|
|
|
; The result is the base value for this code plus some bits read from input.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
getValue:
|
2013-05-09 11:56:54 +00:00
|
|
|
tay
|
|
|
|
ldx lengthExtraBits-1,y
|
|
|
|
lda lengthBaseValue_l-1,y
|
|
|
|
pha
|
|
|
|
lda lengthBaseValue_h-1,y
|
|
|
|
tay
|
|
|
|
pla
|
|
|
|
; jmp getBits
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Read X-bit number from the input and add it to A.
|
|
|
|
; Increment Y if overflow.
|
|
|
|
; If X > 8, read only 8 bits.
|
2001-11-23 11:56:07 +00:00
|
|
|
; On return X holds number of unread bits: X = (X > 8 ? X - 8 : 0);
|
|
|
|
|
|
|
|
getBits:
|
2013-05-09 11:56:54 +00:00
|
|
|
cpx #0
|
|
|
|
beq getBits_ret
|
2003-09-23 20:05:32 +00:00
|
|
|
.ifpc02
|
2013-05-09 11:56:54 +00:00
|
|
|
stz getBits_tmp
|
|
|
|
dec getBits_tmp
|
2003-09-23 20:05:32 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
pha
|
|
|
|
lda #$ff
|
|
|
|
sta getBits_tmp
|
|
|
|
pla
|
2003-09-23 20:05:32 +00:00
|
|
|
.endif
|
2001-11-23 11:56:07 +00:00
|
|
|
getBits_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr getBit
|
|
|
|
bcc getBits_2
|
|
|
|
sbc getBits_tmp ; C flag is set
|
|
|
|
bcc getBits_2
|
|
|
|
iny
|
2001-11-23 11:56:07 +00:00
|
|
|
getBits_2:
|
2013-05-09 11:56:54 +00:00
|
|
|
dex
|
|
|
|
beq getBits_ret
|
|
|
|
asl getBits_tmp
|
|
|
|
bmi getBits_1
|
2001-11-23 11:56:07 +00:00
|
|
|
getBits_ret:
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Read a single bit from input, return it in the C flag.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
getBit:
|
2013-05-09 11:56:54 +00:00
|
|
|
lsr getBit_hold
|
|
|
|
bne getBit_ret
|
|
|
|
pha
|
2001-11-23 11:56:07 +00:00
|
|
|
.ifpc02
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (inputPointer)
|
2001-11-23 11:56:07 +00:00
|
|
|
.else
|
2013-05-09 11:56:54 +00:00
|
|
|
sty getBit_hold
|
|
|
|
ldy #0
|
|
|
|
lda (inputPointer),y
|
|
|
|
ldy getBit_hold
|
2001-11-23 11:56:07 +00:00
|
|
|
.endif
|
2013-05-09 11:56:54 +00:00
|
|
|
inc inputPointer
|
|
|
|
bne getBit_1
|
|
|
|
inc inputPointer+1
|
2001-11-23 11:56:07 +00:00
|
|
|
getBit_1:
|
2013-05-09 11:56:54 +00:00
|
|
|
ror a ; C flag is set
|
|
|
|
sta getBit_hold
|
|
|
|
pla
|
2001-11-23 11:56:07 +00:00
|
|
|
getBit_ret:
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
|
2001-11-23 11:56:07 +00:00
|
|
|
; --------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Constant data
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.rodata
|
2001-11-23 11:56:07 +00:00
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Arrays for the temporary codes.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Order, in which lengths of the temporary codes are stored.
|
2001-11-23 11:56:07 +00:00
|
|
|
tempCodeLengthOrder:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Base values.
|
2001-11-23 11:56:07 +00:00
|
|
|
tempBaseValue:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte 3,3,11
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Number of extra bits to read.
|
2001-11-23 11:56:07 +00:00
|
|
|
tempExtraBits:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte 2,3,7
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Arrays for the length and distance codes.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Base values.
|
2001-11-23 11:56:07 +00:00
|
|
|
lengthBaseValue_l:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte <3,<4,<5,<6,<7,<8,<9,<10
|
|
|
|
.byte <11,<13,<15,<17,<19,<23,<27,<31
|
|
|
|
.byte <35,<43,<51,<59,<67,<83,<99,<115
|
|
|
|
.byte <131,<163,<195,<227,<258
|
2001-11-23 11:56:07 +00:00
|
|
|
distanceBaseValue_l:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte <1,<2,<3,<4,<5,<7,<9,<13
|
|
|
|
.byte <17,<25,<33,<49,<65,<97,<129,<193
|
|
|
|
.byte <257,<385,<513,<769,<1025,<1537,<2049,<3073
|
|
|
|
.byte <4097,<6145,<8193,<12289,<16385,<24577
|
2001-11-23 11:56:07 +00:00
|
|
|
lengthBaseValue_h:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte >3,>4,>5,>6,>7,>8,>9,>10
|
|
|
|
.byte >11,>13,>15,>17,>19,>23,>27,>31
|
|
|
|
.byte >35,>43,>51,>59,>67,>83,>99,>115
|
|
|
|
.byte >131,>163,>195,>227,>258
|
2001-11-23 11:56:07 +00:00
|
|
|
distanceBaseValue_h:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte >1,>2,>3,>4,>5,>7,>9,>13
|
|
|
|
.byte >17,>25,>33,>49,>65,>97,>129,>193
|
|
|
|
.byte >257,>385,>513,>769,>1025,>1537,>2049,>3073
|
|
|
|
.byte >4097,>6145,>8193,>12289,>16385,>24577
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Number of extra bits to read.
|
2001-11-23 11:56:07 +00:00
|
|
|
lengthExtraBits:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte 0,0,0,0,0,0,0,0
|
|
|
|
.byte 1,1,1,1,2,2,2,2
|
|
|
|
.byte 3,3,3,3,4,4,4,4
|
|
|
|
.byte 5,5,5,5,0
|
2001-11-23 11:56:07 +00:00
|
|
|
distanceExtraBits:
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte 0,0,0,0,1,1,2,2
|
|
|
|
.byte 3,3,4,4,5,5,6,6
|
|
|
|
.byte 7,7,8,8,9,9,10,10
|
|
|
|
.byte 11,11,12,12,13,13
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
|
2001-11-23 11:56:07 +00:00
|
|
|
; --------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Uninitialised data
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.bss
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Number of literal codes of each length in the primary tree
|
|
|
|
; (MAX_BITS bytes, overlap with literalCodeLength).
|
2001-11-23 11:56:07 +00:00
|
|
|
literalCount:
|
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Data for building the primary tree.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Lengths of literal codes.
|
2001-11-23 11:56:07 +00:00
|
|
|
literalCodeLength:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res 256
|
2003-09-23 20:05:32 +00:00
|
|
|
; Length of the end code.
|
2001-11-23 11:56:07 +00:00
|
|
|
endCodeLength:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res 1
|
2003-09-23 20:05:32 +00:00
|
|
|
; Lengths of length codes.
|
2001-11-23 11:56:07 +00:00
|
|
|
lengthCodeLength:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res 29
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; Data for building the distance tree.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Lengths of distance codes.
|
2001-11-23 11:56:07 +00:00
|
|
|
distanceCodeLength:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res 30
|
2003-09-23 20:05:32 +00:00
|
|
|
; For two unused codes in the fixed trees and an 'end' mark.
|
2013-05-09 11:56:54 +00:00
|
|
|
.res 3
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
; --------------------------------------------------------------------------
|
2003-09-23 20:05:32 +00:00
|
|
|
; The Huffman trees.
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Number of codes of each length.
|
2001-11-23 11:56:07 +00:00
|
|
|
bitsCount:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res TREES_SIZE
|
2003-09-23 20:05:32 +00:00
|
|
|
; Pointers to sorted codes of each length.
|
2001-11-23 11:56:07 +00:00
|
|
|
bitsPointer_l:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res TREES_SIZE+1
|
2001-11-23 11:56:07 +00:00
|
|
|
bitsPointer_h:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res TREES_SIZE
|
2001-11-23 11:56:07 +00:00
|
|
|
|
2003-09-23 20:05:32 +00:00
|
|
|
; Sorted codes.
|
2001-11-23 11:56:07 +00:00
|
|
|
sortedCodes:
|
2013-05-09 11:56:54 +00:00
|
|
|
.res 256+1+29+30+2
|
2001-11-23 11:56:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|