fork(1) download
  1. import java.io.*;
  2. import java.lang.*;
  3. import java.util.*;
  4.  
  5. class Memory extends java.io.OutputStream
  6. {
  7. public final java.io.DataOutputStream out;
  8. public final java.io.DataInputStream in;
  9. public final byte[] mem;
  10. private int pos = 0;
  11.  
  12. public Memory(int size)
  13. {
  14. mem = new byte[size];
  15. out = new java.io.DataOutputStream(this);
  16. in = new java.io.DataInputStream(new java.io.ByteArrayInputStream(mem));
  17. }
  18.  
  19. public int getOutPos()
  20. { return pos; }
  21.  
  22. public void setPos(int pos) throws java.io.IOException
  23. { this.pos = pos; in.reset(); in.skipBytes(pos); }
  24.  
  25. public void setOutPos(int pos)
  26. { this.pos = pos; }
  27.  
  28. public void setInPos(int pos) throws java.io.IOException
  29. { in.reset(); in.skipBytes(pos); }
  30.  
  31. @Override public void write(int b) throws java.io.IOException
  32. { mem[pos] = (byte)b; pos++; }
  33. @Override public void write(byte[] b, int off, int len) throws java.io.IOException
  34. { while (len-- > 0) { mem[pos++] = b[off++]; } }
  35. @Override public void write(byte[] b) throws java.io.IOException
  36. { int off = 0, len = b.length; while (len-- > 0) { mem[pos++] = b[off++]; } }
  37. }
  38.  
  39. class CometII
  40. {
  41.  
  42. java.io.PrintStream cOut;
  43. java.io.BufferedReader cIn;
  44.  
  45. final Memory memory;
  46.  
  47. int GR0, GR1, GR2, GR3, GR4, GR5, GR6, GR7; // あえて配列にせず
  48. int SP, PR, FR, OFF, CA;
  49.  
  50. java.lang.String op = "NOP";
  51.  
  52. public CometII(byte[] pg) throws java.lang.Exception
  53. {
  54. memory = new Memory(0xFFFF << 1);
  55. PR = FR = 0; SP = 0xFFFF;
  56. OFF = CA = 0;
  57. cOut = java.lang.System.out;
  58. cIn = new java.io.BufferedReader(new java.io.InputStreamReader(java.lang.System.in));
  59. if (pg != null) {
  60. memory.out.write(pg);
  61. memory.setPos(PR);
  62. }
  63. }
  64.  
  65. @Override
  66. public String toString()
  67. {
  68. return "Last OP: " + op + "\n"
  69. + "PR: " + PR + ", FR: " + FR + ", SP: " + SP + "\n"
  70. + "GR0: " + GR0 + ", GR1: " + GR1 + ", GR2: " + GR2 + ", GR3: " + GR3
  71. + ", GR4: " + GR4 + ", GR5: " + GR5 + ", GR6: " + GR6 + ", GR7: " + GR7;
  72.  
  73. }
  74.  
  75. private int flag0a(int v)
  76. {
  77. if (v == 0) {
  78. FR = 0x4;
  79. } else {
  80. if ((v & 0x8000) == 0) {
  81. FR = 0x0;
  82. } else {
  83. FR = 0x2;
  84. }
  85. if (v > 0x7FFF || v < 0) {
  86. FR |= 0x1;
  87. }
  88. }
  89. return v & 0xFFFF;
  90. }
  91.  
  92. private int flag0l(int v)
  93. {
  94. if (v == 0) {
  95. FR = 0x4;
  96. } else if (v > 0xFFFF || v < 0) {
  97. FR = 0x1;
  98. } else {
  99. FR = 0x0;
  100. }
  101. return v & 0xFFFF;
  102. }
  103.  
  104. private void flag1(int v)
  105. {
  106. if (v == 0) {
  107. FR = 0x4;
  108. } else if ((v & 0x8000) == 0) {
  109. FR = 0x0;
  110. } else {
  111. FR = 0x2;
  112. }
  113. }
  114.  
  115. private void flag2a(int ca, int v)
  116. {
  117. if (v == 0) {
  118. FR = 0x4;
  119. } else if ((v & 0x8000) == 0) {
  120. FR = 0;
  121. } else {
  122. FR = 0x2;
  123. }
  124. if (ca != 0) {
  125. FR |= 0x1;
  126. }
  127.  
  128. }
  129.  
  130. private void flag2l(int ca, int v)
  131. {
  132. if (v == 0) {
  133. FR = 0x4;
  134. } else {
  135. FR = 0x0;
  136. }
  137. if (ca != 0) {
  138. FR |= 0x1;
  139. }
  140. }
  141.  
  142. private int calcAddr(java.io.DataInput in, int code) throws java.lang.Exception
  143. {
  144. int adr = in.readShort(); PR++;
  145. switch (code & 0xF) {
  146. case 0x0: break;
  147. case 0x1: adr += GR1; break;
  148. case 0x2: adr += GR2; break;
  149. case 0x3: adr += GR3; break;
  150. case 0x4: adr += GR4; break;
  151. case 0x5: adr += GR5; break;
  152. case 0x6: adr += GR6; break;
  153. case 0x7: adr += GR7; break;
  154. default: throw new Exception(op);
  155. }
  156. op += ", addr: " + adr;
  157. return adr;
  158. }
  159.  
  160.  
  161. private int getAddr(java.io.DataInput in, int code) throws java.lang.Exception
  162. {
  163. int adr = calcAddr(in, code);
  164. memory.setPos((adr + OFF) << 1);
  165. adr = in.readShort();
  166. memory.setPos(PR << 1);
  167. op += ", val: " + adr;
  168. return adr;
  169. }
  170.  
  171. private int getReg(int code) throws java.lang.Exception
  172. {
  173. switch (code & 0xF) {
  174. case 0x0: return GR0;
  175. case 0x1: return GR1;
  176. case 0x2: return GR2;
  177. case 0x3: return GR3;
  178. case 0x4: return GR4;
  179. case 0x5: return GR5;
  180. case 0x6: return GR6;
  181. case 0x7: return GR7;
  182. default: throw new Exception(op);
  183. }
  184. }
  185.  
  186. public void step() throws java.lang.Exception
  187. {
  188. memory.setPos(PR << 1);
  189. step(memory.in, memory.out);
  190. }
  191.  
  192. private void step(java.io.DataInput mIn, java.io.DataOutput mOut) throws java.lang.Exception
  193. {
  194. int code = 0xFFFF & (int)mIn.readShort(); PR++;
  195. int adr;
  196. switch (code >> 8) {
  197. case 0x00: op = "NOP: " + Integer.toString(code, 16);
  198. break;
  199. case 0x10: op = "LD: " + Integer.toString(code, 16);
  200. switch ((code & 0xF0) >> 4) {
  201. case 0x0: flag1(GR0 = getAddr(mIn, code)); break;
  202. case 0x1: flag1(GR1 = getAddr(mIn, code)); break;
  203. case 0x2: flag1(GR2 = getAddr(mIn, code)); break;
  204. case 0x3: flag1(GR3 = getAddr(mIn, code)); break;
  205. case 0x4: flag1(GR4 = getAddr(mIn, code)); break;
  206. case 0x5: flag1(GR5 = getAddr(mIn, code)); break;
  207. case 0x6: flag1(GR6 = getAddr(mIn, code)); break;
  208. case 0x7: flag1(GR7 = getAddr(mIn, code)); break;
  209. default: throw new Exception(op);
  210. }
  211. break;
  212. case 0x11: op = "ST: " + Integer.toString(code, 16);
  213. memory.setPos((calcAddr(mIn, code) + OFF) << 1);
  214. switch ((code & 0xF0) >> 4) {
  215. case 0x0: mOut.writeShort(GR0); break;
  216. case 0x1: mOut.writeShort(GR1); break;
  217. case 0x2: mOut.writeShort(GR2); break;
  218. case 0x3: mOut.writeShort(GR3); break;
  219. case 0x4: mOut.writeShort(GR4); break;
  220. case 0x5: mOut.writeShort(GR5); break;
  221. case 0x6: mOut.writeShort(GR6); break;
  222. case 0x7: mOut.writeShort(GR7); break;
  223. default: throw new Exception(op);
  224. }
  225. memory.setPos(PR);
  226. break;
  227. case 0x12: op = "LAD: " + Integer.toString(code, 16);
  228. switch ((code & 0xF0) >> 4) {
  229. case 0x0: GR0 = calcAddr(mIn, code); break;
  230. case 0x1: GR1 = calcAddr(mIn, code); break;
  231. case 0x2: GR2 = calcAddr(mIn, code); break;
  232. case 0x3: GR3 = calcAddr(mIn, code); break;
  233. case 0x4: GR4 = calcAddr(mIn, code); break;
  234. case 0x5: GR5 = calcAddr(mIn, code); break;
  235. case 0x6: GR6 = calcAddr(mIn, code); break;
  236. case 0x7: GR7 = calcAddr(mIn, code); break;
  237. default: throw new Exception(op);
  238. }
  239. break;
  240. case 0x14: op = "LD: " + Integer.toString(code, 16);
  241. switch ((code & 0xF0) >> 4) {
  242. case 0x0: flag1(GR0 = getReg(code)); break;
  243. case 0x1: flag1(GR1 = getReg(code)); break;
  244. case 0x2: flag1(GR2 = getReg(code)); break;
  245. case 0x3: flag1(GR3 = getReg(code)); break;
  246. case 0x4: flag1(GR4 = getReg(code)); break;
  247. case 0x5: flag1(GR5 = getReg(code)); break;
  248. case 0x6: flag1(GR6 = getReg(code)); break;
  249. case 0x7: flag1(GR7 = getReg(code)); break;
  250. default: throw new Exception(op);
  251. }
  252. break;
  253. case 0x20: op = "ADDA: " + Integer.toString(code, 16);
  254. switch ((code & 0xF0) >> 4) {
  255. case 0x0: GR0 = flag0a(GR0 + getAddr(mIn, code)); break;
  256. case 0x1: GR1 = flag0a(GR1 + getAddr(mIn, code)); break;
  257. case 0x2: GR2 = flag0a(GR2 + getAddr(mIn, code)); break;
  258. case 0x3: GR3 = flag0a(GR3 + getAddr(mIn, code)); break;
  259. case 0x4: GR4 = flag0a(GR4 + getAddr(mIn, code)); break;
  260. case 0x5: GR5 = flag0a(GR5 + getAddr(mIn, code)); break;
  261. case 0x6: GR6 = flag0a(GR6 + getAddr(mIn, code)); break;
  262. case 0x7: GR7 = flag0a(GR7 + getAddr(mIn, code)); break;
  263. default: throw new Exception(op);
  264. }
  265. break;
  266. case 0x21: op = "SUBA: " + Integer.toString(code, 16);
  267. switch ((code & 0xF0) >> 4) {
  268. case 0x0: GR0 = flag0a(GR0 - getAddr(mIn, code)); break;
  269. case 0x1: GR1 = flag0a(GR1 - getAddr(mIn, code)); break;
  270. case 0x2: GR2 = flag0a(GR2 - getAddr(mIn, code)); break;
  271. case 0x3: GR3 = flag0a(GR3 - getAddr(mIn, code)); break;
  272. case 0x4: GR4 = flag0a(GR4 - getAddr(mIn, code)); break;
  273. case 0x5: GR5 = flag0a(GR5 - getAddr(mIn, code)); break;
  274. case 0x6: GR6 = flag0a(GR6 - getAddr(mIn, code)); break;
  275. case 0x7: GR7 = flag0a(GR7 - getAddr(mIn, code)); break;
  276. default: throw new Exception(op);
  277. }
  278. break;
  279. case 0x22: op = "ADDL: " + Integer.toString(code, 16);
  280. switch ((code & 0xF0) >> 4) {
  281. case 0x0: GR0 = flag0l(GR0 + getAddr(mIn, code)); break;
  282. case 0x1: GR1 = flag0l(GR1 + getAddr(mIn, code)); break;
  283. case 0x2: GR2 = flag0l(GR2 + getAddr(mIn, code)); break;
  284. case 0x3: GR3 = flag0l(GR3 + getAddr(mIn, code)); break;
  285. case 0x4: GR4 = flag0l(GR4 + getAddr(mIn, code)); break;
  286. case 0x5: GR5 = flag0l(GR5 + getAddr(mIn, code)); break;
  287. case 0x6: GR6 = flag0l(GR6 + getAddr(mIn, code)); break;
  288. case 0x7: GR7 = flag0l(GR7 + getAddr(mIn, code)); break;
  289. default: throw new Exception(op);
  290. }
  291. break;
  292. case 0x23: op = "SUBL: " + Integer.toString(code, 16);
  293. switch ((code & 0xF0) >> 4) {
  294. case 0x0: GR0 = flag0l(GR0 - getAddr(mIn, code)); break;
  295. case 0x1: GR1 = flag0l(GR1 - getAddr(mIn, code)); break;
  296. case 0x2: GR2 = flag0l(GR2 - getAddr(mIn, code)); break;
  297. case 0x3: GR3 = flag0l(GR3 - getAddr(mIn, code)); break;
  298. case 0x4: GR4 = flag0l(GR4 - getAddr(mIn, code)); break;
  299. case 0x5: GR5 = flag0l(GR5 - getAddr(mIn, code)); break;
  300. case 0x6: GR6 = flag0l(GR6 - getAddr(mIn, code)); break;
  301. case 0x7: GR7 = flag0l(GR7 - getAddr(mIn, code)); break;
  302. default: throw new Exception(op);
  303. }
  304. break;
  305. case 0x24: op = "ADDA: " + Integer.toString(code, 16);
  306. switch ((code & 0xF0) >> 4) {
  307. case 0x0: GR0 = flag0a(GR0 + getReg(code)); break;
  308. case 0x1: GR1 = flag0a(GR1 + getReg(code)); break;
  309. case 0x2: GR2 = flag0a(GR2 + getReg(code)); break;
  310. case 0x3: GR3 = flag0a(GR3 + getReg(code)); break;
  311. case 0x4: GR4 = flag0a(GR4 + getReg(code)); break;
  312. case 0x5: GR5 = flag0a(GR5 + getReg(code)); break;
  313. case 0x6: GR6 = flag0a(GR6 + getReg(code)); break;
  314. case 0x7: GR7 = flag0a(GR7 + getReg(code)); break;
  315. default: throw new Exception(op);
  316. }
  317. break;
  318. case 0x25: op = "SUBA: " + Integer.toString(code, 16);
  319. switch ((code & 0xF0) >> 4) {
  320. case 0x0: GR0 = flag0a(GR0 - getReg(code)); break;
  321. case 0x1: GR1 = flag0a(GR1 - getReg(code)); break;
  322. case 0x2: GR2 = flag0a(GR2 - getReg(code)); break;
  323. case 0x3: GR3 = flag0a(GR3 - getReg(code)); break;
  324. case 0x4: GR4 = flag0a(GR4 - getReg(code)); break;
  325. case 0x5: GR5 = flag0a(GR5 - getReg(code)); break;
  326. case 0x6: GR6 = flag0a(GR6 - getReg(code)); break;
  327. case 0x7: GR7 = flag0a(GR7 - getReg(code)); break;
  328. default: throw new Exception(op);
  329. }
  330. break;
  331. case 0x26: op = "ADDL: " + Integer.toString(code, 16);
  332. switch ((code & 0xF0) >> 4) {
  333. case 0x0: GR0 = flag0l(GR0 + getReg(code)); break;
  334. case 0x1: GR1 = flag0l(GR1 + getReg(code)); break;
  335. case 0x2: GR2 = flag0l(GR2 + getReg(code)); break;
  336. case 0x3: GR3 = flag0l(GR3 + getReg(code)); break;
  337. case 0x4: GR4 = flag0l(GR4 + getReg(code)); break;
  338. case 0x5: GR5 = flag0l(GR5 + getReg(code)); break;
  339. case 0x6: GR6 = flag0l(GR6 + getReg(code)); break;
  340. case 0x7: GR7 = flag0l(GR7 + getReg(code)); break;
  341. default: throw new Exception(op);
  342. }
  343. break;
  344. case 0x27: op = "SUBL: " + Integer.toString(code, 16);
  345. switch ((code & 0xF0) >> 4) {
  346. case 0x0: GR0 = flag0l(GR0 - getReg(code)); break;
  347. case 0x1: GR1 = flag0l(GR1 - getReg(code)); break;
  348. case 0x2: GR2 = flag0l(GR2 - getReg(code)); break;
  349. case 0x3: GR3 = flag0l(GR3 - getReg(code)); break;
  350. case 0x4: GR4 = flag0l(GR4 - getReg(code)); break;
  351. case 0x5: GR5 = flag0l(GR5 - getReg(code)); break;
  352. case 0x6: GR6 = flag0l(GR6 - getReg(code)); break;
  353. case 0x7: GR7 = flag0l(GR7 - getReg(code)); break;
  354. default: throw new Exception(op);
  355. }
  356. break;
  357. case 0x30: op = "AND: " + Integer.toString(code, 16);
  358. switch ((code & 0xF0) >> 4) {
  359. case 0x0: flag1(GR0 = GR0 & getAddr(mIn, code)); break;
  360. case 0x1: flag1(GR1 = GR1 & getAddr(mIn, code)); break;
  361. case 0x2: flag1(GR2 = GR2 & getAddr(mIn, code)); break;
  362. case 0x3: flag1(GR3 = GR3 & getAddr(mIn, code)); break;
  363. case 0x4: flag1(GR4 = GR4 & getAddr(mIn, code)); break;
  364. case 0x5: flag1(GR5 = GR5 & getAddr(mIn, code)); break;
  365. case 0x6: flag1(GR6 = GR6 & getAddr(mIn, code)); break;
  366. case 0x7: flag1(GR7 = GR7 & getAddr(mIn, code)); break;
  367. default: throw new Exception(op);
  368. }
  369. break;
  370. case 0x31: op = "OR: " + Integer.toString(code, 16);
  371. switch ((code & 0xF0) >> 4) {
  372. case 0x0: flag1(GR0 = GR0 | getAddr(mIn, code)); break;
  373. case 0x1: flag1(GR1 = GR1 | getAddr(mIn, code)); break;
  374. case 0x2: flag1(GR2 = GR2 | getAddr(mIn, code)); break;
  375. case 0x3: flag1(GR3 = GR3 | getAddr(mIn, code)); break;
  376. case 0x4: flag1(GR4 = GR4 | getAddr(mIn, code)); break;
  377. case 0x5: flag1(GR5 = GR5 | getAddr(mIn, code)); break;
  378. case 0x6: flag1(GR6 = GR6 | getAddr(mIn, code)); break;
  379. case 0x7: flag1(GR7 = GR7 | getAddr(mIn, code)); break;
  380. default: throw new Exception(op);
  381. }
  382. break;
  383. case 0x32: op = "XOR: " + Integer.toString(code, 16);
  384. switch ((code & 0xF0) >> 4) {
  385. case 0x0: flag1(GR0 = GR0 ^ getAddr(mIn, code)); break;
  386. case 0x1: flag1(GR1 = GR1 ^ getAddr(mIn, code)); break;
  387. case 0x2: flag1(GR2 = GR2 ^ getAddr(mIn, code)); break;
  388. case 0x3: flag1(GR3 = GR3 ^ getAddr(mIn, code)); break;
  389. case 0x4: flag1(GR4 = GR4 ^ getAddr(mIn, code)); break;
  390. case 0x5: flag1(GR5 = GR5 ^ getAddr(mIn, code)); break;
  391. case 0x6: flag1(GR6 = GR6 ^ getAddr(mIn, code)); break;
  392. case 0x7: flag1(GR7 = GR7 ^ getAddr(mIn, code)); break;
  393. default: throw new Exception(op);
  394. }
  395. break;
  396. case 0x34: op = "AND: " + Integer.toString(code, 16);
  397. switch ((code & 0xF0) >> 4) {
  398. case 0x0: flag1(GR0 = GR0 & getReg(code)); break;
  399. case 0x1: flag1(GR1 = GR1 & getReg(code)); break;
  400. case 0x2: flag1(GR2 = GR2 & getReg(code)); break;
  401. case 0x3: flag1(GR3 = GR3 & getReg(code)); break;
  402. case 0x4: flag1(GR4 = GR4 & getReg(code)); break;
  403. case 0x5: flag1(GR5 = GR5 & getReg(code)); break;
  404. case 0x6: flag1(GR6 = GR6 & getReg(code)); break;
  405. case 0x7: flag1(GR7 = GR7 & getReg(code)); break;
  406. default: throw new Exception(op);
  407. }
  408. break;
  409. case 0x35: op = "OR: " + Integer.toString(code, 16);
  410. switch ((code & 0xF0) >> 4) {
  411. case 0x0: flag1(GR0 = GR0 | getReg(code)); break;
  412. case 0x1: flag1(GR1 = GR1 | getReg(code)); break;
  413. case 0x2: flag1(GR2 = GR2 | getReg(code)); break;
  414. case 0x3: flag1(GR3 = GR3 | getReg(code)); break;
  415. case 0x4: flag1(GR4 = GR4 | getReg(code)); break;
  416. case 0x5: flag1(GR5 = GR5 | getReg(code)); break;
  417. case 0x6: flag1(GR6 = GR6 | getReg(code)); break;
  418. case 0x7: flag1(GR7 = GR7 | getReg(code)); break;
  419. default: throw new Exception(op);
  420. }
  421. break;
  422. case 0x36: op = "XOR: " + Integer.toString(code, 16);
  423. switch ((code & 0xF0) >> 4) {
  424. case 0x0: flag1(GR0 = GR0 ^ getReg(code)); break;
  425. case 0x1: flag1(GR1 = GR1 ^ getReg(code)); break;
  426. case 0x2: flag1(GR2 = GR2 ^ getReg(code)); break;
  427. case 0x3: flag1(GR3 = GR3 ^ getReg(code)); break;
  428. case 0x4: flag1(GR4 = GR4 ^ getReg(code)); break;
  429. case 0x5: flag1(GR5 = GR5 ^ getReg(code)); break;
  430. case 0x6: flag1(GR6 = GR6 ^ getReg(code)); break;
  431. case 0x7: flag1(GR7 = GR7 ^ getReg(code)); break;
  432. default: throw new Exception(op);
  433. }
  434. break;
  435. case 0x40: op = "CPA: " + Integer.toString(code, 16);
  436. switch ((code & 0xF0) >> 4) {
  437. case 0x0: flag0a(GR0 - getAddr(mIn, code)); break;
  438. case 0x1: flag0a(GR1 - getAddr(mIn, code)); break;
  439. case 0x2: flag0a(GR2 - getAddr(mIn, code)); break;
  440. case 0x3: flag0a(GR3 - getAddr(mIn, code)); break;
  441. case 0x4: flag0a(GR4 - getAddr(mIn, code)); break;
  442. case 0x5: flag0a(GR5 - getAddr(mIn, code)); break;
  443. case 0x6: flag0a(GR6 - getAddr(mIn, code)); break;
  444. case 0x7: flag0a(GR7 - getAddr(mIn, code)); break;
  445. default: throw new Exception(op);
  446. }
  447. break;
  448. case 0x41: op = "CPL: " + Integer.toString(code, 16);
  449. switch ((code & 0xF0) >> 4) {
  450. case 0x0: flag0l(GR0 - getAddr(mIn, code)); break;
  451. case 0x1: flag0l(GR1 - getAddr(mIn, code)); break;
  452. case 0x2: flag0l(GR2 - getAddr(mIn, code)); break;
  453. case 0x3: flag0l(GR3 - getAddr(mIn, code)); break;
  454. case 0x4: flag0l(GR4 - getAddr(mIn, code)); break;
  455. case 0x5: flag0l(GR5 - getAddr(mIn, code)); break;
  456. case 0x6: flag0l(GR6 - getAddr(mIn, code)); break;
  457. case 0x7: flag0l(GR7 - getAddr(mIn, code)); break;
  458. default: throw new Exception(op);
  459. }
  460. break;
  461. case 0x44: op = "CPA: " + Integer.toString(code, 16);
  462. switch ((code & 0xF0) >> 4) {
  463. case 0x0: flag0a(GR0 - getReg(code)); break;
  464. case 0x1: flag0a(GR1 - getReg(code)); break;
  465. case 0x2: flag0a(GR2 - getReg(code)); break;
  466. case 0x3: flag0a(GR3 - getReg(code)); break;
  467. case 0x4: flag0a(GR4 - getReg(code)); break;
  468. case 0x5: flag0a(GR5 - getReg(code)); break;
  469. case 0x6: flag0a(GR6 - getReg(code)); break;
  470. case 0x7: flag0a(GR7 - getReg(code)); break;
  471. default: throw new Exception(op);
  472. }
  473. break;
  474. case 0x45: op = "CPL: " + Integer.toString(code, 16);
  475. switch ((code & 0xF0) >> 4) {
  476. case 0x0: flag0l(GR0 - getReg(code)); break;
  477. case 0x1: flag0l(GR1 - getReg(code)); break;
  478. case 0x2: flag0l(GR2 - getReg(code)); break;
  479. case 0x3: flag0l(GR3 - getReg(code)); break;
  480. case 0x4: flag0l(GR4 - getReg(code)); break;
  481. case 0x5: flag0l(GR5 - getReg(code)); break;
  482. case 0x6: flag0l(GR6 - getReg(code)); break;
  483. case 0x7: flag0l(GR7 - getReg(code)); break;
  484. default: throw new Exception(op);
  485. }
  486. break;
  487. case 0x50: op = "SLA: " + Integer.toString(code, 16);
  488. adr = getAddr(mIn, code);
  489. switch ((code & 0xF0) >> 4) {
  490. case 0x0: adr = GR0 << adr; flag2a(adr & 0x8000, GR0 = (GR0 & 0x8000) | (adr & 0x7FFF)); break;
  491. case 0x1: adr = GR1 << adr; flag2a(adr & 0x8000, GR1 = (GR1 & 0x8000) | (adr & 0x7FFF)); break;
  492. case 0x2: adr = GR2 << adr; flag2a(adr & 0x8000, GR2 = (GR2 & 0x8000) | (adr & 0x7FFF)); break;
  493. case 0x3: adr = GR3 << adr; flag2a(adr & 0x8000, GR3 = (GR3 & 0x8000) | (adr & 0x7FFF)); break;
  494. case 0x4: adr = GR4 << adr; flag2a(adr & 0x8000, GR4 = (GR4 & 0x8000) | (adr & 0x7FFF)); break;
  495. case 0x5: adr = GR5 << adr; flag2a(adr & 0x8000, GR5 = (GR5 & 0x8000) | (adr & 0x7FFF)); break;
  496. case 0x6: adr = GR6 << adr; flag2a(adr & 0x8000, GR6 = (GR6 & 0x8000) | (adr & 0x7FFF)); break;
  497. case 0x7: adr = GR7 << adr; flag2a(adr & 0x8000, GR7 = (GR7 & 0x8000) | (adr & 0x7FFF)); break;
  498. default: throw new Exception(op);
  499. }
  500. break;
  501. case 0x51: op = "SRA: " + Integer.toString(code, 16);
  502. adr = getAddr(mIn, code);
  503. switch ((code & 0xF0) >> 4) {
  504. case 0x0: flag2a((GR0 >> (adr - 1)) & 0x1, GR0 = (((~(0xFFFF >> adr)) * (GR0 >> 15)) | (GR0 >> adr)) & 0xFFFF); break;
  505. case 0x1: flag2a((GR1 >> (adr - 1)) & 0x1, GR1 = (((~(0xFFFF >> adr)) * (GR1 >> 15)) | (GR1 >> adr)) & 0xFFFF); break;
  506. case 0x2: flag2a((GR2 >> (adr - 1)) & 0x1, GR2 = (((~(0xFFFF >> adr)) * (GR2 >> 15)) | (GR2 >> adr)) & 0xFFFF); break;
  507. case 0x3: flag2a((GR3 >> (adr - 1)) & 0x1, GR3 = (((~(0xFFFF >> adr)) * (GR3 >> 15)) | (GR3 >> adr)) & 0xFFFF); break;
  508. case 0x4: flag2a((GR4 >> (adr - 1)) & 0x1, GR4 = (((~(0xFFFF >> adr)) * (GR4 >> 15)) | (GR4 >> adr)) & 0xFFFF); break;
  509. case 0x5: flag2a((GR5 >> (adr - 1)) & 0x1, GR5 = (((~(0xFFFF >> adr)) * (GR5 >> 15)) | (GR5 >> adr)) & 0xFFFF); break;
  510. case 0x6: flag2a((GR6 >> (adr - 1)) & 0x1, GR6 = (((~(0xFFFF >> adr)) * (GR6 >> 15)) | (GR6 >> adr)) & 0xFFFF); break;
  511. case 0x7: flag2a((GR7 >> (adr - 1)) & 0x1, GR7 = (((~(0xFFFF >> adr)) * (GR7 >> 15)) | (GR7 >> adr)) & 0xFFFF); break;
  512. default: throw new Exception(op);
  513. }
  514. break;
  515. case 0x52: op = "SLL: " + Integer.toString(code, 16);
  516. adr = getAddr(mIn, code);
  517. switch ((code & 0xF0) >> 4) {
  518. case 0x0: flag2l((GR0 << (adr - 1)) & 0x8000, GR0 = (GR0 << adr) & 0xFFFF); break;
  519. case 0x1: flag2l((GR1 << (adr - 1)) & 0x8000, GR1 = (GR1 << adr) & 0xFFFF); break;
  520. case 0x2: flag2l((GR2 << (adr - 1)) & 0x8000, GR2 = (GR2 << adr) & 0xFFFF); break;
  521. case 0x3: flag2l((GR3 << (adr - 1)) & 0x8000, GR3 = (GR3 << adr) & 0xFFFF); break;
  522. case 0x4: flag2l((GR4 << (adr - 1)) & 0x8000, GR4 = (GR4 << adr) & 0xFFFF); break;
  523. case 0x5: flag2l((GR5 << (adr - 1)) & 0x8000, GR5 = (GR5 << adr) & 0xFFFF); break;
  524. case 0x6: flag2l((GR6 << (adr - 1)) & 0x8000, GR6 = (GR6 << adr) & 0xFFFF); break;
  525. case 0x7: flag2l((GR7 << (adr - 1)) & 0x8000, GR7 = (GR7 << adr) & 0xFFFF); break;
  526. default: throw new Exception(op);
  527. }
  528. break;
  529. case 0x53: op = "SRL: " + Integer.toString(code, 16);
  530. adr = getAddr(mIn, code);
  531. switch ((code & 0xF0) >> 4) {
  532. case 0x0: flag2l((GR0 >> (adr - 1)) & 0x1, GR0 = GR0 >> adr); break;
  533. case 0x1: flag2l((GR1 >> (adr - 1)) & 0x1, GR1 = GR1 >> adr); break;
  534. case 0x2: flag2l((GR2 >> (adr - 1)) & 0x1, GR2 = GR2 >> adr); break;
  535. case 0x3: flag2l((GR3 >> (adr - 1)) & 0x1, GR3 = GR3 >> adr); break;
  536. case 0x4: flag2l((GR4 >> (adr - 1)) & 0x1, GR4 = GR4 >> adr); break;
  537. case 0x5: flag2l((GR5 >> (adr - 1)) & 0x1, GR5 = GR5 >> adr); break;
  538. case 0x6: flag2l((GR6 >> (adr - 1)) & 0x1, GR6 = GR6 >> adr); break;
  539. case 0x7: flag2l((GR7 >> (adr - 1)) & 0x1, GR7 = GR7 >> adr); break;
  540. default: throw new Exception(op);
  541. }
  542. break;
  543. case 0x61: op = "JMI: " + Integer.toString(code, 16);
  544. adr = calcAddr(mIn, code);
  545. if ((FR & 0x2) != 0) {
  546. PR = adr + OFF;
  547. memory.setPos(PR << 1);
  548. }
  549. break;
  550. case 0x62: op = "JNZ: " + Integer.toString(code, 16);
  551. adr = calcAddr(mIn, code);
  552. if ((FR & 0x4) == 0) {
  553. PR = adr + OFF;
  554. memory.setPos(PR << 1);
  555. }
  556. break;
  557. case 0x63: op = "JZE: " + Integer.toString(code, 16);
  558. adr = calcAddr(mIn, code);
  559. if ((FR & 0x4) != 0) {
  560. PR = adr + OFF;
  561. memory.setPos(PR << 1);
  562. }
  563. break;
  564. case 0x64: op = "JUMP: " + Integer.toString(code, 16);
  565. PR = calcAddr(mIn, code) + OFF;
  566. memory.setPos(PR << 1);
  567. break;
  568. case 0x65: op = "JPL: " + Integer.toString(code, 16);
  569. adr = calcAddr(mIn, code);
  570. if ((FR & 0x6) == 0) {
  571. PR = adr + OFF;
  572. memory.setPos(PR << 1);
  573. }
  574. break;
  575. case 0x66: op = "JOV: " + Integer.toString(code, 16);
  576. adr = calcAddr(mIn, code);
  577. if ((FR & 0x1) != 0) {
  578. PR = adr + OFF;
  579. memory.setPos(PR << 1);
  580. }
  581. break;
  582. case 0x70: op = "PUSH: " + Integer.toString(code, 16);
  583. adr = calcAddr(mIn, code);
  584. SP--;
  585. memory.setPos(SP << 1);
  586. mOut.writeShort(adr);
  587. memory.setPos(PR << 1);
  588. break;
  589. case 0x71: op = "POP: " + Integer.toString(code, 16);
  590. memory.setPos(SP << 1);
  591. switch ((code & 0xF0) >> 4) {
  592. case 0x0: GR0 = mIn.readShort(); break;
  593. case 0x1: GR1 = mIn.readShort(); break;
  594. case 0x2: GR2 = mIn.readShort(); break;
  595. case 0x3: GR3 = mIn.readShort(); break;
  596. case 0x4: GR4 = mIn.readShort(); break;
  597. case 0x5: GR5 = mIn.readShort(); break;
  598. case 0x6: GR6 = mIn.readShort(); break;
  599. case 0x7: GR7 = mIn.readShort(); break;
  600. default: throw new Exception(op);
  601. }
  602. SP++;
  603. memory.setPos(PR << 1);
  604. break;
  605. case 0x80: op = "CALL: " + Integer.toString(code, 16);
  606. adr = calcAddr(mIn, code);
  607. SP--;
  608. memory.setPos(SP << 1);
  609. mOut.writeShort(PR);
  610. PR = adr;
  611. memory.setPos(PR << 1);
  612. break;
  613. case 0x81: op = "RET: " + Integer.toString(code, 16);
  614. memory.setPos(SP << 1);
  615. PR = mIn.readShort();
  616. SP++;
  617. memory.setPos(PR << 1);
  618. break;
  619. case 0xF0: op = "SVC: " + Integer.toString(code, 16);
  620. adr = calcAddr(mIn, code);
  621. switch (adr) {
  622. case 1: // Read Line
  623. {
  624. int len = -1;
  625. String line = cIn.readLine();
  626. if (line != null) {
  627. len = line.length();
  628. len = len > 256 ? 256 : len;
  629. memory.setPos((GR1 + OFF) << 1);
  630. for (int i = 0; i < len; i++) {
  631. mOut.writeShort(line.charAt(i));
  632. }
  633. }
  634. memory.setPos((GR2 + OFF) << 1);
  635. mOut.writeShort(len);
  636. memory.setPos(PR << 1);
  637. }
  638. break;
  639.  
  640. case 2: // Write String
  641. {
  642. memory.setPos((GR2 + OFF) << 1);
  643. int len = mIn.readShort();
  644. memory.setPos((GR1 + OFF) << 1);
  645. for (int i = 0; i < len; i++) {
  646. cOut.print((char)mIn.readShort());
  647. }
  648. memory.setPos(PR << 1);
  649. }
  650. break;
  651. }
  652. break;
  653.  
  654.  
  655. // for tableswitch
  656.  
  657. case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
  658. case 0x08: case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x0E: case 0x0F:
  659.  
  660. case 0x13: case 0x15: case 0x16: case 0x17:
  661. case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1E: case 0x1F:
  662.  
  663.  
  664. case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F:
  665.  
  666. case 0x33: case 0x37:
  667. case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F:
  668.  
  669. case 0x42: case 0x43: case 0x46: case 0x47:
  670. case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F:
  671.  
  672. case 0x54: case 0x55: case 0x56: case 0x57:
  673. case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F:
  674.  
  675. case 0x60: case 0x67:
  676. case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F:
  677.  
  678. case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
  679. case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
  680.  
  681. case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
  682. case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
  683.  
  684. case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
  685. case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
  686.  
  687. case 0xA0: case 0xA1: case 0xA2: case 0xA3: case 0xA4: case 0xA5: case 0xA6: case 0xA7:
  688. case 0xA8: case 0xA9: case 0xAA: case 0xAB: case 0xAC: case 0xAD: case 0xAE: case 0xAF:
  689.  
  690. case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
  691. case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
  692.  
  693. case 0xC0: case 0xC1: case 0xC2: case 0xC3: case 0xC4: case 0xC5: case 0xC6: case 0xC7:
  694. case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
  695.  
  696. case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4: case 0xD5: case 0xD6: case 0xD7:
  697. case 0xD8: case 0xD9: case 0xDA: case 0xDB: case 0xDC: case 0xDD: case 0xDE: case 0xDF:
  698.  
  699. case 0xE0: case 0xE1: case 0xE2: case 0xE3: case 0xE4: case 0xE5: case 0xE6: case 0xE7:
  700. case 0xE8: case 0xE9: case 0xEA: case 0xEB: case 0xEC: case 0xED: case 0xEE: case 0xEF:
  701.  
  702. case 0xF1: case 0xF2: case 0xF3: case 0xF4: case 0xF5: case 0xF6: case 0xF7:
  703. case 0xF8: case 0xF9: case 0xFA: case 0xFB: case 0xFC: case 0xFD: case 0xFE: case 0xFF:
  704.  
  705. default:
  706. throw new Exception("???: " + Integer.toString(code, 16));
  707. }
  708. }
  709.  
  710. public void run()
  711. {
  712. java.io.DataInput mIn = memory.in;
  713. java.io.DataOutput mOut = memory.out;
  714.  
  715. try {
  716. memory.setPos(PR);
  717. for (;;) {
  718. step(mIn, mOut);
  719. }
  720. } catch (java.lang.Exception ex) {
  721. ex.printStackTrace();
  722. }
  723. }
  724.  
  725. public static void main(String[] args) throws java.lang.Exception
  726. {
  727. Memory mem = new Memory(100);
  728. DataOutputStream dOut = mem.out;
  729.  
  730. dOut.writeShort(0x1210); // LAD GR1, 35
  731. dOut.writeShort(35);
  732.  
  733. dOut.writeShort(0x1220); // LAD GR2, 99
  734. dOut.writeShort(99);
  735.  
  736. dOut.writeShort(0x7001); // PUSH 0, GR1
  737. dOut.writeShort(0);
  738.  
  739. dOut.writeShort(0x7002); // PUSH 0, GR2
  740. dOut.writeShort(0);
  741.  
  742. dOut.writeShort(0x2522); // SUBA GR2, GR2
  743.  
  744. dOut.writeShort(0x3411); // AND GR1, GR1
  745.  
  746. dOut.writeShort(0x6300); // JZE LABEL-RETURN
  747. int p1 = mem.getOutPos();
  748. dOut.writeShort(0); // Writing After time
  749.  
  750. int p2 = mem.getOutPos(); // LABEL-MORE
  751.  
  752. dOut.writeShort(0x1222); // LAD GR2, 1, GR2
  753. dOut.writeShort(1);
  754.  
  755. dOut.writeShort(0x1201); // LAD GR0, -1, GR1
  756. dOut.writeShort(-1);
  757.  
  758. dOut.writeShort(0x3410); // AND GR1, GR0
  759.  
  760. dOut.writeShort(0x6200); // JNZ LABEL-MORE
  761. dOut.writeShort(p2 >> 1);
  762.  
  763. int p3 = mem.getOutPos(); // LABEL-RETURN
  764.  
  765. dOut.writeShort(0x1402); // LD GR0, GR2
  766.  
  767. dOut.writeShort(0x7120); // POP GR2
  768.  
  769. dOut.writeShort(0x7110); // POP GR1
  770.  
  771. dOut.writeShort(0x8100); // RET
  772.  
  773. mem.setPos(p1);
  774. dOut.writeShort(p3 >> 1); // Write JZE LABEL-RETURN
  775.  
  776. CometII c2 = new CometII(mem.mem);
  777.  
  778. System.out.println(c2.toString());
  779. for (int i = 0; i < 100; i++) {
  780. System.in.read();
  781. c2.step();
  782. System.out.println(c2.toString());
  783. System.out.println();
  784. }
  785. }
  786.  
  787. }
