-
Notifications
You must be signed in to change notification settings - Fork 0
/
showdict.c
359 lines (287 loc) · 9.97 KB
/
showdict.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* showdict - part of infodump
*
* Dictionary and abbreviation table routines.
*/
#include "tx.h"
#ifdef __STDC__
void configure_dictionary
(unsigned int *, unsigned long *, unsigned long *);
void configure_abbreviations
(unsigned int *, unsigned long *, unsigned long *, unsigned long *,
unsigned long *);
#else
void configure_dictionary ();
void configure_abbreviations ();
#endif
/*
* show_dictionary
*
* List the dictionary in the number of columns specified. If the number of
* columns is one then also display the data associated with each word.
*/
#ifdef __STDC__
void show_dictionary (int columns)
#else
void show_dictionary (columns)
int columns;
#endif
{
unsigned long dict_address, word_address, word_table_base, word_table_end;
unsigned int separator_count, word_size, word_count, length;
int i, flag;
int inform_flags = 0;
int dictpar1;
/* Force default column count if none specified */
if (columns == 0)
columns = ((unsigned int) header.version < V4) ? 5 : 4;
/* Get dictionary configuration */
configure_dictionary (&word_count, &word_table_base, &word_table_end);
if (header.serial[0] >= '0' && header.serial[0] <= '9' &&
header.serial[1] >= '0' && header.serial[1] <= '9' &&
header.serial[2] >= '0' && header.serial[2] <= '1' &&
header.serial[3] >= '0' && header.serial[3] <= '9' &&
header.serial[4] >= '0' && header.serial[4] <= '3' &&
header.serial[5] >= '0' && header.serial[5] <= '9' &&
header.serial[0] != '8') {
inform_flags = TRUE;
}
tx_printf ("\n **** Dictionary ****\n\n");
/* Display the separators */
dict_address = word_table_base;
separator_count = read_data_byte (&dict_address);
tx_printf (" Word separators = \"");
for (; separator_count; separator_count--)
tx_printf ("%c", (char) read_data_byte (&dict_address));
tx_printf ("\"\n");
/* Get word size and count */
word_size = read_data_byte (&dict_address);
word_count = read_data_word (&dict_address);
tx_printf (" Word count = %d, word size = %d\n", (int) word_count, (int) word_size);
/* Display each entry in the dictionary */
for (i = 1; (unsigned int) i <= word_count; i++) {
/* Set column breaks */
if (columns == 1 || (i % columns) == 1)
tx_printf ("\n");
tx_printf ("[%4d] ", (int) i);
/* Calculate address of next entry */
word_address = dict_address;
dict_address += word_size;
if (columns == 1)
tx_printf ("@ $%02x ", (unsigned int) word_address);
/* Display the text for the word */
for (length = decode_text (&word_address); length <= word_size; length++)
tx_printf (" ");
/* For a single column list also display the data for each entry */
if (columns == 1) {
tx_printf ("[");
for (flag = 0; word_address < dict_address; flag++) {
if (flag)
tx_printf (" ");
else
dictpar1 = get_byte(word_address);
tx_printf ("%02x", (unsigned int) read_data_byte (&word_address));
}
tx_printf ("]");
if (inform_flags) {
if (dictpar1 & NOUN)
tx_printf (" <noun>");
if (dictpar1 & PREP)
tx_printf (" <prep>");
if (dictpar1 & PLURAL)
tx_printf (" <plural>");
if (dictpar1 & META)
tx_printf (" <meta>");
if (dictpar1 & VERB_INFORM)
tx_printf (" <verb>");
}
else if (header.version != V6) {
flag = dictpar1 & DATA_FIRST;
switch (flag) {
case DIR_FIRST:
if (dictpar1 & DIR)
tx_printf (" <dir>");
break;
case ADJ_FIRST:
if (dictpar1 & DESC)
tx_printf (" <adj>");
break;
case VERB_FIRST:
if (dictpar1 & VERB)
tx_printf (" <verb>");
break;
case PREP_FIRST:
if (dictpar1 & PREP)
tx_printf (" <prep>");
break;
}
if ((dictpar1 & DIR) && (flag != DIR_FIRST))
tx_printf (" <dir>");
if ((dictpar1 & DESC) && (flag != ADJ_FIRST))
tx_printf (" <adj>");
if ((dictpar1 & VERB) && (flag != VERB_FIRST))
tx_printf (" <verb>");
if ((dictpar1 & PREP) && (flag != PREP_FIRST))
tx_printf (" <prep>");
if (dictpar1 & NOUN)
tx_printf (" <noun>");
if (dictpar1 & SPECIAL)
tx_printf (" <special>");
}
}
}
tx_printf ("\n");
}/* show_dictionary */
/*
* configure_dictionary
*
* Determine the dictionary start and end addresses, together with the number
* of word entries.
*
* Format:
*
* As ASCIC string listing the punctuation to be treated as words. Correct
* recognition of punctuation is important for parsing.
*
* A byte word size. Not the size of the displayed word, but the amount of data
* occupied by each word entry in the dictionary.
*
* A word word count. Total size of dictionary is word count * word size.
*
* Word count word entries. The format of the textual part of the word is fixed
* by the Z machine, but the data following each word can vary. The text for
* the word starts each entry. It is a packed string. The data
* associated with each word is used in parsing a sentence. It includes flags
* to identify the type of word (verb, noun, etc.) and data specific to each
* word type.
*/
#ifdef __STDC__
void configure_dictionary (unsigned int *word_count,
unsigned long *word_table_base,
unsigned long *word_table_end)
#else
void configure_dictionary (word_count,
word_table_base,
word_table_end)
unsigned int *word_count;
unsigned long *word_table_base;
unsigned long *word_table_end;
#endif
{
unsigned long dict_address;
unsigned int separator_count, word_size;
*word_table_base = 0;
*word_table_end = 0;
*word_count = 0;
/* Dictionary base address comes from the header */
*word_table_base = (unsigned long) header.dictionary;
/* Skip the separator list */
dict_address = *word_table_base;
separator_count = read_data_byte (&dict_address);
dict_address += separator_count;
/* Get entry size and count */
word_size = (unsigned int) read_data_byte (&dict_address);
*word_count = (unsigned int) read_data_word (&dict_address);
/* Calculate dictionary end address */
*word_table_end = (dict_address + (word_size * *word_count)) - 1;
}/* configure_dictionary */
/*
* show_abbreviations
*
* Display the list of abbreviations used to compress text strings.
*/
#ifdef __STDC__
void show_abbreviations (void)
#else
void show_abbreviations ()
#endif
{
unsigned long table_address, abbreviation_address;
unsigned long abbr_table_base, abbr_table_end, abbr_data_base, abbr_data_end;
unsigned int abbr_count;
int i;
/* Get abbreviations configuration */
configure_abbreviations (&abbr_count, &abbr_table_base, &abbr_table_end,
&abbr_data_base, &abbr_data_end);
tx_printf ("\n **** Abbreviations ****\n\n");
/* No abbreviations if count is zero (V1 games only) */
if (abbr_count == 0) {
tx_printf ("No abbreviation information.\n");
} else {
/* Display each abbreviation */
table_address = abbr_table_base;
for (i = 0; (unsigned int) i < abbr_count; i++) {
/* Get address of abbreviation text from table */
abbreviation_address = (unsigned long) read_data_word (&table_address) * 2;
tx_printf ("[%2d] \"", (int) i);
(void) decode_text (&abbreviation_address);
tx_printf ("\"\n");
}
}
}/* show_abbreviations */
/*
* configure_abbreviations
*
* Determine the abbreviation table start and end addresses, together
* with the abbreviation text start and end addresses, and the number
* of abbreviation entries.
*
* Format:
*
* The abbreviation information consists of two parts. Firstly a table of
* word sized pointers corresponding to the abbreviation number, and
* secondly, the packed string data for each abbreviation.
*
* Note: the pointers have to be multiplied by 2 *regardless* of the game
* version to get the byte address for each abbreviation.
*/
#ifdef __STDC__
void configure_abbreviations (unsigned int *abbr_count,
unsigned long *abbr_table_base,
unsigned long *abbr_table_end,
unsigned long *abbr_data_base,
unsigned long *abbr_data_end)
#else
void configure_abbreviations (abbr_count,
abbr_table_base,
abbr_table_end,
abbr_data_base,
abbr_data_end)
unsigned int *abbr_count;
unsigned long *abbr_table_base;
unsigned long *abbr_table_end;
unsigned long *abbr_data_base;
unsigned long *abbr_data_end;
#endif
{
unsigned long table_address, address;
int i, tables;
*abbr_table_base = 0;
*abbr_table_end = 0;
*abbr_data_base = 0;
*abbr_data_end = 0;
*abbr_count = 0;
/* The abbreviation table address comes from the header */
*abbr_table_base = (unsigned long) header.abbreviations;
/* Check if there is any abbreviation table (V2 games and above) */
if (*abbr_table_base) {
/* Calculate the number of abbreviation tables (V2 = 1, V3+ = 3) */
tables = ((unsigned int) header.version < V3) ? 1 : 3;
/* Calculate abbreviation count and table end address */
*abbr_count = tables * 32;
*abbr_table_end = *abbr_table_base + (*abbr_count * 2) - 1;
/* Calculate the high and low address for the abbreviation strings */
table_address = *abbr_table_base;
for (i = 0; (unsigned int) i < *abbr_count; i++) {
address = (unsigned long) read_data_word (&table_address) * 2;
if (*abbr_data_base == 0 || address < *abbr_data_base)
*abbr_data_base = address;
if (*abbr_data_end == 0 || address > *abbr_data_end)
*abbr_data_end = address;
}
/* Scan last string to get the actual end of the string */
while (((unsigned int) read_data_word (abbr_data_end) & 0x8000) == 0)
;
(*abbr_data_end)--;
}
}/* configure_abbreviations */