Skip to content

Commit

Permalink
Compress DISASM tables to make binary 240 bytes smaller.
Browse files Browse the repository at this point in the history
Tables are compressed using elias-gamma codes, see included "encoded.fb"
program.
  • Loading branch information
dmsc committed Mar 3, 2024
1 parent 8dc8a31 commit 7f97a93
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Many of the external commands are reduced in size:
| CUT | 702 | 628 |
| DATE | 661 | 509 |
| DIRMAST | 2682 | 2252 |
| DISASS | 1603 | 1419 |
| DISASS | 1603 | 1179 |
| DUMP | 783 | 600 |
| ELSE | 243 | 166 |
| ENDIF | 9 | 9 |
Expand Down
154 changes: 154 additions & 0 deletions tools/encode.fb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
' FastBasic program to compress DISASM tables
' -------------------------------------------

' Tables to encode:
' Addressing mode text, 4 bytes per mode
' 1 byte: text before operand
' 3 bytes: text after operand
' IMP ABS ZP IMM A,X Z,X A,Y IIDX REL IDXI IND Z,Y
DATA X()B. = 0, 0, 0, 35, 0, 0, 0, 40, 42, 40, 40, 0,
DATA B. = 0, 0, 0, 0, 44, 44, 44, 41, 0, 44, 41, 44,
DATA B. = 0, 0, 0, 0, 88, 88, 89, 44, 0, 88, 0, 89,
DATA B. = 0, 0, 0, 0, 0, 0, 0, 89, 0, 41, 0, 0,

' Length of each addressing mode
DATA B. = 0, 2, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1,

' Instruction OPCODE (name) table
DATA B. = 29,6,0,0,0,6,9,0,
DATA B. = 43,6,9,0,0,6,9,0,
DATA B. = 28,6,0,0,0,6,9,0,
DATA B. = 32,6,0,0,0,6,9,0,
DATA B. = 40,2,0,0,21,2,13,0,
DATA B. = 45,2,13,0,21,2,13,0,
DATA B. = 26,2,0,0,0,2,13,0,
DATA B. = 48,2,0,0,0,2,13,0,
DATA B. = 46,4,0,0,0,4,12,0,
DATA B. = 42,4,12,0,22,4,12,0,
DATA B. = 30,4,0,0,0,4,12,0,
DATA B. = 34,4,0,0,0,4,12,0,
DATA B. = 47,1,0,0,0,1,14,0,
DATA B. = 44,1,14,0,22,1,14,0,
DATA B. = 31,1,0,0,0,1,14,0,
DATA B. = 50,1,0,0,0,1,14,0,
DATA B. = 0,8,0,0,20,8,19,0,
DATA B. = 37,0,54,0,20,8,19,0,
DATA B. = 23,8,0,0,20,8,19,0,
DATA B. = 56,8,55,0,0,8,0,0,
DATA B. = 11,5,10,0,11,5,10,0,
DATA B. = 52,5,51,0,11,5,10,0,
DATA B. = 24,5,0,0,11,5,10,0,
DATA B. = 35,5,53,0,11,5,10,0,
DATA B. = 18,3,0,0,18,3,15,0,
DATA B. = 39,3,36,0,18,3,15,0,
DATA B. = 27,3,0,0,0,3,15,0,
DATA B. = 33,3,0,0,0,3,15,0,
DATA B. = 17,7,0,0,17,7,16,0,
DATA B. = 38,7,41,0,17,7,16,0,
DATA B. = 25,7,0,0,0,7,16,0,
DATA B. = 49,7,0,0,0,7,16,0,

' Addressing mode table of each opcode
DATA B. = 0,9,0,0,0,2,2,0,0,3,0,0,0,1,1,0,
DATA B. = 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0,
DATA B. = 1,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0,
DATA B. = 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0,
DATA B. = 0,9,0,0,0,2,2,0,0,3,0,0,1,1,1,0,
DATA B. = 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0,
DATA B. = 0,9,0,0,0,2,2,0,0,3,0,0,10,1,1,0,
DATA B. = 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0,
DATA B. = 0,9,0,0,2,2,2,0,0,0,0,0,1,1,1,0,
DATA B. = 8,7,0,0,5,5,11,0,0,6,0,0,0,4,0,0,
DATA B. = 3,9,3,0,2,2,2,0,0,3,0,0,1,1,1,0,
DATA B. = 8,7,0,0,5,5,11,0,0,6,0,0,4,4,6,0,
DATA B. = 3,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0,
DATA B. = 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0,
DATA B. = 3,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0,
DATA B. = 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0,