Runtime error #stdin #stdout #stderr 0.09s 380480KB
stdin
Standard input is empty
stdout
Last OP: NOP
PR: 0, FR: 0, SP: 65535
GR0: 0, GR1: 0, GR2: 0, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0
Last OP: LAD: 1210, addr: 35
PR: 2, FR: 0, SP: 65535
GR0: 0, GR1: 35, GR2: 0, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LAD: 1220, addr: 99
PR: 4, FR: 0, SP: 65535
GR0: 0, GR1: 35, GR2: 99, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: PUSH: 7001, addr: 35
PR: 6, FR: 0, SP: 65534
GR0: 0, GR1: 35, GR2: 99, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: PUSH: 7002, addr: 99
PR: 8, FR: 0, SP: 65533
GR0: 0, GR1: 35, GR2: 99, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: SUBA: 2522
PR: 9, FR: 4, SP: 65533
GR0: 0, GR1: 35, GR2: 0, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: AND: 3411
PR: 10, FR: 0, SP: 65533
GR0: 0, GR1: 35, GR2: 0, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: JZE: 6300, addr: 19
PR: 12, FR: 0, SP: 65533
GR0: 0, GR1: 35, GR2: 0, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LAD: 1222, addr: 1
PR: 14, FR: 0, SP: 65533
GR0: 0, GR1: 35, GR2: 1, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LAD: 1201, addr: 34
PR: 16, FR: 0, SP: 65533
GR0: 34, GR1: 35, GR2: 1, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: AND: 3410
PR: 17, FR: 0, SP: 65533
GR0: 34, GR1: 34, GR2: 1, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: JNZ: 6200, addr: 12
PR: 12, FR: 0, SP: 65533
GR0: 34, GR1: 34, GR2: 1, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LAD: 1222, addr: 2
PR: 14, FR: 0, SP: 65533
GR0: 34, GR1: 34, GR2: 2, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LAD: 1201, addr: 33
PR: 16, FR: 0, SP: 65533
GR0: 33, GR1: 34, GR2: 2, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: AND: 3410
PR: 17, FR: 0, SP: 65533
GR0: 33, GR1: 32, GR2: 2, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: JNZ: 6200, addr: 12
PR: 12, FR: 0, SP: 65533
GR0: 33, GR1: 32, GR2: 2, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LAD: 1222, addr: 3
PR: 14, FR: 0, SP: 65533
GR0: 33, GR1: 32, GR2: 3, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LAD: 1201, addr: 31
PR: 16, FR: 0, SP: 65533
GR0: 31, GR1: 32, GR2: 3, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: AND: 3410
PR: 17, FR: 4, SP: 65533
GR0: 31, GR1: 0, GR2: 3, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: JNZ: 6200, addr: 12
PR: 19, FR: 4, SP: 65533
GR0: 31, GR1: 0, GR2: 3, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: LD: 1402
PR: 20, FR: 0, SP: 65533
GR0: 3, GR1: 0, GR2: 3, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: POP: 7120
PR: 21, FR: 0, SP: 65534
GR0: 3, GR1: 0, GR2: 99, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

Last OP: POP: 7110
PR: 22, FR: 0, SP: 65535
GR0: 3, GR1: 35, GR2: 99, GR3: 0, GR4: 0, GR5: 0, GR6: 0, GR7: 0

stderr
Exception in thread "main" java.io.EOFException
	at java.io.DataInputStream.readShort(DataInputStream.java:315)
	at CometII.step(Main.java:615)
	at CometII.step(Main.java:189)
	at CometII.main(Main.java:781)