4 * (c) 2010 Martin Mares <mj@ucw.cz>
11 * - debugging/play mode
12 * - implement printing
14 * - we probably have to disable NOP
25 // Minsk-2 has 37-bit words in sign-magnitude representation (bit 36 = sign)
26 typedef unsigned long long int word;
28 #define WORD_MASK 01777777777777ULL
29 #define SIGN_MASK 01000000000000ULL
30 #define VAL_MASK 00777777777777ULL
32 static int wsign(word w)
34 return (w & SIGN_MASK) ? -1 : 1;
37 static word wabs(word w)
42 #define WF(w) (wsign(w) < 0 ? '-' : '+'), wabs(w)
44 static long long wtoll(word w)
52 static word wfromll(long long x)
54 word w = ((x < 0) ? -x : x) & VAL_MASK;
60 static double wtofrac(word w)
62 return (double)wtoll(w) / (double)(1ULL << 36);
65 static word wfromfrac(double d)
67 return wfromll((long long)(d * (double)(1ULL << 36)));
70 static int inrange(long long x)
72 return (x >= -(long long)VAL_MASK && x <= (long long)VAL_MASK);
75 static int fracinrange(double d)
77 return (d > -1. && d < 1.);
80 static int wexp(word w)
83 return (w & 0100 ? -exp : exp);
86 static word mem[4096];
88 static word rd(int addr)
90 word val = addr ? mem[addr] : 0;
92 printf("\tRD %04o = %c%012llo\n", addr, WF(val));
96 static void wr(int addr, word val)
98 assert(!(val & ~(WORD_MASK)));
100 printf("\tWR %04o = %c%012llo\n", addr, WF(val));
106 static void parse_error(char *msg)
108 printf("Ошибка входа (стр. %d): %s\n", lino, msg);
112 static void parse_in(void)
117 while (fgets(line, sizeof(line), stdin))
120 char *eol = strchr(line, '\n');
122 parse_error("Строка слишком долгая");
126 if (!c[0] || c[0] == ';')
132 for (int i=0; i<4; i++)
136 if (*c >= '0' && *c <= '7')
137 addr = 8*addr + *c++ - '0';
139 parse_error("Плохая цифва");
144 parse_error("Адрес слишком долгий");
152 parse_error("Плохой знак");
154 for (int i=0; i<12; i++)
158 if (*c >= '0' && *c <= '7')
159 w = 8*w + *c++ - '0';
161 parse_error("Плохая цифва");
166 parse_error("Номер слишком долгий");
173 static word r1, r2, current_ins;
174 static int flag_zero = 0;
175 static int ip = 00050; // Standard program start location
178 static void stop(char *reason)
180 printf("MACHINE STOPPED -- %s\n", reason);
181 printf("IP:%04o ACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", prev_ip, WF(acc), WF(r1), WF(r2));
185 static void over(void)
190 static void nofpu(void)
196 static void notimp(void)
199 stop("NOT IMPLEMENTED");
202 static void noins(void)
205 stop("ILLEGAL INSTRUCTION");
208 static uint16_t linebuf[128];
210 static uint16_t russian_chars[64] = {
211 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
212 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
213 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
214 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
215 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, // 4x
216 0x418, 0x419, 0x41a, 0x41b, 0x41c, 0x41d, 0x41e, 0x41f, // 5x
217 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, // 6x
218 0x428, 0x429, 0x42b, 0x42c, 0x42d, 0x42e, 0x42f, 0x2013 // 7x
221 static uint16_t latin_chars[64] = {
222 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
223 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
224 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
225 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
226 'A', 'B', 'W', 'G', 'D', 'E', 'V', 'Z', // 4x
227 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 5x
228 'R', 'S', 'T', 'U', 'F', 'H', 'C', ' ', // 6x
229 ' ', ' ', 'Y', 'X', ' ', ' ', 'Q', 0x2013 // 7x
232 static void print_line(int r)
235 * Meaning of bits of r:
236 * 0 = perform line feed
242 for (int i=0; i<128; i++)
251 putchar(0xc0 | (ch >> 6));
252 putchar(0x80 | (ch & 0x3f));
256 putchar(0xe0 | (ch >> 12));
257 putchar(0x80 | ((ch >> 6) & 0x3f));
258 putchar(0x80 | (ch & 0x3f));
263 memset(linebuf, 0, sizeof(linebuf));
271 static void print_ins(int x, int y)
275 int r = (x >> 9) & 7;
288 case 0: // Decimal float
289 fmt = "+dddddddx+xbd";
291 case 1: // Octal number
292 fmt = "+oooooooooooo";
294 case 2: // Decimal fixed
297 case 3: // Decimal unsigned
301 case 4: // One Russian symbol
305 case 5: // Russian text
308 case 6: // One Latin symbol
312 default: // Latin text
329 ch = (yy & (1ULL << bit)) ? '-' : '+';
333 ch = '0' + ((yy >> bit) & 1);
337 ch = '0' + ((yy >> bit) & 7);
341 ch = '0' + ((yy >> bit) & 15);
347 ch = russian_chars[(yy >> bit) & 077];
351 ch = latin_chars[(yy >> bit) & 077];
359 if (ch == '0' || ch == ' ')
365 pos = (pos+1) & 0177;
370 static void run(void)
379 int op = (w >> 30) & 0177; // Operation code
380 int ax = (w >> 28) & 3; // Address extensions not supported
381 int ix = (w >> 24) & 15; // Indexing
382 int x = (w >> 12) & 07777; // Operands (original form)
384 int xi=x, yi=y; // (indexed form)
386 printf("@%04o %c%02o %02o %04o %04o\n",
388 (w & SIGN_MASK) ? '-' : '+',
389 (int)((w >> 30) & 077),
390 (int)((w >> 24) & 077),
398 xi = (xi + (int)((i >> 12) & 07777)) & 07777;
399 yi = (yi + (int)(i & 07777)) & 07777;
401 printf("\tIndexing -> %04o %04o\n", xi, yi);
406 /* Arithmetic operations */
409 long long aa, bb, cc;
413 auto void afetch(void);
423 auto void astore(word result);
424 void astore(word result)
437 case 004 ... 007: // XOR
441 case 010 ... 013: // FIX addition
443 cc = wtoll(a) + wtoll(b);
448 case 014 ... 017: // FP addition
450 case 020 ... 023: // FIX subtraction
452 cc = wtoll(a) - wtoll(b);
457 case 024 ... 027: // FP subtraction
459 case 030 ... 033: // FIX multiplication
461 // XXX: We ignore the rounding mode settings
462 cd = wtofrac(a) * wtofrac(b);
463 astore(wfromfrac(cd));
465 case 034 ... 037: // FP multiplication
467 case 040 ... 043: // division
472 stop("DIVISION BY ZERO");
474 if (!fracinrange(cd))
476 astore(wfromfrac(cd));
478 case 044 ... 047: // FP division
480 case 050 ... 053: // FIX subtraction of abs values
482 cc = wabs(a) - wabs(b);
487 case 054 ... 057: // FP subtraction of abs values
489 case 060 ... 063: // Shift logical
492 if (i <= -37 || i >= 37)
495 astore((a << i) & WORD_MASK);
499 case 064 ... 067: // Shift arithmetical
503 if (i <= -36 || i >= 36)
506 cc = (aa << i) & VAL_MASK;
509 astore((a & SIGN_MASK) | wfromll(cc));
511 case 070 ... 073: // And
515 case 074 ... 077: // Or
524 case 0103: // I/O magtape
526 case 0104: // Disable rounding
528 case 0105: // Enable rounding
530 case 0106: // Interrupt control
532 case 0107: // Reverse tape
535 wr(yi, r1 = acc = rd(xi));
537 case 0111: // Move negative
538 wr(yi, acc = (r1 = rd(xi)) ^ SIGN_MASK);
540 case 0112: // Move absolute value
541 wr(yi, acc = (r1 = rd(xi)) & VAL_MASK);
543 case 0113: // Read from keyboard
545 case 0114: // Copy sign
546 wr(yi, acc = rd(yi) ^ ((r1 = rd(xi)) & SIGN_MASK));
548 case 0115: // Read code from R1 (obscure)
550 case 0116: // Copy exponent
552 case 0117: // I/O teletype
558 aa = (a >> 24) & 017777;
561 b = rd(y); // (a mountain range near Prague)
562 acc = ((aa-1) << 24) |
563 (((((a >> 12) & 07777) + (b >> 12) & 07777) & 07777) << 12) |
564 (((a & 07777) + (b & 07777)) & 07777);
572 case 0131: // Jump to subroutine
573 wr(y, acc = ((030ULL << 30) | ((ip & 07777ULL) << 12)));
576 case 0132: // Jump if positive
582 case 0133: // Jump if overflow
583 // Since we always trap on overflow, this instruction always jumps to the 1st address
586 case 0134: // Jump if zero
592 case 0135: // Jump if key pressed
593 // No keys are ever pressed, so always jump to 2nd
596 case 0136: // Interrupt masking
598 case 0137: // Used only when reading from tape
600 case 0140 ... 0147: // I/O
602 case 0150 ... 0154: // I/O
604 case 0160 ... 0161: // I/O
606 case 0162: // Printing
611 case 0170: // FIX multiplication, bottom part
613 if (wtofrac(a) * wtofrac(b) >= .1/(1ULL << 32))
615 acc = wfromll(((unsigned long long)wabs(a) * (unsigned long long)wabs(b)) & VAL_MASK);
616 // XXX: What should be the sign? The book does not define that.
623 stop("DIVISION BY ZERO");
629 case 0172: // Add exponents
631 case 0173: // Sub exponents
633 case 0174: // Addition in one's complement
640 // XXX: The effect on the accumulator is undocumented, but likely to be as follows:
643 case 0175: // Normalization
645 case 0176: // Population count
648 for (int i=0; i<36; i++)
651 // XXX: Guessing that acc gets a copy of the result
661 printf("\tACC:%c%012llo R1:%c%012llo R2:%c%012llo Z:%d\n", WF(acc), WF(r1), WF(r2), flag_zero);