| 1 | // File: memorytracepane.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 <QRgb> |
| 23 | #include "memorytracepane.h" |
| 24 | #include "ui_memorytracepane.h" |
| 25 | #include "pep.h" |
| 26 | #include "sim.h" |
| 27 | #include "asm.h" |
| 28 | |
| 29 | #include <QMessageBox> |
| 30 | #include <QDebug> |
| 31 | |
| 32 | MemoryTracePane::MemoryTracePane(QWidget *parent) : |
| 33 | QWidget(parent), |
| 34 | ui(new Ui::MemoryTracePane) |
| 35 | { |
| 36 | ui->setupUi(this); |
| 37 | |
| 38 | ui->label->setFont(QFont(Pep::labelFont, Pep::labelFontSize)); |
| 39 | ui->graphicsView->setFont(QFont(Pep::codeFont, Pep::codeFontSize)); |
| 40 | |
| 41 | connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(zoomFactorChanged(int))); |
| 42 | |
| 43 | scene = new QGraphicsScene(this); |
| 44 | } |
| 45 | |
| 46 | MemoryTracePane::~MemoryTracePane() |
| 47 | { |
| 48 | delete ui; |
| 49 | } |
| 50 | |
| 51 | void MemoryTracePane::setMemoryTrace() |
| 52 | { |
| 53 | globalVars.clear(); |
| 54 | runtimeStack.clear(); |
| 55 | heap.clear(); |
| 56 | isRuntimeStackItemAddedStack.clear(); |
| 57 | isHeapItemAddedStack.clear(); |
| 58 | isStackFrameAddedStack.clear(); |
| 59 | isHeapFrameAddedStack.clear(); |
| 60 | stackHeightToStackFrameMap.clear(); |
| 61 | modifiedBytes.clear(); |
| 62 | bytesWrittenLastStep.clear(); |
| 63 | addressToGlobalItemMap.clear(); |
| 64 | addressToStackItemMap.clear(); |
| 65 | addressToHeapItemMap.clear(); |
| 66 | numCellsInStackFrame.clear(); |
| 67 | graphicItemsInStackFrame.clear(); |
| 68 | heapFrameItemStack.clear(); |
| 69 | newestHeapItemsList.clear(); |
| 70 | scene->clear(); |
| 71 | |
| 72 | if (Pep::traceTagWarning) { |
| 73 | hide(); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | stackLocation = QPointF(200, 0); |
| 78 | globalLocation = QPointF(0, 0); |
| 79 | heapLocation = QPointF(400, 0 - MemoryCellGraphicsItem::boxHeight); |
| 80 | QString blockSymbol; |
| 81 | int multiplier; |
| 82 | |
| 83 | // Globals: |
| 84 | for (int i = 0; i < Pep::blockSymbols.size(); i++) { |
| 85 | blockSymbol = Pep::blockSymbols.at(i); |
| 86 | multiplier = Pep::symbolFormatMultiplier.value(blockSymbol); |
| 87 | int address = Pep::symbolTable.value(blockSymbol); |
| 88 | if (Pep::globalStructSymbols.contains(blockSymbol)) { |
| 89 | int offset = 0; |
| 90 | int bytesPerCell; |
| 91 | QString structField = ""; |
| 92 | for (int j = 0; j < Pep::globalStructSymbols.value(blockSymbol).size(); j++) { |
| 93 | structField = Pep::globalStructSymbols.value(blockSymbol).at(j); |
| 94 | bytesPerCell = Sim::cellSize(Pep::symbolFormat.value(structField)); |
| 95 | MemoryCellGraphicsItem *item = new MemoryCellGraphicsItem(address + offset, |
| 96 | QString("%1.%2").arg(blockSymbol).arg(structField), |
| 97 | Pep::symbolFormat.value(structField), |
| 98 | static_cast<int>(globalLocation.x()), |
| 99 | static_cast<int>(globalLocation.y())); |
| 100 | item->updateValue(); |
| 101 | globalLocation = QPointF(globalLocation.x(), globalLocation.y() + MemoryCellGraphicsItem::boxHeight); |
| 102 | globalVars.push(item); |
| 103 | addressToGlobalItemMap.insert(address + offset, item); |
| 104 | scene->addItem(item); |
| 105 | offset += bytesPerCell; |
| 106 | } |
| 107 | } |
| 108 | else { |
| 109 | if (multiplier == 1) { |
| 110 | MemoryCellGraphicsItem *item = new MemoryCellGraphicsItem(address, |
| 111 | blockSymbol, |
| 112 | Pep::symbolFormat.value(blockSymbol), |
| 113 | static_cast<int>(globalLocation.x()), |
| 114 | static_cast<int>(globalLocation.y())); |
| 115 | item->updateValue(); |
| 116 | globalLocation = QPointF(globalLocation.x(), globalLocation.y() + MemoryCellGraphicsItem::boxHeight); |
| 117 | globalVars.push(item); |
| 118 | addressToGlobalItemMap.insert(address, item); |
| 119 | scene->addItem(item); |
| 120 | } |
| 121 | else { // Array |
| 122 | int offset = 0; |
| 123 | int bytesPerCell = Sim::cellSize(Pep::symbolFormat.value(blockSymbol)); |
| 124 | for (int j = 0; j < multiplier; j++) { |
| 125 | MemoryCellGraphicsItem *item = new MemoryCellGraphicsItem(address + offset, |
| 126 | blockSymbol + QString("[%1]").arg(j), |
| 127 | Pep::symbolFormat.value(blockSymbol), |
| 128 | static_cast<int>(globalLocation.x()), |
| 129 | static_cast<int>(globalLocation.y())); |
| 130 | item->updateValue(); |
| 131 | globalLocation = QPointF(globalLocation.x(), globalLocation.y() + MemoryCellGraphicsItem::boxHeight); |
| 132 | globalVars.push(item); |
| 133 | addressToGlobalItemMap.insert(address + offset, item); |
| 134 | scene->addItem(item); |
| 135 | offset += bytesPerCell; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Stack frame: |
| 142 | stackLocation.setY(globalLocation.y()); |
| 143 | scene->addLine(stackLocation.x() - MemoryCellGraphicsItem::boxWidth * 0.2, stackLocation.y(), |
| 144 | stackLocation.x() + MemoryCellGraphicsItem::boxWidth * 1.2, stackLocation.y(), |
| 145 | QPen(QBrush(Qt::SolidPattern), 2, Qt::SolidLine)); |
| 146 | int dist = static_cast<int>(MemoryCellGraphicsItem::boxWidth * 1.2 - MemoryCellGraphicsItem::boxWidth * 1.4); |
| 147 | for (int i = static_cast<int>(MemoryCellGraphicsItem::boxWidth * 1.2); i > dist; i = i - 10) { |
| 148 | scene->addLine(stackLocation.x() + i - 10, stackLocation.y() + 10, |
| 149 | stackLocation.x() + i, stackLocation.y(), |
| 150 | QPen(QBrush(Qt::SolidPattern), 1, Qt::SolidLine)); |
| 151 | } |
| 152 | stackLocation.setY(stackLocation.y() - MemoryCellGraphicsItem::boxHeight); |
| 153 | |
| 154 | heapLocation.setY(globalLocation.y() - MemoryCellGraphicsItem::boxHeight); |
| 155 | |
| 156 | scene->setSceneRect(scene->itemsBoundingRect()); |
| 157 | ui->graphicsView->setScene(scene); |
| 158 | |
| 159 | ui->warningLabel->clear(); |
| 160 | |
| 161 | stackFrameFSM.reset(); |
| 162 | } |
| 163 | |
| 164 | void MemoryTracePane::updateMemoryTrace() |
| 165 | { |
| 166 | // Color all of the cells normally (globals) |
| 167 | for (int i = 0; i < globalVars.size(); i++) { |
| 168 | globalVars.at(i)->boxBgColor = Qt::white; |
| 169 | globalVars.at(i)->boxTextColor = Qt::black; |
| 170 | } |
| 171 | // Color all of the cells normally (stack) |
| 172 | for (int i = 0; i < runtimeStack.size(); i++) { |
| 173 | runtimeStack.at(i)->boxBgColor = Qt::white; |
| 174 | runtimeStack.at(i)->boxTextColor = Qt::black; |
| 175 | } |
| 176 | // Color all of the cells normally (heap) |
| 177 | for (int i = 0; i < heap.size(); i++) { |
| 178 | heap.at(i)->boxBgColor = Qt::white; |
| 179 | heap.at(i)->boxTextColor = Qt::black; |
| 180 | } |
| 181 | // Color the newest 'new' items on the heap light green |
| 182 | for (int i = 0; i < newestHeapItemsList.size(); i++) { |
| 183 | newestHeapItemsList.at(i)->boxBgColor = QColor(72, 255, 72, 192); |
| 184 | } |
| 185 | newestHeapItemsList.clear(); |
| 186 | |
| 187 | // Add cached stack items to the scene |
| 188 | for (int i = 0; i < runtimeStack.size(); i++) { |
| 189 | if (!isRuntimeStackItemAddedStack.at(i)) { |
| 190 | scene->addItem(runtimeStack.at(i)); |
| 191 | isRuntimeStackItemAddedStack[i] = true; |
| 192 | } |
| 193 | } |
| 194 | // Add cached stack FRAME items to the scene |
| 195 | for (int i = 0; i < isStackFrameAddedStack.size(); i++) { |
| 196 | if (!isStackFrameAddedStack.at(i)) { |
| 197 | scene->addItem(graphicItemsInStackFrame.at(i)); |
| 198 | isStackFrameAddedStack[i] = true; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Add cached heap items to the scene |
| 203 | for (int i = 0; i < isHeapItemAddedStack.size(); i++) { |
| 204 | if (!isHeapItemAddedStack.at(i)) { |
| 205 | scene->addItem(heap.at(i)); |
| 206 | isHeapItemAddedStack[i] = true; |
| 207 | } |
| 208 | } |
| 209 | for (int i = 0; i < isHeapFrameAddedStack.size(); i++) { |
| 210 | if (!isHeapFrameAddedStack.at(i)) { |
| 211 | scene->addItem(heapFrameItemStack.at(i)); |
| 212 | isHeapFrameAddedStack[i] = true; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Color global/stack/heap items red if they were modified last step |
| 217 | QList<int> modifiedBytesToBeUpdated = modifiedBytes.toList(); |
| 218 | for (int i = 0; i < bytesWrittenLastStep.size(); i++) { |
| 219 | if (addressToGlobalItemMap.contains(bytesWrittenLastStep.at(i))) { |
| 220 | addressToGlobalItemMap.value(bytesWrittenLastStep.at(i))->boxBgColor = Qt::red; |
| 221 | addressToGlobalItemMap.value(bytesWrittenLastStep.at(i))->boxTextColor = Qt::white; |
| 222 | } |
| 223 | if (addressToStackItemMap.contains(bytesWrittenLastStep.at(i))) { |
| 224 | addressToStackItemMap.value(bytesWrittenLastStep.at(i))->boxBgColor = Qt::red; |
| 225 | addressToStackItemMap.value(bytesWrittenLastStep.at(i))->boxTextColor = Qt::white; |
| 226 | } |
| 227 | if (addressToHeapItemMap.contains(bytesWrittenLastStep.at(i))) { |
| 228 | addressToHeapItemMap.value(bytesWrittenLastStep.at(i))->boxBgColor = Qt::red; |
| 229 | addressToHeapItemMap.value(bytesWrittenLastStep.at(i))->boxTextColor = Qt::white; |
| 230 | } |
| 231 | } |
| 232 | // Update modified cells |
| 233 | for (int i = 0; i < modifiedBytesToBeUpdated.size(); i++) { |
| 234 | if (addressToGlobalItemMap.contains(modifiedBytesToBeUpdated.at(i))) { |
| 235 | addressToGlobalItemMap.value(modifiedBytesToBeUpdated.at(i))->updateValue(); |
| 236 | } |
| 237 | if (addressToStackItemMap.contains(modifiedBytesToBeUpdated.at(i))) { |
| 238 | addressToStackItemMap.value(modifiedBytesToBeUpdated.at(i))->updateValue(); |
| 239 | } |
| 240 | if (addressToHeapItemMap.contains(modifiedBytesToBeUpdated.at(i))) { |
| 241 | addressToHeapItemMap.value(modifiedBytesToBeUpdated.at(i))->updateValue(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | scene->setSceneRect(scene->itemsBoundingRect()); |
| 246 | // This is time-consuming, but worthwhile to ensure scrollbars aren't going off into oblivion after items are removed. |
| 247 | // From the documentation for the 'itemsBoundingRect()' function: |
| 248 | // Calculates and returns the bounding rect of all items on the scene. |
| 249 | // This function works by iterating over all items, and because if this, it can be slow for large scenes. |
| 250 | // Our it is unlikely that this scene become very large (as in the 30,000 chips example), so I would expect this to remain |
| 251 | // a reasonable function call. It is also called in the 'setMemoryTrace' function, but the scene will have very few items at that point. |
| 252 | |
| 253 | scene->invalidate(); // redraw the scene! |
| 254 | // this is fast, so we do this for the whole scene instead of just certain boxes |
| 255 | |
| 256 | // Scroll to the top item if we have a scrollbar: |
| 257 | if (!runtimeStack.isEmpty() && ui->graphicsView->viewport()->height() < scene->height()) { |
| 258 | ui->graphicsView->centerOn(runtimeStack.top()); |
| 259 | } |
| 260 | |
| 261 | // Clear modified bytes so for the next update: |
| 262 | bytesWrittenLastStep.clear(); |
| 263 | modifiedBytes.clear(); |
| 264 | } |
| 265 | |
| 266 | void MemoryTracePane::cacheChanges() |
| 267 | { |
| 268 | modifiedBytes.unite(Sim::modifiedBytes); |
| 269 | if (Sim::tracingTraps) { |
| 270 | bytesWrittenLastStep.clear(); |
| 271 | bytesWrittenLastStep = Sim::modifiedBytes.toList(); |
| 272 | } |
| 273 | else if (Sim::trapped) { |
| 274 | // We delay for a single vonNeumann step so that we preserve the modified bytes until we leave the trap - this allows for |
| 275 | // recoloring of cells modified by a trap instruction. |
| 276 | delayLastStepClear = true; |
| 277 | bytesWrittenLastStep.append(Sim::modifiedBytes.toList()); |
| 278 | } |
| 279 | else if (delayLastStepClear) { |
| 280 | // Phew! We can now update (in updateMemoryTrace). If we don't, no harm done - they didn't want to see what happened in the trap |
| 281 | delayLastStepClear = false; |
| 282 | } |
| 283 | else { |
| 284 | // Clear the bytes written the step before last, and get the new list from the previous step. This is used in our update for coloring. |
| 285 | bytesWrittenLastStep.clear(); |
| 286 | bytesWrittenLastStep = Sim::modifiedBytes.toList(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void MemoryTracePane::cacheStackChanges() |
| 291 | { |
| 292 | if (Sim::trapped) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | // Look ahead for the symbol trace list (needs to be done here because of the possibility of call (can't look behind on a call) |
| 297 | // so we just do it for them all) |
| 298 | switch (Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)]) { |
| 299 | case Enu::SUBSP: |
| 300 | case Enu::CALL: |
| 301 | case Enu::RET0: |
| 302 | case Enu::RET1: |
| 303 | case Enu::RET2: |
| 304 | case Enu::RET3: |
| 305 | case Enu::RET4: |
| 306 | case Enu::RET5: |
| 307 | case Enu::RET6: |
| 308 | case Enu::RET7: |
| 309 | case Enu::ADDSP: |
| 310 | if (Pep::symbolTraceList.contains(Sim::programCounter)) { |
| 311 | lookAheadSymbolList = Pep::symbolTraceList.value(Sim::programCounter); |
| 312 | } |
| 313 | break; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | // End look ahead |
| 318 | |
| 319 | int multiplier = 0; |
| 320 | int bytesPerCell = 0; |
| 321 | int offset = 0; |
| 322 | int numCellsToAdd = 0; |
| 323 | int frameSizeToAdd = 0; |
| 324 | QString stackSymbol; |
| 325 | |
| 326 | switch (Pep::decodeMnemonic[Sim::instructionSpecifier]) { |
| 327 | case Enu::CALL: |
| 328 | { |
| 329 | MemoryCellGraphicsItem *item = new MemoryCellGraphicsItem(Sim::stackPointer, "retAddr", Enu::F_2H, |
| 330 | static_cast<int>(stackLocation.x()), static_cast<int>(stackLocation.y())); |
| 331 | item->updateValue(); |
| 332 | stackLocation.setY(stackLocation.y() - MemoryCellGraphicsItem::boxHeight); |
| 333 | |
| 334 | isRuntimeStackItemAddedStack.push(false); |
| 335 | runtimeStack.push(item); |
| 336 | addressToStackItemMap.insert(Sim::stackPointer, item); |
| 337 | frameSizeToAdd = stackFrameFSM.makeTransition(1); |
| 338 | } |
| 339 | break; |
| 340 | case Enu::SUBSP: |
| 341 | { |
| 342 | for (int i = 0; i < lookAheadSymbolList.size(); i++) { |
| 343 | stackSymbol = lookAheadSymbolList.at(i); |
| 344 | multiplier = Pep::symbolFormatMultiplier.value(stackSymbol); |
| 345 | if (multiplier == 1) { |
| 346 | offset += Sim::cellSize(Pep::symbolFormat.value(stackSymbol)); |
| 347 | MemoryCellGraphicsItem *item = new MemoryCellGraphicsItem(Sim::stackPointer - offset + Sim::operandSpecifier, |
| 348 | stackSymbol, |
| 349 | Pep::symbolFormat.value(stackSymbol), |
| 350 | static_cast<int>(stackLocation.x()), |
| 351 | static_cast<int>(stackLocation.y())); |
| 352 | item->updateValue(); |
| 353 | stackLocation.setY(stackLocation.y() - MemoryCellGraphicsItem::boxHeight); |
| 354 | isRuntimeStackItemAddedStack.push(false); |
| 355 | runtimeStack.push(item); |
| 356 | addressToStackItemMap.insert(Sim::stackPointer - offset + Sim::operandSpecifier, item); |
| 357 | numCellsToAdd++; |
| 358 | } |
| 359 | else { // This is an array! |
| 360 | bytesPerCell = Sim::cellSize(Pep::symbolFormat.value(stackSymbol)); |
| 361 | for (int j = multiplier - 1; j >= 0; j--) { |
| 362 | offset += bytesPerCell; |
| 363 | MemoryCellGraphicsItem *item = new MemoryCellGraphicsItem(Sim::stackPointer - offset + Sim::operandSpecifier, |
| 364 | stackSymbol + QString("[%1]").arg(j), |
| 365 | Pep::symbolFormat.value(stackSymbol), |
| 366 | static_cast<int>(stackLocation.x()), |
| 367 | static_cast<int>(stackLocation.y())); |
| 368 | item->updateValue(); |
| 369 | stackLocation.setY(stackLocation.y() - MemoryCellGraphicsItem::boxHeight); |
| 370 | isRuntimeStackItemAddedStack.push(false); |
| 371 | runtimeStack.push(item); |
| 372 | addressToStackItemMap.insert(Sim::stackPointer - offset + Sim::operandSpecifier, item); |
| 373 | numCellsToAdd++; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | // qDebug() << "numCellsToAdd before makeTransition in ADDSP: " << numCellsToAdd; |
| 378 | frameSizeToAdd = stackFrameFSM.makeTransition(numCellsToAdd); |
| 379 | } |
| 380 | break; |
| 381 | case Enu::RET0: |
| 382 | popBytes(2); |
| 383 | frameSizeToAdd = stackFrameFSM.makeTransition(0); // makeTransition(0) -> 0 bytes to add to the stack frame FSM. |
| 384 | break; |
| 385 | case Enu::RET1: |
| 386 | popBytes(3); |
| 387 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 388 | break; |
| 389 | case Enu::RET2: |
| 390 | popBytes(4); |
| 391 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 392 | break; |
| 393 | case Enu::RET3: |
| 394 | popBytes(5); |
| 395 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 396 | break; |
| 397 | case Enu::RET4: |
| 398 | popBytes(6); |
| 399 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 400 | break; |
| 401 | case Enu::RET5: |
| 402 | popBytes(7); |
| 403 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 404 | break; |
| 405 | case Enu::RET6: |
| 406 | popBytes(8); |
| 407 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 408 | break; |
| 409 | case Enu::RET7: |
| 410 | popBytes(9); |
| 411 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 412 | break; |
| 413 | case Enu::ADDSP: |
| 414 | popBytes(Sim::operandSpecifier); |
| 415 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 416 | break; |
| 417 | default: |
| 418 | frameSizeToAdd = stackFrameFSM.makeTransition(0); |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | if (frameSizeToAdd != 0) { |
| 423 | addStackFrame(frameSizeToAdd); |
| 424 | // This map is used to correlate the top of the stack frame with the frame itself, |
| 425 | // useful for determining when the frame should dissapear. |
| 426 | // IE: The top byte of the frame gets removed, so does the frame |
| 427 | stackHeightToStackFrameMap.insert(runtimeStack.size() - 1, graphicItemsInStackFrame.top()); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void MemoryTracePane::cacheHeapChanges() |
| 432 | { |
| 433 | if (Sim::trapped) { |
| 434 | return; |
| 435 | } |
| 436 | if (ui->warningLabel->text() != "") { |
| 437 | ui->warningLabel->clear(); |
| 438 | } |
| 439 | |
| 440 | if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::CALL && Pep::symbolTable.value("new") == Sim::operandSpecifier) { |
| 441 | newestHeapItemsList.clear(); |
| 442 | int numCellsToAdd = 0; |
| 443 | int offset = 0; |
| 444 | int multiplier; |
| 445 | QString heapSymbol; |
| 446 | int heapPointer; |
| 447 | if (Pep::symbolTable.contains("hpPtr")) { |
| 448 | heapPointer = Pep::symbolTable.value("hpPtr"); |
| 449 | } |
| 450 | else { |
| 451 | // We have no idea where the heap pointer is. Error! |
| 452 | ui->warningLabel->setText("Warning: hpPtr not found, unable to trace <code>CALL \'new\'</code>."); |
| 453 | return; |
| 454 | } |
| 455 | int listNumBytes = 0; |
| 456 | // Check and make sure the accumulator matches the number of bytes we're newing: |
| 457 | // We'll start by adding up the number of bytes... |
| 458 | for (int i = 0; i < lookAheadSymbolList.size(); i++) { |
| 459 | heapSymbol = lookAheadSymbolList.at(i); |
| 460 | if (Pep::equateSymbols.contains(heapSymbol) || Pep::blockSymbols.contains(heapSymbol)) { |
| 461 | // listNumBytes += number of bytes for that tag * the multiplier (IE, 2d4a is a 4 cell |
| 462 | // array of 2 byte decimals, where 2 is the multiplier and 4 is the number of cells. |
| 463 | // Note: the multiplier should always be 1 for new'd cells, but that's checked below, where we'll give a more specific error. |
| 464 | listNumBytes += Asm::tagNumBytes(Pep::symbolFormat.value(heapSymbol)) * Pep::symbolFormatMultiplier.value(heapSymbol); |
| 465 | } |
| 466 | } |
| 467 | if (listNumBytes != Sim::accumulator) { |
| 468 | ui->warningLabel->setText("Warning: The accumulator doesn't match the number of bytes in the trace tags"); |
| 469 | return; |
| 470 | } |
| 471 | for (int i = 0; i < lookAheadSymbolList.size(); i++) { |
| 472 | heapSymbol = lookAheadSymbolList.at(i); |
| 473 | if (Pep::equateSymbols.contains(heapSymbol) || Pep::blockSymbols.contains(heapSymbol)) { |
| 474 | multiplier = Pep::symbolFormatMultiplier.value(heapSymbol); |
| 475 | } |
| 476 | else { |
| 477 | ui->warningLabel->setText("Warning: Symbol \"" + heapSymbol + "\" not found in .equates, unknown size."); |
| 478 | return; |
| 479 | } |
| 480 | if (multiplier == 1) { // We can't support arrays on the stack with our current addressing modes. |
| 481 | // Very good! Have a cookie. Then, work! *cracks whip* (All our prereqs have been met to make an item) |
| 482 | moveHeapUpOneCell(); |
| 483 | MemoryCellGraphicsItem *item = new MemoryCellGraphicsItem(Sim::readWord(heapPointer) + offset, |
| 484 | heapSymbol, |
| 485 | Pep::symbolFormat.value(heapSymbol), |
| 486 | static_cast<int>(heapLocation.x()), |
| 487 | static_cast<int>(heapLocation.y())); |
| 488 | item->updateValue(); |
| 489 | isHeapItemAddedStack.push(false); |
| 490 | heap.push(item); |
| 491 | addressToHeapItemMap.insert(Sim::readWord(heapPointer) + offset, item); |
| 492 | newestHeapItemsList.append(item); |
| 493 | offset += Sim::cellSize(Pep::symbolFormat.value(heapSymbol)); |
| 494 | numCellsToAdd++; |
| 495 | } |
| 496 | } |
| 497 | if (numCellsToAdd != 0) { |
| 498 | addHeapFrame(numCellsToAdd); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void MemoryTracePane::highlightOnFocus() |
| 504 | { |
| 505 | if (ui->graphicsView->hasFocus() || ui->spinBox->hasFocus()) { |
| 506 | ui->label->setAutoFillBackground(true); |
| 507 | } |
| 508 | else { |
| 509 | ui->label->setAutoFillBackground(false); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | bool MemoryTracePane::hasFocus() |
| 514 | { |
| 515 | return ui->graphicsView->hasFocus() || ui->spinBox->hasFocus(); |
| 516 | } |
| 517 | |
| 518 | void MemoryTracePane::setFocus() |
| 519 | { |
| 520 | ui->graphicsView->setFocus(); |
| 521 | } |
| 522 | |
| 523 | void MemoryTracePane::onFontChanged(QFont font) |
| 524 | { |
| 525 | ui->graphicsView->setFont(font); |
| 526 | } |
| 527 | |
| 528 | void MemoryTracePane::addStackFrame(int numCells) |
| 529 | { |
| 530 | QPen pen(Qt::black); |
| 531 | pen.setWidth(4); |
| 532 | QGraphicsRectItem *item = new QGraphicsRectItem(stackLocation.x() - 2, stackLocation.y() + MemoryCellGraphicsItem::boxHeight, |
| 533 | static_cast<qreal>(MemoryCellGraphicsItem::boxWidth + 4), |
| 534 | static_cast<qreal>(MemoryCellGraphicsItem::boxHeight * numCells), 0); |
| 535 | item->setPen(pen); |
| 536 | graphicItemsInStackFrame.push(item); |
| 537 | isStackFrameAddedStack.push(false); |
| 538 | item->setZValue(1.0); // This moves the stack frame to the front |
| 539 | numCellsInStackFrame.push(numCells); |
| 540 | } |
| 541 | |
| 542 | void MemoryTracePane::addHeapFrame(int numCells) |
| 543 | { |
| 544 | QPen pen(Qt::black); |
| 545 | pen.setWidth(4); |
| 546 | QGraphicsRectItem *item = new QGraphicsRectItem(heapLocation.x() - 2, |
| 547 | heapLocation.y() - MemoryCellGraphicsItem::boxHeight * (numCells - 1), |
| 548 | static_cast<qreal>(MemoryCellGraphicsItem::boxWidth + 4), |
| 549 | static_cast<qreal>(MemoryCellGraphicsItem::boxHeight * numCells), 0); |
| 550 | item->setPen(pen); |
| 551 | heapFrameItemStack.push(item); |
| 552 | isHeapFrameAddedStack.push(false); |
| 553 | item->setZValue(1.0); // This moves the heap frame to the front |
| 554 | } |
| 555 | |
| 556 | void MemoryTracePane::moveHeapUpOneCell() |
| 557 | { |
| 558 | for (int i = 0; i < heap.size(); i++) { |
| 559 | heap.at(i)->moveBy(0, 0 - MemoryCellGraphicsItem::boxHeight); |
| 560 | } |
| 561 | for (int i = 0; i < heapFrameItemStack.size(); i++) { |
| 562 | heapFrameItemStack.at(i)->moveBy(0, 0 - MemoryCellGraphicsItem::boxHeight); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | void MemoryTracePane::popBytes(int bytesToPop) |
| 567 | { |
| 568 | while (bytesToPop > 0 && !runtimeStack.isEmpty()) { |
| 569 | if (stackHeightToStackFrameMap.contains(runtimeStack.size() - 1)) { |
| 570 | if (stackHeightToStackFrameMap.value(runtimeStack.size() - 1)->scene() == scene) { |
| 571 | scene->removeItem(stackHeightToStackFrameMap.value(runtimeStack.size() - 1)); |
| 572 | } |
| 573 | delete stackHeightToStackFrameMap.value(runtimeStack.size() - 1); |
| 574 | graphicItemsInStackFrame.pop(); |
| 575 | stackHeightToStackFrameMap.remove(runtimeStack.size() - 1); |
| 576 | isStackFrameAddedStack.pop(); |
| 577 | numCellsInStackFrame.pop(); |
| 578 | } |
| 579 | |
| 580 | if (runtimeStack.top()->scene() == scene) { |
| 581 | scene->removeItem(runtimeStack.top()); |
| 582 | } |
| 583 | addressToStackItemMap.remove(runtimeStack.top()->getAddress()); |
| 584 | bytesToPop -= runtimeStack.top()->getNumBytes(); |
| 585 | delete runtimeStack.top(); |
| 586 | runtimeStack.pop(); |
| 587 | isRuntimeStackItemAddedStack.pop(); |
| 588 | stackLocation.setY(stackLocation.y() + MemoryCellGraphicsItem::boxHeight); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void MemoryTracePane::mouseReleaseEvent(QMouseEvent *) |
| 593 | { |
| 594 | ui->graphicsView->setFocus(); |
| 595 | } |
| 596 | |
| 597 | void MemoryTracePane::zoomFactorChanged(int factor) |
| 598 | { |
| 599 | QMatrix matrix; |
| 600 | matrix.scale(factor * .01, factor * .01); |
| 601 | ui->graphicsView->setMatrix(matrix); |
| 602 | } |
| 603 | |
| 604 | void MemoryTracePane::mouseDoubleClickEvent(QMouseEvent *) |
| 605 | { |
| 606 | emit labelDoubleClicked(Enu::EMemoryTrace); |
| 607 | } |