Fantasy 8Bit system (F8), is a fantasy 8bit console and a set of libraries for creating fantasy 8bit consoles.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

489 lines
14KB

  1. const expect = require('chai').expect;
  2. const Assembler = require('../src/chip/MOS6502/assembler.js');
  3. describe("Testing MOS6502 Assembler...", function(){
  4. var asm = new Assembler();
  5. it(".reset()", function(){
  6. asm.compile("ADC $44");
  7. asm.reset();
  8. expect(asm.PC).to.equal(0);
  9. expect(Object.keys(asm.variables).length).to.equal(0);
  10. expect(Object.keys(asm.jumplabels).length).to.equal(0);
  11. expect(asm.result().length).to.equal(0);
  12. });
  13. it(".result()", function(){
  14. asm.reset();
  15. asm.compile("ADC $44");
  16. expect(asm.result().length).to.be.gt(0);
  17. });
  18. describe(".compile()", function(){
  19. it(".define directive", function(){
  20. asm.compile(".define TAG $44");
  21. let vars = asm.variables;
  22. expect(vars).to.have.key('TAG');
  23. expect(vars['TAG']).to.equal(0x44);
  24. });
  25. it(".def directive (.define shorthand)", function(){
  26. asm.compile(".define TAG2 $55");
  27. let vars = asm.variables;
  28. expect(vars['TAG2']).to.exist;
  29. expect(vars['TAG2']).to.equal(0x55);
  30. });
  31. it("Jump labels", function(){
  32. let pc = asm.PC;
  33. asm.compile("loop:\nBNE loop");
  34. let jmp = asm.jumplabels;
  35. expect(jmp).to.have.key('loop');
  36. expect(jmp['loop']).to.equal(pc);
  37. });
  38. it(".bytes directive", function(){
  39. let res = asm.reset().compile(".BYTES $44, $55, $66, $77, b10110101, 128").result();
  40. expect(res.length).to.equal(6);
  41. expect(res[0]).to.equal(0x44);
  42. expect(res[1]).to.equal(0x55);
  43. expect(res[2]).to.equal(0x66);
  44. expect(res[3]).to.equal(0x77);
  45. expect(res[4]).to.equal(parseInt('10110101', 2));
  46. expect(res[5]).to.equal(128);
  47. });
  48. it(".byt directive (.bytes shorthand)", function(){
  49. let res = asm.reset().compile(".BYT $44, $55, $66").result();
  50. expect(res.length).to.equal(3);
  51. expect(res[0]).to.equal(0x44);
  52. expect(res[1]).to.equal(0x55);
  53. expect(res[2]).to.equal(0x66);
  54. });
  55. it("Jump label before and after .byte directive", function(){
  56. asm.reset();
  57. let pc1 = asm.PC;
  58. asm.compile("bytes: .bytes $44, $55, $66, $77");
  59. let pc2 = asm.PC;
  60. asm.compile("loop: BNE loop");
  61. let jmp = asm.jumplabels;
  62. //expect(jmp).to.have.key('bytes');
  63. expect(jmp['bytes']).to.equal(pc1);
  64. //expect(jmp).to.have.key('loop');
  65. expect(jmp['loop']).to.equal(pc2);
  66. let res = asm.result();
  67. expect(res.length).to.equal(6);
  68. expect(res[0]).to.equal(0x44);
  69. expect(res[1]).to.equal(0x55);
  70. expect(res[2]).to.equal(0x66);
  71. expect(res[3]).to.equal(0x77);
  72. expect(res[4]).to.equal(0xD0);
  73. expect(res[5]).to.equal(0xFE);
  74. });
  75. it("ADC Immediate", function(){
  76. asm.reset();
  77. asm.compile("ADC #$44");
  78. let res = asm.result();
  79. expect(res.length).to.equal(2);
  80. expect(res[0]).to.equal(0x69);
  81. expect(res[1]).to.equal(0x44);
  82. });
  83. it("ADC Zero Page", function(){
  84. asm.reset();
  85. asm.compile("ADC $44");
  86. let res = asm.result();
  87. expect(res.length).to.equal(2);
  88. expect(res[0]).to.equal(0x65);
  89. expect(res[1]).to.equal(0x44);
  90. });
  91. it("ADC Zero Page, X", function(){
  92. asm.reset();
  93. asm.compile("ADC $44, X");
  94. let res = asm.result();
  95. expect(res.length).to.equal(2);
  96. expect(res[0]).to.equal(0x75);
  97. expect(res[1]).to.equal(0x44);
  98. });
  99. it("ADC Absolute", function(){
  100. asm.reset();
  101. asm.compile("ADC $4400");
  102. let res = asm.result();
  103. expect(res.length).to.equal(3);
  104. expect(res[0]).to.equal(0x6D);
  105. expect(res[1]).to.equal(0x00);
  106. expect(res[2]).to.equal(0x44);
  107. });
  108. it("ADC Absolute, X", function(){
  109. asm.reset();
  110. asm.compile("ADC $4400,X");
  111. let res = asm.result();
  112. expect(res.length).to.equal(3);
  113. expect(res[0]).to.equal(0x7D);
  114. expect(res[1]).to.equal(0x00);
  115. expect(res[2]).to.equal(0x44);
  116. });
  117. it("ADC Absolute, Y", function(){
  118. asm.reset();
  119. asm.compile("ADC $4400, Y");
  120. let res = asm.result();
  121. expect(res.length).to.equal(3);
  122. expect(res[0]).to.equal(0x79);
  123. expect(res[1]).to.equal(0x00);
  124. expect(res[2]).to.equal(0x44);
  125. });
  126. it("ADC Indirect, X", function(){
  127. asm.reset();
  128. asm.compile("ADC ($44,X)");
  129. let res = asm.result();
  130. expect(res.length).to.equal(2);
  131. expect(res[0]).to.equal(0x61);
  132. expect(res[1]).to.equal(0x44);
  133. });
  134. it("ADC Indirect, Y", function(){
  135. asm.reset();
  136. asm.compile("ADC ($44), Y");
  137. let res = asm.result();
  138. expect(res.length).to.equal(2);
  139. expect(res[0]).to.equal(0x71);
  140. expect(res[1]).to.equal(0x44);
  141. });
  142. // -------------------------------------------------------------
  143. it("AND Immediate");
  144. it("AND Zero Page");
  145. it("AND Zero Page, X");
  146. it("AND Absolute");
  147. it("AND Absolute, X");
  148. it("AND Absolute, Y");
  149. it("AND Indirect, X");
  150. it("AND Indirect, Y");
  151. // -------------------------------------------------------------
  152. it("ASL Accumulator");
  153. it("ASL Zero Page");
  154. it("ASL Zero Page, X");
  155. it("ASL Absolute");
  156. it("ASL Absolute, X");
  157. // -------------------------------------------------------------
  158. it("BIT Zero Page");
  159. it("BIT Absolute");
  160. // -------------------------------------------------------------
  161. it("BPL");
  162. it("BMI");
  163. it("BVC");
  164. it("BVS");
  165. it("BCC");
  166. it("BCS");
  167. it("BNE");
  168. it("BEQ");
  169. // -------------------------------------------------------------
  170. it ("BRK", function(){
  171. asm.reset();
  172. let res = asm.compile("BRK").result();
  173. expect(res.length).to.equal(1);
  174. expect(res[0]).to.equal(0x00);
  175. });
  176. // -------------------------------------------------------------
  177. it("CMP Immediate");
  178. it("CMP Zero Page");
  179. it("CMP Zero Page, X");
  180. it("CMP Absolute");
  181. it("CMP Absolute, X");
  182. it("CMP Absolute, Y");
  183. it("CMP Indirect, X");
  184. it("CMP Indirect, Y");
  185. // -------------------------------------------------------------
  186. it("CPX Immediate");
  187. it("CPX Zero Page");
  188. it("CPX Absolute");
  189. // -------------------------------------------------------------
  190. it("CPY Immediate");
  191. it("CPY Zero Page");
  192. it("CPY Absolute");
  193. // -------------------------------------------------------------
  194. it("DEC Zero Page");
  195. it("DEC Zero Page, X");
  196. it("DEC Absolute");
  197. it("DEC Absolute, X");
  198. // -------------------------------------------------------------
  199. it("EOR Immediate");
  200. it("EOR Zero Page");
  201. it("EOR Zero Page, X");
  202. it("EOR Absolute");
  203. it("EOR Absolute, X");
  204. it("EOR Absolute, Y");
  205. it("EOR Indirect, X");
  206. it("EOR Indirect, Y");
  207. // -------------------------------------------------------------
  208. it("CLC", function(){
  209. asm.reset();
  210. let res = asm.compile("CLC").result();
  211. expect(res.length).to.equal(1);
  212. expect(res[0]).to.equal(0x18);
  213. });
  214. it("CLD", function(){
  215. asm.reset();
  216. let res = asm.compile("CLD").result();
  217. expect(res.length).to.equal(1);
  218. expect(res[0]).to.equal(0xD8);
  219. });
  220. it("CLI", function(){
  221. asm.reset();
  222. let res = asm.compile("CLI").result();
  223. expect(res.length).to.equal(1);
  224. expect(res[0]).to.equal(0x58);
  225. });
  226. it("CLV", function(){
  227. asm.reset();
  228. let res = asm.compile("CLV").result();
  229. expect(res.length).to.equal(1);
  230. expect(res[0]).to.equal(0xB8);
  231. });
  232. it("SEC", function(){
  233. asm.reset();
  234. let res = asm.compile("SEC").result();
  235. expect(res.length).to.equal(1);
  236. expect(res[0]).to.equal(0x38);
  237. });
  238. it("SED", function(){
  239. asm.reset();
  240. let res = asm.compile("SED").result();
  241. expect(res.length).to.equal(1);
  242. expect(res[0]).to.equal(0xF8);
  243. });
  244. it("SEI", function(){
  245. asm.reset();
  246. let res = asm.compile("SEI").result();
  247. expect(res.length).to.equal(1);
  248. expect(res[0]).to.equal(0x78);
  249. });
  250. // -------------------------------------------------------------
  251. it("INC Zero Page");
  252. it("INC Zero Page, X");
  253. it("INC Absolute");
  254. it("INC Absolute, X");
  255. // -------------------------------------------------------------
  256. it("JMP Absolute");
  257. it("JMP Indirect");
  258. // -------------------------------------------------------------
  259. it("JRS Absolute");
  260. // -------------------------------------------------------------
  261. it("LDA Immediate");
  262. it("LDA Zero Page");
  263. it("LDA Zero Page, X");
  264. it("LDA Absolute");
  265. it("LDA Absolute, X");
  266. it("LDA Absolute, Y");
  267. it("LDA Indirect, X");
  268. it("LDA Indirect, Y");
  269. // -------------------------------------------------------------
  270. it("LDX Immediate");
  271. it("LDX Zero Page");
  272. it("LDX Zero Page, Y");
  273. it("LDX Absolute");
  274. it("LDX Absolute, Y");
  275. // -------------------------------------------------------------
  276. it("LDY Immediate");
  277. it("LDY Zero Page");
  278. it("LDY Zero Page, X");
  279. it("LDY Absolute");
  280. it("LDY Absolute, X");
  281. // -------------------------------------------------------------
  282. it("LSR Accumulator");
  283. it("LSR Zero Page");
  284. it("LSR Zero Page, X");
  285. it("LSR Absolute");
  286. it("LSR Absolute, X");
  287. // -------------------------------------------------------------
  288. it("NOP", function(){
  289. asm.reset();
  290. let res = asm.compile("NOP").result();
  291. expect(res.length).to.equal(1);
  292. expect(res[0]).to.equal(0xEA);
  293. });
  294. // -------------------------------------------------------------
  295. it("ORA Immediate");
  296. it("ORA Zero Page");
  297. it("ORA Zero Page, X");
  298. it("ORA Absolute");
  299. it("ORA Absolute, X");
  300. it("ORA Absolute, Y");
  301. it("ORA Indirect, X");
  302. it("ORA Indirect, Y");
  303. // -------------------------------------------------------------
  304. it("DEX", function(){
  305. asm.reset();
  306. let res = asm.compile("DEX").result();
  307. expect(res.length).to.equal(1);
  308. expect(res[0]).to.equal(0xCA);
  309. });
  310. it("DEY", function(){
  311. asm.reset();
  312. let res = asm.compile("DEY").result();
  313. expect(res.length).to.equal(1);
  314. expect(res[0]).to.equal(0x88);
  315. });
  316. it("INX", function(){
  317. asm.reset();
  318. let res = asm.compile("INX").result();
  319. expect(res.length).to.equal(1);
  320. expect(res[0]).to.equal(0xE8);
  321. });
  322. it("INY", function(){
  323. asm.reset();
  324. let res = asm.compile("INY").result();
  325. expect(res.length).to.equal(1);
  326. expect(res[0]).to.equal(0xC8);
  327. });
  328. it("TAX", function(){
  329. asm.reset();
  330. let res = asm.compile("TAX").result();
  331. expect(res.length).to.equal(1);
  332. expect(res[0]).to.equal(0xAA);
  333. });
  334. it("TAY", function(){
  335. asm.reset();
  336. let res = asm.compile("TAY").result();
  337. expect(res.length).to.equal(1);
  338. expect(res[0]).to.equal(0xA8);
  339. });
  340. it("TXA", function(){
  341. asm.reset();
  342. let res = asm.compile("TXA").result();
  343. expect(res.length).to.equal(1);
  344. expect(res[0]).to.equal(0x8A);
  345. });
  346. it("TYA", function(){
  347. asm.reset();
  348. let res = asm.compile("TYA").result();
  349. expect(res.length).to.equal(1);
  350. expect(res[0]).to.equal(0x98);
  351. });
  352. // -------------------------------------------------------------
  353. it("ROL Accumulator");
  354. it("ROL Zero Page");
  355. it("ROL Zero Page, X");
  356. it("ROL Absolute");
  357. it("ROL Absolute, X");
  358. // -------------------------------------------------------------
  359. it("ROR Accumulator");
  360. it("ROR Zero Page");
  361. it("ROR Zero Page, X");
  362. it("ROR Absolute");
  363. it("ROR Absolute, X");
  364. // -------------------------------------------------------------
  365. it("RTI", function(){
  366. asm.reset();
  367. let res = asm.compile("RTI").result();
  368. expect(res.length).to.equal(1);
  369. expect(res[0]).to.equal(0x40);
  370. });
  371. it("RTS", function(){
  372. asm.reset();
  373. let res = asm.compile("RTS").result();
  374. expect(res.length).to.equal(1);
  375. expect(res[0]).to.equal(0x60);
  376. });
  377. // -------------------------------------------------------------
  378. it("SBC Immediate");
  379. it("SBC Zero Page");
  380. it("SBC Zero Page, X");
  381. it("SBC Absolute");
  382. it("SBC Absolute, X");
  383. it("SBC Absolute, Y");
  384. it("SBC Indirect, X");
  385. it("SBC Indirect, Y");
  386. // -------------------------------------------------------------
  387. it("STA Zero Page");
  388. it("STA Zero Page, X");
  389. it("STA Absolute");
  390. it("STA Absolute, X");
  391. it("STA Absolute, Y");
  392. it("STA Indirect, X");
  393. it("STA Indirect, Y");
  394. // -------------------------------------------------------------
  395. it("PHA", function(){
  396. asm.reset();
  397. let res = asm.compile("PHA").result();
  398. expect(res.length).to.equal(1);
  399. expect(res[0]).to.equal(0x48);
  400. });
  401. it("PHP", function(){
  402. asm.reset();
  403. let res = asm.compile("PHP").result();
  404. expect(res.length).to.equal(1);
  405. expect(res[0]).to.equal(0x08);
  406. });
  407. it("PLA", function(){
  408. asm.reset();
  409. let res = asm.compile("PLA").result();
  410. expect(res.length).to.equal(1);
  411. expect(res[0]).to.equal(0x68);
  412. });
  413. it("PLP", function(){
  414. asm.reset();
  415. let res = asm.compile("PLP").result();
  416. expect(res.length).to.equal(1);
  417. expect(res[0]).to.equal(0x28);
  418. });
  419. it("TSX", function(){
  420. asm.reset();
  421. let res = asm.compile("TSX").result();
  422. expect(res.length).to.equal(1);
  423. expect(res[0]).to.equal(0xBA);
  424. });
  425. it("TXS", function(){
  426. asm.reset();
  427. let res = asm.compile("TXS").result();
  428. expect(res.length).to.equal(1);
  429. expect(res[0]).to.equal(0x9A);
  430. });
  431. // -------------------------------------------------------------
  432. it("STX Zero Page");
  433. it("STX Zero Page, Y");
  434. it("STX Absolute");
  435. // -------------------------------------------------------------
  436. it("STY Zero Page");
  437. it("STY Zero Page, X");
  438. it("STY Absolute");
  439. // -------------------------------------------------------------
  440. });
  441. });