Debian Codegraph @

cpupane.cpp

raw symbols SCIP
1 // File: cpupane.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 <QMessageBox>
22 #include <QKeyEvent>
23 // #include <QSound>
24 #include "cpupane.h"
25 #include "ui_cpupane.h"
26 #include "sim.h"
27 #include "pep.h"
28 #include <QtGlobal>
29
30 CpuPane::CpuPane(QWidget *parent) :
31 QWidget(parent),
32 ui(new Ui::CpuPane)
33 {
34 ui->setupUi(this);
35
36 connect(ui->singleStepPushButton, SIGNAL(clicked()), this, SLOT(singleStepButton()));
37 connect(ui->resumePushButton, SIGNAL(clicked()), this, SIGNAL(resumeButtonClicked()));
38
39 interruptExecutionFlag = false;
40 clearCpu();
41
42 if (Pep::getSystem() != "Mac") {
43 ui->cpuLabel->setFont(QFont(Pep::labelFont, Pep::labelFontSize));
44 ui->traceTrapsCheckBox->setFont(QFont(Pep::labelFont));
45
46 ui->accLabel->setFont(QFont(Pep::labelFont));
47 ui->xLabel->setFont(QFont(Pep::labelFont));
48 ui->spLabel->setFont(QFont(Pep::labelFont));
49 ui->pcLabel->setFont(QFont(Pep::labelFont));
50 ui->instrSpecLabel->setFont(QFont(Pep::labelFont));
51 ui->oprndSpecLabel->setFont(QFont(Pep::labelFont));
52 ui->oprndLabel->setFont(QFont(Pep::labelFont));
53 ui->singleStepPushButton->setFont(QFont(Pep::labelFont));
54 ui->resumePushButton->setFont(QFont(Pep::labelFont));
55
56 ui->pepNLabel->setFont(QFont(Pep::labelFont));
57 ui->pepZLabel->setFont(QFont(Pep::labelFont));
58 ui->pepVLabel->setFont(QFont(Pep::labelFont));
59 ui->pepCLabel->setFont(QFont(Pep::labelFont));
60
61 ui->nLabel->setFont(QFont(Pep::labelFont));
62 ui->zLabel->setFont(QFont(Pep::labelFont));
63 ui->vLabel->setFont(QFont(Pep::labelFont));
64 ui->cLabel->setFont(QFont(Pep::labelFont));
65
66 ui->accHexLabel->setFont(QFont(Pep::labelFont));
67 ui->accDecLabel->setFont(QFont(Pep::labelFont));
68
69 ui->xHexLabel->setFont(QFont(Pep::labelFont));
70 ui->xDecLabel->setFont(QFont(Pep::labelFont));
71
72 ui->spHexLabel->setFont(QFont(Pep::labelFont));
73 ui->spDecLabel->setFont(QFont(Pep::labelFont));
74
75 ui->pcHexLabel->setFont(QFont(Pep::labelFont));
76 ui->pcDecLabel->setFont(QFont(Pep::labelFont));
77
78 ui->instrSpecBinLabel->setFont(QFont(Pep::labelFont));
79 ui->instrSpecMnemonLabel->setFont(QFont(Pep::labelFont));
80
81 ui->oprndSpecHexLabel->setFont(QFont(Pep::labelFont));
82 ui->oprndSpecDecLabel->setFont(QFont(Pep::labelFont));
83 ui->oprndHexLabel->setFont(QFont(Pep::labelFont));
84 ui->oprndDecLabel->setFont(QFont(Pep::labelFont));
85 }
86 isCurrentlySimulating = false;
87 }
88
89 CpuPane::~CpuPane()
90 {
91 delete ui;
92 }
93
94 void CpuPane::updateCpu() {
95 Enu::EAddrMode addrMode = Pep::decodeAddrMode[Sim::instructionSpecifier];
96
97 ui->nLabel->setText(Sim::nBit ? "1" : "0");
98 ui->zLabel->setText(Sim::zBit ? "1" : "0");
99 ui->vLabel->setText(Sim::vBit ? "1" : "0");
100 ui->cLabel->setText(Sim::cBit ? "1" : "0");
101
102 ui->accHexLabel->setText(QString("0x") + QString("%1").arg(Sim::accumulator, 4, 16, QLatin1Char('0')).toUpper());
103 ui->accDecLabel->setText(QString("%1").arg(Sim::toSignedDecimal(Sim::accumulator)));
104
105 ui->xHexLabel->setText(QString("0x") + QString("%1").arg(Sim::indexRegister, 4, 16, QLatin1Char('0')).toUpper());
106 ui->xDecLabel->setText(QString("%1").arg(Sim::toSignedDecimal(Sim::indexRegister)));
107
108 ui->spHexLabel->setText(QString("0x") + QString("%1").arg(Sim::stackPointer, 4, 16, QLatin1Char('0')).toUpper());
109 ui->spDecLabel->setText(QString("%1").arg(Sim::stackPointer));
110
111 ui->pcHexLabel->setText(QString("0x") + QString("%1").arg(Sim::programCounter, 4, 16, QLatin1Char('0')).toUpper());
112 ui->pcDecLabel->setText(QString("%1").arg(Sim::programCounter));
113
114 ui->instrSpecBinLabel->setText(QString("%1").arg(Sim::instructionSpecifier, 8, 2, QLatin1Char('0')).toUpper());
115 ui->instrSpecMnemonLabel->setText(" " + Pep::enumToMnemonMap.value(Pep::decodeMnemonic[Sim::instructionSpecifier])
116 + Pep::commaSpaceToAddrMode(addrMode));
117
118 if (Pep::decodeAddrMode.value(Sim::instructionSpecifier) == Enu::NONE) {
119 ui->oprndSpecHexLabel->setText("");
120 ui->oprndSpecDecLabel->setText("");
121 ui->oprndHexLabel->setText("");
122 ui->oprndDecLabel->setText("");
123 }
124 else {
125 ui->oprndSpecHexLabel->setText(QString("0x") + QString("%1").arg(Sim::operandSpecifier, 4, 16, QLatin1Char('0')).toUpper());
126 ui->oprndSpecDecLabel->setText(QString("%1").arg(Sim::toSignedDecimal(Sim::operandSpecifier)));
127 ui->oprndHexLabel->setText(QString("0x") + QString("%1").arg(Sim::operand, Sim::operandDisplayFieldWidth, 16, QLatin1Char('0')).toUpper());
128 ui->oprndDecLabel->setText(QString("%1").arg(Sim::toSignedDecimal(Sim::operand)));
129 }
130 }
131
132 void CpuPane::clearCpu()
133 {
134 ui->nLabel->setText("");
135 ui->zLabel->setText("");
136 ui->vLabel->setText("");
137 ui->cLabel->setText("");
138
139 ui->accHexLabel->setText("");
140 ui->accDecLabel->setText("");
141
142 ui->xHexLabel->setText("");
143 ui->xDecLabel->setText("");
144
145 ui->spHexLabel->setText("");
146 ui->spDecLabel->setText("");
147
148 ui->pcHexLabel->setText("");
149 ui->pcDecLabel->setText("");
150
151 ui->instrSpecBinLabel->setText("");
152 ui->instrSpecMnemonLabel->setText("");
153
154 ui->oprndSpecHexLabel->setText("");
155 ui->oprndSpecDecLabel->setText("");
156 ui->oprndHexLabel->setText("");
157 ui->oprndDecLabel->setText("");
158
159 Sim::nBit = false;
160 Sim::zBit = false;
161 Sim::vBit = false;
162 Sim::cBit = false;
163
164 Sim::accumulator = 0;
165 Sim::indexRegister = 0;
166 Sim::stackPointer = 0; // Sim::readWord(Pep::dotBurnArgument - 7);
167 Sim::programCounter = 0;
168 }
169
170 void CpuPane::runClicked() {
171 ui->traceTrapsCheckBox->setChecked(false);
172 }
173
174 void CpuPane::setDebugState(bool b)
175 {
176 ui->traceTrapsCheckBox->setDisabled(b);
177 Sim::tracingTraps = ui->traceTrapsCheckBox->isChecked();
178 }
179
180 void CpuPane::traceTraps(bool b)
181 {
182 ui->traceTrapsCheckBox->setChecked(b);
183 }
184
185 void CpuPane::setButtonsEnabled(bool b) {
186 ui->resumePushButton->setDisabled(!b);
187 ui->singleStepPushButton->setDisabled(!b);
188 if (b) {
189 ui->singleStepPushButton->setFocus();
190 }
191 }
192
193 void CpuPane::runWithBatch()
194 {
195 isCurrentlySimulating = true;
196 interruptExecutionFlag = false;
197 QString errorString;
198 while (true) {
199 qApp->processEvents(); // To make sure that the event filter gets to handle keypresses during the run
200 if (Sim::vonNeumannStep(errorString)) {
201 emit vonNeumannStepped();
202 if (Sim::outputBuffer.length() == 1) {
203 emit appendOutput(Sim::outputBuffer);
204 Sim::outputBuffer = "";
205 }
206 }
207 else {
208 QMessageBox::warning(0, "Pep/8", errorString);
209 updateCpu();
210 emit executionComplete();
211 isCurrentlySimulating = false;
212 return;
213 }
214 if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::STOP) {
215 updateCpu();
216 emit executionComplete();
217 isCurrentlySimulating = false;
218 return;
219 }
220 if (interruptExecutionFlag) {
221 updateCpu();
222 isCurrentlySimulating = false;
223 return;
224 }
225 }
226 }
227
228 void CpuPane::runWithTerminal()
229 {
230 isCurrentlySimulating = true;
231 waiting = Enu::ERunWaiting;
232 interruptExecutionFlag = false;
233 QString errorString;
234 while (true) {
235 qApp->processEvents(); // To make sure that the event filter gets to handle keypresses during the run
236 if ((Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)] == Enu::CHARI) && Sim::inputBuffer.isEmpty()) {
237 // we are waiting for input
238 updateCpu();
239 emit waitingForInput();
240 isCurrentlySimulating = false;
241 return;
242 }
243 else {
244 if (Sim::vonNeumannStep(errorString)) {
245 emit vonNeumannStepped();
246 if (Sim::outputBuffer.length() == 1) {
247 emit appendOutput(Sim::outputBuffer);
248 Sim::outputBuffer = "";
249 }
250 }
251 else {
252 QMessageBox::warning(0, "Pep/8", errorString);
253 updateCpu();
254 emit executionComplete();
255 isCurrentlySimulating = false;
256 return;
257 }
258 if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::STOP) {
259 updateCpu();
260 emit executionComplete();
261 isCurrentlySimulating = false;
262 return;
263 }
264 if (interruptExecutionFlag) {
265 updateCpu();
266 emit updateSimulationView();
267 isCurrentlySimulating = false;
268 return;
269 }
270 }
271 }
272 }
273
274 void CpuPane::resumeWithBatch()
275 {
276 isCurrentlySimulating = true;
277 interruptExecutionFlag = false;
278 QString errorString;
279 while (true) {
280 qApp->processEvents(); // To make sure that the event filter gets to handle keypresses during the run
281 if (ui->traceTrapsCheckBox->isChecked()) {
282 trapLookahead();
283 }
284 else if (Pep::isTrapMap[Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)]]) {
285 Sim::trapped = true;
286 }
287 else if (Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)] == Enu::RETTR) {
288 Sim::trapped = false;
289 }
290 if (Sim::vonNeumannStep(errorString)) {
291 emit vonNeumannStepped();
292 if (Sim::outputBuffer.length() == 1) {
293 emit appendOutput(Sim::outputBuffer);
294 Sim::outputBuffer = "";
295 }
296 if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::STOP) {
297 emit updateSimulationView();
298 emit executionComplete();
299 isCurrentlySimulating = false;
300 return;
301 }
302 if (Pep::memAddrssToAssemblerListing->contains(Sim::programCounter) &&
303 Pep::listingRowChecked->value(Pep::memAddrssToAssemblerListing->value(Sim::programCounter)) == Qt::Checked) {
304 updateCpu();
305 emit updateSimulationView();
306 return;
307 }
308 }
309 else {
310 QMessageBox::warning(0, "Pep/8", errorString);
311 updateCpu();
312 emit updateSimulationView();
313 isCurrentlySimulating = false;
314 emit executionComplete();
315 }
316 if (interruptExecutionFlag) {
317 emit updateSimulationView();
318 isCurrentlySimulating = false;
319 return;
320 }
321 }
322 }
323
324 void CpuPane::resumeWithTerminal()
325 {
326 isCurrentlySimulating = true;
327 waiting = Enu::EDebugResumeWaiting;
328 interruptExecutionFlag = false;
329 QString errorString;
330 while (true) {
331 qApp->processEvents(); // To make sure that the event filter gets to handle keypresses during the run
332 trapLookahead();
333 if (Sim::trapped && !ui->traceTrapsCheckBox->isChecked()) {
334 updateCpu();
335 do {
336 trapLookahead();
337 qApp->processEvents(); // To make sure that the event filter gets to handle keypresses during the run
338 if ((Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)] == Enu::CHARI) && Sim::inputBuffer.isEmpty()) {
339 // we are waiting for input
340 ui->singleStepPushButton->setDisabled(true);
341 ui->resumePushButton->setDisabled(true);
342 emit waitingForInput();
343 isCurrentlySimulating = false;
344 return;
345 }
346 else {
347 if (Sim::vonNeumannStep(errorString)) {
348 emit vonNeumannStepped();
349 if (Sim::outputBuffer.length() == 1) {
350 emit appendOutput(Sim::outputBuffer);
351 Sim::outputBuffer = "";
352 }
353 if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::STOP) {
354 emit updateSimulationView();
355 emit executionComplete();
356 }
357 }
358 else {
359 QMessageBox::warning(0, "Pep/8", errorString);
360 emit updateSimulationView();
361 emit executionComplete();
362 isCurrentlySimulating = false;
363 }
364 }
365 if (interruptExecutionFlag) {
366 updateCpu();
367 emit updateSimulationView();
368 isCurrentlySimulating = false;
369 return;
370 }
371 } while (Sim::trapped);
372 }
373 else if ((Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)] == Enu::CHARI) && Sim::inputBuffer.isEmpty()) {
374 // we are waiting for input
375 ui->singleStepPushButton->setDisabled(true);
376 ui->resumePushButton->setDisabled(true);
377 emit updateSimulationView();
378 updateCpu();
379 emit waitingForInput();
380 isCurrentlySimulating = false;
381 return;
382 }
383 else {
384 if (Sim::vonNeumannStep(errorString)) {
385 emit vonNeumannStepped();
386 if (Sim::outputBuffer.length() == 1) {
387 emit appendOutput(Sim::outputBuffer);
388 Sim::outputBuffer = "";
389 }
390 if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::STOP) {
391 emit updateSimulationView(); // Finish updating the memory before we're done executing
392 emit executionComplete();
393 isCurrentlySimulating = false;
394 return;
395 }
396 if (Pep::memAddrssToAssemblerListing->contains(Sim::programCounter) &&
397 Pep::listingRowChecked->value(Pep::memAddrssToAssemblerListing->value(Sim::programCounter)) == Qt::Checked) {
398 updateCpu();
399 emit updateSimulationView();
400 isCurrentlySimulating = false;
401 return;
402 }
403 }
404 else {
405 QMessageBox::warning(0, "Pep/8", errorString);
406 updateCpu();
407 emit updateSimulationView();
408 emit executionComplete();
409 isCurrentlySimulating = false;
410 #pragma message ("should we return here?")
411 }
412 }
413 if (interruptExecutionFlag) {
414 isCurrentlySimulating = false;
415 return;
416 }
417 }
418 }
419
420 void CpuPane::singleStepWithBatch()
421 {
422 isCurrentlySimulating = true;
423 interruptExecutionFlag = false;
424 QString errorString;
425 trapLookahead();
426 if (Sim::trapped && !ui->traceTrapsCheckBox->isChecked()) {
427 QString errorString;
428 do {
429 trapLookahead();
430 qApp->processEvents();
431 if (Sim::vonNeumannStep(errorString)) {
432 emit vonNeumannStepped();
433 if (Sim::outputBuffer.length() == 1) {
434 emit appendOutput(Sim::outputBuffer);
435 Sim::outputBuffer = "";
436 }
437 if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::STOP) {
438 emit updateSimulationView();
439 emit executionComplete();
440 isCurrentlySimulating = false;
441 }
442 }
443 else {
444 QMessageBox::warning(0, "Pep/8", errorString);
445 emit updateSimulationView();
446 emit executionComplete();
447 isCurrentlySimulating = false;
448 }
449 if (interruptExecutionFlag) {
450 updateCpu();
451 emit updateSimulationView();
452 isCurrentlySimulating = false;
453 return;
454 }
455 } while (Sim::trapped);
456 emit updateSimulationView();
457 updateCpu();
458 }
459 else if (Sim::vonNeumannStep(errorString)) {
460 emit vonNeumannStepped();
461 emit updateSimulationView();
462 if (Sim::outputBuffer.length() == 1) {
463 emit appendOutput(Sim::outputBuffer);
464 Sim::outputBuffer = "";
465 }
466 if (Pep::decodeMnemonic[Sim::instructionSpecifier] != Enu::STOP) {
467 updateCpu();
468 }
469 else {
470 isCurrentlySimulating = false;
471 emit executionComplete();
472 }
473 }
474 else {
475 QMessageBox::warning(0, "Pep/8", errorString);
476 emit executionComplete();
477 }
478 isCurrentlySimulating = false;
479 }
480
481 void CpuPane::singleStepWithTerminal()
482 {
483 isCurrentlySimulating = true;
484 interruptExecutionFlag = false;
485 QString errorString;
486 waiting = Enu::EDebugSSWaiting;
487 trapLookahead();
488 if (Sim::trapped && !ui->traceTrapsCheckBox->isChecked()) {
489 updateCpu();
490 do {
491 trapLookahead();
492 qApp->processEvents();
493 if ((Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)] == Enu::CHARI) && Sim::inputBuffer.isEmpty()) {
494 // we are waiting for input
495 ui->singleStepPushButton->setDisabled(true);
496 ui->resumePushButton->setDisabled(true);
497 emit waitingForInput();
498 isCurrentlySimulating = false;
499 return;
500 }
501 else {
502 if (Sim::vonNeumannStep(errorString)) {
503 emit vonNeumannStepped();
504 if (Sim::outputBuffer.length() == 1) {
505 emit appendOutput(Sim::outputBuffer);
506 Sim::outputBuffer = "";
507 }
508 if (Pep::decodeMnemonic[Sim::instructionSpecifier] == Enu::STOP) {
509 emit updateSimulationView();
510 emit executionComplete();
511 isCurrentlySimulating = false;
512 }
513 }
514 else {
515 QMessageBox::warning(0, "Pep/8", errorString);
516 emit updateSimulationView();
517 emit executionComplete();
518 isCurrentlySimulating = false;
519 }
520 }
521 if (interruptExecutionFlag) {
522 updateCpu();
523 emit updateSimulationView();
524 isCurrentlySimulating = false;
525 return;
526 }
527 } while (Sim::trapped);
528 emit updateSimulationView();
529 }
530 else if ((Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)] == Enu::CHARI) && Sim::inputBuffer.isEmpty()) {
531 ui->singleStepPushButton->setDisabled(true);
532 ui->resumePushButton->setDisabled(true);
533 emit waitingForInput();
534 isCurrentlySimulating = false;
535 }
536 else {
537 if (Sim::vonNeumannStep(errorString)) {
538 emit vonNeumannStepped();
539 emit updateSimulationView();
540 if (Sim::outputBuffer.length() == 1) {
541 emit appendOutput(Sim::outputBuffer);
542 Sim::outputBuffer = "";
543 }
544 if (Pep::decodeMnemonic[Sim::instructionSpecifier] != Enu::STOP) {
545 updateCpu();
546 }
547 else {
548 emit executionComplete();
549 }
550 }
551 else {
552 QMessageBox::warning(0, "Pep/8", errorString);
553 emit executionComplete();
554 return;
555 }
556 if (Pep::decodeMnemonic[Sim::instructionSpecifier] != Enu::STOP) {
557 updateCpu();
558 }
559 else {
560 emit executionComplete();
561 }
562 }
563 isCurrentlySimulating = false;
564 }
565
566 void CpuPane::trapLookahead()
567 {
568 if (Pep::isTrapMap[Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)]]) {
569 Sim::trapped = true;
570 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingOS;
571 Pep::listingRowChecked = &Pep::listingRowCheckedOS;
572 }
573 else if (Pep::decodeMnemonic[Sim::readByte(Sim::programCounter)] == Enu::RETTR) {
574 Sim::trapped = false;
575 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingProg;
576 Pep::listingRowChecked = &Pep::listingRowCheckedProg;
577 }
578 }
579
580 void CpuPane::interruptExecution()
581 {
582 interruptExecutionFlag = true;
583 }
584
585 void CpuPane::highlightOnFocus()
586 {
587 if (ui->singleStepPushButton->hasFocus()) {
588 ui->cpuLabel->setAutoFillBackground(true);
589 }
590 else {
591 ui->cpuLabel->setAutoFillBackground(false);
592 }
593 }
594
595 Enu::EWaiting CpuPane::waitingState()
596 {
597 return waiting;
598 }
599
600 bool CpuPane::singleStepHasFocus()
601 {
602 return ui->singleStepPushButton->isEnabled() && ui->singleStepPushButton->hasFocus();
603 }
604
605 bool CpuPane::hasFocus()
606 {
607 return ui->singleStepPushButton->hasFocus();
608 }
609
610 bool CpuPane::isSimulating()
611 {
612 return isCurrentlySimulating;
613 }
614
615 void CpuPane::giveSingleStepFocus()
616 {
617 // protect against this being used incorrectly:
618 if (ui->singleStepPushButton->isEnabled()) {
619 ui->singleStepPushButton->setFocus();
620 }
621 }
622
623 void CpuPane::mousePressEvent(QMouseEvent *)
624 {
625 if (ui->singleStepPushButton->isEnabled()) {
626 ui->singleStepPushButton->setFocus();
627 }
628 }
629
630 void CpuPane::singleStepButton()
631 {
632 ui->singleStepPushButton->setFocus();
633 emit singleStepButtonClicked();
634 }