Chip-8 emulator.
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.

Chip8.cpp 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #include "Chip8.hpp"
  2. #include <chrono>
  3. #include <cstdint>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <random>
  7. const unsigned int FONTSET_SIZE = 80;
  8. const unsigned int FONTSET_START_ADDRESS = 0x50;
  9. const unsigned int START_ADDRESS = 0x200;
  10. uint8_t fontset[FONTSET_SIZE] =
  11. {
  12. 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
  13. 0x20, 0x60, 0x20, 0x20, 0x70, // 1
  14. 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
  15. 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
  16. 0x90, 0x90, 0xF0, 0x10, 0x10, // 4
  17. 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
  18. 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
  19. 0xF0, 0x10, 0x20, 0x40, 0x40, // 7
  20. 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
  21. 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
  22. 0xF0, 0x90, 0xF0, 0x90, 0x90, // A
  23. 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
  24. 0xF0, 0x80, 0x80, 0x80, 0xF0, // C
  25. 0xE0, 0x90, 0x90, 0x90, 0xE0, // D
  26. 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
  27. 0xF0, 0x80, 0xF0, 0x80, 0x80 // F
  28. };
  29. Chip8::Chip8()
  30. : randGen(std::chrono::system_clock::now().time_since_epoch().count())
  31. {
  32. // Initialize PC
  33. pc = START_ADDRESS;
  34. // Load fonts into memory
  35. for (unsigned int i = 0; i < FONTSET_SIZE; ++i)
  36. {
  37. memory[FONTSET_START_ADDRESS + i] = fontset[i];
  38. }
  39. // Initialize RNG
  40. randByte = std::uniform_int_distribution<uint8_t>(0, 255U);
  41. // Set up function pointer table
  42. table[0x0] = &Chip8::Table0;
  43. table[0x1] = &Chip8::OP_1nnn;
  44. table[0x2] = &Chip8::OP_2nnn;
  45. table[0x3] = &Chip8::OP_3xkk;
  46. table[0x4] = &Chip8::OP_4xkk;
  47. table[0x5] = &Chip8::OP_5xy0;
  48. table[0x6] = &Chip8::OP_6xkk;
  49. table[0x7] = &Chip8::OP_7xkk;
  50. table[0x8] = &Chip8::Table8;
  51. table[0x9] = &Chip8::OP_9xy0;
  52. table[0xA] = &Chip8::OP_Annn;
  53. table[0xB] = &Chip8::OP_Bnnn;
  54. table[0xC] = &Chip8::OP_Cxkk;
  55. table[0xD] = &Chip8::OP_Dxyn;
  56. table[0xE] = &Chip8::TableE;
  57. table[0xF] = &Chip8::TableF;
  58. table0[0x0] = &Chip8::OP_00E0;
  59. table0[0xE] = &Chip8::OP_00EE;
  60. table8[0x0] = &Chip8::OP_8xy0;
  61. table8[0x1] = &Chip8::OP_8xy1;
  62. table8[0x2] = &Chip8::OP_8xy2;
  63. table8[0x3] = &Chip8::OP_8xy3;
  64. table8[0x4] = &Chip8::OP_8xy4;
  65. table8[0x5] = &Chip8::OP_8xy5;
  66. table8[0x6] = &Chip8::OP_8xy6;
  67. table8[0x7] = &Chip8::OP_8xy7;
  68. table8[0xE] = &Chip8::OP_8xyE;
  69. tableE[0x1] = &Chip8::OP_ExA1;
  70. tableE[0xE] = &Chip8::OP_Ex9E;
  71. tableF[0x07] = &Chip8::OP_Fx07;
  72. tableF[0x0A] = &Chip8::OP_Fx0A;
  73. tableF[0x15] = &Chip8::OP_Fx15;
  74. tableF[0x18] = &Chip8::OP_Fx18;
  75. tableF[0x1E] = &Chip8::OP_Fx1E;
  76. tableF[0x29] = &Chip8::OP_Fx29;
  77. tableF[0x33] = &Chip8::OP_Fx33;
  78. tableF[0x55] = &Chip8::OP_Fx55;
  79. tableF[0x65] = &Chip8::OP_Fx65;
  80. }
  81. void Chip8::LoadROM(char const* filename)
  82. {
  83. std::ifstream file(filename, std::ios::binary | std::ios::ate);
  84. if (file.is_open())
  85. {
  86. std::streampos size = file.tellg();
  87. char* buffer = new char[size];
  88. file.seekg(0, std::ios::beg);
  89. file.read(buffer, size);
  90. file.close();
  91. for (long i = 0; i < size; ++i)
  92. {
  93. memory[START_ADDRESS + i] = buffer[i];
  94. }
  95. delete[] buffer;
  96. }
  97. }
  98. void Chip8::Cycle()
  99. {
  100. // Fetch
  101. opcode = (memory[pc] << 8u) | memory[pc + 1];
  102. // Increment the PC before we execute anything
  103. pc += 2;
  104. // Decode and Execute
  105. ((*this).*(table[(opcode & 0xF000u) >> 12u]))();
  106. // Decrement the delay timer if it's been set
  107. if (delayTimer > 0)
  108. {
  109. --delayTimer;
  110. }
  111. // Decrement the sound timer if it's been set
  112. if (soundTimer > 0)
  113. {
  114. --soundTimer;
  115. }
  116. }
  117. void Chip8::Table0()
  118. {
  119. ((*this).*(table0[opcode & 0x000Fu]))();
  120. }
  121. void Chip8::Table8()
  122. {
  123. ((*this).*(table8[opcode & 0x000Fu]))();
  124. }
  125. void Chip8::TableE()
  126. {
  127. ((*this).*(tableE[opcode & 0x000Fu]))();
  128. }
  129. void Chip8::TableF()
  130. {
  131. ((*this).*(tableF[opcode & 0x00FFu]))();
  132. }
  133. void Chip8::OP_NULL()
  134. {}
  135. void Chip8::OP_00E0()
  136. {
  137. memset(video, 0, sizeof(video));
  138. }
  139. void Chip8::OP_00EE()
  140. {
  141. --sp;
  142. pc = stack[sp];
  143. }
  144. void Chip8::OP_1nnn()
  145. {
  146. uint16_t address = opcode & 0x0FFFu;
  147. pc = address;
  148. }
  149. void Chip8::OP_2nnn()
  150. {
  151. uint16_t address = opcode & 0x0FFFu;
  152. stack[sp] = pc;
  153. ++sp;
  154. pc = address;
  155. }
  156. void Chip8::OP_3xkk()
  157. {
  158. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  159. uint8_t byte = opcode & 0x00FFu;
  160. if (registers[Vx] == byte)
  161. {
  162. pc += 2;
  163. }
  164. }
  165. void Chip8::OP_4xkk()
  166. {
  167. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  168. uint8_t byte = opcode & 0x00FFu;
  169. if (registers[Vx] != byte)
  170. {
  171. pc += 2;
  172. }
  173. }
  174. void Chip8::OP_5xy0()
  175. {
  176. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  177. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  178. if (registers[Vx] == registers[Vy])
  179. {
  180. pc += 2;
  181. }
  182. }
  183. void Chip8::OP_6xkk()
  184. {
  185. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  186. uint8_t byte = opcode & 0x00FFu;
  187. registers[Vx] = byte;
  188. }
  189. void Chip8::OP_7xkk()
  190. {
  191. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  192. uint8_t byte = opcode & 0x00FFu;
  193. registers[Vx] += byte;
  194. }
  195. void Chip8::OP_8xy0()
  196. {
  197. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  198. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  199. registers[Vx] = registers[Vy];
  200. }
  201. void Chip8::OP_8xy1()
  202. {
  203. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  204. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  205. registers[Vx] |= registers[Vy];
  206. }
  207. void Chip8::OP_8xy2()
  208. {
  209. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  210. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  211. registers[Vx] &= registers[Vy];
  212. }
  213. void Chip8::OP_8xy3()
  214. {
  215. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  216. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  217. registers[Vx] ^= registers[Vy];
  218. }
  219. void Chip8::OP_8xy4()
  220. {
  221. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  222. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  223. uint16_t sum = registers[Vx] + registers[Vy];
  224. if (sum > 255U)
  225. {
  226. registers[0xF] = 1;
  227. }
  228. else
  229. {
  230. registers[0xF] = 0;
  231. }
  232. registers[Vx] = sum & 0xFFu;
  233. }
  234. void Chip8::OP_8xy5()
  235. {
  236. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  237. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  238. if (registers[Vx] > registers[Vy])
  239. {
  240. registers[0xF] = 1;
  241. }
  242. else
  243. {
  244. registers[0xF] = 0;
  245. }
  246. registers[Vx] -= registers[Vy];
  247. }
  248. void Chip8::OP_8xy6()
  249. {
  250. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  251. // Save LSB in VF
  252. registers[0xF] = (registers[Vx] & 0x1u);
  253. registers[Vx] >>= 1;
  254. }
  255. void Chip8::OP_8xy7()
  256. {
  257. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  258. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  259. if (registers[Vy] > registers[Vx])
  260. {
  261. registers[0xF] = 1;
  262. }
  263. else
  264. {
  265. registers[0xF] = 0;
  266. }
  267. registers[Vx] = registers[Vy] - registers[Vx];
  268. }
  269. void Chip8::OP_8xyE()
  270. {
  271. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  272. // Save MSB in VF
  273. registers[0xF] = (registers[Vx] & 0x80u) >> 7u;
  274. registers[Vx] <<= 1;
  275. }
  276. void Chip8::OP_9xy0()
  277. {
  278. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  279. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  280. if (registers[Vx] != registers[Vy])
  281. {
  282. pc += 2;
  283. }
  284. }
  285. void Chip8::OP_Annn()
  286. {
  287. uint16_t address = opcode & 0x0FFFu;
  288. index = address;
  289. }
  290. void Chip8::OP_Bnnn()
  291. {
  292. uint16_t address = opcode & 0x0FFFu;
  293. pc = registers[0] + address;
  294. }
  295. void Chip8::OP_Cxkk()
  296. {
  297. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  298. uint8_t byte = opcode & 0x00FFu;
  299. registers[Vx] = randByte(randGen) & byte;
  300. }
  301. void Chip8::OP_Dxyn()
  302. {
  303. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  304. uint8_t Vy = (opcode & 0x00F0u) >> 4u;
  305. uint8_t height = opcode & 0x000Fu;
  306. // Wrap if going beyond screen boundaries
  307. uint8_t xPos = registers[Vx] % VIDEO_WIDTH;
  308. uint8_t yPos = registers[Vy] % VIDEO_HEIGHT;
  309. registers[0xF] = 0;
  310. for (unsigned int row = 0; row < height; ++row)
  311. {
  312. uint8_t spriteByte = memory[index + row];
  313. for (unsigned int col = 0; col < 8; ++col)
  314. {
  315. uint8_t spritePixel = spriteByte & (0x80u >> col);
  316. uint32_t* screenPixel = &video[(yPos + row) * VIDEO_WIDTH + (xPos + col)];
  317. // Sprite pixel is on
  318. if (spritePixel)
  319. {
  320. // Screen pixel also on - collision
  321. if (*screenPixel == 0xFFFFFFFF)
  322. {
  323. registers[0xF] = 1;
  324. }
  325. // Effectively XOR with the sprite pixel
  326. *screenPixel ^= 0xFFFFFFFF;
  327. }
  328. }
  329. }
  330. }
  331. void Chip8::OP_Ex9E()
  332. {
  333. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  334. uint8_t key = registers[Vx];
  335. if (keypad[key])
  336. {
  337. pc += 2;
  338. }
  339. }
  340. void Chip8::OP_ExA1()
  341. {
  342. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  343. uint8_t key = registers[Vx];
  344. if (!keypad[key])
  345. {
  346. pc += 2;
  347. }
  348. }
  349. void Chip8::OP_Fx07()
  350. {
  351. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  352. registers[Vx] = delayTimer;
  353. }
  354. void Chip8::OP_Fx0A()
  355. {
  356. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  357. if (keypad[0])
  358. {
  359. registers[Vx] = 0;
  360. }
  361. else if (keypad[1])
  362. {
  363. registers[Vx] = 1;
  364. }
  365. else if (keypad[2])
  366. {
  367. registers[Vx] = 2;
  368. }
  369. else if (keypad[3])
  370. {
  371. registers[Vx] = 3;
  372. }
  373. else if (keypad[4])
  374. {
  375. registers[Vx] = 4;
  376. }
  377. else if (keypad[5])
  378. {
  379. registers[Vx] = 5;
  380. }
  381. else if (keypad[6])
  382. {
  383. registers[Vx] = 6;
  384. }
  385. else if (keypad[7])
  386. {
  387. registers[Vx] = 7;
  388. }
  389. else if (keypad[8])
  390. {
  391. registers[Vx] = 8;
  392. }
  393. else if (keypad[9])
  394. {
  395. registers[Vx] = 9;
  396. }
  397. else if (keypad[10])
  398. {
  399. registers[Vx] = 10;
  400. }
  401. else if (keypad[11])
  402. {
  403. registers[Vx] = 11;
  404. }
  405. else if (keypad[12])
  406. {
  407. registers[Vx] = 12;
  408. }
  409. else if (keypad[13])
  410. {
  411. registers[Vx] = 13;
  412. }
  413. else if (keypad[14])
  414. {
  415. registers[Vx] = 14;
  416. }
  417. else if (keypad[15])
  418. {
  419. registers[Vx] = 15;
  420. }
  421. else
  422. {
  423. pc -= 2;
  424. }
  425. }
  426. void Chip8::OP_Fx15()
  427. {
  428. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  429. delayTimer = registers[Vx];
  430. }
  431. void Chip8::OP_Fx18()
  432. {
  433. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  434. soundTimer = registers[Vx];
  435. }
  436. void Chip8::OP_Fx1E()
  437. {
  438. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  439. index += registers[Vx];
  440. }
  441. void Chip8::OP_Fx29()
  442. {
  443. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  444. uint8_t digit = registers[Vx];
  445. index = FONTSET_START_ADDRESS + (5 * digit);
  446. }
  447. void Chip8::OP_Fx33()
  448. {
  449. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  450. uint8_t value = registers[Vx];
  451. // Ones-place
  452. memory[index + 2] = value % 10;
  453. value /= 10;
  454. // Tens-place
  455. memory[index + 1] = value % 10;
  456. value /= 10;
  457. // Hundreds-place
  458. memory[index] = value % 10;
  459. }
  460. void Chip8::OP_Fx55()
  461. {
  462. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  463. for (uint8_t i = 0; i <= Vx; ++i)
  464. {
  465. memory[index + i] = registers[i];
  466. }
  467. }
  468. void Chip8::OP_Fx65()
  469. {
  470. uint8_t Vx = (opcode & 0x0F00u) >> 8u;
  471. for (uint8_t i = 0; i <= Vx; ++i)
  472. {
  473. registers[i] = memory[index + i];
  474. }
  475. }