chip8

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

commit 34baf1391a813f41ee4bfdfeeda1259e6d65e022
parent e96d56baefc3ef1e745ffb1415d7d6aaa1c077f3
Author: Christos Margiolis <christos@margiolis.net>
Date:   Fri, 14 Aug 2020 03:24:19 +0300

declared header functions extern, minor changes

Diffstat:
M.gitignore | 0
MREADME.md | 0
Mbin/chip8 | 0
Mobj/chip8.o | 0
Mobj/main.o | 0
Msrc/chip8.c | 41++++++++++++++++++++---------------------
Msrc/chip8.h | 17+++++++----------
Msrc/main.c | 6+++---
8 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/.gitignore b/.gitignore diff --git a/README.md b/README.md diff --git a/bin/chip8 b/bin/chip8 Binary files differ. diff --git a/obj/chip8.o b/obj/chip8.o 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 @@ -35,21 +35,20 @@ chip8_init(Chip8 *chip8) 0xF0, 0x80, 0xF0, 0x80, 0x80 // F }; - pc = 0x200; - opcode = 0; - I = 0; - sp = 0; + 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)); - memset(gfx, 0, 2048 * sizeof(uint8_t)); - memset(memory, 0, 4096 * sizeof(uint8_t)); + memset(V, 0, 16 * sizeof(uint8_t)); + memset(keys, 0, 16 * sizeof(uint8_t)); + memset(stack, 0, 16 * sizeof(uint16_t)); + memset(gfx, 0, 2048 * sizeof(uint8_t)); + memset(memory, 0, 4096 * sizeof(uint8_t)); int i; - for (i = 0; i < 80; i++) - memory[i] = fontset[i]; + for (i = 0; i < 80; memory[i] = fontset[i], i++); } int @@ -58,7 +57,7 @@ rom_load(Chip8 *chip8, const char *fpath) FILE *rom = fopen(fpath, "rb"); if (rom == NULL) { - ERROR("Error loading ROM (%s). Exiting. . .", fpath); + fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath); return FALSE; } fseek(rom, 0, SEEK_END); @@ -68,14 +67,14 @@ rom_load(Chip8 *chip8, const char *fpath) char *buf = (char *)malloc(romsize * sizeof(char)); if (buf == NULL) { - ERROR("%s", "Cannot allocate memory. Exiting. . ."); + fprintf(stderr, "Cannot allocate memory. Exiting. . .\n"); return FALSE; } size_t res = fread(buf, sizeof(char), (size_t)romsize, rom); if (res != romsize) { - ERROR("%s", "Error reading ROM. Exiting. . ."); + fprintf(stderr, "Error reading ROM. Exiting. . .\n"); return FALSE; } @@ -85,7 +84,7 @@ rom_load(Chip8 *chip8, const char *fpath) memory[i + 512] = (uint8_t)buf[i]; else { - ERROR("%s", "ROM can't fit into memory. Exiting. . ."); + fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n"); return FALSE; } @@ -103,7 +102,7 @@ emulate(Chip8 *chip8) execute(chip8); timers_update(chip8); } - LOG("opcode: %x\tmemory: %x\tI: %x\tsp: %x\tpc: %d", + printf("opcode: %x\tmemory: %x\tI: %x\tsp: %x\tpc: %d\n", opcode, memory[pc] << 8 | memory[pc + 1], I, sp, pc); } @@ -130,7 +129,7 @@ decode(Chip8 *chip8) pc = stack[--sp]; break; default: - ERROR("Unknown opcode: %x", opcode); + fprintf(stderr, "Unknown opcode: %x\n", opcode); return FALSE; } break; @@ -191,7 +190,7 @@ decode(Chip8 *chip8) V[(opcode & 0x0F00) >> 8] <<= 1; break; default: - ERROR("Unknown opcode: %x", opcode); + fprintf(stderr, "Unknown opcode: %x\n", opcode); return FALSE; } break; @@ -243,7 +242,7 @@ decode(Chip8 *chip8) if (!keys[V[(opcode & 0x0F00) >> 8]]) pc += 2; break; default: - ERROR("Unknown opcode: %x", opcode); + fprintf(stderr, "Unknown opcode: %x\n", opcode); return FALSE; } break; @@ -296,12 +295,12 @@ decode(Chip8 *chip8) I += ((opcode & 0x0F00) >> 8) + 1; break; default: - ERROR("Unknown opcode: %x", opcode); + fprintf(stderr, "Unknown opcode: %x\n", opcode); return FALSE; } break; default: - ERROR("%s", "Unimplemented opcode"); + fprintf(stderr, "Unimplemented opcode\n"); return FALSE; } return TRUE; diff --git a/src/chip8.h b/src/chip8.h @@ -10,9 +10,6 @@ #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 { uint8_t memory[4096]; uint8_t V[16]; @@ -30,12 +27,12 @@ typedef struct { extern Chip8 chip8; -void chip8_init(Chip8 *chip8); -int rom_load(Chip8 *chip8, const char *fpath); -void emulate(Chip8 *chip8); -void fetch(Chip8 *chip8); -int decode(Chip8 *chip8); -void execute(Chip8 *chip8); -void timers_update(Chip8 *chip8); +extern void chip8_init(Chip8 *chip8); +extern int rom_load(Chip8 *chip8, const char *fpath); +extern void emulate(Chip8 *chip8); +extern void fetch(Chip8 *chip8); +extern int decode(Chip8 *chip8); +extern void execute(Chip8 *chip8); +extern void timers_update(Chip8 *chip8); #endif /* CHIP8_H */ diff --git a/src/main.c b/src/main.c @@ -66,12 +66,12 @@ main(int argc, char **argv) if (argc != 2) { - ERROR("%s", "Usage: ./chip8 [ROM]"); + fprintf(stderr, "Usage: ./chip8 [ROM]\n"); return EXIT_FAILURE; } if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { - ERROR("%s", "Cannot initialize SDL. Exiting. . ."); + fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); return EXIT_FAILURE; } @@ -80,7 +80,7 @@ main(int argc, char **argv) SDL_WINDOW_SHOWN); if (win == NULL) { - ERROR("Cannot create SDL window. Exiting. . .\n%s", SDL_GetError()); + fprintf(stderr, "Cannot create SDL window. Exiting. . .\n%s\n", SDL_GetError()); return EXIT_FAILURE; }