| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/AssembleInstructionDialog.h" |
| 5 | |
| 6 | #include <QDialogButtonBox> |
| 7 | #include <QFontDatabase> |
| 8 | #include <QLabel> |
| 9 | #include <QLineEdit> |
| 10 | #include <QPushButton> |
| 11 | #include <QVBoxLayout> |
| 12 | |
| 13 | #include "Common/Assembler/GekkoAssembler.h" |
| 14 | #include "Common/StringUtil.h" |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | QString HtmlFormatErrorLoc(const Common::GekkoAssembler::AssemblerError& err) |
| 19 | { |
| 20 | return QObject::tr("<span style=\"color: red; font-weight: bold\">Error</span> on line %1 col %2") |
| 21 | .arg(err.line + 1) |
| 22 | .arg(err.col + 1); |
| 23 | } |
| 24 | |
| 25 | QString HtmlFormatErrorLine(const Common::GekkoAssembler::AssemblerError& err) |
| 26 | { |
| 27 | const QString line_pre_error = |
| 28 | QString::fromStdString(std::string(err.error_line.substr(0, err.col))).toHtmlEscaped(); |
| 29 | const QString line_error = |
| 30 | QString::fromStdString(std::string(err.error_line.substr(err.col, err.len))).toHtmlEscaped(); |
| 31 | const QString line_post_error = |
| 32 | QString::fromStdString(std::string(err.error_line.substr(err.col + err.len))).toHtmlEscaped(); |
| 33 | |
| 34 | return QStringLiteral("%1<u><span style=\"color:red; font-weight:bold\">%2</span></u>%3") |
| 35 | .arg(line_pre_error) |
| 36 | .arg(line_error) |
| 37 | .arg(line_post_error); |
| 38 | } |
| 39 | } // namespace |
| 40 | |
| 41 | AssembleInstructionDialog::AssembleInstructionDialog(QWidget* parent, u32 address, u32 value) |
| 42 | : QDialog(parent), m_code(value), m_address(address) |
| 43 | { |
| 44 | setWindowModality(Qt::WindowModal); |
| 45 | setWindowTitle(tr("Instruction")); |
| 46 | |
| 47 | CreateWidgets(); |
| 48 | ConnectWidgets(); |
| 49 | } |
| 50 | |
| 51 | void AssembleInstructionDialog::CreateWidgets() |
| 52 | { |
| 53 | auto* layout = new QVBoxLayout; |
| 54 | |
| 55 | m_input_edit = new QLineEdit; |
| 56 | m_error_loc_label = new QLabel; |
| 57 | m_error_line_label = new QLabel; |
| 58 | m_msg_label = new QLabel(tr("No input")); |
| 59 | m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 60 | |
| 61 | m_error_line_label->setFont(QFont(QFontDatabase::systemFont(QFontDatabase::FixedFont).family())); |
| 62 | m_input_edit->setFont(QFont(QFontDatabase::systemFont(QFontDatabase::FixedFont).family())); |
| 63 | layout->addWidget(m_error_loc_label); |
| 64 | layout->addWidget(m_input_edit); |
| 65 | layout->addWidget(m_error_line_label); |
| 66 | layout->addWidget(m_msg_label); |
| 67 | layout->addWidget(m_button_box); |
| 68 | m_input_edit->setText(QStringLiteral(".4byte 0x%1").arg(m_code, 8, 16, QLatin1Char('0'))); |
| 69 | |
| 70 | setLayout(layout); |
| 71 | OnEditChanged(); |
| 72 | } |
| 73 | |
| 74 | void AssembleInstructionDialog::ConnectWidgets() |
| 75 | { |
| 76 | connect(m_button_box, &QDialogButtonBox::accepted, this, &QDialog::accept); |
| 77 | connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 78 | |
| 79 | connect(m_input_edit, &QLineEdit::textChanged, this, &AssembleInstructionDialog::OnEditChanged); |
| 80 | } |
| 81 | |
| 82 | void AssembleInstructionDialog::OnEditChanged() |
| 83 | { |
| 84 | using namespace Common::GekkoAssembler; |
| 85 | std::string line = m_input_edit->text().toStdString(); |
| 86 | Common::ToLower(&line); |
| 87 | |
| 88 | FailureOr<std::vector<CodeBlock>> asm_result = Assemble(line, m_address); |
| 89 | |
| 90 | if (IsFailure(asm_result)) |
| 91 | { |
| 92 | m_button_box->button(QDialogButtonBox::Ok)->setEnabled(false); |
| 93 | |
| 94 | const AssemblerError& failure = GetFailure(asm_result); |
| 95 | m_error_loc_label->setText(HtmlFormatErrorLoc(failure)); |
| 96 | m_error_line_label->setText(HtmlFormatErrorLine(failure)); |
| 97 | m_msg_label->setText(QString::fromStdString(failure.message).toHtmlEscaped()); |
| 98 | } |
| 99 | else if (GetT(asm_result).empty() || GetT(asm_result)[0].instructions.empty()) |
| 100 | { |
| 101 | m_button_box->button(QDialogButtonBox::Ok)->setEnabled(false); |
| 102 | |
| 103 | m_error_loc_label->setText( |
| 104 | QStringLiteral("<span style=\"color: red; font-weight: bold\">%1</span>").arg(tr("Error"))); |
| 105 | m_error_line_label->clear(); |
| 106 | m_msg_label->setText(tr("No input")); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | m_button_box->button(QDialogButtonBox::Ok)->setEnabled(true); |
| 111 | m_code = 0; |
| 112 | |
| 113 | const std::vector<u8>& block_bytes = GetT(asm_result)[0].instructions; |
| 114 | for (size_t i = 0; i < 4 && i < block_bytes.size(); i++) |
| 115 | { |
| 116 | m_code = (m_code << 8) | block_bytes[i]; |
| 117 | } |
| 118 | |
| 119 | m_error_loc_label->setText( |
| 120 | QStringLiteral("<span style=\"color: green; font-weight: bold\">%1</span>").arg(tr("OK"))); |
| 121 | m_error_line_label->clear(); |
| 122 | m_msg_label->setText(tr("Instruction: %1").arg(m_code, 8, 16, QLatin1Char('0'))); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | u32 AssembleInstructionDialog::GetCode() const |
| 127 | { |
| 128 | return m_code; |
| 129 | } |