' END OF TABLE
DATA B. = 255

DIM PROBS(256)
DIM COMP(512)

' Encode all the tables, reporting the bytes/bits used for each one
B=0
N=0
I=0
REPEAT
IF I=48 THEN ? " TABLE 1:";N;" . "; B
IF I=60 THEN ? " TABLE 2:";N;" . "; B
IF I=60+256 THEN ? " TABLE 3:";N;" . "; B
IF I=60+512 THEN ? " TABLE 4:";N;" . "; B

P = X(I)
INC I
' Encode P
INC PROBS(P)

@elias P+1
UNTIL P = 255

' Pad with zeros
WHILE B : @encode 0 : WEND

? "Total encoded bytes:";N

' Report statistics
INPUT "Statistics report(Y/N)?",YN$
IF YN$="Y" OR YN$="y"

TOT=0
? "Sym Count Bits Total Bits"
FOR I=0 TO 255
BITS = 1
IF I>0 THEN BITS = BITS + 2
IF I>2 THEN BITS = BITS + 2
IF I>6 THEN BITS = BITS + 2
IF I>14 THEN BITS = BITS + 2
IF I>30 THEN BITS = BITS + 2
IF I>62 THEN BITS = BITS + 2
IF I>126 THEN BITS = BITS + 2

IF PROBS(I)
? I,PROBS(I),BITS,PROBS(I)*BITS
TOT = TOT + PROBS(I)*BITS
ENDIF
NEXT
? "TOTAL BITS=";TOT,"BYTES=";(TOT+7)/8
ENDIF

FOR I=0 TO N-1
IF I & 15
? ",";COMP(I);
ELSE
? : ? " DTA ";COMP(I);
ENDIF
NEXT
?

PROC elias ev
max=1
WHILE max+max <= ev : max=max+max : WEND

WHILE max>1
max = max / 2
@encode 0
@encode ev & max <> 0
WEND
@encode 1
ENDPROC

PROC encode bit
BYTE=BYTE/2
IF bit THEN BYTE=BYTE+128
INC B
IF B = 8
COMP(N)=BYTE
BYTE=0
B = 0
INC N
ENDIF
ENDPROC

210 changes: 138 additions & 72 deletions utils/disass.src
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ DATA1ER CPY #136

***
*** OPEN
DOOPEN JSR CLOSE
DOOPEN JSR EXPAND
JSR CLOSE

LDA #4
STA $35A
Expand Down Expand Up @@ -348,66 +349,6 @@ CIOCMD LDX #$10
STA $352
JMP $E456
***
***
* Instruction OPCODE (name) table
NMTAB DTA 29,6,0,0,0,6,9,0
DTA 43,6,9,0,0,6,9,0
DTA 28,6,0,0,0,6,9,0
DTA 32,6,0,0,0,6,9,0
DTA 40,2,0,0,21,2,13,0
DTA 45,2,13,0,21,2,13,0
DTA 26,2,0,0,0,2,13,0
DTA 48,2,0,0,0,2,13,0

DTA 46,4,0,0,0,4,12,0
DTA 42,4,12,0,22,4,12,0
DTA 30,4,0,0,0,4,12,0
DTA 34,4,0,0,0,4,12,0
DTA 47,1,0,0,0,1,14,0
DTA 44,1,14,0,22,1,14,0
DTA 31,1,0,0,0,1,14,0
DTA 50,1,0,0,0,1,14,0

DTA 0,8,0,0,20,8,19,0
DTA 37,0,54,0,20,8,19,0
DTA 23,8,0,0,20,8,19,0
DTA 56,8,55,0,0,8,0,0
DTA 11,5,10,0,11,5,10,0
DTA 52,5,51,0,11,5,10,0
DTA 24,5,0,0,11,5,10,0
DTA 35,5,53,0,11,5,10,0

DTA 18,3,0,0,18,3,15,0
DTA 39,3,36,0,18,3,15,0
DTA 27,3,0,0,0,3,15,0
DTA 33,3,0,0,0,3,15,0
DTA 17,7,0,0,17,7,16,0
DTA 38,7,41,0,17,7,16,0
DTA 25,7,0,0,0,7,16,0
DTA 49,7,0,0,0,7,16,0

