mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-07-08 16:18:41 +12:00
Qt: Initial CPU debugger widget implementation
Co-Authored-By: liuk707 <62625900+liuk7071@users.noreply.github.com>
This commit is contained in:
parent
8e20bd6220
commit
9dc52577ea
8 changed files with 311 additions and 6 deletions
232
src/panda_qt/cpu_debugger.cpp
Normal file
232
src/panda_qt/cpu_debugger.cpp
Normal file
|
@ -0,0 +1,232 @@
|
|||
#include "panda_qt/cpu_debugger.hpp"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <QGridLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QListWidget>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QPushButton>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
|
||||
#include "capstone.hpp"
|
||||
|
||||
static int getLinesInViewport(QListWidget* listWidget) {
|
||||
auto viewportHeight = listWidget->viewport()->height();
|
||||
QFontMetrics fm = QFontMetrics(listWidget->font());
|
||||
auto lineHeight = fm.height();
|
||||
|
||||
return int(viewportHeight / lineHeight);
|
||||
}
|
||||
|
||||
static std::pair<int, int> getVisibleLineRange(QListWidget* listWidget, QScrollBar* scrollBar) {
|
||||
int firstLine = scrollBar->value();
|
||||
int lineCount = getLinesInViewport(listWidget);
|
||||
|
||||
return {firstLine, lineCount};
|
||||
}
|
||||
|
||||
CPUDebugger::CPUDebugger(Emulator* emulator, QWidget* parent) : emu(emulator), QWidget(parent, Qt::Window) {
|
||||
setWindowTitle(tr("CPU debugger"));
|
||||
resize(1000, 600);
|
||||
|
||||
// Main grid layout
|
||||
QGridLayout* gridLayout = new QGridLayout(this);
|
||||
|
||||
// Top row: buttons in a horizontal layout
|
||||
QHBoxLayout* horizontalLayout = new QHBoxLayout();
|
||||
QPushButton* stepButton = new QPushButton(tr("Step"), this);
|
||||
QPushButton* goToAddressButton = new QPushButton(tr("Go to address"), this);
|
||||
QPushButton* goToPCButton = new QPushButton(tr("Go to PC"), this);
|
||||
|
||||
horizontalLayout->addWidget(stepButton);
|
||||
horizontalLayout->addWidget(goToAddressButton);
|
||||
horizontalLayout->addWidget(goToPCButton);
|
||||
gridLayout->addLayout(horizontalLayout, 0, 0);
|
||||
|
||||
// Disassembly list on the left
|
||||
disasmListWidget = new QListWidget(this);
|
||||
gridLayout->addWidget(disasmListWidget, 1, 0);
|
||||
|
||||
// Vertical scroll bar in the middle
|
||||
verticalScrollBar = new QScrollBar(Qt::Vertical, this);
|
||||
gridLayout->addWidget(verticalScrollBar, 1, 1);
|
||||
|
||||
// Register view on the right
|
||||
registerTextEdit = new QPlainTextEdit(this);
|
||||
registerTextEdit->setEnabled(true);
|
||||
registerTextEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
registerTextEdit->setMaximumWidth(800);
|
||||
gridLayout->addWidget(registerTextEdit, 1, 2);
|
||||
|
||||
// Setup disabled widget overlay
|
||||
disabledOverlay = new DisabledWidgetOverlay(this, tr("Pause the emulator to use the CPU Debugger"));
|
||||
disabledOverlay->resize(size()); // Fill the whole screen
|
||||
disabledOverlay->raise();
|
||||
disabledOverlay->hide();
|
||||
|
||||
// Monospace font
|
||||
QFont mono_font = QFont("Courier New");
|
||||
mono_font.setStyleHint(QFont::Monospace);
|
||||
disasmListWidget->setFont(mono_font);
|
||||
registerTextEdit->setFont(mono_font);
|
||||
|
||||
// To forward scrolling from the list widget to the external scrollbar
|
||||
disasmListWidget->installEventFilter(this);
|
||||
|
||||
// Setup scroll bar
|
||||
verticalScrollBar->setRange(0, INT32_MAX);
|
||||
verticalScrollBar->setSingleStep(8);
|
||||
verticalScrollBar->setPageStep(getLinesInViewport(disasmListWidget));
|
||||
verticalScrollBar->show();
|
||||
connect(verticalScrollBar, &QScrollBar::valueChanged, this, &CPUDebugger::updateDisasm);
|
||||
registerTextEdit->setReadOnly(true);
|
||||
|
||||
connect(goToPCButton, &QPushButton::clicked, this, [&]() {
|
||||
u32 pc = emu->getCPU().getReg(15);
|
||||
verticalScrollBar->setValue(pc);
|
||||
});
|
||||
|
||||
disable();
|
||||
hide();
|
||||
}
|
||||
|
||||
void CPUDebugger::enable() {
|
||||
enabled = true;
|
||||
auto pc = emu->getCPU().getReg(15);
|
||||
|
||||
disabledOverlay->hide();
|
||||
verticalScrollBar->setValue(pc);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void CPUDebugger::disable() {
|
||||
enabled = false;
|
||||
|
||||
disabledOverlay->show();
|
||||
}
|
||||
|
||||
void CPUDebugger::update() {
|
||||
if (enabled) {
|
||||
updateDisasm();
|
||||
updateRegisters();
|
||||
}
|
||||
}
|
||||
|
||||
void CPUDebugger::updateDisasm() {
|
||||
int currentRow = disasmListWidget->currentRow();
|
||||
disasmListWidget->clear();
|
||||
|
||||
auto [firstLine, lineCount] = getVisibleLineRange(disasmListWidget, verticalScrollBar);
|
||||
const u32 startPC = (firstLine + 3) & ~3; // Align PC to 4 bytes
|
||||
const u32 endPC = startPC + lineCount * sizeof(u32);
|
||||
|
||||
auto& cpu = emu->getCPU();
|
||||
auto& mem = emu->getMemory();
|
||||
u32 pc = cpu.getReg(15);
|
||||
|
||||
Common::CapstoneDisassembler disassembler(CS_ARCH_ARM, CS_MODE_ARM);
|
||||
std::string disassembly;
|
||||
|
||||
for (u32 addr = startPC; addr < endPC; addr += sizeof(u32)) {
|
||||
if (auto pointer = (u32*)mem.getReadPointer(addr)) {
|
||||
const u32 instruction = *pointer;
|
||||
|
||||
// Convert instruction to byte array to pass to Capstone
|
||||
const std::array<u8, 4> bytes = {
|
||||
u8(instruction & 0xff),
|
||||
u8((instruction >> 8) & 0xff),
|
||||
u8((instruction >> 16) & 0xff),
|
||||
u8((instruction >> 24) & 0xff),
|
||||
};
|
||||
|
||||
disassembler.disassemble(disassembly, pc, std::span(bytes));
|
||||
disassembly = fmt::format("{:08X} | {}", addr, disassembly);
|
||||
|
||||
QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(disassembly));
|
||||
if (addr == pc) {
|
||||
item->setBackground(Qt::darkGreen);
|
||||
}
|
||||
disasmListWidget->addItem(item);
|
||||
} else
|
||||
disasmListWidget->addItem(QString::fromStdString(fmt::format("{:08X} | ???", addr)));
|
||||
}
|
||||
|
||||
disasmListWidget->setCurrentRow(currentRow);
|
||||
}
|
||||
|
||||
void CPUDebugger::updateRegisters() {
|
||||
auto& cpu = emu->getCPU();
|
||||
const std::span<u32, 16> gprs = cpu.regs();
|
||||
const std::span<u32, 32> fprs = cpu.fprs();
|
||||
const u32 pc = gprs[15];
|
||||
const u32 cpsr = cpu.getCPSR();
|
||||
const u32 fpscr = cpu.getFPSCR();
|
||||
|
||||
std::string text = "";
|
||||
text.reserve(2048);
|
||||
|
||||
text += fmt::format("PC: {:08X}\nCPSR: {:08X}\nFPSCR: {:08X}\n", pc, cpsr, fpscr);
|
||||
|
||||
text += "\nGeneral Purpose Registers\n";
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text += fmt::format("r{:01d}: 0x{:08X}\n", i, gprs[i]);
|
||||
}
|
||||
for (int i = 10; i < 16; i++) {
|
||||
text += fmt::format("r{:02d}: 0x{:08X}\n", i, gprs[i]);
|
||||
}
|
||||
|
||||
text += "\nFloating Point Registers\n";
|
||||
for (int i = 0; i < 10; i++) {
|
||||
text += fmt::format("f{:01d}: {:f}\n", i, Helpers::bit_cast<float, u32>(fprs[i]));
|
||||
}
|
||||
for (int i = 10; i < 32; i++) {
|
||||
text += fmt::format("f{:01d}: {:f}\n", i, Helpers::bit_cast<float, u32>(fprs[i]));
|
||||
}
|
||||
|
||||
registerTextEdit->setPlainText(QString::fromStdString(text));
|
||||
}
|
||||
|
||||
bool CPUDebugger::eventFilter(QObject* obj, QEvent* event) {
|
||||
// Forward scroll events from the list widget to the scrollbar
|
||||
if (obj == disasmListWidget && event->type() == QEvent::Wheel) {
|
||||
QWheelEvent* wheelEvent = (QWheelEvent*)event;
|
||||
|
||||
int wheelSteps = wheelEvent->angleDelta().y() / 60;
|
||||
int newScrollValue = verticalScrollBar->value() - wheelSteps;
|
||||
newScrollValue = qBound(verticalScrollBar->minimum(), newScrollValue, verticalScrollBar->maximum());
|
||||
verticalScrollBar->setValue(newScrollValue);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(obj, event);
|
||||
}
|
||||
|
||||
void CPUDebugger::showEvent(QShowEvent* event) {
|
||||
QWidget::showEvent(event);
|
||||
|
||||
enable();
|
||||
}
|
||||
|
||||
void CPUDebugger::keyPressEvent(QKeyEvent* event) {
|
||||
constexpr usize instructionSize = sizeof(u32);
|
||||
|
||||
if (event->key() == Qt::Key_Up) {
|
||||
verticalScrollBar->setValue(verticalScrollBar->value() - instructionSize);
|
||||
} else if (event->key() == Qt::Key_Down) {
|
||||
verticalScrollBar->setValue(verticalScrollBar->value() + instructionSize);
|
||||
} else {
|
||||
QWidget::keyPressEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void CPUDebugger::resizeEvent(QResizeEvent* event) {
|
||||
QWidget::resizeEvent(event);
|
||||
disabledOverlay->resize(event->size());
|
||||
verticalScrollBar->setPageStep(getLinesInViewport(disasmListWidget));
|
||||
|
||||
update();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue