aboutsummaryrefslogtreecommitdiff
path: root/textdecomp.s
blob: 617ba2f5d02996bb44804670b92bc08b87a5afe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

; text decompressor for taipan.
; text is packed 6 bits per character. see textcomp.c
; for details.

 .include "atari.inc"
 .export _print_msg
 .import _cputc

 srcptr = FR1
 outbyte = FR0 ; decoded 6-bit byte
 bitcount = FR0+1 ; counts 8..1, current bit in inbyte
 inbyte = FR0+2
 ysave = FR0+3
 dict_escape = FR0+4

 .rodata
; one or two letter words are not worth listing here. 3 is only good
; if it's used pretty often.
; entry 0 is a dummy! The encoder gets confused by "Z\0". This may get fixed.
; dictionary size cannot exceed 255 bytes.
; the quoted stuff in comments is read by messages.pl, it needs to be exact.
dict00:
dict01: .byte $98, $9d, $73, $54, $53, $80 ; "Li Yuen", 4 occurrences
dict02: .byte $7c, $c1, $05, $4b, $57, $12, $3d, $42, $05, $48, $00 ; "Elder Brother", 3
dict03: .byte $64, $f5, $40 ; "you", 30
dict04: .byte $d7, $c1, $4d, $00 ; " 'em", 8
dict05: .byte $cc, $f5, $40 ; "You", 16
dict06: .byte $d4, $80, $56, $14, $00 ; " have", 11
dict07: .byte $d5, $32, $01, $30, $c0, $00 ; " shall", 6
dict08: .byte $fb, $5c, $49, $50, $8d, $40 ; ") With ", 2
dict09: .byte $05, $21, $cf, $00 ; "argo", 6
dict10: .byte $4c, $82, $50, $00 ; "ship", 10
dict11: .byte $d5, $70, $52, $14, $83, $d5, $4c, $50, $00 ; " warehouse", 4
dict12: .byte $d5, $42, $05, $00 ; " the" 17
dict13: .byte $d4, $f1, $80 ; " of", 14
dict14: .byte $5c, $93, $0c, $00 ; "will", 8
dict15: .byte $d4, $21, $45, $3b, $50, $00 ; " been ", 6
dict16: .byte $d5, $43, $f5, $00 ; " to ", 12
dict17: .byte $20, $14, $f5, $00 ; "has ", 7
dict18: .byte $18, $f4, $b5, $00 ; "for ", 7
dict19: .byte $25, $3d, $40 ; "is ", 9
dict20: .byte $04, $e1, $00 ; "and", 10
dict21: .byte $d4, $30, $53, $20, $00 ; " cash", 8
dict22: .byte $04, $41, $09, $50, $93, $ce, $04, $cd, $40 ; "additional ", 3
dict23: .byte $b8, $12, $50, $04, $e0, $00 ; "Taipan", 3 (but really many more!)
dict24: .byte $d4, $f3, $8c, $67, $50 ; " only ", 3
dict25: .byte $d4, $25, $47, $1c, $54, $93, $00 ; " buggers", 3
dict26: .byte $5c, $95, $08, $d4, $00 ; "with ", 4
dict27: .byte $d4, $64, $8f, $37, $50, $00 ; " from ", 3
dict28: .byte $d5, $70, $4e, $50, $00 ; " want"
dict29: .byte $5c, $f4, $94, $20, $93, $85, $4d, $30, $00 ; "worthiness"
dict30: .byte $d4, $d5, $43, $20, $00 ; " much"
dict31: .byte $10, $91, $86, $15, $21, $4e, $0c, $50, $00 ; "difference"

dict_offsets:
 .byte dict00 - dict00
 .byte dict01 - dict00
 .byte dict02 - dict00
 .byte dict03 - dict00
 .byte dict04 - dict00
 .byte dict05 - dict00
 .byte dict06 - dict00
 .byte dict07 - dict00
 .byte dict08 - dict00
 .byte dict09 - dict00
 .byte dict10 - dict00
 .byte dict11 - dict00
 .byte dict12 - dict00
 .byte dict13 - dict00
 .byte dict14 - dict00
 .byte dict15 - dict00
 .byte dict16 - dict00
 .byte dict17 - dict00
 .byte dict18 - dict00
 .byte dict19 - dict00
 .byte dict20 - dict00
 .byte dict21 - dict00
 .byte dict22 - dict00
 .byte dict23 - dict00
 .byte dict24 - dict00
 .byte dict25 - dict00
 .byte dict26 - dict00
 .byte dict27 - dict00
 .byte dict28 - dict00
 .byte dict29 - dict00
 .byte dict30 - dict00
 .byte dict31 - dict00

; rough estimate of how many bytes are saved by the dictionary
; stuff: the dictionary + extra decoder stuff costs 221 bytes (vs.
; the original textdecode.s without dictionary).
; each dictionary entry saves (length - 2) * (occurrences - 1) bytes.
; with only dict00 - dict23, we'll save around 173 bytes.
; actually it works out to 179 bytes, but the estimate was close.
; we've reached the point of diminishing returns: dict00 - dict31 only
; saves 200 bytes.

 dictsize = * - dict00
 .out .sprintf("dictionary plus dict_offsets is %d bytes", dictsize)

 .rodata
table: ; outbyte values 53..63
 .byte ' ', '!', '%', ',', '.', '?', ':', 39, 40, 41, $9b
 tablesize = * - table

 .ifdef CART_TARGET
  .segment "HIGHCODE"
 .else
  .code
 .endif

; extern void __fastcall__ print_msg(const char *msg);
_print_msg:
 sta srcptr
 stx srcptr+1
 lda #0
 sta dict_escape
 sta outbyte
 ldy #$ff ; since we increment it first thing...

 ldx #6 ; counts 6..1, current bit in outbyte
@nextbyte:
 iny
 lda #8
 sta bitcount
 lda (srcptr),y
 sta inbyte
@bitloop:
 asl inbyte
 rol outbyte
 dex
 beq @decode ; got 6 bits
 dec bitcount
 bne @bitloop
 beq @nextbyte

@decode:
 lda outbyte
 bne @notend
 rts ; 0 = end of message

@notend:
 ldx dict_escape ; was last character a Z?
 beq @normalchar

 jsr dict_lookup
 jmp @noprint

@normalchar:
 cmp #27
 bcs @notlower
 adc #'a'-1 ; 1-26 are a-z
 bne @printit

@notlower:
 cmp #52
 bne @notdict
 inc dict_escape ; Z means next 6 bits are dictionary ID
 bne @noprint

@notdict:
 bcs @notupper
 adc #38 ; 27-52 are A-Z
 bne @printit

@notupper:
 sbc #53 ; 53-63 are table lookups
 tax
 lda table,x

@printit:
 sty ysave ; _cputc trashes Y
 jsr _cputc
 ldy ysave
@noprint:
 lda #0
 sta outbyte
 ldx #6
 dec bitcount
 beq @nextbyte
 bne @bitloop

dict_lookup:
 ; dictionary lookup time. save our state on the stack
 tya
 pha
 lda inbyte
 pha
 lda srcptr
 pha
 lda srcptr+1
 pha
 lda bitcount
 pha

 ; recursive call
 ldx outbyte
 lda dict_offsets,x
 clc
 adc #<dict00
 sta dict_escape ; temp usage
 lda #>dict00
 adc #0
 tax
 lda dict_escape
 jsr _print_msg

 ; restore old state
 lda #0
 sta dict_escape
 pla
 sta bitcount
 pla
 sta srcptr+1
 pla
 sta srcptr
 pla
 sta inbyte
 pla
 tay
 rts

 decodersize = * - _print_msg

 .out .sprintf("print_msg() is %d bytes", decodersize + tablesize)
 .out .sprintf("total textdecomp is %d bytes", decodersize + tablesize + dictsize)