Agregando disasembler

This commit is contained in:
2025-06-28 01:40:57 -04:00
parent 82ed59edeb
commit ab4a43163a
11 changed files with 269 additions and 11 deletions

View File

@@ -14,13 +14,20 @@ Interpreter::Interpreter(std::shared_ptr<MachineState> machine_state) :
}
}
void Interpreter::tick()
uint16_t Interpreter::get_word(uint16_t address) const
{
const uint8_t high_word = this->machine_state->memory[this->machine_state->pc];
const uint8_t low_word = this->machine_state->memory[this->machine_state->pc + 1];
const uint8_t high_word = this->machine_state->memory[address];
const uint8_t low_word = this->machine_state->memory[address + 1];
const uint16_t word = high_word << 8 | low_word;
const auto instruction = this->decode(word);
return word;
}
void Interpreter::tick()
{
auto word = this->get_word(this->machine_state->pc);
const auto instruction = this->decode(word, this->machine_state->pc);
this->machine_state->pc += 2;
this->execute_instruction(instruction);
@@ -31,7 +38,7 @@ void Interpreter::tick()
}
}
Instruction Interpreter::decode(const uint16_t word) const
Instruction Interpreter::decode(const uint16_t word, const uint16_t address) const
{
const uint8_t operation = (word & 0xF000) >> 12;
const uint8_t x = (word & 0x0F00) >> 8;
@@ -197,6 +204,7 @@ Instruction Interpreter::decode(const uint16_t word) const
return Instruction{
.op_code = op_code,
.address = address,
.operation = operation,
.instruction = word,
.x = x,
@@ -207,6 +215,19 @@ Instruction Interpreter::decode(const uint16_t word) const
};
}
std::vector<Instruction> Interpreter::disassembly() const
{
std::vector<Instruction> instructions(std::size(machine_state->memory) / 2);
for (auto address = 0; address < std::size(machine_state->memory); address += 2)
{
const auto word = this->get_word(address);
instructions[address / 2] = (this->decode(word, address));
}
return instructions;
}
void Interpreter::execute_instruction(const Instruction& instruction)
{
if (instruction.op_code == OpCode::CLS) this->cls();
@@ -622,4 +643,4 @@ void Interpreter::ld_vx_i(const Instruction& instruction) const
this->machine_state->v[reg] = this->machine_state->memory[this->machine_state->i + reg];
}
}
}
}