| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/AssemblerWidget.h" |
| 5 | |
| 6 | #include <QAction> |
| 7 | #include <QApplication> |
| 8 | #include <QClipboard> |
| 9 | #include <QComboBox> |
| 10 | #include <QFont> |
| 11 | #include <QFontDatabase> |
| 12 | #include <QGridLayout> |
| 13 | #include <QGroupBox> |
| 14 | #include <QLabel> |
| 15 | #include <QLineEdit> |
| 16 | #include <QMenu> |
| 17 | #include <QPlainTextEdit> |
| 18 | #include <QPushButton> |
| 19 | #include <QScrollBar> |
| 20 | #include <QShortcut> |
| 21 | #include <QStyle> |
| 22 | #include <QTabWidget> |
| 23 | #include <QTextBlock> |
| 24 | #include <QTextEdit> |
| 25 | #include <QToolBar> |
| 26 | #include <QToolButton> |
| 27 | |
| 28 | #include <filesystem> |
| 29 | #include <fmt/format.h> |
| 30 | |
| 31 | #include "Common/Assert.h" |
| 32 | #include "Common/FileUtil.h" |
| 33 | |
| 34 | #include "Core/Core.h" |
| 35 | #include "Core/PowerPC/MMU.h" |
| 36 | #include "Core/PowerPC/PowerPC.h" |
| 37 | #include "Core/System.h" |
| 38 | |
| 39 | #include "DolphinQt/Debugger/AssemblyEditor.h" |
| 40 | #include "DolphinQt/QtUtils/DolphinFileDialog.h" |
| 41 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 42 | #include "DolphinQt/Resources.h" |
| 43 | #include "DolphinQt/Settings.h" |
| 44 | |
| 45 | namespace |
| 46 | { |
| 47 | using namespace Common::GekkoAssembler; |
| 48 | |
| 49 | QString HtmlFormatErrorLoc(const AssemblerError& err) |
| 50 | { |
| 51 | return QObject::tr("<span style=\"color: red; font-weight: bold\">Error</span> on line %1 col %2") |
| 52 | .arg(err.line + 1) |
| 53 | .arg(err.col + 1); |
| 54 | } |
| 55 | |
| 56 | QString HtmlFormatErrorLine(const AssemblerError& err) |
| 57 | { |
| 58 | const QString line_pre_error = |
| 59 | QString::fromStdString(std::string(err.error_line.substr(0, err.col))).toHtmlEscaped(); |
| 60 | const QString line_error = |
| 61 | QString::fromStdString(std::string(err.error_line.substr(err.col, err.len))).toHtmlEscaped(); |
| 62 | const QString line_post_error = |
| 63 | QString::fromStdString(std::string(err.error_line.substr(err.col + err.len))).toHtmlEscaped(); |
| 64 | |
| 65 | return QStringLiteral("<span style=\"font-family:'monospace';font-size:16px\">" |
| 66 | "<pre>%1<u><span style=\"color:red;font-weight:bold\">%2</span></u>%3</pre>" |
| 67 | "</span>") |
| 68 | .arg(line_pre_error) |
| 69 | .arg(line_error) |
| 70 | .arg(line_post_error); |
| 71 | } |
| 72 | |
| 73 | QString HtmlFormatMessage(const AssemblerError& err) |
| 74 | { |
| 75 | return QStringLiteral("<span>%1</span>").arg(QString::fromStdString(err.message).toHtmlEscaped()); |
| 76 | } |
| 77 | |
| 78 | void DeserializeBlock(const CodeBlock& blk, std::ostringstream& out_str, bool pad4) |
| 79 | { |
| 80 | size_t i = 0; |
| 81 | for (; i < blk.instructions.size(); i++) |
| 82 | { |
| 83 | out_str << fmt::format("{:02x}", blk.instructions[i]); |
| 84 | if (i % 8 == 7) |
| 85 | { |
| 86 | out_str << '\n'; |
| 87 | } |
| 88 | else if (i % 4 == 3) |
| 89 | { |
| 90 | out_str << ' '; |
| 91 | } |
| 92 | } |
| 93 | if (pad4) |
| 94 | { |
| 95 | bool did_pad = false; |
| 96 | for (; i % 4 != 0; i++) |
| 97 | { |
| 98 | out_str << "00"; |
| 99 | did_pad = true; |
| 100 | } |
| 101 | |
| 102 | if (did_pad) |
| 103 | { |
| 104 | out_str << (i % 8 == 0 ? '\n' : ' '); |
| 105 | } |
| 106 | } |
| 107 | else if (i % 8 != 7) |
| 108 | { |
| 109 | out_str << '\n'; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void DeserializeToRaw(const std::vector<CodeBlock>& blocks, std::ostringstream& out_str) |
| 114 | { |
| 115 | for (const auto& blk : blocks) |
| 116 | { |
| 117 | if (blk.instructions.empty()) |
| 118 | { |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | out_str << fmt::format("# Block {:08x}\n", blk.block_address); |
| 123 | DeserializeBlock(blk, out_str, false); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void DeserializeToAr(const std::vector<CodeBlock>& blocks, std::ostringstream& out_str) |
| 128 | { |
| 129 | for (const auto& blk : blocks) |
| 130 | { |
| 131 | if (blk.instructions.empty()) |
| 132 | { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | size_t i = 0; |
| 137 | for (; i < blk.instructions.size() - 3; i += 4) |
| 138 | { |
| 139 | // type=NormalCode, subtype=SUB_RAM_WRITE, size=32bit |
| 140 | const u32 ar_addr = ((blk.block_address + i) & 0x1ffffff) | 0x04000000; |
| 141 | out_str << fmt::format("{:08x} {:02x}{:02x}{:02x}{:02x}\n", ar_addr, blk.instructions[i], |
| 142 | blk.instructions[i + 1], blk.instructions[i + 2], |
| 143 | blk.instructions[i + 3]); |
| 144 | } |
| 145 | |
| 146 | for (; i < blk.instructions.size(); i++) |
| 147 | { |
| 148 | // type=NormalCode, subtype=SUB_RAM_WRITE, size=8bit |
| 149 | const u32 ar_addr = ((blk.block_address + i) & 0x1ffffff); |
| 150 | out_str << fmt::format("{:08x} 000000{:02x}\n", ar_addr, blk.instructions[i]); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void DeserializeToGecko(const std::vector<CodeBlock>& blocks, std::ostringstream& out_str) |
| 156 | { |
| 157 | DeserializeToAr(blocks, out_str); |
| 158 | } |
| 159 | |
| 160 | void DeserializeToGeckoExec(const std::vector<CodeBlock>& blocks, std::ostringstream& out_str) |
| 161 | { |
| 162 | for (const auto& blk : blocks) |
| 163 | { |
| 164 | if (blk.instructions.empty()) |
| 165 | { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | u32 nlines = 1 + static_cast<u32>((blk.instructions.size() - 1) / 8); |
| 170 | bool ret_on_newline = false; |
| 171 | if (blk.instructions.size() % 8 == 0 || blk.instructions.size() % 8 > 4) |
| 172 | { |
| 173 | // Append extra line for blr |
| 174 | nlines++; |
| 175 | ret_on_newline = true; |
| 176 | } |
| 177 | |
| 178 | out_str << fmt::format("c0000000 {:08x}\n", nlines); |
| 179 | DeserializeBlock(blk, out_str, true); |
| 180 | if (ret_on_newline) |
| 181 | { |
| 182 | out_str << "4e800020 00000000\n"; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | out_str << "4e800020\n"; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void DeserializeToGeckoTramp(const std::vector<CodeBlock>& blocks, std::ostringstream& out_str) |
| 192 | { |
| 193 | for (const auto& blk : blocks) |
| 194 | { |
| 195 | if (blk.instructions.empty()) |
| 196 | { |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | const u32 inject_addr = (blk.block_address & 0x1ffffff) | 0x02000000; |
| 201 | u32 nlines = 1 + static_cast<u32>((blk.instructions.size() - 1) / 8); |
| 202 | bool padding_on_newline = false; |
| 203 | if (blk.instructions.size() % 8 == 0 || blk.instructions.size() % 8 > 4) |
| 204 | { |
| 205 | // Append extra line for nop+branchback |
| 206 | nlines++; |
| 207 | padding_on_newline = true; |
| 208 | } |
| 209 | |
| 210 | out_str << fmt::format("c{:07x} {:08x}\n", inject_addr, nlines); |
| 211 | DeserializeBlock(blk, out_str, true); |
| 212 | if (padding_on_newline) |
| 213 | { |
| 214 | out_str << "60000000 00000000\n"; |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | out_str << "00000000\n"; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } // namespace |
| 223 | |
| 224 | AssemblerWidget::AssemblerWidget(QWidget* parent) |
| 225 | : QDockWidget(parent), m_system(Core::System::GetInstance()), m_unnamed_editor_count(0), |
| 226 | m_net_zoom_delta(0) |
| 227 | { |
| 228 | { |
| 229 | QPalette base_palette; |
| 230 | m_dark_scheme = base_palette.color(QPalette::WindowText).value() > |
| 231 | base_palette.color(QPalette::Window).value(); |
| 232 | } |
| 233 | |
| 234 | setWindowTitle(tr("Assembler")); |
| 235 | setObjectName(QStringLiteral("assemblerwidget")); |
| 236 | |
| 237 | setHidden(!Settings::Instance().IsAssemblerVisible() || |
| 238 | !Settings::Instance().IsDebugModeEnabled()); |
| 239 | |
| 240 | this->setVisible(true); |
| 241 | CreateWidgets(); |
| 242 | |
| 243 | restoreGeometry( |
| 244 | Settings::GetQSettings().value(QStringLiteral("assemblerwidget/geometry")).toByteArray()); |
| 245 | setFloating(Settings::GetQSettings().value(QStringLiteral("assemblerwidget/floating")).toBool()); |
| 246 | |
| 247 | connect(&Settings::Instance(), &Settings::AssemblerVisibilityChanged, this, |
| 248 | [this](bool visible) { setHidden(!visible); }); |
| 249 | |
| 250 | connect(&Settings::Instance(), &Settings::DebugModeToggled, this, [this](bool enabled) { |
| 251 | setHidden(!enabled || !Settings::Instance().IsAssemblerVisible()); |
| 252 | }); |
| 253 | |
| 254 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 255 | &AssemblerWidget::OnEmulationStateChanged); |
| 256 | connect(&Settings::Instance(), &Settings::ThemeChanged, this, &AssemblerWidget::UpdateIcons); |
| 257 | connect(m_asm_tabs, &QTabWidget::tabCloseRequested, this, &AssemblerWidget::OnTabClose); |
| 258 | |
| 259 | auto* save_shortcut = new QShortcut(QKeySequence::Save, this); |
| 260 | // Save should only activate if the active tab is in focus |
| 261 | save_shortcut->connect(save_shortcut, &QShortcut::activated, this, [this] { |
| 262 | if (m_asm_tabs->currentIndex() != -1 && m_asm_tabs->currentWidget()->hasFocus()) |
| 263 | { |
| 264 | OnSave(); |
| 265 | } |
| 266 | }); |
| 267 | |
| 268 | auto* zoom_in_shortcut = new QShortcut(QKeySequence::ZoomIn, this); |
| 269 | zoom_in_shortcut->setContext(Qt::WidgetWithChildrenShortcut); |
| 270 | connect(zoom_in_shortcut, &QShortcut::activated, this, &AssemblerWidget::OnZoomIn); |
| 271 | auto* zoom_out_shortcut = new QShortcut(QKeySequence::ZoomOut, this); |
| 272 | zoom_out_shortcut->setContext(Qt::WidgetWithChildrenShortcut); |
| 273 | connect(zoom_out_shortcut, &QShortcut::activated, this, &AssemblerWidget::OnZoomOut); |
| 274 | |
| 275 | auto* zoom_in_alternate = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Equal), this); |
| 276 | zoom_in_alternate->setContext(Qt::WidgetWithChildrenShortcut); |
| 277 | connect(zoom_in_alternate, &QShortcut::activated, this, &AssemblerWidget::OnZoomIn); |
| 278 | auto* zoom_out_alternate = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Underscore), this); |
| 279 | zoom_out_alternate->setContext(Qt::WidgetWithChildrenShortcut); |
| 280 | connect(zoom_out_alternate, &QShortcut::activated, this, &AssemblerWidget::OnZoomOut); |
| 281 | |
| 282 | auto* zoom_reset = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_0), this); |
| 283 | zoom_reset->setContext(Qt::WidgetWithChildrenShortcut); |
| 284 | connect(zoom_reset, &QShortcut::activated, this, &AssemblerWidget::OnZoomReset); |
| 285 | |
| 286 | ConnectWidgets(); |
| 287 | UpdateIcons(); |
| 288 | } |
| 289 | |
| 290 | void AssemblerWidget::closeEvent(QCloseEvent*) |
| 291 | { |
| 292 | Settings::Instance().SetAssemblerVisible(false); |
| 293 | } |
| 294 | |
| 295 | bool AssemblerWidget::ApplicationCloseRequest() |
| 296 | { |
| 297 | int num_unsaved = 0; |
| 298 | for (int i = 0; i < m_asm_tabs->count(); i++) |
| 299 | { |
| 300 | if (GetEditor(i)->IsDirty()) |
| 301 | { |
| 302 | num_unsaved++; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (num_unsaved > 0) |
| 307 | { |
| 308 | const int result = ModalMessageBox::question( |
| 309 | this, tr("Unsaved Changes"), |
| 310 | tr("You have %1 unsaved assembly tabs open\n\n" |
| 311 | "Do you want to save all and exit?") |
| 312 | .arg(num_unsaved), |
| 313 | QMessageBox::YesToAll | QMessageBox::NoToAll | QMessageBox::Cancel, QMessageBox::Cancel); |
| 314 | switch (result) |
| 315 | { |
| 316 | case QMessageBox::YesToAll: |
| 317 | for (int i = 0; i < m_asm_tabs->count(); i++) |
| 318 | { |
| 319 | AsmEditor* editor = GetEditor(i); |
| 320 | if (editor->IsDirty()) |
| 321 | { |
| 322 | if (!SaveEditor(editor)) |
| 323 | { |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | return true; |
| 329 | case QMessageBox::NoToAll: |
| 330 | return true; |
| 331 | case QMessageBox::Cancel: |
| 332 | return false; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | AssemblerWidget::~AssemblerWidget() |
| 340 | { |
| 341 | auto& settings = Settings::GetQSettings(); |
| 342 | |
| 343 | settings.setValue(QStringLiteral("assemblerwidget/geometry"), saveGeometry()); |
| 344 | settings.setValue(QStringLiteral("assemblerwidget/floating"), isFloating()); |
| 345 | } |
| 346 | |
| 347 | void AssemblerWidget::CreateWidgets() |
| 348 | { |
| 349 | m_asm_tabs = new QTabWidget; |
| 350 | m_toolbar = new QToolBar; |
| 351 | m_output_type = new QComboBox; |
| 352 | m_output_box = new QPlainTextEdit; |
| 353 | m_error_box = new QTextEdit; |
| 354 | m_address_line = new QLineEdit; |
| 355 | m_copy_output_button = new QPushButton; |
| 356 | |
| 357 | m_asm_tabs->setTabsClosable(true); |
| 358 | |
| 359 | // Initialize toolbar and actions |
| 360 | // m_toolbar->setIconSize(QSize(32, 32)); |
| 361 | m_toolbar->setContentsMargins(0, 0, 0, 0); |
| 362 | m_toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 363 | |
| 364 | m_open = m_toolbar->addAction(tr("Open"), this, &AssemblerWidget::OnOpen); |
| 365 | m_new = m_toolbar->addAction(tr("New"), this, &AssemblerWidget::OnNew); |
| 366 | m_assemble = m_toolbar->addAction(tr("Assemble"), this, [this] { |
| 367 | std::vector<CodeBlock> unused; |
| 368 | OnAssemble(&unused); |
| 369 | }); |
| 370 | m_inject = m_toolbar->addAction(tr("Inject"), this, &AssemblerWidget::OnInject); |
| 371 | m_save = m_toolbar->addAction(tr("Save"), this, &AssemblerWidget::OnSave); |
| 372 | |
| 373 | m_inject->setEnabled(false); |
| 374 | m_save->setEnabled(false); |
| 375 | m_assemble->setEnabled(false); |
| 376 | |
| 377 | // Initialize input, output, error text areas |
| 378 | auto palette = m_output_box->palette(); |
| 379 | if (m_dark_scheme) |
| 380 | { |
| 381 | palette.setColor(QPalette::Base, QColor::fromRgb(76, 76, 76)); |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | palette.setColor(QPalette::Base, QColor::fromRgb(180, 180, 180)); |
| 386 | } |
| 387 | m_output_box->setPalette(palette); |
| 388 | m_error_box->setPalette(palette); |
| 389 | |
| 390 | QFont mono_font(QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); |
| 391 | QFont error_font(QFontDatabase::systemFont(QFontDatabase::GeneralFont).family()); |
| 392 | mono_font.setPointSize(12); |
| 393 | error_font.setPointSize(12); |
| 394 | QFontMetrics mono_metrics(mono_font); |
| 395 | QFontMetrics err_metrics(mono_font); |
| 396 | |
| 397 | m_output_box->setFont(mono_font); |
| 398 | m_error_box->setFont(error_font); |
| 399 | m_output_box->setReadOnly(true); |
| 400 | m_error_box->setReadOnly(true); |
| 401 | |
| 402 | const int output_area_width = mono_metrics.horizontalAdvance(QLatin1Char('0')) * OUTPUT_BOX_WIDTH; |
| 403 | m_error_box->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); |
| 404 | m_error_box->setFixedHeight(err_metrics.height() * 3 + mono_metrics.height()); |
| 405 | m_output_box->setFixedWidth(output_area_width); |
| 406 | m_error_box->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); |
| 407 | |
| 408 | // Initialize output format selection box |
| 409 | m_output_type->addItem(tr("Raw")); |
| 410 | m_output_type->addItem(tr("AR Code")); |
| 411 | m_output_type->addItem(tr("Gecko (04)")); |
| 412 | m_output_type->addItem(tr("Gecko (C0)")); |
| 413 | m_output_type->addItem(tr("Gecko (C2)")); |
| 414 | |
| 415 | // Setup layouts |
| 416 | auto* addr_input_layout = new QHBoxLayout; |
| 417 | addr_input_layout->addWidget(new QLabel(tr("Base Address"))); |
| 418 | addr_input_layout->addWidget(m_address_line); |
| 419 | |
| 420 | auto* output_extra_layout = new QHBoxLayout; |
| 421 | output_extra_layout->addWidget(m_output_type); |
| 422 | output_extra_layout->addWidget(m_copy_output_button); |
| 423 | |
| 424 | QWidget* address_input_box = new QWidget(); |
| 425 | address_input_box->setLayout(addr_input_layout); |
| 426 | addr_input_layout->setContentsMargins(0, 0, 0, 0); |
| 427 | |
| 428 | QWidget* output_extra_box = new QWidget(); |
| 429 | output_extra_box->setFixedWidth(output_area_width); |
| 430 | output_extra_box->setLayout(output_extra_layout); |
| 431 | output_extra_layout->setContentsMargins(0, 0, 0, 0); |
| 432 | |
| 433 | auto* assembler_layout = new QGridLayout; |
| 434 | assembler_layout->setSpacing(0); |
| 435 | assembler_layout->setContentsMargins(5, 0, 5, 5); |
| 436 | assembler_layout->addWidget(m_toolbar, 0, 0, 1, 2); |
| 437 | { |
| 438 | auto* input_group = new QGroupBox(tr("Input")); |
| 439 | auto* layout = new QVBoxLayout; |
| 440 | input_group->setLayout(layout); |
| 441 | layout->addWidget(m_asm_tabs); |
| 442 | layout->addWidget(address_input_box); |
| 443 | assembler_layout->addWidget(input_group, 1, 0, 1, 1); |
| 444 | } |
| 445 | { |
| 446 | auto* output_group = new QGroupBox(tr("Output")); |
| 447 | auto* layout = new QGridLayout; |
| 448 | output_group->setLayout(layout); |
| 449 | layout->addWidget(m_output_box, 0, 0); |
| 450 | layout->addWidget(output_extra_box, 1, 0); |
| 451 | assembler_layout->addWidget(output_group, 1, 1, 1, 1); |
| 452 | output_group->setSizePolicy( |
| 453 | QSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Expanding)); |
| 454 | } |
| 455 | { |
| 456 | auto* error_group = new QGroupBox(tr("Error Log")); |
| 457 | auto* layout = new QHBoxLayout; |
| 458 | error_group->setLayout(layout); |
| 459 | layout->addWidget(m_error_box); |
| 460 | assembler_layout->addWidget(error_group, 2, 0, 1, 2); |
| 461 | error_group->setSizePolicy( |
| 462 | QSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Fixed)); |
| 463 | } |
| 464 | |
| 465 | QWidget* widget = new QWidget; |
| 466 | widget->setLayout(assembler_layout); |
| 467 | setWidget(widget); |
| 468 | } |
| 469 | |
| 470 | void AssemblerWidget::ConnectWidgets() |
| 471 | { |
| 472 | m_output_box->connect(m_output_box, &QPlainTextEdit::updateRequest, this, [this] { |
| 473 | if (m_output_box->verticalScrollBar()->isVisible()) |
| 474 | { |
| 475 | m_output_box->setFixedWidth(m_output_box->fontMetrics().horizontalAdvance(QLatin1Char('0')) * |
| 476 | OUTPUT_BOX_WIDTH + |
| 477 | m_output_box->style()->pixelMetric(QStyle::PM_ScrollBarExtent)); |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | m_output_box->setFixedWidth(m_output_box->fontMetrics().horizontalAdvance(QLatin1Char('0')) * |
| 482 | OUTPUT_BOX_WIDTH); |
| 483 | } |
| 484 | }); |
| 485 | m_copy_output_button->connect(m_copy_output_button, &QPushButton::released, this, |
| 486 | &AssemblerWidget::OnCopyOutput); |
| 487 | m_address_line->connect(m_address_line, &QLineEdit::textChanged, this, |
| 488 | &AssemblerWidget::OnBaseAddressChanged); |
| 489 | m_asm_tabs->connect(m_asm_tabs, &QTabWidget::currentChanged, this, &AssemblerWidget::OnTabChange); |
| 490 | } |
| 491 | |
| 492 | void AssemblerWidget::OnAssemble(std::vector<CodeBlock>* asm_out) |
| 493 | { |
| 494 | if (m_asm_tabs->currentIndex() == -1) |
| 495 | { |
| 496 | return; |
| 497 | } |
| 498 | AsmEditor* active_editor = GetEditor(m_asm_tabs->currentIndex()); |
| 499 | |
| 500 | AsmKind kind = AsmKind::Raw; |
| 501 | m_error_box->clear(); |
| 502 | m_output_box->clear(); |
| 503 | switch (m_output_type->currentIndex()) |
| 504 | { |
| 505 | case 0: |
| 506 | kind = AsmKind::Raw; |
| 507 | break; |
| 508 | case 1: |
| 509 | kind = AsmKind::ActionReplay; |
| 510 | break; |
| 511 | case 2: |
| 512 | kind = AsmKind::Gecko; |
| 513 | break; |
| 514 | case 3: |
| 515 | kind = AsmKind::GeckoExec; |
| 516 | break; |
| 517 | case 4: |
| 518 | kind = AsmKind::GeckoTrampoline; |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | bool good; |
| 523 | u32 base_address = m_address_line->text().toUInt(&good, 16); |
| 524 | if (!good) |
| 525 | { |
| 526 | base_address = 0; |
| 527 | m_error_box->append( |
| 528 | tr("<span style=\"color:#ffcc00\">Warning</span> invalid base address, defaulting to 0")); |
| 529 | } |
| 530 | |
| 531 | const std::string contents = active_editor->toPlainText().toStdString(); |
| 532 | auto result = Assemble(contents, base_address); |
| 533 | if (IsFailure(result)) |
| 534 | { |
| 535 | m_error_box->clear(); |
| 536 | asm_out->clear(); |
| 537 | |
| 538 | const AssemblerError& error = GetFailure(result); |
| 539 | m_error_box->append(HtmlFormatErrorLoc(error)); |
| 540 | m_error_box->append(HtmlFormatErrorLine(error)); |
| 541 | m_error_box->append(HtmlFormatMessage(error)); |
| 542 | asm_out->clear(); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | auto& blocks = GetT(result); |
| 547 | std::ostringstream str_contents; |
| 548 | switch (kind) |
| 549 | { |
| 550 | case AsmKind::Raw: |
| 551 | DeserializeToRaw(blocks, str_contents); |
| 552 | break; |
| 553 | case AsmKind::ActionReplay: |
| 554 | DeserializeToAr(blocks, str_contents); |
| 555 | break; |
| 556 | case AsmKind::Gecko: |
| 557 | DeserializeToGecko(blocks, str_contents); |
| 558 | break; |
| 559 | case AsmKind::GeckoExec: |
| 560 | DeserializeToGeckoExec(blocks, str_contents); |
| 561 | break; |
| 562 | case AsmKind::GeckoTrampoline: |
| 563 | DeserializeToGeckoTramp(blocks, str_contents); |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | m_output_box->appendPlainText(QString::fromStdString(str_contents.str())); |
| 568 | m_output_box->moveCursor(QTextCursor::MoveOperation::Start); |
| 569 | m_output_box->ensureCursorVisible(); |
| 570 | |
| 571 | *asm_out = std::move(GetT(result)); |
| 572 | } |
| 573 | |
| 574 | void AssemblerWidget::OnCopyOutput() |
| 575 | { |
| 576 | QApplication::clipboard()->setText(m_output_box->toPlainText()); |
| 577 | } |
| 578 | |
| 579 | void AssemblerWidget::OnOpen() |
| 580 | { |
| 581 | const std::string default_dir = File::GetUserPath(D_ASM_ROOT_IDX); |
| 582 | const QStringList paths = DolphinFileDialog::getOpenFileNames( |
| 583 | this, tr("Select a File"), QString::fromStdString(default_dir), |
| 584 | QStringLiteral("%1 (*.s *.S *.asm);;%2 (*)") |
| 585 | .arg(tr("All Assembly files")) |
| 586 | .arg(tr("All Files"))); |
| 587 | if (paths.isEmpty()) |
| 588 | { |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | std::optional<int> show_index; |
| 593 | for (auto path : paths) |
| 594 | { |
| 595 | show_index = std::nullopt; |
| 596 | for (int i = 0; i < m_asm_tabs->count(); i++) |
| 597 | { |
| 598 | AsmEditor* editor = GetEditor(i); |
| 599 | if (editor->PathsMatch(path)) |
| 600 | { |
| 601 | show_index = i; |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (!show_index) |
| 607 | { |
| 608 | NewEditor(path); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (show_index) |
| 613 | { |
| 614 | m_asm_tabs->setCurrentIndex(*show_index); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | void AssemblerWidget::OnNew() |
| 619 | { |
| 620 | NewEditor(); |
| 621 | } |
| 622 | |
| 623 | void AssemblerWidget::OnInject() |
| 624 | { |
| 625 | Core::CPUThreadGuard guard(m_system); |
| 626 | |
| 627 | std::vector<CodeBlock> asm_result; |
| 628 | OnAssemble(&asm_result); |
| 629 | for (const auto& blk : asm_result) |
| 630 | { |
| 631 | if (!PowerPC::MMU::HostIsRAMAddress(guard, blk.block_address) || blk.instructions.empty()) |
| 632 | { |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | m_system.GetPowerPC().GetDebugInterface().SetPatch(guard, blk.block_address, blk.instructions); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void AssemblerWidget::OnSave() |
| 641 | { |
| 642 | if (m_asm_tabs->currentIndex() == -1) |
| 643 | { |
| 644 | return; |
| 645 | } |
| 646 | AsmEditor* active_editor = GetEditor(m_asm_tabs->currentIndex()); |
| 647 | |
| 648 | SaveEditor(active_editor); |
| 649 | } |
| 650 | |
| 651 | void AssemblerWidget::OnZoomIn() |
| 652 | { |
| 653 | if (m_asm_tabs->currentIndex() != -1) |
| 654 | { |
| 655 | ZoomAllEditors(2); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | void AssemblerWidget::OnZoomOut() |
| 660 | { |
| 661 | if (m_asm_tabs->currentIndex() != -1) |
| 662 | { |
| 663 | ZoomAllEditors(-2); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void AssemblerWidget::OnZoomReset() |
| 668 | { |
| 669 | if (m_asm_tabs->currentIndex() != -1) |
| 670 | { |
| 671 | ZoomAllEditors(-m_net_zoom_delta); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | void AssemblerWidget::OnBaseAddressChanged() |
| 676 | { |
| 677 | if (m_asm_tabs->currentIndex() == -1) |
| 678 | { |
| 679 | return; |
| 680 | } |
| 681 | AsmEditor* active_editor = GetEditor(m_asm_tabs->currentIndex()); |
| 682 | |
| 683 | active_editor->SetBaseAddress(m_address_line->text()); |
| 684 | } |
| 685 | |
| 686 | void AssemblerWidget::OnTabChange(int index) |
| 687 | { |
| 688 | if (index == -1) |
| 689 | { |
| 690 | m_address_line->clear(); |
| 691 | return; |
| 692 | } |
| 693 | AsmEditor* active_editor = GetEditor(index); |
| 694 | |
| 695 | m_address_line->setText(active_editor->BaseAddress()); |
| 696 | } |
| 697 | |
| 698 | QString AssemblerWidget::TabTextForEditor(AsmEditor* editor, bool with_dirty) |
| 699 | { |
| 700 | ASSERT(editor != nullptr); |
| 701 | |
| 702 | QString result; |
| 703 | if (!editor->Path().isEmpty()) |
| 704 | result = editor->EditorTitle(); |
| 705 | else if (editor->EditorNum() == 0) |
| 706 | result = tr("New File"); |
| 707 | else |
| 708 | result = tr("New File (%1)").arg(editor->EditorNum() + 1); |
| 709 | |
| 710 | if (with_dirty && editor->IsDirty()) |
| 711 | { |
| 712 | // i18n: This asterisk is added to the title of an editor to indicate that it has unsaved |
| 713 | // changes |
| 714 | result = tr("%1 *").arg(result); |
| 715 | } |
| 716 | |
| 717 | return result; |
| 718 | } |
| 719 | |
| 720 | AsmEditor* AssemblerWidget::GetEditor(int idx) |
| 721 | { |
| 722 | return qobject_cast<AsmEditor*>(m_asm_tabs->widget(idx)); |
| 723 | } |
| 724 | |
| 725 | void AssemblerWidget::NewEditor(const QString& path) |
| 726 | { |
| 727 | AsmEditor* new_editor = |
| 728 | new AsmEditor(path, path.isEmpty() ? AllocateTabNum() : INVALID_EDITOR_NUM, m_dark_scheme); |
| 729 | if (!path.isEmpty() && !new_editor->LoadFromPath()) |
| 730 | { |
| 731 | ModalMessageBox::warning(this, tr("Failed to open file"), |
| 732 | tr("Failed to read the contents of file:\n%1").arg(path)); |
| 733 | delete new_editor; |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | const int tab_idx = m_asm_tabs->addTab(new_editor, QStringLiteral()); |
| 738 | new_editor->connect(new_editor, &AsmEditor::PathChanged, this, [this] { |
| 739 | AsmEditor* updated_tab = qobject_cast<AsmEditor*>(sender()); |
| 740 | DisambiguateTabTitles(updated_tab); |
| 741 | UpdateTabText(updated_tab); |
| 742 | }); |
| 743 | new_editor->connect(new_editor, &AsmEditor::DirtyChanged, this, |
| 744 | [this] { UpdateTabText(qobject_cast<AsmEditor*>(sender())); }); |
| 745 | new_editor->connect(new_editor, &AsmEditor::ZoomRequested, this, |
| 746 | &AssemblerWidget::ZoomAllEditors); |
| 747 | new_editor->Zoom(m_net_zoom_delta); |
| 748 | |
| 749 | DisambiguateTabTitles(new_editor); |
| 750 | |
| 751 | m_asm_tabs->setTabText(tab_idx, TabTextForEditor(new_editor, true)); |
| 752 | |
| 753 | if (m_save && m_assemble) |
| 754 | { |
| 755 | m_save->setEnabled(true); |
| 756 | m_assemble->setEnabled(true); |
| 757 | } |
| 758 | |
| 759 | m_asm_tabs->setCurrentIndex(tab_idx); |
| 760 | } |
| 761 | |
| 762 | bool AssemblerWidget::SaveEditor(AsmEditor* editor) |
| 763 | { |
| 764 | QString save_path = editor->Path(); |
| 765 | if (save_path.isEmpty()) |
| 766 | { |
| 767 | const std::string default_dir = File::GetUserPath(D_ASM_ROOT_IDX); |
| 768 | const QString asm_filter = QStringLiteral("%1 (*.S)").arg(tr("Assembly File")); |
| 769 | const QString all_filter = QStringLiteral("%2 (*)").arg(tr("All Files")); |
| 770 | |
| 771 | QString selected_filter; |
| 772 | save_path = DolphinFileDialog::getSaveFileName( |
| 773 | this, tr("Save File To"), QString::fromStdString(default_dir), |
| 774 | QStringLiteral("%1;;%2").arg(asm_filter).arg(all_filter), &selected_filter); |
| 775 | |
| 776 | if (save_path.isEmpty()) |
| 777 | { |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | if (selected_filter == asm_filter && |
| 782 | std::filesystem::path(save_path.toStdString()).extension().empty()) |
| 783 | { |
| 784 | save_path.append(QStringLiteral(".S")); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | editor->SaveFile(save_path); |
| 789 | return true; |
| 790 | } |
| 791 | |
| 792 | void AssemblerWidget::OnEmulationStateChanged(Core::State state) |
| 793 | { |
| 794 | m_inject->setEnabled(state == Core::State::Running || state == Core::State::Paused); |
| 795 | } |
| 796 | |
| 797 | void AssemblerWidget::OnTabClose(int index) |
| 798 | { |
| 799 | ASSERT(index < m_asm_tabs->count()); |
| 800 | AsmEditor* editor = GetEditor(index); |
| 801 | |
| 802 | if (editor->IsDirty()) |
| 803 | { |
| 804 | const int result = ModalMessageBox::question( |
| 805 | this, tr("Unsaved Changes"), |
| 806 | tr("There are unsaved changes in \"%1\".\n\n" |
| 807 | "Do you want to save before closing?") |
| 808 | .arg(TabTextForEditor(editor, false)), |
| 809 | QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Cancel); |
| 810 | switch (result) |
| 811 | { |
| 812 | case QMessageBox::Yes: |
| 813 | if (editor->IsDirty()) |
| 814 | { |
| 815 | if (!SaveEditor(editor)) |
| 816 | { |
| 817 | return; |
| 818 | } |
| 819 | } |
| 820 | break; |
| 821 | case QMessageBox::No: |
| 822 | break; |
| 823 | case QMessageBox::Cancel: |
| 824 | return; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | CloseTab(index, editor); |
| 829 | } |
| 830 | |
| 831 | void AssemblerWidget::CloseTab(int index, AsmEditor* editor) |
| 832 | { |
| 833 | FreeTabNum(editor->EditorNum()); |
| 834 | |
| 835 | m_asm_tabs->removeTab(index); |
| 836 | editor->deleteLater(); |
| 837 | |
| 838 | DisambiguateTabTitles(nullptr); |
| 839 | |
| 840 | if (m_asm_tabs->count() == 0 && m_save && m_assemble) |
| 841 | { |
| 842 | m_save->setEnabled(false); |
| 843 | m_assemble->setEnabled(false); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | int AssemblerWidget::AllocateTabNum() |
| 848 | { |
| 849 | auto min_it = std::ranges::min_element(m_free_editor_nums); |
| 850 | if (min_it == m_free_editor_nums.end()) |
| 851 | { |
| 852 | return m_unnamed_editor_count++; |
| 853 | } |
| 854 | |
| 855 | const int min = *min_it; |
| 856 | m_free_editor_nums.erase(min_it); |
| 857 | return min; |
| 858 | } |
| 859 | |
| 860 | void AssemblerWidget::FreeTabNum(int num) |
| 861 | { |
| 862 | if (num != INVALID_EDITOR_NUM) |
| 863 | { |
| 864 | m_free_editor_nums.push_back(num); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | void AssemblerWidget::UpdateTabText(AsmEditor* editor) |
| 869 | { |
| 870 | int tab_idx = 0; |
| 871 | for (; tab_idx < m_asm_tabs->count(); tab_idx++) |
| 872 | { |
| 873 | if (m_asm_tabs->widget(tab_idx) == editor) |
| 874 | { |
| 875 | break; |
| 876 | } |
| 877 | } |
| 878 | ASSERT(tab_idx < m_asm_tabs->count()); |
| 879 | |
| 880 | m_asm_tabs->setTabText(tab_idx, TabTextForEditor(editor, true)); |
| 881 | } |
| 882 | |
| 883 | void AssemblerWidget::DisambiguateTabTitles(AsmEditor* new_tab) |
| 884 | { |
| 885 | for (int i = 0; i < m_asm_tabs->count(); i++) |
| 886 | { |
| 887 | AsmEditor* check = GetEditor(i); |
| 888 | if (check->IsAmbiguous()) |
| 889 | { |
| 890 | // Could group all editors with matching titles in a linked list |
| 891 | // but tracking that nicely without dangling pointers feels messy |
| 892 | bool still_ambiguous = false; |
| 893 | for (int j = 0; j < m_asm_tabs->count(); j++) |
| 894 | { |
| 895 | AsmEditor* against = GetEditor(j); |
| 896 | if (j != i && check->FileName() == against->FileName()) |
| 897 | { |
| 898 | if (!against->IsAmbiguous()) |
| 899 | { |
| 900 | against->SetAmbiguous(true); |
| 901 | UpdateTabText(against); |
| 902 | } |
| 903 | still_ambiguous = true; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | if (!still_ambiguous) |
| 908 | { |
| 909 | check->SetAmbiguous(false); |
| 910 | UpdateTabText(check); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (new_tab != nullptr) |
| 916 | { |
| 917 | bool is_ambiguous = false; |
| 918 | for (int i = 0; i < m_asm_tabs->count(); i++) |
| 919 | { |
| 920 | AsmEditor* against = GetEditor(i); |
| 921 | if (new_tab != against && against->FileName() == new_tab->FileName()) |
| 922 | { |
| 923 | against->SetAmbiguous(true); |
| 924 | UpdateTabText(against); |
| 925 | is_ambiguous = true; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | if (is_ambiguous) |
| 930 | { |
| 931 | new_tab->SetAmbiguous(true); |
| 932 | UpdateTabText(new_tab); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | void AssemblerWidget::UpdateIcons() |
| 938 | { |
| 939 | m_new->setIcon(Resources::GetThemeIcon("assembler_new")); |
| 940 | m_open->setIcon(Resources::GetThemeIcon("assembler_openasm")); |
| 941 | m_save->setIcon(Resources::GetThemeIcon("assembler_save")); |
| 942 | m_assemble->setIcon(Resources::GetThemeIcon("assembler_assemble")); |
| 943 | m_inject->setIcon(Resources::GetThemeIcon("assembler_inject")); |
| 944 | m_copy_output_button->setIcon(Resources::GetThemeIcon("assembler_clipboard")); |
| 945 | } |
| 946 | |
| 947 | void AssemblerWidget::ZoomAllEditors(int amount) |
| 948 | { |
| 949 | if (amount != 0) |
| 950 | { |
| 951 | m_net_zoom_delta += amount; |
| 952 | for (int i = 0; i < m_asm_tabs->count(); i++) |
| 953 | { |
| 954 | GetEditor(i)->Zoom(amount); |
| 955 | } |
| 956 | } |
| 957 | } |