1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-11 11:30:13 +00:00

Fix 256 codes of same length

This commit is contained in:
Piotr Fusik 2017-11-06 19:46:26 +01:00
parent e7137a230c
commit d3451bb299

View File

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