chip8

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

commit 787cfac056e7ac9967196cb7943b857da6aa1a78
parent 6646982373405f9be03c675d56c0de3c39e24dcc
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon,  1 Jun 2020 00:00:33 +0300

main cleanup

Diffstat:
Mbin/chip8 | 0
Mobj/main.o | 0
Msrc/chip8.c | 22+++++++++-------------
Msrc/chip8.h | 5+++--
Msrc/main.c | 82++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
5 files changed, 58 insertions(+), 51 deletions(-)

diff --git a/bin/chip8 b/bin/chip8 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 @@ -16,8 +16,7 @@ void chip8_init(Chip8 *chip8) { - uint8_t fontset[80] = - { + uint8_t fontset[80] = { 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 0x20, 0x60, 0x20, 0x20, 0x70, // 1 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2 @@ -104,8 +103,7 @@ emulate(Chip8 *chip8) execute(chip8); update_timers(chip8); } - - printf("opcode: %x\tmemory: %x\tI: %x\tsp: %x\tpc: %d\n", + LOG("opcode: %x\tmemory: %x\tI: %x\tsp: %x\tpc: %d", opcode, memory[pc] << 8 | memory[pc + 1], I, sp, pc); } @@ -126,7 +124,7 @@ decode(Chip8 *chip8) { case 0xE0: // 00E0 - Clear screen memset(gfx, 0, 2048 * sizeof(uint8_t)); - drawflag = 1; + drawflag = TRUE; break; case 0xEE: // 00EE - Return from subroutine pc = stack[--sp]; @@ -232,17 +230,17 @@ decode(Chip8 *chip8) } } - drawflag = 1; + drawflag = TRUE; } break; case 0xE000: // EX__ switch (opcode & 0x00FF) { case 0x009E: // EX9E - Skip next instruction if key in VX is pressed - if (keys[V[(opcode & 0x0F00) >> 8]] != 0) pc += 2; + if (keys[V[(opcode & 0x0F00) >> 8]]) pc += 2; break; case 0x00A1: // EXA1 - Skip next instruction if key in VX isn't pressed - if (keys[V[(opcode & 0x0F00) >> 8]] == 0) pc += 2; + if (!keys[V[(opcode & 0x0F00) >> 8]]) pc += 2; break; default: ERROR("Unknown opcode: %x", opcode); @@ -257,16 +255,15 @@ decode(Chip8 *chip8) break; case 0x000A: // FX0A - Wait for key press and then store it in VX { - int keypressed = 0; + int keypressed = FALSE; for (i = 0; i < 16; i++) { - if (keys[i] != 0) + if (keys[i]) { V[(opcode & 0x0F00) >> 8] = i; - keypressed = 1; + keypressed = TRUE; } } - if (!keypressed) return FALSE; } break; @@ -307,7 +304,6 @@ decode(Chip8 *chip8) ERROR("%s", "Unimplemented opcode"); return FALSE; } - return TRUE; } diff --git a/src/chip8.h b/src/chip8.h @@ -1,15 +1,16 @@ #ifndef CHIP8_H #define CHIP8_H -#include <stdint.h> +#include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> -#define TRUE 1 +#define TRUE 1 #define FALSE 0 +#define LOG(format, ...) (printf(format"\n", __VA_ARGS__)) #define ERROR(format, ...) (fprintf(stderr, format"\n", __VA_ARGS__)) typedef struct { diff --git a/src/main.c b/src/main.c @@ -14,12 +14,55 @@ static const uint8_t keymap[16] = { SDLK_c, SDLK_v, }; +static int handle_events(Chip8 *chip8); +static void render(SDL_Renderer *r, SDL_Texture *t, Chip8 *chip8); + +int +handle_events(Chip8 *chip8) +{ + int i; + SDL_Event e; + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) return FALSE; + if (e.type == SDL_KEYDOWN) + { + if (e.key.keysym.sym == SDLK_ESCAPE) return FALSE; + for (i = 0; i < 16; i++) + if (e.key.keysym.sym == keymap[i]) + chip8->keys[i] = TRUE; + } + + if (e.type == SDL_KEYUP) + for (i = 0; i < 16; i++) + if (e.key.keysym.sym == keymap[i]) + chip8->keys[i] = FALSE; + } + return TRUE; +} + +void +render(SDL_Renderer *r, SDL_Texture *t, Chip8 *chip8) +{ + int i; + uint32_t pixels[2048]; + chip8->drawflag = FALSE; + for (i = 0; i < 2048; i++) + { + uint8_t pixel = chip8->gfx[i]; + pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000; + } + SDL_UpdateTexture(t, NULL, pixels, 64 * sizeof(Uint32)); + SDL_RenderClear(r); + SDL_RenderCopy(r, t, NULL, NULL); + SDL_RenderPresent(r); +} + int main(int argc, char **argv) { SDL_Window *win = NULL; int w = 1024, h = 512; - uint32_t pixels[2048]; srand(time(NULL)); if (argc != 2) @@ -27,7 +70,6 @@ main(int argc, char **argv) ERROR("%s", "Usage: ./chip8 [ROM]"); return EXIT_FAILURE; } - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { ERROR("%s", "Cannot initialize SDL. Exiting. . ."); @@ -52,43 +94,11 @@ main(int argc, char **argv) Chip8 chip8; chip8_init(&chip8); if (!load(&chip8, argv[1])) return EXIT_FAILURE; - for (;;) { - SDL_Event e; emulate(&chip8); - - int i; - while (SDL_PollEvent(&e)) - { - if (e.type == SDL_QUIT) return EXIT_SUCCESS; - if (e.type == SDL_KEYDOWN) - { - if (e.key.keysym.sym == SDLK_ESCAPE) return EXIT_SUCCESS; - for (i = 0; i < 16; i++) - if (e.key.keysym.sym == keymap[i]) - chip8.keys[i] = 1; - } - - if (e.type == SDL_KEYUP) - for (i = 0; i < 16; i++) - if (e.key.keysym.sym == keymap[i]) - chip8.keys[i] = 0; - } - - if (chip8.drawflag) - { - chip8.drawflag = 0; - for (i = 0; i < 2048; i++) - { - uint8_t pixel = chip8.gfx[i]; - pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000; - } - SDL_UpdateTexture(texture, NULL, pixels, 64 * sizeof(Uint32)); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, texture, NULL, NULL); - SDL_RenderPresent(renderer); - } + if (!handle_events(&chip8)) return EXIT_SUCCESS; + if (chip8.drawflag) render(renderer, texture, &chip8); usleep(1500); }