4 * (c) 2010 Martin Mares <mj@ucw.cz>
11 * - debugging/play mode
13 * - which instructions should modify the accumulator?
14 * - implement printing
16 * - simulate R1 and R2?
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 word mem[4096];
81 static word rd(int addr)
83 word val = addr ? mem[addr] : 0;
84 printf("\tRD %04o = %c%012llo\n", addr, WF(val));
88 static void wr(int addr, word val)
90 assert(!(val & ~(WORD_MASK)));
91 printf("\tWR %04o = %c%012llo\n", addr, WF(val));
97 static void parse_error(char *msg)
99 printf("Ошибка входа (стр. %d): %s\n", lino, msg);
103 static void parse_in(void)
108 while (fgets(line, sizeof(line), stdin))
111 char *eol = strchr(line, '\n');
113 parse_error("Строка слишком долгая");
121 for (int i=0; i<4; i++)
125 if (*c >= '0' && *c <= '7')
126 addr = 8*addr + *c++ - '0';
128 parse_error("Плохая цифва");
133 parse_error("Адрес слишком долгий");
141 parse_error("Плохой знак");
143 for (int i=0; i<12; i++)
147 if (*c >= '0' && *c <= '7')
148 w = 8*w + *c++ - '0';
150 parse_error("Плохая цифва");
155 parse_error("Номер слишком долгий");
161 static word acc, prev_acc;
162 static int flag_zero = 0;
163 static int ip = 00050; // Standard program start location
166 static void stop(char *reason)
168 printf("MACHINE STOPPED -- %s\n", reason);
169 printf("IP:%04o ACC:%c%012llo\n", prev_ip, WF(acc));
173 static void over(void)
178 static void nofpu(void)
183 static void notimp(void)
185 stop("NOT IMPLEMENTED");
188 static void noins(void)
190 stop("ILLEGAL INSTRUCTION");
193 static void run(void)
201 int op = (w >> 30) & 0177; // Operation code
202 int ax = (w >> 28) & 3; // Address extensions not supported
203 int ix = (w >> 24) & 15; // Indexing
204 int x = (w >> 12) & 07777; // Operands (original form)
206 int xi=x, yi=y; // (indexed form)
207 printf("@%04o %c%02o %02o %04o %04o\n",
209 (w & SIGN_MASK) ? '-' : '+',
210 (int)((w >> 30) & 077),
211 (int)((w >> 24) & 077),
219 xi = (xi + (int)((i >> 12) & 07777)) & 07777;
220 yi = (yi + (int)(i & 07777)) & 07777;
221 printf("\tIndexing -> %04o %04o\n", xi, yi);
226 /* Arithmetic operations */
229 long long aa, bb, cc;
232 auto void afetch(void);
242 auto void astore(word result);
243 void astore(word result)
256 case 004 ... 007: // XOR
260 case 010 ... 013: // FIX addition
262 cc = wtoll(a) + wtoll(b);
267 case 014 ... 017: // FP addition
269 case 020 ... 023: // FIX subtraction
271 cc = wtoll(a) - wtoll(b);
276 case 024 ... 027: // FP subtraction
278 case 030 ... 033: // FIX multiplication
280 cd = wtofrac(a) * wtofrac(b);
282 astore(wfromfrac(cd));
284 case 034 ... 037: // FP multiplication
286 case 040 ... 043: // division
291 stop("DIVISION BY ZERO");
293 if (!fracinrange(cd))
295 astore(wfromfrac(cd));
297 case 044 ... 047: // FP division
299 case 050 ... 053: // subtraction of abs values
301 cc = wabs(a) - wabs(b);
306 case 054 ... 057: // FP subtraction of abs values
308 case 060 ... 063: // Shift logical
310 case 064 ... 067: // Shift arithmetical
312 case 070 ... 073: // And
316 case 074 ... 077: // Or
325 case 0103: // I/O magtape
327 case 0104: // Disable rounding
329 case 0105: // Enable rounding
331 case 0106: // Interrupt control
333 case 0107: // Reverse tape
338 case 0111: // Move negative
339 wr(yi, rd(xi) ^ SIGN_MASK);
341 case 0112: // Move absolute value
342 wr(yi, rd(xi) & VAL_MASK);
344 case 0113: // Read from keyboard
346 case 0114: // Copy sign
347 wr(yi, rd(yi) ^ (rd(xi) & SIGN_MASK));
349 case 0115: // Read code from R1 (obscure)
351 case 0116: // Copy exponent
353 case 0117: // I/O teletype
359 aa = (a >> 24) & 017777;
362 b = rd(y); // (a mountain range near Prague)
363 wr(ix, ((aa-1) << 24) |
364 (((((a >> 12) & 07777) + (b >> 12) & 07777) & 07777) << 12) |
365 (((a & 07777) + (b & 07777)) & 07777));
372 case 0131: // Jump to subroutine
373 wr(y, (030ULL << 30) | ((ip & 07777ULL) << 12));
376 case 0132: // Jump if positive
377 if (wsign(prev_acc) >= 0)
382 case 0133: // Jump if overflow
383 // Since we always trap on overflow, this instruction always jumps to the 1st address
386 case 0134: // Jump if zero
392 case 0135: // Jump if key pressed
393 // No keys are ever pressed, so always jump to 2nd
396 case 0136: // Interrupt masking
398 case 0137: // Used only when reading from tape
400 case 0140 ... 0147: // I/O
402 case 0150 ... 0154: // I/O
404 case 0160 ... 0163: // I/O
406 case 0170: // FIX multiplication, bottom part
408 acc = wfromll(((unsigned long long)wabs(a) * (unsigned long long)wabs(b)) & VAL_MASK);
409 // XXX: What should be the sign? The book does not define that.
416 stop("DIVISION BY ZERO");
422 case 0172: // Add exponents
424 case 0173: // Sub exponents
426 case 0174: // Addition in one's complement
432 // XXX: What happens with the accumulator?
435 case 0175: // Normalization
437 case 0176: // Population count
440 for (int i=0; i<36; i++)
450 printf("\tACC:%c%012llo Z:%d\n", WF(acc), flag_zero);