1
0
mirror of https://github.com/pfusik/zlib6502.git synced 2024-06-01 12:41:27 +00:00

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: 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_data` - uninitialized data (764 bytes)
* `inflate_zp` - variables on zero page * `inflate_zp` - variables on zero page

View File

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