-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
debugger_list.cpp
198 lines (163 loc) · 4.85 KB
/
debugger_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "debugger_list.h"
#include "gui_settings.h"
#include "breakpoint_handler.h"
#include "Emu/Cell/SPUThread.h"
#include "Emu/Cell/PPUThread.h"
#include "Emu/CPU/CPUDisAsm.h"
#include "Emu/CPU/CPUThread.h"
#include "Emu/System.h"
#include <QMouseEvent>
#include <QWheelEvent>
#include <memory>
constexpr auto qstr = QString::fromStdString;
debugger_list::debugger_list(QWidget* parent, std::shared_ptr<gui_settings> settings, breakpoint_handler* handler)
: QListWidget(parent)
, xgui_settings(settings)
, m_breakpoint_handler(handler)
{
setWindowTitle(tr("ASM"));
for (uint i = 0; i < m_item_count; ++i)
{
insertItem(i, new QListWidgetItem(""));
}
setSizeAdjustPolicy(QListWidget::AdjustToContents);
}
void debugger_list::UpdateCPUData(std::weak_ptr<cpu_thread> cpu, std::shared_ptr<CPUDisAsm> disasm)
{
this->cpu = cpu;
m_disasm = disasm;
}
u32 debugger_list::GetPc() const
{
const auto cpu = this->cpu.lock();
if (!cpu)
{
return 0;
}
return cpu->id_type() == 1 ? static_cast<ppu_thread*>(cpu.get())->cia : static_cast<spu_thread*>(cpu.get())->pc;
}
u32 debugger_list::GetCenteredAddress(u32 address) const
{
return address - ((m_item_count / 2) * 4);
}
void debugger_list::ShowAddress(u32 addr, bool force)
{
auto IsBreakpoint = [this](u32 pc)
{
return m_breakpoint_handler->HasBreakpoint(pc);
};
const bool center_pc = xgui_settings->GetValue(gui::d_centerPC).toBool();
// How many spaces addr can move down without us needing to move the entire view
const u32 addr_margin = (m_item_count / (center_pc ? 2 : 1) - 4); // 4 is just a buffer of 4 spaces at the bottom
if (force || addr - m_pc > addr_margin * 4) // 4 is the number of bytes in each instruction
{
if (center_pc)
{
m_pc = GetCenteredAddress(addr);
}
else
{
m_pc = addr;
}
}
const auto cpu = this->cpu.lock();
if (!cpu)
{
u32 pc = m_pc;
for (uint i = 0; i < m_item_count; ++i, pc += 4)
{
item(i)->setText(qstr(fmt::format(" [%08x] illegal address", pc)));
}
}
else
{
const bool is_spu = cpu->id_type() != 1;
const u32 cpu_offset = is_spu ? static_cast<spu_thread&>(*cpu).offset : 0;
const u32 address_limits = (is_spu ? 0x3fffc : ~3);
m_pc &= address_limits;
m_disasm->offset = vm::get_super_ptr(cpu_offset);
u32 pc = m_pc;
for (uint i = 0, count = 4; i<m_item_count; ++i, pc = (pc + count) & address_limits)
{
if (!vm::check_addr(cpu_offset + pc, 4))
{
item(i)->setText((IsBreakpoint(pc) ? ">> " : " ") + qstr(fmt::format("[%08x] illegal address", pc)));
count = 4;
continue;
}
count = m_disasm->disasm(m_disasm->dump_pc = pc);
item(i)->setText((IsBreakpoint(pc) ? ">> " : " ") + qstr(m_disasm->last_opcode));
if (cpu->is_paused() && pc == GetPc())
{
item(i)->setForeground(m_text_color_pc);
item(i)->setBackground(m_color_pc);
}
else if (IsBreakpoint(pc))
{
item(i)->setForeground(m_text_color_bp);
item(i)->setBackground(m_color_bp);
}
else
{
item(i)->setForeground(palette().color(foregroundRole()));
item(i)->setBackground(palette().color(backgroundRole()));
}
}
}
setLineWidth(-1);
}
void debugger_list::keyPressEvent(QKeyEvent* event)
{
if (!isActiveWindow() || currentRow() < 0 || !this->cpu.lock())
{
return;
}
switch (event->key())
{
case Qt::Key_PageUp: ShowAddress(m_pc - (m_item_count * 2) * 4); return;
case Qt::Key_PageDown: ShowAddress(m_pc); return;
case Qt::Key_Up: ShowAddress(m_pc - (m_item_count + 1) * 4); return;
case Qt::Key_Down: ShowAddress(m_pc - (m_item_count - 1) * 4); return;
default: break;
}
}
void debugger_list::mouseDoubleClickEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton && !Emu.IsStopped() && !m_no_thread_selected)
{
int i = currentRow();
if (i < 0) return;
const u32 pc = m_pc + i * 4;
// Let debugger_frame know about breakpoint.
// Other option is to add to breakpoint manager directly and have a signal there instead.
// Either the flow goes from debugger_list->breakpoint_manager->debugger_frame, or it goes debugger_list->debugger_frame, and I felt this was easier to read for now.
Q_EMIT BreakpointRequested(pc);
}
}
void debugger_list::wheelEvent(QWheelEvent* event)
{
const QPoint numSteps = event->angleDelta() / 8 / 15; // http://doc.qt.io/qt-5/qwheelevent.html#pixelDelta
const int value = numSteps.y();
const auto direction = (event->modifiers() == Qt::ControlModifier);
ShowAddress(m_pc + (direction ? value : -value) * 4, true);
}
void debugger_list::resizeEvent(QResizeEvent* event)
{
Q_UNUSED(event);
if (count() < 1 || visualItemRect(item(0)).height() < 1)
{
return;
}
m_item_count = (rect().height() - frameWidth() * 2) / visualItemRect(item(0)).height();
clear();
for (u32 i = 0; i < m_item_count; ++i)
{
insertItem(i, new QListWidgetItem(""));
}
if (horizontalScrollBar())
{
m_item_count--;
delete item(m_item_count);
}
ShowAddress(m_pc);
}