-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc3sim.c
1826 lines (1584 loc) · 47.9 KB
/
lc3sim.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
/* tab:8
*
* lc3sim.c - the main source file for the LC-3 simulator
*
* "Copyright (c) 2003 by Steven S. Lumetta."
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written
* agreement is hereby granted, provided that the above copyright notice
* and the following two paragraphs appear in all copies of this software,
* that the files COPYING and NO_WARRANTY are included verbatim with
* any distribution, and that the contents of the file README are included
* verbatim as part of a file named README with any distribution.
*
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR
* HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
* BASIS, AND THE AUTHOR NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
* UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: Steve Lumetta
* Version: 1
* Creation Date: 18 October 2003
* Filename: lc3sim.c
* History:
* SSL 1 18 October 2003
* Copyright notices and Gnu Public License marker added.
*/
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <strings.h>
#include <sys/poll.h>
#include <sys/termios.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#if defined(USE_READLINE)
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "lc3sim.h"
#include "symbol.h"
/* Disassembly format specification. */
#define OPCODE_WIDTH 6
/* NOTE: hardcoded in scanfs! */
#define MAX_CMD_WORD_LEN 41 /* command word limit + 1 */
#define MAX_FILE_NAME_LEN 251 /* file name limit + 1 */
#define MAX_LABEL_LEN 81 /* label limit + 1 */
#define MAX_SCRIPT_DEPTH 10 /* prevent infinite recursion in scripts */
#define MAX_FINISH_DEPTH 10000000 /* avoid waiting to finish subroutine */
/* that recurses infinitely */
#define TOO_MANY_ARGS "WARNING: Ignoring excess arguments."
#define BAD_ADDRESS \
"Addresses must be labels or values in the range x0000 to xFFFF."
/*
Types of breakpoints. Currently only user breakpoints are
handled in this manner; the system breakpoint used for the
"next" command is specified by sys_bpt_addr.
*/
typedef enum bpt_type_t bpt_type_t;
enum bpt_type_t {BPT_NONE, BPT_USER};
static int launch_gui_connection ();
static char* simple_readline (const char* prompt);
static void init_machine ();
static void print_register (int which);
static void print_registers ();
static void dump_delayed_mem_updates ();
static void show_state_if_stop_visible ();
static int read_obj_file (const unsigned char* filename, int* startp,
int* endp);
static int read_sym_file (const unsigned char* filename);
static void squash_symbols (int addr_s, int addr_e);
static int execute_instruction ();
static void disassemble_one (int addr);
static void disassemble (int addr_s, int addr_e);
static void dump_memory (int addr_s, int addr_e);
static void run_until_stopped ();
static void clear_breakpoint (int addr);
static void clear_all_breakpoints ();
static void list_breakpoints ();
static void set_breakpoint (int addr);
static void warn_too_many_args ();
static void no_args_allowed (const unsigned char* args);
static int parse_address (const unsigned char* addr);
static int parse_range (const unsigned char* cmd, int* startptr, int* endptr,
int last_end, int scale);
static void flush_console_input ();
static void gui_stop_and_dump ();
static void cmd_break (const unsigned char* args);
static void cmd_continue (const unsigned char* args);
static void cmd_dump (const unsigned char* args);
static void cmd_execute (const unsigned char* args);
static void cmd_file (const unsigned char* args);
static void cmd_finish (const unsigned char* args);
static void cmd_help (const unsigned char* args);
static void cmd_list (const unsigned char* args);
static void cmd_memory (const unsigned char* args);
static void cmd_next (const unsigned char* args);
static void cmd_option (const unsigned char* args);
static void cmd_printregs (const unsigned char* args);
static void cmd_quit (const unsigned char* args);
static void cmd_register (const unsigned char* args);
static void cmd_reset (const unsigned char* args);
static void cmd_step (const unsigned char* args);
static void cmd_translate (const unsigned char* args);
static void cmd_lc3_stop (const unsigned char* args);
typedef enum cmd_flag_t cmd_flag_t;
enum cmd_flag_t {
CMD_FLAG_NONE = 0,
CMD_FLAG_REPEATABLE = 1, /* pressing ENTER repeats command */
CMD_FLAG_LIST_TYPE = 2, /* pressing ENTER shows more */
CMD_FLAG_GUI_ONLY = 4 /* only valid in GUI mode */
};
typedef struct command_t command_t;
struct command_t {
unsigned char* command; /* string for command */
int min_len; /* minimum length for abbrevation--typically 1 */
void (*cmd_func) (const unsigned char*);
/* function implementing command */
cmd_flag_t flags; /* flags for command properties */
};
static const struct command_t command[] = {
{"break", 1, cmd_break, CMD_FLAG_NONE },
{"continue", 1, cmd_continue, CMD_FLAG_REPEATABLE},
{"dump", 1, cmd_dump, CMD_FLAG_LIST_TYPE },
{"execute", 1, cmd_execute, CMD_FLAG_NONE },
{"file", 1, cmd_file, CMD_FLAG_NONE },
{"finish", 3, cmd_finish, CMD_FLAG_REPEATABLE},
{"help", 1, cmd_help, CMD_FLAG_NONE },
{"list", 1, cmd_list, CMD_FLAG_LIST_TYPE },
{"memory", 1, cmd_memory, CMD_FLAG_NONE },
{"next", 1, cmd_next, CMD_FLAG_REPEATABLE},
{"option", 1, cmd_option, CMD_FLAG_NONE },
{"printregs", 1, cmd_printregs, CMD_FLAG_NONE },
{"quit", 4, cmd_quit, CMD_FLAG_NONE },
{"register", 1, cmd_register, CMD_FLAG_NONE },
{"reset", 5, cmd_reset, CMD_FLAG_NONE },
{"step", 1, cmd_step, CMD_FLAG_REPEATABLE},
{"translate", 1, cmd_translate, CMD_FLAG_NONE },
{"x", 1, cmd_lc3_stop, CMD_FLAG_GUI_ONLY },
{NULL, 0, NULL, CMD_FLAG_NONE }
};
static int lc3_register[NUM_REGS];
#define REG(i) lc3_register[(i)]
static int lc3_memory[65536];
static int lc3_show_later[65536];
static bpt_type_t lc3_breakpoints[65536];
/* startup script or file */
static char* start_script = NULL;
static char* start_file = NULL;
static int should_halt = 1, last_KBSR_read = 0, last_DSR_read = 0, gui_mode;
static int interrupted_at_gui_request = 0, stop_scripts = 0, in_init = 0;
static int have_mem_to_dump = 0, need_a_stop_notice = 0;
static int sys_bpt_addr = -1, finish_depth = 0;
static inst_flag_t last_flags;
/* options and script recursion level */
static int flush_on_start = 1, keep_input_on_stop = 1;
static int rand_device = 1, delay_mem_update = 1;
static int script_uses_stdin = 1, script_depth = 0;
static FILE* lc3in;
static FILE* lc3out;
static FILE* sim_in;
static char* (*lc3readline) (const char*) = simple_readline;
static const char* const ccodes[8] = {
"BAD_CC", "POSITIVE", "ZERO", "BAD_CC",
"NEGATIVE", "BAD_CC", "BAD_CC", "BAD_CC"
};
static int
execute_instruction ()
{
/* Fetch the instruction. */
REG (R_IR) = read_memory (REG (R_PC));
REG (R_PC) = (REG (R_PC) + 1) & 0xFFFF;
/* Try to execute it. */
#define ADD_FLAGS(value) (last_flags |= (value))
#define DEF_INST(name,format,mask,match,flags,code) \
if ((REG (R_IR) & (mask)) == (match)) { \
last_flags = (flags); \
code; \
goto executed; \
}
#define DEF_P_OP(name,format,mask,match)
#include "lc3.def"
#undef DEF_P_OP
#undef DEF_INST
#undef ADD_FLAGS
REG (R_PC) = (REG (R_PC) - 1) & 0xFFFF;
if (gui_mode)
printf ("ERR {Illegal instruction at x%04X!}\n", REG (R_PC));
else
printf ("Illegal instruction at x%04X!\n", REG (R_PC));
return 0;
executed:
/* Check for user breakpoints. */
if (lc3_breakpoints[REG (R_PC)] == BPT_USER) {
if (!gui_mode)
printf ("The LC-3 hit a breakpoint...\n");
return 0;
}
/* Check for system breakpoint (associated with "next" command). */
if (REG (R_PC) == sys_bpt_addr)
return 0;
if (finish_depth > 0) {
if ((last_flags & FLG_SUBROUTINE) &&
++finish_depth == MAX_FINISH_DEPTH) {
if (gui_mode)
puts ("ERR {Stopping due to possibly infinite "
"recursion.}");
else
puts ("Stopping due to possibly infinite recursion.");
finish_depth = 0;
return 0;
} else if ((last_flags & FLG_RETURN) && --finish_depth == 0) {
/* Done with finish command; stop execution. */
return 0;
}
}
/* Check for GUI needs. */
if (!in_init && gui_mode) {
struct pollfd p;
p.fd = fileno (sim_in);
p.events = POLLIN;
if (poll (&p, 1, 0) == 1 && (p.revents & POLLIN) != 0) {
interrupted_at_gui_request = 1;
return 0;
}
}
return 1;
}
void
halt_lc3 (int sig)
{
/* Set the signal handler again, which has the effect of overriding
the Solaris behavior and making signal look like sigset, which
is non-standard and non-portable, but the desired behavior. */
signal (SIGINT, halt_lc3);
/* has no effect unless LC-3 is running... */
should_halt = 1;
/* print a stop notice after ^C */
need_a_stop_notice = 1;
}
static int
launch_gui_connection ()
{
u_short port;
int fd; /* server socket file descriptor */
struct sockaddr_in addr; /* server socket address */
/* wait for the GUI to tell us the portfor the LC-3 console socket */
if (fscanf (sim_in, "%hd", &port) != 1)
return -1;
/* don't buffer output to GUI */
if (setvbuf (stdout, NULL, _IONBF, 0) == -1)
return -1;
/* create a TCP socket */
if ((fd = socket (PF_INET, SOCK_STREAM, 0)) == -1)
return -1;
/* bind the port to the loopback address with any port */
bzero (&addr, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
addr.sin_port = 0;
if (bind (fd, (struct sockaddr*)&addr, sizeof (addr)) == -1) {
close (fd);
return -1;
}
/* now connect to the given port */
addr.sin_port = htons (port);
if (connect (fd, (struct sockaddr*)&addr,
sizeof (struct sockaddr_in)) == -1) {
close (fd);
return -1;
}
/* use it for LC-3 keyboard and display I/O */
if ((lc3in = fdopen (fd, "r")) == NULL ||
(lc3out = fdopen (fd, "w")) == NULL ||
setvbuf (lc3out, NULL, _IONBF, 0) == -1) {
close (fd);
return -1;
}
return 0;
}
static char*
simple_readline (const char* prompt)
{
char buf[200];
char* strip_nl;
struct pollfd p;
/* If we exhaust all commands after being interrupted by the
GUI, start running again... */
if (gui_mode) {
p.fd = fileno (sim_in);
p.events = POLLIN;
if ((poll (&p, 1, 0) != 1 || (p.revents & POLLIN) == 0) &&
interrupted_at_gui_request) {
/* flag is reset to 0 in cmd_continue */
return strdup ("c");
}
}
/* Prompt and read a line until successful. */
while (1) {
#if !defined(USE_READLINE)
if (!gui_mode && script_depth == 0)
printf ("%s", prompt);
#endif
/* read a line */
if (fgets (buf, 200, sim_in) != NULL)
break;
/* no more input? */
if (feof (sim_in))
return NULL;
/* Otherwise, probably a CTRL-C, so print a blank line and
(possibly) another prompt, then try again. */
puts ("");
}
/* strip carriage returns and linefeeds */
for (strip_nl = buf + strlen (buf) - 1;
strip_nl >= buf && (*strip_nl == '\n' || *strip_nl == '\r');
strip_nl--);
*++strip_nl = 0;
return strdup (buf);
}
static void
command_loop ()
{
int cword_len;
unsigned char* cmd = NULL;
unsigned char* start;
unsigned char* last_cmd = NULL;
unsigned char cword[MAX_CMD_WORD_LEN];
const command_t* a_command;
while (!stop_scripts && (cmd = lc3readline ("(lc3sim) ")) != NULL) {
/* Skip white space. */
for (start = cmd; isspace (*start); start++);
if (*start == '\0') {
/* An empty line repeats the last command, if allowed. */
free (cmd);
if ((cmd = last_cmd) == NULL)
continue;
/* Skip white space. */
for (start = cmd; isspace (*start); start++);
} else if (last_cmd != NULL)
free (last_cmd);
last_cmd = NULL;
/* Should never fail; just ignore the command if it does. */
/* 40 below == MAX_CMD_WORD_LEN - 1 */
if (sscanf (start, "%40s", cword) != 1) {
free (cmd);
break;
}
/* Record command word length, then point to arguments. */
cword_len = strlen (cword);
for (start += cword_len; isspace (*start); start++);
/* Match command word to list of commands. */
a_command = command;
while (1) {
if (a_command->command == NULL) {
/* No match found--complain! */
free (cmd);
printf ("Unknown command. Type 'h' for help.\n");
break;
}
/* Try to match a_command. */
if (strncasecmp (cword, a_command->command, cword_len) == 0 &&
cword_len >= a_command->min_len &&
(gui_mode || (a_command->flags & CMD_FLAG_GUI_ONLY) == 0)) {
/* Execute the command. */
(*a_command->cmd_func) (start);
/* Handle list type and repeatable commands. */
if (a_command->flags & CMD_FLAG_LIST_TYPE) {
unsigned char buf[MAX_CMD_WORD_LEN + 5];
strcpy (buf, cword);
strcat (buf, " more");
last_cmd = strdup (buf);
} else if (a_command->flags & CMD_FLAG_REPEATABLE &&
script_depth == 0) {
last_cmd = cmd;
} else {
free (cmd);
}
break;
}
a_command++;
}
}
}
int
main (int argc, char** argv)
{
/* check for -gui argument */
sim_in = stdin;
if (argc > 1 && strcmp (argv[1], "-gui") == 0) {
if (launch_gui_connection () != 0) {
printf ("failed to connect to GUI\n");
return 1;
}
/* skip the -gui argument in later parsing */
argc--;
argv++;
gui_mode = 1;
} else {
lc3out = stdout;
lc3in = stdin;
gui_mode = 0;
#if defined(USE_READLINE)
lc3readline = readline;
#endif
}
/* used to simulate random device timing behavior */
srandom (time (NULL));
/* used to halt LC-3 when CTRL-C pressed */
signal (SIGINT, halt_lc3);
/* load any object, symbol, or script files requested on command line */
if (argc == 3 && strcmp (argv[1], "-s") == 0) {
start_script = argv[2];
init_machine (); /* also executes script */
return 0;
} else if (argc == 2 && strcmp (argv[1], "-h") != 0) {
start_file = strdup (argv[1]);
init_machine (); /* also loads file */
} else if (argc != 1) {
/* argv[0] may not be valid if -gui entered */
printf ("syntax: lc3sim [<object file>|<symbol file>]\n");
printf (" lc3sim [-s <script file>]\n");
printf (" lc3sim -h\n");
return 0;
} else
init_machine ();
command_loop ();
puts ("");
return 0;
}
int
read_memory (int addr)
{
struct pollfd p;
switch (addr) {
case 0xFE00: /* KBSR */
if (!last_KBSR_read) {
p.fd = fileno (lc3in);
p.events = POLLIN;
if (poll (&p, 1, 0) == 1 && (p.revents & POLLIN) != 0)
last_KBSR_read = (!rand_device || (random () & 15) == 0);
}
return (last_KBSR_read ? 0x8000 : 0x0000);
case 0xFE02: /* KBDR */
if (last_KBSR_read && (lc3_memory[0xFE02] = fgetc (lc3in)) == -1) {
/* Should not happen in GUI mode. */
/* FIXME: This won't show up correctly in GUI.
Exit is likely to be detected first, and error message
given (LC-3 sim. died), followed by message below
(read past end), then Tcl/Tk error caused by bad
window access after sim died. Confusing sequence
if it occurs. */
if (gui_mode)
puts ("ERR {LC-3 read past end of input stream.}");
else
puts ("LC-3 read past end of input stream.");
exit (3);
}
last_KBSR_read = 0;
return lc3_memory[0xFE02];
case 0xFE04: /* DSR */
if (!last_DSR_read)
last_DSR_read = (!rand_device || (random () & 15) == 0);
return (last_DSR_read ? 0x8000 : 0x0000);
case 0xFE06: /* DDR */
return 0x0000;
case 0xFFFE: return 0x8000; /* MCR */
}
return lc3_memory[addr];
}
void
write_memory (int addr, int value)
{
switch (addr) {
case 0xFE00: /* KBSR */
case 0xFE02: /* KBDR */
case 0xFE04: /* DSR */
return;
case 0xFE06: /* DDR */
if (last_DSR_read == 0)
return;
fprintf (lc3out, "%c", value);
fflush (lc3out);
last_DSR_read = 0;
return;
case 0xFFFE:
if ((value & 0x8000) == 0)
should_halt = 1;
return;
}
/* No need to write/update GUI if the same value is already in memory. */
if (value != lc3_memory[addr]) {
lc3_memory[addr] = value;
if (gui_mode) {
if (!delay_mem_update)
disassemble_one (addr);
else {
lc3_show_later[addr] = 1;
have_mem_to_dump = 1; /* a hint */
}
}
}
}
static int
read_obj_file (const unsigned char* filename, int* startp, int* endp)
{
FILE* f;
int start, addr;
unsigned char buf[2];
if ((f = fopen (filename, "r")) == NULL)
return -1;
if (fread (buf, 2, 1, f) != 1) {
fclose (f);
return -1;
}
addr = start = (buf[0] << 8) | buf[1];
while (fread (buf, 2, 1, f) == 1) {
write_memory (addr, (buf[0] << 8) | buf[1]);
addr = (addr + 1) & 0xFFFF;
}
fclose (f);
squash_symbols (start, addr);
*startp = start;
*endp = addr;
return 0;
}
static int
read_sym_file (const unsigned char* filename)
{
FILE* f;
int adding = 0;
unsigned char buf[100];
unsigned char sym[81];
int addr;
if ((f = fopen (filename, "r")) == NULL)
return -1;
while (fgets (buf, 100, f) != NULL) {
if (!adding) {
if (sscanf (buf, "%*s%*s%80s", sym) == 1 &&
strcmp (sym, "------------") == 0)
adding = 1;
continue;
}
if (sscanf (buf, "%*s%80s%x", sym, &addr) != 2)
break;
add_symbol (sym, addr, 1);
}
fclose (f);
return 0;
}
static void
squash_symbols (int addr_s, int addr_e)
{
while (addr_s != addr_e) {
remove_symbol_at_addr (addr_s);
addr_s = (addr_s + 1) & 0xFFFF;
}
}
static void
init_machine ()
{
int os_start, os_end;
in_init = 1;
bzero (lc3_register, sizeof (lc3_register));
REG (R_PSR) = (2L << 9); /* set to condition ZERO */
bzero (lc3_memory, sizeof (lc3_memory));
bzero (lc3_show_later, sizeof (lc3_show_later));
bzero (lc3_sym_names, sizeof (lc3_sym_names));
bzero (lc3_sym_hash, sizeof (lc3_sym_hash));
clear_all_breakpoints ();
if (read_obj_file (INSTALL_DIR "/lc3os.obj", &os_start, &os_end) == -1) {
if (gui_mode)
puts ("ERR {Failed to read LC-3 OS code.}");
else
puts ("Failed to read LC-3 OS code.");
show_state_if_stop_visible ();
} else {
if (read_sym_file (INSTALL_DIR "/lc3os.sym") == -1) {
if (gui_mode)
puts ("ERR {Failed to read LC-3 OS symbols.}");
else
puts ("Failed to read LC-3 OS symbols.");
}
if (gui_mode) /* load new code into GUI display */
disassemble (os_start, os_end);
REG (R_PC) = 0x0200;
run_until_stopped ();
}
in_init = 0;
if (start_script != NULL)
cmd_execute (start_script);
else if (start_file != NULL)
cmd_file (start_file);
}
/* only called in GUI mode */
static void
print_register (int which)
{
printf ("REG R%d x%04X\n", which, REG (which));
/* condition codes are not stored outside of PSR */
if (which == R_PSR)
printf ("REG R%d %s\n", NUM_REGS, ccodes[(REG (R_PSR) >> 9) & 7]);
/* change focus in GUI */
printf ("TOCODE\n");
}
static void
print_registers ()
{
int regnum;
if (!gui_mode) {
printf ("PC=x%04X IR=x%04X PSR=x%04X (%s)\n", REG (R_PC), REG (R_IR),
REG (R_PSR), ccodes[(REG (R_PSR) >> 9) & 7]);
for (regnum = 0; regnum < R_PC; regnum++)
printf ("R%d=x%04X ", regnum, REG (regnum));
puts ("");
disassemble_one (REG (R_PC));
} else {
for (regnum = 0; regnum < NUM_REGS; regnum++)
printf ("REG R%d x%04X\n", regnum, REG (regnum));
/* regnum is now NUM_REGS */
printf ("REG R%d %s\n", regnum, ccodes[(REG (R_PSR) >> 9) & 7]);
}
}
static void
dump_delayed_mem_updates ()
{
int addr;
if (!have_mem_to_dump)
return;
have_mem_to_dump = 0;
/* FIXME: Could use a hash table here, but hint is probably enough. */
for (addr = 0; addr < 65536; addr++) {
if (lc3_show_later[addr]) {
disassemble_one (addr);
lc3_show_later[addr] = 0;
}
}
}
static void
show_state_if_stop_visible ()
{
/*
If the GUI has interrupted the simulator (e.g., to set or clear
a breakpoint), print nothing. The simulator restarts automatically
unless a new file is loaded, in which case cmd_file performs the
updates.
*/
if (interrupted_at_gui_request)
return;
if (gui_mode && delay_mem_update)
dump_delayed_mem_updates ();
print_registers ();
}
static void
print_operands (int addr, int inst, format_t fmt)
{
int found = 0, tgt;
if (fmt & FMT_R1) {
printf ("%sR%d", (found ? "," : ""), F_DR (inst));
found = 1;
}
if (fmt & FMT_R2) {
printf ("%sR%d", (found ? "," : ""), F_SR1 (inst));
found = 1;
}
if (fmt & FMT_R3) {
printf ("%sR%d", (found ? "," : ""), F_SR2 (inst));
found = 1;
}
if (fmt & FMT_IMM5) {
printf ("%s#%d", (found ? "," : ""), F_imm5 (inst));
found = 1;
}
if (fmt & FMT_IMM6) {
printf ("%s#%d", (found ? "," : ""), F_imm6 (inst));
found = 1;
}
if (fmt & FMT_VEC8) {
printf ("%sx%02X", (found ? "," : ""), F_vec8 (inst));
found = 1;
}
if (fmt & FMT_ASC8) {
printf ("%s", (found ? "," : ""));
found = 1;
switch (F_vec8 (inst)) {
case 7: printf ("'\\a'"); break;
case 8: printf ("'\\b'"); break;
case 9: printf ("'\\t'"); break;
case 10: printf ("'\\n'"); break;
case 11: printf ("'\\v'"); break;
case 12: printf ("'\\f'"); break;
case 13: printf ("'\\r'"); break;
case 27: printf ("'\\e'"); break;
case 34: printf ("'\\\"'"); break;
case 44: printf ("'\\''"); break;
case 92: printf ("'\\\\'"); break;
default:
if (isprint (F_vec8 (inst)))
printf ("'%c'", F_vec8 (inst));
else
printf ("x%02X", F_vec8 (inst));
break;
}
}
if (fmt & FMT_IMM9) {
printf ("%s", (found ? "," : ""));
found = 1;
tgt = (addr + 1 + F_imm9 (inst)) & 0xFFFF;
if (lc3_sym_names[tgt] != NULL)
printf ("%s", lc3_sym_names[tgt]->name);
else
printf ("x%04X", tgt);
}
if (fmt & FMT_IMM11) {
printf ("%s", (found ? "," : ""));
found = 1;
tgt = (addr + 1 + F_imm11 (inst)) & 0xFFFF;
if (lc3_sym_names[tgt] != NULL)
printf ("%s", lc3_sym_names[tgt]->name);
else
printf ("x%04X", tgt);
}
if (fmt & FMT_IMM16) {
printf ("%s", (found ? "," : ""));
found = 1;
if (lc3_sym_names[inst] != NULL)
printf ("%s", lc3_sym_names[inst]->name);
else
printf ("x%04X", inst);
}
}
static void
disassemble_one (int addr)
{
static const char* const dis_cc[8] = {
"", "P", "Z", "ZP", "N", "NP", "NZ", "NZP"
};
int inst = read_memory (addr);
/* GUI prefix */
if (gui_mode)
printf ("CODE%c%5d",
(!in_init && addr == lc3_register[R_PC] ? 'P' : ' '),
addr + 1);
/* Try to find a label. */
if (lc3_sym_names[addr] != NULL)
printf ("%c %16.16s x%04X x%04X ",
(lc3_breakpoints[addr] == BPT_USER ? 'B' : ' '),
lc3_sym_names[addr]->name, addr, inst);
else
printf ("%c %17sx%04X x%04X ",
(lc3_breakpoints[addr] == BPT_USER ? 'B' : ' '),
"", addr, inst);
/* Try to disassemble it. */
#define DEF_INST(name,format,mask,match,flags,code) \
if ((inst & (mask)) == (match)) { \
if ((format) & FMT_CC) \
printf ("%s%-*s", #name, (int)(OPCODE_WIDTH - strlen (#name)), \
dis_cc[F_CC (inst) >> 9]); \
else \
printf ("%-*s", OPCODE_WIDTH, #name); \
print_operands (addr, inst, (format)); \
goto printed; \
}
#define DEF_P_OP(name,format,mask,match) \
DEF_INST(name,format,mask,match,FLG_NONE,{})
#include "lc3.def"
#undef DEF_P_OP
#undef DEF_INST
printf ("%-*s", OPCODE_WIDTH, "???");
printed:
puts ("");
}
static void
disassemble (int addr_s, int addr_e)
{
do {
disassemble_one (addr_s);
addr_s = (addr_s + 1) & 0xFFFF;
} while (addr_s != addr_e);
}
static void
dump_memory (int addr_s, int addr_e)
{
int start, addr, i;
int a[12];
if (addr_s >= addr_e)
addr_e += 0x10000;
for (start = (addr_s / 12) * 12; start < addr_e; start = start + 12) {
printf ("%04X: ", start & 0xFFFF);
for (i = 0, addr = start; i < 12; i++, addr++) {
if (addr >= addr_s && addr < addr_e)
printf ("%04X ", (a[i] = read_memory (addr & 0xFFFF)));
else
printf (" ");
}
printf (" ");
for (i = 0, addr = start; i < 12; i++, addr++) {
if (addr >= addr_s && addr < addr_e)
printf ("%c", (a[i] < 0x100 && isprint (a[i])) ? a[i] : '.');
else
printf (" ");
}
puts ("");
}
}
static void
run_until_stopped ()
{
struct termios tio;
int old_lflag, old_min, old_time, tty_fail;
should_halt = 0;
if (gui_mode) {
/* removes PC marker in GUI */
printf ("CONT\n");
tty_fail = 1;
} else if (!isatty (fileno (lc3in)) ||
tcgetattr (fileno (lc3in), &tio) != 0)
tty_fail = 1;
else {
tty_fail = 0;
old_lflag = tio.c_lflag;
old_min = tio.c_cc[VMIN];
old_time = tio.c_cc[VTIME];
tio.c_lflag &= ~(ICANON | ECHO);
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
(void)tcsetattr (fileno (lc3in), TCSANOW, &tio);
}
while (!should_halt && execute_instruction ());
if (!tty_fail) {
tio.c_lflag = old_lflag;
tio.c_cc[VMIN] = old_min;
tio.c_cc[VTIME] = old_time;
(void)tcsetattr (fileno (lc3in), TCSANOW, &tio);
/*
Discard any remaining input if requested. This flush occurs
when the LC-3 stops, in which case any remaining input
to the console will be treated as simulator commands if it
is not discarded.
However, discarding can interfere with command sequences that
include moderately long execution periods.
As with gdb, not discarding is the default, since typing in
a bunch of random junk that happens to look like valid
commands happens less frequently than the case above, although
I myself have been bitten a few times in gdb by pressing
return once too often after issuing a repeatable command.
*/
if (!keep_input_on_stop)
(void)tcflush (fileno (lc3in), TCIFLUSH);
}
/* stopped by CTRL-C? Check if we need a stop notice... */
if (need_a_stop_notice) {
printf ("\nLC-3 stopped.\n\n");
need_a_stop_notice = 0;
}
/*
If stopped for any reason other than interruption by GUI,
clear system breakpoint and terminate any "finish" command.
*/
if (!interrupted_at_gui_request) {
sys_bpt_addr = -1;
finish_depth = 0;
}
/* Dump memory and registers if necessary. */
show_state_if_stop_visible ();
}