-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxd.c
2068 lines (1814 loc) · 63.1 KB
/
txd.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* txd.c V7/3
*
* Z code disassembler for Infocom game files
*
* Requires txio.c, getopt.c, showverb.c and tx.h.
*
* Works for all V1, V2, V3, V4, V5, V6, V7 and V8 games.
*
* Usage: txd story-file-name
*
* Mark Howell 25 August 1992 [email protected]
*
* History:
* Merge separate disassemblers for each type into one program
* Fix logic error in low routine scan
* Force PC past start PC in middle routine scan
* Update opcodes
* Add pre-action and action verb names to routines
* Add print mode descriptions
* Wrap long lines of text
* Cleanup for 16 bit machines
* Change JGE and JLE to JG and JL
* Add support for V1 and V2 games
* Fix bug in printing of last string
* Add symbolic names for routines and labels
* Add verb syntax
* Improve verb formatting
* Add command line options
* Fix check for low address
* Add a switch to turn off grammar
* Add support for V6 games
* Make start of code for low scan the end of dictionary data
* Generate Inform style syntax as an option
* Add dump style and width option
* Fix lint warnings
* Fix inter-routine backward jumps
* Update operand names
* Add support for V7 and V8 games
* Limit cache size to MAX_CACHE pages
* Improve translation of constants to symbols
* Distinguish between pre-actions and Inform parsing routines
* Introduce indirect operands, eg. load [sp] sp
* Fix object 0 problem
* Add support for European characters (codes up to 223)
* Add support for Inform 6 (helped by Matthew T. Russotto)
* Add support for GV2 (MTR)
* Add support for Infocom V6 games
* Fix dependencies on sizeof(int) == 2 (mostly cosmetic) NOT DONE
* Add -S dump-strings at address option
* Remove GV2A support
* Add unicode disassembly support
* Add Inform and user symbol table support
*/
#include "tx.h"
#define MAX_PCS 100
#define ROUND_CODE(address) ((address + (code_scaler - 1)) & ~(code_scaler - 1))
#define ROUND_DATA(address) ((address + (story_scaler - 1)) & ~(story_scaler - 1))
#ifndef HAS_GETOPT
#ifdef __STDC__
extern int getopt (int, char *[], const char *);
#else
extern int getopt ();
#endif
#endif
#ifdef __STDC__
static void process_story (const char *);
static void decode_program (void);
static int decode_routine (void);
static int decode_code (void);
static int decode_opcode (void);
static int decode_operands (const char *, int, int, int, int, int, int);
static int decode_parameters (int *);
static int decode_parameter (int, int);
static int decode_extra (void);
static void decode_strings (unsigned long);
static void scan_strings (unsigned long);
static int lookup_string (unsigned long);
static void lookup_verb (unsigned long);
static void setup_dictionary (void);
static int in_dictionary (unsigned long);
static void add_label (unsigned long);
static void add_routine (unsigned long);
static int lookup_label (unsigned long, int);
static int lookup_routine (unsigned long, int);
static void renumber_cref (cref_item_t *);
static void free_cref (cref_item_t *);
static int print_object_desc (unsigned int);
static void print_text (unsigned long *);
static void print_integer (unsigned int, int);
static void dump_data (unsigned long, unsigned long);
static void dump_opcode (unsigned long, int, int, int *, int);
static void dump_operand (unsigned long *, int, int, int *, int *);
static void print_variable (int);
#else
static void process_story ();
static void decode_program ();
static int decode_routine ();
static int decode_code ();
static int decode_opcode ();
static int decode_operands ();
static int decode_parameters ();
static int decode_parameter ();
static int decode_extra ();
static void decode_strings ();
static void scan_strings ();
static int lookup_string ();
static void lookup_verb ();
static void setup_dictionary ();
static int in_dictionary ();
static void add_label ();
static void add_routine ();
static int lookup_label ();
static int lookup_routine ();
static void renumber_cref ();
static void free_cref ();
static int print_object_desc ();
static void print_text ();
static void print_integer ();
static void dump_data ();
static void dump_opcode ();
static void dump_operand ();
static void print_variable ();
#endif
static unsigned long pctable[MAX_PCS];
static int pcindex = 0;
static unsigned long start_data_pc, end_data_pc;
static decode_t decode;
static opcode_t opcode;
static cref_item_t *strings_base = NULL;
static cref_item_t *routines_base = NULL;
static cref_item_t *current_routine = NULL;
static int locals_count = 0;
static unsigned long start_of_routine = 0;
static unsigned int verb_count = 0;
static unsigned int action_count = 0;
static unsigned int parse_count = 0;
static unsigned int parser_type = 0;
static unsigned int prep_type = 0;
static unsigned long verb_table_base = 0;
static unsigned long verb_data_base = 0;
static unsigned long action_table_base = 0;
static unsigned long preact_table_base = 0;
static unsigned long prep_table_base = 0;
static unsigned long prep_table_end = 0;
static const int verb_sizes[4] = { 2, 4, 7, 7 };
static unsigned long dict_start = 0;
static unsigned long dict_end = 0;
static unsigned long word_size = 0;
static unsigned long word_count = 0;
static unsigned long code_base = 0;
static unsigned int obj_count = 0;
static unsigned long obj_table_base = 0, obj_table_end = 0, obj_data_base = 0, obj_data_end = 0;
static unsigned short inform_version = 0;
static unsigned long class_numbers_base = 0, class_numbers_end = 0;
static unsigned long property_names_base = 0, property_names_end = 0;
static unsigned long attr_names_base = 0, attr_names_end = 0;
#ifndef HAS_GETOPT
/* getopt linkages */
extern int optind;
extern const char *optarg;
#endif
static int option_labels = 1;
static int option_grammar = 1;
static int option_dump = 0;
static int option_width = 79;
static int option_symbols = 0;
static unsigned long string_location = 0;
#ifdef __STDC__
int main (int argc, char *argv [])
#else
int main (argc, argv)
int argc;
char *argv [];
#endif
{
int c, errflg = 0;
/* Parse the options */
while ((c = getopt (argc, argv, "abdghnsw:S:u:")) != EOF) {
switch (c) {
case 'a':
option_inform = 6;
break;
case 'd':
option_dump = 1;
break;
case 'g':
option_grammar = 0;
break;
case 'n':
option_labels = 0;
break;
case 'w':
option_width = atoi (optarg);
break;
case 'u':
init_symbols(optarg);
/*FALLTHRU*/
case 's':
option_symbols = 1;
break;
case 'S':
#ifdef HAS_STRTOUL
string_location = strtoul(optarg, (char **)NULL, 0);
#else
string_location = atoi(optarg);
#endif
break;
case 'h':
case '?':
default:
errflg++;
}
}
/* Display usage if unknown flag or no story file */
if (errflg || optind >= argc) {
(void) fprintf (stderr, "usage: %s [options...] story-file [story-file...]\n\n", argv[0]);
(void) fprintf (stderr, "TXD version 7/3 - disassemble Infocom story files. By Mark Howell\n");
(void) fprintf (stderr, "Works with V1 to V8 Infocom games.\n\n");
(void) fprintf (stderr, "\t-a generate alternate syntax used by Inform\n");
(void) fprintf (stderr, "\t-d dump hex of opcodes and data\n");
(void) fprintf (stderr, "\t-g turn off grammar for action routines\n");
(void) fprintf (stderr, "\t-n use addresses instead of labels\n");
(void) fprintf (stderr, "\t-w n display width (0 = no wrap)\n");
(void) fprintf (stderr, "\t-s Symbolic mode (Inform 6+ only)\n");
(void) fprintf (stderr, "\t-u <file> Read user symbol table, implies -s for Inform games\n");
(void) fprintf (stderr, "\t-S n Dump high strings only, starting at address n\n");
exit (EXIT_FAILURE);
}
/* Process any story files on the command line */
for (; optind < argc; optind++)
process_story (argv[optind]);
exit (EXIT_SUCCESS);
return (0);
}/* main */
#ifdef __STDC__
static void process_story (const char *file_name)
#else
static void process_story (file_name)
const char *file_name;
#endif
{
tx_set_width (option_width);
open_story (file_name);
configure (V1, V8);
load_cache ();
setup_dictionary ();
if (option_grammar)
configure_parse_tables (&verb_count, &action_count, &parse_count, &parser_type, &prep_type,
&verb_table_base, &verb_data_base,
&action_table_base, &preact_table_base,
&prep_table_base, &prep_table_end);
if (option_symbols && (parser_type >= inform_gv1)) {
configure_object_tables (&obj_count, &obj_table_base, &obj_table_end,
&obj_data_base, &obj_data_end);
configure_inform_tables(obj_data_end, &inform_version, &class_numbers_base, &class_numbers_end,
&property_names_base, &property_names_end, &attr_names_base, &attr_names_end);
}
if (header.version != V6 && header.version != V7) {
decode.pc = code_base = dict_end;
decode.initial_pc = (unsigned long) header.start_pc - 1;
} else {
decode.pc = code_base = (unsigned long) header.routines_offset * story_scaler;
decode.initial_pc = decode.pc + (unsigned long) header.start_pc * code_scaler;
}
tx_printf ("Resident data ends at %lx, program starts at %lx, file ends at %lx\n",
(unsigned long) header.resident_size, (unsigned long) decode.initial_pc, (unsigned long) file_size);
tx_printf ("\nStarting analysis pass at address %lx\n", (unsigned long) decode.pc);
#if defined(TXD_DEBUG)
decode.first_pass = 0;
decode.low_address = decode.initial_pc;
decode.high_address = file_size;
#if 0
decode.low_address = atoi (getenv ("LOW_ADDRESS"));
decode.high_address = atoi (getenv ("HIGH_ADDRESS"));
#endif
#else
decode.first_pass = 1;
#endif
if (string_location) {
decode_strings (string_location);
exit(0);
}
decode_program ();
scan_strings (decode.pc);
#if !defined(TXD_DEBUG)
tx_printf ("\nEnd of analysis pass, low address = %lx, high address = %lx\n",
(unsigned long) decode.low_address, (unsigned long) decode.high_address);
if (start_data_pc)
tx_printf ("\n%ld bytes of data in code from %lx to %lx\n",
(unsigned long) (end_data_pc - start_data_pc),
(unsigned long) start_data_pc, (unsigned long) end_data_pc);
if ((decode.low_address - code_base) >= story_scaler) {
tx_printf ("\n%ld bytes of data before code from %lx to %lx\n",
(unsigned long) (decode.low_address - code_base),
(unsigned long) code_base, (unsigned long) decode.low_address);
if (option_dump) {
tx_printf ("\n[Start of data");
if (option_labels == 0)
tx_printf (" at %lx", (unsigned long) code_base);
tx_printf ("]\n\n");
dump_data (code_base, decode.low_address - 1);
tx_printf ("\n[End of data");
if (option_labels == 0)
tx_printf (" at %lx", (unsigned long) (decode.low_address - 1));
tx_printf ("]\n");
}
}
if (option_labels)
renumber_cref (routines_base);
decode.first_pass = 0;
decode_program ();
decode_strings (decode.pc);
#endif
close_story ();
}/* process_story */
/* decode_program - Decode Z code in two passes */
#ifdef __STDC__
static void decode_program (void)
#else
static void decode_program ()
#endif
{
unsigned long pc, low_pc, high_pc, prev_low_pc, prev_high_pc;
int i, flag, vars;
if (decode.first_pass) {
if (decode.pc < decode.initial_pc) {
/* Scan for low routines */
decode.pc = ROUND_CODE (decode.pc);
for (pc = decode.pc, flag = 0; pc < decode.initial_pc && flag == 0; pc += code_scaler) {
for (vars = (char) read_data_byte (&pc); vars < 0 || vars > 15; vars = (char) read_data_byte (&pc))
pc = ROUND_CODE (pc);
decode.pc = pc - 1;
for (i = 0, flag = 1; i < 3 && flag; i++) {
pcindex = 0;
decode.pc = ROUND_CODE (decode.pc);
if (decode_routine () != END_OF_ROUTINE || pcindex)
flag = 0;
}
decode.pc = pc - 1;
}
if (flag && (unsigned int) header.version < V5) {
pc = decode.pc;
vars = (char) read_data_byte (&pc);
low_pc = decode.pc;
for (pc = pc + (vars * 2) - 1, flag = 0; pc >= low_pc && flag == 0; pc -= story_scaler) {
decode.pc = pc;
for (i = 0, flag = 1; i < 3 && flag; i++) {
pcindex = 0;
decode.pc = ROUND_CODE (decode.pc);
if (decode_routine () != END_OF_ROUTINE || pcindex)
flag = 0;
}
decode.pc = pc;
}
}
if (flag == 0 || decode.pc > decode.initial_pc)
decode.pc = decode.initial_pc;
}
/* Fill in middle routines */
decode.low_address = decode.pc;
decode.high_address = decode.pc;
start_data_pc = 0;
end_data_pc = 0;
do {
if (option_labels) {
free_cref (routines_base);
routines_base = NULL;
}
prev_low_pc = decode.low_address;
prev_high_pc = decode.high_address;
flag = 0;
pcindex = 0;
low_pc = decode.low_address;
high_pc = decode.high_address;
pc = decode.pc = decode.low_address;
while (decode.pc <= high_pc || decode.pc <= decode.initial_pc) {
if (start_data_pc == decode.pc)
decode.pc = end_data_pc;
if (decode_routine () != END_OF_ROUTINE) {
if (start_data_pc == 0)
start_data_pc = decode.pc;
flag = 1;
end_data_pc = 0;
pcindex = 0;
pc = ROUND_CODE (pc);
do {
pc += code_scaler;
vars = (char) read_data_byte (&pc);
pc--;
} while (vars < 0 || vars > 15);
decode.pc = pc;
} else {
if (pc < decode.initial_pc && decode.pc > decode.initial_pc) {
pc = decode.pc = decode.initial_pc;
decode.low_address = low_pc;
decode.high_address = high_pc;
} else {
if (start_data_pc && end_data_pc == 0)
end_data_pc = pc;
pc = ROUND_CODE (decode.pc);
if (flag == 0) {
low_pc = decode.low_address;
high_pc = decode.high_address;
}
}
}
}
decode.low_address = low_pc;
decode.high_address = high_pc;
} while (low_pc < prev_low_pc || high_pc > prev_high_pc);
/* Scan for high routines */
pc = decode.pc;
while (decode_routine () == END_OF_ROUTINE) {
decode.high_address = pc;
pc = decode.pc;
}
} else {
tx_printf ("\n[Start of code");
if (option_labels == 0)
tx_printf (" at %lx", (unsigned long) decode.low_address);
tx_printf ("]\n");
for (decode.pc = decode.low_address;
decode.pc <= decode.high_address; )
(void) decode_routine ();
tx_printf ("\n[End of code");
if (option_labels == 0)
tx_printf (" at %lx", (unsigned long) decode.pc);
tx_printf ("]\n");
}
}/* decode_program */
/* decode_routine - Decode a routine from start address to last instruction */
#ifdef __STDC__
static int decode_routine (void)
#else
static int decode_routine ()
#endif
{
unsigned long old_pc, old_start;
cref_item_t *cref_item;
int vars, status, i, locals;
if (decode.first_pass) {
cref_item = NULL;
if (option_labels)
cref_item = current_routine;
old_start = start_of_routine;
locals = locals_count;
old_pc = decode.pc;
decode.pc = ROUND_CODE (decode.pc);
vars = read_data_byte (&decode.pc);
if (vars >= 0 && vars <= 15) {
if (option_labels)
add_routine (decode.pc - 1);
locals_count = vars;
if ((unsigned int) header.version < V5)
for (; vars; vars--)
(void) read_data_word (&decode.pc);
start_of_routine = decode.pc;
if (decode_code () == END_OF_ROUTINE)
return (END_OF_ROUTINE);
if (option_labels)
current_routine = cref_item;
start_of_routine = old_start;
locals_count = locals;
}
decode.pc = old_pc;
if ((status = decode_code ()) != END_OF_ROUTINE) {
decode.pc = old_pc;
} else {
pctable[pcindex++] = old_pc;
if (pcindex == MAX_PCS) {
(void) fprintf (stderr, "\nFatal: too many orphan code fragments\n");
exit (EXIT_FAILURE);
}
}
} else {
if (decode.pc == start_data_pc) {
if (option_dump) {
tx_printf ("\n[Start of data");
if (option_labels == 0)
tx_printf (" at %lx", (unsigned long) start_data_pc);
tx_printf ("]\n\n");
dump_data (start_data_pc, end_data_pc - 1);
tx_printf ("\n[End of data");
if (option_labels == 0)
tx_printf (" at %lx", (unsigned long) (end_data_pc - 1));
tx_printf ("]\n");
}
decode.pc = end_data_pc;
}
for (i = 0; i < pcindex && decode.pc != pctable[i]; i++)
;
if (i == pcindex) {
decode.pc = ROUND_CODE (decode.pc);
start_of_routine = decode.pc;
vars = read_data_byte (&decode.pc);
if (option_labels) {
tx_printf ("%soutine %c%04d, %d local",
(decode.pc - 1 == decode.initial_pc) ? "\nMain r" : "\nR",
(option_inform) ? 'r' : 'R',
(int) lookup_routine (decode.pc - 1, 1),
(int) vars);
} else {
tx_printf ("%soutine %lx, %d local",
(decode.pc - 1 == decode.initial_pc) ? "\nMain r" : "\nR",
(unsigned long) (decode.pc - 1),
(int) vars);
}
if (vars != 1)
tx_printf ("s");
if ((unsigned int) header.version < V5) {
tx_printf (" (");
tx_fix_margin (1);
for (; vars; vars--) {
tx_printf ("%04x", (unsigned int) read_data_word (&decode.pc));
if (vars > 1)
tx_printf (", ");
}
tx_fix_margin (0);
tx_printf (")");
}
tx_printf ("\n");
lookup_verb (start_of_routine);
tx_printf ("\n");
} else
tx_printf ("\norphan code fragment:\n\n");
status = decode_code ();
}
return (status);
}/* decode_routine */
/* decode_code - grab opcode and determine the class */
#ifdef __STDC__
static int decode_code (void)
#else
static int decode_code ()
#endif
{
int status;
int label;
decode.high_pc = decode.pc;
do {
if (decode.first_pass == 0) {
if (option_labels) {
label = lookup_label (decode.pc, 0);
if (label != 0)
tx_printf ("%c%04d: ", (option_inform) ? 'l' : 'L', (int) label);
else
tx_printf (" ");
} else
tx_printf ("%5lx: ", (unsigned long) decode.pc);
}
opcode.opcode = read_data_byte (&decode.pc);
if ((unsigned int) header.version > V4 && opcode.opcode == 0xbe) {
opcode.opcode = read_data_byte (&decode.pc);
opcode.class = EXTENDED_OPERAND;
} else if (opcode.opcode < 0x80)
opcode.class = TWO_OPERAND;
else
if (opcode.opcode < 0xb0)
opcode.class = ONE_OPERAND;
else
if (opcode.opcode < 0xc0)
opcode.class = ZERO_OPERAND;
else
opcode.class = VARIABLE_OPERAND;
status = decode_opcode ();
} while (status == END_OF_INSTRUCTION);
return (status);
}/* decode_code */
/* decode_opcode - Check and decode the opcode itself */
#define caseline(opc, text, par1, par2, par3, par4, extra, type) \
case opc: return (decode_operands (text, par1, par2, par3, par4, extra, type))
#ifdef __STDC__
static int decode_opcode (void)
#else
static int decode_opcode ()
#endif
{
int code;
code = opcode.opcode;
switch (opcode.class) {
case EXTENDED_OPERAND:
code &= 0x3f;
switch (code) {
caseline (0x00, "SAVE", LOW_ADDR, NUMBER, LOW_ADDR, NIL, STORE, PLAIN);
caseline (0x01, "RESTORE", LOW_ADDR, NUMBER, LOW_ADDR, NIL, STORE, PLAIN);
caseline (0x02, "LOG_SHIFT", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x03, "ART_SHIFT", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x04, "SET_FONT", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x05, "DRAW_PICTURE", NUMBER, NUMBER, NUMBER, NIL, NONE, PLAIN);
caseline (0x06, "PICTURE_DATA", NUMBER, LOW_ADDR, NIL, NIL, BRANCH, PLAIN);
caseline (0x07, "ERASE_PICTURE", NUMBER, NUMBER, NUMBER, NIL, NONE, PLAIN);
caseline (0x08, "SET_MARGINS", NUMBER, NUMBER, NUMBER, NIL, NONE, PLAIN);
caseline (0x09, "SAVE_UNDO", NIL, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x0A, "RESTORE_UNDO", NIL, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x10, "MOVE_WINDOW", NUMBER, NUMBER, NUMBER, NIL, NONE, PLAIN);
caseline (0x11, "WINDOW_SIZE", NUMBER, NUMBER, NUMBER, NIL, NONE, PLAIN);
caseline (0x12, "WINDOW_STYLE", NUMBER, NUMBER, NUMBER, NIL, NONE, PLAIN);
caseline (0x13, "GET_WIND_PROP", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x14, "SCROLL_WINDOW", NUMBER, NUMBER, NIL, NIL, NONE, PLAIN);
caseline (0x15, "POP_STACK", NUMBER, LOW_ADDR, NIL, NIL, NONE, PLAIN);
caseline (0x16, "READ_MOUSE", LOW_ADDR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x17, "MOUSE_WINDOW", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x18, "PUSH_STACK", NUMBER, LOW_ADDR, NIL, NIL, BRANCH, PLAIN);
caseline (0x19, "PUT_WIND_PROP", NUMBER, NUMBER, ANYTHING, NIL, NONE, PLAIN);
caseline (0x1A, "PRINT_FORM", LOW_ADDR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x1B, "MAKE_MENU", NUMBER, LOW_ADDR, NIL, NIL, BRANCH, PLAIN);
caseline (0x1C, "PICTURE_TABLE", LOW_ADDR, NIL, NIL, NIL, NONE, PLAIN);
default:
return (decode_operands ("ILLEGAL", NIL, NIL, NIL, NIL, NONE, ILLEGAL));
}
case TWO_OPERAND:
code &= 0x1f;
case VARIABLE_OPERAND:
code &= 0x3f;
switch (code) {
caseline (0x01, "JE", ANYTHING, ANYTHING, ANYTHING, ANYTHING, BRANCH, PLAIN);
caseline (0x02, "JL", NUMBER, NUMBER, NIL, NIL, BRANCH, PLAIN);
caseline (0x03, "JG", NUMBER, NUMBER, NIL, NIL, BRANCH, PLAIN);
caseline (0x04, "DEC_CHK", VAR, NUMBER, NIL, NIL, BRANCH, PLAIN);
caseline (0x05, "INC_CHK", VAR, NUMBER, NIL, NIL, BRANCH, PLAIN);
caseline (0x06, "JIN", OBJECT, OBJECT, NIL, NIL, BRANCH, PLAIN);
caseline (0x07, "TEST", NUMBER, NUMBER, NIL, NIL, BRANCH, PLAIN);
caseline (0x08, "OR", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x09, "AND", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x0A, "TEST_ATTR", OBJECT, ATTRNUM, NIL, NIL, BRANCH, PLAIN);
caseline (0x0B, "SET_ATTR", OBJECT, ATTRNUM, NIL, NIL, NONE, PLAIN);
caseline (0x0C, "CLEAR_ATTR", OBJECT, ATTRNUM, NIL, NIL, NONE, PLAIN);
caseline (0x0D, "STORE", VAR, ANYTHING, NIL, NIL, NONE, PLAIN);
caseline (0x0E, "INSERT_OBJ", OBJECT, OBJECT, NIL, NIL, NONE, PLAIN);
caseline (0x0F, "LOADW", LOW_ADDR, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x10, "LOADB", LOW_ADDR, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x11, "GET_PROP", OBJECT, PROPNUM, NIL, NIL, STORE, PLAIN);
caseline (0x12, "GET_PROP_ADDR", OBJECT, PROPNUM, NIL, NIL, STORE, PLAIN);
caseline (0x13, "GET_NEXT_PROP", OBJECT, PROPNUM, NIL, NIL, STORE, PLAIN);
caseline (0x14, "ADD", LOW_ADDR, LOW_ADDR, NIL, NIL, STORE, PLAIN);
caseline (0x15, "SUB", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x16, "MUL", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x17, "DIV", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x18, "MOD", NUMBER, NUMBER, NIL, NIL, STORE, PLAIN);
caseline (0x21, "STOREW", LOW_ADDR, NUMBER, ANYTHING, NIL, NONE, PLAIN);
caseline (0x22, "STOREB", LOW_ADDR, NUMBER, ANYTHING, NIL, NONE, PLAIN);
caseline (0x23, "PUT_PROP", OBJECT, NUMBER, ANYTHING, NIL, NONE, PLAIN);
caseline (0x25, "PRINT_CHAR", PCHAR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x26, "PRINT_NUM", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x27, "RANDOM", NUMBER, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x28, "PUSH", ANYTHING, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2A, "SPLIT_WINDOW", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2B, "SET_WINDOW", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x33, "OUTPUT_STREAM", PATTR, LOW_ADDR, NUMBER, NIL, NONE, PLAIN);
caseline (0x34, "INPUT_STREAM", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x35, "SOUND_EFFECT", NUMBER, NUMBER, NUMBER, ROUTINE, NONE, PLAIN);
default:
switch ((unsigned int) header.version) {
case V1:
case V2:
case V3:
switch (code) {
caseline (0x20, "CALL", ROUTINE, ANYTHING, ANYTHING, ANYTHING, STORE, CALL);
caseline (0x24, "READ", LOW_ADDR, LOW_ADDR, NIL, NIL, NONE, PLAIN);
caseline (0x29, "PULL", VAR, NIL, NIL, NIL, NONE, PLAIN);
}
case V4:
switch (code) {
caseline (0x19, "CALL_2S", ROUTINE, ANYTHING, NIL, NIL, STORE, CALL);
caseline (0x20, "CALL_VS", ROUTINE, ANYTHING, ANYTHING, ANYTHING, STORE, CALL);
caseline (0x24, "READ", LOW_ADDR, LOW_ADDR, NUMBER, ROUTINE, NONE, PLAIN);
caseline (0x29, "PULL", VAR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2C, "CALL_VS2", ROUTINE, ANYTHING, ANYTHING, ANYTHING, STORE, CALL);
caseline (0x2D, "ERASE_WINDOW", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2E, "ERASE_LINE", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2F, "SET_CURSOR", NUMBER, NUMBER, NIL, NIL, NONE, PLAIN);
caseline (0x31, "SET_TEXT_STYLE", VATTR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x32, "BUFFER_MODE", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x36, "READ_CHAR", NUMBER, NUMBER, ROUTINE, NIL, STORE, PLAIN);
caseline (0x37, "SCAN_TABLE", ANYTHING, LOW_ADDR, NUMBER, NUMBER, BOTH, PLAIN);
}
case V5:
case V7:
case V8:
switch (code) {
caseline (0x19, "CALL_2S", ROUTINE, ANYTHING, NIL, NIL, STORE, CALL);
caseline (0x1A, "CALL_2N", ROUTINE, ANYTHING, NIL, NIL, NONE, CALL);
caseline (0x1B, "SET_COLOUR", NUMBER, NUMBER, NIL, NIL, NONE, PLAIN);
caseline (0x20, "CALL_VS", ROUTINE, ANYTHING, ANYTHING, ANYTHING, STORE, CALL);
caseline (0x24, "READ", LOW_ADDR, LOW_ADDR, NUMBER, ROUTINE, STORE, PLAIN);
caseline (0x29, "PULL", VAR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2C, "CALL_VS2", ROUTINE, ANYTHING, ANYTHING, ANYTHING, STORE, CALL);
caseline (0x2D, "ERASE_WINDOW", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2E, "ERASE_LINE", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2F, "SET_CURSOR", NUMBER, NUMBER, NIL, NIL, NONE, PLAIN);
caseline (0x31, "SET_TEXT_STYLE", VATTR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x32, "BUFFER_MODE", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x36, "READ_CHAR", NUMBER, NUMBER, ROUTINE, NIL, STORE, PLAIN);
caseline (0x37, "SCAN_TABLE", ANYTHING, LOW_ADDR, NUMBER, NUMBER, BOTH, PLAIN);
caseline (0x39, "CALL_VN", ROUTINE, ANYTHING, ANYTHING, ANYTHING, NONE, CALL);
caseline (0x3A, "CALL_VN2", ROUTINE, ANYTHING, ANYTHING, ANYTHING, NONE, CALL);
caseline (0x3B, "TOKENISE", LOW_ADDR, LOW_ADDR, LOW_ADDR, NUMBER, NONE, PLAIN);
caseline (0x3C, "ENCODE_TEXT", LOW_ADDR, NUMBER, NUMBER, LOW_ADDR, NONE, PLAIN);
caseline (0x3D, "COPY_TABLE", LOW_ADDR, LOW_ADDR, NUMBER, NIL, NONE, PLAIN);
caseline (0x3E, "PRINT_TABLE", LOW_ADDR, NUMBER, NUMBER, NUMBER, NONE, PLAIN);
caseline (0x3F, "CHECK_ARG_COUNT", NUMBER, NIL, NIL, NIL, BRANCH, PLAIN);
}
case V6:
switch (code) {
caseline (0x19, "CALL_2S", ROUTINE, ANYTHING, NIL, NIL, STORE, CALL);
caseline (0x1A, "CALL_2N", ROUTINE, ANYTHING, NIL, NIL, NONE, CALL);
caseline (0x1B, "SET_COLOUR", NUMBER, NUMBER, NIL, NIL, NONE, PLAIN);
caseline (0x1C, "THROW", ANYTHING, NUMBER, NIL, NIL, NONE, PLAIN);
caseline (0x20, "CALL_VS", ROUTINE, ANYTHING, ANYTHING, ANYTHING, STORE, CALL);
caseline (0x24, "READ", LOW_ADDR, LOW_ADDR, NUMBER, ROUTINE, STORE, PLAIN);
caseline (0x29, "PULL", LOW_ADDR, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x2C, "CALL_VS2", ROUTINE, ANYTHING, ANYTHING, ANYTHING, STORE, CALL);
caseline (0x2D, "ERASE_WINDOW", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2E, "ERASE_LINE", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x2F, "SET_CURSOR", NUMBER, NUMBER, NUMBER, NIL, NONE, PLAIN);
caseline (0x30, "GET_CURSOR", LOW_ADDR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x31, "SET_TEXT_STYLE", VATTR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x32, "BUFFER_MODE", NUMBER, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x36, "READ_CHAR", NUMBER, NUMBER, ROUTINE, NIL, STORE, PLAIN);
caseline (0x37, "SCAN_TABLE", ANYTHING, LOW_ADDR, NUMBER, NUMBER, BOTH, PLAIN);
caseline (0x38, "NOT", NUMBER, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x39, "CALL_VN", ROUTINE, ANYTHING, ANYTHING, ANYTHING, NONE, CALL);
caseline (0x3A, "CALL_VN2", ROUTINE, ANYTHING, ANYTHING, ANYTHING, NONE, CALL);
caseline (0x3B, "TOKENISE", LOW_ADDR, LOW_ADDR, LOW_ADDR, NUMBER, NONE, PLAIN);
caseline (0x3C, "ENCODE_TEXT", LOW_ADDR, NUMBER, NUMBER, LOW_ADDR, NONE, PLAIN);
caseline (0x3D, "COPY_TABLE", LOW_ADDR, NUMBER, LOW_ADDR, NIL, NONE, PLAIN);
caseline (0x3E, "PRINT_TABLE", LOW_ADDR, NUMBER, NUMBER, NUMBER, NONE, PLAIN);
caseline (0x3F, "CHECK_ARG_COUNT", NUMBER, NIL, NIL, NIL, BRANCH, PLAIN);
}
}
return (decode_operands ("ILLEGAL", NIL, NIL, NIL, NIL, NONE, ILLEGAL));
}
case ONE_OPERAND:
code &= 0x0f;
switch (code) {
caseline (0x00, "JZ", NUMBER, NIL, NIL, NIL, BRANCH, PLAIN);
caseline (0x01, "GET_SIBLING", OBJECT, NIL, NIL, NIL, BOTH, PLAIN);
caseline (0x02, "GET_CHILD", OBJECT, NIL, NIL, NIL, BOTH, PLAIN);
caseline (0x03, "GET_PARENT", OBJECT, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x04, "GET_PROP_LEN", LOW_ADDR, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x05, "INC", VAR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x06, "DEC", VAR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x07, "PRINT_ADDR", LOW_ADDR, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x09, "REMOVE_OBJ", OBJECT, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x0A, "PRINT_OBJ", OBJECT, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x0B, "RET", ANYTHING, NIL, NIL, NIL, NONE, RETURN);
caseline (0x0C, "JUMP", LABEL, NIL, NIL, NIL, NONE, RETURN);
caseline (0x0D, "PRINT_PADDR", STATIC, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x0E, "LOAD", VAR, NIL, NIL, NIL, STORE, PLAIN);
default:
switch ((unsigned int) header.version) {
case V1:
case V2:
case V3:
switch (code) {
caseline (0x0F, "NOT", NUMBER, NIL, NIL, NIL, STORE, PLAIN);
}
case V4:
switch (code) {
caseline (0x08, "CALL_1S", ROUTINE, NIL, NIL, NIL, STORE, CALL);
caseline (0x0F, "NOT", NUMBER, NIL, NIL, NIL, STORE, PLAIN);
}
case V5:
case V6:
case V7:
case V8:
switch (code) {
caseline (0x08, "CALL_1S", ROUTINE, NIL, NIL, NIL, STORE, CALL);
caseline (0x0F, "CALL_1N", ROUTINE, NIL, NIL, NIL, NONE, CALL);
}
}
return (decode_operands ("ILLEGAL", NIL, NIL, NIL, NIL, NONE, ILLEGAL));
}
case ZERO_OPERAND:
code &= 0x0f;
switch (code) {
caseline (0x00, "RTRUE", NIL, NIL, NIL, NIL, NONE, RETURN);
caseline (0x01, "RFALSE", NIL, NIL, NIL, NIL, NONE, RETURN);
caseline (0x02, "PRINT", NIL, NIL, NIL, NIL, TEXT, PLAIN);
caseline (0x03, "PRINT_RET", NIL, NIL, NIL, NIL, TEXT, RETURN);
caseline (0x04, "NOP", NIL, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x07, "RESTART", NIL, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x08, "RET_POPPED", NIL, NIL, NIL, NIL, NONE, RETURN);
caseline (0x0A, "QUIT", NIL, NIL, NIL, NIL, NONE, RETURN);
caseline (0x0B, "NEW_LINE", NIL, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x0D, "VERIFY", NIL, NIL, NIL, NIL, BRANCH, PLAIN);
default:
switch ((unsigned int) header.version) {
case V1:
case V2:
case V3:
switch (code) {
caseline (0x05, "SAVE", NIL, NIL, NIL, NIL, BRANCH, PLAIN);
caseline (0x06, "RESTORE", NIL, NIL, NIL, NIL, BRANCH, PLAIN);
caseline (0x09, "POP", NIL, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x0C, "SHOW_STATUS", NIL, NIL, NIL, NIL, NONE, PLAIN);
}
case V4:
switch (code) {
caseline (0x09, "POP", NIL, NIL, NIL, NIL, NONE, PLAIN);
caseline (0x05, "SAVE", NIL, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x06, "RESTORE", NIL, NIL, NIL, NIL, STORE, PLAIN);
}
case V5:
case V7:
case V8:
switch (code) {
caseline (0x09, "CATCH", NIL, NIL, NIL, NIL, STORE, PLAIN);
/* From a bug in Wishbringer V23 */
caseline (0x0C, "SHOW_STATUS", NIL, NIL, NIL, NIL, NONE, PLAIN);
}
case V6:
switch (code) {
caseline (0x09, "CATCH", NIL, NIL, NIL, NIL, STORE, PLAIN);
caseline (0x0F, "PIRACY", NIL, NIL, NIL, NIL, BRANCH, PLAIN);
}
}
return (decode_operands ("ILLEGAL", NIL, NIL, NIL, NIL, NONE, ILLEGAL));
}
default:
(void) fprintf (stderr, "\nFatal: bad class (%d)\n", (int) opcode.class);
exit (EXIT_FAILURE);
}
return (BAD_OPCODE);
}/* decode_opcode */
#undef caseline
/* decode_operands - Decode operands of opcode */
#ifdef __STDC__
static int decode_operands (const char *opcode_name, int par1, int par2, int par3, int par4, int extra, int type)
#else
static int decode_operands (opcode_name, par1, par2, par3, par4, extra, type)
const char *opcode_name;
int par1;
int par2;
int par3;
int par4;
int extra;
int type;
#endif
{
size_t len;
int i, opers, status;
opcode.par[0] = par1;
opcode.par[1] = par2;
opcode.par[2] = par3;
opcode.par[3] = par4;
opcode.extra = extra;
opcode.type = type;
if (opcode.type == ILLEGAL)
return (BAD_OPCODE);
if (decode.first_pass) {
status = decode_parameters (&opers);
if (status)
return (BAD_OPCODE);
status = decode_extra ();
} else {
if (option_dump)
dump_opcode (decode.pc, opcode.opcode, opcode.class, opcode.par, opcode.extra);
if (option_inform) {
len = strlen (opcode_name);
for (i = 0; i < len; i++)
tx_printf ("%c", tolower (opcode_name[i]));
} else {
tx_printf (opcode_name);
len = strlen (opcode_name);
}
for (; len < 16; len++)
tx_printf (" ");
(void) decode_parameters (&opers);
if (opers > 0 && opcode.extra != NONE)
tx_printf (" ");
status = decode_extra ();
tx_printf ("\n");
}
if (decode.pc > decode.high_pc)
decode.high_pc = decode.pc;
return (status);
}/* decode_operands */
/* decode_parameters - Decode input parameters */