disassembler.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "disassembler.hxx"
  2. #include "../bootmanager.hxx"
  3. #include "../hotkeys.hxx"
  4. #include "common/common.h"
  5. #include "core/mem_map.h"
  6. #include "core/core.h"
  7. #include "common/break_points.h"
  8. #include "common/symbols.h"
  9. #include "core/arm/skyeye_common/armdefs.h"
  10. #include "core/arm/disassembler/arm_disasm.h"
  11. DisassemblerModel::DisassemblerModel(QObject* parent) : QAbstractItemModel(parent), base_address(0), code_size(0), program_counter(0), selection(QModelIndex()) {
  12. }
  13. QModelIndex DisassemblerModel::index(int row, int column, const QModelIndex& parent) const {
  14. return createIndex(row, column);
  15. }
  16. QModelIndex DisassemblerModel::parent(const QModelIndex& child) const {
  17. return QModelIndex();
  18. }
  19. int DisassemblerModel::columnCount(const QModelIndex& parent) const {
  20. return 3;
  21. }
  22. int DisassemblerModel::rowCount(const QModelIndex& parent) const {
  23. return code_size;
  24. }
  25. QVariant DisassemblerModel::data(const QModelIndex& index, int role) const {
  26. switch (role) {
  27. case Qt::DisplayRole:
  28. {
  29. u32 address = base_address + index.row() * 4;
  30. u32 instr = Memory::Read32(address);
  31. std::string disassembly = ARM_Disasm::Disassemble(address, instr);
  32. if (index.column() == 0) {
  33. return QString("0x%1").arg((uint)(address), 8, 16, QLatin1Char('0'));
  34. } else if (index.column() == 1) {
  35. return QString::fromStdString(disassembly);
  36. } else if (index.column() == 2) {
  37. if(Symbols::HasSymbol(address)) {
  38. TSymbol symbol = Symbols::GetSymbol(address);
  39. return QString("%1 - Size:%2").arg(QString::fromStdString(symbol.name))
  40. .arg(symbol.size / 4); // divide by 4 to get instruction count
  41. } else if (ARM_Disasm::Decode(instr) == OP_BL) {
  42. u32 offset = instr & 0xFFFFFF;
  43. // Sign-extend the 24-bit offset
  44. if ((offset >> 23) & 1)
  45. offset |= 0xFF000000;
  46. // Pre-compute the left-shift and the prefetch offset
  47. offset <<= 2;
  48. offset += 8;
  49. TSymbol symbol = Symbols::GetSymbol(address + offset);
  50. return QString(" --> %1").arg(QString::fromStdString(symbol.name));
  51. }
  52. }
  53. break;
  54. }
  55. case Qt::BackgroundRole:
  56. {
  57. unsigned int address = base_address + 4 * index.row();
  58. if (breakpoints.IsAddressBreakPoint(address))
  59. return QBrush(QColor(0xFF, 0xC0, 0xC0));
  60. else if (address == program_counter)
  61. return QBrush(QColor(0xC0, 0xC0, 0xFF));
  62. break;
  63. }
  64. default:
  65. break;
  66. }
  67. return QVariant();
  68. }
  69. QModelIndex DisassemblerModel::IndexFromAbsoluteAddress(unsigned int address) const {
  70. return index((address - base_address) / 4, 0);
  71. }
  72. const BreakPoints& DisassemblerModel::GetBreakPoints() const {
  73. return breakpoints;
  74. }
  75. void DisassemblerModel::ParseFromAddress(unsigned int address) {
  76. // NOTE: A too large value causes lagging when scrolling the disassembly
  77. const unsigned int chunk_size = 1000*500;
  78. // If we haven't loaded anything yet, initialize base address to the parameter address
  79. if (code_size == 0)
  80. base_address = address;
  81. // If the new area is already loaded, just continue
  82. if (base_address + code_size > address + chunk_size && base_address <= address)
  83. return;
  84. // Insert rows before currently loaded data
  85. if (base_address > address) {
  86. unsigned int num_rows = (address - base_address) / 4;
  87. beginInsertRows(QModelIndex(), 0, num_rows);
  88. code_size += num_rows;
  89. base_address = address;
  90. endInsertRows();
  91. }
  92. // Insert rows after currently loaded data
  93. if (base_address + code_size < address + chunk_size) {
  94. unsigned int num_rows = (base_address + chunk_size - code_size - address) / 4;
  95. beginInsertRows(QModelIndex(), 0, num_rows);
  96. code_size += num_rows;
  97. endInsertRows();
  98. }
  99. SetNextInstruction(address);
  100. }
  101. void DisassemblerModel::OnSelectionChanged(const QModelIndex& new_selection) {
  102. selection = new_selection;
  103. }
  104. void DisassemblerModel::OnSetOrUnsetBreakpoint() {
  105. if (!selection.isValid())
  106. return;
  107. unsigned int address = base_address + selection.row() * 4;
  108. if (breakpoints.IsAddressBreakPoint(address)) {
  109. breakpoints.Remove(address);
  110. } else {
  111. breakpoints.Add(address);
  112. }
  113. emit dataChanged(selection, selection);
  114. }
  115. void DisassemblerModel::SetNextInstruction(unsigned int address) {
  116. QModelIndex cur_index = IndexFromAbsoluteAddress(program_counter);
  117. QModelIndex prev_index = IndexFromAbsoluteAddress(address);
  118. program_counter = address;
  119. emit dataChanged(cur_index, cur_index);
  120. emit dataChanged(prev_index, prev_index);
  121. }
  122. DisassemblerWidget::DisassemblerWidget(QWidget* parent, EmuThread& emu_thread) : QDockWidget(parent), base_addr(0), emu_thread(emu_thread)
  123. {
  124. disasm_ui.setupUi(this);
  125. model = new DisassemblerModel(this);
  126. disasm_ui.treeView->setModel(model);
  127. RegisterHotkey("Disassembler", "Start/Stop", QKeySequence(Qt::Key_F5), Qt::ApplicationShortcut);
  128. RegisterHotkey("Disassembler", "Step", QKeySequence(Qt::Key_F10), Qt::ApplicationShortcut);
  129. RegisterHotkey("Disassembler", "Step into", QKeySequence(Qt::Key_F11), Qt::ApplicationShortcut);
  130. RegisterHotkey("Disassembler", "Set Breakpoint", QKeySequence(Qt::Key_F9), Qt::ApplicationShortcut);
  131. connect(disasm_ui.button_breakpoint, SIGNAL(clicked()), model, SLOT(OnSetOrUnsetBreakpoint()));
  132. connect(disasm_ui.button_step, SIGNAL(clicked()), this, SLOT(OnStep()));
  133. connect(disasm_ui.button_pause, SIGNAL(clicked()), this, SLOT(OnPause()));
  134. connect(disasm_ui.button_continue, SIGNAL(clicked()), this, SLOT(OnContinue()));
  135. connect(disasm_ui.treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
  136. model, SLOT(OnSelectionChanged(const QModelIndex&)));
  137. connect(GetHotkey("Disassembler", "Start/Stop", this), SIGNAL(activated()), this, SLOT(OnToggleStartStop()));
  138. connect(GetHotkey("Disassembler", "Step", this), SIGNAL(activated()), this, SLOT(OnStep()));
  139. connect(GetHotkey("Disassembler", "Step into", this), SIGNAL(activated()), this, SLOT(OnStepInto()));
  140. connect(GetHotkey("Disassembler", "Set Breakpoint", this), SIGNAL(activated()), model, SLOT(OnSetOrUnsetBreakpoint()));
  141. }
  142. void DisassemblerWidget::Init()
  143. {
  144. model->ParseFromAddress(Core::g_app_core->GetPC());
  145. disasm_ui.treeView->resizeColumnToContents(0);
  146. disasm_ui.treeView->resizeColumnToContents(1);
  147. disasm_ui.treeView->resizeColumnToContents(2);
  148. QModelIndex model_index = model->IndexFromAbsoluteAddress(Core::g_app_core->GetPC());
  149. disasm_ui.treeView->scrollTo(model_index);
  150. disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
  151. }
  152. void DisassemblerWidget::OnContinue()
  153. {
  154. emu_thread.SetCpuRunning(true);
  155. }
  156. void DisassemblerWidget::OnStep()
  157. {
  158. OnStepInto(); // change later
  159. }
  160. void DisassemblerWidget::OnStepInto()
  161. {
  162. emu_thread.SetCpuRunning(false);
  163. emu_thread.ExecStep();
  164. }
  165. void DisassemblerWidget::OnPause()
  166. {
  167. emu_thread.SetCpuRunning(false);
  168. // TODO: By now, the CPU might not have actually stopped...
  169. model->SetNextInstruction(Core::g_app_core->GetPC());
  170. }
  171. void DisassemblerWidget::OnToggleStartStop()
  172. {
  173. emu_thread.SetCpuRunning(!emu_thread.IsCpuRunning());
  174. }
  175. void DisassemblerWidget::OnCPUStepped()
  176. {
  177. ARMword next_instr = Core::g_app_core->GetPC();
  178. // TODO: Make BreakPoints less crappy (i.e. const-correct) so that this doesn't need a const_cast.
  179. if (const_cast<BreakPoints&>(model->GetBreakPoints()).IsAddressBreakPoint(next_instr))
  180. {
  181. emu_thread.SetCpuRunning(false);
  182. }
  183. model->SetNextInstruction(next_instr);
  184. QModelIndex model_index = model->IndexFromAbsoluteAddress(next_instr);
  185. disasm_ui.treeView->scrollTo(model_index);
  186. disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
  187. }
  188. int DisassemblerWidget::SelectedRow()
  189. {
  190. QModelIndex index = disasm_ui.treeView->selectionModel()->currentIndex();
  191. if (!index.isValid())
  192. return -1;
  193. return disasm_ui.treeView->selectionModel()->currentIndex().row();
  194. }