2018-03-16 05:39:53 -06:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-06 16:40:15 -06:00
|
|
|
#include "DolphinQt/Debugger/MemoryViewWidget.h"
|
2018-03-16 05:39:53 -06:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QScrollBar>
|
2018-05-17 15:09:55 -06:00
|
|
|
|
|
|
|
#include <cctype>
|
2018-03-16 05:39:53 -06:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "Core/Core.h"
|
2019-07-15 21:20:26 -06:00
|
|
|
#include "Core/HW/AddressSpace.h"
|
2018-03-16 05:39:53 -06:00
|
|
|
#include "Core/PowerPC/BreakPoints.h"
|
|
|
|
#include "Core/PowerPC/PowerPC.h"
|
2018-07-06 16:40:15 -06:00
|
|
|
#include "DolphinQt/Resources.h"
|
|
|
|
#include "DolphinQt/Settings.h"
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2018-08-19 05:29:52 -06:00
|
|
|
// "Most mouse types work in steps of 15 degrees, in which case the delta value is a multiple of
|
|
|
|
// 120; i.e., 120 units * 1/8 = 15 degrees." (http://doc.qt.io/qt-5/qwheelevent.html#angleDelta)
|
|
|
|
constexpr double SCROLL_FRACTION_DEGREES = 15.;
|
|
|
|
|
2018-03-16 05:39:53 -06:00
|
|
|
MemoryViewWidget::MemoryViewWidget(QWidget* parent) : QTableWidget(parent)
|
|
|
|
{
|
|
|
|
horizontalHeader()->hide();
|
|
|
|
verticalHeader()->hide();
|
2018-08-02 13:50:23 -06:00
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2018-03-16 05:39:53 -06:00
|
|
|
setShowGrid(false);
|
|
|
|
|
|
|
|
setFont(Settings::Instance().GetDebugFont());
|
|
|
|
|
|
|
|
connect(&Settings::Instance(), &Settings::DebugFontChanged, this, &QWidget::setFont);
|
|
|
|
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this] { Update(); });
|
|
|
|
connect(this, &MemoryViewWidget::customContextMenuRequested, this,
|
|
|
|
&MemoryViewWidget::OnContextMenu);
|
2018-04-11 15:43:47 -06:00
|
|
|
connect(&Settings::Instance(), &Settings::ThemeChanged, this, &MemoryViewWidget::Update);
|
2018-03-16 05:39:53 -06:00
|
|
|
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
2018-04-16 14:30:37 -06:00
|
|
|
static int GetColumnCount(MemoryViewWidget::Type type)
|
2018-03-16 05:39:53 -06:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case MemoryViewWidget::Type::ASCII:
|
|
|
|
case MemoryViewWidget::Type::U8:
|
2018-04-16 14:30:37 -06:00
|
|
|
return 16;
|
2018-03-16 05:39:53 -06:00
|
|
|
case MemoryViewWidget::Type::U16:
|
2018-04-16 14:30:37 -06:00
|
|
|
return 8;
|
2018-03-16 05:39:53 -06:00
|
|
|
case MemoryViewWidget::Type::U32:
|
|
|
|
case MemoryViewWidget::Type::Float32:
|
2018-04-16 14:30:37 -06:00
|
|
|
return 4;
|
|
|
|
default:
|
|
|
|
return 0;
|
2018-03-16 05:39:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::Update()
|
|
|
|
{
|
|
|
|
clearSelection();
|
|
|
|
|
2018-04-16 14:30:37 -06:00
|
|
|
setColumnCount(3 + GetColumnCount(m_type));
|
2018-03-16 05:39:53 -06:00
|
|
|
|
|
|
|
if (rowCount() == 0)
|
|
|
|
setRowCount(1);
|
|
|
|
|
|
|
|
setRowHeight(0, 24);
|
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
const AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_address_space);
|
|
|
|
|
2018-03-16 05:39:53 -06:00
|
|
|
// Calculate (roughly) how many rows will fit in our table
|
|
|
|
int rows = std::round((height() / static_cast<float>(rowHeight(0))) - 0.25);
|
|
|
|
|
|
|
|
setRowCount(rows);
|
|
|
|
|
|
|
|
for (int i = 0; i < rows; i++)
|
|
|
|
{
|
|
|
|
setRowHeight(i, 24);
|
|
|
|
|
|
|
|
u32 addr = m_address - ((rowCount() / 2) * 16) + i * 16;
|
|
|
|
|
|
|
|
auto* bp_item = new QTableWidgetItem;
|
|
|
|
bp_item->setFlags(Qt::ItemIsEnabled);
|
|
|
|
bp_item->setData(Qt::UserRole, addr);
|
|
|
|
|
|
|
|
setItem(i, 0, bp_item);
|
|
|
|
|
|
|
|
auto* addr_item = new QTableWidgetItem(QStringLiteral("%1").arg(addr, 8, 16, QLatin1Char('0')));
|
|
|
|
|
|
|
|
addr_item->setData(Qt::UserRole, addr);
|
|
|
|
addr_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
|
|
|
|
|
|
|
setItem(i, 1, addr_item);
|
|
|
|
|
|
|
|
if (addr == m_address)
|
|
|
|
addr_item->setSelected(true);
|
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
if (Core::GetState() != Core::State::Paused || !accessors->IsValidAddress(addr))
|
2018-03-16 05:39:53 -06:00
|
|
|
{
|
|
|
|
for (int c = 2; c < columnCount(); c++)
|
|
|
|
{
|
|
|
|
auto* item = new QTableWidgetItem(QStringLiteral("-"));
|
|
|
|
item->setFlags(Qt::ItemIsEnabled);
|
|
|
|
item->setData(Qt::UserRole, addr);
|
|
|
|
|
|
|
|
setItem(i, c, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
if (m_address_space == AddressSpace::Type::Effective)
|
|
|
|
{
|
|
|
|
auto* description_item = new QTableWidgetItem(
|
|
|
|
QString::fromStdString(PowerPC::debug_interface.GetDescription(addr)));
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
description_item->setForeground(Qt::blue);
|
|
|
|
description_item->setFlags(Qt::ItemIsEnabled);
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
setItem(i, columnCount() - 1, description_item);
|
|
|
|
}
|
2018-05-31 14:07:05 -06:00
|
|
|
bool row_breakpoint = true;
|
|
|
|
|
|
|
|
auto update_values = [&](auto value_to_string) {
|
2018-04-16 14:30:37 -06:00
|
|
|
for (int c = 0; c < GetColumnCount(m_type); c++)
|
2018-03-16 05:39:53 -06:00
|
|
|
{
|
|
|
|
auto* hex_item = new QTableWidgetItem;
|
2018-05-31 14:07:05 -06:00
|
|
|
hex_item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
2018-04-16 14:30:37 -06:00
|
|
|
const u32 address = addr + c * (16 / GetColumnCount(m_type));
|
2018-05-31 14:07:05 -06:00
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
if (m_address_space == AddressSpace::Type::Effective &&
|
|
|
|
PowerPC::memchecks.OverlapsMemcheck(address, 16 / GetColumnCount(m_type)))
|
|
|
|
{
|
2018-05-31 14:07:05 -06:00
|
|
|
hex_item->setBackground(Qt::red);
|
2019-04-10 23:50:52 -06:00
|
|
|
}
|
2018-05-31 14:07:05 -06:00
|
|
|
else
|
2019-04-10 23:50:52 -06:00
|
|
|
{
|
2018-05-31 14:07:05 -06:00
|
|
|
row_breakpoint = false;
|
2019-04-10 23:50:52 -06:00
|
|
|
}
|
2018-05-31 14:07:05 -06:00
|
|
|
setItem(i, 2 + c, hex_item);
|
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
if (accessors->IsValidAddress(address))
|
2018-03-16 05:39:53 -06:00
|
|
|
{
|
2018-04-16 14:30:37 -06:00
|
|
|
hex_item->setText(value_to_string(address));
|
2018-05-31 14:07:05 -06:00
|
|
|
hex_item->setData(Qt::UserRole, address);
|
2018-03-16 05:39:53 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hex_item->setFlags(0);
|
|
|
|
hex_item->setText(QStringLiteral("-"));
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 14:30:37 -06:00
|
|
|
};
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case Type::U8:
|
2019-04-10 23:50:52 -06:00
|
|
|
update_values([&accessors](u32 address) {
|
|
|
|
const u8 value = accessors->ReadU8(address);
|
2018-04-16 14:30:37 -06:00
|
|
|
return QStringLiteral("%1").arg(value, 2, 16, QLatin1Char('0'));
|
|
|
|
});
|
2018-03-16 05:39:53 -06:00
|
|
|
break;
|
|
|
|
case Type::ASCII:
|
2019-04-10 23:50:52 -06:00
|
|
|
update_values([&accessors](u32 address) {
|
|
|
|
const char value = accessors->ReadU8(address);
|
2019-07-30 05:57:06 -06:00
|
|
|
return std::isprint(value) ? QString{QChar::fromLatin1(value)} :
|
|
|
|
QString{QChar::fromLatin1('.')};
|
2018-04-16 14:30:37 -06:00
|
|
|
});
|
2018-03-16 05:39:53 -06:00
|
|
|
break;
|
|
|
|
case Type::U16:
|
2019-04-10 23:50:52 -06:00
|
|
|
update_values([&accessors](u32 address) {
|
|
|
|
const u16 value = accessors->ReadU16(address);
|
2018-04-16 14:30:37 -06:00
|
|
|
return QStringLiteral("%1").arg(value, 4, 16, QLatin1Char('0'));
|
|
|
|
});
|
2018-03-16 05:39:53 -06:00
|
|
|
break;
|
|
|
|
case Type::U32:
|
2019-04-10 23:50:52 -06:00
|
|
|
update_values([&accessors](u32 address) {
|
|
|
|
const u32 value = accessors->ReadU32(address);
|
2018-04-16 14:30:37 -06:00
|
|
|
return QStringLiteral("%1").arg(value, 8, 16, QLatin1Char('0'));
|
|
|
|
});
|
2018-03-16 05:39:53 -06:00
|
|
|
break;
|
|
|
|
case Type::Float32:
|
2019-04-10 23:50:52 -06:00
|
|
|
update_values(
|
|
|
|
[&accessors](u32 address) { return QString::number(accessors->ReadF32(address)); });
|
2018-03-16 05:39:53 -06:00
|
|
|
break;
|
|
|
|
}
|
2018-05-31 14:07:05 -06:00
|
|
|
|
|
|
|
if (row_breakpoint)
|
|
|
|
{
|
|
|
|
bp_item->setData(Qt::DecorationRole,
|
|
|
|
Resources::GetScaledThemeIcon("debugger_breakpoint").pixmap(QSize(24, 24)));
|
|
|
|
}
|
2018-03-16 05:39:53 -06:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:43:47 -06:00
|
|
|
setColumnWidth(0, 24 + 5);
|
2018-03-16 05:39:53 -06:00
|
|
|
for (int i = 1; i < columnCount(); i++)
|
|
|
|
{
|
|
|
|
resizeColumnToContents(i);
|
|
|
|
// Add some extra spacing because the default width is too small in most cases
|
|
|
|
setColumnWidth(i, columnWidth(i) * 1.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
viewport()->update();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
void MemoryViewWidget::SetAddressSpace(AddressSpace::Type address_space)
|
|
|
|
{
|
|
|
|
if (m_address_space == address_space)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_address_space = address_space;
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddressSpace::Type MemoryViewWidget::GetAddressSpace() const
|
|
|
|
{
|
|
|
|
return m_address_space;
|
|
|
|
}
|
|
|
|
|
2018-03-16 05:39:53 -06:00
|
|
|
void MemoryViewWidget::SetType(Type type)
|
|
|
|
{
|
|
|
|
if (m_type == type)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_type = type;
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::SetBPType(BPType type)
|
|
|
|
{
|
|
|
|
m_bp_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::SetAddress(u32 address)
|
|
|
|
{
|
|
|
|
if (m_address == address)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_address = address;
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::SetBPLoggingEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
m_do_log = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::resizeEvent(QResizeEvent*)
|
|
|
|
{
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
switch (event->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Up:
|
2018-08-19 05:29:52 -06:00
|
|
|
m_address -= 16;
|
2018-03-16 05:39:53 -06:00
|
|
|
Update();
|
|
|
|
return;
|
|
|
|
case Qt::Key_Down:
|
2018-08-19 05:29:52 -06:00
|
|
|
m_address += 16;
|
2018-03-16 05:39:53 -06:00
|
|
|
Update();
|
|
|
|
return;
|
|
|
|
case Qt::Key_PageUp:
|
|
|
|
m_address -= rowCount() * 16;
|
|
|
|
Update();
|
|
|
|
return;
|
|
|
|
case Qt::Key_PageDown:
|
|
|
|
m_address += rowCount() * 16;
|
|
|
|
Update();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
QWidget::keyPressEvent(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 MemoryViewWidget::GetContextAddress() const
|
|
|
|
{
|
|
|
|
return m_context_address;
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:07:05 -06:00
|
|
|
void MemoryViewWidget::ToggleRowBreakpoint(bool row)
|
2018-03-16 05:39:53 -06:00
|
|
|
{
|
2018-05-31 14:07:05 -06:00
|
|
|
TMemCheck check;
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2018-05-31 14:07:05 -06:00
|
|
|
const u32 addr = row ? GetContextAddress() & 0xFFFFFFF0 : GetContextAddress();
|
|
|
|
const auto length = row ? 16 : (16 / GetColumnCount(m_type));
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
if (m_address_space == AddressSpace::Type::Effective)
|
2018-05-31 14:07:05 -06:00
|
|
|
{
|
2019-04-10 23:50:52 -06:00
|
|
|
if (!PowerPC::memchecks.OverlapsMemcheck(addr, length))
|
|
|
|
{
|
|
|
|
check.start_address = addr;
|
|
|
|
check.end_address = check.start_address + length - 1;
|
|
|
|
check.is_ranged = length > 0;
|
|
|
|
check.is_break_on_read = (m_bp_type == BPType::ReadOnly || m_bp_type == BPType::ReadWrite);
|
|
|
|
check.is_break_on_write = (m_bp_type == BPType::WriteOnly || m_bp_type == BPType::ReadWrite);
|
|
|
|
check.log_on_hit = m_do_log;
|
|
|
|
check.break_on_hit = true;
|
|
|
|
|
|
|
|
PowerPC::memchecks.Add(check);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PowerPC::memchecks.Remove(addr);
|
|
|
|
}
|
2018-03-16 05:39:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
emit BreakpointsChanged();
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:07:05 -06:00
|
|
|
void MemoryViewWidget::ToggleBreakpoint()
|
|
|
|
{
|
|
|
|
ToggleRowBreakpoint(false);
|
|
|
|
}
|
|
|
|
|
2018-03-16 05:39:53 -06:00
|
|
|
void MemoryViewWidget::wheelEvent(QWheelEvent* event)
|
|
|
|
{
|
2018-08-19 05:29:52 -06:00
|
|
|
auto delta =
|
|
|
|
-static_cast<int>(std::round((event->angleDelta().y() / (SCROLL_FRACTION_DEGREES * 8))));
|
|
|
|
|
|
|
|
if (delta == 0)
|
|
|
|
return;
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2018-08-19 05:29:52 -06:00
|
|
|
m_address += delta * 16;
|
2018-03-16 05:39:53 -06:00
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
auto* item = itemAt(event->pos());
|
|
|
|
if (item == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const u32 addr = item->data(Qt::UserRole).toUInt();
|
|
|
|
|
|
|
|
m_context_address = addr;
|
|
|
|
|
|
|
|
switch (event->button())
|
|
|
|
{
|
|
|
|
case Qt::LeftButton:
|
|
|
|
if (column(item) == 0)
|
2018-05-31 14:07:05 -06:00
|
|
|
ToggleRowBreakpoint(true);
|
2018-03-16 05:39:53 -06:00
|
|
|
else
|
2018-05-31 14:07:05 -06:00
|
|
|
SetAddress(addr & 0xFFFFFFF0);
|
2018-03-16 05:39:53 -06:00
|
|
|
|
|
|
|
Update();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::OnCopyAddress()
|
|
|
|
{
|
|
|
|
u32 addr = GetContextAddress();
|
|
|
|
QApplication::clipboard()->setText(QStringLiteral("%1").arg(addr, 8, 16, QLatin1Char('0')));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::OnCopyHex()
|
|
|
|
{
|
|
|
|
u32 addr = GetContextAddress();
|
|
|
|
|
2018-05-31 14:07:05 -06:00
|
|
|
const auto length = 16 / GetColumnCount(m_type);
|
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
const AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_address_space);
|
|
|
|
u64 value = accessors->ReadU64(addr);
|
2018-03-16 05:39:53 -06:00
|
|
|
|
|
|
|
QApplication::clipboard()->setText(
|
2019-11-19 11:47:00 -07:00
|
|
|
QStringLiteral("%1").arg(value, sizeof(u64) * 2, 16, QLatin1Char('0')).left(length * 2));
|
2018-03-16 05:39:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryViewWidget::OnContextMenu()
|
|
|
|
{
|
|
|
|
auto* menu = new QMenu(this);
|
|
|
|
|
2018-07-09 02:02:10 -06:00
|
|
|
menu->addAction(tr("Copy Address"), this, &MemoryViewWidget::OnCopyAddress);
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2018-07-09 02:02:10 -06:00
|
|
|
auto* copy_hex = menu->addAction(tr("Copy Hex"), this, &MemoryViewWidget::OnCopyHex);
|
2018-03-16 05:39:53 -06:00
|
|
|
|
2019-04-10 23:50:52 -06:00
|
|
|
const AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_address_space);
|
2018-03-16 05:39:53 -06:00
|
|
|
copy_hex->setEnabled(Core::GetState() != Core::State::Uninitialized &&
|
2019-04-10 23:50:52 -06:00
|
|
|
accessors->IsValidAddress(GetContextAddress()));
|
2018-03-16 05:39:53 -06:00
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
2018-12-28 11:12:30 -07:00
|
|
|
menu->addAction(tr("Show in code"), this, [this] { emit ShowCode(GetContextAddress()); });
|
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
2018-07-09 02:02:10 -06:00
|
|
|
menu->addAction(tr("Toggle Breakpoint"), this, &MemoryViewWidget::ToggleBreakpoint);
|
2018-03-16 05:39:53 -06:00
|
|
|
|
|
|
|
menu->exec(QCursor::pos());
|
|
|
|
}
|