From 80d9832fbde793be08108b4d8022684c244db1a5 Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Sun, 16 Feb 2020 22:19:13 +0100 Subject: [PATCH] Displaying CPU registers --- CMakeLists.txt | 4 +- .../CPU/Instructions/InternalInstruction.cpp | 3 + sources/Debugger/DebugCpu.cpp | 42 ++++++ sources/Debugger/DebugCpu.hpp | 4 + sources/Utility/Utility.hpp | 37 ++++++ ui/cpu.ui | 124 +++++++++++++++++- 6 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 sources/Utility/Utility.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c68f5eb..dfd570a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,7 @@ add_executable(unit_tests tests/CPU/testInternal.cpp sources/Ram/ExtendedRam.cpp sources/Ram/ExtendedRam.hpp -) + sources/Utility/Utility.hpp) # include criterion & coverage target_link_libraries(unit_tests criterion -lgcov) @@ -133,7 +133,7 @@ add_executable(ComSquare resources/appResources.qrc sources/Renderer/QtRenderer/QtWindow.cpp sources/Renderer/QtRenderer/QtWindow.hpp -) + sources/Utility/Utility.hpp) target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED) diff --git a/sources/CPU/Instructions/InternalInstruction.cpp b/sources/CPU/Instructions/InternalInstruction.cpp index 3f2db8b..e1e0555 100644 --- a/sources/CPU/Instructions/InternalInstruction.cpp +++ b/sources/CPU/Instructions/InternalInstruction.cpp @@ -71,6 +71,7 @@ namespace ComSquare::CPU void CPU::PLA() { + // TODO this register should be poped by 8 if the m flag is 1 this->_registers.a = this->_pop16(); this->_registers.p.z = this->_registers.a == 0; this->_registers.p.n = this->_registers.a & 0x8000u; @@ -101,6 +102,7 @@ namespace ComSquare::CPU void CPU::PLX() { + // TODO this register should be poped by 8 if the x_b is 1 this->_registers.x = this->_pop16(); this->_registers.p.z = this->_registers.x == 0; this->_registers.p.n = this->_registers.x & 0x8000u; @@ -108,6 +110,7 @@ namespace ComSquare::CPU void CPU::PLY() { + // TODO this register should be poped by 8 if the x_b is 1 this->_registers.y = this->_pop16(); this->_registers.p.z = this->_registers.y == 0; this->_registers.p.n = this->_registers.y & 0x8000u; diff --git a/sources/Debugger/DebugCpu.cpp b/sources/Debugger/DebugCpu.cpp index a0afc3e..e889f32 100644 --- a/sources/Debugger/DebugCpu.cpp +++ b/sources/Debugger/DebugCpu.cpp @@ -3,6 +3,7 @@ // #include "DebugCpu.hpp" +#include "../Utility/Utility.hpp" using namespace ComSquare::CPU; @@ -18,6 +19,7 @@ namespace ComSquare::Debugger QMainWindow::connect(this->_ui.actionPause, &QAction::triggered, this, &CPUDebug::pause); QMainWindow::connect(this->_ui.actionStep, &QAction::triggered, this, &CPUDebug::step); this->show(); + this->_updateRegistersPanel(); } unsigned CPUDebug::update() @@ -41,6 +43,7 @@ namespace ComSquare::Debugger this->_isPaused = true; } this->_ui.logger->append(CPUDebug::_getInstructionString(opcode).c_str()); + this->_updateRegistersPanel(); return CPU::_executeInstruction(opcode); } @@ -59,6 +62,45 @@ namespace ComSquare::Debugger this->_isPaused = false; } + void CPUDebug::_updateRegistersPanel() + { + if (!this->_registers.p.m) + this->_ui.accumulatorLineEdit->setText(Utility::to_hex(this->_registers.a).c_str()); + else + this->_ui.accumulatorLineEdit->setText(Utility::to_hex(this->_registers.al).c_str()); + this->_ui.programBankRegisterLineEdit->setText(Utility::to_hex(this->_registers.pbr).c_str()); + this->_ui.programCounterLineEdit->setText(Utility::to_hex(this->_registers.pc).c_str()); + this->_ui.directBankLineEdit->setText(Utility::to_hex(this->_registers.dbr).c_str()); + this->_ui.directPageLineEdit->setText(Utility::to_hex(this->_registers.d).c_str()); + this->_ui.stackPointerLineEdit->setText(Utility::to_hex(this->_registers.s).c_str()); + if (this->_registers.p.x_b) { + this->_ui.xIndexLineEdit->setText(Utility::to_hex(this->_registers.x).c_str()); + this->_ui.yIndexLineEdit->setText(Utility::to_hex(this->_registers.y).c_str()); + } else { + this->_ui.xIndexLineEdit->setText(Utility::to_hex(this->_registers.xl).c_str()); + this->_ui.yIndexLineEdit->setText(Utility::to_hex(this->_registers.yl).c_str()); + } + this->_ui.flagsLineEdit->setText(this->_getFlagsString().c_str()); + this->_ui.emulationModeCheckBox->setCheckState(this->_isEmulationMode ? Qt::CheckState::Checked : Qt::CheckState::Unchecked); + } + + std::string CPUDebug::_getFlagsString() + { + std::string str; + str += this->_registers.p.n ? "n" : "-"; + str += this->_registers.p.v ? "v" : "-"; + str += this->_registers.p.m ? "m" : "-"; + if (this->_isEmulationMode) + str += this->_registers.p.x_b ? "b" : "-"; + else + str += this->_registers.p.x_b ? "x" : "-"; + str += this->_registers.p.d ? "d" : "-"; + str += this->_registers.p.i ? "i" : "-"; + str += this->_registers.p.z ? "z" : "-"; + str += this->_registers.p.c ? "c" : "-"; + return str; + } + std::string CPUDebug::_getInstructionString(uint8_t opcode) { switch (opcode) { diff --git a/sources/Debugger/DebugCpu.hpp b/sources/Debugger/DebugCpu.hpp index 0506b28..7b6959a 100644 --- a/sources/Debugger/DebugCpu.hpp +++ b/sources/Debugger/DebugCpu.hpp @@ -28,6 +28,10 @@ namespace ComSquare::Debugger unsigned _executeInstruction(uint8_t opcode) override; //! @brief Get a printable string representing an instruction. static std::string _getInstructionString(uint8_t opcode); + //! @brief Get a printable string representing the flags. + std::string _getFlagsString(); + //! @brief Update the register's panel (accumulator, stack pointer...) + void _updateRegistersPanel(); public slots: //! @brief Pause/Resume the CPU. void pause(); diff --git a/sources/Utility/Utility.hpp b/sources/Utility/Utility.hpp new file mode 100644 index 0000000..f0f28df --- /dev/null +++ b/sources/Utility/Utility.hpp @@ -0,0 +1,37 @@ +// +// Created by anonymus-raccoon on 2/16/20. +// + +#ifndef COMSQUARE_UTILITY_HPP +#define COMSQUARE_UTILITY_HPP + +#include +#include +#include +#include "../Models/Int24.hpp" + +namespace ComSquare::Utility +{ + std::string to_hex(uint8_t i) + { + char buf[5]; + sprintf(buf, "0x%02X", i); + return buf; + } + + std::string to_hex(uint16_t i) + { + char buf[7]; + sprintf(buf, "0x%04X", i); + return buf; + } + + std::string to_hex(uint24_t i) + { + char buf[9]; + sprintf(buf, "0x%06X", i); + return buf; + } +} + +#endif //COMSQUARE_UTILITY_HPP diff --git a/ui/cpu.ui b/ui/cpu.ui index 2b563b3..6921550 100644 --- a/ui/cpu.ui +++ b/ui/cpu.ui @@ -7,7 +7,7 @@ 0 0 600 - 400 + 399 @@ -27,7 +27,7 @@ 0 25 200 - 375 + 305 @@ -47,6 +47,126 @@ Qt::AlignCenter + + + + 390 + 0 + 201 + 291 + + + + + + + Accumulator + + + + + + + + + + + + + + Program Bank + + + + + + + + + + Program Counter + + + + + + + + + + Direct Bank + + + + + + + + + + Direct Page + + + + + + + + + + Stack Pointer + + + + + + + + + + X Index + + + + + + + + + + Y Index + + + + + + + + + + Flags + + + + + + + + + + Emulation mode + + + + + + + Qt::RightToLeft + + + + +