From e5dfc7db2fede03d0d9d2798fa8f8f4f4a7e68e6 Mon Sep 17 00:00:00 2001 From: Piotr Fusik Date: Tue, 7 Feb 2017 15:04:19 +0100 Subject: [PATCH] Make the routine ROM-friendly - the initialized data is now constant. Shorten by four bytes. --- README.md | 2 +- inflate.asx | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 475f11f..d6302df 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Use [xasm](https://github.com/pfusik/xasm). The routine uses three memory areas: -* `inflate` - code and initialized data (508 bytes) +* `inflate` - code and constants (504 bytes) * `inflate_data` - uninitialized data (764 bytes) * `inflate_zp` - variables on zero page diff --git a/inflate.asx b/inflate.asx index 5986553..56632d9 100644 --- a/inflate.asx +++ b/inflate.asx @@ -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 508 bytes of code and initialized data +; inflate is 504 bytes of code and constants ; inflate_data is 764 bytes of uninitialized data ; inflate_zp is 10 bytes on page zero @@ -24,13 +24,13 @@ inflateStoredBlock_pageCounter equ inflate_zp+5 ; 1 byte inflateCodes_sourcePointer equ inflate_zp+6 ; 2 bytes inflateDynamicBlock_lengthIndex equ inflate_zp+6 ; 1 byte -inflateDynamicBlock_lastLength equ inflate_zp+7 ; 1 byte +inflateDynamicBlock_lastLength equ inflate_zp+7 ; 1 byte inflateDynamicBlock_tempCodes equ inflate_zp+7 ; 1 byte inflateCodes_lengthMinus2 equ inflate_zp+8 ; 1 byte inflateDynamicBlock_allCodes equ inflate_zp+8 ; 1 byte -inflateCodes_primaryCodes equ inflate_zp+9 ; 1 byte +inflateDynamicBlock_primaryCodes equ inflate_zp+9 ; 1 byte ; Argument values for getBits @@ -123,7 +123,6 @@ inflateFixedBlock_setControlCodeLength inflateFixedBlock_noControlSymbol iny bne inflateFixedBlock_setCodeLengths - mva #LENGTH_SYMBOLS inflateCodes_primaryCodes dex beq inflateCodes @@ -142,11 +141,11 @@ inflateDynamicBlock_decodeLength ; Fetch a temporary code jsr fetchPrimaryCode ; Temporary code 0..15: put this length - tax bpl inflateDynamicBlock_verbatimLength ; Temporary code 16: repeat last length 3 + getBits(2) times ; Temporary code 17: put zero length 3 + getBits(3) times ; Temporary code 18: put zero length 11 + getBits(7) times + tax jsr getBits ; sec adc #1 @@ -171,8 +170,13 @@ inflateDynamicBlock_storeNext ; jmp inflateDynamicBlock_decodeLength beq inflateDynamicBlock_decodeLength inflateDynamicBlock_controlSymbolCodeLength - cpx inflateCodes_primaryCodes - scc:ora #DISTANCE_TREE + cpx inflateDynamicBlock_primaryCodes + bcc inflateDynamicBlock_storeControl +; the code lengths we skip here were zero-initialized +; in inflateDynamicBlock_clearCodeLengths + sne:ldx #LENGTH_SYMBOLS + ora #DISTANCE_TREE +inflateDynamicBlock_storeControl sta controlSymbolCodeLength,x+ cpx inflateDynamicBlock_allCodes bcc inflateDynamicBlock_storeNext @@ -186,7 +190,6 @@ inflateCodes inflateCodes_loop jsr fetchPrimaryCode bcc inflateStoreByte - tax beq inflate_nextBlock ; Copy sequence from look-behind buffer ; ldy #0 @@ -211,9 +214,6 @@ inflateCodes_setSequenceLength sta inflateCodes_lengthMinus2 ldx #DISTANCE_TREE jsr fetchCode -; sec - sbc inflateCodes_primaryCodes - tax cmp #4 bcc inflateCodes_setOffsetLowByte inc getBits_base @@ -260,7 +260,6 @@ inflateDynamicBlock_getHeader ; sec adc inflateDynamicBlock_headerBase-1,x sta inflateDynamicBlock_tempCodes-1,x - sta inflateDynamicBlock_headerBase+1 dex bne inflateDynamicBlock_getHeader @@ -353,6 +352,8 @@ fetchCode_control adc nBitCode_controlOffset-1,x tax lda codeToControlSymbol-1,x + and #$1f ; make distance symbols zero-based + tax sec rts @@ -422,7 +423,7 @@ tempCodeLengthOrder dta GET_2_BITS,GET_3_BITS,GET_7_BITS,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 inflateDynamicBlock_headerBits dta GET_4_BITS,GET_5_BITS,GET_5_BITS -inflateDynamicBlock_headerBase dta 3,0,0 ; second byte is modified at runtime! +inflateDynamicBlock_headerBase dta 3,LENGTH_SYMBOLS,0 org inflate_data