***
* Addressing mode table of each opcode
ADTAB DTA 0,9,0,0,0,2,2,0,0,3,0,0,0,1,1,0
DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
DTA 1,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0
DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0

DTA 0,9,0,0,0,2,2,0,0,3,0,0,1,1,1,0
DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
DTA 0,9,0,0,0,2,2,0,0,3,0,0,10,1,1,0
DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0

DTA 0,9,0,0,2,2,2,0,0,0,0,0,1,1,1,0
DTA 8,7,0,0,5,5,11,0,0,6,0,0,0,4,0,0
DTA 3,9,3,0,2,2,2,0,0,3,0,0,1,1,1,0
DTA 8,7,0,0,5,5,11,0,0,6,0,0,4,4,6,0

DTA 3,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0
DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
DTA 3,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0
DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0

***
* Messages
MSTART
Expand Down Expand Up @@ -435,17 +376,142 @@ NMTXT1 DTA C"?AACELOSSALLLRRDICCSSBJBBBBBBBBBCCCCDDIIJNPPPPRRSSSTTTTTT"
NMTXT2 DTA C"?DNMODRBTSDDSOOENPPTTIMCCEMNPRVVLLLLEENNSOHHLLTTEEEAASXXY"
NMTXT3 DTA C"?CDPRAACALXYRLRCCXYXYTPCSQIELKCSCDIVXYXYRPAPAPISCDIXYXASA"

* Addressing mode text, 4 bytes per mode
* 1 byte: text before operand
* 3 bytes: text after operand
* IMP ABS ZP IMM A,X Z,X A,Y IIDX REL IDXI IND Z,Y
ADTXT1 DTA 0, 0, 0, '#', 0, 0, 0, '(', '*', '(', '(', 0
ADTXT2 DTA 0, 0, 0, 0, ',', ',', ',', ')', 0, ',', ')', ','
ADTXT3 DTA 0, 0, 0, 0, 'X', 'X', 'Y', ',', 0, 'X', 0, 'Y'
ADTXT4 DTA 0, 0, 0, 0, 0, 0, 0, 'Y', 0, ')', 0, 0

* Length of each addressing mode
LENTAB DTA 0, 2, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1
*****************************************************
* NEXT SECTION IS COMPRESSED TO USE LESS DISK SPACE *
*****************************************************

* * Addressing mode text, 4 bytes per mode
* * 1 byte: text before operand
* * 3 bytes: text after operand
* * IMP ABS ZP IMM A,X Z,X A,Y IIDX REL IDXI IND Z,Y
* ADTXT1 DTA 0, 0, 0, '#', 0, 0, 0, '(', '*', '(', '(', 0
* ADTXT2 DTA 0, 0, 0, 0, ',', ',', ',', ')', 0, ',', ')', ','
* ADTXT3 DTA 0, 0, 0, 0, 'X', 'X', 'Y', ',', 0, 'X', 0, 'Y'
* ADTXT4 DTA 0, 0, 0, 0, 0, 0, 0, 'Y', 0, ')', 0, 0
*
* * Length of each addressing mode
* LENTAB DTA 0, 2, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1
*
* ***
* * Instruction OPCODE (name) table
* NMTAB DTA 29,6,0,0,0,6,9,0
* DTA 43,6,9,0,0,6,9,0
* DTA 28,6,0,0,0,6,9,0
* DTA 32,6,0,0,0,6,9,0
* DTA 40,2,0,0,21,2,13,0
* DTA 45,2,13,0,21,2,13,0
* DTA 26,2,0,0,0,2,13,0
* DTA 48,2,0,0,0,2,13,0
*
* DTA 46,4,0,0,0,4,12,0
* DTA 42,4,12,0,22,4,12,0
* DTA 30,4,0,0,0,4,12,0
* DTA 34,4,0,0,0,4,12,0
* DTA 47,1,0,0,0,1,14,0
* DTA 44,1,14,0,22,1,14,0
* DTA 31,1,0,0,0,1,14,0
* DTA 50,1,0,0,0,1,14,0
*
* DTA 0,8,0,0,20,8,19,0
* DTA 37,0,54,0,20,8,19,0
* DTA 23,8,0,0,20,8,19,0
* DTA 56,8,55,0,0,8,0,0
* DTA 11,5,10,0,11,5,10,0
* DTA 52,5,51,0,11,5,10,0
* DTA 24,5,0,0,11,5,10,0
* DTA 35,5,53,0,11,5,10,0
*
* DTA 18,3,0,0,18,3,15,0
* DTA 39,3,36,0,18,3,15,0
* DTA 27,3,0,0,0,3,15,0
* DTA 33,3,0,0,0,3,15,0
* DTA 17,7,0,0,17,7,16,0
* DTA 38,7,41,0,17,7,16,0
* DTA 25,7,0,0,0,7,16,0
* DTA 49,7,0,0,0,7,16,0
*
* ***
* * Addressing mode table of each opcode
* ADTAB DTA 0,9,0,0,0,2,2,0,0,3,0,0,0,1,1,0
* DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
* DTA 1,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0
* DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
*
* DTA 0,9,0,0,0,2,2,0,0,3,0,0,1,1,1,0
* DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
* DTA 0,9,0,0,0,2,2,0,0,3,0,0,10,1,1,0
* DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
*
* DTA 0,9,0,0,2,2,2,0,0,0,0,0,1,1,1,0
* DTA 8,7,0,0,5,5,11,0,0,6,0,0,0,4,0,0
* DTA 3,9,3,0,2,2,2,0,0,3,0,0,1,1,1,0
* DTA 8,7,0,0,5,5,11,0,0,6,0,0,4,4,6,0
*
* DTA 3,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0
* DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0
* DTA 3,9,0,0,2,2,2,0,0,3,0,0,1,1,1,0
* DTA 8,7,0,0,0,5,5,0,0,6,0,0,0,4,4,0

