| 1 | #include "BreakpointDialog.h" |
| 2 | #include "DebugSession.h" |
| 3 | #include "Convert.h" |
| 4 | #include <QStandardItemModel> |
| 5 | #include <memory> |
| 6 | |
| 7 | BreakpointDialog::BreakpointDialog(const MemoryLayout& ml, DebugSession* session, QWidget* parent) |
| 8 | : QDialog(parent), memLayout(ml), currentSymbol(nullptr) |
| 9 | { |
| 10 | setupUi(this); |
| 11 | |
| 12 | debugSession = session; |
| 13 | if (session) { |
| 14 | // create address completer |
| 15 | jumpCompleter = std::make_unique<QCompleter>(session->symbolTable().labelList(), this); |
| 16 | allCompleter = std::make_unique<QCompleter>(session->symbolTable().labelList(true), this); |
| 17 | jumpCompleter->setCaseSensitivity(Qt::CaseInsensitive); |
| 18 | allCompleter->setCaseSensitivity(Qt::CaseInsensitive); |
| 19 | connect(jumpCompleter.get(), qOverload<const QString&>(&QCompleter::activated), this, &BreakpointDialog::addressChanged); |
| 20 | connect(allCompleter.get(), qOverload<const QString&>(&QCompleter::activated), this, &BreakpointDialog::addressChanged); |
| 21 | } |
| 22 | |
| 23 | connect(edtAddress, &QLineEdit::textEdited, this, &BreakpointDialog::addressChanged); |
| 24 | connect(edtAddressRange, &QLineEdit::textEdited, this, &BreakpointDialog::addressChanged); |
| 25 | connect(cmbxType, qOverload<int>(&QComboBox::activated), this, &BreakpointDialog::typeChanged); |
| 26 | connect(cmbxSlot, qOverload<int>(&QComboBox::activated), this, &BreakpointDialog::slotChanged); |
| 27 | connect(cmbxSubslot, qOverload<int>(&QComboBox::activated), this, &BreakpointDialog::subslotChanged); |
| 28 | connect(cbCondition, &QCheckBox::stateChanged, this, &BreakpointDialog::hasCondition); |
| 29 | |
| 30 | typeChanged(0); |
| 31 | slotChanged(0); |
| 32 | idxSlot = idxSubSlot = 0; |
| 33 | hasCondition(0); |
| 34 | } |
| 35 | |
| 36 | Breakpoint::Type BreakpointDialog::type() const |
| 37 | { |
| 38 | return Breakpoint::Type(cmbxType->currentIndex()); |
| 39 | } |
| 40 | |
| 41 | std::optional<uint16_t> BreakpointDialog::parseInput(const QLineEdit& ed, Symbol** symbol) const { |
| 42 | if (auto value = stringToValue<uint16_t>(ed.text())) { |
| 43 | return value; |
| 44 | } |
| 45 | |
| 46 | // convert symbol to value |
| 47 | if (debugSession) { |
| 48 | if (Symbol* s = debugSession->symbolTable().getAddressSymbol(ed.text())) { |
| 49 | if (symbol) *symbol = s; |
| 50 | return s->value(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return {}; |
| 55 | } |
| 56 | |
| 57 | std::optional<AddressRange> BreakpointDialog::addressRange(Symbol** symbol) const |
| 58 | { |
| 59 | if (auto address = parseInput(*edtAddress, symbol)) { |
| 60 | if (edtAddressRange->text().isEmpty()) { |
| 61 | return AddressRange{*address}; |
| 62 | } else if (auto endAddress = parseInput(*edtAddressRange)) { |
| 63 | if (*address <= *endAddress) { |
| 64 | return AddressRange{*address, make_if(*address != *endAddress, *endAddress)}; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | Slot BreakpointDialog::slot() const |
| 72 | { |
| 73 | auto slot = make_positive_optional<uint8_t>(cmbxSlot->currentIndex() - 1); |
| 74 | auto subslot = make_positive_optional<uint8_t>(cmbxSubslot->currentIndex() - 1); |
| 75 | return Slot{slot, subslot}; |
| 76 | } |
| 77 | |
| 78 | std::optional<uint8_t> BreakpointDialog::segment() const |
| 79 | { |
| 80 | return make_positive_optional<uint8_t>(cmbxSegment->currentIndex() - 1); |
| 81 | } |
| 82 | |
| 83 | QString BreakpointDialog::condition() const |
| 84 | { |
| 85 | return (cbCondition->checkState() == Qt::Checked) ? txtCondition->toPlainText() : ""; |
| 86 | } |
| 87 | |
| 88 | void BreakpointDialog::setData(Breakpoint::Type type, std::optional<AddressRange> range, Slot slot, |
| 89 | std::optional<uint8_t> segment, QString condition) |
| 90 | { |
| 91 | // set type |
| 92 | cmbxType->setCurrentIndex(int(type)); |
| 93 | typeChanged(cmbxType->currentIndex()); |
| 94 | |
| 95 | // set address |
| 96 | edtAddress->setText(range ? hexValue(range->start): ""); |
| 97 | if (range) { |
| 98 | addressChanged(edtAddress->text()); |
| 99 | } |
| 100 | |
| 101 | // set end address |
| 102 | if (edtAddressRange->isEnabled()) { |
| 103 | edtAddressRange->setText(range && range->end ? hexValue(*range->end) : ""); |
| 104 | addressChanged(edtAddressRange->text()); |
| 105 | } |
| 106 | |
| 107 | // set primary slot |
| 108 | if (cmbxSlot->isEnabled()) { |
| 109 | cmbxSlot->setCurrentIndex(slot.ps ? *slot.ps + 1 : -1); |
| 110 | slotChanged(cmbxSlot->currentIndex()); |
| 111 | } |
| 112 | |
| 113 | // set secondary slot |
| 114 | if (cmbxSubslot->isEnabled()) { |
| 115 | cmbxSubslot->setCurrentIndex(slot.ss ? *slot.ss + 1 : -1); |
| 116 | subslotChanged(cmbxSubslot->currentIndex()); |
| 117 | } |
| 118 | |
| 119 | // set segment |
| 120 | if (cmbxSegment->isEnabled()) { |
| 121 | cmbxSegment->setCurrentIndex(segment ? *segment + 1 : -1); |
| 122 | } |
| 123 | |
| 124 | // set condition |
| 125 | condition = condition.trimmed(); |
| 126 | if (cbCondition->isChecked() == condition.isEmpty()) |
| 127 | cbCondition->setChecked(!condition.isEmpty()); |
| 128 | txtCondition->setText(condition); |
| 129 | } |
| 130 | |
| 131 | void BreakpointDialog::disableSlots() |
| 132 | { |
| 133 | cmbxSlot->setCurrentIndex(0); |
| 134 | cmbxSlot->setEnabled(false); |
| 135 | } |
| 136 | |
| 137 | void BreakpointDialog::enableSlots() |
| 138 | { |
| 139 | auto* model = qobject_cast<QStandardItemModel*>(cmbxSlot->model()); |
| 140 | cmbxSlot->setEnabled(true); |
| 141 | |
| 142 | if (currentSymbol) { |
| 143 | // enable allowed slots |
| 144 | int s = currentSymbol->validSlots(); |
| 145 | int num = 0; |
| 146 | int last = 0; |
| 147 | |
| 148 | for (int i = 4; i >= 0; i--) { |
| 149 | QModelIndex index = model->index(i, 0, cmbxSlot->rootModelIndex()); |
| 150 | if (i) { |
| 151 | bool ena = (s & (15 << (4 * (i - 1)))) != 0; |
| 152 | model->itemFromIndex(index)->setEnabled(ena); |
| 153 | if (ena) { |
| 154 | num++; |
| 155 | last = i; |
| 156 | } |
| 157 | } else { |
| 158 | model->itemFromIndex(index)->setEnabled(num == 4); |
| 159 | if (num == 4) last = 0; |
| 160 | } |
| 161 | } |
| 162 | cmbxSlot->setCurrentIndex(last); |
| 163 | } else { |
| 164 | // enable everything |
| 165 | for (int i = 0; i < 5; i++) { |
| 166 | QModelIndex index = model->index(i, 0, cmbxSlot->rootModelIndex()); |
| 167 | model->itemFromIndex(index)->setEnabled(true); |
| 168 | } |
| 169 | cmbxSlot->setCurrentIndex(idxSlot); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void BreakpointDialog::addressChanged(const QString& /*text*/) |
| 174 | { |
| 175 | // adjust controls for (in)correct values |
| 176 | auto range = addressRange(¤tSymbol); |
| 177 | QPalette pal; |
| 178 | |
| 179 | if (!range) { |
| 180 | // set line edit text to red for invalid values TODO: make configurable |
| 181 | pal.setColor(QPalette::Normal, QPalette::Text, Qt::red); |
| 182 | disableSlots(); |
| 183 | } else { |
| 184 | enableSlots(); |
| 185 | slotChanged(cmbxSlot->currentIndex()); |
| 186 | } |
| 187 | |
| 188 | edtAddress->setPalette(pal); |
| 189 | edtAddressRange->setPalette(pal); |
| 190 | okButton->setEnabled(range.has_value()); |
| 191 | } |
| 192 | |
| 193 | void BreakpointDialog::typeChanged(int s) |
| 194 | { |
| 195 | switch(s) { |
| 196 | case 1: |
| 197 | case 2: |
| 198 | lblAddress->setText(tr("Add watchpoint at memory address or range:")); |
| 199 | edtAddressRange->setVisible(true); |
| 200 | edtAddress->setCompleter(allCompleter.get()); |
| 201 | edtAddressRange->setCompleter(allCompleter.get()); |
| 202 | break; |
| 203 | case 3: |
| 204 | case 4: |
| 205 | lblAddress->setText(tr("Add watchpoint on IO port or range:")); |
| 206 | edtAddressRange->setVisible(true); |
| 207 | edtAddress->setCompleter(nullptr); |
| 208 | edtAddressRange->setCompleter(nullptr); |
| 209 | break; |
| 210 | default: |
| 211 | lblAddress->setText(tr("Add breakpoint at address:")); |
| 212 | edtAddressRange->setVisible(false); |
| 213 | edtAddress->setCompleter(jumpCompleter.get()); |
| 214 | } |
| 215 | |
| 216 | switch(s) { |
| 217 | case 1: |
| 218 | case 2: |
| 219 | lblInSlot->setText(tr("With address in")); |
| 220 | break; |
| 221 | default: |
| 222 | lblInSlot->setText(tr("With PC in")); |
| 223 | } |
| 224 | |
| 225 | if (s == 5) { |
| 226 | lblAddress->setEnabled(false); |
| 227 | edtAddress->setEnabled(false); |
| 228 | lblSlot->setEnabled(false); |
| 229 | lblSubslot->setEnabled(false); |
| 230 | lblSegment->setEnabled(false); |
| 231 | cmbxSlot->setEnabled(false); |
| 232 | cmbxSubslot->setEnabled(false); |
| 233 | cmbxSegment->setEnabled(false); |
| 234 | cbCondition->setCheckState(Qt::Checked); |
| 235 | cbCondition->setEnabled(false); |
| 236 | } else { |
| 237 | lblAddress->setEnabled(true); |
| 238 | edtAddress->setEnabled(true); |
| 239 | lblSlot->setEnabled(true); |
| 240 | lblSubslot->setEnabled(true); |
| 241 | lblSegment->setEnabled(true); |
| 242 | addressChanged(edtAddress->text()); |
| 243 | cbCondition->setEnabled(true); |
| 244 | cbCondition->setCheckState(txtCondition->toPlainText().isEmpty() ? Qt::Unchecked : Qt::Checked); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void BreakpointDialog::slotChanged(int s) |
| 249 | { |
| 250 | if (!currentSymbol) idxSlot = s; |
| 251 | if (s && memLayout.isSubslotted[s - 1]) { |
| 252 | if (currentSymbol) { |
| 253 | // enable subslots |
| 254 | int v = (currentSymbol->validSlots() >> (4 * (s - 1))) & 15; |
| 255 | int num = 0; |
| 256 | int last = 0; |
| 257 | auto* model = qobject_cast<QStandardItemModel*>(cmbxSubslot->model()); |
| 258 | for (int i = 4; i >= 0; i--) { |
| 259 | QModelIndex index = model->index(i, 0, cmbxSubslot->rootModelIndex()); |
| 260 | if (i) { |
| 261 | bool ena = (v & (1 << (i - 1))) != 0; |
| 262 | model->itemFromIndex(index)->setEnabled(ena); |
| 263 | if (ena) { |
| 264 | num++; |
| 265 | last = i; |
| 266 | } |
| 267 | } else { |
| 268 | model->itemFromIndex(index)->setEnabled(num == 4); |
| 269 | if (num == 4) last = 0; |
| 270 | } |
| 271 | } |
| 272 | cmbxSubslot->setCurrentIndex(last); |
| 273 | } else { |
| 274 | cmbxSubslot->setCurrentIndex(idxSubSlot); |
| 275 | } |
| 276 | cmbxSubslot->setEnabled(true); |
| 277 | } else { |
| 278 | cmbxSubslot->setEnabled(false); |
| 279 | cmbxSubslot->setCurrentIndex(0); |
| 280 | } |
| 281 | subslotChanged(cmbxSubslot->currentIndex()); |
| 282 | } |
| 283 | |
| 284 | void BreakpointDialog::subslotChanged(int i) |
| 285 | { |
| 286 | if (!currentSymbol) idxSubSlot = i; |
| 287 | int oldseg = cmbxSegment->currentIndex() - 1; |
| 288 | cmbxSegment->clear(); |
| 289 | cmbxSegment->addItem("any"); |
| 290 | |
| 291 | int ps = cmbxSlot->currentIndex() - 1; |
| 292 | int ss = i - 1; |
| 293 | |
| 294 | if (ps >= 0 && !memLayout.isSubslotted[ps]) ss = 0; |
| 295 | |
| 296 | int mapsize; |
| 297 | if (ps < 0 || ss < 0 || memLayout.mapperSize[ps][ss] == 0) { |
| 298 | auto range = addressRange(); |
| 299 | |
| 300 | // no memory mapper, maybe rom mapper? |
| 301 | if (range && memLayout.romBlock[((range->start & 0xE000) >> 13)] >= 0) { |
| 302 | mapsize = 256; // TODO: determine real size |
| 303 | } else { |
| 304 | cmbxSegment->setEnabled(false); |
| 305 | cmbxSegment->setCurrentIndex(0); |
| 306 | return; |
| 307 | } |
| 308 | } else |
| 309 | mapsize = memLayout.mapperSize[ps][ss]; |
| 310 | |
| 311 | for (int s = 0; s < mapsize; ++s) { |
| 312 | cmbxSegment->addItem(QString("%1").arg(s)); |
| 313 | } |
| 314 | cmbxSegment->setEnabled(true); |
| 315 | if (oldseg >= 0 && oldseg < mapsize) { |
| 316 | cmbxSegment->setCurrentIndex(oldseg + 1); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void BreakpointDialog::hasCondition(int state) |
| 321 | { |
| 322 | if (state == Qt::Checked) { |
| 323 | txtCondition->setVisible(true); |
| 324 | layout()->setSizeConstraint(QLayout::SetDefaultConstraint); |
| 325 | resize(width(), conditionHeight); |
| 326 | } else { |
| 327 | conditionHeight = height(); |
| 328 | txtCondition->setVisible(false); |
| 329 | resize(width(), sizeHint().height()); |
| 330 | layout()->setSizeConstraint(QLayout::SetMaximumSize); |
| 331 | } |
| 332 | } |