Fix 256 codes of same length.

Fixes #1
This commit is contained in:
Piotr Fusik 2017-11-06 19:13:29 +01:00
parent f7af9d3980
commit 02c09a514e
2 changed files with 20 additions and 24 deletions

View File

@ -15,7 +15,7 @@ Use [xasm](https://github.com/pfusik/xasm).
The routine uses three memory areas:
* `inflate` - code and constants (499 bytes)
* `inflate` - code and constants (492 bytes)
* `inflate_data` - uninitialized data (764 bytes)
* `inflate_zp` - variables on zero page

View File

@ -4,7 +4,7 @@
; Compile with xasm (http://xasm.atari.org/), for example:
; xasm inflate.asx /l /d:inflate=$b700 /d:inflate_data=$b900 /d:inflate_zp=$f0
; inflate is 499 bytes of code and constants
; inflate is 492 bytes of code and constants
; inflate_data is 764 bytes of uninitialized data
; inflate_zp is 10 bytes on page zero
@ -279,7 +279,7 @@ inflateDynamic_getTempCodeLengths
; 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
sta:rne nBitCode_clearFrom,y+
@ -288,21 +288,19 @@ buildHuffmanTree
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 #-3*TREE_SIZE
ldx #-4*TREE_SIZE
buildHuffmanTree_calculateOffsets
sta nBitCode_literalOffset+3*TREE_SIZE-$100,x
add nBitCode_literalCount+3*TREE_SIZE-$100,x
sta nBitCode_literalOffset+4*TREE_SIZE-$100,x
add nBitCode_literalCount+4*TREE_SIZE-$100,x
inx
bne buildHuffmanTree_calculateOffsets
; Put symbols in their place in the sorted array
@ -337,26 +335,26 @@ fetchCode_nextBit
jsr getBit
rol @
inx
sub nBitCode_totalCount,x
sub 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
@ -440,14 +438,12 @@ controlSymbolCodeLength
; Huffman trees
nBitCode_clearFrom
nBitCode_totalCount
org *+2*TREE_SIZE
nBitCode_literalCount
org *+TREE_SIZE
org *+2*TREE_SIZE
nBitCode_controlCount
org *+2*TREE_SIZE
nBitCode_literalOffset
org *+TREE_SIZE
org *+2*TREE_SIZE
nBitCode_controlOffset
org *+2*TREE_SIZE