From 8622df3bc001dea6acca1de3aa5d4cfb5bcecb16 Mon Sep 17 00:00:00 2001 From: Toni Date: Fri, 11 Jul 2025 18:12:58 +0200 Subject: [PATCH] move to raylib --- build.sh | 2 +- chip8.c | 42 +++++++++++++++++++++++------------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/build.sh b/build.sh index 429541b..405fcb8 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/bash set -xe -cc -O3 -o chip8 chip8.c -lSDL2 \ No newline at end of file +cc -O3 -o chip8 chip8.c -L/usr/local/lib/libraylib.a -lraylib -lm \ No newline at end of file diff --git a/chip8.c b/chip8.c index 61d6431..6dc6569 100644 --- a/chip8.c +++ b/chip8.c @@ -1,8 +1,11 @@ // http://devernay.free.fr/hacks/chip8/C8TECH10.HTM -#include +// TODO: keyboard +// TODO: buzzer +#include #include #include #include +#include #define READ_INS() \ uint8_t low = c->memory[c->pc++]; \ @@ -200,6 +203,13 @@ void chip8_disassemble(CHIP8 *c, size_t ins_count) { void chip8_step(CHIP8 *c) { do { + if (c->delay_timer > 0) { + c->delay_timer--; + } + if (c->sound_timer > 0) { + c->sound_timer--; + } + READ_INS(); switch ((ins >> 12) & 0xF) { @@ -213,7 +223,9 @@ void chip8_step(CHIP8 *c) { c->sp--; break; default: - c->pc = nnn; + // >This instruction is only used on the old computers on which Chip-8 + // >was originally implemented. It is ignored by modern interpreters. + break; } } break; case 0x1: { @@ -394,33 +406,25 @@ int main(int argc, char *argv[]) { c.memory[0x200 + i] = buffer[i]; } - SDL_Init(SDL_INIT_VIDEO); - SDL_Window *window = - SDL_CreateWindow("CHIP-8", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, 640, 320, SDL_WINDOW_SHOWN); - SDL_Surface *surface = SDL_GetWindowSurface(window); + InitWindow(640, 320, "CHIP-8"); + SetTargetFPS(60); - while (1) { - SDL_Event e; - while (SDL_PollEvent(&e)) { - if (e.type == SDL_QUIT) { - return 0; - } + while (!WindowShouldClose()) { + for (int i = 0; i < 25; i++) { + chip8_step(&c); } - chip8_step(&c); + BeginDrawing(); - SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)); + ClearBackground(WHITE); for (size_t y = 0; y < 32; y++) { for (size_t x = 0; x < 64; x++) { if (c.display[x + y * 64] == 1) { - SDL_Rect rect = {x * 10, y * 10, 10, 10}; - SDL_FillRect(surface, &rect, - SDL_MapRGB(surface->format, 0x00, 0x00, 0x00)); + DrawRectangle(x * 10, y * 10, 10, 10, BLACK); } } } - SDL_UpdateWindowSurface(window); + EndDrawing(); } } \ No newline at end of file