summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2023-05-16 20:59:52 -0400
committerAndrew Opalach <andrew@akon.city> 2023-05-16 20:59:52 -0400
commit229829f0c8352120bc108ca0190b5d8d982d47f3 (patch)
tree51c0b0a18c84f9034fac5a380f71ae3777b9aa70
parent1530351408e88d60bd9c5f73927181cfee4c1500 (diff)
downloadchip8-229829f0c8352120bc108ca0190b5d8d982d47f3.tar.gz
chip8-229829f0c8352120bc108ca0190b5d8d982d47f3.tar.bz2
chip8-229829f0c8352120bc108ca0190b5d8d982d47f3.zip
bring up-to-date
-rw-r--r--chip8.c17
-rw-r--r--chip8.h6
-rw-r--r--ui_sdl.c28
3 files changed, 28 insertions, 23 deletions
diff --git a/chip8.c b/chip8.c
index b79bc3a..9c3d7eb 100644
--- a/chip8.c
+++ b/chip8.c
@@ -39,7 +39,7 @@ static u8 font[] = {
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
-void init_vm() {
+void init_vm(void) {
vm.PC = 0x200; // start of chip8 programs
// write fonts into memory
@@ -48,9 +48,9 @@ void init_vm() {
}
}
-void load_rom(char *file_path) {
+void load_rom(char *path) {
memset(&vm, 0, sizeof(chip8_vm));
- int fd = open(file_path, O_RDONLY);
+ int fd = open(path, O_RDONLY);
FILE* f = fdopen(fd, "r");
fseek(f, 0, SEEK_END);
off_t fsize = ftell(f);
@@ -73,7 +73,7 @@ void load_rom(char *file_path) {
fclose(f); close(fd);
}
-void vm_step() {
+void vm_step(void) {
// read 2 bytes and convert to big-endian
u16 in = htons(vm.ram[vm.PC + 0x1]<<8 | vm.ram[vm.PC]);
@@ -198,7 +198,7 @@ void vm_step() {
for (u32 i = 0; i < (in&0xF); i++) {
for (u32 s = 0; s < 8; s++) {
- // 0x80 = 10000000
+ // 0x80 = b10000000
u8 pix = (vm.ram[vm.I + i] & (0x80 >> s)) != 0;
if (pix) {
@@ -207,12 +207,13 @@ void vm_step() {
u32 byte = y * 64 + x;
- // put the single 1 bit in the position
+ // put the single '1' bit in the position
// that is relative to the pixel
- u32 bit = 1 << (byte & 0x07);
+ u8 bit = 1 << (byte & 0x07);
// byte>>3 ~ byte / 8
- // used to get the index of byte to xor
+ // used to get the index of the byte
+ // containing the bit to change
if (vm.screen[byte>>3] & bit) {
vm.Vx[0xF] = 1;
}
diff --git a/chip8.h b/chip8.h
index ce02394..d21ae0a 100644
--- a/chip8.h
+++ b/chip8.h
@@ -2,9 +2,9 @@
#include "util.h"
-void init_vm();
-void vm_step();
-void load_rom();
+void init_vm(void);
+void vm_step(void);
+void load_rom(char *path);
typedef struct {
u8* ram;
diff --git a/ui_sdl.c b/ui_sdl.c
index 0433d5f..7ac32ca 100644
--- a/ui_sdl.c
+++ b/ui_sdl.c
@@ -1,6 +1,7 @@
#define SDL_MAIN_HANDLED
#include <time.h>
+#include <stdbool.h>
#ifdef _WIN32
#include <SDL.h>
@@ -15,19 +16,18 @@
SDL_Renderer *r;
-void setup() {
+void setup(void) {
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
SDL_Window* win = SDL_CreateWindow("chip8", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN);
r = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED);
SDL_RenderClear(r);
}
-void draw() {
- SDL_Rect box = (SDL_Rect){1, 0, 10, 10};
+void draw(void) {
+ SDL_Rect box = (SDL_Rect){0, 0, 10, 10};
SDL_SetRenderDrawColor(r, 12, 5, 9, 255);
SDL_RenderClear(r);
SDL_SetRenderDrawColor(r, 228, 167, 104, 255);
- //SDL_RenderFillRect(r, &box);
for (int x = 0; x <= 0x3F; x++) {
for (int y = 0; y <= 0x1F; y++) {
int byte = y * 64 + x;
@@ -44,7 +44,7 @@ void draw() {
int main(int argc, char* argv[]) {
if (argc != 2) {
- printf("%s [rom file]\n", argv[0]);
+ printf("./%s <rom file>\n", argv[0]);
return 1;
}
@@ -55,19 +55,20 @@ int main(int argc, char* argv[]) {
setup();
int last_500hz = SDL_GetTicks();
- int last_60hz = SDL_GetTicks();
- int last_45hz = SDL_GetTicks();
+ int last_60hz = last_500hz;
+ int last_45hz = last_500hz;
+
+ bool quit = false;
SDL_Event e;
- while (1) {
+ while (!quit) {
SDL_PollEvent(&e);
switch (e.type) {
+ case SDL_QUIT:
+ quit = true; break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym) {
- case SDLK_ESCAPE:
- SDL_Quit();
- exit(0);
- break;
+ case SDLK_ESCAPE: quit = true; break;
case '1': vm.keyboard[0x0] = 1; break;
case '2': vm.keyboard[0x1] = 1; break;
case '3': vm.keyboard[0x2] = 1; break;
@@ -132,6 +133,9 @@ int main(int argc, char* argv[]) {
last_45hz = cur;
}
}
+
+ SDL_Quit();
+
return 0;
}