-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmon.c
813 lines (757 loc) · 23.8 KB
/
mon.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
/* EXORcister simulator
* Copyright
* (C) 2011 Joseph H. Allen
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 1, or (at your option) any later version.
*
* It is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this software; see the file COPYING. If not, write to the Free Software Foundation,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <signal.h>
#include <unistd.h>
#include "utils.h"
#include "unasm6800.h"
#include "asm6800.h"
#include "sim6800.h"
#include "exor.h"
#include "exorterm.h"
FILE *mon_out;
FILE *mon_in;
int last;
unsigned short last_u;
int step;
struct cmd { char *name; int (*func)(char *); char *help; } cmds[];
void huh()
{
printf("Huh?\n");
}
extern FILE *lpt_file;
int quit_cmd(char *p)
{
printf("\nBye\n");
restore_termios();
if (lpt_file)
fclose(lpt_file);
exit(-1);
return 0;
}
int help_cmd(char *p)
{
int x;
for (x = 0; cmds[x].name; ++x)
fprintf(mon_out, "%s%s\n", cmds[x].name, cmds[x].help);
printf("Append >file to redirect output to file\n");
printf("Append >>file to append output to file\n");
printf("Append <file to redirect input from file\n");
return 0;
}
int caps_cmd(char *p)
{
if (match_word(&p, "on"))
lower = 0;
else if (match_word(&p, "off"))
lower = 1;
else
huh();
return 0;
}
int m_cmd(char *p)
{
int a;
if (parse_hex(&p, &a)) {
unsigned short addr;
if (a < 0 || a > 65535) {
printf("Invalid address\n");
return 0;
}
addr = a;
for (;;) {
char c;
unsigned char d;
printf("%4.4x %2.2x ", addr, mem[addr]);
fflush(stdout);
again:
d = 0;
if (read(fileno(stdin), &c, 1) < 0) {
putchar('\n');
break;
}
if (c == 'C' - '@') {
putchar('\n');
break;
} else if (c >= '0' && c <= '9') {
putchar(c);
fflush(stdout);
d = ((c - '0') << 4);
} else if (c >= 'a' && c <= 'f') {
putchar(c);
fflush(stdout);
d = ((c - 'a' + 10) << 4);
} else if (c >= 'A' && c <= 'F') {
putchar(c);
fflush(stdout);
d = ((c - 'A' + 10) << 4);
} else if (c == 8 || c == 127) {
putchar('\n');
--addr;
continue;
} else {
putchar('\n');
++addr;
continue;
}
if (read(fileno(stdin), &c, 1) < 0) {
putchar('\n');
break;
}
if (c == 'C' - '@') {
putchar(8);
putchar(32);
putchar('\n');
break;
} else if (c >= '0' && c <= '9') {
putchar(c);
fflush(stdout);
d += (c - '0');
} else if (c >= 'a' && c <= 'f') {
putchar(c);
fflush(stdout);
d += (c - 'a' + 10);
} else if (c >= 'A' && c <= 'F') {
putchar(c);
fflush(stdout);
d += (c - 'A' + 10);
} else if (c == 8 || c == 127) {
putchar(8);
putchar(32);
putchar(8);
fflush(stdout);
goto again;
} else {
putchar(8);
putchar(32);
putchar('\n');
++addr;
continue;
}
mem[addr++] = d;
putchar('\n');
}
} else
huh();
return 0;
}
int regs_cmd(char *p)
{
int val;
if (!*p) {
#ifdef M6809
fprintf(mon_out, "PC=%4.4X A=%2.2X B=%2.2X X=%4.4X Y=%4.4X U=%4.4X SP=%2.2X DP=%2.2X CC=%2.2X\n", pc, acca, accb, ix, iy, up, sp, dp, read_flags());
#endif
#ifdef M6800
fprintf(mon_out, "PC=%4.4X A=%2.2X B=%2.2X X=%4.4X SP=%2.2X CC=%2.2X\n", pc, acca, accb, ix, sp, read_flags());
#endif
} else if (match_word(&p, "pc") && parse_hex(&p, &val)) {
pc = val;
} else if (match_word(&p, "sp") && parse_hex(&p, &val)) {
sp = val;
} else if (match_word(&p, "x") && parse_hex(&p, &val)) {
ix = val;
#ifdef M6809
} else if (match_word(&p, "y") && parse_hex(&p, &val)) {
iy = val;
} else if (match_word(&p, "u") && parse_hex(&p, &val)) {
up = val;
} else if (match_word(&p, "dp") && parse_hex(&p, &val)) {
dp = val;
#endif
} else if (match_word(&p, "a") && parse_hex(&p, &val)) {
acca = val;
} else if (match_word(&p, "b") && parse_hex(&p, &val)) {
accb = val;
} else if (match_word(&p, "cc") && parse_hex(&p, &val)) {
write_flags(val);
} else
huh();
return 0;
}
int call_cmd(char *p)
{
int val;
if (parse_hex(&p, &val)) {
pc = val;
sp_stop = sp;
printf("Continuing until return when SP=%4.4x\n", sp);
stop = 0;
} else
huh();
return 1;
}
int c_cmd(char *p)
{
int val;
if (!*p) {
stop = 0;
return 1;
} else if (parse_hex(&p, &val)) {
stop = 0;
pc = val;
return 1;
} else
huh();
return 0;
}
int s_cmd(char *p)
{
int val;
if (!*p) {
stop = 1;
step = 1;
return 1;
} else if (parse_hex(&p, &val)) {
stop = 1;
step = 1;
pc = val;
return 1;
} else
huh();
return 0;
}
int clr_cmd(char *p)
{
clr_syms();
return 0;
}
int sy_cmd(char *p)
{
show_syms(mon_out);
#ifdef M6809
printf("setdp value is %2.2x\n", pseudo_dp);
#endif
return 0;
}
int b_cmd(char *p)
{
int val;
if (!*p) {
mybrk = 0;
printf("Breakpoint cleared\n");
} else if (parse_hex(&p, &val)) {
brk_addr = val;
mybrk = 1;
printf("Breakpoint set at %4.4X\n", brk_addr);
} else
huh();
return 0;
}
int d_cmd(char *p)
{
int val;
int len;
if (!*p) {
hd(mon_out, mem, last, 0x80);
last += 0x80;
} else if (parse_hex(&p, &val) && skipws(&p)) {
len = 0x80;
parse_hex(&p, &len);
hd(mon_out, mem, last = val, len);
last += len;
} else
huh();
return 0;
}
int p_cmd(char *p)
{
int start;
int len;
int addr = 0;
if (parse_hex(&p, &start) && skipws(&p) && parse_hex(&p, &len)) {
int l;
int cksum;
while (len) {
int x;
cksum = 0;
if (len >= 16)
l = 16;
else
l = len;
cksum = l + 3;
cksum += (start >> 8);
cksum += (start & 0xFF);
fprintf(mon_out, "S1%2.2X%4.4X", l + 3, start);
for (x = 0; x != l; ++x) {
fprintf(mon_out,"%2.2X", mem[start + x]);
cksum += mem[start + x];
}
fprintf(mon_out, "%2.2X\n", (~cksum & 0xFF));
len -= l;
start += l;
}
/* End mark, starting address */
skipws(&p);
parse_hex(&p, &addr);
l = 0;
cksum = l + 3;
cksum += (addr >> 8);
cksum += (addr & 0xFF);
fprintf(mon_out, "S9%2.2X%4.4X%2.2X\n", l + 3, addr, (~cksum & 0xFF));
}
return 0;
}
int l_cmd(char *p)
{
char name[100];
char buf[180];
FILE *f = 0;
int err = 0;
int count = 0;
int line = 0;
if (parse_word(&p, name))
{
f = fopen(name, "r");
if (!f) {
printf("Couldn't open %s\n", name);
return 0;
}
}
while (f ? !!fgets(buf, sizeof(buf), f) : !jgetline(mon_in, buf)) {
++line;
if (buf[0] == 'S' && buf[1] == '1') {
int l;
int addr;
int x;
int data;
int cksum;
int chk;
p = buf + 2;
parse_hex2(&p, &l);
chk = l;
parse_hex4(&p, &addr);
chk += (addr >> 8);
chk += (addr & 0xFF);
for (x = 0; x < l - 3; ++x) {
parse_hex2(&p, &data);
chk += data;
mem[addr + x] = data;
++count;
}
parse_hex2(&p, &cksum);
if (cksum != (~chk & 0xFF)) {
printf("Checksum mismatch on line %d\n", line);
err = 1;
}
} else if (buf[0] == 'S' && buf[1] == '9') {
int l;
int addr;
int cksum;
int chk;
p = buf + 2;
parse_hex2(&p, &l);
chk = l;
parse_hex4(&p, &addr);
chk += (addr >> 8);
chk += (addr & 0xFF);
parse_hex2(&p, &cksum);
if (cksum != (~chk & 0xFF)) {
printf("Checksum mismatch on line %d\n", line);
err = 1;
}
pc = addr;
printf("PC set to %4.4x\n", addr);
break;
} else {
printf("Unknown record on line %d\n", line);
break;
}
}
if (f)
fclose(f);
printf("%d bytes loaded\n", count);
if (err) {
printf("There were checksum errors during the load.\n");
} else {
printf("No checksum errors.\n");
}
return 0;
}
int a_cmd(char *p)
{
int addr;
char buf[180];
if (parse_hex(&p, &addr)) {
for (;;) {
fprintf(mon_out, "%4.4x: ", addr);
if (!jgetline(mon_in, buf)) {
if (mon_in != stdin) {
fprintf(mon_out, "%s\n", buf);
}
if (buf[0]) {
addr = assemble(mem, addr, buf);
} else {
break;
}
} else {
fprintf(mon_out, "\n");
break;
}
}
} else
huh();
return 0;
}
int u_cmd(char *p)
{
char buf[180];
int iaddr = last_u;
if (parse_hex(&p, &iaddr) || !*p) {
int target;
int x;
unsigned short addr = (unsigned short)iaddr;
for (x = 0; x != 22; ++x) {
unasm_line(mem, &addr, buf, &target, 1);
fprintf(mon_out, "%s\n", buf);
}
last_u = addr;
} else
huh();
return 0;
}
int abort_cmd(char *p)
{
abrt = 1;
stop = 0;
return 1;
}
int reset_cmd(char *p)
{
reset = 1;
stop = 0;
return 1;
}
int dump_cmd(char *p)
{
char name[180];
int start = 0;
int size = 65536;
int fd;
strcpy(name, "dump");
if (parse_word(&p, name) && skipws(&p))
{
if (parse_hex(&p, &start) && skipws(&p))
{
parse_hex(&p, &size);
}
}
if (!size || start + size > 65536) {
printf("start + size > 64K or size == 0\n");
return 0;
}
fd = creat(name, 0666);
if (fd != -1) {
write(fd, mem + start, size);
close(fd);
printf("Wrote %d bytes starting at %d to file '%s'\n", size, start, name);
} else {
printf("Couldn't open file '%s'\n", name);
}
return 0;
}
int read_cmd(char *p)
{
char name[180];
int start = 0;
int size;
FILE *f;
strcpy(name, "dump");
if (parse_word(&p, name) && skipws(&p))
parse_hex(&p, &start);
f = fopen(name, "r");
if (!f) {
printf("Couldn't open '%s'\n", name);
return 0;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
if (!size || start + size > 65536) {
printf("start + size > 64K or size == 0\n");
fclose(f);
return 0;
}
rewind(f);
fread(mem + start, size, 1, f);
fclose(f);
printf("Loaded %d bytes starting at %d\n", size, start);
return 0;
}
int lpt_cmd(char *p)
{
char name[180];
if (lpt_file)
{
fclose(lpt_file);
lpt_file = 0;
}
if (parse_word(&p, name))
{
lpt_file = fopen(name, "w");
if (lpt_file)
fprintf(stderr, "Line printer file %s opened\n", name);
else
fprintf(stderr, "Couldn't open %s\n", name);
}
return 0;
}
int drive0_cmd(char *p)
{
char name[180];
if (parse_word(&p, name))
{
close_drive(0);
set_drive(0, strdup(name));
load_drive(0);
}
else
{
show_drive(0);
}
return 0;
}
int drive1_cmd(char *p)
{
char name[180];
if (parse_word(&p, name))
{
close_drive(1);
set_drive(1, strdup(name));
load_drive(1);
}
else
{
show_drive(1);
}
return 0;
}
int drive2_cmd(char *p)
{
char name[180];
if (parse_word(&p, name))
{
close_drive(2);
set_drive(2, strdup(name));
load_drive(2);
}
else
{
show_drive(2);
}
return 0;
}
int drive3_cmd(char *p)
{
char name[180];
if (parse_word(&p, name))
{
close_drive(3);
set_drive(3, strdup(name));
load_drive(3);
}
else
{
show_drive(3);
}
return 0;
}
int t_cmd(char *p)
{
if (match_word(&p, "on"))
trace = 1;
else if (match_word(&p, "off"))
trace = 0;
else
huh();
return 0;
}
int bt_cmd(char *p)
{
show_traces(128);
return 0;
}
int poll_cmd(char *p)
{
if (match_word(&p, "on"))
polling = 1;
else if (match_word(&p, "off"))
polling = 0;
else
huh();
return 0;
}
int cd_cmd(char *p)
{
if (!*p)
p = getenv("HOME");
if (chdir(p))
printf("Error changing directory\n");
else
system("/bin/pwd");
return 0;
}
int pwd_cmd(char *p)
{
system("/bin/pwd");
return 0;
}
int ls_cmd(char *p)
{
char buf[180];
sprintf(buf, "/bin/ls %s", p);
system(buf);
return 0;
}
/* Command table */
struct cmd cmds[]=
{
{ "help", help_cmd, " Show this help text" },
{ "h", help_cmd, " Show this help text" },
{ "q", quit_cmd, " Exit simulator" },
{ "quit", quit_cmd, " Exit simulator" },
{ "t", t_cmd, " [on|off] Turn tracing on / off" },
{ "bt", bt_cmd, " Show most recent 128 instructions" },
{ "poll", poll_cmd, " [on|off] Turn ACIA polling on / off" },
{ "c", c_cmd, " [hhhh] Continue simulating [jump to address]" },
{ "s", s_cmd, " [hhhh] Step one instruction [jump to address]" },
{ "b", b_cmd, " [hhhh] Set/Clear breakpoint" },
{ "r", regs_cmd, " [reg hhhh] Show regs, set reg" },
{ "x", call_cmd, " hhhh Call subroutine, return to monitor when done" },
{ "reset", reset_cmd, " Hit reset button" },
{ "abort", abort_cmd, " Hit abort button" },
{ "caps", caps_cmd, " [on|off] Force uppercase on / off" },
{ "d", d_cmd, " hhhh [nnnn] Hex dump" },
{ "m", m_cmd, " hhhh Modify" },
{ "a", a_cmd, " hhhh Assemble" },
{ "clr", clr_cmd, " Clear symbol table" },
{ "sy", sy_cmd, " Show symbol table" },
{ "u", u_cmd, " hhhh Unassemble" },
{ "p", p_cmd, " hhhh nnnn [ssss] Punch nn bytes starting at hh in S19 format (ss is PC in S9)" },
{ "l", l_cmd, " [file] Load S19 from file or stdin" },
{ "save", dump_cmd, " [file [hhhh [nnnn]]] Save nn bytes of memory starting at hh to file in binary\n" },
{ "read", read_cmd, " [file [hhhh]] Read binary file into memory starting at hh\n" },
{ "lpt", lpt_cmd, " [file] Open line printer file\n" },
{ "drive0", drive0_cmd, " [file] Change disk in drive 0\n" },
{ "drive1", drive1_cmd, " [file] Change disk in drive 1\n" },
{ "drive2", drive2_cmd, " [file] Change disk in drive 2\n" },
{ "drive3", drive3_cmd, " [file] Change disk in drive 3\n" },
{ "cd", cd_cmd, " path Change directory\n" },
{ "pwd", pwd_cmd, " Print current directory\n" },
{ "ls", ls_cmd, " [ls-args] Print directory\n" },
{ 0, 0, 0 }
};
void monitor()
{
/* Flush any remaining terminal output */
update();
if (mon_out != stdout) {
fclose(mon_out);
mon_out = stdout;
}
if (mon_in != stdin) {
fclose(mon_in);
mon_in = stdin;
}
/* system("stty -isig"); */
nosig_termios();
if (step) {
if (trace)
show_traces(1);
else
show_traces(2);
step = 0;
} else {
if (trace)
show_traces(1);
else
show_traces(2);
}
#ifdef M6809
printf("\n6809 Monitor: Ctrl-C to exit, 'c' to continue, or type 'help'\n");
#else
printf("\n6800 Monitor: Ctrl-C to exit, 'c' to continue, or type 'help'\n");
#endif
for (;;) {
char buf[180];
char word[180];
char *p = buf;
char *q;
fputs("% ", stdout);
if (jgetline(stdin, buf)) {
quit_cmd(p);
}
if ((p = strstr(buf, " >>")) && parse_word(((q = p + 3), &q), word)) {
/* Append output */
jstrcpy(p, q);
mon_out = fopen(word, "a");
if (!mon_out) {
printf("Couldn't open '%s' for append\n", word);
continue;
}
} else if ((p = strstr(buf, " >")) && parse_word(((q = p + 2), &q), word)) {
/* Redirect output */
jstrcpy(p, q);
mon_out = fopen(word, "w");
if (!mon_out) {
printf("Couldn't open '%s' for write\n", word);
continue;
}
}
if ((p = strstr(buf, " <")) && parse_word(((q = p + 2), &q), word)) {
/* Redirect input */
jstrcpy(p, q);
mon_in = fopen(word, "r");
if (!mon_in) {
printf("Couldn't open '%s' for reading\n", word);
if (mon_out != stdout) {
fclose(mon_out);
mon_out = stdout;
}
continue;
}
}
p = buf;
if (skipws(&p) && *p) {
int x;
for (x = 0; cmds[x].name; ++x)
if (match_word(&p, cmds[x].name))
break;
if (cmds[x].name) {
skipws(&p);
if (cmds[x].func(p))
break;
} else
printf("Huh?\n");
}
if (mon_out != stdout) {
fclose(mon_out);
mon_out = stdout;
}
if (mon_in != stdin) {
fclose(mon_in);
mon_in = stdin;
}
}
/* system("stty isig"); */
sig_termios();
izexorterm();
}