diff options
| -rw-r--r-- | chip8.c | 17 | ||||
| -rw-r--r-- | chip8.h | 6 | ||||
| -rw-r--r-- | ui_sdl.c | 28 |
3 files changed, 28 insertions, 23 deletions
@@ -39,7 +39,7 @@ static u8 font[] = { 0xF0, 0x80, 0xF0, 0x80, 0x80 // F }; -void init_vm() { +void init_vm(void) { vm.PC = 0x200; // start of chip8 programs // write fonts into memory @@ -48,9 +48,9 @@ void init_vm() { } } -void load_rom(char *file_path) { +void load_rom(char *path) { memset(&vm, 0, sizeof(chip8_vm)); - int fd = open(file_path, O_RDONLY); + int fd = open(path, O_RDONLY); FILE* f = fdopen(fd, "r"); fseek(f, 0, SEEK_END); off_t fsize = ftell(f); @@ -73,7 +73,7 @@ void load_rom(char *file_path) { fclose(f); close(fd); } -void vm_step() { +void vm_step(void) { // read 2 bytes and convert to big-endian u16 in = htons(vm.ram[vm.PC + 0x1]<<8 | vm.ram[vm.PC]); @@ -198,7 +198,7 @@ void vm_step() { for (u32 i = 0; i < (in&0xF); i++) { for (u32 s = 0; s < 8; s++) { - // 0x80 = 10000000 + // 0x80 = b10000000 u8 pix = (vm.ram[vm.I + i] & (0x80 >> s)) != 0; if (pix) { @@ -207,12 +207,13 @@ void vm_step() { u32 byte = y * 64 + x; - // put the single 1 bit in the position + // put the single '1' bit in the position // that is relative to the pixel - u32 bit = 1 << (byte & 0x07); + u8 bit = 1 << (byte & 0x07); // byte>>3 ~ byte / 8 - // used to get the index of byte to xor + // used to get the index of the byte + // containing the bit to change if (vm.screen[byte>>3] & bit) { vm.Vx[0xF] = 1; } @@ -2,9 +2,9 @@ #include "util.h" -void init_vm(); -void vm_step(); -void load_rom(); +void init_vm(void); +void vm_step(void); +void load_rom(char *path); typedef struct { u8* ram; @@ -1,6 +1,7 @@ #define SDL_MAIN_HANDLED #include <time.h> +#include <stdbool.h> #ifdef _WIN32 #include <SDL.h> @@ -15,19 +16,18 @@ SDL_Renderer *r; -void setup() { +void setup(void) { SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); SDL_Window* win = SDL_CreateWindow("chip8", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN); r = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED); SDL_RenderClear(r); } -void draw() { - SDL_Rect box = (SDL_Rect){1, 0, 10, 10}; +void draw(void) { + SDL_Rect box = (SDL_Rect){0, 0, 10, 10}; SDL_SetRenderDrawColor(r, 12, 5, 9, 255); SDL_RenderClear(r); SDL_SetRenderDrawColor(r, 228, 167, 104, 255); - //SDL_RenderFillRect(r, &box); for (int x = 0; x <= 0x3F; x++) { for (int y = 0; y <= 0x1F; y++) { int byte = y * 64 + x; @@ -44,7 +44,7 @@ void draw() { int main(int argc, char* argv[]) { if (argc != 2) { - printf("%s [rom file]\n", argv[0]); + printf("./%s <rom file>\n", argv[0]); return 1; } @@ -55,19 +55,20 @@ int main(int argc, char* argv[]) { setup(); int last_500hz = SDL_GetTicks(); - int last_60hz = SDL_GetTicks(); - int last_45hz = SDL_GetTicks(); + int last_60hz = last_500hz; + int last_45hz = last_500hz; + + bool quit = false; SDL_Event e; - while (1) { + while (!quit) { SDL_PollEvent(&e); switch (e.type) { + case SDL_QUIT: + quit = true; break; case SDL_KEYDOWN: switch (e.key.keysym.sym) { - case SDLK_ESCAPE: - SDL_Quit(); - exit(0); - break; + case SDLK_ESCAPE: quit = true; break; case '1': vm.keyboard[0x0] = 1; break; case '2': vm.keyboard[0x1] = 1; break; case '3': vm.keyboard[0x2] = 1; break; @@ -132,6 +133,9 @@ int main(int argc, char* argv[]) { last_45hz = cur; } } + + SDL_Quit(); + return 0; } |