4 * (c) 2010 Martin Mares <mj@ucw.cz>
11 * - debugging/play mode
12 * - implement printing
14 * - we probably have to disable NOP
24 // Minsk-2 has 37-bit words in sign-magnitude representation (bit 36 = sign)
25 typedef unsigned long long int word;
27 #define WORD_MASK 01777777777777ULL
28 #define SIGN_MASK 01000000000000ULL
29 #define VAL_MASK 00777777777777ULL
31 static int wsign(word w)
33 return (w & SIGN_MASK) ? -1 : 1;
36 static word wabs(word w)
41 #define WF(w) (wsign(w) < 0 ? '-' : '+'), wabs(w)
43 static long long wtoll(word w)
51 static word wfromll(long long x)
53 word w = ((x < 0) ? -x : x) & VAL_MASK;
59 static double wtofrac(word w)
61 return (double)wtoll(w) / (double)(1ULL << 36);
64 static word wfromfrac(double d)
66 return wfromll((long long)(d * (double)(1ULL << 36)));
69 static int inrange(long long x)
71 return (x >= -(long long)VAL_MASK && x <= (long long)VAL_MASK);
74 static int fracinrange(double d)
76 return (d > -1. && d < 1.);
79 static int wexp(word w)
82 return (w & 0100 ? -exp : exp);
85 static word mem[4096];
87 static word rd(int addr)
89 word val = addr ? mem[addr] : 0;
91 printf("\tRD %04o = %c%012llo\n", addr, WF(val));
95 static void wr(int addr, word val)
97 assert(!(val & ~(WORD_MASK)));
99 printf("\tWR %04o = %c%012llo\n", addr, WF(val));
105 static void parse_error(char *msg)
107 printf("Ошибка входа (стр. %d): %s\n", lino, msg);
111 static void parse_in(void)
116 while (fgets(line, sizeof(line), stdin))
119 char *eol = strchr(line, '\n');
121 parse_error("Строка слишком долгая");
129 for (int i=0; i<4; i++)
133 if (*c >= '0' && *c <= '7')
134 addr = 8*addr + *c++ - '0';
136 parse_error("Плохая цифва");
141 parse_error("Адрес слишком долгий");
149 parse_error("Плохой знак");
151 for (int i=0; i<12; i++)
155 if (*c >= '0' && *c <= '7')
156 w = 8*w + *c++ - '0';
158 parse_error("Плохая цифва");
163 parse_error("Номер слишком долгий");
170 static word r1, r2, current_ins;
171 static int flag_zero = 0;
172 static int ip = 00050; // Standard program start location
175 static void stop(char *reason)
177 printf("MACHINE STOPPED -- %s\n", reason);
178 printf("IP:%04o ACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", prev_ip, WF(acc), WF(r1), WF(r2));
182 static void over(void)
187 static void nofpu(void)
193 static void notimp(void)
196 stop("NOT IMPLEMENTED");
199 static void noins(void)
202 stop("ILLEGAL INSTRUCTION");
205 static void run(void)
214 int op = (w >> 30) & 0177; // Operation code
215 int ax = (w >> 28) & 3; // Address extensions not supported
216 int ix = (w >> 24) & 15; // Indexing
217 int x = (w >> 12) & 07777; // Operands (original form)
219 int xi=x, yi=y; // (indexed form)
221 printf("@%04o %c%02o %02o %04o %04o\n",
223 (w & SIGN_MASK) ? '-' : '+',
224 (int)((w >> 30) & 077),
225 (int)((w >> 24) & 077),
233 xi = (xi + (int)((i >> 12) & 07777)) & 07777;
234 yi = (yi + (int)(i & 07777)) & 07777;
236 printf("\tIndexing -> %04o %04o\n", xi, yi);
241 /* Arithmetic operations */
244 long long aa, bb, cc;
248 auto void afetch(void);
258 auto void astore(word result);
259 void astore(word result)
272 case 004 ... 007: // XOR
276 case 010 ... 013: // FIX addition
278 cc = wtoll(a) + wtoll(b);
283 case 014 ... 017: // FP addition
285 case 020 ... 023: // FIX subtraction
287 cc = wtoll(a) - wtoll(b);
292 case 024 ... 027: // FP subtraction
294 case 030 ... 033: // FIX multiplication
296 // XXX: We ignore the rounding mode settings
297 cd = wtofrac(a) * wtofrac(b);
298 astore(wfromfrac(cd));
300 case 034 ... 037: // FP multiplication
302 case 040 ... 043: // division
307 stop("DIVISION BY ZERO");
309 if (!fracinrange(cd))
311 astore(wfromfrac(cd));
313 case 044 ... 047: // FP division
315 case 050 ... 053: // FIX subtraction of abs values
317 cc = wabs(a) - wabs(b);
322 case 054 ... 057: // FP subtraction of abs values
324 case 060 ... 063: // Shift logical
327 if (i <= -37 || i >= 37)
330 astore((a << i) & WORD_MASK);
334 case 064 ... 067: // Shift arithmetical
338 if (i <= -36 || i >= 36)
341 cc = (aa << i) & VAL_MASK;
344 astore((a & SIGN_MASK) | wfromll(cc));
346 case 070 ... 073: // And
350 case 074 ... 077: // Or
359 case 0103: // I/O magtape
361 case 0104: // Disable rounding
363 case 0105: // Enable rounding
365 case 0106: // Interrupt control
367 case 0107: // Reverse tape
370 wr(yi, r1 = acc = rd(xi));
372 case 0111: // Move negative
373 wr(yi, acc = (r1 = rd(xi)) ^ SIGN_MASK);
375 case 0112: // Move absolute value
376 wr(yi, acc = (r1 = rd(xi)) & VAL_MASK);
378 case 0113: // Read from keyboard
380 case 0114: // Copy sign
381 wr(yi, acc = rd(yi) ^ ((r1 = rd(xi)) & SIGN_MASK));
383 case 0115: // Read code from R1 (obscure)
385 case 0116: // Copy exponent
387 case 0117: // I/O teletype
393 aa = (a >> 24) & 017777;
396 b = rd(y); // (a mountain range near Prague)
397 acc = ((aa-1) << 24) |
398 (((((a >> 12) & 07777) + (b >> 12) & 07777) & 07777) << 12) |
399 (((a & 07777) + (b & 07777)) & 07777);
407 case 0131: // Jump to subroutine
408 wr(y, (030ULL << 30) | ((ip & 07777ULL) << 12));
411 case 0132: // Jump if positive
417 case 0133: // Jump if overflow
418 // Since we always trap on overflow, this instruction always jumps to the 1st address
421 case 0134: // Jump if zero
427 case 0135: // Jump if key pressed
428 // No keys are ever pressed, so always jump to 2nd
431 case 0136: // Interrupt masking
433 case 0137: // Used only when reading from tape
435 case 0140 ... 0147: // I/O
437 case 0150 ... 0154: // I/O
439 case 0160 ... 0163: // I/O
441 case 0170: // FIX multiplication, bottom part
443 if (wtofrac(a) * wtofrac(b) >= .1/(1ULL << 32))
445 acc = wfromll(((unsigned long long)wabs(a) * (unsigned long long)wabs(b)) & VAL_MASK);
446 // XXX: What should be the sign? The book does not define that.
453 stop("DIVISION BY ZERO");
459 case 0172: // Add exponents
461 case 0173: // Sub exponents
463 case 0174: // Addition in one's complement
470 // XXX: The effect on the accumulator is undocumented, but likely to be as follows:
473 case 0175: // Normalization
475 case 0176: // Population count
478 for (int i=0; i<36; i++)
481 // XXX: Guessing that acc gets a copy of the result
491 printf("\tACC:%c%012llo R1:%c%012llo R2:%c%012llo Z:%d\n", WF(acc), WF(r1), WF(r2), flag_zero);