;
-; 2017-02-12, Piotr Fusik
+; 2017-11-06, Piotr Fusik
;
; unsigned __fastcall__ inflatemem (char* dest, const char* source);
;
; ldy #0
sty getBit_buffer ; ignore bits until byte boundary
jsr getWord ; skip the length we don't need
- jsr getWord ; get the two's complement length
+ jsr getWord ; get the one's complement length
sta inflateStored_pageCounter
bcs inflateStored_firstByte ; jmp
inflateStored_copyByte:
; Build Huffman trees basing on code lengths (in bits)
; stored in the *SymbolCodeLength arrays
buildHuffmanTree:
-; Clear nBitCode_totalCount, nBitCode_literalCount, nBitCode_controlCount
+; Clear nBitCode_literalCount, nBitCode_controlCount
tya
; lda #0
buildHuffmanTree_clear:
buildHuffmanTree_countCodeLengths:
ldx literalSymbolCodeLength,y
inc nBitCode_literalCount,x
- inc nBitCode_totalCount,x
cpy #CONTROL_SYMBOLS
bcs buildHuffmanTree_noControlSymbol
ldx controlSymbolCodeLength,y
inc nBitCode_controlCount,x
- inc nBitCode_totalCount,x
buildHuffmanTree_noControlSymbol:
iny
bne buildHuffmanTree_countCodeLengths
; Calculate offsets of symbols sorted by code length
; lda #0
- ldx #$100-3*TREE_SIZE
+ ldx #$100-4*TREE_SIZE
buildHuffmanTree_calculateOffsets:
- sta nBitCode_literalOffset+3*TREE_SIZE-$100,x
+ sta nBitCode_literalOffset+4*TREE_SIZE-$100,x
clc
- adc nBitCode_literalCount+3*TREE_SIZE-$100,x
+ adc nBitCode_literalCount+4*TREE_SIZE-$100,x
inx
bne buildHuffmanTree_calculateOffsets
; Put symbols in their place in the sorted array
rol a
inx
sec
- sbc nBitCode_totalCount,x
+ sbc nBitCode_literalCount,x
+ bcc fetchCode_literal
+; sec
+ sbc nBitCode_controlCount,x
bcs fetchCode_nextBit
; clc
- adc nBitCode_controlCount,x
- bcs fetchCode_control
+ adc nBitCode_controlOffset,x
+ tax
+ lda codeToControlSymbol,x
+ and #$1f ; make distance symbols zero-based
+ tax
+; sec
+ rts
+fetchCode_literal:
; clc
adc nBitCode_literalOffset,x
tax
lda codeToLiteralSymbol,x
clc
rts
-fetchCode_control:
-; sec
- adc nBitCode_controlOffset-1,x
- tax
- lda codeToControlSymbol-1,x
- and #$1f ; make distance symbols zero-based
- tax
- sec
- rts
; Read A minus 1 bits, but no more than 8
getAMinus1BitsMax8:
; Huffman trees.
nBitCode_clearFrom:
-nBitCode_totalCount:
- .res 2*TREE_SIZE
nBitCode_literalCount:
- .res TREE_SIZE
+ .res 2*TREE_SIZE
nBitCode_controlCount:
.res 2*TREE_SIZE
nBitCode_literalOffset:
- .res TREE_SIZE
+ .res 2*TREE_SIZE
nBitCode_controlOffset:
.res 2*TREE_SIZE