Avances y correccion basica, funciona pero con detalles

This commit is contained in:
2025-06-21 20:39:24 -04:00
parent 32d70aa657
commit 4e48337e3f
12 changed files with 79 additions and 79 deletions

View File

@@ -7,8 +7,11 @@
#include "bitops.h"
Chip8::Chip8(): target_cycle_time(1.0 / 700.0), last_update_time(0), accumulator(0) {}
Chip8::Chip8(): target_cycle_time{1.0 / 700.0},
last_update_time{0},
accumulator{0},
run{false},
step{false} {}
bool Chip8::init() {
if (!graphics.init()) {
@@ -18,11 +21,9 @@ bool Chip8::init() {
last_update_time = SDL_GetTicks();
accumulator = 0;
const auto rom = read_rom("roms/1-ibm-logo.ch8");
const auto rom = read_rom("roms/6-keypad.ch8");
interpreter.load_rom(rom);
interpreter.display.set();
return true;
}
@@ -30,7 +31,6 @@ 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);
@@ -72,24 +72,59 @@ void Chip8::load_keyboard() {
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);
buffer << std::format(
"PC: {:03X} | SP: {:02X} | I {:02X} | DT {:02X} | ST {:02X} | INST: {:02X}{:02X} | V0-VF: ",
interpreter.pc,
interpreter.sp,
interpreter.i,
interpreter.dt,
interpreter.st,
interpreter.memory[interpreter.pc],
interpreter.memory[interpreter.pc + 1]
);
for (int i = 0; i < 16; ++i) {
buffer << std::format("{:02X}", interpreter.v[i]);
if (i < 15) buffer << ",";
}
buffer << " |";
buffer << " | ";
if (run) {
execute_instructions();
} else if (step) {
execute_instruction();
step = false;
last_update_time = SDL_GetTicks();
} else {
last_update_time = SDL_GetTicks();
}
graphics.draw(interpreter.display, buffer.str());
}
void Chip8::execute_instructions() {
const auto current_time = SDL_GetTicks();
const auto delta_time = static_cast<double>(current_time - last_update_time) / 1000.0;
last_update_time = current_time;
accumulator += delta_time;
while (accumulator >= target_cycle_time) {
execute_instruction();
accumulator -= target_cycle_time;
}
}
void Chip8::execute_instruction() {
interpreter.run();
}
void Chip8::on_keydown(SDL_Scancode scancode) {
if (scancode == SDL_SCANCODE_F1) {
run = !run;
} else if (scancode == SDL_SCANCODE_F2) {
step = true;
}
}