4 * (c) 2010 Martin Mares <mj@ucw.cz>
11 * - debugging/play mode
12 * - implement printing
14 * - we probably have to disable NOP
22 // Minsk-2 has 37-bit words in sign-magnitude representation (bit 36 = sign)
23 typedef unsigned long long int word;
25 #define WORD_MASK 01777777777777ULL
26 #define SIGN_MASK 01000000000000ULL
27 #define VAL_MASK 00777777777777ULL
29 static int wsign(word w)
31 return (w & SIGN_MASK) ? -1 : 1;
34 static word wabs(word w)
39 #define WF(w) (wsign(w) < 0 ? '-' : '+'), wabs(w)
41 static long long wtoll(word w)
49 static word wfromll(long long x)
51 word w = ((x < 0) ? -x : x) & VAL_MASK;
57 static double wtofrac(word w)
59 return (double)wtoll(w) / (double)(1ULL << 36);
62 static word wfromfrac(double d)
64 return wfromll((long long)(d * (double)(1ULL << 36)));
67 static int inrange(long long x)
69 return (x >= -(long long)VAL_MASK && x <= (long long)VAL_MASK);
72 static int fracinrange(double d)
74 return (d > -1. && d < 1.);
77 static int wexp(word w)
80 return (w & 0100 ? -exp : exp);
83 static word mem[4096];
85 static word rd(int addr)
87 word val = addr ? mem[addr] : 0;
88 printf("\tRD %04o = %c%012llo\n", addr, WF(val));
92 static void wr(int addr, word val)
94 assert(!(val & ~(WORD_MASK)));
95 printf("\tWR %04o = %c%012llo\n", addr, WF(val));
101 static void parse_error(char *msg)
103 printf("Ошибка входа (стр. %d): %s\n", lino, msg);
107 static void parse_in(void)
112 while (fgets(line, sizeof(line), stdin))
115 char *eol = strchr(line, '\n');
117 parse_error("Строка слишком долгая");
125 for (int i=0; i<4; i++)
129 if (*c >= '0' && *c <= '7')
130 addr = 8*addr + *c++ - '0';
132 parse_error("Плохая цифва");
137 parse_error("Адрес слишком долгий");
145 parse_error("Плохой знак");
147 for (int i=0; i<12; i++)
151 if (*c >= '0' && *c <= '7')
152 w = 8*w + *c++ - '0';
154 parse_error("Плохая цифва");
159 parse_error("Номер слишком долгий");
166 static word r1, r2, current_ins;
167 static int flag_zero = 0;
168 static int ip = 00050; // Standard program start location
171 static void stop(char *reason)
173 printf("MACHINE STOPPED -- %s\n", reason);
174 printf("IP:%04o ACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", prev_ip, WF(acc), WF(r1), WF(r2));
178 static void over(void)
183 static void nofpu(void)
189 static void notimp(void)
192 stop("NOT IMPLEMENTED");
195 static void noins(void)
198 stop("ILLEGAL INSTRUCTION");
201 static void run(void)
210 int op = (w >> 30) & 0177; // Operation code
211 int ax = (w >> 28) & 3; // Address extensions not supported
212 int ix = (w >> 24) & 15; // Indexing
213 int x = (w >> 12) & 07777; // Operands (original form)
215 int xi=x, yi=y; // (indexed form)
216 printf("@%04o %c%02o %02o %04o %04o\n",
218 (w & SIGN_MASK) ? '-' : '+',
219 (int)((w >> 30) & 077),
220 (int)((w >> 24) & 077),
228 xi = (xi + (int)((i >> 12) & 07777)) & 07777;
229 yi = (yi + (int)(i & 07777)) & 07777;
230 printf("\tIndexing -> %04o %04o\n", xi, yi);
235 /* Arithmetic operations */
238 long long aa, bb, cc;
242 auto void afetch(void);
252 auto void astore(word result);
253 void astore(word result)
266 case 004 ... 007: // XOR
270 case 010 ... 013: // FIX addition
272 cc = wtoll(a) + wtoll(b);
277 case 014 ... 017: // FP addition
279 case 020 ... 023: // FIX subtraction
281 cc = wtoll(a) - wtoll(b);
286 case 024 ... 027: // FP subtraction
288 case 030 ... 033: // FIX multiplication
290 // XXX: We ignore the rounding mode settings
291 cd = wtofrac(a) * wtofrac(b);
292 astore(wfromfrac(cd));
294 case 034 ... 037: // FP multiplication
296 case 040 ... 043: // division
301 stop("DIVISION BY ZERO");
303 if (!fracinrange(cd))
305 astore(wfromfrac(cd));
307 case 044 ... 047: // FP division
309 case 050 ... 053: // FIX subtraction of abs values
311 cc = wabs(a) - wabs(b);
316 case 054 ... 057: // FP subtraction of abs values
318 case 060 ... 063: // Shift logical
321 if (i <= -37 || i >= 37)
324 astore((a << i) & WORD_MASK);
328 case 064 ... 067: // Shift arithmetical
332 if (i <= -36 || i >= 36)
335 cc = (aa << i) & VAL_MASK;
338 astore((a & SIGN_MASK) | wfromll(cc));
340 case 070 ... 073: // And
344 case 074 ... 077: // Or
353 case 0103: // I/O magtape
355 case 0104: // Disable rounding
357 case 0105: // Enable rounding
359 case 0106: // Interrupt control
361 case 0107: // Reverse tape
364 wr(yi, r1 = acc = rd(xi));
366 case 0111: // Move negative
367 wr(yi, acc = (r1 = rd(xi)) ^ SIGN_MASK);
369 case 0112: // Move absolute value
370 wr(yi, acc = (r1 = rd(xi)) & VAL_MASK);
372 case 0113: // Read from keyboard
374 case 0114: // Copy sign
375 wr(yi, acc = rd(yi) ^ ((r1 = rd(xi)) & SIGN_MASK));
377 case 0115: // Read code from R1 (obscure)
379 case 0116: // Copy exponent
381 case 0117: // I/O teletype
387 aa = (a >> 24) & 017777;
390 b = rd(y); // (a mountain range near Prague)
391 acc = ((aa-1) << 24) |
392 (((((a >> 12) & 07777) + (b >> 12) & 07777) & 07777) << 12) |
393 (((a & 07777) + (b & 07777)) & 07777);
401 case 0131: // Jump to subroutine
402 wr(y, (030ULL << 30) | ((ip & 07777ULL) << 12));
405 case 0132: // Jump if positive
411 case 0133: // Jump if overflow
412 // Since we always trap on overflow, this instruction always jumps to the 1st address
415 case 0134: // Jump if zero
421 case 0135: // Jump if key pressed
422 // No keys are ever pressed, so always jump to 2nd
425 case 0136: // Interrupt masking
427 case 0137: // Used only when reading from tape
429 case 0140 ... 0147: // I/O
431 case 0150 ... 0154: // I/O
433 case 0160 ... 0163: // I/O
435 case 0170: // FIX multiplication, bottom part
437 if (wtofrac(a) * wtofrac(b) >= .1/(1ULL << 32))
439 acc = wfromll(((unsigned long long)wabs(a) * (unsigned long long)wabs(b)) & VAL_MASK);
440 // XXX: What should be the sign? The book does not define that.
447 stop("DIVISION BY ZERO");
453 case 0172: // Add exponents
455 case 0173: // Sub exponents
457 case 0174: // Addition in one's complement
464 // XXX: The effect on the accumulator is undocumented, but likely to be as follows:
467 case 0175: // Normalization
469 case 0176: // Population count
472 for (int i=0; i<36; i++)
475 // XXX: Guessing that acc gets a copy of the result
484 printf("\tACC:%c%012llo R1:%c%012llo R2:%c%012llo Z:%d\n", WF(acc), WF(r1), WF(r2), flag_zero);