| 1 | #include "DisasmViewer.h" |
| 2 | #include "OpenMSXConnection.h" |
| 3 | #include "CommClient.h" |
| 4 | #include "DebuggerData.h" |
| 5 | #include "Settings.h" |
| 6 | #include <QPaintEvent> |
| 7 | #include <QPainter> |
| 8 | #include <QStyleOptionFocusRect> |
| 9 | #include <QScrollBar> |
| 10 | #include <QWheelEvent> |
| 11 | #include <QApplication> |
| 12 | #include <QClipboard> |
| 13 | #include <QDesktopWidget> |
| 14 | #include <algorithm> |
| 15 | #include <cmath> |
| 16 | #include <cassert> |
| 17 | #include <regex> |
| 18 | |
| 19 | class CommMemoryRequest : public ReadDebugBlockCommand |
| 20 | { |
| 21 | public: |
| 22 | CommMemoryRequest(uint16_t offset_, uint16_t size_, unsigned char* target, |
| 23 | DisasmViewer& viewer_) |
| 24 | : ReadDebugBlockCommand("memory", offset_, size_, target) |
| 25 | , offset(offset_), size(size_) |
| 26 | , viewer(viewer_) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | void replyOk(const QString& message) override |
| 31 | { |
| 32 | copyData(message); |
| 33 | viewer.memoryUpdated(this); |
| 34 | } |
| 35 | |
| 36 | void cancel() override |
| 37 | { |
| 38 | viewer.updateCancelled(this); |
| 39 | } |
| 40 | |
| 41 | // TODO Refactor: public members are ugly |
| 42 | unsigned offset; |
| 43 | unsigned size; |
| 44 | int address; |
| 45 | int line; |
| 46 | int method; |
| 47 | |
| 48 | private: |
| 49 | DisasmViewer& viewer; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | |
| 54 | DisasmViewer::DisasmViewer(QWidget* parent) |
| 55 | : QFrame(parent) |
| 56 | , wheelRemainder(0) |
| 57 | { |
| 58 | setFrameStyle(WinPanel | Sunken); |
| 59 | setFocusPolicy(Qt::StrongFocus); |
| 60 | setBackgroundRole(QPalette::Base); |
| 61 | setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred)); |
| 62 | breakMarker = QPixmap(":/icons/breakpoint.png"); |
| 63 | watchMarker = QPixmap(":/icons/watchpoint.png"); |
| 64 | pcMarker = QPixmap(":/icons/pcarrow.png"); |
| 65 | |
| 66 | memory = nullptr; |
| 67 | cursorAddr = 0; |
| 68 | cursorLine = 0; |
| 69 | visibleLines = 0; |
| 70 | programAddr = 0xFFFF; |
| 71 | waitingForData = false; |
| 72 | nextRequest = nullptr; |
| 73 | |
| 74 | scrollBar = new QScrollBar(Qt::Vertical, this); |
| 75 | scrollBar->setMinimum(0); |
| 76 | scrollBar->setMaximum(0xFFFF); |
| 77 | scrollBar->setSingleStep(0); |
| 78 | scrollBar->setPageStep(0); |
| 79 | |
| 80 | updateLayout(); |
| 81 | |
| 82 | // manual scrollbar handling routines (real size of the data is not known) |
| 83 | connect(scrollBar, &QScrollBar::actionTriggered, |
| 84 | this, &DisasmViewer::scrollBarAction); |
| 85 | connect(scrollBar, &QScrollBar::valueChanged, |
| 86 | this, &DisasmViewer::scrollBarChanged); |
| 87 | } |
| 88 | |
| 89 | QSize DisasmViewer::sizeHint() const |
| 90 | { |
| 91 | return {xMnemArg + xMCode[0], 20 * codeFontHeight}; |
| 92 | } |
| 93 | |
| 94 | void DisasmViewer::resizeEvent(QResizeEvent* e) |
| 95 | { |
| 96 | QFrame::resizeEvent(e); |
| 97 | |
| 98 | scrollBar->setGeometry(width() - frameR, frameT, |
| 99 | scrollBar->sizeHint().width(), |
| 100 | height() - frameT - frameB); |
| 101 | |
| 102 | // reset the address in order to trigger a check on the disasmLines |
| 103 | if (!waitingForData) { |
| 104 | setAddress(scrollBar->value()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void DisasmViewer::updateLayout() |
| 109 | { |
| 110 | frameL = frameT = frameB = frameWidth(); |
| 111 | frameR = frameL + scrollBar->sizeHint().width(); |
| 112 | setMinimumHeight(frameT + 5 * fontMetrics().height() + frameB); |
| 113 | |
| 114 | // set font sizes |
| 115 | Settings& s = Settings::get(); |
| 116 | QFontMetrics lfm(s.font(Settings::LABEL_FONT)); |
| 117 | QFontMetrics cfm(s.font(Settings::CODE_FONT)); |
| 118 | labelFontHeight = lfm.height(); |
| 119 | labelFontAscent = lfm.ascent(); |
| 120 | codeFontHeight = cfm.height(); |
| 121 | codeFontAscent = cfm.ascent(); |
| 122 | |
| 123 | // calculate layout locations |
| 124 | int charWidth = cfm.horizontalAdvance("0"); |
| 125 | xAddr = frameL + 40; |
| 126 | xMCode[0] = xAddr + 6 * charWidth; |
| 127 | xMCode[1] = xMCode[0] + 3 * charWidth; |
| 128 | xMCode[2] = xMCode[1] + 3 * charWidth; |
| 129 | xMCode[3] = xMCode[2] + 3 * charWidth; |
| 130 | xMnem = xMCode[3] + 4 * charWidth; |
| 131 | xMnemArg = xMnem + 7 * charWidth; |
| 132 | |
| 133 | setMinimumSize(xMCode[0], 2*codeFontHeight); |
| 134 | setMaximumSize(QApplication::desktop()->width(), |
| 135 | QApplication::desktop()->height()); |
| 136 | update(); |
| 137 | } |
| 138 | |
| 139 | void DisasmViewer::requestMemory(uint16_t start, uint16_t end, uint16_t addr, int infoLine, int method) |
| 140 | { |
| 141 | auto* req = new CommMemoryRequest(start, end - start, &memory[start], *this); |
| 142 | req->address = addr; |
| 143 | req->line = infoLine; |
| 144 | req->method = method; |
| 145 | |
| 146 | if (waitingForData) { |
| 147 | delete nextRequest; |
| 148 | nextRequest = req; |
| 149 | } else { |
| 150 | waitingForData = true; |
| 151 | CommClient::instance().sendCommand(req); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void DisasmViewer::refresh() |
| 156 | { |
| 157 | int start = disasmLines.front().addr; |
| 158 | int end = disasmLines.back().addr + disasmLines.back().numBytes; |
| 159 | int infoLine = disasmLines[disasmTopLine].infoLine; |
| 160 | |
| 161 | requestMemory(start, end, disasmLines[disasmTopLine].addr, infoLine, TopAlways); |
| 162 | } |
| 163 | |
| 164 | void DisasmViewer::paintEvent(QPaintEvent* e) |
| 165 | { |
| 166 | // call parent for drawing the actual frame |
| 167 | QFrame::paintEvent(e); |
| 168 | |
| 169 | QPainter p(this); |
| 170 | |
| 171 | // calc and set drawing bounds |
| 172 | QRect r(e->rect()); |
| 173 | if (r.left() < frameL) r.setLeft(frameL); |
| 174 | if (r.top() < frameT) r.setTop(frameT); |
| 175 | if (r.right() > width() - frameR - 1) r.setRight (width() - frameR - 1); |
| 176 | if (r.bottom() > height() - frameB - 1) r.setBottom(height() - frameB - 1); |
| 177 | p.setClipRect(r); |
| 178 | |
| 179 | // draw background |
| 180 | p.fillRect(frameL + 32, frameT, width() - 32 - frameL - frameR, |
| 181 | height() - frameT - frameB, palette().color(QPalette::Base)); |
| 182 | |
| 183 | // calculate lines while drawing |
| 184 | visibleLines = 0; |
| 185 | int y = frameT; |
| 186 | |
| 187 | QString hexStr; |
| 188 | const DisasmRow* row; |
| 189 | bool displayDisasm = memory != nullptr && isEnabled(); |
| 190 | |
| 191 | Settings& s = Settings::get(); |
| 192 | p.setFont(s.font(Settings::CODE_FONT)); |
| 193 | while (y < height() - frameB) { |
| 194 | // fetch the data for this line |
| 195 | row = displayDisasm |
| 196 | ? &disasmLines[disasmTopLine+visibleLines] |
| 197 | : &DISABLED_ROW; |
| 198 | int h, a; |
| 199 | switch (row->rowType) { |
| 200 | case DisasmRow::LABEL: |
| 201 | h = labelFontHeight; |
| 202 | a = labelFontAscent; |
| 203 | break; |
| 204 | case DisasmRow::INSTRUCTION: |
| 205 | h = codeFontHeight; |
| 206 | a = codeFontAscent; |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | // draw cursor line |
| 211 | bool isCursorLine = cursorAddr >= row->addr && cursorAddr < row->addr+row->numBytes && |
| 212 | row->infoLine == cursorLine; |
| 213 | if (isCursorLine) { |
| 214 | cursorAddr = row->addr; |
| 215 | p.fillRect(frameL + 32, y, width() - 32 - frameL - frameR, h, |
| 216 | palette().color(QPalette::Highlight)); |
| 217 | if (hasFocus()) { |
| 218 | QStyleOptionFocusRect so; |
| 219 | so.backgroundColor = palette().color(QPalette::Highlight); |
| 220 | so.rect = QRect(frameL + 32, y, width() - 32 - frameL - frameR, h); |
| 221 | style()->drawPrimitive(QStyle::PE_FrameFocusRect, &so, &p, this); |
| 222 | } |
| 223 | p.setPen(palette().color(QPalette::HighlightedText)); |
| 224 | } |
| 225 | |
| 226 | // if there is a label here, draw the label, otherwise code |
| 227 | if (row->rowType == DisasmRow::LABEL) { |
| 228 | // draw label |
| 229 | hexStr = QString("%1:").arg(row->instr.c_str()); |
| 230 | p.setFont(s.font(Settings::LABEL_FONT)); |
| 231 | if (!isCursorLine) { |
| 232 | p.setPen(s.fontColor(Settings::LABEL_FONT)); |
| 233 | } |
| 234 | p.drawText(xAddr, y + a, hexStr); |
| 235 | p.setFont(s.font(Settings::CODE_FONT)); |
| 236 | } else { |
| 237 | // draw code line |
| 238 | // default to text pen |
| 239 | if (!isCursorLine) { |
| 240 | p.setPen(s.fontColor(Settings::CODE_FONT)); |
| 241 | } |
| 242 | |
| 243 | // draw breakpoint marker |
| 244 | if (row->infoLine == 0) { |
| 245 | if (breakpoints->isBreakpoint(row->addr)) { |
| 246 | p.drawPixmap(frameL + 2, y + h / 2 -5, breakMarker); |
| 247 | if (!isCursorLine) { |
| 248 | p.fillRect(frameL + 32, y, width() - 32 - frameL - frameR, h, |
| 249 | Qt::red); |
| 250 | p.setPen(Qt::white); |
| 251 | } |
| 252 | } else if (breakpoints->isWatchpoint(row->addr)) { |
| 253 | p.drawPixmap(frameL + 2, y + h / 2 -5, watchMarker); |
| 254 | } |
| 255 | } |
| 256 | // draw PC marker |
| 257 | if (row->addr == programAddr && row->infoLine == 0) { |
| 258 | p.drawPixmap(frameL + 18, y + h / 2 - 5, pcMarker); |
| 259 | } |
| 260 | |
| 261 | // print the address |
| 262 | hexStr = QString("%1").arg(row->addr, 4, 16, QChar('0')).toUpper(); |
| 263 | p.drawText(xAddr, y + a, hexStr); |
| 264 | |
| 265 | // print 1 to 4 bytes |
| 266 | for (int j = 0; j < row->numBytes; ++j) { |
| 267 | hexStr = QString("%1").arg(displayDisasm ? memory[row->addr + j] : 0, 2, 16, QChar('0')).toUpper(); |
| 268 | p.drawText(xMCode[j], y + a, hexStr); |
| 269 | } |
| 270 | |
| 271 | // print the instruction and arguments |
| 272 | p.drawText(xMnem, y + a, row->instr.substr(0, 7).c_str()); |
| 273 | p.drawText(xMnemArg, y + a, row->instr.substr(7 ).c_str()); |
| 274 | } |
| 275 | // next line |
| 276 | y += h; |
| 277 | ++visibleLines; |
| 278 | // check for end of data |
| 279 | if (disasmTopLine+visibleLines == int(disasmLines.size())) break; |
| 280 | } |
| 281 | partialBottomLine = y > height() - frameB; |
| 282 | visibleLines -= partialBottomLine; |
| 283 | } |
| 284 | |
| 285 | void DisasmViewer::copyCodeToClipboard() const |
| 286 | { |
| 287 | if (!memory || !isEnabled()) return; |
| 288 | |
| 289 | QClipboard *clipboard = QApplication::clipboard(); |
| 290 | QString buffer; |
| 291 | |
| 292 | // calculate lines while writing |
| 293 | int maxY = height() - frameB; |
| 294 | int maxLine = int(disasmLines.size()); |
| 295 | int y = frameT; |
| 296 | |
| 297 | for (int currentLine = disasmTopLine; |
| 298 | (y < maxY) && (currentLine < maxLine); |
| 299 | ++currentLine) { |
| 300 | const DisasmRow* row = &disasmLines[currentLine]; |
| 301 | switch (row->rowType) { |
| 302 | case DisasmRow::INSTRUCTION: |
| 303 | buffer += QString("%1%2\n") |
| 304 | .arg(row->instr.substr(0, 7).c_str()) |
| 305 | .arg(row->instr.substr(7 ).c_str()); |
| 306 | break; |
| 307 | case DisasmRow::LABEL: |
| 308 | buffer += QString("%1:\n").arg(row->instr.c_str()); |
| 309 | break; |
| 310 | } |
| 311 | // next line |
| 312 | y += row->rowType == DisasmRow::INSTRUCTION ? codeFontHeight : labelFontHeight; |
| 313 | } |
| 314 | |
| 315 | clipboard->setText(buffer, QClipboard::Clipboard); |
| 316 | } |
| 317 | |
| 318 | void DisasmViewer::setCursorAddress(uint16_t addr, int infoLine, int method) |
| 319 | { |
| 320 | cursorAddr = addr; |
| 321 | cursorLine = 0; |
| 322 | setAddress(addr, infoLine, method); |
| 323 | } |
| 324 | |
| 325 | int DisasmViewer::findPosition(uint16_t addr, int infoLine, int method) |
| 326 | { |
| 327 | if (int line = findDisasmLine(addr, infoLine); line >= 0) { |
| 328 | int dt, db; |
| 329 | |
| 330 | switch (method) { |
| 331 | case Top: |
| 332 | case Middle: |
| 333 | case Bottom: |
| 334 | case Closest: |
| 335 | dt = db = 10; |
| 336 | break; |
| 337 | case TopAlways: |
| 338 | dt = 10; |
| 339 | db = visibleLines + 10; |
| 340 | break; |
| 341 | case MiddleAlways: |
| 342 | dt = db = 10 + visibleLines / 2; |
| 343 | break; |
| 344 | case BottomAlways: |
| 345 | dt = 10 + visibleLines; |
| 346 | db = 10; |
| 347 | break; |
| 348 | default: |
| 349 | assert(false); |
| 350 | dt = db = 0; // avoid warning |
| 351 | } |
| 352 | |
| 353 | if ((line > dt || disasmLines[0].addr == 0) && |
| 354 | (line < int(disasmLines.size()) - db || |
| 355 | disasmLines.back().addr + disasmLines.back().numBytes > 0xFFFF)) { |
| 356 | return line; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | void DisasmViewer::setAddress(uint16_t addr, int infoLine, int method) |
| 364 | { |
| 365 | if (method != Reload) { |
| 366 | int line = findPosition(addr, infoLine, method); |
| 367 | |
| 368 | if (line >= 0) { |
| 369 | // line is within existing disasm'd data. Find where to put it. |
| 370 | if (method == Top || method == TopAlways || (method == Closest && line < disasmTopLine)) { |
| 371 | // Move line to top |
| 372 | disasmTopLine = line; |
| 373 | } else if (method == Bottom || method == BottomAlways || |
| 374 | (method == Closest && line >= disasmTopLine + visibleLines)) { |
| 375 | // Move line to bottom |
| 376 | disasmTopLine = line - visibleLines + 1; |
| 377 | } else if (method == MiddleAlways || |
| 378 | (method == Middle && (line < disasmTopLine || line >= disasmTopLine + visibleLines))) { |
| 379 | // Move line to middle |
| 380 | disasmTopLine = line - visibleLines / 2; |
| 381 | } |
| 382 | disasmTopLine = std::min( |
| 383 | disasmTopLine, int(disasmLines.size()) - visibleLines); |
| 384 | disasmTopLine = std::max(disasmTopLine, 0); |
| 385 | update(); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (method == Closest) { |
| 390 | if (addr < disasmLines[disasmTopLine].addr) { |
| 391 | method = Top; |
| 392 | } else { |
| 393 | method = Bottom; |
| 394 | } |
| 395 | } |
| 396 | } else { |
| 397 | // cursor always on the middle when on break state anyway. |
| 398 | method = MiddleAlways; |
| 399 | } |
| 400 | |
| 401 | // The requested address it outside the pre-disassembled bounds. |
| 402 | // This means that a new block of memory must be transfered from |
| 403 | // openMSX and disassembled. |
| 404 | |
| 405 | // determine disasm bounds |
| 406 | int disasmStart; |
| 407 | int disasmEnd; |
| 408 | int extra = 4 * (visibleLines > 9 ? visibleLines+partialBottomLine : 10); |
| 409 | switch (method) { |
| 410 | case Middle: |
| 411 | case MiddleAlways: |
| 412 | disasmStart = addr - 3 * extra / 2; |
| 413 | disasmEnd = addr + 3 * extra / 2; |
| 414 | break; |
| 415 | case Bottom: |
| 416 | case BottomAlways: |
| 417 | disasmStart = addr - 2 * extra; |
| 418 | disasmEnd = addr + extra; |
| 419 | break; |
| 420 | default: |
| 421 | disasmStart = addr - extra; |
| 422 | disasmEnd = addr + 2 * extra; |
| 423 | } |
| 424 | disasmStart = std::max(disasmStart, 0); |
| 425 | disasmEnd = std::min(disasmEnd, 0xFFFF); |
| 426 | |
| 427 | requestMemory(disasmStart, disasmEnd + 1, addr, infoLine, method); |
| 428 | } |
| 429 | |
| 430 | void DisasmViewer::memoryUpdated(CommMemoryRequest* req) |
| 431 | { |
| 432 | // disassemble the newly received memory |
| 433 | dasm(memory, req->offset, req->offset + req->size - 1, disasmLines, |
| 434 | memLayout, symTable, programAddr); |
| 435 | |
| 436 | // locate the requested line |
| 437 | disasmTopLine = findDisasmLine(req->address, req->line); |
| 438 | |
| 439 | switch (req->method) { |
| 440 | case Middle: |
| 441 | case MiddleAlways: |
| 442 | disasmTopLine -= visibleLines / 2; |
| 443 | break; |
| 444 | case Bottom: |
| 445 | case BottomAlways: |
| 446 | disasmTopLine -= visibleLines - 1; |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | disasmTopLine = std::min(disasmTopLine, |
| 451 | int(disasmLines.size()) - visibleLines); |
| 452 | disasmTopLine = std::max(disasmTopLine, 0); |
| 453 | |
| 454 | updateCancelled(req); |
| 455 | |
| 456 | // sync the scrollbar with the actual address reached |
| 457 | if (!waitingForData) { |
| 458 | // set the slider with without the signal |
| 459 | disconnect(scrollBar, &QScrollBar::valueChanged, |
| 460 | this, &DisasmViewer::scrollBarChanged); |
| 461 | scrollBar->setSliderPosition(disasmLines[disasmTopLine].addr); |
| 462 | connect(scrollBar, &QScrollBar::valueChanged, |
| 463 | this, &DisasmViewer::scrollBarChanged); |
| 464 | // set the line |
| 465 | setAddress(disasmLines[disasmTopLine].addr, |
| 466 | disasmLines[disasmTopLine].infoLine); |
| 467 | update(); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | void DisasmViewer::updateCancelled(CommMemoryRequest* req) |
| 472 | { |
| 473 | delete req; |
| 474 | if (nextRequest) { |
| 475 | CommClient::instance().sendCommand(nextRequest); |
| 476 | nextRequest = nullptr; |
| 477 | } else { |
| 478 | waitingForData = false; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | uint16_t DisasmViewer::cursorAddress() const |
| 483 | { |
| 484 | return cursorAddr; |
| 485 | } |
| 486 | |
| 487 | uint16_t DisasmViewer::programCounter() const |
| 488 | { |
| 489 | return programAddr; |
| 490 | } |
| 491 | |
| 492 | void DisasmViewer::setProgramCounter(uint16_t pc, bool reload) |
| 493 | { |
| 494 | cursorAddr = pc; |
| 495 | programAddr = pc; |
| 496 | setAddress(pc, 0, reload ? Reload : MiddleAlways); |
| 497 | } |
| 498 | |
| 499 | int DisasmViewer::findDisasmLine(uint16_t lineAddr, int infoLine) |
| 500 | { |
| 501 | int line = disasmLines.size() - 1; |
| 502 | while (line >= 0) { |
| 503 | if (lineAddr >= disasmLines[line].addr) { |
| 504 | if (infoLine == 0) { |
| 505 | return line; |
| 506 | } |
| 507 | if (infoLine == LAST_INFO_LINE) { |
| 508 | return line - 1; |
| 509 | } |
| 510 | while (disasmLines[line].infoLine != infoLine && disasmLines[line].addr == lineAddr) { |
| 511 | line--; |
| 512 | if (line < 0) return -1; |
| 513 | } |
| 514 | return line; |
| 515 | } |
| 516 | line--; |
| 517 | } |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | void DisasmViewer::scrollBarAction(int action) |
| 522 | { |
| 523 | switch (action) { |
| 524 | case QScrollBar::SliderSingleStepAdd: |
| 525 | setAddress(disasmLines[disasmTopLine + 1].addr, |
| 526 | disasmLines[disasmTopLine + 1].infoLine, |
| 527 | TopAlways); |
| 528 | break; |
| 529 | case QScrollBar::SliderSingleStepSub: |
| 530 | if (disasmTopLine > 0) { |
| 531 | setAddress(disasmLines[disasmTopLine - 1].addr, |
| 532 | disasmLines[disasmTopLine - 1].infoLine, |
| 533 | TopAlways); |
| 534 | } |
| 535 | break; |
| 536 | case QScrollBar::SliderPageStepAdd: { |
| 537 | int line = disasmTopLine + visibleLines + partialBottomLine - 1; |
| 538 | if (line < int(disasmLines.size())) { |
| 539 | setAddress(disasmLines[line].addr, |
| 540 | disasmLines[line].infoLine, |
| 541 | TopAlways); |
| 542 | } |
| 543 | break; |
| 544 | } |
| 545 | case QScrollBar::SliderPageStepSub: |
| 546 | if (disasmTopLine > 0) { |
| 547 | setAddress(disasmLines[disasmTopLine - 1].addr, |
| 548 | disasmLines[disasmTopLine - 1].infoLine, |
| 549 | BottomAlways); |
| 550 | } |
| 551 | break; |
| 552 | default: |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // moving the slider is handled separately because |
| 558 | // the SliderMoved action won't catch the extreme values |
| 559 | void DisasmViewer::scrollBarChanged(int value) |
| 560 | { |
| 561 | setAddress(value, FIRST_INFO_LINE, TopAlways); |
| 562 | } |
| 563 | |
| 564 | void DisasmViewer::setMemory(unsigned char* memPtr) |
| 565 | { |
| 566 | memory = memPtr; |
| 567 | // init disasmLines |
| 568 | DisasmRow newRow; |
| 569 | newRow.rowType = DisasmRow::INSTRUCTION; |
| 570 | newRow.numBytes = 1; |
| 571 | newRow.infoLine = 0; |
| 572 | newRow.instr = "nop"; |
| 573 | newRow.instr.resize(8, ' '); |
| 574 | for (int i = 0; i < 150; ++i) { |
| 575 | newRow.addr = i; |
| 576 | disasmLines.push_back(newRow); |
| 577 | } |
| 578 | disasmTopLine = 50; |
| 579 | } |
| 580 | |
| 581 | void DisasmViewer::setBreakpoints(Breakpoints* bps) |
| 582 | { |
| 583 | breakpoints = bps; |
| 584 | } |
| 585 | |
| 586 | void DisasmViewer::setMemoryLayout(MemoryLayout* ml) |
| 587 | { |
| 588 | memLayout = ml; |
| 589 | } |
| 590 | |
| 591 | void DisasmViewer::setSymbolTable(SymbolTable* st) |
| 592 | { |
| 593 | symTable = st; |
| 594 | } |
| 595 | |
| 596 | void DisasmViewer::keyPressEvent(QKeyEvent* e) |
| 597 | { |
| 598 | switch (e->key()) { |
| 599 | case Qt::Key_Up: { |
| 600 | int line = findDisasmLine(cursorAddr, cursorLine); |
| 601 | if (line > 0) { |
| 602 | cursorAddr = disasmLines[line - 1].addr; |
| 603 | cursorLine = disasmLines[line - 1].infoLine; |
| 604 | } |
| 605 | setAddress(cursorAddr, cursorLine, Closest); |
| 606 | e->accept(); |
| 607 | break; |
| 608 | } |
| 609 | case Qt::Key_Down: { |
| 610 | int line = findDisasmLine(cursorAddr, cursorLine); |
| 611 | if (line >= 0 && line < int(disasmLines.size()) - 1) { |
| 612 | cursorAddr = disasmLines[line + 1].addr; |
| 613 | cursorLine = disasmLines[line + 1].infoLine; |
| 614 | } |
| 615 | setAddress(cursorAddr, cursorLine, Closest); |
| 616 | e->accept(); |
| 617 | break; |
| 618 | } |
| 619 | case Qt::Key_PageUp: { |
| 620 | int line = findDisasmLine(cursorAddr, cursorLine); |
| 621 | if (line >= disasmTopLine && line < disasmTopLine+visibleLines) { |
| 622 | line -= visibleLines; |
| 623 | if (line >= 0) { |
| 624 | cursorAddr = disasmLines[line].addr; |
| 625 | cursorLine = disasmLines[line].infoLine; |
| 626 | setAddress(disasmLines[disasmTopLine - 1].addr, |
| 627 | disasmLines[disasmTopLine - 1].infoLine, |
| 628 | BottomAlways); |
| 629 | } else { |
| 630 | setCursorAddress(disasmLines[0].addr, |
| 631 | disasmLines[0].infoLine, |
| 632 | Closest); |
| 633 | } |
| 634 | } else |
| 635 | setAddress(cursorAddr, cursorLine, Closest); |
| 636 | e->accept(); |
| 637 | break; |
| 638 | } |
| 639 | case Qt::Key_PageDown: { |
| 640 | int line = findDisasmLine(cursorAddr, cursorLine); |
| 641 | if (line >= disasmTopLine && line < disasmTopLine+visibleLines) { |
| 642 | line += visibleLines; |
| 643 | if (line < int(disasmLines.size())) { |
| 644 | cursorAddr = disasmLines[line].addr; |
| 645 | cursorLine = disasmLines[line].infoLine; |
| 646 | line = disasmTopLine + visibleLines + partialBottomLine - 1; |
| 647 | line = std::min(line, int(disasmLines.size())); |
| 648 | setAddress(disasmLines[line].addr, |
| 649 | disasmLines[line].infoLine, |
| 650 | TopAlways); |
| 651 | } else { |
| 652 | setCursorAddress(disasmLines.back().addr, |
| 653 | disasmLines.back().infoLine, |
| 654 | Closest); |
| 655 | } |
| 656 | } else |
| 657 | setAddress(cursorAddr, cursorLine, Closest); |
| 658 | e->accept(); |
| 659 | break; |
| 660 | } |
| 661 | case Qt::Key_Home: { |
| 662 | setCursorAddress(0, 0, Middle); |
| 663 | e->accept(); |
| 664 | break; |
| 665 | } |
| 666 | case Qt::Key_End: { |
| 667 | setCursorAddress(0xffff, 0, Middle); |
| 668 | e->accept(); |
| 669 | break; |
| 670 | } |
| 671 | case Qt::Key_Right: |
| 672 | case Qt::Key_Return: { |
| 673 | int line = findDisasmLine(cursorAddr, cursorLine); |
| 674 | if (line >= 0 && line < int(disasmLines.size())) { |
| 675 | int naddr = INT_MAX; |
| 676 | const DisasmRow &row = disasmLines[line]; |
| 677 | std::string instr = row.instr; |
| 678 | std::regex re_absolute("(call|jp)\\ .*"); |
| 679 | std::regex re_relative("(djnz|jr)\\ .*"); |
| 680 | if (std::regex_match(instr, re_absolute)) |
| 681 | naddr = memory[row.addr + 1] + memory[row.addr + 2] * 256; |
| 682 | else if (std::regex_match(instr, re_relative)) |
| 683 | naddr = (row.addr + 2 + (signed char)memory[row.addr + 1]) & 0xFFFF; |
| 684 | if (naddr != INT_MAX) { |
| 685 | jumpStack.push_back(cursorAddr); |
| 686 | setCursorAddress(naddr, 0, Middle); |
| 687 | } |
| 688 | } |
| 689 | e->accept(); |
| 690 | break; |
| 691 | } |
| 692 | case Qt::Key_Left: |
| 693 | case Qt::Key_Backspace: { |
| 694 | if (!jumpStack.empty()) { |
| 695 | int addr = jumpStack.takeLast(); |
| 696 | setCursorAddress(addr, 0, Middle); |
| 697 | } |
| 698 | e->accept(); |
| 699 | break; |
| 700 | } |
| 701 | default: |
| 702 | QFrame::keyReleaseEvent(e); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | void DisasmViewer::wheelEvent(QWheelEvent* e) |
| 707 | { |
| 708 | wheelRemainder += e->angleDelta().y(); |
| 709 | const int delta = wheelRemainder / 40; |
| 710 | wheelRemainder %= 40; |
| 711 | if (delta) { |
| 712 | int line = std::max(0, disasmTopLine - delta); |
| 713 | setAddress(disasmLines[line].addr, disasmLines[line].infoLine, TopAlways); |
| 714 | } |
| 715 | e->accept(); |
| 716 | } |
| 717 | |
| 718 | void DisasmViewer::mousePressEvent(QMouseEvent* e) |
| 719 | { |
| 720 | if (!(e->button() == Qt::LeftButton)) { |
| 721 | QFrame::mousePressEvent(e); |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | if (e->x() >= frameL && e->x() < width() - frameR && |
| 726 | e->y() >= frameT && e->y() < height() - frameB) { |
| 727 | int line = lineAtPos(e->pos()); |
| 728 | if (e->x() > frameL + 32) { |
| 729 | // check if the line exists |
| 730 | // (bottom of memory could have an empty line) |
| 731 | if (line + disasmTopLine < int(disasmLines.size())) { |
| 732 | cursorAddr = disasmLines[disasmTopLine + line].addr; |
| 733 | cursorLine = disasmLines[disasmTopLine + line].infoLine; |
| 734 | } else { |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | // scroll if partial line |
| 739 | if (line == visibleLines) { |
| 740 | setAddress(disasmLines[disasmTopLine + 1].addr, TopAlways); |
| 741 | } else { |
| 742 | update(); |
| 743 | } |
| 744 | } else if (e->x() < frameL + 16) { |
| 745 | // clicked on the breakpoint area |
| 746 | if (line + disasmTopLine < int(disasmLines.size())) { |
| 747 | emit breakpointToggled(disasmLines[line + disasmTopLine].addr); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | int DisasmViewer::lineAtPos(const QPoint& pos) |
| 754 | { |
| 755 | int line = -1; |
| 756 | int y = frameT; |
| 757 | do { |
| 758 | ++line; |
| 759 | switch (disasmLines[disasmTopLine+line].rowType) { |
| 760 | case DisasmRow::LABEL: |
| 761 | y += labelFontHeight; |
| 762 | break; |
| 763 | case DisasmRow::INSTRUCTION: |
| 764 | y += codeFontHeight; |
| 765 | break; |
| 766 | } |
| 767 | } while (y < pos.y()); |
| 768 | |
| 769 | return line; |
| 770 | } |