No Description

main.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <SDL2/SDL.h>
  7. #define DEBUG 0
  8. #define TDISP 0
  9. typedef uint8_t byte;
  10. typedef uint16_t word;
  11. typedef struct {
  12. byte memory[0x1000];
  13. byte screen[64][32];
  14. byte registers[0x10];
  15. word stack[24];
  16. byte input[0x10];
  17. word address_I;
  18. word program_counter;
  19. byte stack_pointer;
  20. } emulator_state_s;
  21. byte chip8_fontset[80] =
  22. {
  23. 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
  24. 0x20, 0x60, 0x20, 0x20, 0x70, // 1
  25. 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
  26. 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
  27. 0x90, 0x90, 0xF0, 0x10, 0x10, // 4
  28. 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
  29. 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
  30. 0xF0, 0x10, 0x20, 0x40, 0x40, // 7
  31. 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
  32. 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
  33. 0xF0, 0x90, 0xF0, 0x90, 0x90, // A
  34. 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
  35. 0xF0, 0x80, 0x80, 0x80, 0xF0, // C
  36. 0xE0, 0x90, 0x90, 0x90, 0xE0, // D
  37. 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
  38. 0xF0, 0x80, 0xF0, 0x80, 0x80 // F
  39. };
  40. byte keymap[0x10] = { SDL_SCANCODE_X, SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_3, SDL_SCANCODE_Q,
  41. SDL_SCANCODE_W, SDL_SCANCODE_E, SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_D,
  42. SDL_SCANCODE_Z, SDL_SCANCODE_C, SDL_SCANCODE_4, SDL_SCANCODE_R, SDL_SCANCODE_F, SDL_SCANCODE_V };
  43. int main(int argc, char **argv) {
  44. SDL_Window *window = NULL;
  45. SDL_Renderer *renderer = NULL;
  46. SDL_Init(SDL_INIT_VIDEO);
  47. window = SDL_CreateWindow("Chip8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 64 * 8, 32 * 8, SDL_WINDOW_SHOWN);
  48. renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
  49. if (renderer == NULL) printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
  50. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  51. srand(time(NULL));
  52. emulator_state_s state = { 0 }; // initialize everything to 0
  53. state.program_counter = 0x200; // chip8 programs start at 0x200 address space
  54. memcpy(state.memory, chip8_fontset, 80);
  55. FILE *f = fopen(argv[1], "rb");
  56. fread(&state.memory[0x200], 0x1000 - 0x200, 1, f); // see above comment
  57. fclose(f);
  58. SDL_Event e;
  59. int delay = 0;
  60. int sound = 0;
  61. int frame = 0;
  62. int press = 0;
  63. int wait = 0;
  64. int store = 0;
  65. while (1) {
  66. if (DEBUG) printf("0x%x ", state.program_counter);
  67. if (frame == 10)
  68. {
  69. frame = 0;
  70. if (delay > 0) --delay;
  71. if (sound > 0) --sound;
  72. memset(state.input, 0, 0x10);
  73. press = 0;
  74. }
  75. // opcodes are 16 bits
  76. SDL_PollEvent(&e);
  77. if (e.type == SDL_QUIT) break;
  78. // SDL_PumpEvents();
  79. const byte *keystate = SDL_GetKeyboardState(NULL);
  80. for (int i = 0; i < 0x10; ++i) {
  81. if (keystate[keymap[i]]){
  82. state.input[i] = 1;
  83. press = 1;
  84. }
  85. }
  86. if (wait && !press) continue;
  87. if (wait && press)
  88. {
  89. for (int i = 0; i < 0x10; ++i) {
  90. if (state.input[i] != 0)
  91. {
  92. state.registers[store] = i;
  93. break;
  94. }
  95. }
  96. wait = 0;
  97. }
  98. word opcode = (state.memory[state.program_counter++] << 8) | (state.memory[state.program_counter++]);
  99. switch (opcode & 0xF000)
  100. {
  101. case 0x0000: // screen clear or function return
  102. if (opcode == 0x00E0){
  103. memset(state.screen, 0, 64 * 32);
  104. if (DEBUG) printf("Clearing screen ");
  105. }
  106. else if (opcode == 0x00EE) {
  107. state.program_counter = state.stack[--state.stack_pointer];
  108. if (DEBUG) printf("Returning to %x ", state.stack[state.stack_pointer]);
  109. }
  110. break;
  111. case 0x1000: // goto statement
  112. state.program_counter = opcode & 0x0FFF;
  113. if (DEBUG) printf("Jumping to %x ", state.program_counter);
  114. break;
  115. case 0x2000: // CALL
  116. state.stack[state.stack_pointer++] = state.program_counter;
  117. state.program_counter = opcode & 0x0FFF;
  118. if (DEBUG) printf("Calling subroutine at %x (returning to %x)", state.program_counter, state.stack[state.stack_pointer - 1]);
  119. break;
  120. case 0x3000:; // skip if reg 0x0F00 is equal to constant 0x00FF
  121. int reg = (opcode & 0x0F00) >> 8;
  122. if (DEBUG) printf("Comparing (==) register %i (%x) to %x ", reg, state.registers[reg], (byte) opcode);
  123. if (state.registers[reg] == (byte) opcode){
  124. state.program_counter += 2;
  125. if (DEBUG) printf("and incrementing program_counter ");
  126. }
  127. break;
  128. case 0x4000: // skip if not equal
  129. reg = (opcode & 0x0F00) >> 8;
  130. if (DEBUG) printf("Comparing (!=) register %i (%x) to %x ", reg, state.registers[reg], (byte) opcode);
  131. if (state.registers[reg] != (byte) opcode){
  132. state.program_counter += 2;
  133. if (DEBUG) printf("and incrementing program_counter ");
  134. }
  135. break;
  136. case 0x5000:; // skip if reg 0x0F00 is equal to 0x00F0
  137. int first = (opcode & 0x0F00) >> 8;
  138. int second = (opcode & 0x00F0) >> 4;
  139. if (DEBUG) printf("Comparing (==) register %i (%x) to %i (%x) ", first, state.registers[first], second, state.registers[second]);
  140. if (state.registers[first] == state.registers[second]){
  141. state.program_counter += 2;
  142. if (DEBUG) printf("and incrementing program_counter ");
  143. }
  144. break;
  145. case 0x6000: // set reg to constant
  146. reg = (opcode & 0x0F00) >> 8;
  147. state.registers[reg] = (byte) opcode;
  148. if (DEBUG) printf("Assigning %hx to %i", state.registers[reg], reg);
  149. break;
  150. case 0x7000: // add constant to reg
  151. reg = (opcode & 0x0F00) >> 8;
  152. state.registers[reg] += (byte) opcode;
  153. if (DEBUG) printf("Add %hi to reg %i (%hi)", opcode & 0x00FF, reg, state.registers[reg]);
  154. break;
  155. case 0x8000:; // math/bitop operators
  156. first = (opcode & 0x0F00) >> 8; // all use XY regs
  157. second = (opcode & 0x00F0) >> 4;
  158. switch (opcode & 0x000F)
  159. {
  160. case 0: // copy 1 register to another
  161. state.registers[first] = state.registers[second];
  162. if (DEBUG) printf("Copied register %i to %i (%hx)", first, second, state.registers[first]);
  163. break;
  164. case 1: // X = X | Y
  165. state.registers[first] |= state.registers[second];
  166. if (DEBUG) printf("OR register %i and %i (%hx)", first, second, state.registers[first]);
  167. break;
  168. case 2: // X = X & Y
  169. state.registers[first] &= state.registers[second];
  170. if (DEBUG) printf("AND register %i and %i (%hx)", first, second, state.registers[first]);
  171. break;
  172. case 3: // X = X ^ Y
  173. state.registers[first] ^= state.registers[second];
  174. if (DEBUG) printf("XOR register %i to %i (%hx)", first, second, state.registers[first]);
  175. break;
  176. case 4:; // X = X + Y, VF = X + 5 > 255
  177. unsigned int add = state.registers[first] + state.registers[second];
  178. if (add > 255) state.registers[0xF] = 1;
  179. else state.registers[0xF] = 0;
  180. state.registers[first] = (byte) add;
  181. if (DEBUG) printf("ADD register %i to %i (%hx) ", second, first, state.registers[first]);
  182. if (DEBUG) printf("VF is %hx", state.registers[0xF]);
  183. break;
  184. case 5: // X = X - Y, VF = X - Y < 0
  185. state.registers[first] -= state.registers[second];
  186. if (state.registers[first] > state.registers[second]) state.registers[0xF] = 0;
  187. else state.registers[0xF] = 1;
  188. if (DEBUG) printf("SUB register %i from %i (%hx) ", second, first, state.registers[first]);
  189. if (DEBUG) printf("VF is %hx", state.registers[0xF]);
  190. break;
  191. case 6: // set least significant bit of X in VF, bitshift right
  192. state.registers[0xF] = state.registers[first] & 0b00000001;
  193. state.registers[first] /= 2;
  194. if (DEBUG) printf("RS register %i (%hx) ", first, state.registers[first]);
  195. if (DEBUG) printf("VF is %hx", state.registers[0xF]);
  196. break;
  197. case 7: // X = Y - X, VF = Y - X < 0
  198. state.registers[first] = state.registers[second] - state.registers[first];
  199. if (state.registers[second] > state.registers[first]) state.registers[0xF] = 1;
  200. else state.registers[0xF] = 0;
  201. if (DEBUG) printf("SUB %i from %i (%hx) ", first, second, state.registers[first]);
  202. if (DEBUG) printf("VF is %hx", state.registers[0xF]);
  203. break;
  204. case 0xE:; // set msot significant bit of X in VF, bitshift left
  205. state.registers[0xF] = (state.registers[first] & 0b10000000) >> 7;
  206. state.registers[first] *= 2;
  207. if (DEBUG) printf("LS register %i (%hx) ", first, state.registers[first]);
  208. if (DEBUG) printf("VF is %hx", state.registers[0xF]);
  209. break;
  210. default: break;
  211. } break;
  212. case 0x9000: // skip if two regs are not equal
  213. first = (opcode & 0x0F00) >> 8;
  214. second = (opcode & 0x00F0) >> 4;
  215. if (DEBUG) printf("Comparing (!=) register %i (%hx) to %i (%hx) ", first, state.registers[first], second, state.registers[second]);
  216. if (state.registers[first] != state.registers[second]) {
  217. state.program_counter += 2;
  218. if (DEBUG) printf("and incrementing program_counter");
  219. }
  220. break;
  221. case 0xA000: // assign I address
  222. state.address_I = opcode & 0x0FFF;
  223. if (DEBUG) printf("Assigning %x to I address ", opcode & 0x0FFF);
  224. break;
  225. case 0xB000: // jump to 0x0FFF + V0
  226. state.program_counter = (opcode & 0x0FFF) + state.registers[0x0];
  227. if (DEBUG) printf("Jumping to %x + %hx (%x)", opcode & 0x0FFF, state.registers[0], (opcode & 0x0FFF) + state.registers[0x0]);
  228. break;
  229. case 0xC000: // random generator
  230. reg = (opcode & 0x0F00) >> 8;
  231. state.registers[reg] = (rand() % 255) & ((byte) opcode);
  232. if (DEBUG) printf("Storing random in %i (%x)\n", reg, state.registers[reg]);
  233. break;
  234. case 0xD000: // draw sprite
  235. first = (opcode & 0x0F00) >> 8;
  236. second = (opcode & 0x00F0) >> 4;
  237. int x = state.registers[first];
  238. int y = state.registers[second];
  239. int h = (opcode & 0x000F);
  240. for (int line = 0; line < h; ++line)
  241. {
  242. byte data = state.memory[state.address_I + line];
  243. for (int xpos = 0; xpos < 8; ++xpos)
  244. {
  245. int ypos = y + line;
  246. byte mask = 1 << 7 - xpos;
  247. if (ypos > 31) ypos -= 32;
  248. if (!(data & mask)) continue;
  249. if (state.screen[x + xpos][ypos]){
  250. state.registers[0xF] = 1;
  251. }
  252. state.screen[x + xpos][ypos] ^= 1;
  253. }
  254. }
  255. if (DEBUG) printf("Drawing");
  256. if (!TDISP) break;
  257. printf("\n");
  258. for (int y = 0; y < 32; ++y) {
  259. for (int x = 0; x < 64; ++x) {
  260. printf(state.screen[x][y] ? "O" : " ");
  261. }
  262. printf("\n");
  263. }
  264. break;
  265. case 0xE000: // input handling
  266. reg = (opcode & 0x0F00) >> 8;
  267. switch ((byte) opcode)
  268. {
  269. case 0x9E: // skip if button pressed
  270. if (DEBUG) printf("Testing if %i is pressed", state.registers[reg]);
  271. if (state.input[state.registers[reg]]) state.program_counter += 2;
  272. break;
  273. case 0xA1: // skip if button *not* pressed
  274. if (DEBUG) printf("Testing if %i is not pressed", state.registers[reg]);
  275. if (!state.input[state.registers[reg]]) state.program_counter += 2;
  276. break;
  277. }
  278. break;
  279. case 0xF000: // misc (timer, input, sound, memory)
  280. reg = (opcode & 0x0F00) >> 8;
  281. switch ((byte) opcode)
  282. {
  283. case 0x07:
  284. if (DEBUG) printf("Copying delay timer to reg %i (%hx)", reg, state.registers[reg]);
  285. state.registers[reg] = delay;
  286. break;
  287. case 0x0A:
  288. wait = 1;
  289. store = reg;
  290. break;
  291. case 0x15:
  292. delay = state.registers[reg];
  293. break;
  294. case 0x18:
  295. sound = state.registers[reg];
  296. break;
  297. case 0x1E:
  298. state.address_I += state.registers[reg];
  299. if (DEBUG) printf("Adding reg %i (%hx) to I (%x)", reg, state.registers[reg], state.address_I);
  300. break;
  301. case 0x29:
  302. state.address_I = state.registers[reg] * 5;
  303. break;
  304. case 0x33:;
  305. int value = state.registers[reg];
  306. state.memory[state.address_I] = value / 100;
  307. state.memory[state.address_I + 1] = (value / 10) % 10;
  308. state.memory[state.address_I + 2] = value % 10;
  309. break;
  310. case 0x55:
  311. for (int i = 0; i <= reg; ++i)
  312. state.memory[state.address_I + i] = state.registers[i];
  313. break;
  314. case 0x65:
  315. for (int i = 0; i <= reg + 1; ++i)
  316. state.registers[i] = state.memory[state.address_I + i];
  317. break;
  318. default:
  319. break;
  320. }
  321. break;
  322. default:
  323. if (DEBUG) printf("%x", opcode);
  324. break;
  325. }
  326. if (DEBUG) printf("\n");
  327. for (int x = 0; x < 64; ++x) {
  328. for (int y = 0; y < 32; ++y)
  329. {
  330. SDL_Rect pixel = { .x = (x * 8), .y = (y * 8), .w = 8, .h = 8};
  331. if (state.screen[x][y])
  332. {
  333. SDL_SetRenderDrawColor(renderer, 0xff, 0x69, 0xb4, 0xFF);
  334. SDL_RenderFillRect(renderer, &pixel);
  335. }
  336. else
  337. {
  338. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
  339. SDL_RenderFillRect(renderer, &pixel);
  340. }
  341. }
  342. }
  343. SDL_RenderPresent(renderer);
  344. SDL_Delay(1);
  345. ++frame;
  346. // SDL_DestroyWindow(window);
  347. // SDL_Quit();
  348. }
  349. }