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.

275 lines
7.6KB

  1. const expect = require('chai').expect;
  2. const MOS6502 = require('../src/MOS/6502');
  3. const Mem = require('../src/memory');
  4. describe("Testing MOS6502 CPU...", function(){
  5. var asm = new MOS6502.Assembler();
  6. var cpu = new MOS6502.CPU();
  7. var tick = cpu.clk();
  8. cpu.memory = new Mem.RAM(256);
  9. cpu.memory.load(0xFFFC, [0x00, 0x00]);
  10. it("Resetting (IRQ Disabled flag must be on", function(){
  11. cpu.reset = true;
  12. tick(); // One tick to handle resets.
  13. expect(cpu.I).to.equal(1);
  14. expect(cpu.PC).to.equal(0); // Test program counter set to the vector stored at 0xFFFC - 0xFFFD
  15. });
  16. describe("Testing flag set/clear calls...", function(){
  17. it("CLC / SEC", function(){
  18. cpu.memory.clearPage(0);
  19. cpu.memory.load(0, [0x18, 0x38]);
  20. cpu.reset = true;
  21. tick(); // reset.
  22. tick(); tick(); // Two ticks to process opcode.
  23. expect(cpu.C).to.equal(0);
  24. tick(); tick();
  25. expect(cpu.C).to.equal(1);
  26. });
  27. it("CLI / SEI", function(){
  28. cpu.memory.clearPage(0);
  29. cpu.memory.load(0, [0x58, 0x78]);
  30. cpu.reset = true;
  31. tick();
  32. tick(); tick();
  33. expect(cpu.I).to.equal(0);
  34. tick(); tick();
  35. expect(cpu.I).to.equal(1);
  36. });
  37. it("CLD / SED", function(){
  38. cpu.memory.clearPage(0);
  39. cpu.memory.load(0, [0xD8, 0xF8]);
  40. cpu.reset = true;
  41. tick();
  42. tick(); tick();
  43. expect(cpu.D).to.equal(0);
  44. tick(); tick();
  45. expect(cpu.D).to.equal(1);
  46. });
  47. it("CLV", function(){
  48. cpu.memory.clearPage(0);
  49. cpu.memory.load(0, [0xB8]);
  50. cpu.reset = true;
  51. tick();
  52. tick(); tick();
  53. expect(cpu.V).to.equal(0);
  54. });
  55. });
  56. describe("Testing LDA...", function(){
  57. it("LDA Immediate", function(){
  58. let prg = "LDA #$01\n";
  59. prg += "LDA #$BB";
  60. asm.reset().compile(prg);
  61. cpu.memory.clearPage(0);
  62. cpu.memory.load(0, asm.result());
  63. cpu.hardReset();
  64. //cpu.reset = true;
  65. //tick();
  66. tick();
  67. tick();
  68. expect(cpu.A).to.equal(0x01);
  69. tick();
  70. expect(cpu.A).to.equal(0x01);
  71. tick();
  72. expect(cpu.A).to.equal(0xBB);
  73. });
  74. it("LDA Zero Page");
  75. it("LDA Zero Page, X");
  76. it("LDA Absolute");
  77. it("LDA Absolute, X");
  78. it("LDA Absolute, Y");
  79. it("LDA Indirect, X");
  80. it("LDA Indirect, Y");
  81. });
  82. describe("Testing ADC...", function(){
  83. describe("ADC Binary Mode...", function(){
  84. it("ADC Immediate", function(){
  85. let prg = "CLC\n";
  86. prg += "CLV\n";
  87. prg += "CLD\n";
  88. prg += "CLI\n";
  89. prg += "LDA #$00\n";
  90. prg += "ADC #$01\n";
  91. prg += "ADC #$7F\n";
  92. prg += "ADC #$80";
  93. asm.reset().compile(prg);
  94. cpu.memory.clearPage(0);
  95. cpu.memory.load(0, asm.result());
  96. cpu.hardReset();
  97. //cpu.reset = true;
  98. //tick(); // To reset;
  99. while (cpu.PC !== 13){
  100. switch(cpu.PC){
  101. case 6:
  102. expect(cpu.A).to.equal(0x00); break;
  103. case 8:
  104. expect(cpu.A).to.equal(0x01);
  105. expect(cpu.Z).to.equal(0);
  106. expect(cpu.C).to.equal(0);
  107. expect(cpu.N).to.equal(0);
  108. break;
  109. case 10:
  110. expect(cpu.A).to.equal(0x80);
  111. expect(cpu.Z).to.equal(0);
  112. expect(cpu.C).to.equal(0);
  113. expect(cpu.N).to.equal(1);
  114. break;
  115. case 12:
  116. expect(cpu.A).to.equal(0x00);
  117. expect(cpu.Z).to.equal(1);
  118. expect(cpu.C).to.equal(1);
  119. expect(cpu.N).to.equal(0);
  120. }
  121. tick();
  122. }
  123. });
  124. it("ADC Zero Page");
  125. it("ADC Zero Page, X");
  126. it("ADC Absolute");
  127. it("ADC Absolute, X");
  128. it("ADC Absolute, Y");
  129. it("ADC Indirect, X");
  130. it("ADC Indirect, Y");
  131. });
  132. describe("ADC Decimal (BCD) Mode...", function(){
  133. it("ADC Immediate", function(){
  134. let prg = "CLC\n";
  135. prg += "CLV\n";
  136. prg += "SED\n";
  137. prg += "CLI\n";
  138. prg += "LDA #$00\n";
  139. prg += "ADC #$01\n";
  140. prg += "ADC #$09\n";
  141. prg += "ADC #$89\n";
  142. prg += "ADC #$01\n";
  143. prg += "LDA #$0A\n"; // 0A is an invalid BCD number
  144. prg += "CLC\n"; // Need to reset the clear flag from previous add.
  145. prg += "ADC #$01";
  146. asm.reset().compile(prg);
  147. cpu.memory.clearPage(0);
  148. cpu.memory.load(0, asm.result());
  149. cpu.hardReset();
  150. //cpu.reset = true;
  151. //tick(); // To reset;
  152. while (cpu.PC !== 20){
  153. switch(cpu.PC){
  154. case 6:
  155. expect(cpu.A).to.equal(0x00); break;
  156. case 8:
  157. expect(cpu.A).to.equal(0x01);
  158. expect(cpu.Z).to.equal(0);
  159. expect(cpu.C).to.equal(0);
  160. expect(cpu.N).to.equal(0);
  161. break;
  162. case 10:
  163. expect(cpu.A).to.equal(0x10);
  164. expect(cpu.Z).to.equal(0);
  165. expect(cpu.C).to.equal(0);
  166. expect(cpu.N).to.equal(0);
  167. break;
  168. case 12:
  169. expect(cpu.A).to.equal(0x99);
  170. expect(cpu.Z).to.equal(0);
  171. expect(cpu.C).to.equal(0);
  172. expect(cpu.N).to.equal(1);
  173. break;
  174. case 14:
  175. expect(cpu.A).to.equal(0x00);
  176. expect(cpu.Z).to.equal(0);
  177. expect(cpu.C).to.equal(1);
  178. expect(cpu.N).to.equal(0);
  179. break;
  180. case 19:
  181. //console.log(cpu.A);
  182. expect(cpu.A).to.equal(0x11);
  183. break;
  184. }
  185. tick();
  186. }
  187. });
  188. it("ADC Zero Page");
  189. it("ADC Zero Page, X");
  190. it("ADC Absolute");
  191. it("ADC Absolute, X");
  192. it("ADC Absolute, Y");
  193. it("ADC Indirect, X");
  194. it("ADC Indirect, Y");
  195. });
  196. });
  197. describe("Testing SBC...", function(){
  198. describe("SBC Binary Mode...", function(){
  199. it("SBC Immediate", function(){
  200. let prg = "SEC\n";
  201. prg += "CLV\n";
  202. prg += "CLD\n";
  203. prg += "CLI\n";
  204. prg += "LDA #$01\n";
  205. prg += "SBC #$01\n";
  206. prg += "SBC #$01";
  207. asm.reset().compile(prg);
  208. cpu.memory.clearPage(0);
  209. cpu.memory.load(0, asm.result());
  210. cpu.hardReset();
  211. //cpu.reset = true;
  212. //tick(); // To reset;
  213. while(cpu.PC !== 11){
  214. // NOTE TO SELF: Depending on the OP code, these tests could be
  215. // checked multiple times, as the cpu may sit at a program cntr
  216. // for a couple cycles depending on the OP.
  217. switch(cpu.PC){
  218. case 1:
  219. expect(cpu.C).to.be.equal(1); break;
  220. case 6:
  221. expect(cpu.A).to.be.equal(0x01); break;
  222. case 8:
  223. expect(cpu.A).to.be.equal(0x00);
  224. expect(cpu.C).to.be.equal(1);
  225. expect(cpu.Z).to.be.equal(1);
  226. expect(cpu.N).to.be.equal(0);
  227. break;
  228. case 9:
  229. expect(cpu.A).to.be.equal(0xFF);
  230. expect(cpu.C).to.be.equal(0);
  231. expect(cpu.Z).to.be.equal(0);
  232. expect(cpu.N).to.be.equal(1);
  233. break;
  234. }
  235. tick();
  236. }
  237. });
  238. it("SBC Zero Page");
  239. it("SBC Zero Page, X");
  240. it("SBC Absolute");
  241. it("SBC Absolute, X");
  242. it("SBC Absolute, Y");
  243. it("SBC Indirect, X");
  244. it("SBC Indirect, Y");
  245. });
  246. describe("SBC Decimal (BCD) Mode...", function(){
  247. it("SBC Immediate");
  248. it("SBC Zero Page");
  249. it("SBC Zero Page, X");
  250. it("SBC Absolute");
  251. it("SBC Absolute, X");
  252. it("SBC Absolute, Y");
  253. it("SBC Indirect, X");
  254. it("SBC Indirect, Y");
  255. });
  256. });
  257. });