chip8

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

commit f7fb7be8726576bcc5c1687d9d3e41ca25021f8c
parent 8b89546910d16965dac0807842306e77c9287762
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed,  2 Sep 2020 18:09:39 +0300

changed brace convention

Diffstat:
Msrc/chip8.c | 49+++++++++++++++++--------------------------------
Msrc/chip8.h | 6+++---
Msrc/main.c | 15+++++----------
3 files changed, 25 insertions(+), 45 deletions(-)

diff --git a/src/chip8.c b/src/chip8.c @@ -13,8 +13,8 @@ #define soundtimer chip8->soundtimer #define drawflag chip8->drawflag -static int chip8_decode(struct Chip8 *chip8); -static void chip8_timers_update(struct Chip8 *chip8); +static int chip8_decode(struct Chip8 *); +static void chip8_timers_update(struct Chip8 *); void chip8_init(struct Chip8 *chip8) @@ -56,8 +56,7 @@ int chip8_rom_load(struct Chip8 *chip8, const char *fpath) { FILE *rom = fopen(fpath, "rb"); - if (rom == NULL) - { + if (rom == NULL) { fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath); return FALSE; } @@ -66,15 +65,13 @@ chip8_rom_load(struct Chip8 *chip8, const char *fpath) rewind(rom); char *buf = (char *)malloc(romsize * sizeof(char)); - if (buf == NULL) - { + if (buf == NULL) { fprintf(stderr, "Cannot allocate memory. Exiting. . .\n"); return FALSE; } size_t res = fread(buf, sizeof(char), (size_t)romsize, rom); - if (res != romsize) - { + if (res != romsize) { fprintf(stderr, "Error reading ROM. Exiting. . .\n"); return FALSE; } @@ -83,8 +80,7 @@ chip8_rom_load(struct Chip8 *chip8, const char *fpath) if ((4096 - 512) > romsize) for (i = 0; i < romsize; i++) memory[i + 512] = (uint8_t)buf[i]; - else - { + else { fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n"); return FALSE; } @@ -98,8 +94,7 @@ void chip8_emulate(struct Chip8 *chip8) { opcode = memory[pc] << 8 | memory[pc + 1]; // fetch - if (chip8_decode(chip8)) - { + if (chip8_decode(chip8)) { pc += 2; // execute chip8_timers_update(chip8); } @@ -110,12 +105,10 @@ chip8_emulate(struct Chip8 *chip8) int chip8_decode(struct Chip8 *chip8) { - switch (opcode & 0xF000) - { + switch (opcode & 0xF000) { int i; case 0x0000: // 00E_ - switch (opcode & 0x00FF) - { + switch (opcode & 0x00FF) { case 0xE0: // 00E0 - Clear screen memset(gfx, 0, 2048 * sizeof(uint8_t)); drawflag = TRUE; @@ -150,8 +143,7 @@ chip8_decode(struct Chip8 *chip8) V[(opcode & 0x0F00) >> 8] += opcode & 0x00FF; break; case 0x8000: // 8XY_ - switch (opcode & 0x000F) - { + switch (opcode & 0x000F) { case 0x0000: // 8XY0 - Set VX to VY V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4]; break; @@ -210,13 +202,10 @@ chip8_decode(struct Chip8 *chip8) V[0xF] = 0; int yl, xl; - for (yl = 0; yl < h; yl++) - { + for (yl = 0; yl < h; yl++) { pixel = memory[I + yl]; - for (xl = 0; xl < 8; xl++) - { - if ((pixel & (0x80 >> xl)) != 0) - { + for (xl = 0; xl < 8; xl++) { + if ((pixel & (0x80 >> xl)) != 0) { if (gfx[VX + xl + ((VY + yl) * 64)] == 1) V[0xF] = 1; gfx[VX + xl + ((VY + yl) * 64)] ^= 1; @@ -227,8 +216,7 @@ chip8_decode(struct Chip8 *chip8) } break; case 0xE000: // EX__ - switch (opcode & 0x00FF) - { + switch (opcode & 0x00FF) { case 0x009E: // EX9E - Skip next instruction if key in VX is pressed if (keys[V[(opcode & 0x0F00) >> 8]]) pc += 2; break; @@ -241,18 +229,15 @@ chip8_decode(struct Chip8 *chip8) } break; case 0xF000: // FX__ - switch (opcode & 0x00FF) - { + switch (opcode & 0x00FF) { case 0x0007: // FX07 - Set VX to delaytimer V[(opcode & 0x0F00) >> 8] = delaytimer; break; case 0x000A: // FX0A - Wait for key press and then store it in VX { int keypressed = FALSE; - for (i = 0; i < 16; i++) - { - if (keys[i]) - { + for (i = 0; i < 16; i++) { + if (keys[i]) { V[(opcode & 0x0F00) >> 8] = i; keypressed = TRUE; } diff --git a/src/chip8.h b/src/chip8.h @@ -27,8 +27,8 @@ struct Chip8 { extern struct Chip8 chip8; -extern void chip8_init(struct Chip8 *chip8); -extern int chip8_rom_load(struct Chip8 *chip8, const char *fpath); -extern void chip8_emulate(struct Chip8 *chip8); +extern void chip8_init(struct Chip8 *); +extern int chip8_rom_load(struct Chip8 *, const char *); +extern void chip8_emulate(struct Chip8 *); #endif /* CHIP8_H */ diff --git a/src/main.c b/src/main.c @@ -36,8 +36,7 @@ render(SDL_Renderer *ren, SDL_Texture *tex, struct Chip8 *chip8) int i; uint32_t pixels[2048]; chip8->drawflag = FALSE; - for (i = 0; i < 2048; i++) - { + for (i = 0; i < 2048; i++) { uint8_t pixel = chip8->gfx[i]; pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000; } @@ -53,13 +52,11 @@ main(int argc, char **argv) int w = 1024, h = 512; srand(time(NULL)); - if (argc != 2) - { + if (argc != 2) { fprintf(stderr, "Usage: ./chip8 [ROM]\n"); return EXIT_FAILURE; } - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) - { + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); return EXIT_FAILURE; } @@ -70,8 +67,7 @@ main(int argc, char **argv) SDL_RenderSetLogicalSize(ren, w, h); SDL_Texture *tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 64, 32); - if (!win || !ren || !tex) - { + if (!win || !ren || !tex) { fprintf(stderr, "SDL error. Exiting. . .\n%s\n", SDL_GetError()); return EXIT_FAILURE; } @@ -79,8 +75,7 @@ main(int argc, char **argv) struct Chip8 chip8; chip8_init(&chip8); if (!chip8_rom_load(&chip8, argv[1])) return EXIT_FAILURE; - for (; evts(&chip8); usleep(1500)) - { + for (; evts(&chip8); usleep(1500)) { chip8_emulate(&chip8); if (chip8.drawflag) render(ren, tex, &chip8); }