chip8

CHIP-8 emulator
git clone git://git.christosmarg.xyz/chip8.git
Log | Files | Refs | README | LICENSE

commit 7a3b70aed4c1fbc5dd1869290be3b411de342a36
parent 02a0ffc815aa0b8b83a405329dbeedda1a331b3c
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sat, 23 May 2020 07:25:58 +0300

minor improvements

Diffstat:
Mbin/chip8 | 0
Mobj/chip8.o | 0
Mobj/main.o | 0
Msrc/chip8.c | 26+++++++++++++-------------
Msrc/chip8.h | 3+++
Msrc/main.c | 20++++++++++++--------
6 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/bin/chip8 b/bin/chip8 Binary files differ. diff --git a/obj/chip8.o b/obj/chip8.o Binary files differ. diff --git a/obj/main.o b/obj/main.o Binary files differ. diff --git a/src/chip8.c b/src/chip8.c @@ -60,7 +60,7 @@ load(Chip8 *chip8, const char *fpath) if (rom == NULL) { fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath); - return -1; + return FALSE; } fseek(rom, 0, SEEK_END); long romsize = ftell(rom); @@ -70,14 +70,14 @@ load(Chip8 *chip8, const char *fpath) if (buf == NULL) { fprintf(stderr, "Cannot allocate memory. Exiting. . .\n"); - return -1; + return FALSE; } size_t res = fread(buf, sizeof(char), (size_t)romsize, rom); if (res != romsize) { fprintf(stderr, "Error reading ROM. Exiting. . .\n"); - return -1; + return FALSE; } int i; @@ -87,19 +87,19 @@ load(Chip8 *chip8, const char *fpath) else { fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n"); - return -1; + return FALSE; } fclose(rom); free(buf); - return 1; + return TRUE; } void emulate(Chip8 *chip8) { fetch(chip8); - if (decode(chip8) > 0) + if (decode(chip8)) { execute(chip8); update_timers(chip8); @@ -133,7 +133,7 @@ decode(Chip8 *chip8) break; default: fprintf(stderr, "Unknown opcode: %x\n", opcode); - return -1; + return FALSE; } break; case 0x1000: // 1NNN - Jump to address NNN @@ -194,7 +194,7 @@ decode(Chip8 *chip8) break; default: fprintf(stderr, "Unknown opcode: %x\n", opcode); - return -1; + return FALSE; } break; case 0x9000: // 9XY0 - Skip next instruction if VX != VY @@ -246,7 +246,7 @@ decode(Chip8 *chip8) break; default: fprintf(stderr, "Unknown opcode: %x\n", opcode); - return -1; + return FALSE; } break; case 0xF000: // FX__ @@ -267,7 +267,7 @@ decode(Chip8 *chip8) } } - if (!keypressed) return -1; + if (!keypressed) return FALSE; } break; case 0x0015: // FX15 - Set the delaytimer to VX @@ -300,15 +300,15 @@ decode(Chip8 *chip8) break; default: fprintf(stderr, "Unknown opcode: %x\n", opcode); - return -1; + return FALSE; } break; default: fprintf(stderr, "Unimplemented opcode\n"); - return -1; + return FALSE; } - return 1; + return TRUE; } void diff --git a/src/chip8.h b/src/chip8.h @@ -7,6 +7,9 @@ #include <string.h> #include <time.h> +#define TRUE 1 +#define FALSE 0 + typedef struct { uint8_t memory[4096]; uint16_t stack[16]; diff --git a/src/main.c b/src/main.c @@ -25,13 +25,13 @@ main(int argc, char **argv) if (argc != 2) { fprintf(stderr, "Usage: ./chip8 [ROM]\n"); - return -1; + return EXIT_FAILURE; } if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); - return -1; + return EXIT_FAILURE; } win = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, @@ -41,7 +41,7 @@ main(int argc, char **argv) { fprintf(stderr, "Cannot create SDL window. Exiting. . .\n%s\n", SDL_GetError()); - return -1; + return EXIT_FAILURE; } SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, 0); @@ -52,7 +52,7 @@ main(int argc, char **argv) Chip8 chip8; chip8_init(&chip8); - if (load(&chip8, argv[1]) < 0) return -1; + if (!load(&chip8, argv[1])) return EXIT_FAILURE; for (;;) { @@ -62,10 +62,10 @@ main(int argc, char **argv) int i; while (SDL_PollEvent(&e)) { - if (e.type == SDL_QUIT) return -1; + if (e.type == SDL_QUIT) return EXIT_FAILURE; if (e.type == SDL_KEYDOWN) { - if (e.key.keysym.sym == SDLK_ESCAPE) return -1; + if (e.key.keysym.sym == SDLK_ESCAPE) return EXIT_FAILURE; for (i = 0; i < 16; i++) if (e.key.keysym.sym == keymap[i]) chip8.keys[i] = 1; @@ -90,8 +90,12 @@ main(int argc, char **argv) SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); } - usleep(1500); + usleep(2000); } - return 0; + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(win); + SDL_Quit(); + return EXIT_SUCCESS; }