summaryrefslogtreecommitdiff
path: root/src/citra_qt/debugger/disassembler.cpp
diff options
context:
space:
mode:
authorGravatar Tony Wasserka2014-07-02 21:16:36 +0200
committerGravatar Tony Wasserka2014-07-02 21:16:36 +0200
commitfd787868288521ae655ddd1ac8000895662eaa77 (patch)
treed8d1b2958ff60d0a209d0268ecfc417f7bb3f45c /src/citra_qt/debugger/disassembler.cpp
parentMerge pull request #22 from bunnei/loader-improvements (diff)
downloadyuzu-fd787868288521ae655ddd1ac8000895662eaa77.tar.gz
yuzu-fd787868288521ae655ddd1ac8000895662eaa77.tar.xz
yuzu-fd787868288521ae655ddd1ac8000895662eaa77.zip
citra-qt: Rewrite disassembly view.
Diffstat (limited to 'src/citra_qt/debugger/disassembler.cpp')
-rw-r--r--src/citra_qt/debugger/disassembler.cpp205
1 files changed, 150 insertions, 55 deletions
diff --git a/src/citra_qt/debugger/disassembler.cpp b/src/citra_qt/debugger/disassembler.cpp
index 4e8f841f4..4fd6a6e62 100644
--- a/src/citra_qt/debugger/disassembler.cpp
+++ b/src/citra_qt/debugger/disassembler.cpp
@@ -1,5 +1,3 @@
1#include <QtGui>
2
3#include "disassembler.hxx" 1#include "disassembler.hxx"
4 2
5#include "../bootmanager.hxx" 3#include "../bootmanager.hxx"
@@ -14,14 +12,143 @@
14#include "core/arm/interpreter/armdefs.h" 12#include "core/arm/interpreter/armdefs.h"
15#include "core/arm/disassembler/arm_disasm.h" 13#include "core/arm/disassembler/arm_disasm.h"
16 14
15DisassemblerModel::DisassemblerModel(QObject* parent) : QAbstractItemModel(parent), base_address(0), code_size(0), program_counter(0), selection(QModelIndex()) {
16
17}
18
19QModelIndex DisassemblerModel::index(int row, int column, const QModelIndex& parent) const {
20 return createIndex(row, column);
21}
22
23QModelIndex DisassemblerModel::parent(const QModelIndex& child) const {
24 return QModelIndex();
25}
26
27int DisassemblerModel::columnCount(const QModelIndex& parent) const {
28 return 3;
29}
30
31int DisassemblerModel::rowCount(const QModelIndex& parent) const {
32 return code_size;
33}
34
35QVariant DisassemblerModel::data(const QModelIndex& index, int role) const {
36 switch (role) {
37 case Qt::DisplayRole:
38 {
39 static char result[255];
40
41 u32 address = base_address + index.row() * 4;
42 ARM_Disasm::disasm(address, Memory::Read32(address), result);
43
44 if (index.column() == 0) {
45 return QString("0x%1").arg((uint)(address), 8, 16, QLatin1Char('0'));
46 } else if (index.column() == 1) {
47 return QString::fromLatin1(result);
48 } else if (index.column() == 2 && Symbols::HasSymbol(address)) {
49 TSymbol symbol = Symbols::GetSymbol(address);
50 return QString("%1 - Size:%2").arg(QString::fromStdString(symbol.name))
51 .arg(symbol.size / 4); // divide by 4 to get instruction count
52 }
53
54 break;
55 }
56
57 case Qt::BackgroundRole:
58 {
59 unsigned int address = base_address + 4 * index.row();
60
61 if (breakpoints.IsAddressBreakPoint(address))
62 return QBrush(QColor(0xFF, 0xC0, 0xC0));
63 else if (address == program_counter)
64 return QBrush(QColor(0xC0, 0xC0, 0xFF));
65
66 break;
67 }
68
69 default:
70 break;
71 }
72
73 return QVariant();
74}
75
76QModelIndex DisassemblerModel::IndexFromAbsoluteAddress(unsigned int address) const {
77 return index((address - base_address) / 4, 0);
78}
79
80const BreakPoints& DisassemblerModel::GetBreakPoints() const {
81 return breakpoints;
82}
83
84void DisassemblerModel::ParseFromAddress(unsigned int address) {
85 const unsigned int chunk_size = 1000*1000; // 10*1000*1000 is critical
86
87 // If we haven't loaded anything yet, initialize base address to the parameter address
88 if (code_size == 0)
89 base_address = address;
90
91 // If the new area is already loaded, just continue
92 if (base_address + code_size > address + chunk_size && base_address <= address)
93 return;
94
95 // Insert rows before currently loaded data
96 if (base_address > address) {
97 unsigned int num_rows = (address - base_address) / 4;
98
99 beginInsertRows(QModelIndex(), 0, num_rows);
100 code_size += num_rows;
101 base_address = address;
102
103 endInsertRows();
104 }
105
106 // Insert rows after currently loaded data
107 if (base_address + code_size < address + chunk_size) {
108 unsigned int num_rows = (base_address + chunk_size - code_size - address) / 4;
109
110 beginInsertRows(QModelIndex(), 0, num_rows);
111 code_size += num_rows;
112 endInsertRows();
113 }
114
115 SetNextInstruction(address);
116}
117
118void DisassemblerModel::OnSelectionChanged(const QModelIndex& new_selection) {
119 selection = new_selection;
120}
121
122void DisassemblerModel::OnSetOrUnsetBreakpoint() {
123 if (!selection.isValid())
124 return;
125
126 unsigned int address = base_address + selection.row() * 4;
127
128 if (breakpoints.IsAddressBreakPoint(address)) {
129 breakpoints.Remove(address);
130 } else {
131 breakpoints.Add(address);
132 }
133
134 emit dataChanged(selection, selection);
135}
136
137void DisassemblerModel::SetNextInstruction(unsigned int address) {
138 QModelIndex cur_index = IndexFromAbsoluteAddress(program_counter);
139 QModelIndex prev_index = IndexFromAbsoluteAddress(address);
140
141 program_counter = address;
142
143 emit dataChanged(cur_index, cur_index);
144 emit dataChanged(prev_index, prev_index);
145}
146
17DisassemblerWidget::DisassemblerWidget(QWidget* parent, EmuThread& emu_thread) : QDockWidget(parent), base_addr(0), emu_thread(emu_thread) 147DisassemblerWidget::DisassemblerWidget(QWidget* parent, EmuThread& emu_thread) : QDockWidget(parent), base_addr(0), emu_thread(emu_thread)
18{ 148{
19 disasm_ui.setupUi(this); 149 disasm_ui.setupUi(this);
20 150
21 breakpoints = new BreakPoints(); 151 model = new DisassemblerModel(this);
22
23 model = new QStandardItemModel(this);
24 model->setColumnCount(3);
25 disasm_ui.treeView->setModel(model); 152 disasm_ui.treeView->setModel(model);
26 153
27 RegisterHotkey("Disassembler", "Start/Stop", QKeySequence(Qt::Key_F5), Qt::ApplicationShortcut); 154 RegisterHotkey("Disassembler", "Start/Stop", QKeySequence(Qt::Key_F5), Qt::ApplicationShortcut);
@@ -29,70 +156,33 @@ DisassemblerWidget::DisassemblerWidget(QWidget* parent, EmuThread& emu_thread) :
29 RegisterHotkey("Disassembler", "Step into", QKeySequence(Qt::Key_F11), Qt::ApplicationShortcut); 156 RegisterHotkey("Disassembler", "Step into", QKeySequence(Qt::Key_F11), Qt::ApplicationShortcut);
30 RegisterHotkey("Disassembler", "Set Breakpoint", QKeySequence(Qt::Key_F9), Qt::ApplicationShortcut); 157 RegisterHotkey("Disassembler", "Set Breakpoint", QKeySequence(Qt::Key_F9), Qt::ApplicationShortcut);
31 158
32 connect(disasm_ui.button_breakpoint, SIGNAL(clicked()), this, SLOT(OnSetBreakpoint())); 159 connect(disasm_ui.button_breakpoint, SIGNAL(clicked()), model, SLOT(OnSetOrUnsetBreakpoint()));
33 connect(disasm_ui.button_step, SIGNAL(clicked()), this, SLOT(OnStep())); 160 connect(disasm_ui.button_step, SIGNAL(clicked()), this, SLOT(OnStep()));
34 connect(disasm_ui.button_pause, SIGNAL(clicked()), this, SLOT(OnPause())); 161 connect(disasm_ui.button_pause, SIGNAL(clicked()), this, SLOT(OnPause()));
35 connect(disasm_ui.button_continue, SIGNAL(clicked()), this, SLOT(OnContinue())); 162 connect(disasm_ui.button_continue, SIGNAL(clicked()), this, SLOT(OnContinue()));
36 163
164 connect(disasm_ui.treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
165 model, SLOT(OnSelectionChanged(const QModelIndex&)));
166
37 connect(GetHotkey("Disassembler", "Start/Stop", this), SIGNAL(activated()), this, SLOT(OnToggleStartStop())); 167 connect(GetHotkey("Disassembler", "Start/Stop", this), SIGNAL(activated()), this, SLOT(OnToggleStartStop()));
38 connect(GetHotkey("Disassembler", "Step", this), SIGNAL(activated()), this, SLOT(OnStep())); 168 connect(GetHotkey("Disassembler", "Step", this), SIGNAL(activated()), this, SLOT(OnStep()));
39 connect(GetHotkey("Disassembler", "Step into", this), SIGNAL(activated()), this, SLOT(OnStepInto())); 169 connect(GetHotkey("Disassembler", "Step into", this), SIGNAL(activated()), this, SLOT(OnStepInto()));
40 connect(GetHotkey("Disassembler", "Set Breakpoint", this), SIGNAL(activated()), this, SLOT(OnSetBreakpoint())); 170 connect(GetHotkey("Disassembler", "Set Breakpoint", this), SIGNAL(activated()), model, SLOT(OnSetOrUnsetBreakpoint()));
41} 171}
42 172
43void DisassemblerWidget::Init() 173void DisassemblerWidget::Init()
44{ 174{
45 ARM_Disasm* disasm = new ARM_Disasm(); 175 model->ParseFromAddress(Core::g_app_core->GetPC());
46 176
47 base_addr = Core::g_app_core->GetPC();
48 unsigned int curInstAddr = base_addr;
49 char result[255];
50
51 for (int i = 0; i < 20000; i++) // fixed for now
52 {
53 disasm->disasm(curInstAddr, Memory::Read32(curInstAddr), result);
54 model->setItem(i, 0, new QStandardItem(QString("0x%1").arg((uint)(curInstAddr), 8, 16, QLatin1Char('0'))));
55 model->setItem(i, 1, new QStandardItem(QString(result)));
56 if (Symbols::HasSymbol(curInstAddr))
57 {
58 TSymbol symbol = Symbols::GetSymbol(curInstAddr);
59 model->setItem(i, 2, new QStandardItem(QString("%1 - Size:%2").arg(QString::fromStdString(symbol.name))
60 .arg(symbol.size / 4))); // divide by 4 to get instruction count
61
62 }
63 curInstAddr += 4;
64 }
65 disasm_ui.treeView->resizeColumnToContents(0); 177 disasm_ui.treeView->resizeColumnToContents(0);
66 disasm_ui.treeView->resizeColumnToContents(1); 178 disasm_ui.treeView->resizeColumnToContents(1);
67 disasm_ui.treeView->resizeColumnToContents(2); 179 disasm_ui.treeView->resizeColumnToContents(2);
68 180
69 QModelIndex model_index = model->index(0, 0); 181 QModelIndex model_index = model->IndexFromAbsoluteAddress(Core::g_app_core->GetPC());
70 disasm_ui.treeView->scrollTo(model_index); 182 disasm_ui.treeView->scrollTo(model_index);
71 disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); 183 disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
72} 184}
73 185
74void DisassemblerWidget::OnSetBreakpoint()
75{
76 int selected_row = SelectedRow();
77
78 if (selected_row == -1)
79 return;
80
81 u32 address = base_addr + (selected_row * 4);
82 if (breakpoints->IsAddressBreakPoint(address))
83 {
84 breakpoints->Remove(address);
85 model->item(selected_row, 0)->setBackground(QBrush());
86 model->item(selected_row, 1)->setBackground(QBrush());
87 }
88 else
89 {
90 breakpoints->Add(address);
91 model->item(selected_row, 0)->setBackground(QBrush(QColor(0xFF, 0x99, 0x99)));
92 model->item(selected_row, 1)->setBackground(QBrush(QColor(0xFF, 0x99, 0x99)));
93 }
94}
95
96void DisassemblerWidget::OnContinue() 186void DisassemblerWidget::OnContinue()
97{ 187{
98 emu_thread.SetCpuRunning(true); 188 emu_thread.SetCpuRunning(true);
@@ -112,6 +202,9 @@ void DisassemblerWidget::OnStepInto()
112void DisassemblerWidget::OnPause() 202void DisassemblerWidget::OnPause()
113{ 203{
114 emu_thread.SetCpuRunning(false); 204 emu_thread.SetCpuRunning(false);
205
206 // TODO: By now, the CPU might not have actually stopped...
207 model->SetNextInstruction(Core::g_app_core->GetPC());
115} 208}
116 209
117void DisassemblerWidget::OnToggleStartStop() 210void DisassemblerWidget::OnToggleStartStop()
@@ -123,13 +216,15 @@ void DisassemblerWidget::OnCPUStepped()
123{ 216{
124 ARMword next_instr = Core::g_app_core->GetPC(); 217 ARMword next_instr = Core::g_app_core->GetPC();
125 218
126 if (breakpoints->IsAddressBreakPoint(next_instr)) 219 // TODO: Make BreakPoints less crappy (i.e. const-correct) so that this doesn't need a const_cast.
220 if (const_cast<BreakPoints&>(model->GetBreakPoints()).IsAddressBreakPoint(next_instr))
127 { 221 {
128 emu_thread.SetCpuRunning(false); 222 emu_thread.SetCpuRunning(false);
129 } 223 }
130 224
131 unsigned int index = (next_instr - base_addr) / 4; 225 model->SetNextInstruction(next_instr);
132 QModelIndex model_index = model->index(index, 0); 226
227 QModelIndex model_index = model->IndexFromAbsoluteAddress(next_instr);
133 disasm_ui.treeView->scrollTo(model_index); 228 disasm_ui.treeView->scrollTo(model_index);
134 disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); 229 disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
135} 230}
@@ -140,5 +235,5 @@ int DisassemblerWidget::SelectedRow()
140 if (!index.isValid()) 235 if (!index.isValid())
141 return -1; 236 return -1;
142 237
143 return model->itemFromIndex(disasm_ui.treeView->selectionModel()->currentIndex())->row(); 238 return disasm_ui.treeView->selectionModel()->currentIndex().row();
144} \ No newline at end of file 239}