Better commenting of decompressor.

This commit is contained in:
Martin Haye 2017-01-02 11:33:52 -08:00
parent c67a4ea718
commit b62f4cbe53

View File

@ -30,20 +30,21 @@ pSrc = $C ; len 2
pDst = $E ; len 2 pDst = $E ; len 2
pEnd = $10 ; len 2 pEnd = $10 ; len 2
; Decompress from pSrc to pDst. They can overlap, as long as the source block ; Decompress from pSrc to pDst, stop at pEnd. The source and dest can overlap, as long as the
; ends (at least) 2 bytes beyond the end of the dest block, e.g. ; source block ends (at least) 2 bytes beyond the end of the dest block, e.g.
; DDDDDDDDDDDDDDD ; DDDDDDDDDDDDDDD
; SSSSSSSSSSSss <-- 2 bytes beyond dest ; SSSSSSSss <-- 2 bytes beyond dest
; This guarantees that the decompression won't overwrite any source material ; This guarantees that the decompression won't overwrite any source material
; before it gets used. ; before it gets used.
decomp ldy #0 ; invariant: Y=0 unless we're mid-copy decomp !zone {
ldy #0 ; In lit loop Y must be zero
beq .lits2 ; always taken beq .lits2 ; always taken
.lits lsr bits .lits asl bits ; get bit that tells us whether there's a literal string
bne + bne + ; ran out of bits in bit buffer?
.lits2 jsr getBits .lits2 jsr .getbts ; get more bits
+ bcc .seq + bcc .seq ; if bit was zero, no literals: go straight to sequence
jsr rdGamma jsr .gamma ; Yes we have literals. Get the count.
tax tax
cpx #255 ; special case: long literal marked by len=255; chk and save to carry cpx #255 ; special case: long literal marked by len=255; chk and save to carry
- lda (pSrc),y - lda (pSrc),y
@ -57,13 +58,11 @@ decomp ldy #0 ; invariant: Y=0 unless we're mid-copy
+ dex + dex
bne - bne -
bcs .lits ; (see special case above) bcs .lits ; (see special case above)
.endchk lda pDst+1 .endchk lda pDst ; check for done at end of each literal string
cmp pEnd+1 ; check for done at end of each literal string
bcc .seq
lda pDst
cmp pEnd cmp pEnd
bcc .seq lda pDst+1
rts sbc pEnd+1
bcs .ret
.seq lda (pSrc),y .seq lda (pSrc),y
inc pSrc inc pSrc
@ -78,7 +77,7 @@ decomp ldy #0 ; invariant: Y=0 unless we're mid-copy
bcc .gotoff ; always taken bcc .gotoff ; always taken
.bigoff asl .bigoff asl
sta tmp sta tmp
jsr rdGamma jsr .gamma
lsr lsr
rol tmp rol tmp
lsr lsr
@ -94,7 +93,7 @@ decomp ldy #0 ; invariant: Y=0 unless we're mid-copy
.len ldx #2 .len ldx #2
plp plp
bcc .gotlen bcc .gotlen
jsr rdGamma jsr .gamma
adc #1 ; A>=1 + sec + 1 => final len 3 or more adc #1 ; A>=1 + sec + 1 => final len 3 or more
tax tax
.gotlen .gotlen
@ -113,20 +112,19 @@ decomp ldy #0 ; invariant: Y=0 unless we're mid-copy
bcs .lits ; always taken bcs .lits ; always taken
; Read an Elias Gamma value into A. Destroys X. Sets carry. ; Read an Elias Gamma value into A. Destroys X. Sets carry.
rdGamma lda #1 .gamma lda #1
- asl bits - asl bits
bne + bne +
jsr getBits jsr .getbts
+ bcs .ret + bcs .ret
asl bits asl bits
bne + bne +
jsr getBits jsr .getbts
+ rol + rol
bcc - ; always taken except if overflow error, in which case WTF. bcc - ; always taken except if overflow error, in which case whatevs.
.ret rts
; Get another 8 bits into our bit buffer. Destroys X. Preserves A. Requires Y=0. ; Get another 8 bits into our bit buffer. Destroys X. Preserves A. Requires Y=0.
getBits tax .getbts tax
lda (pSrc),y lda (pSrc),y
inc pSrc inc pSrc
bne + bne +
@ -135,4 +133,5 @@ getBits tax
rol rol
sta bits sta bits
txa txa
rts .ret rts
} ; end of zone