4 * (c) 2010 Martin Mares <mj@ucw.cz>
11 * - debugging/play mode
12 * - we probably have to disable NOP
16 * Things that are not implemented:
18 * - exact behavior of accumulator/R1/R2 (the manual lacks details)
19 * - exact behavior of negative zero
31 // Minsk-2 has 37-bit words in sign-magnitude representation (bit 36 = sign)
32 typedef unsigned long long int word;
34 #define WORD_MASK 01777777777777ULL
35 #define SIGN_MASK 01000000000000ULL
36 #define VAL_MASK 00777777777777ULL
38 static int wsign(word w)
40 return (w & SIGN_MASK) ? -1 : 1;
43 static word wabs(word w)
48 #define WF(w) (wsign(w) < 0 ? '-' : '+'), wabs(w)
50 static long long wtoll(word w)
58 static word wfromll(long long x)
60 word w = ((x < 0) ? -x : x) & VAL_MASK;
66 static double wtofrac(word w)
68 return (double)wtoll(w) / (double)(1ULL << 36);
71 static word wfromfrac(double d)
73 return wfromll((long long)(d * (double)(1ULL << 36)));
76 static int int_in_range(long long x)
78 return (x >= -(long long)VAL_MASK && x <= (long long)VAL_MASK);
81 static int frac_in_range(double d)
83 return (d > -1. && d < 1.);
86 static int wexp(word w)
89 return (w & 0100 ? -exp : exp);
92 static word wputexp(word w, int exp)
94 return ((w & ~(word)0177) | ((exp < 0) ? 0100 | (-exp) : exp));
97 static int wmanti(word w)
99 return ((w >> 8) & ((1 << 28) - 1));
102 static double wtofloat(word w)
104 double x = wmanti(w);
105 return ldexp(x, wexp(w) - 28);
108 static int float_in_range(double x)
111 return (x <= ldexp((1 << 28) - 1, 63 - 28));
114 static word wfromfloat(double x, int normalized)
123 double m = frexp(x, &exp);
124 word mm = (word) ldexp(m, 28);
129 if (normalized || exp < -91)
147 static word mem[4096];
149 static word rd(int addr)
151 word val = addr ? mem[addr] : 0;
153 printf("\tRD %04o = %c%012llo\n", addr, WF(val));
157 static void wr(int addr, word val)
159 assert(!(val & ~(WORD_MASK)));
161 printf("\tWR %04o = %c%012llo\n", addr, WF(val));
167 static void parse_error(char *msg)
169 printf("Ошибка входа (стр. %d): %s\n", lino, msg);
173 static void parse_in(void)
178 while (fgets(line, sizeof(line), stdin))
181 char *eol = strchr(line, '\n');
183 parse_error("Строка слишком долгая");
187 if (!c[0] || c[0] == ';')
193 for (int i=0; i<4; i++)
197 if (*c >= '0' && *c <= '7')
198 addr = 8*addr + *c++ - '0';
200 parse_error("Плохая цифва");
205 parse_error("Адрес слишком долгий");
213 parse_error("Плохой знак");
215 for (int i=0; i<12; i++)
219 if (*c >= '0' && *c <= '7')
220 w = 8*w + *c++ - '0';
222 parse_error("Плохая цифва");
227 parse_error("Номер слишком долгий");
234 static word r1, r2, current_ins;
235 static int ip = 00050; // Standard program start location
238 static void stop(char *reason)
240 printf("MACHINE STOPPED -- %s\n", reason);
241 printf("IP:%04o ACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", prev_ip, WF(acc), WF(r1), WF(r2));
245 static void over(void)
250 static void notimp(void)
253 stop("NOT IMPLEMENTED");
256 static void noins(void)
259 stop("ILLEGAL INSTRUCTION");
262 static uint16_t linebuf[128];
264 static uint16_t russian_chars[64] = {
265 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
266 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
267 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
268 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
269 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, // 4x
270 0x418, 0x419, 0x41a, 0x41b, 0x41c, 0x41d, 0x41e, 0x41f, // 5x
271 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, // 6x
272 0x428, 0x429, 0x42b, 0x42c, 0x42d, 0x42e, 0x42f, 0x2013 // 7x
275 static uint16_t latin_chars[64] = {
276 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
277 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
278 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
279 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
280 'A', 'B', 'W', 'G', 'D', 'E', 'V', 'Z', // 4x
281 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 5x
282 'R', 'S', 'T', 'U', 'F', 'H', 'C', ' ', // 6x
283 ' ', ' ', 'Y', 'X', ' ', ' ', 'Q', 0x2013 // 7x
286 static void print_line(int r)
289 * Meaning of bits of r:
290 * 0 = perform line feed
296 for (int i=0; i<128; i++)
305 putchar(0xc0 | (ch >> 6));
306 putchar(0x80 | (ch & 0x3f));
310 putchar(0xe0 | (ch >> 12));
311 putchar(0x80 | ((ch >> 6) & 0x3f));
312 putchar(0x80 | (ch & 0x3f));
317 memset(linebuf, 0, sizeof(linebuf));
325 static void print_ins(int x, int y)
329 int r = (x >> 9) & 7;
342 case 0: // Decimal float
343 fmt = "+dddddddx+xbd";
345 case 1: // Octal number
346 fmt = "+oooooooooooo";
348 case 2: // Decimal fixed
351 case 3: // Decimal unsigned
355 case 4: // One Russian symbol
359 case 5: // Russian text
362 case 6: // One Latin symbol
366 default: // Latin text
383 ch = (yy & (1ULL << bit)) ? '-' : '+';
387 ch = '0' + ((yy >> bit) & 1);
391 ch = '0' + ((yy >> bit) & 7);
395 ch = '0' + ((yy >> bit) & 15);
401 ch = russian_chars[(yy >> bit) & 077];
405 ch = latin_chars[(yy >> bit) & 077];
413 if (ch == '0' || ch == ' ')
419 pos = (pos+1) & 0177;
424 static void run(void)
433 int op = (w >> 30) & 0177; // Operation code
434 int ax = (w >> 28) & 3; // Address extensions not supported
435 int ix = (w >> 24) & 15; // Indexing
436 int x = (w >> 12) & 07777; // Operands (original form)
438 int xi=x, yi=y; // (indexed form)
440 printf("@%04o %c%02o %02o %04o %04o\n",
442 (w & SIGN_MASK) ? '-' : '+',
443 (int)((w >> 30) & 077),
444 (int)((w >> 24) & 077),
452 xi = (xi + (int)((i >> 12) & 07777)) & 07777;
453 yi = (yi + (int)(i & 07777)) & 07777;
455 printf("\tIndexing -> %04o %04o\n", xi, yi);
460 /* Arithmetic operations */
463 long long aa, bb, cc;
467 auto void afetch(void);
477 auto void astore(word result);
478 void astore(word result)
485 auto void astore_int(long long x);
486 void astore_int(long long x)
488 if (!int_in_range(x))
493 auto void astore_frac(double f);
494 void astore_frac(double f)
496 if (!frac_in_range(f))
498 astore(wfromfrac(f));
501 auto void astore_float(double f);
502 void astore_float(double f)
504 if (!float_in_range(f))
506 astore(wfromfloat(f, 0));
515 case 004 ... 007: // XOR
519 case 010 ... 013: // FIX addition
521 astore_int(wtoll(a) + wtoll(b));
523 case 014 ... 017: // FP addition
525 astore_float(wtofloat(a) + wtofloat(b));
527 case 020 ... 023: // FIX subtraction
529 astore_int(wtoll(a) - wtoll(b));
531 case 024 ... 027: // FP subtraction
533 astore_float(wtofloat(a) - wtofloat(b));
535 case 030 ... 033: // FIX multiplication
537 astore_frac(wtofrac(a) * wtofrac(b));
539 case 034 ... 037: // FP multiplication
541 astore_float(wtofloat(a) * wtofloat(b));
543 case 040 ... 043: // FIX division
548 stop("DIVISION BY ZERO");
549 astore_frac(ad / bd);
551 case 044 ... 047: // FP division
556 stop("DIVISION BY ZERO");
557 astore_float(ad / bd);
559 case 050 ... 053: // FIX subtraction of abs values
561 astore_int(wabs(a) - wabs(b));
563 case 054 ... 057: // FP subtraction of abs values
565 astore_float(fabs(wtofloat(a)) - fabs(wtofloat(b)));
567 case 060 ... 063: // Shift logical
570 if (i <= -37 || i >= 37)
573 astore((a << i) & WORD_MASK);
577 case 064 ... 067: // Shift arithmetical
581 if (i <= -36 || i >= 36)
584 cc = (aa << i) & VAL_MASK;
587 astore((a & SIGN_MASK) | wfromll(cc));
589 case 070 ... 073: // And
593 case 074 ... 077: // Or
602 case 0103: // I/O magtape
604 case 0104: // Disable rounding
606 case 0105: // Enable rounding
608 case 0106: // Interrupt control
610 case 0107: // Reverse tape
613 wr(yi, r1 = acc = rd(xi));
615 case 0111: // Move negative
616 wr(yi, acc = (r1 = rd(xi)) ^ SIGN_MASK);
618 case 0112: // Move absolute value
619 wr(yi, acc = (r1 = rd(xi)) & VAL_MASK);
621 case 0113: // Read from keyboard
623 case 0114: // Copy sign
624 wr(yi, acc = rd(yi) ^ ((r1 = rd(xi)) & SIGN_MASK));
626 case 0115: // Read code from R1 (obscure)
628 case 0116: // Copy exponent
629 wr(yi, acc = wputexp(rd(yi), wexp(r1 = rd(xi))));
631 case 0117: // I/O teletype
637 aa = (a >> 24) & 017777;
640 b = rd(y); // (a mountain range near Prague)
641 acc = ((aa-1) << 24) |
642 (((((a >> 12) & 07777) + (b >> 12) & 07777) & 07777) << 12) |
643 (((a & 07777) + (b & 07777)) & 07777);
651 case 0131: // Jump to subroutine
652 wr(y, acc = ((030ULL << 30) | ((ip & 07777ULL) << 12)));
655 case 0132: // Jump if positive
661 case 0133: // Jump if overflow
662 // Since we always trap on overflow, this instruction always jumps to the 1st address
665 case 0134: // Jump if zero
671 case 0135: // Jump if key pressed
672 // No keys are ever pressed, so always jump to 2nd
675 case 0136: // Interrupt masking
677 case 0137: // Used only when reading from tape
679 case 0140 ... 0147: // I/O
681 case 0150 ... 0154: // I/O
683 case 0160 ... 0161: // I/O
685 case 0162: // Printing
690 case 0170: // FIX multiplication, bottom part
692 if (wtofrac(a) * wtofrac(b) >= .1/(1ULL << 32))
694 acc = wfromll(((unsigned long long)wabs(a) * (unsigned long long)wabs(b)) & VAL_MASK);
695 // XXX: What should be the sign? The book does not define that.
702 stop("DIVISION BY ZERO");
708 case 0172: // Add exponents
711 i = wexp(a) + wexp(b);
712 if (i < -63 || i > 63)
717 case 0173: // Sub exponents
720 i = wexp(b) - wexp(a);
721 if (i < -63 || i > 63)
726 case 0174: // Addition in one's complement
733 // XXX: The effect on the accumulator is undocumented, but likely to be as follows:
736 case 0175: // Normalization
741 wr((yi+1) & 07777, 0);
749 while (!(a & (SIGN_MASK >> 1)))
756 wr((yi+1) & 07777, i);
759 case 0176: // Population count
762 for (int i=0; i<36; i++)
765 // XXX: Guessing that acc gets a copy of the result
774 printf("\tACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", WF(acc), WF(r1), WF(r2));