Debian Codegraph @

src/CPURegsViewer.cpp

raw symbols SCIP
1 #include "CPURegsViewer.h"
2 #include "CPURegs.h"
3 #include "CommClient.h"
4 #include "OpenMSXConnection.h"
5 #include "ranges.h"
6 #include <QPainter>
7 #include <QPaintEvent>
8 #include <QMessageBox>
9 #include <QToolTip>
10
11 CPURegsViewer::CPURegsViewer(QWidget* parent)
12 : QFrame(parent)
13 {
14 // avoid UMR
15 regs.fill(0);
16 regsChanged.fill(false);
17 regsModified.fill(false);
18
19 setFrameStyle(WinPanel | Sunken);
20 setFocusPolicy(Qt::StrongFocus);
21 setBackgroundRole(QPalette::Base);
22 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
23
24 frameR = frameL = frameT = frameB = frameWidth();
25 cursorLoc = -1;
26 }
27
28 void CPURegsViewer::resizeEvent(QResizeEvent* e)
29 {
30 QFrame::resizeEvent(e);
31 }
32
33 void CPURegsViewer::paintEvent(QPaintEvent* e)
34 {
35 // call parent for drawing the actual frame
36 QFrame::paintEvent(e);
37
38 QPainter p(this);
39 p.setPen(palette().color(QPalette::Text));
40
41 rowHeight = fontMetrics().height();
42 int regWidth = fontMetrics().horizontalAdvance("HLW");
43 int valWidth = fontMetrics().horizontalAdvance("FFFFWW");
44 int d = fontMetrics().descent();
45
46 leftRegPos = frameL + 4;
47 leftValuePos = leftRegPos + regWidth;
48 rightRegPos = leftValuePos + valWidth;
49 rightValuePos = rightRegPos + regWidth;
50
51 // calc and set drawing bounds
52 QRect r(e->rect());
53 if (r.left() < frameL) r.setLeft(frameL);
54 if (r.top() < frameT) r.setTop (frameT);
55 if (r.right() > (width() - frameR - 1)) r.setRight (width() - frameR - 1);
56 if (r.bottom() > (height() - frameB - 1)) r.setBottom(height() - frameB - 1);
57 p.setClipRect(r);
58
59 // redraw background
60 p.fillRect(r, palette().color(QPalette::Base));
61
62 int y = frameT + rowHeight - 1 - d;
63 for (int r = 0; r < 14; r += 2) {
64 p.drawText(leftRegPos, y, CpuRegs::regNames[r + 0]);
65 drawValue(p, r + 0, leftValuePos, y);
66 p.drawText(rightRegPos, y, CpuRegs::regNames[r + 1]);
67 drawValue(p, r + 1, rightValuePos, y);
68 y += rowHeight;
69 }
70
71 // draw interrupt mode
72 p.drawText(leftRegPos, y, "IM");
73 drawValue(p, 14, leftValuePos, y);
74
75 // draw interrupt state
76 if (regsChanged[CpuRegs::REG_IFF] & 1) {
77 p.setPen(Qt::red);
78 }
79 p.drawText(rightRegPos, y, regs[CpuRegs::REG_IFF] ? "EI" : "DI");
80 }
81
82 QSize CPURegsViewer::sizeHint() const
83 {
84 return {frameL + 4 + fontMetrics().horizontalAdvance("HLWFFFFWWHLWFFFFW") + 4 + frameR,
85 frameT + 8 * fontMetrics().height() + frameB};
86 }
87
88 void CPURegsViewer::drawValue(QPainter& p, int id, int x, int y)
89 {
90 // determine value colour
91 QColor penClr = palette().color(QPalette::Text);
92 if (regsModified[id]) {
93 penClr = Qt::darkGreen;
94 } else if (regsChanged[id]) {
95 penClr = Qt::red;
96 }
97 // print (edit) value
98 if ((cursorLoc >> 2) == id) {
99 // cursor is in this value, print separate digits
100 int digit = 3;
101 if (id < CpuRegs::REG_I) {
102 digit = 0;
103 } else if (id < CpuRegs::REG_IM) {
104 digit = 2;
105 }
106 // write all digit
107 for (/**/; digit < 4; ++digit) {
108 // create string with a single digit
109 QString digitTxt = QString("%1")
110 .arg((regs[id] >> (4 * (3 - digit))) & 15, 0, 16)
111 .toUpper();
112 // if digit has cursor, draw as cursor
113 if ((cursorLoc & 3) == digit) {
114 // draw curser background
115 QBrush b(palette().color(QPalette::Highlight));
116 p.fillRect(x, frameT + (cursorLoc >> 3) * rowHeight,
117 fontMetrics().horizontalAdvance(digitTxt), rowHeight, b);
118 p.setPen(palette().color(QPalette::HighlightedText));
119 } else {
120 p.setPen(penClr);
121 }
122 p.drawText(x, y, digitTxt);
123 x += fontMetrics().horizontalAdvance(digitTxt);
124 }
125 } else {
126 // regular value print
127 p.setPen(penClr);
128 // create string
129 QString str;
130 if (id < CpuRegs::REG_I) {
131 str = QString("%1").arg(regs[id], 4, 16, QChar('0')).toUpper();
132 } else if (id < CpuRegs::REG_IM) {
133 str = QString("%1").arg(regs[id], 2, 16, QChar('0')).toUpper();
134 } else {
135 str = QString("%1").arg(regs[id], 1, 16, QChar('0')).toUpper();
136 }
137 // draw
138 p.drawText(x, y, str);
139 }
140 // reset pen
141 p.setPen(palette().color(QPalette::Text));
142 }
143
144 void CPURegsViewer::setRegister(int id, int value)
145 {
146 regsChanged[id] = regs[id] != value;
147 regs[id] = value;
148 if (regsChanged[id]) {
149 emit registerChanged(id, value);
150 }
151 }
152
153 void CPURegsViewer::setData(unsigned char* datPtr)
154 {
155 setRegister(CpuRegs::REG_AF , datPtr[ 0] * 256 + datPtr[ 1]);
156 setRegister(CpuRegs::REG_BC , datPtr[ 2] * 256 + datPtr[ 3]);
157 setRegister(CpuRegs::REG_DE , datPtr[ 4] * 256 + datPtr[ 5]);
158 setRegister(CpuRegs::REG_HL , datPtr[ 6] * 256 + datPtr[ 7]);
159 setRegister(CpuRegs::REG_AF2, datPtr[ 8] * 256 + datPtr[ 9]);
160 setRegister(CpuRegs::REG_BC2, datPtr[10] * 256 + datPtr[11]);
161 setRegister(CpuRegs::REG_DE2, datPtr[12] * 256 + datPtr[13]);
162 setRegister(CpuRegs::REG_HL2, datPtr[14] * 256 + datPtr[15]);
163 setRegister(CpuRegs::REG_IX , datPtr[16] * 256 + datPtr[17]);
164 setRegister(CpuRegs::REG_IY , datPtr[18] * 256 + datPtr[19]);
165 setRegister(CpuRegs::REG_PC , datPtr[20] * 256 + datPtr[21]);
166 setRegister(CpuRegs::REG_SP , datPtr[22] * 256 + datPtr[23]);
167 setRegister(CpuRegs::REG_I , datPtr[24]);
168 setRegister(CpuRegs::REG_R , datPtr[25]);
169 setRegister(CpuRegs::REG_IM , datPtr[26]);
170
171 // IFF separately to only check bit 0 for change
172 regsChanged[CpuRegs::REG_IFF] = (regs[CpuRegs::REG_IFF] & 1) != (datPtr[27] & 1);
173 regs[CpuRegs::REG_IFF] = datPtr[27];
174
175 // reset modifications
176 cursorLoc = -1;
177 regsModified.fill(false);
178 regsCopy = regs;
179
180 update();
181
182 emit pcChanged(regs[CpuRegs::REG_PC]);
183 emit spChanged(regs[CpuRegs::REG_SP]);
184 emit flagsChanged(regs[CpuRegs::REG_AF] & 0xFF);
185 }
186
187 void CPURegsViewer::focusOutEvent(QFocusEvent* e)
188 {
189 if (e->lostFocus()) {
190 cancelModifications();
191 cursorLoc = -1;
192 update();
193 }
194 }
195
196 void CPURegsViewer::mousePressEvent(QMouseEvent* e)
197 {
198 if (e->button() == Qt::LeftButton) {
199 int pos = -1;
200 if (e->x() >= leftValuePos && e->x() < rightRegPos) {
201 pos = 0;
202 }
203 if (e->x() >= rightValuePos) {
204 pos = 4;
205 }
206 if (pos >= 0 && e->y() < frameT + 7 * rowHeight) {
207 int row = (e->y() - frameT) / rowHeight;
208 cursorLoc = pos + 8 * row;
209 if (row == 6) cursorLoc += 2;
210 }
211 update();
212 }
213 }
214
215 void CPURegsViewer::keyPressEvent(QKeyEvent* e)
216 {
217 // don't accept when not editting
218 if (cursorLoc < 0) {
219 QFrame::keyPressEvent(e);
220 return;
221 }
222
223 int move = e->key();
224 if ((e->key() >= Qt::Key_0 && e->key() <= Qt::Key_9) ||
225 (e->key() >= Qt::Key_A && e->key() <= Qt::Key_F)) {
226 // calculate numerical value
227 int v = e->key() - Qt::Key_0;
228 if (v > 9) v -= Qt::Key_A - Qt::Key_0 - 10;
229 // modify value
230 int id = cursorLoc >> 2;
231 int digit = cursorLoc & 3;
232 regs[id] &= ~(0xF000 >> (4 * digit));
233 regs[id] |= v << (12 - 4 * digit);
234 regsModified[id] = true;
235 move = Qt::Key_Right;
236 }
237
238 if (move == Qt::Key_Right) {
239 ++cursorLoc;
240 if (cursorLoc == 4 * CpuRegs::REG_I ||
241 cursorLoc == 4 * CpuRegs::REG_R) {
242 cursorLoc += 2;
243 } else if (cursorLoc == 4 * CpuRegs::REG_IM) {
244 cursorLoc = 0;
245 }
246 } else if (move == Qt::Key_Left) {
247 --cursorLoc;
248 if (cursorLoc == -1) {
249 cursorLoc = 4 * CpuRegs::REG_R + 3;
250 } else if (cursorLoc == 4 * CpuRegs::REG_R + 1 ||
251 cursorLoc == 4 * CpuRegs::REG_I + 1) {
252 cursorLoc -= 2;
253 }
254 } else if (move == Qt::Key_Up) {
255 cursorLoc -= 8;
256 if (cursorLoc < 0) {
257 cursorLoc += 4 * CpuRegs::REG_IM;
258 // move to lowest row
259 if (cursorLoc == 4 * CpuRegs::REG_I) {
260 cursorLoc = 4 * CpuRegs::REG_I + 2;
261 } else if (cursorLoc < 4 * CpuRegs::REG_R) {
262 cursorLoc = 4 * CpuRegs::REG_I + 3;
263 } else if (cursorLoc == 4 * CpuRegs::REG_R) {
264 cursorLoc = 4 * CpuRegs::REG_R + 2;
265 } else {
266 cursorLoc = 4 * CpuRegs::REG_R + 3;
267 }
268 } else if (cursorLoc >= 4 * CpuRegs::REG_PC) {
269 // move from lowest row
270 cursorLoc -= 2;
271 }
272 } else if (move == Qt::Key_Down) {
273 cursorLoc += 8;
274 if (cursorLoc >= 4 * CpuRegs::REG_IM) {
275 // move from lowest row
276 cursorLoc -= 4 * CpuRegs::REG_IM + 2;
277 } else if (cursorLoc >= 4 * CpuRegs::REG_I) {
278 // move to lowest row
279 if (cursorLoc == 4 * CpuRegs::REG_I) {
280 cursorLoc = 4 * CpuRegs::REG_I + 2;
281 } else if (cursorLoc < 4 * CpuRegs::REG_R) {
282 cursorLoc = 4 * CpuRegs::REG_I + 3;
283 } else if (cursorLoc == 4 * CpuRegs::REG_R) {
284 cursorLoc = 4 * CpuRegs::REG_R + 2;
285 } else {
286 cursorLoc = 4 * CpuRegs::REG_R + 3;
287 }
288 }
289 } else if (move == Qt::Key_Escape) {
290 // cancel changes
291 cancelModifications();
292 cursorLoc = -1;
293 } else if (move == Qt::Key_Return || move == Qt::Key_Enter) {
294 // apply changes
295 applyModifications();
296 } else {
297 QFrame::keyPressEvent(e);
298 return;
299 }
300 e->accept();
301 update();
302 }
303
304 //void CPURegsViewer::mouseMoveEvent(QMouseEvent* e)
305 //{
306 // QToolTip::hideText();
307 // QFrame::mouseMoveEvent(e);
308 //}
309
310 int CPURegsViewer::readRegister(int id)
311 {
312 return regs[id];
313 }
314
315 void CPURegsViewer::getRegister(int id, unsigned char* data)
316 {
317 data[0] = regs[id] >> 8;
318 data[1] = regs[id] & 255;
319 }
320
321 void CPURegsViewer::applyModifications()
322 {
323 unsigned char data[26];
324 getRegister(CpuRegs::REG_AF, &data[ 0]);
325 getRegister(CpuRegs::REG_BC, &data[ 2]);
326 getRegister(CpuRegs::REG_DE, &data[ 4]);
327 getRegister(CpuRegs::REG_HL, &data[ 6]);
328 getRegister(CpuRegs::REG_AF2, &data[ 8]);
329 getRegister(CpuRegs::REG_BC2, &data[10]);
330 getRegister(CpuRegs::REG_DE2, &data[12]);
331 getRegister(CpuRegs::REG_HL2, &data[14]);
332 getRegister(CpuRegs::REG_IX, &data[16]);
333 getRegister(CpuRegs::REG_IY, &data[18]);
334 getRegister(CpuRegs::REG_PC, &data[20]);
335 getRegister(CpuRegs::REG_SP, &data[22]);
336 data[24] = regs[CpuRegs::REG_I];
337 data[25] = regs[CpuRegs::REG_R];
338
339 // send new data to openmsx
340 auto* req = new WriteDebugBlockCommand("{CPU regs}", 0, 26, data);
341 CommClient::instance().sendCommand(req);
342 // turn off editing
343 cursorLoc = -1;
344 // update copy
345 for (int i = 0; i < 14; ++i) {
346 if (regsModified[i]) {
347 regsModified[i] = false;
348 regsCopy[i] = regs[i];
349 regsChanged[i] = true;
350 }
351 }
352 // update screen
353 update();
354
355 // update disasm window
356 if (regsChanged[CpuRegs::REG_PC]) {
357 emit pcChanged(regs[CpuRegs::REG_PC]);
358 }
359 }
360
361 void CPURegsViewer::cancelModifications()
362 {
363 static bool isVisible = false;
364
365 if (!ranges::contains(regsModified, true)) return;
366
367 if (!isVisible) {
368 isVisible = true;
369 // this modal window blocks execution of next instruction
370 int ret = QMessageBox::warning(
371 this,
372 tr("CPU registers changes"),
373 tr("You made changes to the CPU registers.\n"
374 "Do you want to apply your changes or ignore them?"),
375 QMessageBox::Apply | QMessageBox::Ignore,
376 QMessageBox::Ignore);
377 isVisible = false;
378 if (ret == QMessageBox::Ignore) {
379 regs = regsCopy;
380 regsModified.fill(false);
381 } else {
382 applyModifications();
383 }
384 }
385 }
386
387 bool CPURegsViewer::event(QEvent* e)
388 {
389 if (e->type() != QEvent::ToolTip) {
390 return QFrame::event(e);
391 }
392
393 auto* helpEvent = static_cast<QHelpEvent*>(e);
394 // calc register number
395 int pos = -1;
396 if (helpEvent->x() >= leftValuePos &&
397 helpEvent->x() < rightRegPos) {
398 pos = 0;
399 }
400 if (helpEvent->x() >= rightValuePos) {
401 pos = 1;
402 }
403 if (pos >= 0 && helpEvent->y() < frameT + 7 * rowHeight) {
404 pos += 2 * ((helpEvent->y() - frameT) / rowHeight);
405 }
406 if (pos >= 0 && pos != 10 && pos != 11) {
407 // create text with binary and decimal values
408 QString text(CpuRegs::regNames[pos]);
409 text += "\nBinary: ";
410 if (pos < 12) {
411 text += QString("%1 ")
412 .arg((regs[pos] & 0xF000) >> 12, 4, 2, QChar('0'));
413 text += QString("%1 ")
414 .arg((regs[pos] & 0x0F00) >> 8, 4, 2, QChar('0'));
415 }
416 text += " ";
417 text += QString("%1 ").arg((regs[pos] & 0x00F0) >> 4, 4, 2, QChar('0'));
418 text += QString("%1") .arg((regs[pos] & 0x000F) >> 0, 4, 2, QChar('0'));
419 text += "\nDecimal: ";
420 text += QString::number(regs[pos]);
421 // print 8 bit values
422 if (pos < 8) {
423 text += QString("\n%1: %2 %3: %4")
424 .arg(CpuRegs::regNames[pos][0])
425 .arg(regs[pos] >> 8)
426 .arg(CpuRegs::regNames[pos][1])
427 .arg(regs[pos] & 255);
428 } else if (pos < 10) {
429 text += QString("\nI%1H: %2 I%3L: %4")
430 .arg(CpuRegs::regNames[pos][1])
431 .arg(regs[pos] >> 8)
432 .arg(CpuRegs::regNames[pos][1])
433 .arg(regs[pos] & 255);
434 }
435 QToolTip::showText(helpEvent->globalPos(), text);
436 } else {
437 QToolTip::hideText();
438 }
439 return QFrame::event(e);
440 }