From 71b524519d171067cb1e2ad128ad5c0fba0fbe6e Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Mon, 28 Feb 2022 17:04:20 +0100 Subject: [PATCH] fixed disassembler --- src/CPU.cpp | 6 +++--- src/CPU.hpp | 2 +- src/debugger/Disassembler.cpp | 27 ++++++++++++++++----------- src/debugger/Disassembler.hpp | 2 ++ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/CPU.cpp b/src/CPU.cpp index 8ba1230..adf31eb 100644 --- a/src/CPU.cpp +++ b/src/CPU.cpp @@ -56,9 +56,9 @@ uint8_t CPU::Tick() Byte opcode = Read(pc.Raw++); currentInstruction = &(InstructionTable[opcode]); - pastPCs.push_back(currentInstruction); - if (pastPCs.size() > 50) - pastPCs.pop_front(); + pastInstructions.push_back(std::make_pair(pc.Raw - 1, currentInstruction)); + if (pastInstructions.size() > 50) + pastInstructions.pop_front(); if (currentInstruction->Operation == nullptr || currentInstruction->Mode == nullptr) { diff --git a/src/CPU.hpp b/src/CPU.hpp index 38ec0a0..8241d57 100644 --- a/src/CPU.hpp +++ b/src/CPU.hpp @@ -240,7 +240,7 @@ private: // CPU internals uint8_t remainingCycles = 0; uint8_t additionalCycles = 0; //< E.g. when a page boundary was crossed uint64_t totalCycles = 0; - std::deque pastPCs; //< For debugging, saves the past 50 instructions + std::deque> pastInstructions; //< For debugging, saves the past 50 instructions bool halted = false; #ifndef NDEBUG diff --git a/src/debugger/Disassembler.cpp b/src/debugger/Disassembler.cpp index 7df3a71..9f8ed49 100644 --- a/src/debugger/Disassembler.cpp +++ b/src/debugger/Disassembler.cpp @@ -28,17 +28,17 @@ void Disassembler::OnRender() std::string disassembly; ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 0.8f, 0.8f, 0.8f, 1.0f }); - if (cpu->pastPCs.size() < 50) + if (cpu->pastInstructions.size() < 50) { - for (int i = 0; i < 50 - cpu->pastPCs.size(); i++) + for (int i = 0; i < 50 - cpu->pastInstructions.size(); i++) { ImGui::Text("-"); } } - for (Word pc : cpu->pastPCs) + for (auto pair : cpu->pastInstructions) { - Disassemble(disassembly, pc); + Disassemble(disassembly, pair.first, pair.second); ImGui::Text("- %s", disassembly.c_str()); } ImGui::PopStyleColor(); @@ -70,18 +70,24 @@ void Disassembler::OnRender() void Disassembler::Disassemble(std::string& target, uint16_t& pc) { - std::stringstream ss; - ss << FORMAT << pc << ": "; Instruction* currentInstr = &cpu->InstructionTable[cpu->Read(pc)]; + Disassemble(target, pc, currentInstr); + pc += currentInstr->Size; +} - for (int i = 0; i < currentInstr->Size; i++) +void Disassembler::Disassemble(std::string& target, uint16_t pc, const Instruction* instr) +{ + std::stringstream ss; + ss << FORMAT << pc << ": "; + + for (int i = 0; i < instr->Size; i++) { ss << FORMAT << std::setw(2) << (Word)cpu->Read(pc + i) << " "; } - ss << std::string(15 - ss.str().size(), ' ') << currentInstr->Mnemonic << " "; + ss << std::string(15 - ss.str().size(), ' ') << instr->Mnemonic << " "; Address absoluteAddress; - switch (currentInstr->AddrType) + switch (instr->AddrType) { case Addressing::ACC: ss << "A"; @@ -118,7 +124,7 @@ void Disassembler::Disassemble(std::string& target, uint16_t& pc) ss << "#$" << FORMAT << std::setw(2) << value; } break; - case Addressing::IMP: + case Addressing::IMP: break; case Addressing::IND: @@ -172,6 +178,5 @@ void Disassembler::Disassemble(std::string& target, uint16_t& pc) } break; } - pc += currentInstr->Size; target = ss.str(); } diff --git a/src/debugger/Disassembler.hpp b/src/debugger/Disassembler.hpp index 68dc21f..e0b9a5d 100644 --- a/src/debugger/Disassembler.hpp +++ b/src/debugger/Disassembler.hpp @@ -3,6 +3,7 @@ #include "DebugWindow.hpp" class CPU; +struct Instruction; class Disassembler : public DebugWindow @@ -14,6 +15,7 @@ public: private: void Disassemble(std::string& target, uint16_t& pc); + void Disassemble(std::string& target, uint16_t pc, const Instruction* instr); private: CPU* cpu;