| 1 | #include "SlotViewer.h" |
| 2 | #include "DebuggerData.h" |
| 3 | #include "OpenMSXConnection.h" |
| 4 | #include "CommClient.h" |
| 5 | #include <QPainter> |
| 6 | #include <QPaintEvent> |
| 7 | #include <QStyleOptionHeader> |
| 8 | |
| 9 | |
| 10 | class DebugMemMapperHandler : public SimpleCommand |
| 11 | { |
| 12 | public: |
| 13 | DebugMemMapperHandler(SlotViewer& viewer_) |
| 14 | : SimpleCommand("debug_memmapper") |
| 15 | , viewer(viewer_) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void replyOk(const QString& message) override |
| 20 | { |
| 21 | viewer.updateSlots(message); |
| 22 | delete this; |
| 23 | } |
| 24 | |
| 25 | private: |
| 26 | SlotViewer& viewer; |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | SlotViewer::SlotViewer(QWidget* parent) |
| 31 | : QFrame(parent) |
| 32 | { |
| 33 | setFrameStyle(WinPanel | Sunken); |
| 34 | setFocusPolicy(Qt::StrongFocus); |
| 35 | setBackgroundRole(QPalette::Base); |
| 36 | setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum)); |
| 37 | |
| 38 | memLayout = nullptr; |
| 39 | for (int p = 0; p < 4; ++p) { |
| 40 | slotsChanged[p] = false; |
| 41 | segmentsChanged[p] = false; |
| 42 | } |
| 43 | |
| 44 | frameR = frameL = frameT = frameB = frameWidth(); |
| 45 | |
| 46 | headerSize1 = 8 + fontMetrics().horizontalAdvance("Page"); |
| 47 | headerSize2 = 8 + fontMetrics().horizontalAdvance("Address"); |
| 48 | headerSize3 = 8 + fontMetrics().horizontalAdvance("Slot"); |
| 49 | headerSize4 = 8 + fontMetrics().horizontalAdvance("Segment"); |
| 50 | headerHeight = 8 + fontMetrics().height(); |
| 51 | } |
| 52 | |
| 53 | void SlotViewer::resizeEvent(QResizeEvent* e) |
| 54 | { |
| 55 | QFrame::resizeEvent(e); |
| 56 | } |
| 57 | |
| 58 | void SlotViewer::paintEvent(QPaintEvent* e) |
| 59 | { |
| 60 | // call parent for drawing the actual frame |
| 61 | QFrame::paintEvent(e); |
| 62 | |
| 63 | QPainter p(this); |
| 64 | |
| 65 | // calc and set drawing bounds |
| 66 | QRect r(e->rect()); |
| 67 | if (r.left() < frameL) r.setLeft(frameL); |
| 68 | if (r.top() < frameT) r.setTop (frameT); |
| 69 | if (r.right() > width() - frameR - 1) r.setRight (width() - frameR - 1); |
| 70 | if (r.bottom() > height() - frameB - 1) r.setBottom(height() - frameB - 1); |
| 71 | p.setClipRect(r); |
| 72 | |
| 73 | // redraw background |
| 74 | p.fillRect(r, palette().color(QPalette::Base)); |
| 75 | |
| 76 | QStyleOptionHeader so; |
| 77 | so.init(this); |
| 78 | so.state |= QStyle::State_Raised; |
| 79 | so.orientation = Qt::Horizontal; |
| 80 | so.position = QStyleOptionHeader::Beginning; |
| 81 | so.sortIndicator = QStyleOptionHeader::None; |
| 82 | so.textAlignment = Qt::AlignHCenter; |
| 83 | so.rect.setTop(frameT); |
| 84 | so.rect.setHeight(headerHeight); |
| 85 | so.rect.setLeft(frameL); |
| 86 | so.rect.setWidth(headerSize1); |
| 87 | so.section = 0; |
| 88 | so.text = "Page"; |
| 89 | style()->drawControl(QStyle::CE_Header, &so, &p, this); |
| 90 | so.rect.setLeft(so.rect.left() + headerSize1); |
| 91 | so.rect.setWidth(headerSize2); |
| 92 | so.section = 1; |
| 93 | so.text = "Address"; |
| 94 | style()->drawControl(QStyle::CE_Header, &so, &p, this); |
| 95 | so.rect.setLeft(so.rect.left() + headerSize2); |
| 96 | so.rect.setWidth(headerSize3); |
| 97 | so.section = 2; |
| 98 | so.text = "Slot"; |
| 99 | style()->drawControl(QStyle::CE_Header, &so, &p, this); |
| 100 | so.rect.setLeft(so.rect.left() + headerSize3); |
| 101 | so.rect.setWidth(headerSize4); |
| 102 | so.section = 3; |
| 103 | so.text = "Segment"; |
| 104 | style()->drawControl(QStyle::CE_Header, &so, &p, this); |
| 105 | |
| 106 | int mid1 = frameL + headerSize1 / 2; |
| 107 | int mid2 = frameL + headerSize1 + headerSize2 / 2; |
| 108 | int mid3 = frameL + headerSize1 + headerSize2 + headerSize3 / 2; |
| 109 | int mid4 = frameL + headerSize1 + headerSize2 + headerSize3 + headerSize4 / 2; |
| 110 | int dy = (height() - frameT - frameB - headerHeight) / 4; |
| 111 | int y = frameT + headerHeight + dy / 2 + fontMetrics().height() / 2 - |
| 112 | fontMetrics().descent(); |
| 113 | |
| 114 | int isOn = isEnabled() && memLayout != nullptr; |
| 115 | for (int i = 0; i < 4; ++i) { |
| 116 | QString str; |
| 117 | p.setPen(palette().color(QPalette::Text)); |
| 118 | |
| 119 | // print page nr |
| 120 | str = QString("$%1").arg(i); |
| 121 | p.drawText(mid1 - fontMetrics().horizontalAdvance(str) / 2, y, str); |
| 122 | |
| 123 | // print address |
| 124 | str = QString("$%1").arg(i * 0x4000, 4, 16, QChar('0')).toUpper(); |
| 125 | p.drawText(mid2 - fontMetrics().horizontalAdvance(str) / 2, y, str); |
| 126 | |
| 127 | // print slot |
| 128 | if (isOn) { |
| 129 | if (memLayout->isSubslotted[memLayout->primarySlot[i]]) { |
| 130 | str = QString("%1-%2").arg(memLayout->primarySlot[i]).arg(memLayout->secondarySlot[i]); |
| 131 | } else { |
| 132 | str = QString::number(memLayout->primarySlot[i]); |
| 133 | } |
| 134 | } else { |
| 135 | str = "-"; |
| 136 | } |
| 137 | // set pen colour to red if slot was recently changed |
| 138 | if (slotsChanged[i] && isOn) { |
| 139 | p.setPen(Qt::red); |
| 140 | } else { |
| 141 | p.setPen(palette().color(QPalette::Text)); |
| 142 | } |
| 143 | |
| 144 | p.drawText(mid3 - fontMetrics().horizontalAdvance(str) / 2, y, str); |
| 145 | |
| 146 | // print segment |
| 147 | if (isOn) { |
| 148 | int ms; |
| 149 | if (memLayout->isSubslotted[memLayout->primarySlot[i] & 3]) { |
| 150 | ms = memLayout->mapperSize[memLayout->primarySlot[i] & 3] |
| 151 | [memLayout->secondarySlot[i] & 3]; |
| 152 | } else { |
| 153 | ms = memLayout->mapperSize[memLayout->primarySlot[i] & 3][0]; |
| 154 | } |
| 155 | if (ms > 0) { |
| 156 | str = QString("%1").arg(memLayout->mapperSegment[i]); |
| 157 | } else if (memLayout->romBlock[2*i] >= 0) { |
| 158 | if (memLayout->romBlock[2*i] == memLayout->romBlock[2*i+1]) { |
| 159 | str = QString("R%1").arg(memLayout->romBlock[2*i]); |
| 160 | } else { |
| 161 | str = QString("R%1/%2").arg(memLayout->romBlock[2*i]).arg(memLayout->romBlock[2*i+1]); |
| 162 | } |
| 163 | } else { |
| 164 | str = "-"; |
| 165 | } |
| 166 | } else { |
| 167 | str = "-"; |
| 168 | } |
| 169 | // set pen colour to red if slot was recently changed |
| 170 | if (segmentsChanged[i] && isOn) { |
| 171 | p.setPen(Qt::red); |
| 172 | } else { |
| 173 | p.setPen(palette().color(QPalette::Text)); |
| 174 | } |
| 175 | p.drawText(mid4 - fontMetrics().horizontalAdvance(str) / 2, y, str); |
| 176 | |
| 177 | y += dy; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | QSize SlotViewer::sizeHint() const |
| 182 | { |
| 183 | return {headerSize1 + headerSize2 + headerSize3 + headerSize4 + frameL + frameR, |
| 184 | headerHeight + 4*fontMetrics().height()}; |
| 185 | } |
| 186 | |
| 187 | void SlotViewer::refresh() |
| 188 | { |
| 189 | CommClient::instance().sendCommand(new DebugMemMapperHandler(*this)); |
| 190 | } |
| 191 | |
| 192 | void SlotViewer::setMemoryLayout(MemoryLayout* ml) |
| 193 | { |
| 194 | memLayout = ml; |
| 195 | } |
| 196 | |
| 197 | void SlotViewer::updateSlots(const QString& message) |
| 198 | { |
| 199 | QStringList lines = message.split('\n'); |
| 200 | bool changed = false; |
| 201 | |
| 202 | // parse page slots and segments |
| 203 | for (int p = 0; p < 4; ++p) { |
| 204 | bool subSlotted = (lines[p * 2][1] != 'X'); |
| 205 | slotsChanged[p] = (memLayout->primarySlot [p] != lines[p * 2][0].toLatin1()-'0') || |
| 206 | (memLayout->secondarySlot[p] != (subSlotted ? lines[p * 2][1].toLatin1() - '0' : -1)); |
| 207 | changed |= slotsChanged[p]; |
| 208 | memLayout->primarySlot [p] = lines[p * 2][0].toLatin1()-'0'; |
| 209 | memLayout->secondarySlot[p] = subSlotted |
| 210 | ? lines[p * 2][1].toLatin1() - '0' : -1; |
| 211 | segmentsChanged[p] = memLayout->mapperSegment[p] != |
| 212 | lines[p * 2 + 1].toInt(); |
| 213 | memLayout->mapperSegment[p] = lines[p * 2 + 1].toInt(); |
| 214 | } |
| 215 | // parse slot layout |
| 216 | int l = 8; |
| 217 | for (int ps = 0; ps < 4; ++ps) { |
| 218 | memLayout->isSubslotted[ps] = lines[l++][0] == '1'; |
| 219 | if (memLayout->isSubslotted[ps]) { |
| 220 | for (int ss = 0; ss < 4; ++ss) { |
| 221 | memLayout->mapperSize[ps][ss] = lines[l++].toUShort(); |
| 222 | } |
| 223 | } else { |
| 224 | memLayout->mapperSize[ps][0] = lines[l++].toUShort(); |
| 225 | } |
| 226 | } |
| 227 | // parse rom blocks |
| 228 | for (int i = 0; i < 8; ++i, ++l) { |
| 229 | if (lines[l][0] == 'X') |
| 230 | memLayout->romBlock[i] = -1; |
| 231 | else |
| 232 | memLayout->romBlock[i] = lines[l].toInt(); |
| 233 | } |
| 234 | update(); |
| 235 | emit slotsUpdated(changed); |
| 236 | } |