Version 0 not fully working
This commit is contained in:
10
src/CMakeLists.txt
Normal file
10
src/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
add_executable(${PROJECT_NAME}
|
||||
Chip8.cpp
|
||||
Chip8.h
|
||||
Interpreter.cpp
|
||||
Interpreter.h)
|
||||
target_sources(${PROJECT_NAME} PRIVATE main.cpp
|
||||
Graphics.cpp
|
||||
Graphics.h
|
||||
)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE vendor)
|
||||
95
src/Chip8.cpp
Normal file
95
src/Chip8.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "Chip8.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "bitops.h"
|
||||
|
||||
Chip8::Chip8(): target_cycle_time(1.0 / 700.0), last_update_time(0), accumulator(0) {}
|
||||
|
||||
|
||||
bool Chip8::init() {
|
||||
if (!graphics.init()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
last_update_time = SDL_GetTicks();
|
||||
accumulator = 0;
|
||||
|
||||
const auto rom = read_rom("roms/1-ibm-logo.ch8");
|
||||
interpreter.load_rom(rom);
|
||||
|
||||
interpreter.display.set();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Chip8::set_keyboard_state(std::span<const bool> keyboard_state) {
|
||||
this->keyboard_state = keyboard_state;
|
||||
}
|
||||
|
||||
|
||||
std::vector<uint8_t> Chip8::read_rom(const std::string& path) {
|
||||
std::ifstream rom_file(path, std::ios::binary);
|
||||
|
||||
rom_file.seekg(0, std::ios::end);
|
||||
const std::streampos file_size = rom_file.tellg();
|
||||
rom_file.seekg(0, std::ios::beg);
|
||||
|
||||
std::cout << "ROM size: " << file_size << std::endl;
|
||||
|
||||
std::vector<std::uint8_t> rom(file_size);
|
||||
|
||||
rom.insert(
|
||||
rom.begin(),
|
||||
std::istreambuf_iterator<char>(rom_file),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
|
||||
return rom;
|
||||
}
|
||||
|
||||
void Chip8::load_keyboard() {
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_X] ? bit_set(interpreter.keyboard, 0x0) : bit_clear(interpreter.keyboard, 0x0);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_1] ? bit_set(interpreter.keyboard, 0x1) : bit_clear(interpreter.keyboard, 0x1);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_2] ? bit_set(interpreter.keyboard, 0x2) : bit_clear(interpreter.keyboard, 0x2);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_3] ? bit_set(interpreter.keyboard, 0x3) : bit_clear(interpreter.keyboard, 0x3);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_Q] ? bit_set(interpreter.keyboard, 0x4) : bit_clear(interpreter.keyboard, 0x4);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_W] ? bit_set(interpreter.keyboard, 0x5) : bit_clear(interpreter.keyboard, 0x5);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_E] ? bit_set(interpreter.keyboard, 0x6) : bit_clear(interpreter.keyboard, 0x6);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_A] ? bit_set(interpreter.keyboard, 0x7) : bit_clear(interpreter.keyboard, 0x7);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_S] ? bit_set(interpreter.keyboard, 0x8) : bit_clear(interpreter.keyboard, 0x8);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_D] ? bit_set(interpreter.keyboard, 0x9) : bit_clear(interpreter.keyboard, 0x9);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_Z] ? bit_set(interpreter.keyboard, 0xA) : bit_clear(interpreter.keyboard, 0xA);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_C] ? bit_set(interpreter.keyboard, 0xB) : bit_clear(interpreter.keyboard, 0xB);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_4] ? bit_set(interpreter.keyboard, 0xC) : bit_clear(interpreter.keyboard, 0xC);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_R] ? bit_set(interpreter.keyboard, 0xD) : bit_clear(interpreter.keyboard, 0xD);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_F] ? bit_set(interpreter.keyboard, 0xE) : bit_clear(interpreter.keyboard, 0xE);
|
||||
interpreter.keyboard = keyboard_state[SDL_SCANCODE_V] ? bit_set(interpreter.keyboard, 0xF) : bit_clear(interpreter.keyboard, 0xF);
|
||||
}
|
||||
|
||||
|
||||
void Chip8::update() {
|
||||
auto current_time = SDL_GetTicks();
|
||||
double delta_time = static_cast<double>(current_time - last_update_time) / 1000.0;
|
||||
last_update_time = current_time;
|
||||
accumulator += delta_time;
|
||||
|
||||
load_keyboard();
|
||||
while (accumulator >= target_cycle_time) {
|
||||
interpreter.run();
|
||||
accumulator -= target_cycle_time;
|
||||
}
|
||||
|
||||
std::stringstream buffer;
|
||||
buffer << std::format("PC: {:03X} | SP: {:02X} | V0-VF: ", interpreter.pc, interpreter.sp);
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
buffer << std::format("{:02X}", interpreter.v[i]);
|
||||
if (i < 15) buffer << ",";
|
||||
}
|
||||
buffer << " |";
|
||||
|
||||
graphics.draw(interpreter.display, buffer.str());
|
||||
}
|
||||
35
src/Chip8.h
Normal file
35
src/Chip8.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef CHIP8_H
|
||||
#define CHIP8_H
|
||||
#include <span>
|
||||
|
||||
#include "Graphics.h"
|
||||
#include "Interpreter.h"
|
||||
|
||||
|
||||
class Chip8 {
|
||||
Graphics graphics;
|
||||
Interpreter interpreter;
|
||||
|
||||
double target_cycle_time;
|
||||
|
||||
Uint64 last_update_time;
|
||||
double accumulator;
|
||||
|
||||
std::span<const bool> keyboard_state;
|
||||
|
||||
void load_keyboard();
|
||||
|
||||
public:
|
||||
Chip8();
|
||||
|
||||
bool init();
|
||||
|
||||
void update();
|
||||
|
||||
std::vector<uint8_t> read_rom(const std::string& path);
|
||||
|
||||
void set_keyboard_state(std::span<const bool> keyboard_state);
|
||||
};
|
||||
|
||||
|
||||
#endif //CHIP8_H
|
||||
111
src/Graphics.cpp
Normal file
111
src/Graphics.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// Created by ryuuji on 6/20/25.
|
||||
//
|
||||
|
||||
#include "Graphics.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void SDLWindowDestroyer::operator()(SDL_Window* window) const {
|
||||
std::cout << "Destroying window" << std::endl;
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
|
||||
void SDLRendererDestroyer::operator()(SDL_Renderer* renderer) const {
|
||||
std::cout << "Destroying renderer" << std::endl;
|
||||
SDL_DestroyRenderer(renderer);
|
||||
}
|
||||
|
||||
void SDLTextureDestroyer::operator()(SDL_Texture* texture) const {
|
||||
std::cout << "Destroying texture" << std::endl;
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
Graphics::Graphics(): width(64 * 30),
|
||||
height(35 * 30),
|
||||
scale(30) {}
|
||||
|
||||
|
||||
bool Graphics::init() {
|
||||
SDL_SetAppMetadata("CHIP-8 Emulator", "0.0.1", "fun.skrd.chip8");
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_Window* raw_window = nullptr;
|
||||
SDL_Renderer* raw_renderer = nullptr;
|
||||
if (!SDL_CreateWindowAndRenderer("CHIP-8 Emulator", width, height, 0, &raw_window, &raw_renderer)) {
|
||||
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
this->window = std::unique_ptr<SDL_Window, SDLWindowDestroyer>(raw_window);
|
||||
this->renderer = std::unique_ptr<SDL_Renderer, SDLRendererDestroyer>(raw_renderer);
|
||||
|
||||
SDL_Texture* raw_texture = SDL_CreateTexture(renderer.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 64 * scale, 32 * scale);
|
||||
this->texture = std::unique_ptr<SDL_Texture, SDLTextureDestroyer>(raw_texture);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Graphics::draw(std::bitset<2048> display, std::string info) {
|
||||
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderClear(renderer.get());
|
||||
|
||||
draw_display(display);
|
||||
draw_info(info);
|
||||
|
||||
SDL_RenderPresent(renderer.get());
|
||||
}
|
||||
|
||||
void Graphics::draw_display(std::bitset<2048> display) {
|
||||
SDL_Surface* surface = nullptr;
|
||||
|
||||
if (SDL_LockTextureToSurface(texture.get(), nullptr, &surface)) {
|
||||
SDL_FillSurfaceRect(surface, nullptr, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), nullptr, 0, 0, 0));
|
||||
|
||||
for (int i = 0; i < display.size(); i++) {
|
||||
if (display[i]) {
|
||||
int x = (i % 64) * scale;
|
||||
int y = (i / 64) * scale;
|
||||
|
||||
SDL_Rect rect = {x, y, scale, scale};
|
||||
Uint32 color = SDL_MapRGB(
|
||||
SDL_GetPixelFormatDetails(surface->format),
|
||||
nullptr,
|
||||
0,
|
||||
255,
|
||||
0
|
||||
);
|
||||
|
||||
|
||||
SDL_FillSurfaceRect(surface, &rect, color);
|
||||
}
|
||||
}
|
||||
SDL_UnlockTexture(texture.get());
|
||||
}
|
||||
|
||||
SDL_FRect destination_rect{0.0, 0.0, static_cast<float>(64 * scale), static_cast<float>(32 * scale)};
|
||||
SDL_RenderTexture(renderer.get(), texture.get(), nullptr, &destination_rect);
|
||||
}
|
||||
|
||||
void Graphics::draw_info(std::string info) {
|
||||
const int charsize = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
|
||||
const float screen_scale = static_cast<float>(scale);
|
||||
const float text_scale = 2.0f;
|
||||
|
||||
const float info_area_start = 32.0f * screen_scale;
|
||||
const float info_area_height = (35.0f - 32.0f) * screen_scale;
|
||||
const float info_area_center = info_area_start + (info_area_height / 2.0f);
|
||||
|
||||
const float text_line = info_area_center / text_scale - (static_cast<float>(charsize) / 2.0f);
|
||||
|
||||
SDL_SetRenderScale(renderer.get(), text_scale, text_scale);
|
||||
SDL_SetRenderDrawColor(renderer.get(), 255, 255, 255, SDL_ALPHA_OPAQUE);
|
||||
|
||||
SDL_RenderDebugText(renderer.get(), 10 / text_scale, text_line, info.data());
|
||||
|
||||
SDL_SetRenderScale(renderer.get(), 1.0f, 1.0f);
|
||||
}
|
||||
41
src/Graphics.h
Normal file
41
src/Graphics.h
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by ryuuji on 6/20/25.
|
||||
//
|
||||
|
||||
#ifndef RENDERER_H
|
||||
#define RENDERER_H
|
||||
#include <bitset>
|
||||
#include <memory>
|
||||
|
||||
#include "SDL3/SDL.h"
|
||||
|
||||
struct SDLWindowDestroyer {
|
||||
void operator()(SDL_Window *window) const;
|
||||
};
|
||||
struct SDLRendererDestroyer {
|
||||
void operator()(SDL_Renderer *renderer) const;
|
||||
};
|
||||
struct SDLTextureDestroyer {
|
||||
void operator()(SDL_Texture *texture) const;
|
||||
};
|
||||
|
||||
class Graphics {
|
||||
std::shared_ptr<SDL_Window> window;
|
||||
std::shared_ptr<SDL_Renderer> renderer;
|
||||
std::shared_ptr<SDL_Texture> texture;
|
||||
|
||||
int width;
|
||||
int height;
|
||||
int scale;
|
||||
|
||||
void draw_display(std::bitset<2048> display);
|
||||
void draw_info(std::string info);
|
||||
public:
|
||||
Graphics();
|
||||
bool init();
|
||||
void draw(std::bitset<2048> display, std::string info);
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //RENDERER_H
|
||||
533
src/Interpreter.cpp
Normal file
533
src/Interpreter.cpp
Normal file
@@ -0,0 +1,533 @@
|
||||
#include "Interpreter.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <bits/ostream.tcc>
|
||||
|
||||
#include "SDL3/SDL_log.h"
|
||||
|
||||
Interpreter::Interpreter():
|
||||
memory(4096, 0),
|
||||
v(16, 0),
|
||||
stack(16, 0),
|
||||
pc(0x200),
|
||||
sp(0),
|
||||
i(0),
|
||||
dt(0),
|
||||
st(0),
|
||||
keyboard(0),
|
||||
quirks(0) {
|
||||
this->random_generator = std::mt19937(std::random_device{}());
|
||||
this->load_fonts();
|
||||
}
|
||||
|
||||
void Interpreter::load_fonts() {
|
||||
constexpr uint8_t font_set[] = {
|
||||
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
|
||||
};
|
||||
|
||||
std::copy(std::begin(font_set), std::end(font_set), memory.begin() + 0x050);
|
||||
}
|
||||
|
||||
|
||||
void Interpreter::set_quirks(uint8_t quirks) {
|
||||
this->quirks = quirks;
|
||||
}
|
||||
|
||||
void Interpreter::load_rom(const std::vector<uint8_t>& rom) {
|
||||
std::copy(std::begin(rom), std::end(rom), memory.begin() + 0x200);
|
||||
}
|
||||
|
||||
|
||||
void Interpreter::run() {
|
||||
const uint8_t high_instruction = memory[pc];
|
||||
const uint8_t low_instruction = memory[pc + 1];
|
||||
const uint16_t instruction = (high_instruction << 8) | low_instruction;
|
||||
|
||||
const uint8_t opcode = instruction & 0xF000 >> 12;
|
||||
const uint8_t x = instruction & 0x0F00 >> 8;
|
||||
const uint8_t y = instruction & 0x00F0 >> 4;
|
||||
const uint8_t n = instruction & 0x000F;
|
||||
const uint8_t kk = instruction & 0x00FF;
|
||||
const uint16_t nnn = instruction & 0x0FFF;
|
||||
|
||||
pc += 2;
|
||||
|
||||
switch (opcode) {
|
||||
case 0:
|
||||
if (kk == 0xE0) {
|
||||
clear_screen();
|
||||
} else if (kk == 0xEE) {
|
||||
return_from_subrutine();
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
jump(nnn);
|
||||
break;
|
||||
case 2: call_subrutine(nnn);
|
||||
break;
|
||||
case 3:
|
||||
skip_if_vx_eq_val(x, kk);
|
||||
break;
|
||||
case 4:
|
||||
skip_if_vx_neq_val(x, kk);
|
||||
break;
|
||||
case 5:
|
||||
skip_if_vx_eq_vy(x, y);
|
||||
break;
|
||||
case 6:
|
||||
load_vx_val(x, kk);
|
||||
break;
|
||||
case 7:
|
||||
add_vx_val(x, kk);
|
||||
break;
|
||||
case 8:
|
||||
switch (n) {
|
||||
case 0:
|
||||
load_vx_vy(x, y);
|
||||
break;
|
||||
case 1:
|
||||
vx_or_vy(x, y);
|
||||
break;
|
||||
case 2:
|
||||
vx_and_vy(x, y);
|
||||
break;
|
||||
case 3:
|
||||
vx_xor_vy(x, y);
|
||||
break;
|
||||
case 4:
|
||||
vx_add_vy(x, y);
|
||||
break;
|
||||
case 5:
|
||||
vx_sub_vy(x, y);
|
||||
break;
|
||||
case 6:
|
||||
if (quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_SHIFT)) {
|
||||
vx_shift_right_cosmac(x, y);
|
||||
} else {
|
||||
vx_shift_right(x);
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
vx_subn_vy(x, y);
|
||||
break;
|
||||
case 0xE:
|
||||
if (quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_SHIFT)) {
|
||||
vx_shift_left_cosmac(x, y);
|
||||
} else {
|
||||
vx_shift_left(x);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 9:
|
||||
skip_if_vx_neq_vy(x, y);
|
||||
break;
|
||||
case 0xA:
|
||||
set_index_val(nnn);
|
||||
break;
|
||||
case 0xB:
|
||||
if (quirks & static_cast<int>(InterpreterQuirks::SUPER_CHIP_JUMP)) {
|
||||
jump_with_offset_super_chip(x, nnn);
|
||||
} else {
|
||||
jump_with_offset(nnn);
|
||||
}
|
||||
break;
|
||||
case 0XC:
|
||||
set_random_value(x, kk);
|
||||
break;
|
||||
case 0xD:
|
||||
display_sprite(x, y, n);
|
||||
break;
|
||||
case 0xE:
|
||||
if (kk == 0x9E) {
|
||||
skip_if_key_pressed(x);
|
||||
} else if (kk == 0xA1) {
|
||||
skip_if_key_not_pressed(x);
|
||||
}
|
||||
case 0xF:
|
||||
switch (kk) {
|
||||
case 0x07:
|
||||
load_vx_dt(x);
|
||||
break;
|
||||
case 0x0A:
|
||||
get_key(x);
|
||||
break;
|
||||
case 0x15:
|
||||
load_dt_vx(x);
|
||||
break;
|
||||
case 0x18:
|
||||
load_st_vx(x);
|
||||
break;
|
||||
case 0x1E:
|
||||
add_i_vx(x);
|
||||
break;
|
||||
case 0x29:
|
||||
font_sprite_location(x);
|
||||
break;
|
||||
case 0x33:
|
||||
calculate_bcd(x);
|
||||
break;
|
||||
case 0x55:
|
||||
store_registers_to_memory(x);
|
||||
break;
|
||||
case 0x65:
|
||||
load_registers_from_memory(x);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (pc >= 0xFFF) {
|
||||
SDL_Log("PC Outside of memory, going back 0x200");
|
||||
pc = 0x200;
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::clear_screen() {
|
||||
display.reset();
|
||||
}
|
||||
|
||||
void Interpreter::return_from_subrutine() {
|
||||
sp -= 1;
|
||||
pc = stack[sp];
|
||||
}
|
||||
|
||||
void Interpreter::jump(const uint16_t nnn) {
|
||||
pc = nnn;
|
||||
}
|
||||
|
||||
void Interpreter::call_subrutine(const uint16_t nnn) {
|
||||
stack[sp] = pc;
|
||||
sp += 1;
|
||||
pc = nnn;
|
||||
}
|
||||
|
||||
void Interpreter::skip_if_vx_eq_val(const uint8_t x, const uint8_t kk) {
|
||||
if (v[x] == kk) {
|
||||
pc += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::skip_if_vx_neq_val(const uint8_t x, const uint8_t kk) {
|
||||
if (v[x] != kk) {
|
||||
pc += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::skip_if_vx_eq_vy(const uint8_t x, const uint8_t y) {
|
||||
if (v[x] == v[y]) {
|
||||
pc += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::load_vx_val(const uint8_t x, const uint8_t kk) {
|
||||
v[x] = kk;
|
||||
}
|
||||
|
||||
void Interpreter::add_vx_val(const uint8_t x, const uint8_t kk) {
|
||||
v[x] += kk;
|
||||
}
|
||||
|
||||
void Interpreter::load_vx_vy(const uint8_t x, const uint8_t y) {
|
||||
v[x] = v[y];
|
||||
}
|
||||
|
||||
void Interpreter::vx_or_vy(const uint8_t x, const uint8_t y) {
|
||||
v[x] = v[x] | v[y];
|
||||
}
|
||||
|
||||
void Interpreter::vx_and_vy(const uint8_t x, const uint8_t y) {
|
||||
v[x] = v[x] & v[y];
|
||||
}
|
||||
|
||||
void Interpreter::vx_xor_vy(const uint8_t x, const uint8_t y) {
|
||||
v[x] = v[x] ^ v[y];
|
||||
}
|
||||
|
||||
void Interpreter::vx_add_vy(const uint8_t x, const uint8_t y) {
|
||||
if (v[x] + v[y] > 0xFF) {
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
v[0xF] = 0;
|
||||
}
|
||||
|
||||
v[x] = (v[x] + v[y]);
|
||||
}
|
||||
|
||||
void Interpreter::vx_sub_vy(const uint8_t x, const uint8_t y) {
|
||||
if (v[x] > v[y]) {
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
v[0xF] = 0;
|
||||
}
|
||||
|
||||
v[x] = v[x] - v[y];
|
||||
}
|
||||
|
||||
void Interpreter::vx_shift_right(const uint8_t x) {
|
||||
if (v[x] & 0x01) {
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
v[0xF] = 0;
|
||||
}
|
||||
|
||||
v[x] = v[x] >> 1;
|
||||
}
|
||||
|
||||
void Interpreter::vx_shift_right_cosmac(const uint8_t x, const uint8_t y) {
|
||||
v[x] = v[y];
|
||||
|
||||
if (v[x] & 0x01) {
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
v[0xF] = 0;
|
||||
}
|
||||
|
||||
v[x] = v[x] >> 1;
|
||||
}
|
||||
|
||||
void Interpreter::vx_subn_vy(const uint8_t x, const uint8_t y) {
|
||||
if (v[y] > v[x]) {
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
v[0xF] = 0;
|
||||
}
|
||||
|
||||
v[x] = v[y] - v[x];
|
||||
}
|
||||
|
||||
void Interpreter::vx_shift_left(const uint8_t x) {
|
||||
if (v[x] & 0x80) {
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
v[0xF] = 0;
|
||||
}
|
||||
|
||||
v[x] = v[x] << 1;
|
||||
}
|
||||
|
||||
void Interpreter::vx_shift_left_cosmac(const uint8_t x, const uint8_t y) {
|
||||
v[x] = v[y];
|
||||
|
||||
if (v[x] & 0x80) {
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
v[0xF] = 0;
|
||||
}
|
||||
|
||||
v[x] = v[x] << 1;
|
||||
}
|
||||
|
||||
void Interpreter::skip_if_vx_neq_vy(const uint8_t x, const uint8_t y) {
|
||||
if (v[x] != v[y]) {
|
||||
pc += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::set_index_val(const uint16_t nnn) {
|
||||
i = nnn;
|
||||
}
|
||||
|
||||
void Interpreter::jump_with_offset(const uint16_t nnn) {
|
||||
pc = nnn + v[0];
|
||||
}
|
||||
|
||||
void Interpreter::jump_with_offset_super_chip(const uint8_t x, const uint16_t nnn) {
|
||||
pc = nnn + v[x];
|
||||
}
|
||||
|
||||
void Interpreter::set_random_value(const uint8_t x, const uint8_t kk) {
|
||||
auto random = std::uniform_int_distribution<uint8_t>(0, 0xFF);
|
||||
const auto value = random(random_generator);
|
||||
|
||||
v[x] = value & kk;
|
||||
}
|
||||
|
||||
void Interpreter::display_sprite(uint8_t x, uint8_t y, const uint8_t n) {
|
||||
uint8_t start_x = v[x] & 63;
|
||||
uint8_t start_y = v[y] & 31;
|
||||
v[0xF] = 0;
|
||||
|
||||
for (auto row = 0; row < n; row++) {
|
||||
auto current_y = start_y + row;
|
||||
|
||||
if (current_y > 31) {
|
||||
break;
|
||||
}
|
||||
|
||||
const auto sprite_byte = memory[i + row];
|
||||
|
||||
for (auto bit = 0; bit < 8; bit++) {
|
||||
auto current_x = start_x + bit;
|
||||
|
||||
if (current_x > 63) {
|
||||
break;
|
||||
}
|
||||
|
||||
const auto pixel = (sprite_byte >> (7 - bit)) & 0x01;
|
||||
const auto index = (start_y * 64) + current_x;
|
||||
|
||||
if (pixel) {
|
||||
if (display[index]) {
|
||||
display.set(index, false);
|
||||
v[0xF] = 1;
|
||||
} else {
|
||||
display.set(index, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::skip_if_key_pressed(const uint8_t x) {
|
||||
if (keyboard & (1 << x)) {
|
||||
pc += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::skip_if_key_not_pressed(const uint8_t x) {
|
||||
if (!(keyboard & (1 << x))) {
|
||||
pc += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::load_vx_dt(const uint8_t x) {
|
||||
v[x] = dt;
|
||||
}
|
||||
|
||||
void Interpreter::get_key(const uint8_t x) {
|
||||
if (keyboard == 0) {
|
||||
pc -= 2;
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto key = 0; key < 16; key++) {
|
||||
if (keyboard & (1 << key)) {
|
||||
v[x] = key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::load_dt_vx(const uint8_t x) {
|
||||
dt = v[x];
|
||||
}
|
||||
|
||||
void Interpreter::load_st_vx(const uint8_t x) {
|
||||
st = v[x];
|
||||
}
|
||||
|
||||
void Interpreter::add_i_vx(const uint8_t x) {
|
||||
i = i + v[x];
|
||||
}
|
||||
|
||||
void Interpreter::font_sprite_location(const uint8_t x) {
|
||||
i = ((v[x] & 0xF) * 5) + 0x50;
|
||||
}
|
||||
|
||||
void Interpreter::calculate_bcd(const uint8_t x) {
|
||||
const auto number = v[x];
|
||||
memory[i] = number / 100;
|
||||
memory[i + 1] = (number - (memory[i] * 100)) / 10;
|
||||
memory[i + 2] = number - (memory[i] * 100) - (memory[i + 1] * 10);
|
||||
}
|
||||
|
||||
void Interpreter::store_registers_to_memory(const uint8_t x) {
|
||||
bool use_quirk = quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_STORE_AND_LOAD);
|
||||
|
||||
for (auto reg = 0; reg <= x; reg++) {
|
||||
if (use_quirk) {
|
||||
memory[i] = v[reg];
|
||||
i++;
|
||||
} else {
|
||||
memory[i + reg] = v[reg];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::load_registers_from_memory(const uint8_t x) {
|
||||
bool use_quirk = quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_STORE_AND_LOAD);
|
||||
|
||||
for (auto reg = 0; reg <= x; reg++) {
|
||||
if (use_quirk) {
|
||||
v[reg] = memory[i];
|
||||
i++;
|
||||
} else {
|
||||
v[reg] = memory[i + reg];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Interpreter::debug_display_console() {
|
||||
std::cout << "\n=== CHIP-8 Display Debug ===" << std::endl;
|
||||
std::cout << "Resolution: 64x32 pixels" << std::endl;
|
||||
std::cout << "Display buffer size: " << display.size() << std::endl;
|
||||
|
||||
// Contar píxeles activos
|
||||
int active_pixels = 0;
|
||||
for (int i = 0; i < display.size(); i++) {
|
||||
if (display[i]) active_pixels++;
|
||||
}
|
||||
std::cout << "Active pixels: " << active_pixels << std::endl;
|
||||
|
||||
std::cout << "\nDisplay contents:" << std::endl;
|
||||
std::cout << " ";
|
||||
|
||||
// Números de columna (cada 8 píxeles)
|
||||
for (int x = 0; x < 64; x += 8) {
|
||||
std::cout << std::setw(2) << std::setfill(' ') << x << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Dibujar el display
|
||||
for (int y = 0; y < 32; y++) {
|
||||
std::cout << std::setw(2) << std::setfill(' ') << y << " ";
|
||||
|
||||
for (int x = 0; x < 64; x++) {
|
||||
int index = (y * 64) + x;
|
||||
|
||||
if (display[index]) {
|
||||
std::cout << "█"; // Píxel activo
|
||||
} else {
|
||||
std::cout << "·"; // Píxel inactivo
|
||||
}
|
||||
|
||||
// Separador cada 8 píxeles para mayor legibilidad
|
||||
if ((x + 1) % 8 == 0 && x < 63) {
|
||||
std::cout << "|";
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Línea separadora cada 8 filas
|
||||
if ((y + 1) % 8 == 0 && y < 31) {
|
||||
std::cout << " ";
|
||||
for (int x = 0; x < 64; x++) {
|
||||
std::cout << "-";
|
||||
if ((x + 1) % 8 == 0 && x < 63) {
|
||||
std::cout << "+";
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << "=== End Display Debug ===" << std::endl << std::endl;
|
||||
}
|
||||
83
src/Interpreter.h
Normal file
83
src/Interpreter.h
Normal file
@@ -0,0 +1,83 @@
|
||||
#ifndef INTERPRETER_H
|
||||
#define INTERPRETER_H
|
||||
#include <bitset>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
enum class InterpreterQuirks {
|
||||
COSMAC_SHIFT = 1 << 0,
|
||||
SUPER_CHIP_JUMP = 1 << 1,
|
||||
COSMAC_STORE_AND_LOAD = 1 << 2,
|
||||
};
|
||||
|
||||
class Interpreter {
|
||||
std::mt19937 random_generator;
|
||||
|
||||
public:
|
||||
std::vector<uint8_t> memory;
|
||||
std::vector<uint16_t> v;
|
||||
std::vector<uint16_t> stack;
|
||||
uint16_t pc;
|
||||
uint8_t sp;
|
||||
uint16_t i;
|
||||
uint8_t dt;
|
||||
uint8_t st;
|
||||
std::bitset<2048> display;
|
||||
uint16_t keyboard;
|
||||
|
||||
private:
|
||||
uint8_t quirks;
|
||||
|
||||
void load_fonts();
|
||||
|
||||
void clear_screen();
|
||||
void return_from_subrutine();
|
||||
void jump(uint16_t nnn);
|
||||
void call_subrutine(uint16_t nnn);
|
||||
void skip_if_vx_eq_val(uint8_t x, uint8_t kk);
|
||||
void skip_if_vx_neq_val(uint8_t x, uint8_t kk);
|
||||
void skip_if_vx_eq_vy(uint8_t x, uint8_t y);
|
||||
void load_vx_val(uint8_t x, uint8_t kk);
|
||||
void add_vx_val(uint8_t x, uint8_t kk);
|
||||
void load_vx_vy(uint8_t x, uint8_t y);
|
||||
void vx_or_vy(uint8_t x, uint8_t y);
|
||||
void vx_and_vy(uint8_t x, uint8_t y);
|
||||
void vx_xor_vy(uint8_t x, uint8_t y);
|
||||
void vx_add_vy(uint8_t x, uint8_t y);
|
||||
void vx_sub_vy(uint8_t x, uint8_t y);
|
||||
void vx_shift_right(uint8_t x);
|
||||
void vx_shift_right_cosmac(uint8_t x, uint8_t y);
|
||||
void vx_subn_vy(uint8_t x, uint8_t y);
|
||||
void vx_shift_left(uint8_t x);
|
||||
void vx_shift_left_cosmac(uint8_t x, uint8_t y);
|
||||
void skip_if_vx_neq_vy(uint8_t x, uint8_t y);
|
||||
void set_index_val(uint16_t nnn);
|
||||
void jump_with_offset(uint16_t nnn);
|
||||
void jump_with_offset_super_chip(uint8_t x, uint16_t nnn);
|
||||
void set_random_value(uint8_t x, uint8_t kk);
|
||||
void display_sprite(uint8_t x, uint8_t y, uint8_t n);
|
||||
void skip_if_key_pressed(uint8_t x);
|
||||
void skip_if_key_not_pressed(uint8_t x);
|
||||
void load_vx_dt(uint8_t x);
|
||||
void get_key(uint8_t x);
|
||||
void load_dt_vx(uint8_t x);
|
||||
void load_st_vx(uint8_t x);
|
||||
void add_i_vx(uint8_t x);
|
||||
void font_sprite_location(uint8_t x);
|
||||
void calculate_bcd(uint8_t x);
|
||||
void store_registers_to_memory(uint8_t x);
|
||||
void load_registers_from_memory(uint8_t x);
|
||||
|
||||
public:
|
||||
Interpreter();
|
||||
|
||||
void set_quirks(uint8_t quirks);
|
||||
void load_rom(const std::vector<uint8_t>& rom);
|
||||
|
||||
void run();
|
||||
void debug_display_console();
|
||||
};
|
||||
|
||||
|
||||
#endif //INTERPRETER_H
|
||||
33
src/bitops.h
Normal file
33
src/bitops.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by ryuuji on 6/21/25.
|
||||
//
|
||||
|
||||
#ifndef BITOPS_H
|
||||
#define BITOPS_H
|
||||
#include <type_traits>
|
||||
|
||||
template <typename T>
|
||||
T bit_set(const T number, const int bit) {
|
||||
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integral type");
|
||||
return number | (1 << bit);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T bit_clear(const T number, const int bit) {
|
||||
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integral type");
|
||||
return number & ~(1 << bit);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T bit_toggle(const T number, const int bit) {
|
||||
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integral type");
|
||||
return number ^ (1 << bit);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool bit_check(const T number, const int bit) {
|
||||
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integral type");
|
||||
return (number >> bit) & 1;
|
||||
}
|
||||
|
||||
#endif //BITOPS_H
|
||||
41
src/main.cpp
Normal file
41
src/main.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#define SDL_MAIN_USE_CALLBACKS 1
|
||||
#include <iostream>
|
||||
#include <bits/ostream.tcc>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
#include "Chip8.h"
|
||||
|
||||
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
|
||||
const auto chip8 = new Chip8();
|
||||
*appstate = chip8;
|
||||
|
||||
if (!chip8->init()) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
int num_keys;
|
||||
const bool* kb_state = SDL_GetKeyboardState(&num_keys);
|
||||
const std::span kb_state_view(kb_state, num_keys);
|
||||
chip8->set_keyboard_state(kb_state_view);
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void* appstate) {
|
||||
const auto chip8 = static_cast<Chip8*>(appstate);
|
||||
|
||||
chip8->update();
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS;
|
||||
}
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
|
||||
const auto chip8 = static_cast<Chip8*>(appstate);
|
||||
delete chip8;
|
||||
}
|
||||
Reference in New Issue
Block a user