4 * (c) 2010 Martin Mares <mj@ucw.cz>
10 * - debugging/play mode
11 * - we probably have to disable NOP
15 * Things that are not implemented:
17 * - exact behavior of accumulator/R1/R2 (the manual lacks details)
18 * - exact behavior of negative zero
29 static int cpu_quota = -1;
30 static int print_quota = -1;
32 // Minsk-2 has 37-bit words in sign-magnitude representation (bit 36 = sign)
33 typedef unsigned long long int word;
35 #define WORD_MASK 01777777777777ULL
36 #define SIGN_MASK 01000000000000ULL
37 #define VAL_MASK 00777777777777ULL
39 static int wsign(word w)
41 return (w & SIGN_MASK) ? -1 : 1;
44 static word wabs(word w)
49 #define WF(w) (wsign(w) < 0 ? '-' : '+'), wabs(w)
51 static long long wtoll(word w)
59 static word wfromll(long long x)
61 word w = ((x < 0) ? -x : x) & VAL_MASK;
67 static double wtofrac(word w)
69 return (double)wtoll(w) / (double)(1ULL << 36);
72 static word wfromfrac(double d)
74 return wfromll((long long)(d * (double)(1ULL << 36)));
77 static int int_in_range(long long x)
79 return (x >= -(long long)VAL_MASK && x <= (long long)VAL_MASK);
82 static int frac_in_range(double d)
84 return (d > -1. && d < 1.);
87 static int wexp(word w)
90 return (w & 0100 ? -exp : exp);
93 static word wputexp(word w, int exp)
95 return ((w & ~(word)0177) | ((exp < 0) ? 0100 | (-exp) : exp));
98 static int wmanti(word w)
100 return ((w >> 8) & ((1 << 28) - 1));
103 static double wtofloat(word w)
105 double x = wmanti(w);
106 return ldexp(x, wexp(w) - 28);
109 static int float_in_range(double x)
112 return (x <= ldexp((1 << 28) - 1, 63 - 28));
115 static word wfromfloat(double x, int normalized)
124 double m = frexp(x, &exp);
125 word mm = (word) ldexp(m, 28);
130 if (normalized || exp < -91)
148 static word mem[4096];
150 static word rd(int addr)
152 word val = addr ? mem[addr] : 0;
154 printf("\tRD %04o = %c%012llo\n", addr, WF(val));
158 static void wr(int addr, word val)
160 assert(!(val & ~(WORD_MASK)));
162 printf("\tWR %04o = %c%012llo\n", addr, WF(val));
168 static void parse_error(char *msg)
170 printf("Ошибка входа (стр. %d): %s\n", lino, msg);
174 static void parse_in(void)
179 while (fgets(line, sizeof(line), stdin))
182 char *eol = strchr(line, '\n');
184 parse_error("Строка слишком долгая");
188 if (!c[0] || c[0] == ';')
194 for (int i=0; i<4; i++)
198 if (*c >= '0' && *c <= '7')
199 addr = 8*addr + *c++ - '0';
201 parse_error("Плохая цифва");
206 parse_error("Адрес слишком долгий");
214 parse_error("Плохой знак");
216 for (int i=0; i<12; i++)
220 if (*c >= '0' && *c <= '7')
221 w = 8*w + *c++ - '0';
223 parse_error("Плохая цифва");
228 parse_error("Номер слишком долгий");
235 static word r1, r2, current_ins;
236 static int ip = 00050; // Standard program start location
239 static void stop(char *reason)
241 printf("MACHINE STOPPED -- %s\n", reason);
242 printf("IP:%04o ACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", prev_ip, WF(acc), WF(r1), WF(r2));
246 static void over(void)
251 static void notimp(void)
254 stop("NOT IMPLEMENTED");
257 static void noins(void)
260 stop("ILLEGAL INSTRUCTION");
263 static uint16_t linebuf[128];
265 static uint16_t russian_chars[64] = {
266 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
267 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
268 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
269 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
270 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, // 4x
271 0x418, 0x419, 0x41a, 0x41b, 0x41c, 0x41d, 0x41e, 0x41f, // 5x
272 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, // 6x
273 0x428, 0x429, 0x42b, 0x42c, 0x42d, 0x42e, 0x42f, 0x2013 // 7x
276 static uint16_t latin_chars[64] = {
277 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
278 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
279 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
280 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
281 'A', 'B', 'W', 'G', 'D', 'E', 'V', 'Z', // 4x
282 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 5x
283 'R', 'S', 'T', 'U', 'F', 'H', 'C', ' ', // 6x
284 ' ', ' ', 'Y', 'X', ' ', ' ', 'Q', 0x2013 // 7x
287 static void print_line(int r)
290 * Meaning of bits of r:
291 * 0 = perform line feed
297 if (print_quota > 0 && !--print_quota)
298 stop("OUT OF PAPER");
299 for (int i=0; i<128; i++)
308 putchar(0xc0 | (ch >> 6));
309 putchar(0x80 | (ch & 0x3f));
313 putchar(0xe0 | (ch >> 12));
314 putchar(0x80 | ((ch >> 6) & 0x3f));
315 putchar(0x80 | (ch & 0x3f));
320 memset(linebuf, 0, sizeof(linebuf));
328 static void print_ins(int x, int y)
332 int r = (x >> 9) & 7;
345 case 0: // Decimal float
346 fmt = "+dddddddx+xbd";
348 case 1: // Octal number
349 fmt = "+oooooooooooo";
351 case 2: // Decimal fixed
354 case 3: // Decimal unsigned
358 case 4: // One Russian symbol
362 case 5: // Russian text
365 case 6: // One Latin symbol
369 default: // Latin text
386 ch = (yy & (1ULL << bit)) ? '-' : '+';
390 ch = '0' + ((yy >> bit) & 1);
394 ch = '0' + ((yy >> bit) & 7);
398 ch = '0' + ((yy >> bit) & 15);
404 ch = russian_chars[(yy >> bit) & 077];
408 ch = latin_chars[(yy >> bit) & 077];
416 if (ch == '0' || ch == ' ')
422 pos = (pos+1) & 0177;
427 static void run(void)
436 int op = (w >> 30) & 0177; // Operation code
437 int ax = (w >> 28) & 3; // Address extensions not supported
438 int ix = (w >> 24) & 15; // Indexing
439 int x = (w >> 12) & 07777; // Operands (original form)
441 int xi=x, yi=y; // (indexed form)
443 printf("@%04o %c%02o %02o %04o %04o\n",
445 (w & SIGN_MASK) ? '-' : '+',
446 (int)((w >> 30) & 077),
447 (int)((w >> 24) & 077),
455 xi = (xi + (int)((i >> 12) & 07777)) & 07777;
456 yi = (yi + (int)(i & 07777)) & 07777;
458 printf("\tIndexing -> %04o %04o\n", xi, yi);
463 if (cpu_quota > 0 && !--cpu_quota)
466 /* Arithmetic operations */
469 long long aa, bb, cc;
473 auto void afetch(void);
483 auto void astore(word result);
484 void astore(word result)
491 auto void astore_int(long long x);
492 void astore_int(long long x)
494 if (!int_in_range(x))
499 auto void astore_frac(double f);
500 void astore_frac(double f)
502 if (!frac_in_range(f))
504 astore(wfromfrac(f));
507 auto void astore_float(double f);
508 void astore_float(double f)
510 if (!float_in_range(f))
512 astore(wfromfloat(f, 0));
521 case 004 ... 007: // XOR
525 case 010 ... 013: // FIX addition
527 astore_int(wtoll(a) + wtoll(b));
529 case 014 ... 017: // FP addition
531 astore_float(wtofloat(a) + wtofloat(b));
533 case 020 ... 023: // FIX subtraction
535 astore_int(wtoll(a) - wtoll(b));
537 case 024 ... 027: // FP subtraction
539 astore_float(wtofloat(a) - wtofloat(b));
541 case 030 ... 033: // FIX multiplication
543 astore_frac(wtofrac(a) * wtofrac(b));
545 case 034 ... 037: // FP multiplication
547 astore_float(wtofloat(a) * wtofloat(b));
549 case 040 ... 043: // FIX division
554 stop("DIVISION BY ZERO");
555 astore_frac(ad / bd);
557 case 044 ... 047: // FP division
562 stop("DIVISION BY ZERO");
563 astore_float(ad / bd);
565 case 050 ... 053: // FIX subtraction of abs values
567 astore_int(wabs(a) - wabs(b));
569 case 054 ... 057: // FP subtraction of abs values
571 astore_float(fabs(wtofloat(a)) - fabs(wtofloat(b)));
573 case 060 ... 063: // Shift logical
576 if (i <= -37 || i >= 37)
579 astore((a << i) & WORD_MASK);
583 case 064 ... 067: // Shift arithmetical
587 if (i <= -36 || i >= 36)
590 cc = (aa << i) & VAL_MASK;
593 astore((a & SIGN_MASK) | wfromll(cc));
595 case 070 ... 073: // And
599 case 074 ... 077: // Or
608 case 0103: // I/O magtape
610 case 0104: // Disable rounding
612 case 0105: // Enable rounding
614 case 0106: // Interrupt control
616 case 0107: // Reverse tape
619 wr(yi, r1 = acc = rd(xi));
621 case 0111: // Move negative
622 wr(yi, acc = (r1 = rd(xi)) ^ SIGN_MASK);
624 case 0112: // Move absolute value
625 wr(yi, acc = (r1 = rd(xi)) & VAL_MASK);
627 case 0113: // Read from keyboard
629 case 0114: // Copy sign
630 wr(yi, acc = rd(yi) ^ ((r1 = rd(xi)) & SIGN_MASK));
632 case 0115: // Read code from R1 (obscure)
634 case 0116: // Copy exponent
635 wr(yi, acc = wputexp(rd(yi), wexp(r1 = rd(xi))));
637 case 0117: // I/O teletype
643 aa = (a >> 24) & 017777;
646 b = rd(y); // (a mountain range near Prague)
647 acc = ((aa-1) << 24) |
648 (((((a >> 12) & 07777) + (b >> 12) & 07777) & 07777) << 12) |
649 (((a & 07777) + (b & 07777)) & 07777);
657 case 0131: // Jump to subroutine
658 wr(y, acc = ((030ULL << 30) | ((ip & 07777ULL) << 12)));
661 case 0132: // Jump if positive
667 case 0133: // Jump if overflow
668 // Since we always trap on overflow, this instruction always jumps to the 1st address
671 case 0134: // Jump if zero
677 case 0135: // Jump if key pressed
678 // No keys are ever pressed, so always jump to 2nd
681 case 0136: // Interrupt masking
683 case 0137: // Used only when reading from tape
685 case 0140 ... 0147: // I/O
687 case 0150 ... 0154: // I/O
689 case 0160 ... 0161: // I/O
691 case 0162: // Printing
696 case 0170: // FIX multiplication, bottom part
698 if (wtofrac(a) * wtofrac(b) >= .1/(1ULL << 32))
700 acc = wfromll(((unsigned long long)wabs(a) * (unsigned long long)wabs(b)) & VAL_MASK);
701 // XXX: What should be the sign? The book does not define that.
708 stop("DIVISION BY ZERO");
714 case 0172: // Add exponents
717 i = wexp(a) + wexp(b);
718 if (i < -63 || i > 63)
723 case 0173: // Sub exponents
726 i = wexp(b) - wexp(a);
727 if (i < -63 || i > 63)
732 case 0174: // Addition in one's complement
739 // XXX: The effect on the accumulator is undocumented, but likely to be as follows:
742 case 0175: // Normalization
747 wr((yi+1) & 07777, 0);
755 while (!(a & (SIGN_MASK >> 1)))
762 wr((yi+1) & 07777, i);
765 case 0176: // Population count
768 for (int i=0; i<36; i++)
771 // XXX: Guessing that acc gets a copy of the result
780 printf("\tACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", WF(acc), WF(r1), WF(r2));