No Description

main.c 12KB

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