chip8

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

commit 934636d6f440f339294b052a15024ce8bcb31e3b
parent b03a879d31f31a5f47590c693a01fbdd2f85367c
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue,  1 Sep 2020 21:31:51 +0300

minor changes

Diffstat:
Msrc/chip8.c | 23++++-------------------
Msrc/chip8.h | 2--
Msrc/main.c | 39++++++++++++++++-----------------------
3 files changed, 20 insertions(+), 44 deletions(-)

diff --git a/src/chip8.c b/src/chip8.c @@ -34,14 +34,12 @@ chip8_init(Chip8 *chip8) 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E 0xF0, 0x80, 0xF0, 0x80, 0x80 // F }; - pc = 0x200; opcode = 0; I = 0; sp = 0; delaytimer = 0; soundtimer = 0; - memset(V, 0, 16 * sizeof(uint8_t)); memset(keys, 0, 16 * sizeof(uint8_t)); memset(stack, 0, 16 * sizeof(uint16_t)); @@ -96,28 +94,22 @@ chip8_rom_load(Chip8 *chip8, const char *fpath) void chip8_emulate(Chip8 *chip8) { - chip8_fetch(chip8); + opcode = memory[pc] << 8 | memory[pc + 1]; // fetch if (chip8_decode(chip8)) { - chip8_execute(chip8); + pc += 2; // execute chip8_timers_update(chip8); } - printf("opcode: %x\tmemory: %x\tI: %x\tsp: %x\tpc: %d\n", + printf("Opcode: %x\tMemory: %x\tI: %x\tSP: %x\tPC: %d\n", opcode, memory[pc] << 8 | memory[pc + 1], I, sp, pc); } -void -chip8_fetch(Chip8 *chip8) -{ - opcode = memory[pc] << 8 | memory[pc + 1]; -} - int chip8_decode(Chip8 *chip8) { - int i; switch (opcode & 0xF000) { + int i; case 0x0000: // 00E_ switch (opcode & 0x00FF) { @@ -228,7 +220,6 @@ chip8_decode(Chip8 *chip8) } } } - drawflag = TRUE; } break; @@ -307,12 +298,6 @@ chip8_decode(Chip8 *chip8) } void -chip8_execute(Chip8 *chip8) -{ - pc += 2; -} - -void chip8_timers_update(Chip8 *chip8) { if (delaytimer > 0) --delaytimer; diff --git a/src/chip8.h b/src/chip8.h @@ -30,9 +30,7 @@ extern Chip8 chip8; extern void chip8_init(Chip8 *chip8); extern int chip8_rom_load(Chip8 *chip8, const char *fpath); extern void chip8_emulate(Chip8 *chip8); -extern void chip8_fetch(Chip8 *chip8); extern int chip8_decode(Chip8 *chip8); -extern void chip8_execute(Chip8 *chip8); extern void chip8_timers_update(Chip8 *chip8); #endif /* CHIP8_H */ diff --git a/src/main.c b/src/main.c @@ -17,14 +17,11 @@ evts(Chip8 *chip8) SDL_Event e; while (SDL_PollEvent(&e)) { - if (e.type == SDL_QUIT) return FALSE; + if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE) 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]) @@ -34,7 +31,7 @@ evts(Chip8 *chip8) } static void -render(SDL_Renderer *r, SDL_Texture *t, Chip8 *chip8) +render(SDL_Renderer *ren, SDL_Texture *tex, Chip8 *chip8) { int i; uint32_t pixels[2048]; @@ -44,16 +41,15 @@ render(SDL_Renderer *r, SDL_Texture *t, Chip8 *chip8) 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); + SDL_UpdateTexture(tex, NULL, pixels, 64 * sizeof(Uint32)); + SDL_RenderClear(ren); + SDL_RenderCopy(ren, tex, NULL, NULL); + SDL_RenderPresent(ren); } int main(int argc, char **argv) { - SDL_Window *win = NULL; int w = 1024, h = 512; srand(time(NULL)); @@ -67,33 +63,30 @@ main(int argc, char **argv) fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); return EXIT_FAILURE; } - - win = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, + SDL_Window *win = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN); - if (win == NULL) + SDL_Renderer *ren = SDL_CreateRenderer(win, -1, 0); + SDL_RenderSetLogicalSize(ren, w, h); + SDL_Texture *tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, 64, 32); + if (!win || !ren || !tex) { - fprintf(stderr, "Cannot create SDL window. Exiting. . .\n%s\n", SDL_GetError()); + fprintf(stderr, "SDL error. Exiting. . .\n%s\n", SDL_GetError()); return EXIT_FAILURE; } - SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, 0); - SDL_RenderSetLogicalSize(renderer, w, h); - SDL_Texture *texture = SDL_CreateTexture( - renderer, SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, 64, 32); - Chip8 chip8; chip8_init(&chip8); if (!chip8_rom_load(&chip8, argv[1])) return EXIT_FAILURE; for (; evts(&chip8); usleep(1500)) { chip8_emulate(&chip8); - if (chip8.drawflag) render(renderer, texture, &chip8); + if (chip8.drawflag) render(ren, tex, &chip8); } - SDL_DestroyTexture(texture); - SDL_DestroyRenderer(renderer); + SDL_DestroyTexture(tex); + SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return EXIT_SUCCESS;