-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmm.c
768 lines (720 loc) · 21.3 KB
/
mmm.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
#include <ctype.h>
#include <stdlib.h>
#include "emulator.h"
#include "assembler.h"
typedef struct {
mix mix;
parsestate ps;
char prevline[LINELEN];
char globalcardfile[LINELEN];
char globaltapefiles[8][LINELEN];
char debuglines[4000][LINELEN];
bool shouldtrace;
} mmmstate;
#define RED(s) "\033[31m" s "\033[37m"
#define GREEN(s) "\033[32m" s "\033[37m"
#define YELLOW(s) "\033[33m" s "\033[37m"
#define BLUE(s) "\033[34m" s "\033[37m"
#define CYAN(s) "\033[36m" s "\033[37m"
// Similar to printf, the following display functions return the
// *visual width* of the printed string.
// Thus a string like "\t" has width 8.
int displayshort(word w) {
bool sign = SIGN(w);
byte b4 = (w >> 6) & ONES(6);
byte b5 = w & ONES(6);
printf("%c %02d %02d", sign ? '+' : '-', b4, b5);
return 1+2*3;
}
int displayword(word w) {
bool sign = SIGN(w);
byte b1 = (w >> 24) & ONES(6);
byte b2 = (w >> 18) & ONES(6);
byte b3 = (w >> 12) & ONES(6);
byte b4 = (w >> 6) & ONES(6);
byte b5 = w & ONES(6);
printf("%c %02d %02d %02d %02d %02d", sign ? '+' : '-', b1, b2, b3, b4, b5);
return 1+5*3;
}
// Display instruction in a format similar to displayword, but with
// first two bytes combined
int displayinstr_raw(word w) {
bool sign = (getA(w) >> 12) & 1;
word A_abs = getA(w) & ONES(12);
byte I = getI(w);
byte F = getF(w);
byte C = getC(w);
printf("%c %04d %02d %02d %02d", sign ? '+' : '-', A_abs, I, F, C);
return 1+5+3*3;
}
// Display the A,I(F) portion of the canonical representation of an
// instruction
int _displayinstr_fields(bool sign, word A_abs, byte I, byte F, byte default_F) {
int numchars = 0;
if (!sign) {
numchars++;
putchar('-');
}
numchars += printf("%d", A_abs);
if (I != 0)
numchars += printf(",%d", I);
if (F != default_F)
numchars += printf("(%d)", F);
return numchars;
}
// Display the canonical representation of an instruction, or ??? if
// invalid instruction.
int displayinstr_canonical(word w) {
bool sign = (getA(w) >> 12) & 1;
word A_abs = getA(w) & ONES(12);
byte I = getI(w);
byte F = getF(w);
byte C = getC(w);
#define PP(s,DEFAULTF) { printf(s "\t"); return 8 + _displayinstr_fields(sign, A_abs, I, F, DEFAULTF); }
#define UNKNOWN() { printf("???"); return 3; }
if (C == 0) PP("NOP", 0)
else if (C == 1) PP("ADD", 5)
else if (C == 2) PP("SUB", 5)
else if (C == 3) PP("MUL", 5)
else if (C == 4) PP("DIV", 5)
else if (C == 5) {
if (F == 0) PP("MUL", 0)
else if (F == 1) PP("CHAR", 1)
else if (F == 2) PP("HLT", 2)
else UNKNOWN()
}
else if (C == 6) {
if (F == 0) PP("SLA", 0)
else if (F == 1) PP("SRA", 1)
else if (F == 2) PP("SLAX", 2)
else if (F == 3) PP("SRAX", 3)
else if (F == 4) PP("SLC", 4)
else if (F == 5) PP("SRC", 5)
else UNKNOWN()
}
else if (C == 7) PP("MOVE", 1)
else if (C == 8) PP("LDA", 5)
else if (C == 9) PP("LD1", 5)
else if (C == 10) PP("LD2", 5)
else if (C == 11) PP("LD3", 5)
else if (C == 12) PP("LD4", 5)
else if (C == 13) PP("LD5", 5)
else if (C == 14) PP("LD6", 5)
else if (C == 15) PP("LDX", 5)
else if (C == 16) PP("LDAN", 5)
else if (C == 17) PP("LD1N", 5)
else if (C == 18) PP("LD2N", 5)
else if (C == 19) PP("LD3N", 5)
else if (C == 20) PP("LD4N", 5)
else if (C == 21) PP("LD5N", 5)
else if (C == 22) PP("LD6N", 5)
else if (C == 23) PP("LDXN", 5)
else if (C == 24) PP("STA", 5)
else if (C == 25) PP("ST1", 5)
else if (C == 26) PP("ST2", 5)
else if (C == 27) PP("ST3", 5)
else if (C == 28) PP("ST4", 5)
else if (C == 29) PP("ST5", 5)
else if (C == 30) PP("ST6", 5)
else if (C == 31) PP("STX", 5)
else if (C == 32) PP("STJ", 2)
else if (C == 33) PP("STZ", 5)
else if (C == 34) PP("JBUS", 0)
else if (C == 35) PP("IOC", 0)
else if (C == 36) PP("IN", 0)
else if (C == 37) PP("OUT", 0)
else if (C == 38) PP("JRED", 0)
else if (C == 39) {
if (F == 0) PP("JMP", 0)
else if (F == 1) PP("JSJ", 1)
else if (F == 2) PP("JOV", 2)
else if (F == 3) PP("JNOV", 3)
else if (F == 4) PP("JL", 4)
else if (F == 5) PP("JE", 5)
else if (F == 6) PP("JG", 6)
else if (F == 7) PP("JGE", 7)
else if (F == 8) PP("JNE", 8)
else if (F == 9) PP("JLE", 9)
else UNKNOWN()
}
else if (C == 40) {
if (F == 0) PP("JAN", 0)
else if (F == 1) PP("JAZ", 1)
else if (F == 2) PP("JAP", 2)
else if (F == 3) PP("JANN", 3)
else if (F == 4) PP("JANZ", 4)
else if (F == 5) PP("JANP", 5)
else UNKNOWN()
}
else if (C == 41) {
if (F == 0) PP("J1N", 0)
else if (F == 1) PP("J1Z", 1)
else if (F == 2) PP("J1P", 2)
else if (F == 3) PP("J1NN", 3)
else if (F == 4) PP("J1NZ", 4)
else if (F == 5) PP("J1NP", 5)
else UNKNOWN()
}
else if (C == 42) {
if (F == 0) PP("J2N", 0)
else if (F == 1) PP("J1Z", 1)
else if (F == 2) PP("J2P", 2)
else if (F == 3) PP("J2NN", 3)
else if (F == 4) PP("J2NZ", 4)
else if (F == 5) PP("J2NP", 5)
else UNKNOWN()
}
else if (C == 43) {
if (F == 0) PP("J3N", 0)
else if (F == 1) PP("J3Z", 1)
else if (F == 2) PP("J3P", 2)
else if (F == 3) PP("J3NN", 3)
else if (F == 4) PP("J3NZ", 4)
else if (F == 5) PP("J3NP", 5)
else UNKNOWN()
}
else if (C == 44) {
if (F == 0) PP("J4N", 0)
else if (F == 1) PP("J4Z", 1)
else if (F == 2) PP("J4P", 2)
else if (F == 3) PP("J4NN", 3)
else if (F == 4) PP("J4NZ", 4)
else if (F == 5) PP("J4NP", 5)
else UNKNOWN()
}
else if (C == 45) {
if (F == 0) PP("J5N", 0)
else if (F == 1) PP("J5Z", 1)
else if (F == 2) PP("J5P", 2)
else if (F == 3) PP("J5NN", 3)
else if (F == 4) PP("J5NZ", 4)
else if (F == 5) PP("J5NP", 5)
else UNKNOWN()
}
else if (C == 46) {
if (F == 0) PP("J6N", 0)
else if (F == 1) PP("J6Z", 1)
else if (F == 2) PP("J6P", 2)
else if (F == 3) PP("J6NN", 3)
else if (F == 4) PP("J6NZ", 4)
else if (F == 5) PP("J6NP", 5)
else UNKNOWN()
}
else if (C == 47) {
if (F == 0) PP("JXN", 0)
else if (F == 1) PP("JXZ", 1)
else if (F == 2) PP("JXP", 2)
else if (F == 3) PP("JXNN", 3)
else if (F == 4) PP("JXNZ", 4)
else if (F == 5) PP("JXNP", 5)
else UNKNOWN()
}
else if (C == 48) {
if (F == 0) PP("INCA", 0)
else if (F == 1) PP("DECA", 1)
else if (F == 2) PP("ENTA", 2)
else if (F == 3) PP("ENNA", 3)
else UNKNOWN()
}
else if (C == 49) {
if (F == 0) PP("INC1", 0)
else if (F == 1) PP("DEC1", 1)
else if (F == 2) PP("ENT1", 2)
else if (F == 3) PP("ENN1", 3)
else UNKNOWN()
}
else if (C == 50) {
if (F == 0) PP("INC2", 0)
else if (F == 1) PP("DEC2", 1)
else if (F == 2) PP("ENT2", 2)
else if (F == 3) PP("ENN2", 3)
else UNKNOWN()
}
else if (C == 51) {
if (F == 0) PP("INC3", 0)
else if (F == 1) PP("DEC3", 1)
else if (F == 2) PP("ENT3", 2)
else if (F == 3) PP("ENN3", 3)
else UNKNOWN()
}
else if (C == 52) {
if (F == 0) PP("INC4", 0)
else if (F == 1) PP("DEC4", 1)
else if (F == 2) PP("ENT4", 2)
else if (F == 3) PP("ENN4", 3)
else UNKNOWN()
}
else if (C == 53) {
if (F == 0) PP("INC5", 0)
else if (F == 1) PP("DEC5", 1)
else if (F == 2) PP("ENT5", 2)
else if (F == 3) PP("ENN5", 3)
else UNKNOWN()
}
else if (C == 54) {
if (F == 0) PP("INC6", 0)
else if (F == 1) PP("DEC6", 1)
else if (F == 2) PP("ENT6", 2)
else if (F == 3) PP("ENN6", 3)
else UNKNOWN()
}
else if (C == 55) {
if (F == 0) PP("INCX", 0)
else if (F == 1) PP("DECX", 1)
else if (F == 2) PP("ENTX", 2)
else if (F == 3) PP("ENNX", 3)
else UNKNOWN()
}
else if (C == 56) PP("CMPA", 5)
else if (C == 57) PP("CMP1", 5)
else if (C == 58) PP("CMP2", 5)
else if (C == 59) PP("CMP3", 5)
else if (C == 60) PP("CMP4", 5)
else if (C == 61) PP("CMP5", 5)
else if (C == 62) PP("CMP6", 5)
else if (C == 63) PP("CMPX", 5)
else UNKNOWN()
}
// Display the MIXAL source resulting in that instruction.
// This differs from the canonical representation in that there may be
// user-defined constants.
// If MIXAL source is not available, fall back to the canonical representation.
void displayinstr_mixal(int i, mmmstate *mmm) {
if (i < 0 || i >= 4000) {
printf(RED("Invalid memory address %04d\n"), i);
return;
}
if (mmm->debuglines[i][0] == '\0') {
putchar('\t');
displayinstr_canonical(mmm->mix.mem[i]);
}
else
printf(mmm->debuglines[i]);
}
// Display the instruction as a complete line, for debugging purposes.
// The following format is used:
// LINENUM:EXECCOUNT +- AAAA I F C MIXAL or canonical
void displayinstr_debug(int i, mmmstate *mmm) {
int execcount = mmm->mix.execcounts[i];
printf(BLUE("%04d:%d "), i, execcount+1);
printf("\033[33m");
displayinstr_raw(mmm->mix.mem[i]);
printf("\033[37m\t");
displayinstr_mixal(i, mmm);
putchar('\n');
}
// Display the memory address as a complete line, with lots of information.
// The following format is used:
// LINENUM +- AAAA I F C +- B1 B2 B3 B4 B5 (integer value) MIXAL or canonical
void displayaddr_verbose(int i, mmmstate *mmm) {
if (i < 0 || i >= 4000) {
printf(RED("Invalid memory address %04d\n"), i);
return;
}
// Display currently running instruction in green
if (i == mmm->mix.PC) {
printf("\033[32m%04d ", i);
displayinstr_raw(mmm->mix.mem[i]);
printf(" ");
displayword(mmm->mix.mem[i]);
printf(" ");
// If the number of chars to display the integer value of memory
// contents exceeds a certain amount
if (INT(mmm->mix.mem[i]) >= 100000 || (int)INT(mmm->mix.mem[i]) <= -10000)
printf("(%d)\t", INT(mmm->mix.mem[i]));
else
printf("(%d)\t\t", INT(mmm->mix.mem[i]));
displayinstr_mixal(i, mmm);
printf("\033[37m\n");
}
// Display line in normal formatting
else {
printf(BLUE("%04d "), i);
printf("\033[33m");
displayinstr_raw(mmm->mix.mem[i]);
printf(" ");
displayword(mmm->mix.mem[i]);
printf("\033[37m ");
if (INT(mmm->mix.mem[i]) >= 100000 || (int)INT(mmm->mix.mem[i]) <= -10000)
printf(CYAN("(%d)\t"), INT(mmm->mix.mem[i]));
else
printf(CYAN("(%d)\t\t"), INT(mmm->mix.mem[i]));
displayinstr_mixal(i, mmm);
putchar('\n');
}
}
void printregisters(mmmstate *mmm) {
printf(" A: ");
printf("\033[33m"); displayword(mmm->mix.A); printf("\033[37m");
printf("\t");
printf(CYAN("(%d)"), INT(mmm->mix.A));
printf("\n X: ");
printf("\033[33m"); displayword(mmm->mix.X); printf("\033[37m");
printf("\t");
printf(CYAN("(%d)"), INT(mmm->mix.X));
printf("\nI1: ");
printf("\033[33m"); displayshort(mmm->mix.Is[0]); printf("\033[37m");
printf("\t"); printf(CYAN("(%d)"), INT(mmm->mix.Is[0]));
printf("\nI2: ");
printf("\033[33m"); displayshort(mmm->mix.Is[1]); printf("\033[37m");
printf("\t"); printf(CYAN("(%d)"), INT(mmm->mix.Is[1]));
printf("\nI3: ");
printf("\033[33m"); displayshort(mmm->mix.Is[2]); printf("\033[37m");
printf("\t"); printf(CYAN("(%d)"), INT(mmm->mix.Is[2]));
printf("\nI4: ");
printf("\033[33m"); displayshort(mmm->mix.Is[3]); printf("\033[37m");
printf("\t"); printf(CYAN("(%d)"), INT(mmm->mix.Is[3]));
printf("\nI5: ");
printf("\033[33m"); displayshort(mmm->mix.Is[4]); printf("\033[37m");
printf("\t"); printf(CYAN("(%d)"), INT(mmm->mix.Is[4]));
printf("\nI6: ");
printf("\033[33m"); displayshort(mmm->mix.Is[5]); printf("\033[37m");
printf("\t"); printf(CYAN("(%d)"), INT(mmm->mix.Is[5]));
printf("\n J: ");
printf("\033[33m"); displayshort(mmm->mix.J); printf("\033[37m");
printf("\t"); printf(CYAN("(%d)"), INT(mmm->mix.J));
printf("\n\nOverflow: ");
if (mmm->mix.overflow)
printf(CYAN("ON\n"));
else
printf(CYAN("OFF\n"));
printf("Comparison: ");
if (mmm->mix.cmp == -1)
printf(CYAN("<"));
else if (mmm->mix.cmp == 1)
printf(CYAN(">"));
else
printf(CYAN("="));
printf("\n\nBusy IO devices:\n");
for (int i = 0; i < 21; i++) {
IOthread iothread = mmm->mix.iothreads[i];
if (iothread.timer == 0)
continue;
if (iothread.C == 35)
printf(GREEN("%d") CYAN(" IOC") " (%du left)\n", i, iothread.timer);
else if (iothread.C == 36)
printf(GREEN("%d") CYAN(" IN ") " (%du left)\n", i, iothread.timer);
else if (iothread.C == 37)
printf(GREEN("%d") CYAN(" OUT") " (%du left)\n", i, iothread.timer);
}
printf("\nCur instruction:\n");
displayinstr_debug(mmm->mix.PC, mmm);
}
int numdigits(int x) {
if (x == 0) return 1;
int i = 0;
while ((x = x/10) > 0)
i++;
return i;
}
void printtime(mmmstate *mmm) {
int totaltime = 0;
int maxdigits = 0;
for (int i = 0; i < 4000; i++) {
totaltime += mmm->mix.exectimes[i];
int nd = numdigits(mmm->mix.execcounts[i]);
if (nd > maxdigits)
maxdigits = nd;
}
printf("Time taken: %du\n\n", totaltime);
printf(CYAN("BREAKDOWN\n"));
for (int i = 0; i < 4000; i++) {
int count = mmm->mix.execcounts[i];
int numchars;
if (count == 1)
numchars = printf(BLUE("%04d ") YELLOW("%*c%d time, %du"), i,
maxdigits-numdigits(count)+1, ' ', count, mmm->mix.exectimes[i]);
else if (count > 1)
numchars = printf(BLUE("%04d ") YELLOW("%*c%d times, %du"), i,
maxdigits-numdigits(count)+1, ' ', count, mmm->mix.exectimes[i]);
// Compensate for the \033 sequences using up 20 characters, since
// they don't move the cursor right
numchars -= 20;
if (count >= 1) {
if (numchars < 24)
printf("\t\t");
else if (numchars < 32)
printf("\t");
displayinstr_mixal(i, mmm);
putchar('\n');
}
}
}
bool onestepwrapper(int tracecount, mmmstate *mmm) {
int execcount = mmm->mix.execcounts[mmm->mix.PC];
if (execcount < tracecount) {
if (!mmm->shouldtrace) {
printf("----------------------------------------------------------------------------------------------\n");
mmm->shouldtrace = true;
}
displayinstr_debug(mmm->mix.PC, mmm);
}
else {
mmm->shouldtrace = false;
}
onestep(&mmm->mix);
if (mmm->mix.err[0] != '\0') {
printf(RED("Emulator stopped at %d: %s\n"), mmm->mix.PC, mmm->mix.err);
return false;
}
return true;
}
bool getsymvalue(char *s, mmmstate *mmm, word *w) {
// Find the symbol in parse state
for (int i = 0; i < MAXSYMS; i++) {
if (strncmp(s, mmm->ps.syms[i], 11) == 0) {
*w = mmm->ps.symvals[i];
return true;
}
}
return false;
}
void viewcommand(char *arg, mmmstate *mmm) {
// Print all nonzero memory addresses
if (arg[0] == '\0') {
for (int i = 0; i < 4000; i++) {
if (mmm->mix.mem[i] != POS(0))
displayaddr_verbose(i, mmm);
}
}
// Print selected addresses based on arg, where
// arg = "<from>-<to>" or "<arg>"
else if (isdigit(arg[0])) {
char *fromstart = arg, *tostart;
int from, to;
while (!isspace(*arg) && *arg != '-')
arg++;
if (*arg == '-') {
*(arg++) = '\0';
tostart = arg;
while (!isspace(*(arg++))) {};
*(arg-1) = '\0';
from = atoi(fromstart);
to = atoi(tostart);
}
else {
*arg = '\0';
from = atoi(fromstart);
to = from;
}
for (int i = from; i <= to; i++)
displayaddr_verbose(i, mmm);
}
// Print address corresponding to the given symbol
else if (arg[0] == '.') {
word w;
if (getsymvalue(arg+1, mmm, &w))
displayaddr_verbose(INT(w), mmm);
else
printf(BLUE("I'm not aware of the symbol %s\n"), arg);
}
}
void breakpointcommand(char *arg, mmmstate *mmm) {
int bp;
if (isdigit(arg[0])) {
bp = atoi(arg);
}
else if (arg[0] == '.') {
word w;
if (!getsymvalue(arg+1, mmm, &w)) {
printf(BLUE("I'm not aware of the symbol %s\n"), arg);
return;
}
bp = INT(w);
}
if (bp < 0 || bp >= 4000) {
printf(RED("Breakpoint must be between 0-4000\n"));
return;
}
do {
if (!onestepwrapper(0, mmm))
break;
} while (mmm->mix.PC != bp);
displayinstr_debug(bp, mmm);
}
void gocommand(char *arg, mmmstate *mmm) {
int tracecount;
if (isdigit(arg[0]))
tracecount = arg[0] - '0';
else if (arg[0] == '+')
tracecount = 2147483647;
else
tracecount = 0;
mmm->shouldtrace = true;
while (!mmm->mix.done)
onestepwrapper(tracecount, mmm);
printf(GREEN("Program has finished running; type l to reset\n"));
}
bool loadcardfile(char *filename, mmmstate *mmm) {
if (filename[0] == '\0')
return true;
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL) {
printf(RED("Could not open card file %s\n"), filename);
return false;
}
printf(GREEN("Loaded card file %s\n"), filename);
if (mmm->mix.cardfile)
fclose(mmm->mix.cardfile);
mmm->mix.cardfile = fp;
return true;
}
bool loadtapefile(char *filename, int n, mmmstate *mmm) {
if (filename[0] == '\0')
return true;
if (n < 0 || n > 7) {
printf(RED("Invalid tape number %d\n"), n);
return false;
}
FILE *fp;
if ((fp = fopen(filename, "r+")) == NULL) {
printf(RED("Could not open tape file %s\n"), filename);
return false;
}
printf(GREEN("Loaded tape file %s\n"), filename);
if (mmm->mix.tapefiles[n])
fclose(mmm->mix.tapefiles[n]);
mmm->mix.tapefiles[n] = fp;
return true;
}
bool loadmixalfile(char *filename, mmmstate *mmm) {
initmix(&mmm->mix);
initparsestate(&mmm->ps);
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL) {
printf(RED("Could not open MIXAL file %s\n"), filename);
return false;
}
int linenum = 0;
char line[LINELEN];
extraparseinfo extraparseinfo;
while (++linenum && fgets(line, LINELEN, fp) != NULL) {
if (!parseline(line, &mmm->ps, &mmm->mix, &extraparseinfo)) {
printf(RED("Assembler error at line %d: %s"), linenum, line);
initmix(&mmm->mix);
return false;
}
if (extraparseinfo.setdebugline) {
strncpy(mmm->debuglines[mmm->ps.star-1], line, LINELEN);
int len = strlen(line);
mmm->debuglines[mmm->ps.star-1][len-1] = '\0';
}
if (extraparseinfo.isend)
break;
}
printf(GREEN("Loaded MIXAL file %s\n"), filename);
return true;
}
void printhelp() {
printf(
CYAN("COMMANDS:\n")
"<empty>\t\trepeat previous command\n"
"l\t\treload MIXAL and card file\n"
"@<file>\t\tload new card file\n"
"s\t\trun one step\n"
"b<line>\t\trun till specified line\n"
"b.<sym>\t\trun till specified line\n"
"g\t\trun whole program\n"
"g<n>\t\ttrace first n executions of each line (n is a digit)\n"
"g+\t\ttrace everything\n"
"v\t\tview nonempty memory cells\n"
"v<line>\t\tview specific cell\n"
"v.<sym>\t\tview specific cell\n"
"v<from>-<to>\tview a range of cells\n"
"r\t\tview registers and flags\n"
"t\t\tprint timing statistics\n"
"h\t\tprint this help\n"
"q\t\tquit\n"
);
}
void initmmmstate(mmmstate *mmm) {
// mmm->mix will be initialized in loadmixalfile()
mmm->globalcardfile[0] = '\0';
for (int i = 0; i < 8; i++)
mmm->globaltapefiles[i][0] = '\0';
mmm->prevline[0] = '\0';
for (int i = 0; i < 4000; i++)
mmm->debuglines[i][0] = '\0';
mmm->shouldtrace = true;
// Default IO operation times
mmm->mix.INtimes[16] = 10000;
mmm->mix.IOCtimes[18] = 10000;
mmm->mix.OUTtimes[18] = 7500;
// I chose a value arbitrarily for tape IO because I haven't seen an
// official figure yet
for (int i = 0; i < 8; i++) {
mmm->mix.INtimes[i] = 30000;
mmm->mix.OUTtimes[i] = 30000;
}
}
int main(int argc, char **argv) {
mmmstate mmm;
initmmmstate(&mmm);
// Handle arguments
if (argc < 2) {
printf(RED("Please specify a filename!\n"));
return 0;
}
if (!loadmixalfile(argv[1], &mmm))
return 0;
if (argc >= 3 && loadcardfile(argv[2], &mmm))
strncpy(mmm.globalcardfile, argv[2], LINELEN);
printf(CYAN("MIX Management Module, by wyan\n"));
printf("Type h for help\n");
char line[LINELEN+1];
while (true) {
printf(">> ");
fgets(line, LINELEN, stdin);
// Strip the last newline
line[strnlen(line, LINELEN)-1] = '\0';
if (feof(stdin))
return 0;
if (line[0] == '\0') { // Previous command
strncpy(line, mmm.prevline, LINELEN);
}
strncpy(mmm.prevline, line, LINELEN);
if (line[0] == 'l') { // Reload MIXAL and card file
if (!loadmixalfile(argv[1], &mmm))
return 0;
loadcardfile(mmm.globalcardfile, &mmm);
}
else if (line[0] == '@') { // Load new card file
if (loadcardfile(line+1, &mmm))
strncpy(mmm.globalcardfile, line+1, LINELEN);
}
else if (line[0] == '#') { // Load new tape file
if (line[1] == '\0')
printf(RED("You have to provide a tape number!\n"));
else {
int n = line[1]-'0';
if (loadtapefile(line+2, n, &mmm))
strncpy(mmm.globaltapefiles[n], line+2, LINELEN);
}
}
else if (line[0] == 's') { // Run one step
if (mmm.mix.done) {
printf(GREEN("Program has finished running; type l to reset\n"));
continue;
}
onestepwrapper(0, &mmm);
displayinstr_debug(mmm.mix.PC, &mmm);
}
else if (line[0] == 'b') // Run till breakpoint
breakpointcommand(line+1, &mmm);
else if (line[0] == 'g') { // Run whole program
gocommand(line+1, &mmm);
}
else if (line[0] == 'v') // View memory
viewcommand(line+1, &mmm);
else if (line[0] == 'r') // View registers + flags
printregisters(&mmm);
else if (line[0] == 't') // View timing statistics
printtime(&mmm);
else if (line[0] == 'h') // Help
printhelp();
else if (line[0] == 'q') // Quit
return 0;
else
printf(BLUE("Hold up, I don't understand that command\n"));
}
}