***
* Reads compressed data and expands into tables
EXPAND LDY #1
STY TMP
DEY
LDX #0
EXP1 LDA #1
EXP2 JSR EXPBIT
BCS EXP3
JSR EXPBIT
ROL
BCC EXP2
RTS
EXP3 ADC #254
EXPOUT STA TABOUT,Y
INY
BNE EXP1
INC EXPOUT+2
BNE EXP1
* Read next data bit of input
EXPBIT LSR TMP
BNE EXP5
PHA
EXPINP LDA TABINP,X
ROR
INX
BNE EXP4
INC EXPINP+2
EXP4 STA TMP
PLA
EXP5 RTS

TABINP DTA 7,225,17,140,104,4,35,248,163,24,197,40,70,100,20,35
DTA 18,197,143,130,81,48,138,68,113,20,28,69,254,81,100,68
DTA 222,164,105,146,166,74,125,141,140,66,141,92,35,43,214,215
DTA 200,0,214,215,200,8,246,40,173,140,74,43,163,180,178,104
DTA 223,202,2,246,173,140,106,124,44,142,104,44,142,26,139,171
DTA 198,199,226,128,198,199,226,2,228,169,142,98,170,163,166,58
DTA 0,121,170,11,154,167,122,240,136,193,8,131,178,168,35,6
DTA 35,44,16,60,98,48,194,10,6,43,112,240,66,137,46,148
DTA 232,34,150,34,44,148,232,130,229,66,137,14,66,41,202,66
DTA 137,14,26,14,26,2,140,64,8,226,160,33,192,10,225,33
DTA 192,128,132,135,0,131,4,14,18,8,56,168,129,136,12,18
DTA 8,184,72,224,129,128,11,18,120,32,224,145,183,135,39,131
DTA 129,151,114,125,140,19,185,237,225,36,131,129,151,114,125,140
DTA 71,222,30,78,50,24,120,41,215,199,120,228,237,225,104,50
DTA 24,120,41,215,199,120,228,182,63,201,96,224,82,10,215,199
DTA 67,36,108,123,56,201,96,224,82,10,215,99,172,67,228,182
DTA 135,147,12,6,94,202,245,49,14,145,219,30,78,50,24,120
DTA 41,215,199,56 ; ,0,0 omitted

* Output tables
TABOUT
ADTXT1 ORG *+12
ADTXT2 ORG *+12
ADTXT3 ORG *+12
ADTXT4 ORG *+12
LENTAB ORG *+12
NMTAB ORG *+256
ADTAB ORG *+256

NAME EQU *

Expand Down

0 comments on commit 7f97a93

Please sign in to comment.