Fantasy 8Bit system (F8), is a fantasy 8bit console and a set of libraries for creating fantasy 8bit consoles.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1024 lines
29KB

  1. /*
  2. * Emulate a basic 6502 (MOS) chip.
  3. */
  4. const BITM = require('../utils/bitman.js');
  5. var IMem = require('../memory').IMem;
  6. // mode = 0 - Immediate
  7. // mode = 1 - Zero Page
  8. // mode = 2 - Zero Page, X
  9. // mode = 3 - Zero Page, Y
  10. // mode = 4 - Absolute
  11. // mode = 5 - Absolute, X
  12. // mode = 6 - Absolute, Y
  13. // mode = 7 - Indirect, X
  14. // mode = 8 - Indirect, Y
  15. function ProcessOp(cpu, mode){
  16. if (mode < 0 || mode > 9){return false;}
  17. switch(cpu.__cycle){
  18. case 0:
  19. if (mode === 0){
  20. PCUp(cpu, 1);
  21. cpu.__mem.address = cpu.__PC;
  22. //PCUp(cpu, 1);
  23. cpu.__opv = cpu.__mem.byte;
  24. }
  25. return (mode === 0 || mode === 9);
  26. case 1:
  27. switch(mode){
  28. case 1: // Zero Page
  29. cpu.__mem.address = cpu.__opv;
  30. cpu.__opv = cpu.__mem.byte;
  31. return true;
  32. case 2: // Zero Page, X
  33. case 3: // Zero Page, Y
  34. cpu.__opv = (cpu.__opv + ((mode === 2) ? cpu.__XR : cpu.__YR)) & 0xFF; break;
  35. case 4: // Absolute
  36. case 5: // Absolute, X
  37. case 6: // Absolute, Y
  38. PCUp(cpu, 1);
  39. cpu.__mem.address = cpu.__PC;
  40. //PCUp(cpu, 1);
  41. cpu.__opv |= cpu.__mem.byte << 8;
  42. break;
  43. case 7: // Indirect, X
  44. cpu.__opv = (cpu.__opv + cpu.__XR) & 0xFF; break;
  45. case 8: // Indirect, Y
  46. cpu.__mem.address = cpu.__opv;
  47. cpu.__opv = cpu.__mem.byte;
  48. break;
  49. } break;
  50. case 2:
  51. switch(mode){
  52. case 2: // Zero Page, X
  53. case 3: // Zero Page, Y
  54. case 4: // Absolute
  55. cpu.__mem.address = cpu.__opv;
  56. cpu.__opv = cpu.__mem.byte;
  57. return true;
  58. case 5: // Absolute, X
  59. case 6: // Absolute, Y
  60. let s = (mode === 5) ? cpu.__XR : cpu.__YR;
  61. let l = (cpu.__opv & 0xFF) + s;
  62. cpu.__opv = (cpu.__opv & 0xFF00) | (l & 0xFF);
  63. if (l < 255){
  64. cpu.__mem.address = cpu.__opv;
  65. cpu.__opv = cpu.__mem.byte;
  66. return true;
  67. }
  68. break;
  69. case 7: // Indirect, X
  70. cpu.__mem.address = cpu.__opv;
  71. cpu.__opv = cpu.__mem.byte;
  72. break;
  73. case 8: // Indirect, Y
  74. cpu.__mem.address += 1;
  75. cpu.__opv |= cpu.__mem.byte << 8;
  76. break;
  77. } break;
  78. case 3:
  79. switch(mode){
  80. case 5: // Absolute, X
  81. case 6: // Absolute, Y
  82. let h = (cpu.__opv >> 8) + 1;
  83. cpu.__mem.address = (cpu.__opv & 0xFF) | (h << 8);
  84. cpu.__opv = cpu.__mem.byte;
  85. return true;
  86. case 7: // Indirect, X
  87. cpu.__mem.address += 1;
  88. cpu.__opv |= cpu.__mem.byte << 8;
  89. break;
  90. case 8: // Indirect, Y
  91. let l = (cpu.__opv & 0xFF) + cpu.__YR;
  92. cpu.__opv = (cpu.__opv & 0xFF00) | (l & 0xFF);
  93. if (l <= 255){
  94. cpu.__mem.address = cpu.__opv;
  95. cpu.__opv = cpu.__mem.byte;
  96. return true;
  97. }
  98. break;
  99. } break;
  100. case 4:
  101. if (mode === 8){
  102. let h = (cpu.__opv >> 8) + 1;
  103. cpu.__opv = (cpu.__opv & 0x00FF) | (h << 8);
  104. }
  105. cpu.__mem.address = cpu.__opv;
  106. cpu.__opv = cpu.__mem.byte;
  107. return true;
  108. }
  109. cpu.__cycle += 1;
  110. return false;
  111. }
  112. function ADC(cpu){
  113. let pmode = [0x69, 0x65, 0x75, null, 0x6D, 0x7D, 0x79, 0x61, 0x71, null].indexOf(cpu.__op);
  114. if (ProcessOp(cpu, pmode) === true){
  115. cpu.__op = -1;
  116. ALU(cpu, 0, cpu.__opv);
  117. }
  118. }
  119. function AND(cpu){
  120. let pmode = [0x29, 0x25, 0x35, null, 0x2D, 0x3D, 0x39, 0x21, 0x31, null].indexOf(cpu.__op);
  121. if (ProcessOp(cpu, pmode) === true){
  122. cpu.__op = -1;
  123. this.__AR &= this.__opv;
  124. cpu.Z = (this.__AR === 0);
  125. cpu.N = BITM.val(this.__AR, 7);
  126. }
  127. }
  128. function ASL(cpu){
  129. let pmode = [null, 0x06, 0x16, null, 0x0E, 0x1E, null, null, null, 0x0A].indexOf(cpu.__op);
  130. if (cpu.__ophold === 1){pmode = -1;}
  131. if (cpu.__ophold === 1 || ProcessOp(cpu, pmode) === true){
  132. if (cpu.__ophold === 0){
  133. cpu.__opv = (mode === 9) ? cpu.__AR : cpu.__opv;
  134. cpu.C = BITM.val(cpu.__opv, 7);
  135. cpu.__opv = (cpu.__opv << 1) & 0xFF;
  136. cpu.__ophold = 1;
  137. } else {
  138. if (mode === 9){
  139. cpu.__AR = cpu.__opv;
  140. } else {
  141. cpu.__mem.byte = cpu.__opv;
  142. }
  143. cpu.Z = (this.__opv === 0);
  144. cpu.N = BITM.val(this.__opv, 7);
  145. cpu.__op = -1;
  146. cpu.__ophold = 0;
  147. }
  148. }
  149. }
  150. function BIT(cpu){
  151. let pmode = [null, 0x24, null, null, 0x2C, null, null, null, null, null].indexOf(cpu.__op);
  152. if (ProcessOp(cpu, pmode) === true){
  153. cpu.__op = -1;
  154. let v = this.__AR & this.__opv;
  155. cpu.Z = (v === 0);
  156. cpu.N = BITM.val(this.__opv, 7);
  157. cpu.V = BITM.val(this.__opv, 6);
  158. }
  159. }
  160. function BRANCH(cpu){
  161. switch(cpu.__cycle){
  162. case 0:
  163. let branch = false;
  164. switch(cpu.__op){
  165. case 0x10: // BPL
  166. branch = (cpu.N === 0); break;
  167. case 0x30: // BMI
  168. branch = (cpu.N === 1); break;
  169. case 0x50: // BVC
  170. branch = (cpu.V === 0); break;
  171. case 0x70: // BVS
  172. branch = (cpu.V === 1); break;
  173. case 0x90: // BCC
  174. branch = (cpu.C === 0); break;
  175. case 0xB0: // BCS
  176. branch = (cpu.C === 1); break;
  177. case 0xD0: // BNE
  178. branch = (cpu.Z === 0); break;
  179. case 0xF0: // BEQ
  180. branch = (cpu.Z === 1); break;
  181. }
  182. if (branch === false)
  183. PCUp(cpu, 1);
  184. case 1:
  185. if (cpu.__cycle === 1){ // TODO: Huh???
  186. cpu.__mem.address = this.__PC;
  187. let v = cpu.__mem.byte;
  188. if (v > 128){
  189. PCDown(cpu, 255 - v);
  190. } else {
  191. PCUp(cpu, v);
  192. }
  193. }
  194. cpu.__op = -1;
  195. }
  196. cpu.__cycle += 1;
  197. }
  198. function BRK(cpu, nmi){
  199. nmi = (nmi === true);
  200. switch(cpu.__cycle){
  201. case 0:
  202. PCUp(cpu, 1);
  203. case 1:
  204. StackPush(cpu, cpu.__PC >> 8); break;
  205. case 2:
  206. StackPush(cpu, cpu.__PC & 0xFF); break;
  207. case 3:
  208. StackPush(cpu, cpu.__SP); break;
  209. case 4:
  210. cpu.__mem.address = (nmi) ? 0xFFFC : 0xFFFE;
  211. cpu.__PC = cpu.__mem.byte;
  212. break;
  213. case 5:
  214. cpu.__mem.address = (nmi) ? 0xFFFD : 0xFFFF;
  215. cpu.__PC |= cpu.__mem.byte << 8;
  216. cpu.B = 1;
  217. cpu.__op = -1;
  218. break;
  219. }
  220. cpu.__cycle += 1;
  221. }
  222. function CMP(cpu){
  223. let pmode = [0xC9, 0xC5, 0xD5, null, 0xCD, 0xDD, 0xD9, 0xC1, 0xD1, null].indexOf(cpu.__op);
  224. if (ProcessOp(cpu, pmode) === true){
  225. cpu.__op = -1;
  226. cpu.C = (cpu.__AR >= cpu.__opv);
  227. cpu.Z = (this.__AR === cpu.__opv);
  228. let v = this.__AR - cpu.__opv;
  229. if (v < 0){v += 256;}
  230. cpu.N = BITM.val(v, 7);
  231. }
  232. }
  233. function CPX(cpu){
  234. let pmode = [0xE0, 0xE4, null, null, 0xEC, null, null, null, null, null].indexOf(cpu.__op);
  235. if (ProcessOp(cpu, pmode) === true){
  236. cpu.__op = -1;
  237. cpu.C = (cpu.__XR >= cpu.__opv);
  238. cpu.Z = (this.__XR === cpu.__opv);
  239. let v = this.__XR - cpu.__opv;
  240. if (v < 0){v += 256;}
  241. cpu.N = BITM.val(v, 7);
  242. }
  243. }
  244. function CPY(cpu){
  245. let pmode = [0xC0, 0xC4, null, null, 0xCC, null, null, null, null, null].indexOf(cpu.__op);
  246. if (ProcessOp(cpu, pmode) === true){
  247. cpu.__op = -1;
  248. cpu.C = (cpu.__YR >= cpu.__opv);
  249. cpu.Z = (this.__YR === cpu.__opv);
  250. let v = this.__YR - cpu.__opv;
  251. if (v < 0){v += 256;}
  252. cpu.N = BITM.val(v, 7);
  253. }
  254. }
  255. function DEC(cpu){
  256. let pmode = [null, 0xC6, 0xD6, null, 0xCE, 0xDE, null, null, null, null].indexOf(cpu.__op);
  257. if (cpu.__ophold === 1){pmode = -1;}
  258. if (cpu.__ophold === 1 || ProcessOp(cpu, pmode) === true){
  259. if (cpu.__ophold === 0){
  260. cpu.__opv = ByteWrap(cpu.__opv, -1);
  261. cpu.__ophold = 1;
  262. } else {
  263. cpu.__mem.byte = cpu.__opv;
  264. cpu.Z = (this.__opv === 0);
  265. cpu.N = BITM.val(this.__opv, 7);
  266. cpu.__op = -1;
  267. cpu.__ophold = 0;
  268. }
  269. }
  270. }
  271. function EOR(cpu){
  272. let pmode = [0x49, 0x45, 0x55, null, 0x4D, 0x5D, 0x59, 0x41, 0x51, null].indexOf(cpu.__op);
  273. if (ProcessOp(cpu, pmode) === true){
  274. cpu.__op = -1;
  275. cpu.__AR ^= cpu.__opv;
  276. cpu.Z = (cpu.__AR === 0);
  277. cpu.N = BITM.val(cpu.__AR, 7);
  278. }
  279. }
  280. function FLAG(cpu){
  281. switch (cpu.__op){
  282. case 0x18: // CLC
  283. cpu.C = 0; break;
  284. case 0x38: // SEC
  285. cpu.C = 1; break;
  286. case 0x58: // CLI
  287. cpu.I = 0; break;
  288. case 0x78: // SEI
  289. cpu.I = 1; break;
  290. case 0xB8: // CLV
  291. cpu.V = 0; break;
  292. case 0xD8: // CLD
  293. cpu.D = 0; break;
  294. case 0xF8: // SED
  295. cpu.D = 1; break;
  296. }
  297. cpu.__op = -1;
  298. }
  299. function INC(cpu){
  300. let pmode = [null, 0xE6, 0xF6, null, 0xEE, 0xFE, null, null, null, null].indexOf(cpu.__op);
  301. if (cpu.__ophold === 1){pmode = -1;}
  302. if (cpu.__ophold === 1 || ProcessOp(cpu, pmode) === true){
  303. if (cpu.__ophold === 0){
  304. cpu.__opv = ByteWrap(cpu.__opv, 1);
  305. cpu.__ophold = 1;
  306. } else {
  307. cpu.__mem.byte = cpu.__opv;
  308. cpu.Z = (this.__opv === 0);
  309. cpu.N = BITM.val(this.__opv, 7);
  310. cpu.__op = -1;
  311. cpu.__ophold = 0;
  312. }
  313. }
  314. }
  315. function JMP(cpu){
  316. switch(cpu.__cycle){
  317. case 0:
  318. cpu.__mem.address = cpu.__PC;
  319. cpu.__opv = cpu.__mem.byte;
  320. PCUp(cpu, 1); break;
  321. case 1:
  322. cpu.__mem.address = cpu.__PC;
  323. cpu.__opv |= cpu.__mem.byte << 8;
  324. if (cpu.__op === 0x4C){
  325. cpu.__PC = cpu.__opv;
  326. cpu.__op = -1;
  327. }
  328. break;
  329. case 2:
  330. cpu.__mem.address = cpu.__opv;
  331. cpu.__PC = cpu.__mem.byte;
  332. break;
  333. case 3:
  334. cpu.__mem.address += 1;
  335. cpu.__PC |= cpu.__mem.byte << 8;
  336. cpu.__op = -1;
  337. break;
  338. }
  339. cpu.__cycle += 1;
  340. }
  341. function JRS(cpu){
  342. switch(cpu.__cycle){
  343. case 0:
  344. cpu.__mem.address = cpu.__PC;
  345. cpu.__opv = cpu.__mem.byte;
  346. PCUp(cpu, 1); break;
  347. case 1:
  348. // Discard stack data (or... No Op)
  349. break;
  350. case 2:
  351. StackPush(cpu, cpu.__PC >> 8); break;
  352. case 3:
  353. StackPush(cpu, cpu.__PC & 0xFF); break;
  354. case 4:
  355. cpu.__mem.address = cpu.__PC;
  356. cpu.__PC = cpu.__opv | (cpu.__mem.byte << 8)
  357. cpu.__op = -1;
  358. break;
  359. }
  360. cpu.__cycle += 1;
  361. }
  362. function LDA(cpu){
  363. let pmode = [0xA9, 0xA5, 0xB5, null, 0xAD, 0xBD, 0xB9, 0xA1, 0xB1, null].indexOf(cpu.__op);
  364. if (ProcessOp(cpu, pmode) === true){
  365. cpu.__op = -1;
  366. cpu.__AR = cpu.__opv;
  367. cpu.Z = (cpu.__AR === 0);
  368. cpu.N = (cpu.__AR >= 0x80);
  369. }
  370. }
  371. function LDX(cpu){
  372. let pmode = [0xA2, 0xA6, null, 0xB6, 0xAE, null, 0xBE, null, null, null].indexOf(cpu.__op);
  373. if (ProcessOp(cpu, pmode) === true){
  374. cpu.__op = -1;
  375. cpu.__XR = cpu.__opv;
  376. cpu.Z = (cpu.__XR === 0);
  377. cpu.N = (cpu.__XR >= 0x80);
  378. }
  379. }
  380. function LDY(cpu){
  381. let pmode = [0xA0, 0xA4, 0xB4, null, 0xAC, 0xBC, null, null, null, null].indexOf(cpu.__op);
  382. if (ProcessOp(cpu, pmode) === true){
  383. cpu.__op = -1;
  384. cpu.__YR = cpu.__opv;
  385. cpu.Z = (cpu.__YR === 0);
  386. cpu.N = (cpu.__YR >= 0x80);
  387. }
  388. }
  389. function LSR(cpu){
  390. let pmode = [null, 0x46, 0x56, null, 0x4E, 0x5E, null, null, null, 0x4A].indexOf(cpu.__op);
  391. if (cpu.__ophold === 1){pmode = -1;}
  392. if (cpu.__ophold === 1 || ProcessOp(cpu, pmode) === true){
  393. if (cpu.__ophold === 0){
  394. cpu.__opv = (mode === 9) ? cpu.__AR : cpu.__opv;
  395. cpu.C = BITM.val(cpu.__opv, 0);
  396. cpu.__opv = cpu.__opv >> 1;
  397. cpu.__ophold = 1;
  398. } else {
  399. if (mode === 9){
  400. cpu.__AR = cpu.__opv;
  401. } else {
  402. cpu.__mem.byte = cpu.__opv;
  403. }
  404. // TODO: Is Z and N effected by the A register, or by the resulting value? I think the latter.
  405. cpu.Z = (this.__opv === 0);
  406. cpu.N = BITM.val(this.__opv, 7);
  407. cpu.__op = -1;
  408. cpu.__ophold = 0;
  409. }
  410. }
  411. }
  412. function ORA(cpu){
  413. let pmode = [0x09, 0x05, 0x15, null, 0x0D, 0x1D, 0x19, 0x01, 0x11, null].indexOf(cpu.__op);
  414. if (ProcessOp(cpu, pmode) === true){
  415. cpu.__op = -1;
  416. cpu.__AR |= cpu.__opv;
  417. cpu.Z = (cpu.__AR === 0);
  418. cpu.N = BITM.val(cpu.__AR, 7);
  419. }
  420. }
  421. function REGISTER(cpu){
  422. let t = 0;
  423. switch(this.__op){
  424. case 0xAA: // TAX
  425. cpu.__XR = cpu.__AR;
  426. t = cpu.__XR;
  427. break;
  428. case 0x8A: // TXA
  429. cpu.__AR = cpu.__XR;
  430. t = cpu.__AR;
  431. break;
  432. case 0xCA: // DEX
  433. cpu.__XR = (cpu.__XR === 0) ? 255 : cpu.__XR - 1;
  434. t = cpu.__XR;
  435. break;
  436. case 0xE8: // INX
  437. cpu.__XR = (cpu.__XR === 255) ? 0 : cpu.__XR + 1;
  438. t = cpu.__XR;
  439. break;
  440. case 0xA8: // TAY
  441. cpu.__YR = cpu.__AR;
  442. t = cpu.__YR;
  443. break;
  444. case 0x98: // TYA
  445. cpu.__AR = cpu.__YR;
  446. t = cpu.__AR;
  447. break;
  448. case 0x88: // DEY
  449. cpu.__YR = (cpu.__YR === 0) ? 255 : cpu.__YR - 1;
  450. t = cpu.__YR;
  451. break;
  452. case 0xC8: // INY
  453. cpu.__YR = (cpu.__YR === 255) ? 0 : cpu.__YR + 1;
  454. t = cpu.__YR;
  455. break;
  456. }
  457. cpu.N = (t >= 0x80);
  458. cpu.Z = (t === 0);
  459. cpu.__op = -1;
  460. }
  461. function ROL(cpu){
  462. let pmode = [null, 0x26, 0x36, null, 0x2E, 0x3E, null, null, null, 0x2A].indexOf(cpu.__op);
  463. if (cpu.__ophold === 1){pmode = -1;}
  464. if (cpu.__ophold === 1 || ProcessOp(cpu, pmode) === true){
  465. if (cpu.__ophold === 0){
  466. let v = cpu.C;
  467. cpu.__opv = (mode === 9) ? cpu.__AR : cpu.__opv;
  468. cpu.C = BITM.val(cpu.__opv, 7);
  469. cpu.__opv = ((cpu.__opv << 1) + v) & 0xFF;
  470. cpu.__ophold = 1;
  471. } else {
  472. if (mode === 9){
  473. cpu.__AR = cpu.__opv;
  474. } else {
  475. cpu.__mem.byte = cpu.__opv;
  476. }
  477. cpu.Z = (this.__opv === 0);
  478. cpu.N = BITM.val(this.__opv, 7);
  479. cpu.__op = -1;
  480. cpu.__ophold = 0;
  481. }
  482. }
  483. }
  484. function ROR(cpu){
  485. let pmode = [null, 0x66, 0x76, null, 0x6E, 0x7E, null, null, null, 0x6A].indexOf(cpu.__op);
  486. if (cpu.__ophold === 1){pmode = -1;}
  487. if (cpu.__ophold === 1 || ProcessOp(cpu, pmode) === true){
  488. if (cpu.__ophold === 0){
  489. let v = cpu.C;
  490. cpu.__opv = (mode === 9) ? cpu.__AR : cpu.__opv;
  491. cpu.C = BITM.val(cpu.__opv, 0);
  492. cpu.__opv = ((cpu.__opv >> 1) + (v * 0x80)) & 0xFF;
  493. cpu.__ophold = 1;
  494. } else {
  495. if (mode === 9){
  496. cpu.__AR = cpu.__opv;
  497. } else {
  498. cpu.__mem.byte = cpu.__opv;
  499. }
  500. cpu.Z = (this.__opv === 0);
  501. cpu.N = BITM.val(this.__opv, 7);
  502. cpu.__op = -1;
  503. cpu.__ophold = 0;
  504. }
  505. }
  506. }
  507. function RTI(cpu){
  508. switch(cpu.__cycle){
  509. case 0: break; // Discard PC
  510. case 1: // Discard Stack Pointer Data
  511. StackPop(cpu); break;
  512. case 2:
  513. cpu.__PR = StackPop(cpu); break;
  514. case 3:
  515. cpu.__PC = StackPop(cpu); break;
  516. case 4:
  517. cpu.__PC |= StackPop(cpu) << 8;
  518. cpu.__op = -1;
  519. break;
  520. }
  521. cpu.__cycle += 1;
  522. }
  523. function RTS(cpu){
  524. switch(cpu.__cycle){
  525. case 0: break; // Discard PC
  526. case 1:
  527. StackPop(cpu); break;
  528. case 2:
  529. cpu.__PC = StackPop(cpu); break;
  530. case 3:
  531. cpu.__PC |= StackPop(cpu) << 8; break;
  532. case 4:
  533. PCUp(cpu, 1);
  534. cpu.__op = -1;
  535. break;
  536. }
  537. cpu.__cycle += 1;
  538. }
  539. function SBC(cpu){
  540. let pmode = [0xE9, 0xE5, 0xF5, null, 0xED, 0xFD, 0xF9, 0xE1, 0xF1].indexOf(cpu.__op);
  541. if (ProcessOp(cpu, pmode) === true){
  542. cpu.__op = -1;
  543. ALU(cpu, 1, cpu.__opv);
  544. }
  545. }
  546. function STA(cpu){
  547. let pmode = [null, 0x85, 0x95, null, 0x8D, 0x9D, 0x99, 0x81, 0x91, null].indexOf(cpu.__op);
  548. if (ProcessOp(cpu, pmode) === true){
  549. cpu.__op = -1;
  550. cpu.__mem.address = cpu.__opv;
  551. cpu.__mem.byte = cpu.__AR;
  552. }
  553. }
  554. function STACK(cpu){
  555. switch(cpu.__op){
  556. case 0x9A: // TXS
  557. cpu.__SP = cpu.__XR;
  558. cpu.__op = -1;
  559. break;
  560. case 0xBA: // TSX
  561. cpu.__XR = cpu.__SP;
  562. cpu.__op = -1;
  563. break;
  564. case 0x48: // PHA
  565. if (cpu.__cycle === 0){
  566. cpu.__mem.address = 0x0100 | cpu.__SP;
  567. } else if (cpu.__cycle === 1){
  568. cpu.__mem.byte = cpu.__AR;
  569. cpu.__SP = (cpu.__SP === 0) ? 255 : cpu.__SP - 1;
  570. cpu.__op = -1;
  571. }
  572. break;
  573. case 0x68: // PLA
  574. if (cpu.__cycle === 0){
  575. cpu.__mem.address = 0x0100 | cpu.__SP;
  576. } else if (cpu.__cycle === 1){
  577. cpu.__AR = cpu.__mem.byte;
  578. cpu.Z = (cpu.__AR === 0);
  579. cpu.N = BITM.isOn(cpu.__AR, 7);
  580. } else if (cpu.__step === 2){
  581. cpu.__SP = (cpu.__SP === 255) ? 0 : cpu.__SP + 1;
  582. cpu.__op = -1;
  583. }
  584. break;
  585. case 0x08: // PHP
  586. if (cpu.__cycle === 0){
  587. cpu.__mem.address = 0x0100 | cpu.__SP;
  588. } else if (cpu.__cycle === 1){
  589. cpu.__mem.byte = cpu.__PR;
  590. cpu.__SP = (cpu.__SP === 0) ? 255 : cpu.__SP - 1;
  591. cpu.__op = -1;
  592. }
  593. break;
  594. case 0x28: // PLP
  595. if (cpu.__cycle === 0){
  596. cpu.__mem.address = 0x0100 | cpu.__SP;
  597. } else if (cpu.__cycle === 1){
  598. cpu.__SP = cpu.__mem.byte;
  599. } else if (cpu.__cycle === 2){
  600. cpu.__SP = (cpu.__SP === 255) ? 0 : cpu.__SP + 1;
  601. cpu.__op = -1;
  602. }
  603. break;
  604. }
  605. cpu.__cycle += 1;
  606. }
  607. function STX(cpu){
  608. let pmode = [null, 0x86, null, 0x96, 0x8E, null, null, null, null, null].indexOf(cpu.__op);
  609. if (ProcessOp(cpu, pmode) === true){
  610. cpu.__op = -1;
  611. cpu.__mem.address = cpu.__opv;
  612. cpu.__mem.byte = cpu.__XR;
  613. }
  614. }
  615. function STY(cpu){
  616. let pmode = [null, 0x84, 0x94, null, 0x8C, null, null, null, null, null].indexOf(cpu.__op);
  617. if (ProcessOp(cpu, pmode) === true){
  618. cpu.__op = -1;
  619. cpu.__mem.address = cpu.__opv;
  620. cpu.__mem.byte = cpu.__YR;
  621. }
  622. }
  623. // --------------------------------------------------------------------------------------------
  624. // Test to see if both a and b's high byte is the same... same page.
  625. function SamePage(a, b){
  626. return ((a >> 8) == (b >> 8));
  627. }
  628. function ByteWrap(v, a){
  629. v += a;
  630. if (v < 0){v += 256;}
  631. else if (v > 255){v -= 256;}
  632. return v;
  633. }
  634. function StackPush(cpu, v){
  635. cpu.__mem.address = 0x0100 | cpu.__SP;
  636. cpu.__mem.byte = v & 0xFF;
  637. cpu.__SP = ByteWrap(cpu.__SP, -1);
  638. }
  639. function StackPop(cpu){
  640. cpu.__mem.address = 0x0100 | cpu.__SP;
  641. cpu.__SP = ByteWrap(cpu.__SP, 1);
  642. return cpu.__mem.byte;
  643. }
  644. function PCHI(cpu, b){
  645. cpu.__PC = (cpu.__PC & 0x00FF) | (b << 8);
  646. }
  647. function PCLOW(cpu, b){
  648. cpu.__PC = (cpu.__PC & 0xFF00) | b;
  649. }
  650. function PCUp(cpu, amount){
  651. if (cpu.__pcc === 1){
  652. PCHI(cpu, ((cpu.__PC & 0xFF00) >> 8) + 1);
  653. cpu.__pcc = 0;
  654. } else if ((cpu.__PC & 0x00FF) + amount > 255){
  655. cpu.__pcc = 1;
  656. PCLOW(cpu, ((cpu.__PC & 0x00FF) + amount) - 256);
  657. } else {
  658. PCLOW(cpu, (cpu.__PC & 0x00FF) + amount);
  659. }
  660. }
  661. function PCDown(cpu, amount){
  662. if (cpu.__pcc === -1){
  663. PCHI(cpu, ((cpu.__PC & 0xFF00) >> 8) - 1);
  664. cpu.__pcc = 0;
  665. } else if ((cpu.__PC & 0x00FF) - amount < 0){
  666. cpu.__pcc = -1;
  667. PCLOW(cpu, ((cpu.__PC & 0x00FF) - amount) + 256);
  668. } else {
  669. PCLOW(cpu, (cpu.__PC & 0x00FF) - amount);
  670. }
  671. }
  672. function MemAddrFrom(cpu, addr){
  673. cpu.__mem.address = addr;
  674. let v = cpu.__mem.byte;
  675. cpu.__mem.address = (addr + 1) & 0xFF;
  676. v |= cpu.__mem.byte << 8;
  677. return v;
  678. }
  679. function ALU(cpu, m, b){
  680. let A = 0;
  681. let IAR = cpu.__AR;
  682. if (cpu.D === 0){ // Binary Mode
  683. switch(m){
  684. case 0: // Addition
  685. A = (cpu.__AR + b) + ((cpu.C === 1) ? 1 : 0);
  686. break;
  687. case 1: // Subtraction
  688. A = (cpu.__AR - b) - ((cpu.C === 0) ? 1 : 0);
  689. A += (A < 0) ? 256 : 0;
  690. break;
  691. }
  692. cpu.__AR = A & 0xFF;
  693. // Both inputs have the same sign, but the output doesn't match the sign of the input.
  694. // (Twos Compliment)
  695. cpu.V = ((IAR & 0x80) === (b & 0x80)) && ((IAR & 0x80) !== (cpu.__AR & 0x80));
  696. cpu.C = (m === 0) ? (A >= 256) : (cpu.V === 0);
  697. cpu.N = BITM.isOn(cpu.__AR, 7);
  698. cpu.Z = (cpu.__AR === 0);
  699. } else { // Decimal (BCD) Mode
  700. // Borrowed from information located on...
  701. // http://www.6502.org/tutorials/decimal_mode.html
  702. let AL = 0;
  703. switch(m){
  704. case 0: // Addition
  705. AL = (cpu.__AR & 0x0F) + (b & 0x0F) + ((cpu.C === 1) ? 1 : 0);
  706. AL = (AL < 0x0A) ? AL : ((AL + 0x06) & 0x0F) + 0x10;
  707. A = (cpu.__AR & 0xF0) + (b & 0xF0) + AL;
  708. A += (A >= 0xA0) ? 0x60 : 0;
  709. cpu.__AR = A & 0xFF;
  710. cpu.Z = (IAR + b + ((cpu.C === 1) ? 1 : 0) === 256);
  711. cpu.C = (A >= 0x100);
  712. cpu.N = (BITM.isOn(A, 7)) ? 1 : 0;
  713. cpu.V = (A >= -128 && A <= 127) ? 0 : 1;
  714. break;
  715. case 1: // Subtraction
  716. AL = (cpu.__AR & 0x0F) - (b & 0x0F) + (cpu.C - 1);
  717. AL = (AL >= 0) ? AL : ((AL - 0x06) & 0x0F) - 0x10;
  718. A = (A & 0xF0) - (b & 0xF0) + AL;
  719. A -= (A >= 0) ? 0 : 0x60;
  720. cpu.__AR = A & 0xFF;
  721. cpu.V = ((IAR & 0x80) === (b & 0x80)) && ((IAR & 0x80) !== (cpu.__AR & 0x80));
  722. cpu.Z = ((cpu.__AR - b) - ((cpu.C === 0) ? 1 : 0) === 0);
  723. cpu.C = (cpu.V === 0);
  724. cpu.N = BITM.isOn(cpu.__AR, 7);
  725. break;
  726. }
  727. }
  728. return cpu.__AR;
  729. }
  730. // --------------------------------------------------------------------------------------------
  731. // --------------------------------------------------------------------------------------------
  732. // --------------------------------------------------------------------------------------------
  733. class CPU{
  734. constructor(){
  735. // Registers
  736. this.__PC = 0; // Program Counter (16 bit)
  737. this.__SP = 255; // Stack Pointer (8 bit)
  738. this.__PR = 32; // Status Register (8 bit, bit 5 is always 1)
  739. this.__XR = 0; // X Register (8 bit)
  740. this.__YR = 0; // Y Register (8 bit)
  741. this.__AR = 0; // Accumulator Register (8 bit)
  742. // Variables to watch for Hardware Interrupts.
  743. this.__nmi = false;
  744. this.__irq = false;
  745. this.__rst = true;
  746. this.__iinit = false;
  747. // Variable for tracking tick operations.
  748. this.__op = -1;
  749. this.__ophold = 0;
  750. this.__opv = 0;
  751. this.__cycle = 0;
  752. this.__pcc = 0; // Program Counter Carry.
  753. // Memory module or controller.
  754. this.__mem = null; // Must be explicitly attached.
  755. // Hold any created CLK instances.
  756. this.__clkfn = null;
  757. }
  758. // ----------------------------------------
  759. // CPU Registers. Here for debug purposes.
  760. get PC(){return this.__PC;}
  761. get SP(){return this.__SP;}
  762. get P(){return this.__PR;}
  763. get X(){return this.__XR;}
  764. get Y(){return this.__YR;}
  765. get A(){return this.__AR;}
  766. // ----------------------------------------
  767. // Quick Flag Access
  768. get N(){return (BITM.isOn(this.__PR, 7)) ? 1 : 0;}
  769. set N(n){this.__PR = (n === true || n === 1) ? BITM.set(this.__PR, 7) : BITM.clear(this.__PR, 7);}
  770. get V(){return (BITM.isOn(this.__PR, 6)) ? 1 : 0;}
  771. set V(v){this.__PR = (v === true || v === 1) ? BITM.set(this.__PR, 6) : BITM.clear(this.__PR, 6);}
  772. get B(){return (BITM.isOn(this.__PR, 4)) ? 1 : 0;}
  773. set B(b){this.__PR = (b === true || b === 1) ? BITM.set(this.__PR, 4) : BITM.clear(this.__PR, 4);}
  774. get D(){return (BITM.isOn(this.__PR, 3)) ? 1 : 0;}
  775. set D(d){this.__PR = (d === true || d === 1) ? BITM.set(this.__PR, 3) : BITM.clear(this.__PR, 3);}
  776. get I(){return (BITM.isOn(this.__PR, 2)) ? 1 : 0;}
  777. set I(i){this.__PR = (i === true || i === 1) ? BITM.set(this.__PR, 2) : BITM.clear(this.__PR, 2);}
  778. get Z(){return (BITM.isOn(this.__PR, 1)) ? 1 : 0;}
  779. set Z(z){this.__PR = (z === true || z === 1) ? BITM.set(this.__PR, 1) : BITM.clear(this.__PR, 1);}
  780. get C(){return (BITM.isOn(this.__PR, 0)) ? 1 : 0;}
  781. set C(c){this.__PR = (c === true || c === 1) ? BITM.set(this.__PR, 0) : BITM.clear(this.__PR, 0);}
  782. // ----------------------------------------
  783. // Hardware interrupt triggers. Settable only.
  784. set NMI(n){
  785. this.__nmi = (n === true);
  786. this.__iinit = true;
  787. }
  788. set IRQ(q){
  789. if (BITM.val(this.__PR, 2) === 0 && this.__nmi === false){
  790. this.__irq = (q === true);
  791. this.__iinit = true;
  792. }
  793. }
  794. set reset(r){
  795. this.__rst = (r === true);
  796. }
  797. // -----------------------------------------
  798. // Set and Get Memory property.
  799. get memory(){return this.__mem;}
  800. set memory(m){
  801. if (!(m instanceof IMem))
  802. throw new ValueError("Expected Memory instance object.");
  803. this.__mem = m;
  804. }
  805. // -----------------------------------------
  806. hardReset(){
  807. this.__pcc = 0;
  808. this.__cycle = 0;
  809. this.__op = -1;
  810. this.__ophold = 0;
  811. this.__opv = 0;
  812. this.__iinit = false;
  813. this.__irq = false;
  814. this.__nmi = false;
  815. this.__rst = false;
  816. this.__SP = 255;
  817. this.__PR = 32;
  818. this.__mem.address = 0xFFFC;
  819. this.__PC = this.__mem.byte;
  820. this.__mem.address = 0xFFFD;
  821. this.__PC |= this.__mem.byte << 8;
  822. }
  823. clk(){
  824. if (this.__clkfn === null){
  825. this.__clkfn = (function(){
  826. if (this.__pcc !== 0){
  827. if (this.__pcc > 0) {
  828. PCUp(this, 1);
  829. } else {
  830. PCDown(this, 1);
  831. }
  832. } else if (this.__op < 0){
  833. if (this.__rst){
  834. this.__rst = false;
  835. this.__mem.address = 0xFFFC;
  836. this.__PC = this.__mem.byte;
  837. this.__mem.address = 0xFFFD;
  838. this.__PC |= this.__mem.byte << 8;
  839. // Disabling the IRQ interrupts is the ONLY flag that must be set
  840. // during reset. The others are random.
  841. this.I = 1;
  842. } else if (this.__nmi) {
  843. if (this.__iinit){
  844. this.__cycle = 0;
  845. this.__iinit = false;
  846. }
  847. BRK(this, true); // BRK does all of the interrupt work. The 'true' tells BRK to use the nmi vectors.
  848. if (this.__cycle === 6)
  849. this.__nmi = false;
  850. } else if (this.__irq) {
  851. if (this.I === 0){
  852. if (this.__iinit){
  853. this.__cycle = 0;
  854. this.__iinit = false;
  855. }
  856. BRK(this); // Because both the BRK opcode and an IRQ interrupt do the same things!
  857. if (this.__cycle === 6)
  858. this.__irq = false;
  859. } else {
  860. // NOPE... try again!
  861. this.__irq = false;
  862. this.__clkfn();
  863. }
  864. } else {
  865. this.__cycle = 0;
  866. this.__mem.address = this.__PC;
  867. this.__op = this.__mem.byte;
  868. //PCUp(this, 1);
  869. }
  870. } else {
  871. switch(this.__op){
  872. case 0x69: case 0x65: case 0x75: case 0x6D: case 0x7D: case 0x79: case 0x61: case 0x71:
  873. ADC(this); break;
  874. case 0x29: case 0x25: case 0x35: case 0x2D: case 0x3D: case 0x39: case 0x21: case 0x31:
  875. AND(this); break;
  876. case 0x0A: case 0x06: case 0x16: case 0x0E: case 0x1E:
  877. ASL(this); break;
  878. case 0x24: case 0x2C:
  879. BIT(this); break;
  880. case 0x10: case 0x30: case 0x50: case 0x70: case 0x90: case 0xB0: case 0xD0: case 0xF0:
  881. BRANCH(this); break;
  882. case 0x00:
  883. BRK(this); break;
  884. case 0xC9: case 0xC5: case 0xD5: case 0xCD: case 0xDD: case 0xD9: case 0xC1: case 0xD1:
  885. CMP(this); break;
  886. case 0xE0: case 0xE4: case 0xEC:
  887. CPX(this); break;
  888. case 0xC0: case 0xC4: case 0xCC:
  889. CPY(this); break;
  890. case 0xC6: case 0xD6: case 0xCE: case 0xDE:
  891. DEC(this); break;
  892. case 0x49: case 0x45: case 0x55: case 0x4D: case 0x5D: case 0x59: case 0x41: case 0x51:
  893. EOR(this); break;
  894. case 0x18: case 0x38: case 0x58: case 0x78: case 0xB8: case 0xD8: case 0xF8:
  895. FLAG(this); break;
  896. case 0xE6: case 0xF6: case 0xEE: case 0xFE:
  897. INC(this); break;
  898. case 0x4C: case 0x6C:
  899. JMP(this); break;
  900. case 0x20:
  901. JSR(this); break;
  902. case 0xA9: case 0xA5: case 0xB5: case 0xAD: case 0xBD: case 0xB9: case 0xA1: case 0xB1:
  903. LDA(this); break;
  904. case 0xA2: case 0xA6: case 0xB6: case 0xAE: case 0xBE:
  905. LDX(this); break;
  906. case 0xA0: case 0xA4: case 0xB4: case 0xAC: case 0xBC:
  907. LDY(this); break;
  908. case 0x4A: case 0x46: case 0x56: case 0x4E: case 0x5E:
  909. LSR(this); break;
  910. case 0xEA:
  911. // NOP
  912. if (this.__step == 1){
  913. this.__op = -1;
  914. } else {this.__step += 1;}
  915. break;
  916. case 0x09: case 0x05: case 0x15: case 0x0D: case 0x1D: case 0x19: case 0x01: case 0x11:
  917. ORA(this); break;
  918. case 0xAA: case 0x8A: case 0xCA: case 0xE8: case 0xA8: case 0x98: case 0x88: case 0xC8:
  919. REGISTER(this); break;
  920. case 0x2A: case 0x26: case 0x36: case 0x2E: case 0x3E:
  921. ROL(this); break;
  922. case 0x6A: case 0x66: case 0x76: case 0x6E: case 0x7E:
  923. ROR(this); break;
  924. case 0x40:
  925. RTI(this); break;
  926. case 0x60:
  927. RTS(this); break;
  928. case 0xE9: case 0xE5: case 0xF5: case 0xED: case 0xFD: case 0xF9: case 0xE1: case 0xF1:
  929. SBC(this); break;
  930. case 0x85: case 0x95: case 0x8D: case 0x9D: case 0x99: case 0x81: case 0x91:
  931. STA(this); break;
  932. case 0x9A: case 0xBA: case 0x48: case 0x68: case 0x08: case 0x28:
  933. STACK(this); break;
  934. case 0x86: case 0x96: case 0x8E:
  935. STX(this); break;
  936. case 0x84: case 0x94: case 0x8C:
  937. STY(this); break;
  938. }
  939. if (this.__op < 0)
  940. PCUp(this, 1);
  941. }
  942. }).bind(this);
  943. }
  944. return this.__clkfn;
  945. }
  946. }
  947. module.exports = CPU;