#include #include #include int d, C, Z, I, D, B, V, S, i = 0xC000, t, s, o, h, z, e, w, n=0xFFFF; unsigned char *p, m[65536], a, x, y, k, *u, *j = " ./ p/ 7 ] . 6 6 p t7 " "r(0)1*+2,4WgcovGn^f_NVO>F?T\\swldiHZYI9QJ" "RCKSL[b<% <% '< <% +< <% 7$ ($A @$A ($A &$ $A *$ $A" " C2 2; =2; 62; '2 2; +2 2; D# #B ?#B 6#B &# #B *# #B" " F HFG 1 L HFG 'F HFG NFM F :89 :89 J8I :89 &8 :89 +8K :89" " ., .,/ 5,0 .,/ ', ,/ +, ,/ -E -E3 4E -E3 &E E3 *E E3" /* A data table (used for calculating addressing modes) */ " 2133024425660788"; R (x) { /* Update the Zero and Sign flags */ Z = x ? 0 : 2; S = x & 0x80; } K (x) { /* Update flags after a subtract/compare operation (Zero, Sign and Carry) */ R (x - *p); C = x < *p ? 0 : 1; } A () { /* Load and decode the flags from stack */ R (t = m[n & ++k + 0x100]); C = t & 1; Z = t & 2; I = t & 4; D = t & 8; B = t & 16; V = t & 64; } X () { /* Push the program pointer (d) into stack (used in JSR and interrupts) */ m[n & k-- + 0x100] = d / 0x100; m[n & k-- + 0x100] = d; } N () { X (); /* Encode and push the flags into stack */ m[n & k-- + 0x100] = C | Z | I | D | B | V | S | 32; /* Goto interrupt handler */ I = 4; d = (t = n - 1, d += 0, m[n & t] + m[n & t + 1] * 0x100); } main (int c, char *v[]) { FILE *g; m[n & z] = time (0); /* Random number */ g = fopen (c > 1 ? v[1] : "", j + 32); t = i; if (g) {; /* Load the ROM */ for (; (s = fgetc (g)) + 1; m[n & t++] = s); w = t & n; d = w ? i : (t = n - 3, d += C, m[n & t] + m[n & t + 1] * 0x100); z = c > 2 ? atoi (v[2]) + 1 : 4; c = z ? n * z / 4 : n; nodelay (initscr (), 1); curs_set (I); cbreak (); noecho (); for (;;) { o++ % (c * 4) ? h : (h = getch (), h - ERR ? (m[n & 162] = h | 0x80) : 0); if (!w) { s = (m[n & 59408] |= z ? 0x80 : 0) & 15; m[n & 59410] = ~(h - ERR ? (s ^ 8 ? 0 : j[h | 0x80] & 1) | (j[h] / 8 - 5 - s ? 0 : 1 << j[h] % 8) : 0); } o % c | I || N (); p = m + 0x100 + 0x100 + m[n & 0x80 - 9]; t = d % 65493; /* Load opcode */ i = m[n & d++]; if (w + t < 4) { if (*p && (u = strchr (++p, 34))) { *u = 0; m[n & 0x80 - 9] = u - m + 1; if (g = fopen (p, t ? j + 61 : j + 32)) { i = 1025 - t; if (!t) { (s = fgetc (g)); for ((s = fgetc (g)); (s = fgetc (g)) + 1; m[n & i++] = s); for (p = m + 42; p < m + 47;) { *p++ = i % 0x100; *p++ = i / 0x100; } } for (; t; fputc (m[n & i], g)) t = m[n & ++i] ? 3 : t - 1; fclose (g); } } i = 96; } /* Decode the addressing mode of this opcode, and calculate it */ t = j[i / 2 & 14 | i & 1 | 0x100 + 0x100] & 15; e = t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? (t = d, d += 2, m[n & t] + m[n & t + 1] * 0x100 ) + (i - 190 ? x : y) : (t = d, d += 2, m[n & t] + m[n & t + 1] * 0x100 ) + y : 0xFF & m[n & d++] + (i - 150 && i - 182 ? x : y) : (t = m[n & d], d += 1, m[n & t] + m[n & t + 1] * 0x100 ) + y : (t = d, d += 2, m[n & t] + m[n & t + 1] * 0x100 ) : m[n & d++] : d++ : (t = m[n & d] + x & 0xFF, d += 1, m[n & t] + m[n & t + 1] * 0x100) : &a - m; p = e + m; s = i >> 6; /* Decode the function of this opcode, and execute it */ t = j[i + 0x100] - 35; t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? t-- ? 0 : R (a = y) : (k = x) : (R (a = x)) : (R (x = k)) : (R (y = a)) : (R (x = a)) : (*p = y) : (*p = x) : (*p = a) : (t = a - *p - 1 + C, C = t & 0x100 ? 0 : 1, V = ((a ^ *p) & (a ^ t) & 0x80) / 2, R (a = t & 0xFF)) : (d = m[n & ++k + 0x100], d += m[n & ++k + 0x100] * 0x100 + 1) : (A (), d = m[n & ++k + 0x100], d |= m[n & ++k + 0x100] * 0x100) : (t = *p, R (*p = *p / 2 | C * 0x80), C = t & 1) : (t = * p, R (*p = *p * 2 | C), C = t / 0x80) : A () : R (a = m[n & ++k + 0x100]) : (m[n & k-- + 0x100] = C | Z | I | D | B | V | S | 32) : (m[n & k-- + 0x100] = a) : (R (a |= *p)) : (C = *p & 1, R (*p /= 2)) : (R (y = *p)) : R (x = *p) : R (a = *p) : (s = (t = --d, d += 1, m[n & t] + m[n & t + 1] * 0x100), X (), d = s) : (d = i & 32 ? (t = e, d += 0, m[n & t] + m[n & t + 1] * 0x100) : e) : R (++y) : R (++x) : R (++*p) : R (a ^= *p) : R (--y) : R (--x) : R (--*p) : K (y) : K (x) : K (a) : (*(s ? s - 1 ? s - 2 ? &D : &V : &I : &C) = 0) : (*(s ? s - 1 ? s - 2 ? &D : &V : &I : &C) = (s ? s - 1 ? s - 2 ? 8 : 8 : 4 : 1)) : (d--, B = 16, N ()) : (R (a & *p), V = *p & 64, S = *p & 0x80) : !(s ? s - 1 ? s - 2 ? Z : C : V : S) && (d += *p & 0x80 ? *p - 0x100 : *p) : (s ? s - 1 ? s - 2 ? Z : C : V : S) && (d += *p & 0x80 ? *p - 0x100 : *p) : (C = *p / 0x80, R (*p *= 2)) : R (a &= *p) : (t = a + *p + C, C = t & 0x100 ? 1 : 0, V = (~(a ^ *p) & (a ^ t) & 0x80) / 2, R (a = t & 0xFF)); t = e ^ 0x100 * 0x100 / 2; if (t < 1000) { s = *p % 0x80; i = A_REVERSE; *p > s ? attron (i) : attroff (i); mvaddch (t / 40, t % 40, s + w < 32 ? s + 64 : s > 95 + w ? s - 32 : s); } } } perror (*v); }