Reorganizando codigo, otra vez :p

This commit is contained in:
2025-06-23 01:52:28 -04:00
parent 8df1300bd1
commit b0d8e6135d
23 changed files with 942 additions and 46 deletions

View File

@@ -0,0 +1,458 @@
#include "Interpreter.h"
#include <iostream>
#include <random>
Interpreter::Interpreter(std::shared_ptr<MachineState> machine_state):
machine_state{std::move(machine_state)},
random_generator{std::random_device{}()} {}
void Interpreter::tick() {
const uint8_t high_word = machine_state->memory[machine_state->pc];
const uint8_t low_word = machine_state->memory[machine_state->pc + 1];
const uint16_t word = high_word << 8 | low_word;
const auto instruction = decode(word);
machine_state->pc += 2;
execute_instruction(instruction);
if (machine_state->pc >= 0xFFF) {
std::cout << "PC Outside of memory, going back 0x200" << std::endl;
machine_state->pc = 0x200;
}
}
Instruction Interpreter::decode(const uint16_t word) {
const uint8_t operation = (word & 0xF000) >> 12;
const uint8_t x = (word & 0x0F00) >> 8;
const uint8_t y = (word & 0x00F0) >> 4;
const uint8_t n = word & 0x000F;
const uint8_t kk = word & 0x00FF;
const uint16_t nnn = word & 0x0FFF;
auto op_code = OpCode::NOP;
if (operation == 0) {
if (kk == 0xE0) {
op_code = OpCode::CLS;
} else if (kk == 0xEE) {
op_code = OpCode::RET;
} else {
op_code = OpCode::SYS_ADDR;
}
} else if (operation == 1) {
op_code = OpCode::JP_ADDR;
} else if (operation == 2) {
op_code = OpCode::CALL_ADDR;
} else if (operation == 3) {
op_code = OpCode::SE_VX_BYTE;
} else if (operation == 4) {
op_code = OpCode::SNE_VX_BYTE;
} else if (operation == 5) {
op_code = OpCode::SE_VX_VY;
} else if (operation == 6) {
op_code = OpCode::LD_VX_BYTE;
} else if (operation == 7) {
op_code = OpCode::ADD_VX_BYTE;
} else if (operation == 8) {
if (n == 0) {
op_code = OpCode::LD_VX_VY;
} else if (n == 1) {
op_code = OpCode::OR_VX_VY;
} else if (n == 2) {
op_code = OpCode::AND_VX_VY;
} else if (n == 3) {
op_code = OpCode::XOR_VX_VY;
} else if (n == 4) {
op_code = OpCode::ADD_VX_VY;
} else if (n == 5) {
op_code = OpCode::SUB_VX_VY;
} else if (n == 6) {
op_code = OpCode::SHR_VX_VY;
} else if (n == 7) {
op_code = OpCode::SUBN_VX_VY;
} else if (n == 0xE) {
op_code = OpCode::SHL_VX_VY;
}
} else if (operation == 9) {
op_code = OpCode::SNE_VX_VY;
} else if (operation == 0xA) {
op_code = OpCode::LD_I_ADDR;
} else if (operation == 0xB) {
op_code = OpCode::JP_V0_ADDR;
} else if (operation == 0xC) {
op_code = OpCode::RND_VX_BYTE;
} else if (operation == 0xD) {
op_code = OpCode::DRW_VX_VY_NIBBLE;
} else if (operation == 0xE) {
if (kk == 0x9E) {
op_code = OpCode::SKP_VX;
} else if (kk == 0xA1) {
op_code = OpCode::SKNP_VX;
}
} else if (operation == 0xF) {
if (kk == 0x07) {
op_code = OpCode::LD_VX_DT;
} else if (kk == 0x0A) {
op_code = OpCode::LD_VX_K;
} else if (kk == 0x15) {
op_code = OpCode::LD_DT_VX;
} else if (kk == 0x18) {
op_code = OpCode::LD_ST_VX;
} else if (kk == 0x1E) {
op_code = OpCode::ADD_I_VX;
} else if (kk == 0x29) {
op_code = OpCode::LD_F_VX;
} else if (kk == 0x33) {
op_code = OpCode::LD_B_VX;
} else if (kk == 0x55) {
op_code = OpCode::LD_I_VX;
} else if (kk == 0x65) {
op_code = OpCode::LD_VX_I;
}
}
return Instruction{
.op_code = op_code,
.operation = operation,
.instruction = word,
.x = x,
.y = y,
.n = n,
.kk = kk,
.nnn = nnn
};
}
void Interpreter::execute_instruction(const Instruction& instruction) {
if (instruction.op_code == OpCode::CLS) cls();
else if (instruction.op_code == OpCode::RET) ret();
else if (instruction.op_code == OpCode::SYS_ADDR) sys_addr();
else if (instruction.op_code == OpCode::JP_ADDR) jp_addr(instruction);
else if (instruction.op_code == OpCode::CALL_ADDR) call_addr(instruction);
else if (instruction.op_code == OpCode::SE_VX_BYTE) se_vx_byte(instruction);
else if (instruction.op_code == OpCode::SNE_VX_BYTE) sne_vx_byte(instruction);
else if (instruction.op_code == OpCode::SE_VX_VY) se_vx_vy(instruction);
else if (instruction.op_code == OpCode::LD_VX_BYTE) ld_vx_byte(instruction);
else if (instruction.op_code == OpCode::ADD_VX_BYTE) add_vx_byte(instruction);
else if (instruction.op_code == OpCode::LD_VX_VY) ld_vx_vy(instruction);
else if (instruction.op_code == OpCode::OR_VX_VY) or_vx_vy(instruction);
else if (instruction.op_code == OpCode::AND_VX_VY) and_vx_vy(instruction);
else if (instruction.op_code == OpCode::XOR_VX_VY) xor_vx_vy(instruction);
else if (instruction.op_code == OpCode::ADD_VX_VY) add_vx_vy(instruction);
else if (instruction.op_code == OpCode::SUB_VX_VY) sub_vx_vy(instruction);
else if (instruction.op_code == OpCode::SHR_VX_VY) shr_vx_vy(instruction);
else if (instruction.op_code == OpCode::SUBN_VX_VY) subn_vx_vy(instruction);
else if (instruction.op_code == OpCode::SHL_VX_VY) shl_vx_vy(instruction);
else if (instruction.op_code == OpCode::SNE_VX_VY) sne_vx_vy(instruction);
else if (instruction.op_code == OpCode::LD_I_ADDR) ld_i_addr(instruction);
else if (instruction.op_code == OpCode::JP_V0_ADDR) jp_v0_addr(instruction);
else if (instruction.op_code == OpCode::RND_VX_BYTE) rnd_vx_byte(instruction);
else if (instruction.op_code == OpCode::DRW_VX_VY_NIBBLE) drw_vx_vy_nibble(instruction);
else if (instruction.op_code == OpCode::SKP_VX) skp_vx(instruction);
else if (instruction.op_code == OpCode::SKNP_VX) sknp_vx(instruction);
else if (instruction.op_code == OpCode::LD_VX_DT) ld_vx_dt(instruction);
else if (instruction.op_code == OpCode::LD_VX_K) ld_vx_k(instruction);
else if (instruction.op_code == OpCode::LD_DT_VX) ld_dt_vx(instruction);
else if (instruction.op_code == OpCode::LD_ST_VX) ld_st_vx(instruction);
else if (instruction.op_code == OpCode::ADD_I_VX) add_i_vx(instruction);
else if (instruction.op_code == OpCode::LD_F_VX) ld_f_vx(instruction);
else if (instruction.op_code == OpCode::LD_B_VX) ld_b_vx(instruction);
else if (instruction.op_code == OpCode::LD_I_VX) ld_i_vx(instruction);
else if (instruction.op_code == OpCode::LD_VX_I) ld_vx_i(instruction);
else if (instruction.op_code == OpCode::NOP) nop();
}
void Interpreter::sys_addr() {
// NOP
}
void Interpreter::nop() {
// NOP
}
void Interpreter::cls() const {
machine_state->display.fill(false);
}
void Interpreter::ret() const {
machine_state->sp -= 1;
machine_state->pc = machine_state->stack[machine_state->sp];
}
void Interpreter::jp_addr(const Instruction& instruction) const {
machine_state->pc = instruction.nnn;
}
void Interpreter::call_addr(const Instruction& instruction) const {
machine_state->stack[machine_state->sp] = machine_state->pc;
machine_state->sp += 1;
machine_state->pc = instruction.nnn;
}
void Interpreter::se_vx_byte(const Instruction& instruction) const {
if (machine_state->v[instruction.x] == instruction.kk) {
machine_state->pc += 2;
}
}
void Interpreter::sne_vx_byte(const Instruction& instruction) const {
if (machine_state->v[instruction.x] != instruction.kk) {
machine_state->pc += 2;
}
}
void Interpreter::se_vx_vy(const Instruction& instruction) const {
if (machine_state->v[instruction.x] == machine_state->v[instruction.y]) {
machine_state->pc += 2;
}
}
void Interpreter::ld_vx_byte(const Instruction& instruction) const {
machine_state->v[instruction.x] = instruction.kk;
}
void Interpreter::add_vx_byte(const Instruction& instruction) const {
machine_state->v[instruction.x] += instruction.kk;
}
void Interpreter::ld_vx_vy(const Instruction& instruction) const {
machine_state->v[instruction.x] = machine_state->v[instruction.y];
}
void Interpreter::or_vx_vy(const Instruction& instruction) const {
machine_state->v[instruction.x] |= machine_state->v[instruction.y];
}
void Interpreter::and_vx_vy(const Instruction& instruction) const {
machine_state->v[instruction.x] &= machine_state->v[instruction.y];
}
void Interpreter::xor_vx_vy(const Instruction& instruction) const {
machine_state->v[instruction.x] ^= machine_state->v[instruction.y];
}
void Interpreter::add_vx_vy(const Instruction& instruction) const {
auto& vx = machine_state->v[instruction.x];
const auto& vy = machine_state->v[instruction.y];
auto& vf = machine_state->v[0xF];
if (vx + vy > 0xFF) {
vf = 1;
} else {
vf = 0;
}
vx += vy;
}
void Interpreter::sub_vx_vy(const Instruction& instruction) const {
auto& vx = machine_state->v[instruction.x];
const auto& vy = machine_state->v[instruction.y];
auto& vf = machine_state->v[0xF];
if (vx > vy) {
vf = 1;
} else {
vf = 0;
}
vx -= vy;
}
void Interpreter::shr_vx_vy(const Instruction& instruction) const {
auto& vx = machine_state->v[instruction.x];
const auto& vy = machine_state->v[instruction.y];
auto& vf = machine_state->v[0xF];
if (quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_SHIFT)) {
vx = vy;
}
if (vx & 0x01) {
vf = 1;
} else {
vf = 0;
}
vx = vx >> 1;
}
void Interpreter::subn_vx_vy(const Instruction& instruction) const {
auto& vx = machine_state->v[instruction.x];
const auto& vy = machine_state->v[instruction.y];
auto& vf = machine_state->v[0xF];
if (vy > vx) {
vf = 1;
} else {
vf = 0;
}
vx = vy - vx;
}
void Interpreter::shl_vx_vy(const Instruction& instruction) const {
auto& vx = machine_state->v[instruction.x];
const auto& vy = machine_state->v[instruction.y];
auto& vf = machine_state->v[0xF];
if (quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_SHIFT)) {
vx = vy;
}
if (vx & 0x80) {
vf = 1;
} else {
vf = 0;
}
vx = vx << 1;
}
void Interpreter::sne_vx_vy(const Instruction& instruction) const {
if (machine_state->v[instruction.x] != machine_state->v[instruction.y]) {
machine_state->pc += 2;
}
}
void Interpreter::ld_i_addr(const Instruction& instruction) const {
machine_state->i = instruction.nnn;
}
void Interpreter::jp_v0_addr(const Instruction& instruction) const {
if (quirks & static_cast<uint8_t>(InterpreterQuirks::SUPER_CHIP_JUMP)) {
machine_state->pc = instruction.nnn + machine_state->v[instruction.x];
} else {
machine_state->pc = instruction.nnn + machine_state->v[0];
}
}
void Interpreter::rnd_vx_byte(const Instruction& instruction) {
auto random = std::uniform_int_distribution<uint8_t>(0, 0xFF);
const auto value = random(random_generator);
machine_state->v[instruction.x] = value & instruction.kk;
}
void Interpreter::drw_vx_vy_nibble(const Instruction& instruction) const {
const auto& memory = machine_state->memory;
auto& display = machine_state->display;
const auto& vx = machine_state->v[instruction.x];
const auto& vy = machine_state->v[instruction.y];
auto& vf = machine_state->v[0xF];
const uint8_t start_x = vx & 63;
const uint8_t start_y = vy & 31;
vf = 0;
for (auto row = 0; row < instruction.n; row++) {
const auto current_y = start_y + row;
if (current_y > 31) {
break;
}
const auto sprite_byte = memory[machine_state->i + row];
for (auto bit = 0; bit < 8; bit++) {
const auto current_x = start_x + bit;
if (current_x > 63) {
break;
}
const auto pixel = sprite_byte >> (7 - bit) & 0x01;
const auto index = current_y * 64 + current_x;
if (pixel) {
if (display[index]) {
display[index] = false;
vf = 1;
} else {
display[index] = true;
}
}
}
}
}
void Interpreter::skp_vx(const Instruction& instruction) const {
if (machine_state->keyboard & 1 << instruction.x) {
machine_state->pc += 2;
}
}
void Interpreter::sknp_vx(const Instruction& instruction) const {
if (!(machine_state->keyboard & 1 << instruction.x)) {
machine_state->pc += 2;
}
}
void Interpreter::ld_vx_dt(const Instruction& instruction) const {
machine_state->v[instruction.x] = machine_state->dt;
}
void Interpreter::ld_vx_k(const Instruction& instruction) const {
if (machine_state->keyboard == 0) {
machine_state->pc -= 2;
return;
}
for (auto key = 0; key < 16; key++) {
if (machine_state->keyboard & 1 << key) {
machine_state->v[instruction.x] = key;
break;
}
}
}
void Interpreter::ld_dt_vx(const Instruction& instruction) const {
machine_state->dt = machine_state->v[instruction.x];
}
void Interpreter::ld_st_vx(const Instruction& instruction) const {
machine_state->st = machine_state->v[instruction.x];
}
void Interpreter::add_i_vx(const Instruction& instruction) const {
machine_state->i += machine_state->v[instruction.x];
}
void Interpreter::ld_f_vx(const Instruction& instruction) const {
machine_state->i = (machine_state->v[instruction.x] & 0xF) * 5 + 0x50;
}
void Interpreter::ld_b_vx(const Instruction& instruction) const {
const auto number = machine_state->v[instruction.x];
machine_state->memory[machine_state->i] = number / 100;
machine_state->memory[machine_state->i + 1] = (number - machine_state->memory[machine_state->i] * 100) / 10;
machine_state->memory[machine_state->i + 2] = number - machine_state->memory[machine_state->i] * 100 - machine_state->memory[machine_state->i + 1] * 10;
}
void Interpreter::ld_i_vx(const Instruction& instruction) const {
const bool use_quirk = quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_STORE_AND_LOAD);
for (auto reg = 0; reg <= instruction.x; reg++) {
if (use_quirk) {
machine_state->memory[machine_state->i] = machine_state->v[reg];
machine_state->i++;
} else {
machine_state->memory[machine_state->i + reg] = machine_state->v[reg];
}
}
}
void Interpreter::ld_vx_i(const Instruction& instruction) const {
const bool use_quirk = quirks & static_cast<uint8_t>(InterpreterQuirks::COSMAC_STORE_AND_LOAD);
for (auto reg = 0; reg <= instruction.x; reg++) {
if (use_quirk) {
machine_state->v[reg] = machine_state->memory[machine_state->i];
machine_state->i++;
} else {
machine_state->v[reg] = machine_state->memory[machine_state->i + reg];
}
}
}