Debian Codegraph @

memorydumppane.cpp

raw symbols SCIP
1 // File: memorydumppane.cpp
2 /*
3 Pep8-1 is a virtual machine for writing machine language and assembly
4 language programs.
5
6 Copyright (C) 2009 J. Stanley Warford, Pepperdine University
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QFontDialog>
22 #include <QTextCharFormat>
23 #include <QAbstractTextDocumentLayout>
24 #include "memorydumppane.h"
25 #include "ui_memorydumppane.h"
26 #include "sim.h"
27 #include "pep.h"
28 #include "enu.h"
29
30 #include <QDebug>
31
32 MemoryDumpPane::MemoryDumpPane(QWidget *parent) :
33 QWidget(parent),
34 ui(new Ui::MemoryDumpPane)
35 {
36 ui->setupUi(this);
37
38 if (Pep::getSystem() != "Mac") {
39 ui->label->setFont(QFont(Pep::labelFont, Pep::labelFontSize));
40 ui->textEdit->setFont(QFont(Pep::codeFont, Pep::codeFontSize));
41 }
42
43 connect(ui->pcPushButton, SIGNAL(clicked()), this, SLOT(scrollToPC()));
44 connect(ui->spPushButton, SIGNAL(clicked()), this, SLOT(scrollToSP()));
45 connect(ui->scrollToLineEdit, SIGNAL(textChanged(QString)), this, SLOT(scrollToAddress(QString)));
46 }
47
48 MemoryDumpPane::~MemoryDumpPane()
49 {
50 delete ui;
51 }
52
53 void MemoryDumpPane::refreshMemory()
54 {
55 QStringList memoryDump;
56 QString memoryDumpLine;
57 QChar ch;
58 for (int i = 0; i < 65536; i += 8) {
59 memoryDumpLine = "";
60 memoryDumpLine.append(QString("%1 | ").arg(i, 4, 16, QLatin1Char('0')).toUpper());
61 for (int j = 0; j < 8; j++) {
62 memoryDumpLine.append(QString("%1 ").arg(Sim::Mem[i + j], 2, 16, QLatin1Char('0')).toUpper());
63 }
64 memoryDumpLine.append("|");
65 for (int j = 0; j < 8; j++) {
66 ch = QChar(Sim::Mem[i + j]);
67 if (ch.isPrint()) {
68 memoryDumpLine.append(ch);
69 } else {
70 memoryDumpLine.append(".");
71 }
72 }
73 memoryDump.append(memoryDumpLine);
74 }
75 ui->textEdit->setText(memoryDump.join("\n"));
76 }
77
78 void MemoryDumpPane::refreshMemoryLines(int firstByte, int lastByte)
79 {
80 int vertScrollBarPosition = ui->textEdit->verticalScrollBar()->value();
81 int horizScrollBarPosition = ui->textEdit->horizontalScrollBar()->value();
82
83 int firstLine = firstByte / 8;
84 int lastLine = lastByte / 8;
85
86 QTextCursor cursor(ui->textEdit->document());
87 cursor.setPosition(0);
88 for (int i = 0; i < firstLine; i++) {
89 cursor.movePosition(QTextCursor::NextBlock);
90 }
91
92 QString memoryDumpLine;
93 QChar ch;
94 int byteNum;
95 for (int i = firstLine; i <= lastLine; i++) {
96 memoryDumpLine = "";
97 byteNum = i * 8;
98 memoryDumpLine.append(QString("%1 | ").arg(byteNum, 4, 16, QLatin1Char('0')).toUpper());
99 for (int j = 0; j < 8; j++) {
100 memoryDumpLine.append(QString("%1 ").arg(Sim::Mem[byteNum++], 2, 16, QLatin1Char('0')).toUpper());
101 }
102 memoryDumpLine.append("|");
103 byteNum = i * 8;
104 for (int j = 0; j < 8; j++) {
105 ch = QChar(Sim::Mem[byteNum++]);
106 if (ch.isPrint()) {
107 memoryDumpLine.append(ch);
108 } else {
109 memoryDumpLine.append(".");
110 }
111 }
112 cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
113 ui->textEdit->setTextCursor(cursor);
114 ui->textEdit->insertPlainText(memoryDumpLine);
115 cursor.movePosition(QTextCursor::NextBlock);
116 }
117
118 ui->textEdit->verticalScrollBar()->setValue(vertScrollBarPosition);
119 ui->textEdit->horizontalScrollBar()->setValue(horizScrollBarPosition);
120 }
121
122 void MemoryDumpPane::highlightMemory(bool b)
123 {
124 while (!highlightedData.isEmpty()) {
125 highlightByte(highlightedData.takeFirst(), Qt::black, Qt::white);
126 }
127
128 if (b) {
129 highlightByte(Sim::stackPointer, Qt::white, Qt::darkMagenta);
130 highlightedData.append(Sim::stackPointer);
131
132 if (!Pep::isUnaryMap.value(Pep::decodeMnemonic.value(Sim::readByte(Sim::programCounter)))) {
133 QTextCursor cursor(ui->textEdit->document());
134 QTextCharFormat format;
135 format.setBackground(Qt::blue);
136 format.setForeground(Qt::white);
137 cursor.setPosition(0);
138 for (int i = 0; i < Sim::programCounter / 8; i++) {
139 cursor.movePosition(QTextCursor::NextBlock);
140 }
141 for (int i = 0; i < 7 + 3 * (Sim::programCounter % 8); i++) {
142 cursor.movePosition(QTextCursor::NextCharacter);
143 }
144 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
145 cursor.mergeCharFormat(format);
146 highlightedData.append(Sim::programCounter);
147 if (Sim::programCounter / 8 == (Sim::programCounter + 1) / 8) {
148 cursor.clearSelection();
149 cursor.movePosition(QTextCursor::NextCharacter);
150 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
151 cursor.mergeCharFormat(format);
152 }
153 else {
154 cursor.clearSelection();
155 cursor.movePosition(QTextCursor::NextBlock);
156 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, 7);
157 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
158 cursor.mergeCharFormat(format);
159 }
160 highlightedData.append(Sim::add(Sim::programCounter, 1));
161 if ((Sim::programCounter + 1) / 8 == (Sim::programCounter + 2) / 8) {
162 cursor.clearSelection();
163 cursor.movePosition(QTextCursor::NextCharacter);
164 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
165 cursor.mergeCharFormat(format);
166 }
167 else {
168 cursor.clearSelection();
169 cursor.movePosition(QTextCursor::NextBlock);
170 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, 7);
171 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
172 cursor.mergeCharFormat(format);
173 }
174 highlightedData.append(Sim::add(Sim::programCounter, 2));
175 }
176 else { // unary.
177 highlightByte(Sim::programCounter, Qt::white, Qt::blue);
178 highlightedData.append(Sim::programCounter);
179 }
180
181 bytesWrittenLastStep = bytesWrittenLastStep.toSet().toList();
182 qSort(bytesWrittenLastStep);
183 while (!bytesWrittenLastStep.isEmpty()) {
184 // This is to prevent bytes modified by the OS from being highlighted when we are not tracing traps:
185 if (bytesWrittenLastStep.at(0) < Sim::readWord(Pep::dotBurnArgument - 0x7) || Sim::trapped) {
186 highlightByte(bytesWrittenLastStep.at(0), Qt::white, Qt::red);
187 highlightedData.append(bytesWrittenLastStep.takeFirst());
188 }
189 else {
190 return;
191 }
192 }
193 }
194 }
195
196 void MemoryDumpPane::cacheModifiedBytes()
197 {
198 modifiedBytes.unite(Sim::modifiedBytes);
199 if (Sim::tracingTraps) {
200 bytesWrittenLastStep.clear();
201 bytesWrittenLastStep = Sim::modifiedBytes.toList();
202 }
203 else if (Sim::trapped) {
204 delayLastStepClear = true;
205 bytesWrittenLastStep.append(Sim::modifiedBytes.toList());
206 }
207 else if (delayLastStepClear) {
208 delayLastStepClear = false;
209 }
210 else {
211 bytesWrittenLastStep.clear();
212 bytesWrittenLastStep = Sim::modifiedBytes.toList();
213 }
214 }
215
216 void MemoryDumpPane::updateMemory()
217 {
218 int vertScrollBarPosition = ui->textEdit->verticalScrollBar()->value();
219 int horizScrollBarPosition = ui->textEdit->horizontalScrollBar()->value();
220
221 QList<int> list;
222 QSet<int> linesToBeUpdated;
223 QString memoryDumpLine;
224 QChar ch;
225 int byteNum;
226 int lineNum;
227
228 modifiedBytes.unite(Sim::modifiedBytes);
229 list = modifiedBytes.toList();
230 while(!list.isEmpty()) {
231 linesToBeUpdated.insert(list.takeFirst() / 8);
232 }
233 list = linesToBeUpdated.toList();
234 qSort(list.begin(), list.end());
235 QTextCursor cursor(ui->textEdit->document());
236 cursor.setPosition(0);
237 lineNum = 0;
238 while (!list.isEmpty()) {
239 while (lineNum < list.first()) {
240 cursor.movePosition(QTextCursor::NextBlock);
241 lineNum++;
242 }
243
244 memoryDumpLine = "";
245 byteNum = lineNum * 8;
246 memoryDumpLine.append(QString("%1 | ").arg(byteNum, 4, 16, QLatin1Char('0')).toUpper());
247 for (int j = 0; j < 8; j++) {
248 memoryDumpLine.append(QString("%1 ").arg(Sim::Mem[byteNum++], 2, 16, QLatin1Char('0')).toUpper());
249 }
250 memoryDumpLine.append("|");
251 byteNum = lineNum * 8;
252 for (int j = 0; j < 8; j++) {
253 ch = QChar(Sim::Mem[byteNum++]);
254 if (ch.isPrint()) {
255 memoryDumpLine.append(ch);
256 } else {
257 memoryDumpLine.append(".");
258 }
259 }
260 cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
261 ui->textEdit->setTextCursor(cursor);
262 ui->textEdit->insertPlainText(memoryDumpLine);
263 cursor.movePosition(QTextCursor::NextBlock);
264 lineNum++;
265 list.removeFirst();
266 }
267 modifiedBytes.clear();
268
269 ui->textEdit->verticalScrollBar()->setValue(vertScrollBarPosition);
270 ui->textEdit->horizontalScrollBar()->setValue(horizScrollBarPosition);
271
272 }
273
274 void MemoryDumpPane::scrollToTop()
275 {
276 ui->textEdit->verticalScrollBar()->setValue(0);
277 ui->textEdit->horizontalScrollBar()->setValue(0);
278 }
279
280 void MemoryDumpPane::highlightOnFocus()
281 {
282 if (ui->textEdit->hasFocus() || ui->scrollToLineEdit->hasFocus()) {
283 ui->label->setAutoFillBackground(true);
284 }
285 else {
286 ui->label->setAutoFillBackground(false);
287 }
288 }
289
290 bool MemoryDumpPane::hasFocus()
291 {
292 return ui->textEdit->hasFocus() || ui->scrollToLineEdit->hasFocus();
293 }
294
295 void MemoryDumpPane::copy()
296 {
297 ui->textEdit->copy();
298 }
299
300 int MemoryDumpPane::memoryDumpWidth()
301 {
302 return ui->textEdit->document()->documentLayout()->documentSize().toSize().width() +
303 ui->textEdit->verticalScrollBar()->width() + 6;
304 }
305
306 void MemoryDumpPane::onFontChanged(QFont font)
307 {
308 ui->textEdit->setFont(font);
309 }
310
311 void MemoryDumpPane::highlightByte(int memAddr, QColor foreground, QColor background)
312 {
313 QTextCursor cursor(ui->textEdit->document());
314 cursor.setPosition(0);
315 for (int i = 0; i < memAddr / 8; i++) {
316 cursor.movePosition(QTextCursor::NextBlock);
317 }
318 for (int i = 0; i < 7 + 3 * (memAddr % 8); i++) {
319 cursor.movePosition(QTextCursor::NextCharacter);
320 }
321 cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
322 QTextCharFormat format;
323 format.setBackground(background);
324 format.setForeground(foreground);
325 cursor.mergeCharFormat(format);
326 }
327
328 void MemoryDumpPane::mouseReleaseEvent(QMouseEvent *)
329 {
330 ui->textEdit->setFocus();
331 }
332
333 void MemoryDumpPane::scrollToByte(int byte)
334 {
335 int min = ui->textEdit->verticalScrollBar()->minimum();
336 int max = ui->textEdit->verticalScrollBar()->maximum();
337 ui->textEdit->verticalScrollBar()->setValue(min + static_cast<int>(8 * (byte / 4096 - 8) + ((byte - byte % 8) / 65536.0) * (max - min)));
338 }
339
340 void MemoryDumpPane::scrollToPC()
341 {
342 ui->scrollToLineEdit->setText(QString("0x") + QString("%1").arg(Sim::programCounter, 4, 16, QLatin1Char('0')).toUpper());
343 }
344
345 void MemoryDumpPane::scrollToSP()
346 {
347 ui->scrollToLineEdit->setText(QString("0x") + QString("%1").arg(Sim::stackPointer, 4, 16, QLatin1Char('0')).toUpper());
348 }
349
350 void MemoryDumpPane::scrollToAddress(QString string)
351 {
352 bool ok;
353 int byte;
354 if (string.startsWith("0x", Qt::CaseInsensitive)) {
355 byte = string.toInt(&ok, 16);
356 if (ok) {
357 if (byte > 65535) {
358 ui->scrollToLineEdit->setText("0xFFFF");
359 } else {
360 scrollToByte(byte);
361 }
362 }
363 else {
364 ui->scrollToLineEdit->setText("0x");
365 }
366 }
367 else {
368 ui->scrollToLineEdit->setText("0x");
369 }
370
371 // else {
372 // byte = string.toInt(&ok, 10);
373 // if (ok) {
374 // if (byte < 0) {
375 // ui->scrollToLineEdit->setText("0");
376 // }
377 // else if (byte > 65535) {
378 // ui->scrollToLineEdit->setText("65535");
379 // } else {
380 // scrollToByte(byte);
381 // ui->scrollToLineEdit->setText(QString("%1").arg(byte));
382 // }
383 // }
384 // else {
385 // ui->scrollToLineEdit->setText("");
386 // }
387 // }
388 }