chip8

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

commit b089101d50de1271b2f567ef6536b1fe2f70382a
parent f7fb7be8726576bcc5c1687d9d3e41ca25021f8c
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu,  3 Sep 2020 03:10:32 +0300

added everything in one file

Diffstat:
M.gitignore | 3+--
MMakefile | 30+++++++++++-------------------
Achip8.c | 420+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/chip8.c | 306-------------------------------------------------------------------------------
Dsrc/chip8.h | 34----------------------------------
Dsrc/main.c | 88-------------------------------------------------------------------------------
6 files changed, 432 insertions(+), 449 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,2 @@ /roms/* -/obj/ -/bin/ +*.o diff --git a/Makefile b/Makefile @@ -1,17 +1,8 @@ TARGET = chip8 INSTALL_PATH = /usr/local/bin -SRC_DIR = src -OBJ_DIR = obj -BIN_DIR = bin - -SRC = $(wildcard $(SRC_DIR)/*.c) -OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) - -CP=cp -MOVE = mv -MKDIR_P = mkdir -p -RM_DIR=rm -rf +SRC = $(wildcard *.c) +OBJ = $(SRC:%.c=%.o) CC = gcc CPPFLAGS += -Iinclude -pedantic @@ -19,24 +10,25 @@ CFLAGS += -Wall -std=c99 -O3 LDFLAGS += -Llib LDLIBS += -lSDL2 +CP=cp +MOVE = mv +MKDIR_P = mkdir -p + .PHONY: all clean all: $(TARGET) $(TARGET): $(OBJ) - $(MKDIR_P) $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ - $(MOVE) $(TARGET) $(BIN_DIR) -$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c - $(MKDIR_P) $(OBJ_DIR) +%.o: %.c $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ run: - ./$(BIN_DIR)/$(TARGET) + ./$(TARGET) install: $(TARGET) - $(CP) $(BIN_DIR)/$(TARGET) $(INSTALL_PATH) - + $(CP) $(TARGET) $(INSTALL_PATH) + clean: - $(RM_DIR) $(OBJ_DIR) $(BIN_DIR) + $(RM) $(OBJ) $(TARGET) diff --git a/chip8.c b/chip8.c @@ -0,0 +1,420 @@ +#define _DEFAULT_SOURCE +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> +#include <SDL2/SDL.h> + +#define TRUE 1 +#define FALSE 0 + +struct Chip8 { + uint8_t memory[4096]; + uint8_t V[16]; + uint8_t gfx[64 * 32]; + uint8_t keys[16]; + uint8_t delaytimer; + uint8_t soundtimer; + uint8_t drawflag; + uint16_t stack[16]; + uint16_t sp; + uint16_t opcode; + uint16_t I; + uint16_t pc; +}; + +static const uint8_t keymap[16] = { + SDLK_1, SDLK_2, SDLK_3, SDLK_4, + SDLK_q, SDLK_w, SDLK_e, SDLK_r, + SDLK_a, SDLK_s, SDLK_d, SDLK_f, + SDLK_z, SDLK_x, SDLK_c, SDLK_v +}; + +static void chip8_init(struct Chip8 *); +static int romload(struct Chip8 *, const char *); +static void emulate(struct Chip8 *); +static int decode(struct Chip8 *); +static void timers_update(struct Chip8 *); +static int evts(struct Chip8 *); +static void render(SDL_Renderer *, SDL_Texture *, struct Chip8 *); + +#define V chip8->V +#define pc chip8->pc +#define opcode chip8->opcode +#define I chip8->I +#define sp chip8->sp +#define memory chip8->memory +#define gfx chip8->gfx +#define stack chip8->stack +#define keys chip8->keys +#define delaytimer chip8->delaytimer +#define soundtimer chip8->soundtimer +#define drawflag chip8->drawflag + +void +chip8_init(struct Chip8 *chip8) +{ + uint8_t fontset[80] = { + 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 + 0x20, 0x60, 0x20, 0x20, 0x70, // 1 + 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2 + 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3 + 0x90, 0x90, 0xF0, 0x10, 0x10, // 4 + 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5 + 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6 + 0xF0, 0x10, 0x20, 0x40, 0x40, // 7 + 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8 + 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9 + 0xF0, 0x90, 0xF0, 0x90, 0x90, // A + 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B + 0xF0, 0x80, 0x80, 0x80, 0xF0, // C + 0xE0, 0x90, 0x90, 0x90, 0xE0, // D + 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)); + memset(gfx, 0, 2048 * sizeof(uint8_t)); + memset(memory, 0, 4096 * sizeof(uint8_t)); + int i; + for (i = 0; i < 80; memory[i] = fontset[i], i++); +} + +int +romload(struct Chip8 *chip8, const char *fpath) +{ + FILE *rom = fopen(fpath, "rb"); + if (rom == NULL) { + fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath); + return FALSE; + } + fseek(rom, 0, SEEK_END); + long romsize = ftell(rom); + rewind(rom); + + char *buf = (char *)malloc(romsize * sizeof(char)); + if (buf == NULL) { + fprintf(stderr, "Cannot allocate memory. Exiting. . .\n"); + return FALSE; + } + + size_t res = fread(buf, sizeof(char), (size_t)romsize, rom); + if (res != romsize) { + fprintf(stderr, "Error reading ROM. Exiting. . .\n"); + return FALSE; + } + + int i; + if ((4096 - 512) > romsize) + for (i = 0; i < romsize; i++) + memory[i + 512] = (uint8_t)buf[i]; + else { + fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n"); + return FALSE; + } + + fclose(rom); + free(buf); + return TRUE; +} + +void +emulate(struct Chip8 *chip8) +{ + opcode = memory[pc] << 8 | memory[pc + 1]; // fetch + if (decode(chip8)) { + pc += 2; // execute + timers_update(chip8); + } + printf("Opcode: %x\tMemory: %x\tI: %x\tSP: %x\tPC: %d\n", + opcode, memory[pc] << 8 | memory[pc + 1], I, sp, pc); +} + +int +decode(struct Chip8 *chip8) +{ + switch (opcode & 0xF000) { + int i; + case 0x0000: // 00E_ + switch (opcode & 0x00FF) { + case 0xE0: // 00E0 - Clear screen + memset(gfx, 0, 2048 * sizeof(uint8_t)); + drawflag = TRUE; + break; + case 0xEE: // 00EE - Return from subroutine + pc = stack[--sp]; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + case 0x1000: // 1NNN - Jump to address NNN + pc = (opcode & 0x0FFF) - 2; + break; + case 0x2000: // 2NNN - Call subroutine at NNN + stack[sp++] = pc; + pc = (opcode & 0x0FFF) - 2; + case 0x3000: // 3NNN - Skip next instruction if VX == NN + if (V[(opcode & 0x0F00) >> 8] == (opcode & 0x00FF)) pc += 2; + break; + case 0x4000: // 4NNN - Skip next instruction if VX != NN + if (V[(opcode & 0x0F00) >> 8] != (opcode & 0x00FF)) pc += 2; + break; + case 0x5000: // 5XY0 - Skip next instruction if VX == VY + if (V[(opcode & 0x0F00) >> 8] == V[(opcode & 0x00F0) >> 4]) pc += 2; + break; + case 0x6000: // 6XNN - Set VX to NN + V[(opcode & 0x0F00) >> 8] = opcode & 0x00FF; + break; + case 0x7000: // 7XNN - Add NN to VX + V[(opcode & 0x0F00) >> 8] += opcode & 0x00FF; + break; + case 0x8000: // 8XY_ + switch (opcode & 0x000F) { + case 0x0000: // 8XY0 - Set VX to VY + V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4]; + break; + case 0x0001: // 8XY1 - Set VX to (VX OR VY) + V[(opcode & 0x0F00) >> 8] |= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0002: // 8XY2 - Set VX to (VX AND VY) + V[(opcode & 0x0F00) >> 8] &= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0003: // 8XY3 - Set VX to (VX XOR VY) + V[(opcode & 0x0F00) >> 8] ^= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0004: // 8XY4 - Add VY to VX, VF = 1 if there is a carry + V[(opcode & 0x0F00) >> 8] += V[(opcode & 0x00F0) >> 4]; + V[0xF] = (V[(opcode & 0x00F0) >> 4] > (0xFF - V[(opcode & 0x0F00) >> 8])) ? 1 : 0; + break; + case 0x0005: // 8XY5 - Sub VY from VX, VF = 0 if there is a borrow + V[0xF] = (V[(opcode & 0x00F0) >> 4] > V[(opcode & 0x0F00) >> 8]) ? 0 : 1; + V[(opcode & 0x0F00) >> 8] -= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0006: // 8XY6 - Shift VX right by 1. VF = LSB of VX before shift + V[0xF] = V[(opcode & 0x0F00) >> 8] & 0x1; + V[(opcode & 0x0F00) >> 8] >>= 1; + break; + case 0x0007: // 8XY7 - Set VX to VY-VX. VF = 0 if there is a borrow + V[0xF] = (V[(opcode & 0x0F00) >> 8] > V[(opcode & 0x00F0) >> 4]) ? 0 : 1; + V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4] - V[(opcode & 0x0F00) >> 8]; + break; + case 0x000E: // 8XYE - Shift VX left by 1. VF = MSB of VX before shift + V[0xF] = V[(opcode & 0x0F00) >> 8] >> 7; + V[(opcode & 0x0F00) >> 8] <<= 1; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + case 0x9000: // 9XY0 - Skip next instruction if VX != VY + if (V[(opcode & 0x0F00) >> 8] != V[(opcode & 0x00F0) >> 4]) pc += 2; + break; + case 0xA000: // ANNN - Set I to the address NNN + I = opcode & 0x0FFF; + break; + case 0xB000: // BNNN - Jump to NNN + V0 + pc = ((opcode & 0x0FFF) + V[0]) - 2; + break; + case 0xC000: // CNNN - Set VX to random number masked by NN + V[(opcode & 0x0F00) >> 8] = (rand() % (0xFF + 1)) & (opcode & 0x00FF); + break; + case 0xD000: // Draw an 8 pixel sprite at (VX, VY) + { + uint8_t VX = V[(opcode & 0x0F00) >> 8]; + uint8_t VY = V[(opcode & 0x00F0) >> 4]; + uint16_t h = opcode & 0x000F; + uint16_t pixel; + + V[0xF] = 0; + int yl, xl; + for (yl = 0; yl < h; yl++) { + pixel = memory[I + yl]; + for (xl = 0; xl < 8; xl++) { + if ((pixel & (0x80 >> xl)) != 0) { + if (gfx[VX + xl + ((VY + yl) * 64)] == 1) + V[0xF] = 1; + gfx[VX + xl + ((VY + yl) * 64)] ^= 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]]) pc += 2; + break; + case 0x00A1: // EXA1 - Skip next instruction if key in VX isn't pressed + if (!keys[V[(opcode & 0x0F00) >> 8]]) pc += 2; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + case 0xF000: // FX__ + switch (opcode & 0x00FF) { + case 0x0007: // FX07 - Set VX to delaytimer + V[(opcode & 0x0F00) >> 8] = delaytimer; + break; + case 0x000A: // FX0A - Wait for key press and then store it in VX + { + int keypressed = FALSE; + for (i = 0; i < 16; i++) { + if (keys[i]) { + V[(opcode & 0x0F00) >> 8] = i; + keypressed = TRUE; + } + } + if (!keypressed) return FALSE; + } + break; + case 0x0015: // FX15 - Set the delaytimer to VX + delaytimer = V[(opcode & 0x0F00) >> 8]; + break; + case 0x0018: // FX18 - Set the soundtimer to VX + soundtimer = V[(opcode & 0x0F00) >> 8]; + break; + case 0x001E: // FX1E - Add VX to I + V[0xF] = ((I + V[(opcode & 0x0F00) >> 8]) > 0xFFF) ? 1 : 0; + I += V[(opcode & 0x0F00) >> 8]; + break; + case 0x0029: // FX29 - Set I to the location of the sprite for char VX + I = V[(opcode & 0x0F00) >> 8] * 0x5; + break; + case 0x0033: // FX33 - Store bin coded decimal of VX at I, I+1 and I+2 + memory[I] = V[(opcode & 0x0F00) >> 8] / 100; + memory[I+1] = (V[(opcode & 0x0F00) >> 8] / 10) % 10; + memory[I+2] = V[(opcode & 0x0F00) >> 8] % 10; + break; + case 0x0055: // FX55 - Store V0 to VX in memory starting at I + for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) + memory[I + i] = V[i]; + I += ((opcode & 0x0F00) >> 8) + 1; + break; + case 0x0065: // FX65 - Fill V0 to VX with vals from memory starting at I + for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) + V[i] = memory[I + i]; + I += ((opcode & 0x0F00) >> 8) + 1; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + default: + fprintf(stderr, "Unimplemented opcode\n"); + return FALSE; + } + return TRUE; +} + +void +timers_update(struct Chip8 *chip8) +{ + if (delaytimer > 0) --delaytimer; + if (soundtimer > 0) --soundtimer; +} + +#undef V +#undef pc +#undef opcode +#undef I +#undef sp +#undef memory +#undef gfx +#undef stack +#undef keys +#undef delaytimer +#undef soundtimer +#undef drawflag + +int +evts(struct Chip8 *chip8) +{ + int i; + SDL_Event e; + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE) return FALSE; + if (e.type == SDL_KEYDOWN) + 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 *ren, SDL_Texture *tex, struct 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(tex, NULL, pixels, 64 * sizeof(Uint32)); + SDL_RenderClear(ren); + SDL_RenderCopy(ren, tex, NULL, NULL); + SDL_RenderPresent(ren); +} + +int +main(int argc, char **argv) +{ + int w = 1024, h = 512; + srand(time(NULL)); + + if (argc != 2) { + fprintf(stderr, "Usage: ./chip8 [ROM]\n"); + return EXIT_FAILURE; + } + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { + fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); + return EXIT_FAILURE; + } + SDL_Window *win = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, w, h, + SDL_WINDOW_SHOWN); + 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, "SDL error. Exiting. . .\n%s\n", SDL_GetError()); + return EXIT_FAILURE; + } + + struct Chip8 chip8; + chip8_init(&chip8); + if (!romload(&chip8, argv[1])) return EXIT_FAILURE; + for (; evts(&chip8); usleep(1500)) { + emulate(&chip8); + if (chip8.drawflag) render(ren, tex, &chip8); + } + + SDL_DestroyTexture(tex); + SDL_DestroyRenderer(ren); + SDL_DestroyWindow(win); + SDL_Quit(); + return EXIT_SUCCESS; +} diff --git a/src/chip8.c b/src/chip8.c @@ -1,306 +0,0 @@ -#include "chip8.h" - -#define V chip8->V -#define pc chip8->pc -#define opcode chip8->opcode -#define I chip8->I -#define sp chip8->sp -#define memory chip8->memory -#define gfx chip8->gfx -#define stack chip8->stack -#define keys chip8->keys -#define delaytimer chip8->delaytimer -#define soundtimer chip8->soundtimer -#define drawflag chip8->drawflag - -static int chip8_decode(struct Chip8 *); -static void chip8_timers_update(struct Chip8 *); - -void -chip8_init(struct Chip8 *chip8) -{ - uint8_t fontset[80] = { - 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 - 0x20, 0x60, 0x20, 0x20, 0x70, // 1 - 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2 - 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3 - 0x90, 0x90, 0xF0, 0x10, 0x10, // 4 - 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5 - 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6 - 0xF0, 0x10, 0x20, 0x40, 0x40, // 7 - 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8 - 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9 - 0xF0, 0x90, 0xF0, 0x90, 0x90, // A - 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B - 0xF0, 0x80, 0x80, 0x80, 0xF0, // C - 0xE0, 0x90, 0x90, 0x90, 0xE0, // D - 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)); - memset(gfx, 0, 2048 * sizeof(uint8_t)); - memset(memory, 0, 4096 * sizeof(uint8_t)); - int i; - for (i = 0; i < 80; memory[i] = fontset[i], i++); -} - -int -chip8_rom_load(struct Chip8 *chip8, const char *fpath) -{ - FILE *rom = fopen(fpath, "rb"); - if (rom == NULL) { - fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath); - return FALSE; - } - fseek(rom, 0, SEEK_END); - long romsize = ftell(rom); - rewind(rom); - - char *buf = (char *)malloc(romsize * sizeof(char)); - if (buf == NULL) { - fprintf(stderr, "Cannot allocate memory. Exiting. . .\n"); - return FALSE; - } - - size_t res = fread(buf, sizeof(char), (size_t)romsize, rom); - if (res != romsize) { - fprintf(stderr, "Error reading ROM. Exiting. . .\n"); - return FALSE; - } - - int i; - if ((4096 - 512) > romsize) - for (i = 0; i < romsize; i++) - memory[i + 512] = (uint8_t)buf[i]; - else { - fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n"); - return FALSE; - } - - fclose(rom); - free(buf); - return TRUE; -} - -void -chip8_emulate(struct Chip8 *chip8) -{ - opcode = memory[pc] << 8 | memory[pc + 1]; // fetch - if (chip8_decode(chip8)) { - pc += 2; // execute - chip8_timers_update(chip8); - } - printf("Opcode: %x\tMemory: %x\tI: %x\tSP: %x\tPC: %d\n", - opcode, memory[pc] << 8 | memory[pc + 1], I, sp, pc); -} - -int -chip8_decode(struct Chip8 *chip8) -{ - switch (opcode & 0xF000) { - int i; - case 0x0000: // 00E_ - switch (opcode & 0x00FF) { - case 0xE0: // 00E0 - Clear screen - memset(gfx, 0, 2048 * sizeof(uint8_t)); - drawflag = TRUE; - break; - case 0xEE: // 00EE - Return from subroutine - pc = stack[--sp]; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - case 0x1000: // 1NNN - Jump to address NNN - pc = (opcode & 0x0FFF) - 2; - break; - case 0x2000: // 2NNN - Call subroutine at NNN - stack[sp++] = pc; - pc = (opcode & 0x0FFF) - 2; - case 0x3000: // 3NNN - Skip next instruction if VX == NN - if (V[(opcode & 0x0F00) >> 8] == (opcode & 0x00FF)) pc += 2; - break; - case 0x4000: // 4NNN - Skip next instruction if VX != NN - if (V[(opcode & 0x0F00) >> 8] != (opcode & 0x00FF)) pc += 2; - break; - case 0x5000: // 5XY0 - Skip next instruction if VX == VY - if (V[(opcode & 0x0F00) >> 8] == V[(opcode & 0x00F0) >> 4]) pc += 2; - break; - case 0x6000: // 6XNN - Set VX to NN - V[(opcode & 0x0F00) >> 8] = opcode & 0x00FF; - break; - case 0x7000: // 7XNN - Add NN to VX - V[(opcode & 0x0F00) >> 8] += opcode & 0x00FF; - break; - case 0x8000: // 8XY_ - switch (opcode & 0x000F) { - case 0x0000: // 8XY0 - Set VX to VY - V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4]; - break; - case 0x0001: // 8XY1 - Set VX to (VX OR VY) - V[(opcode & 0x0F00) >> 8] |= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0002: // 8XY2 - Set VX to (VX AND VY) - V[(opcode & 0x0F00) >> 8] &= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0003: // 8XY3 - Set VX to (VX XOR VY) - V[(opcode & 0x0F00) >> 8] ^= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0004: // 8XY4 - Add VY to VX, VF = 1 if there is a carry - V[(opcode & 0x0F00) >> 8] += V[(opcode & 0x00F0) >> 4]; - V[0xF] = (V[(opcode & 0x00F0) >> 4] > (0xFF - V[(opcode & 0x0F00) >> 8])) ? 1 : 0; - break; - case 0x0005: // 8XY5 - Sub VY from VX, VF = 0 if there is a borrow - V[0xF] = (V[(opcode & 0x00F0) >> 4] > V[(opcode & 0x0F00) >> 8]) ? 0 : 1; - V[(opcode & 0x0F00) >> 8] -= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0006: // 8XY6 - Shift VX right by 1. VF = LSB of VX before shift - V[0xF] = V[(opcode & 0x0F00) >> 8] & 0x1; - V[(opcode & 0x0F00) >> 8] >>= 1; - break; - case 0x0007: // 8XY7 - Set VX to VY-VX. VF = 0 if there is a borrow - V[0xF] = (V[(opcode & 0x0F00) >> 8] > V[(opcode & 0x00F0) >> 4]) ? 0 : 1; - V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4] - V[(opcode & 0x0F00) >> 8]; - break; - case 0x000E: // 8XYE - Shift VX left by 1. VF = MSB of VX before shift - V[0xF] = V[(opcode & 0x0F00) >> 8] >> 7; - V[(opcode & 0x0F00) >> 8] <<= 1; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - case 0x9000: // 9XY0 - Skip next instruction if VX != VY - if (V[(opcode & 0x0F00) >> 8] != V[(opcode & 0x00F0) >> 4]) pc += 2; - break; - case 0xA000: // ANNN - Set I to the address NNN - I = opcode & 0x0FFF; - break; - case 0xB000: // BNNN - Jump to NNN + V0 - pc = ((opcode & 0x0FFF) + V[0]) - 2; - break; - case 0xC000: // CNNN - Set VX to random number masked by NN - V[(opcode & 0x0F00) >> 8] = (rand() % (0xFF + 1)) & (opcode & 0x00FF); - break; - case 0xD000: // Draw an 8 pixel sprite at (VX, VY) - { - uint8_t VX = V[(opcode & 0x0F00) >> 8]; - uint8_t VY = V[(opcode & 0x00F0) >> 4]; - uint16_t h = opcode & 0x000F; - uint16_t pixel; - - V[0xF] = 0; - int yl, xl; - for (yl = 0; yl < h; yl++) { - pixel = memory[I + yl]; - for (xl = 0; xl < 8; xl++) { - if ((pixel & (0x80 >> xl)) != 0) { - if (gfx[VX + xl + ((VY + yl) * 64)] == 1) - V[0xF] = 1; - gfx[VX + xl + ((VY + yl) * 64)] ^= 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]]) pc += 2; - break; - case 0x00A1: // EXA1 - Skip next instruction if key in VX isn't pressed - if (!keys[V[(opcode & 0x0F00) >> 8]]) pc += 2; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - case 0xF000: // FX__ - switch (opcode & 0x00FF) { - case 0x0007: // FX07 - Set VX to delaytimer - V[(opcode & 0x0F00) >> 8] = delaytimer; - break; - case 0x000A: // FX0A - Wait for key press and then store it in VX - { - int keypressed = FALSE; - for (i = 0; i < 16; i++) { - if (keys[i]) { - V[(opcode & 0x0F00) >> 8] = i; - keypressed = TRUE; - } - } - if (!keypressed) return FALSE; - } - break; - case 0x0015: // FX15 - Set the delaytimer to VX - delaytimer = V[(opcode & 0x0F00) >> 8]; - break; - case 0x0018: // FX18 - Set the soundtimer to VX - soundtimer = V[(opcode & 0x0F00) >> 8]; - break; - case 0x001E: // FX1E - Add VX to I - V[0xF] = ((I + V[(opcode & 0x0F00) >> 8]) > 0xFFF) ? 1 : 0; - I += V[(opcode & 0x0F00) >> 8]; - break; - case 0x0029: // FX29 - Set I to the location of the sprite for char VX - I = V[(opcode & 0x0F00) >> 8] * 0x5; - break; - case 0x0033: // FX33 - Store bin coded decimal of VX at I, I+1 and I+2 - memory[I] = V[(opcode & 0x0F00) >> 8] / 100; - memory[I+1] = (V[(opcode & 0x0F00) >> 8] / 10) % 10; - memory[I+2] = V[(opcode & 0x0F00) >> 8] % 10; - break; - case 0x0055: // FX55 - Store V0 to VX in memory starting at I - for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) - memory[I + i] = V[i]; - I += ((opcode & 0x0F00) >> 8) + 1; - break; - case 0x0065: // FX65 - Fill V0 to VX with vals from memory starting at I - for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) - V[i] = memory[I + i]; - I += ((opcode & 0x0F00) >> 8) + 1; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - default: - fprintf(stderr, "Unimplemented opcode\n"); - return FALSE; - } - return TRUE; -} - -void -chip8_timers_update(struct Chip8 *chip8) -{ - if (delaytimer > 0) --delaytimer; - if (soundtimer > 0) --soundtimer; -} - -#undef V -#undef pc -#undef opcode -#undef I -#undef sp -#undef memory -#undef gfx -#undef stack -#undef keys -#undef delaytimer -#undef soundtimer -#undef drawflag diff --git a/src/chip8.h b/src/chip8.h @@ -1,34 +0,0 @@ -#ifndef CHIP8_H -#define CHIP8_H - -#include <inttypes.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> - -#define TRUE 1 -#define FALSE 0 - -struct Chip8 { - uint8_t memory[4096]; - uint8_t V[16]; - uint8_t gfx[64 * 32]; - uint8_t keys[16]; - uint8_t delaytimer; - uint8_t soundtimer; - uint8_t drawflag; - uint16_t stack[16]; - uint16_t sp; - uint16_t opcode; - uint16_t I; - uint16_t pc; -}; - -extern struct Chip8 chip8; - -extern void chip8_init(struct Chip8 *); -extern int chip8_rom_load(struct Chip8 *, const char *); -extern void chip8_emulate(struct Chip8 *); - -#endif /* CHIP8_H */ diff --git a/src/main.c b/src/main.c @@ -1,88 +0,0 @@ -#define _DEFAULT_SOURCE -#include <SDL2/SDL.h> -#include <unistd.h> -#include "chip8.h" - -static const uint8_t keymap[16] = { - SDLK_1, SDLK_2, SDLK_3, SDLK_4, - SDLK_q, SDLK_w, SDLK_e, SDLK_r, - SDLK_a, SDLK_s, SDLK_d, SDLK_f, - SDLK_z, SDLK_x, SDLK_c, SDLK_v -}; - -static int -evts(struct Chip8 *chip8) -{ - int i; - SDL_Event e; - while (SDL_PollEvent(&e)) - { - if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE) return FALSE; - if (e.type == SDL_KEYDOWN) - 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; -} - -static void -render(SDL_Renderer *ren, SDL_Texture *tex, struct 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(tex, NULL, pixels, 64 * sizeof(Uint32)); - SDL_RenderClear(ren); - SDL_RenderCopy(ren, tex, NULL, NULL); - SDL_RenderPresent(ren); -} - -int -main(int argc, char **argv) -{ - int w = 1024, h = 512; - srand(time(NULL)); - - if (argc != 2) { - fprintf(stderr, "Usage: ./chip8 [ROM]\n"); - return EXIT_FAILURE; - } - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { - fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); - return EXIT_FAILURE; - } - SDL_Window *win = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, w, h, - SDL_WINDOW_SHOWN); - 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, "SDL error. Exiting. . .\n%s\n", SDL_GetError()); - return EXIT_FAILURE; - } - - struct 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(ren, tex, &chip8); - } - - SDL_DestroyTexture(tex); - SDL_DestroyRenderer(ren); - SDL_DestroyWindow(win); - SDL_Quit(); - return EXIT_SUCCESS; -}