Debian Codegraph @

mainwindow.cpp

raw symbols SCIP
1
2 // File: mainwindow.cpp
3 /*
4 Pep8-1 is a virtual machine for writing machine language and assembly
5 language programs.
6
7 Copyright (C) 2009 J. Stanley Warford, Pepperdine University
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include <QMessageBox>
23 #include <QDesktopServices>
24 #include <QFileDialog>
25 #include <QCloseEvent>
26 #include <QUrl>
27 #include <QSettings>
28 #include <QApplication>
29 #include <QPrinter>
30 #include <QPrintDialog>
31 #include <QFontDialog>
32 #include <QTextDocument>
33 #include <QFontDatabase>
34 #include <QTextStream>
35 #include <QDesktopWidget>
36 #include <QTextCodec>
37 #include "mainwindow.h"
38 #include "ui_mainwindow.h"
39 #include "pep.h"
40 #include "sim.h"
41
42 #include <QDebug>
43
44 MainWindow::MainWindow(QWidget *parent)
45 : QMainWindow(parent), ui(new Ui::MainWindowClass),codeFont(Pep::codeFont,Pep::codeFontSize)
46 {
47 ui->setupUi(this);
48
49 // Left pane setup
50 sourceCodePane = new SourceCodePane(ui->codeSplitter);
51 delete ui->SourceCodeWidgetPane;
52 objectCodePane = new ObjectCodePane(ui->codeSplitter);
53 delete ui->ObjectCodeWidgetPane;
54 assemblerListingPane = new AssemblerListingPane(ui->codeSplitter);
55 delete ui->AssemblerListingWidgetPane;
56 listingTracePane = new ListingTracePane(ui->traceSplitter);
57 delete ui->ListingTraceWidgetPane;
58 memoryTracePane = new MemoryTracePane(ui->traceSplitter);
59 delete ui->MemoryTraceWidgetPane;
60 ui->pepCodeTraceTab->setCurrentIndex(0);
61
62 // Middle pane setup
63 cpuPane = new CpuPane(ui->middleSplitter);
64 ui->middleSplitter->insertWidget(0, cpuPane); // Move cpu pane to index 0 (above I/O tabs).
65 delete ui->CpuWidgetPane;
66 inputPane = new InputPane(ui->batchSplitter);
67 delete ui->InputWidgetPane;
68 outputPane = new OutputPane(ui->batchSplitter);
69 delete ui->OutputWidgetPane;
70 terminalPane = new TerminalPane(ui->terminalSplitter);
71 delete ui->TerminalWidgetPane;
72 ui->pepInputOutputTab->setCurrentIndex(0);
73
74 // Right pane setup
75 memoryDumpPane = new MemoryDumpPane(ui->horizontalSplitter);
76 delete ui->MemoryDumpWidgetPane;
77
78 // Dialog boxes setup
79 redefineMnemonicsDialog = new RedefineMnemonicsDialog(this);
80 helpDialog = new HelpDialog(this);
81 aboutPepDialog = new AboutPep(this);
82
83 connect(helpDialog, SIGNAL(clicked()), this, SLOT(helpCopyToSourceButtonClicked()));
84
85 // Byte converter setup
86 byteConverterDec = new ByteConverterDec();
87 ui->byteConverterToolBar->addWidget(byteConverterDec);
88 byteConverterHex = new ByteConverterHex();
89 ui->byteConverterToolBar->addWidget(byteConverterHex);
90 byteConverterBin = new ByteConverterBin();
91 ui->byteConverterToolBar->addWidget(byteConverterBin);
92 byteConverterChar = new ByteConverterChar();
93 ui->byteConverterToolBar->addWidget(byteConverterChar);
94 byteConverterInstr = new ByteConverterInstr();
95 ui->byteConverterToolBar->addWidget(byteConverterInstr);
96 connect(byteConverterDec, SIGNAL(textEdited(const QString &)), this, SLOT(slotByteConverterDecEdited(const QString &)));
97 connect(byteConverterHex, SIGNAL(textEdited(const QString &)), this, SLOT(slotByteConverterHexEdited(const QString &)));
98 connect(byteConverterBin, SIGNAL(textEdited(const QString &)), this, SLOT(slotByteConverterBinEdited(const QString &)));
99 connect(byteConverterChar, SIGNAL(textEdited(const QString &)), this, SLOT(slotByteConverterCharEdited(const QString &)));
100
101 // Pep tables setup
102 Pep::initEnumMnemonMaps();
103 Pep::initAddrModesMap();
104 Pep::initMnemonicMaps();
105 Pep::initDecoderTables();
106
107 // Adjust initial configuration
108 ui->actionView_Code_CPU->setDisabled(true);
109 ui->horizontalSplitter->widget(2)->hide();
110 ui->pepCodeTraceTab->resize(QSize(800,1)); // Enlarge Code/Trace pane on left.
111 sourceCodePane->resize(QSize(1, 9001)); // Enlarge Source Code pane.
112 // memoryDumpPane->resize(QSize(500, 1)); // Enlarges the Memory Dump pane.
113
114 // Install OS into memory
115 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingOS;
116 Pep::listingRowChecked = &Pep::listingRowCheckedOS;
117 if (sourceCodePane->installDefaultOs()) {
118 sourceCodePane->getAssemblerListingList();
119 listingTracePane->setListingTrace(sourceCodePane->getAssemblerListingList(), sourceCodePane->getHasCheckBox());
120 ui->statusbar->showMessage("OS installed", 4000);
121 }
122 else {
123 ui->statusbar->showMessage("OS assembly failed", 4000);
124 }
125
126 // Focus highlighting, actions enable/disable
127 connect(qApp->instance(), SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(mainWindowUtilities(QWidget*, QWidget*)));
128 connect(objectCodePane, SIGNAL(undoAvailable(bool)), this, SLOT(setUndoability(bool)));
129 connect(objectCodePane, SIGNAL(redoAvailable(bool)), this, SLOT(setRedoability(bool)));
130 connect(sourceCodePane, SIGNAL(undoAvailable(bool)), this, SLOT(setUndoability(bool)));
131 connect(sourceCodePane, SIGNAL(redoAvailable(bool)), this, SLOT(setRedoability(bool)));
132 connect(inputPane, SIGNAL(undoAvailable(bool)), this, SLOT(setUndoability(bool)));
133 connect(inputPane, SIGNAL(redoAvailable(bool)), this, SLOT(setRedoability(bool)));
134 connect(terminalPane, SIGNAL(undoAvailable(bool)), this, SLOT(setUndoability(bool)));
135 connect(terminalPane, SIGNAL(redoAvailable(bool)), this, SLOT(setRedoability(bool)));
136
137 connect(sourceCodePane, SIGNAL(labelDoubleClicked(Enu::EPane)), this, SLOT(doubleClickedCodeLabel(Enu::EPane)));
138 connect(objectCodePane, SIGNAL(labelDoubleClicked(Enu::EPane)), this, SLOT(doubleClickedCodeLabel(Enu::EPane)));
139 connect(assemblerListingPane, SIGNAL(labelDoubleClicked(Enu::EPane)), this, SLOT(doubleClickedCodeLabel(Enu::EPane)));
140 connect(memoryTracePane, SIGNAL(labelDoubleClicked(Enu::EPane)), this, SLOT(doubleClickedCodeLabel(Enu::EPane)));
141 connect(listingTracePane, SIGNAL(labelDoubleClicked(Enu::EPane)), this, SLOT(doubleClickedCodeLabel(Enu::EPane)));
142
143 // Simulator signals
144 connect(cpuPane, SIGNAL(updateSimulationView()), this, SLOT(updateSimulationView()));
145 connect(cpuPane, SIGNAL(executionComplete()), this, SLOT(on_actionBuild_Stop_Debugging_triggered()));
146 connect(cpuPane, SIGNAL(appendOutput(QString)), this, SLOT(appendOutput(QString)));
147 connect(cpuPane, SIGNAL(resumeButtonClicked()), this, SLOT(resumeButtonClicked()));
148 connect(cpuPane, SIGNAL(singleStepButtonClicked()), this, SLOT(singleStepButtonClicked()));
149 connect(cpuPane, SIGNAL(vonNeumannStepped()), this, SLOT(vonNeumannStepped()));
150 connect(cpuPane, SIGNAL(waitingForInput()), this, SLOT(waitingForInput()));
151 connect(terminalPane, SIGNAL(inputReceived()), this, SLOT(inputReceived()));
152
153 // connect(ui->horizontalSplitter, SIGNAL(splitterMoved(int,int)), this, SLOT(resizeDocWidth(int,int)));
154 //Handle synchronizing fonts
155 connect(this,SIGNAL(fontChanged(QFont)),sourceCodePane,SLOT(onFontChanged(QFont)));
156 connect(this,SIGNAL(fontChanged(QFont)),objectCodePane,SLOT(onFontChanged(QFont)));
157 connect(this,SIGNAL(fontChanged(QFont)),assemblerListingPane,SLOT(onFontChanged(QFont)));
158 connect(this,SIGNAL(fontChanged(QFont)),listingTracePane,SLOT(onFontChanged(QFont)));
159 connect(this,SIGNAL(fontChanged(QFont)),memoryTracePane,SLOT(onFontChanged(QFont)));
160 connect(this,SIGNAL(fontChanged(QFont)),inputPane,SLOT(onFontChanged(QFont)));
161 connect(this,SIGNAL(fontChanged(QFont)),outputPane,SLOT(onFontChanged(QFont)));
162 connect(this,SIGNAL(fontChanged(QFont)),terminalPane,SLOT(onFontChanged(QFont)));
163 connect(this,SIGNAL(fontChanged(QFont)),memoryDumpPane,SLOT(onFontChanged(QFont)));
164 connect(this,SIGNAL(fontChanged(QFont)),helpDialog,SLOT(onFontChanged(QFont)));
165 readSettings();
166
167 // Recent files
168 for (int i = 0; i < MaxRecentFiles; ++i) {
169 recentFileActs[i] = new QAction(this);
170 recentFileActs[i]->setVisible(false);
171 connect(recentFileActs[i], SIGNAL(triggered()), this, SLOT(openRecentFile()));
172 }
173 separatorAct = ui->menu_File->addSeparator();
174 for (int i = 0; i < MaxRecentFiles; ++i)
175 ui->menu_File->addAction(recentFileActs[i]);
176 updateRecentFileActions();
177
178 memoryDumpPane->refreshMemory();
179
180 qApp->installEventFilter(this);
181 memoryTracePane->hide(); // Hidden until successful assembly with trace tags.
182 }
183
184 MainWindow::~MainWindow()
185 {
186 delete ui;
187 }
188
189 // Protected closeEvent
190 void MainWindow::closeEvent(QCloseEvent *event)
191 {
192 if (maybeSaveSource() && maybeSaveObject()) {
193 writeSettings();
194 cpuPane->interruptExecution();
195 event->accept();
196 }
197 else {
198 event->ignore();
199 }
200 }
201
202 // Save methods
203 bool MainWindow::saveSource()
204 {
205 if (curSourceFile.isEmpty()) {
206 return on_actionFile_Save_Source_As_triggered();
207 }
208 else {
209 return saveFileSource(curSourceFile);
210 }
211 }
212
213 bool MainWindow::saveObject()
214 {
215 if (curObjectFile.isEmpty()) {
216 return on_actionFile_Save_Object_As_triggered();
217 }
218 else {
219 return saveFileObject(curObjectFile);
220 }
221 }
222
223 void MainWindow::readSettings()
224 {
225 QSettings settings("cslab.pepperdine","Pep8");
226 settings.beginGroup("MainWindow");
227 QDesktopWidget *desktop = QApplication::desktop();
228 int width = static_cast<int>(desktop->width() * 0.80);
229 int height = static_cast<int>(desktop->height() * 0.70);
230 int screenWidth = desktop->width();
231 int screenHeight = desktop->height();
232 QPoint pos = settings.value("pos", QPoint((screenWidth - width) / 2, (screenHeight - height) / 2)).toPoint();
233 QSize size = settings.value("size", QSize(width, height)).toSize();
234 if (Pep::getSystem() == "Mac") {
235 pos.setY(pos.y() + 20); // Every time the app launches, it seems OSX moves the window 20 pixels up the screen, so we compensate here.
236 }
237 else if (Pep::getSystem() == "Linux") { // Linux has a similar issue, so compensate here.
238 pos.setY(pos.y() - 20);
239 }
240 if (pos.x() > width || pos.x() < 0 || pos.y() > height || pos.y() < 0) {
241 pos = QPoint(0, 0);
242 }
243 resize(size);
244 move(pos);
245 QVariant val = settings.value("font",codeFont);
246 if(val.canConvert<QFont>())
247 {
248 codeFont = qvariant_cast<QFont>(val);
249 }
250 emit fontChanged(codeFont);
251 curPath = settings.value("filePath", QDir::homePath()).toString();
252 settings.endGroup();
253 }
254
255 void MainWindow::writeSettings()
256 {
257 QSettings settings("cslab.pepperdine","Pep8");
258 settings.beginGroup("MainWindow");
259 settings.setValue("pos", pos());
260 settings.setValue("size", size());
261 settings.setValue("filePath", curPath);
262 settings.setValue("font",codeFont);
263 settings.endGroup();
264 }
265
266 bool MainWindow::maybeSaveSource()
267 {
268 if (sourceCodePane->isModified()) {
269 QMessageBox::StandardButton ret;
270 ret = QMessageBox::warning(this, "Pep/8",
271 "The source code has been modified.\n"
272 "Do you want to save your changes?",
273 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
274 if (ret == QMessageBox::Save)
275 return saveSource();
276 else if (ret == QMessageBox::Cancel)
277 return false;
278 }
279 return true;
280 }
281
282 bool MainWindow::maybeSaveObject()
283 {
284 if (objectCodePane->isModified()) {
285 QMessageBox::StandardButton ret;
286 ret = QMessageBox::warning(this, "Pep/8",
287 "The object code has been modified.\n"
288 "Do you want to save your changes?",
289 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
290 if (ret == QMessageBox::Save)
291 return saveObject();
292 else if (ret == QMessageBox::Cancel)
293 return false;
294 }
295 return true;
296 }
297
298 void MainWindow::loadFile(const QString &fileName)
299 {
300 QFile file(fileName);
301 if (!file.open(QFile::ReadOnly | QFile::Text)) {
302 QMessageBox::warning(this, tr("Application"),
303 tr("Cannot read file %1:\n%2.")
304 .arg(fileName)
305 .arg(file.errorString()));
306 return;
307 }
308
309 QTextStream in(&file);
310 in.setCodec(QTextCodec::codecForName("ISO 8859-1"));
311 QApplication::setOverrideCursor(Qt::WaitCursor);
312 QRegExp rx(".*.pepo");
313 Enu::EPane pane;
314 if (rx.exactMatch(strippedName(fileName))) {
315 if (maybeSaveObject()) {
316 // Set object code pane text
317 objectCodePane->setObjectCodePaneText(in.readAll());
318 pane = Enu::EObject;
319 setCurrentFile(fileName, pane);
320 statusBar()->showMessage(tr("File loaded"), 4000);
321 }
322 }
323 else if (maybeSaveSource()) {
324 // Set source code pane text
325 sourceCodePane->setSourceCodePaneText(in.readAll());
326 pane = Enu::ESource;
327 setCurrentFile(fileName, pane);
328 statusBar()->showMessage(tr("File loaded"), 4000);
329 }
330 QApplication::restoreOverrideCursor();
331 }
332
333 bool MainWindow::saveFileSource(const QString &fileName)
334 {
335 QFile file(fileName);
336 if (!file.open(QFile::WriteOnly | QFile::Text)) {
337 QMessageBox::warning(this, tr("Application"),
338 tr("Cannot write file %1:\n%2.")
339 .arg(fileName)
340 .arg(file.errorString()));
341 return false;
342 }
343
344 QTextStream out(&file);
345 out.setCodec(QTextCodec::codecForName("ISO 8859-1"));
346 QApplication::setOverrideCursor(Qt::WaitCursor);
347 out << sourceCodePane->toPlainText();
348 QApplication::restoreOverrideCursor();
349
350 setCurrentFile(fileName, Enu::ESource);
351 statusBar()->showMessage("Source saved", 4000);
352 return true;
353 }
354
355 bool MainWindow::saveFileObject(const QString &fileName) // Copied and pasted, change.
356 {
357 QFile file(fileName);
358 if (!file.open(QFile::WriteOnly | QFile::Text)) {
359 QMessageBox::warning(this, tr("Application"),
360 tr("Cannot write file %1:\n%2.")
361 .arg(fileName)
362 .arg(file.errorString()));
363 return false;
364 }
365
366 QTextStream out(&file);
367 out.setCodec(QTextCodec::codecForName("ISO 8859-1"));
368 QApplication::setOverrideCursor(Qt::WaitCursor);
369 out << objectCodePane->toPlainText();
370 QApplication::restoreOverrideCursor();
371
372 setCurrentFile(fileName, Enu::EObject);
373 statusBar()->showMessage("Object code saved", 4000);
374 return true;
375 }
376
377 bool MainWindow::saveFileListing(const QString &fileName)
378 {
379 QFile file(fileName);
380 if (!file.open(QFile::WriteOnly | QFile::Text)) {
381 QMessageBox::warning(this, tr("Pep/8"),
382 tr("Cannot write file %1:\n%2.")
383 .arg(fileName)
384 .arg(file.errorString()));
385 return false;
386 }
387
388 QTextStream out(&file);
389 out.setCodec(QTextCodec::codecForName("ISO 8859-1"));
390 QApplication::setOverrideCursor(Qt::WaitCursor);
391 out << assemblerListingPane->toPlainText();
392 QApplication::restoreOverrideCursor();
393
394 setCurrentFile(fileName, Enu::EListing);
395 statusBar()->showMessage("Assembler listing saved", 4000);
396 return true;
397 }
398
399 void MainWindow::setCurrentFile(const QString &fileName, Enu::EPane pane)
400 {
401 if (pane == Enu::ESource) {
402 curSourceFile = fileName;
403 sourceCodePane->setModifiedFalse();
404 }
405 else if (pane == Enu::EObject) {
406 curObjectFile = fileName;
407 objectCodePane->setModifiedFalse();
408 }
409 else if (pane == Enu::EListing) {
410 curListingFile = fileName;
411 }
412
413 if (!fileName.isEmpty()) {
414 curPath = QFileInfo(fileName).path();
415 }
416
417 QString shownName;
418 if (pane == Enu::ESource) {
419 if (curSourceFile.isEmpty()) {
420 shownName = "untitled.pep";
421 }
422 else {
423 shownName = strippedName(curSourceFile);
424 }
425 sourceCodePane->setCurrentFile(shownName);
426 }
427 else if (pane == Enu::EObject) {
428 if (curObjectFile.isEmpty()) {
429 shownName = "untitled.pepo";
430 }
431 else {
432 shownName = strippedName(curObjectFile);
433 }
434 objectCodePane->setCurrentFile(shownName);
435 }
436 else if (pane == Enu::EListing) {
437 if (curListingFile.isEmpty()) {
438 shownName = "untitled.pepl";
439 }
440 else {
441 shownName = strippedName(curListingFile);
442 }
443 assemblerListingPane->setCurrentFile(shownName);
444 }
445 }
446
447 QString MainWindow::strippedName(const QString &fullFileName)
448 {
449 return QFileInfo(fullFileName).fileName();
450 }
451
452 // Recent files:
453 void MainWindow::updateRecentFileActions()
454 {
455 QSettings settings("Pep8", "Recent Files");
456 QStringList files = settings.value("recentFileList").toStringList();
457
458 int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
459
460 for (int i = 0; i < numRecentFiles; ++i) {
461 QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
462 recentFileActs[i]->setText(text);
463 recentFileActs[i]->setData(files[i]);
464 recentFileActs[i]->setVisible(true);
465 }
466 for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
467 recentFileActs[j]->setVisible(false);
468
469 separatorAct->setVisible(numRecentFiles > 0);
470 }
471
472 bool MainWindow::assemble()
473 {
474 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingProg;
475 Pep::listingRowChecked = &Pep::listingRowCheckedProg;
476 Pep::burnCount = 0;
477 if (sourceCodePane->assemble()) {
478 if (Pep::burnCount > 0) {
479 QString errorString = ";ERROR: .BURN not allowed in program unless installing OS.";
480 sourceCodePane->appendMessageInSourceCodePaneAt(0, errorString);
481 assemblerListingPane->clearAssemblerListing();
482 objectCodePane->clearObjectCode();
483 listingTracePane->clearListingTrace();
484 ui->pepCodeTraceTab->setCurrentIndex(0); // Make source code pane visible
485 return false;
486 }
487 else {
488 objectCodePane->setObjectCode(sourceCodePane->getObjectCode());
489 assemblerListingPane->setAssemblerListing(sourceCodePane->getAssemblerListingList());
490 listingTracePane->setListingTrace(sourceCodePane->getAssemblerListingList(), sourceCodePane->getHasCheckBox());
491 memoryTracePane->setMemoryTrace();
492 listingTracePane->showAssemblerListing();
493
494 QString temp = curSourceFile;
495 if (!curSourceFile.isEmpty()) {
496 if (temp.endsWith(".pep", Qt::CaseInsensitive) || temp.endsWith(".txt", Qt::CaseInsensitive)) {
497 temp.chop(4);
498 }
499 temp.append(".pepo");
500 curObjectFile = temp;
501 setCurrentFile(curObjectFile, Enu::EObject);
502 temp.chop(5);
503 temp.append(".pepl");
504 curListingFile = temp;
505 setCurrentFile(curListingFile, Enu::EListing);
506 }
507 else {
508 setCurrentFile("", Enu::EObject);
509 setCurrentFile("", Enu::EListing);
510 }
511 ui->actionEdit_Format_From_Listing->setEnabled(true);
512 if (!Pep::traceTagWarning && !(Pep::blockSymbols.isEmpty()
513 && Pep::equateSymbols.isEmpty()
514 && Pep::globalStructSymbols.isEmpty()
515 && Pep::symbolFormat.isEmpty()
516 && Pep::symbolFormatMultiplier.isEmpty())) {
517 memoryTracePane->show();
518 }
519 else {
520 memoryTracePane->hide();
521 }
522 ui->actionBuild_Start_Debugging_Source->setEnabled(true);
523 return true;
524 }
525 }
526 assemblerListingPane->clearAssemblerListing();
527 objectCodePane->clearObjectCode();
528 listingTracePane->clearListingTrace();
529 ui->pepCodeTraceTab->setCurrentIndex(0); // Make source code pane visible
530 return false;
531 }
532
533 bool MainWindow::load()
534 {
535 QList<int> objectCodeList;
536 if (objectCodePane->getObjectCode(objectCodeList)) {
537 Sim::loadMem(objectCodeList);
538 memoryDumpPane->refreshMemoryLines(0, objectCodeList.size());
539 return true;
540 }
541 return false;
542 }
543
544 void MainWindow::setDebugState(bool b)
545 {
546 ui->actionFile_New->setDisabled(b);
547 ui->actionFile_Open->setDisabled(b);
548 ui->actionEdit_Format_From_Listing->setDisabled(b);
549 ui->actionEdit_Remove_Error_Messages->setDisabled(b);
550 ui->actionBuild_Assemble->setDisabled(b);
551 ui->actionBuild_Load->setDisabled(b);
552 ui->actionBuild_Execute->setDisabled(b);
553 ui->actionBuild_Run_Source->setDisabled(b);
554 ui->actionBuild_Start_Debugging_Source->setDisabled(b);
555 ui->actionBuild_Run_Object->setDisabled(b);
556 ui->actionBuild_Start_Debugging_Object->setDisabled(b);
557 ui->actionBuild_Start_Debugging_Loader->setDisabled(b);
558 ui->actionBuild_Stop_Debugging->setDisabled(!b);
559 ui->actionBuild_Interrupt_Execution->setDisabled(!b);
560 ui->actionSystem_Clear_Memory->setDisabled(b);
561 ui->actionSystem_Redefine_Mnemonics->setDisabled(b);
562 ui->actionSystem_Assemble_Install_New_OS->setDisabled(b);
563 ui->actionSystem_Reinstall_Default_OS->setDisabled(b);
564 inputPane->setReadOnly(b);
565 cpuPane->setDebugState(b);
566 sourceCodePane->setReadOnly(b);
567 objectCodePane->setReadOnly(b);
568 helpDialog->setCopyButtonDisabled(b);
569 if (b) {
570 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingProg;
571 Pep::listingRowChecked = &Pep::listingRowCheckedProg;
572 }
573 else {
574 ui->pepInputOutputTab->setTabEnabled(1, true);
575 ui->pepInputOutputTab->setTabEnabled(0, true);
576 }
577 }
578
579 bool MainWindow::eventFilter(QObject *, QEvent *event)
580 {
581 if (event->type() == QEvent::KeyPress) {
582 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
583 if ((keyEvent->modifiers() & Qt::ControlModifier) && (keyEvent->key() == Qt::Key_Period)) {
584 on_actionBuild_Interrupt_Execution_triggered();
585 return true;
586 }
587 else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
588 if (cpuPane->singleStepHasFocus()) {
589 if (cpuPane->isSimulating()) {
590 return true;
591 }
592 singleStepButtonClicked();
593 return true;
594 }
595 else if ((listingTracePane->hasFocus() || memoryTracePane->hasFocus()) && ui->actionBuild_Stop_Debugging->isEnabled()) {
596 // listing trace or memory trace has focus, and we are debugging
597 cpuPane->giveSingleStepFocus();
598 return true;
599 }
600 }
601 else if (keyEvent->key() == Qt::Key_Tab) {
602 if (inputPane->hasFocus()) {
603 inputPane->tab();
604 return true;
605 }
606 else if (sourceCodePane->hasFocus()) {
607 sourceCodePane->tab();
608 return true;
609 }
610 }
611 }
612 else if (event->type() == QEvent::FileOpen) {
613 if (ui->actionBuild_Stop_Debugging->isEnabled()) {
614 ui->statusbar->showMessage("Open failed, currently debugging.", 4000);
615 return false;
616 }
617 loadFile(static_cast<QFileOpenEvent *>(event)->file());
618 return true;
619 }
620 return false;
621 }
622
623 // File MainWindow triggers
624 void MainWindow::on_actionFile_New_triggered()
625 {
626 if (maybeSaveSource() && maybeSaveObject()) {
627 sourceCodePane->clearSourceCode();
628 objectCodePane->clearObjectCode();
629 assemblerListingPane->clearAssemblerListing();
630 listingTracePane->clearListingTrace();
631 cpuPane->clearCpu();
632 outputPane->clearOutput();
633 ui->pepCodeTraceTab->setCurrentIndex(0);
634 setCurrentFile("", Enu::ESource);
635 setCurrentFile("", Enu::EObject);
636 setCurrentFile("", Enu::EListing);
637 ui->actionBuild_Start_Debugging_Source->setEnabled(false);
638 }
639 }
640
641 void MainWindow::on_actionFile_Open_triggered()
642 {
643 if (maybeSaveSource()) {
644 QString fileName = QFileDialog::getOpenFileName(
645 this,
646 "Open text file",
647 curPath,
648 "Text files (*.pepo *.txt *.pep)");
649 if (!fileName.isEmpty()) {
650 loadFile(fileName);
651 ui->pepCodeTraceTab->setCurrentIndex(0);
652 curPath = QFileInfo(fileName).path();
653 ui->actionBuild_Start_Debugging_Source->setEnabled(false);
654 }
655 }
656 }
657
658 bool MainWindow::on_actionFile_Save_Source_triggered()
659 {
660 if (curSourceFile.isEmpty()) {
661 return on_actionFile_Save_Source_As_triggered();
662 }
663 else {
664 return saveFileSource(curSourceFile);
665 }
666 }
667
668 bool MainWindow::on_actionFile_Save_Source_As_triggered()
669 {
670 QString fileName = QFileDialog::getSaveFileName(
671 this,
672 "Save Source Code",
673 curSourceFile.isEmpty() ? curPath + "/untitled.pep" : curPath + "/" + strippedName(curSourceFile),
674 "Pep8 Source (*.pep *.txt)");
675 if (fileName.isEmpty())
676 return false;
677
678 QSettings settings("Pep8", "Recent Files");
679 QStringList files = settings.value("recentFileList").toStringList();
680 files.removeAll(fileName);
681 files.prepend(fileName);
682 while (files.size() > MaxRecentFiles)
683 files.removeLast();
684
685 settings.setValue("recentFileList", files);
686
687 foreach (QWidget *widget, QApplication::topLevelWidgets()) {
688 MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
689 if (mainWin)
690 mainWin->updateRecentFileActions();
691 }
692
693 return saveFileSource(fileName);
694 }
695
696 bool MainWindow::on_actionFile_Save_Object_As_triggered()
697 {
698 QString fileName = QFileDialog::getSaveFileName(
699 this,
700 "Save Object Code",
701 curObjectFile.isEmpty() ? curPath + "/untitled.pepo" : curPath + "/" + strippedName(curObjectFile),
702 "Pep8 Object (*.pepo *.txt)");
703 if (fileName.isEmpty())
704 return false;
705
706 QSettings settings("Pep8", "Recent Files");
707 QStringList files = settings.value("recentFileList").toStringList();
708 files.removeAll(fileName);
709 files.prepend(fileName);
710 while (files.size() > MaxRecentFiles)
711 files.removeLast();
712
713 settings.setValue("recentFileList", files);
714
715 foreach (QWidget *widget, QApplication::topLevelWidgets()) {
716 MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
717 if (mainWin)
718 mainWin->updateRecentFileActions();
719 }
720
721 return saveFileObject(fileName);
722 }
723
724 bool MainWindow::on_actionFile_Save_Listing_As_triggered()
725 {
726 QString fileName = QFileDialog::getSaveFileName(
727 this,
728 "Save Assembler Listing",
729 curListingFile.isEmpty() ? curPath + "/untitled.pepl" : curPath + "/" + strippedName(curListingFile),
730 "Pep8 Listing (*.pepl)");
731 if (fileName.isEmpty()) {
732 return false;
733 }
734 return saveFileListing(fileName);
735 }
736
737 void MainWindow::on_actionFile_Print_Source_triggered()
738 {
739 QTextDocument document(sourceCodePane->toPlainText(), this);
740 document.setDefaultFont(QFont("Courier", 10, -1));
741
742 QPrinter printer(QPrinter::HighResolution);
743
744 QPrintDialog *dialog = new QPrintDialog(&printer, this);
745 dialog->setWindowTitle("Print Source Code");
746 if (dialog->exec() == QDialog::Accepted) {
747 document.print(&printer);
748 }
749 }
750
751 void MainWindow::on_actionFile_Print_Object_triggered()
752 {
753 QTextDocument document(objectCodePane->toPlainText(), this);
754 document.setDefaultFont(QFont("Courier", 10, -1));
755
756 QPrinter printer(QPrinter::HighResolution);
757
758 QPrintDialog *dialog = new QPrintDialog(&printer, this);
759 dialog->setWindowTitle("Print Object Code");
760 if (dialog->exec() == QDialog::Accepted) {
761 document.print(&printer);
762 }
763 }
764
765 void MainWindow::on_actionFile_Print_Listing_triggered()
766 {
767 QTextDocument document(assemblerListingPane->toPlainText(), this);
768 document.setDefaultFont(QFont("Courier", 10, -1));
769
770 QPrinter printer(QPrinter::HighResolution);
771
772 QPrintDialog *dialog = new QPrintDialog(&printer, this);
773 dialog->setWindowTitle("Print Assembler Listing");
774 if (dialog->exec() == QDialog::Accepted) {
775 document.print(&printer);
776 }
777
778 }
779
780 // Edit MainWindow triggers
781 void MainWindow::on_actionEdit_Undo_triggered()
782 {
783 if (sourceCodePane->hasFocus()) {
784 sourceCodePane->undo();
785 }
786 else if (objectCodePane->hasFocus()) {
787 objectCodePane->undo();
788 }
789 else if (inputPane->hasFocus()) {
790 inputPane->undo();
791 }
792 }
793
794 void MainWindow::on_actionEdit_Redo_triggered()
795 {
796 if (sourceCodePane->hasFocus()) {
797 sourceCodePane->redo();
798 }
799 else if (objectCodePane->hasFocus()) {
800 objectCodePane->redo();
801 }
802 else if (inputPane->hasFocus()) {
803 inputPane->redo();
804 }
805 }
806
807 void MainWindow::on_actionEdit_Cut_triggered()
808 {
809 if (sourceCodePane->hasFocus()) {
810 sourceCodePane->cut();
811 }
812 else if (objectCodePane->hasFocus()) {
813 objectCodePane->cut();
814 }
815 else if (inputPane->hasFocus()) {
816 inputPane->cut();
817 }
818 }
819
820 void MainWindow::on_actionEdit_Copy_triggered()
821 {
822 if (sourceCodePane->hasFocus()) {
823 sourceCodePane->copy();
824 }
825 else if (objectCodePane->hasFocus()) {
826 objectCodePane->copy();
827 }
828 else if (assemblerListingPane->hasFocus()) {
829 assemblerListingPane->copy();
830 }
831 else if (inputPane->hasFocus()) {
832 inputPane->copy();
833 }
834 else if (outputPane->hasFocus()) {
835 outputPane->copy();
836 }
837 else if (terminalPane->hasFocus()) {
838 terminalPane->copy();
839 }
840 else if (memoryDumpPane->hasFocus()) {
841 memoryDumpPane->copy();
842 }
843 else if (helpDialog->hasFocus()) {
844 helpDialog->copy();
845 }
846 }
847
848 void MainWindow::on_actionEdit_Paste_triggered()
849 {
850 if (sourceCodePane->hasFocus()) {
851 sourceCodePane->paste();
852 }
853 else if (objectCodePane->hasFocus()) {
854 objectCodePane->paste();
855 }
856 else if (inputPane->hasFocus()) {
857 inputPane->paste();
858 }
859 }
860
861 void MainWindow::on_actionEdit_Format_From_Listing_triggered()
862 {
863 QStringList assemblerListingList = sourceCodePane->getAssemblerListingList();
864 assemblerListingList.replaceInStrings(QRegExp("^............."), "");
865 assemblerListingList.removeAll("");
866 if (!assemblerListingList.isEmpty()) {
867 sourceCodePane->setSourceCodePaneText(assemblerListingList.join("\n"));
868 }
869 }
870 void MainWindow::on_actionReset_Fonts_to_Defaults_triggered()
871 {
872 codeFont = QFont(Pep::codeFont,Pep::codeFontSize);
873 emit fontChanged(codeFont);
874 }
875
876 void MainWindow::on_actionEdit_Font_triggered()
877 {
878 bool ok = false;
879 QFont font = QFontDialog::getFont(&ok, codeFont, this, "Set Source Code Font");
880 if (ok) {
881 codeFont = font;
882 emit fontChanged(codeFont);
883 }
884 }
885
886 void MainWindow::on_actionEdit_Remove_Error_Messages_triggered()
887 {
888 sourceCodePane->removeErrorMessages();
889 }
890
891 // Build MainWindow triggers
892 void MainWindow::on_actionBuild_Assemble_triggered()
893 {
894 if (assemble()) {
895 ui->statusbar->showMessage("Assembly succeeded", 4000);
896 }
897 else {
898 ui->statusbar->showMessage("Assembly failed", 4000);
899 }
900 }
901
902 void MainWindow::on_actionBuild_Load_triggered()
903 {
904 if (load()) {
905 ui->statusbar->showMessage("Load succeeded", 4000);
906 }
907 else {
908 ui->statusbar->showMessage("Load failed", 4000);
909 }
910 }
911
912 void MainWindow::on_actionBuild_Execute_triggered()
913 {
914 cpuPane->clearCpu();
915 Sim::stackPointer = Sim::readWord(Pep::dotBurnArgument - 7);
916 Sim::programCounter = 0x0000;
917 setDebugState(true);
918 Sim::trapped = false;
919 cpuPane->runClicked();
920 sourceCodePane->setReadOnly(true);
921 objectCodePane->setReadOnly(true);
922 if (ui->pepInputOutputTab->currentIndex() == 0) {
923 ui->pepInputOutputTab->setTabEnabled(1, false);
924 outputPane->clearOutput();
925 QString s = inputPane->toPlainText();
926 if (!s.endsWith("\n")) {
927 s.append("\n");
928 }
929 Sim::inputBuffer = s;
930 cpuPane->runWithBatch();
931 }
932 else {
933 ui->pepInputOutputTab->setTabEnabled(0, false);
934 Sim::inputBuffer.clear();
935 terminalPane->clearTerminal();
936 cpuPane->runWithTerminal();
937 }
938 // Other things may go here.
939 }
940
941 void MainWindow::on_actionBuild_Run_Source_triggered()
942 {
943 if (assemble() && load()) {
944 on_actionBuild_Execute_triggered();
945 }
946 }
947
948 void MainWindow::on_actionBuild_Start_Debugging_Source_triggered()
949 {
950 if (!assemblerListingPane->isEmpty() && load()) {
951 ui->statusbar->showMessage("Load succeeded", 4000);
952 Sim::stackPointer = Sim::readWord(Pep::dotBurnArgument - 7);
953 Sim::programCounter = 0x0000;
954
955 setDebugState(true);
956 Sim::trapped = false;
957
958 if (ui->pepInputOutputTab->currentIndex() == 0) {
959 ui->pepInputOutputTab->setTabEnabled(1, false);
960 outputPane->clearOutput();
961 QString s = inputPane->toPlainText();
962 if (!s.endsWith("\n")) {
963 s.append("\n");
964 }
965 Sim::inputBuffer = s;
966 }
967 else {
968 ui->pepInputOutputTab->setTabEnabled(0, false);
969 Sim::inputBuffer.clear();
970 terminalPane->clearTerminal();
971 }
972
973 ui->pepCodeTraceTab->setCurrentIndex(1); // Make listing trace pane visible
974
975 cpuPane->updateCpu();
976 listingTracePane->setDebuggingState(true);
977 cpuPane->setButtonsEnabled(true);
978
979 if (!memoryDumpPane->isHidden()) {
980 memoryDumpPane->highlightMemory(true);
981 }
982 if (!memoryTracePane->isHidden()) {
983 memoryTracePane->setMemoryTrace();
984 }
985 }
986 else {
987 if (assemblerListingPane->isEmpty()) {
988 ui->statusbar->showMessage("No assembler listing, cannot debug source", 4000);
989 }
990 else {
991 ui->statusbar->showMessage("Load failed", 4000);
992 }
993 }
994 }
995
996 void MainWindow::on_actionBuild_Run_Object_triggered()
997 {
998 if (load()) {
999 ui->statusbar->showMessage("Load succeeded", 4000);
1000 on_actionBuild_Execute_triggered();
1001 }
1002 else {
1003 ui->statusbar->showMessage("Load failed", 4000);
1004 }
1005 }
1006
1007 void MainWindow::on_actionBuild_Start_Debugging_Object_triggered()
1008 {
1009 if (load()) {
1010 Sim::stackPointer = Sim::readWord(Pep::dotBurnArgument - 7);
1011 Sim::programCounter = 0x0000;
1012
1013 setDebugState(true);
1014 Sim::trapped = false;
1015
1016 ui->statusbar->showMessage("Load succeeded", 4000);
1017 cpuPane->updateCpu();
1018 listingTracePane->setDebuggingState(true);
1019 if (ui->pepInputOutputTab->currentIndex() == 0) {
1020 ui->pepInputOutputTab->setTabEnabled(1, false);
1021 outputPane->clearOutput();
1022 QString s = inputPane->toPlainText();
1023 if (!s.endsWith("\n")) {
1024 s.append("\n");
1025 }
1026 Sim::inputBuffer = s;
1027 }
1028 else {
1029 ui->pepInputOutputTab->setTabEnabled(0, false);
1030 Sim::inputBuffer.clear();
1031 terminalPane->clearTerminal();
1032 }
1033
1034 cpuPane->setButtonsEnabled(true);
1035
1036 if (!memoryDumpPane->isHidden()) {
1037 memoryDumpPane->highlightMemory(true);
1038 }
1039 }
1040 else {
1041 ui->statusbar->showMessage("Load failed", 4000);
1042 }
1043 }
1044
1045 void MainWindow::on_actionBuild_Start_Debugging_Loader_triggered()
1046 {
1047 Sim::stackPointer = Sim::readWord(Pep::dotBurnArgument - 5);
1048 // 5 is the vector offset from the last byte of the OS for the System stack pointer
1049 Sim::programCounter = Sim::readWord(Pep::dotBurnArgument - 3);
1050 // 3 is the vector offset from the last byte of the OS for the Loader program counter
1051
1052 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingOS;
1053 Pep::listingRowChecked = &Pep::listingRowCheckedOS;
1054 Sim::trapped = true;
1055
1056 Sim::inputBuffer = objectCodePane->toPlainText();
1057 inputPane->setText(objectCodePane->toPlainText());
1058 ui->pepInputOutputTab->setCurrentIndex(0);
1059 ui->pepCodeTraceTab->setCurrentIndex(1);
1060
1061 ui->actionFile_New->setDisabled(true);
1062 ui->actionFile_Open->setDisabled(true);
1063 ui->actionEdit_Format_From_Listing->setDisabled(true);
1064 ui->actionBuild_Assemble->setDisabled(true);
1065 ui->actionBuild_Load->setDisabled(true);
1066 ui->actionBuild_Execute->setDisabled(true);
1067 ui->actionBuild_Run_Source->setDisabled(true);
1068 ui->actionBuild_Start_Debugging_Source->setDisabled(true);
1069 ui->actionBuild_Run_Object->setDisabled(true);
1070 ui->actionBuild_Start_Debugging_Object->setDisabled(true);
1071 ui->actionBuild_Start_Debugging_Loader->setDisabled(true);
1072 ui->actionBuild_Stop_Debugging->setDisabled(false);
1073 ui->actionBuild_Interrupt_Execution->setDisabled(false);
1074 ui->actionEdit_Remove_Error_Messages->setDisabled(true);
1075 inputPane->setReadOnly(true);
1076 sourceCodePane->setReadOnly(true);
1077 objectCodePane->setReadOnly(true);
1078 cpuPane->traceTraps(true);
1079 cpuPane->setDebugState(true);
1080 cpuPane->setButtonsEnabled(true);
1081 if (!memoryDumpPane->isHidden()) {
1082 memoryDumpPane->highlightMemory(true);
1083 }
1084 listingTracePane->setDebuggingState(true);
1085
1086 cpuPane->updateCpu();
1087 }
1088
1089 void MainWindow::on_actionBuild_Stop_Debugging_triggered()
1090 {
1091 cpuPane->interruptExecution();
1092 cpuPane->updateCpu();
1093 listingTracePane->updateListingTrace();
1094 if (cpuPane->singleStepHasFocus()) {
1095 if (ui->pepCodeTraceTab->currentIndex() == 0) {
1096 assemblerListingPane->setFocus();
1097 }
1098 else {
1099 if (!memoryTracePane->isHidden()) {
1100 memoryTracePane->setFocus();
1101 }
1102 else {
1103 listingTracePane->setFocus();
1104 }
1105 }
1106 }
1107 setDebugState(false);
1108 listingTracePane->setDebuggingState(false);
1109 cpuPane->setButtonsEnabled(false);
1110 memoryDumpPane->highlightMemory(false);
1111
1112 mainWindowUtilities(0, 0);
1113 }
1114
1115 void MainWindow::on_actionBuild_Interrupt_Execution_triggered()
1116 {
1117 cpuPane->interruptExecution();
1118 setDebugState(true);
1119 cpuPane->updateCpu();
1120 cpuPane->setButtonsEnabled(true);
1121 if (!memoryDumpPane->isHidden()) {
1122 memoryDumpPane->highlightMemory(true);
1123 }
1124
1125 listingTracePane->updateListingTrace();
1126 }
1127
1128 // View MainWindow triggers
1129 void MainWindow::on_actionView_Code_Only_triggered()
1130 {
1131 ui->horizontalSplitter->widget(0)->show();
1132 ui->horizontalSplitter->widget(1)->hide();
1133 ui->horizontalSplitter->widget(2)->hide();
1134 ui->actionView_Code_Only->setDisabled(true);
1135 ui->actionView_Code_CPU->setDisabled(false);
1136 ui->actionView_Code_CPU_Memory->setDisabled(false);
1137 }
1138
1139 void MainWindow::on_actionView_Code_CPU_triggered()
1140 {
1141 ui->horizontalSplitter->widget(0)->show();
1142 ui->horizontalSplitter->widget(1)->show();
1143 ui->horizontalSplitter->widget(2)->hide();
1144 ui->actionView_Code_Only->setDisabled(false);
1145 ui->actionView_Code_CPU->setDisabled(true);
1146 ui->actionView_Code_CPU_Memory->setDisabled(false);
1147 QList<int> list;
1148 list.append(3000);
1149 list.append(1);
1150 list.append(1);
1151 ui->horizontalSplitter->setSizes(list);
1152 }
1153
1154 void MainWindow::on_actionView_Code_CPU_Memory_triggered()
1155 {
1156 memoryDumpPane->updateMemory();
1157 memoryDumpPane->highlightMemory(ui->actionBuild_Stop_Debugging->isEnabled());
1158 if (ui->horizontalSplitter->widget(2)->isHidden()) {
1159 memoryDumpPane->scrollToTop();
1160 }
1161 ui->horizontalSplitter->widget(0)->show();
1162 ui->horizontalSplitter->widget(1)->show();
1163 ui->horizontalSplitter->widget(2)->show();
1164 ui->actionView_Code_Only->setDisabled(false);
1165 ui->actionView_Code_CPU->setDisabled(false);
1166 ui->actionView_Code_CPU_Memory->setDisabled(true);
1167 memoryDumpPane->setMaximumWidth(memoryDumpPane->memoryDumpWidth());
1168 QList<int> list;
1169 list.append(3000);
1170 list.append(1);
1171 list.append(3000);
1172 ui->horizontalSplitter->setSizes(list);
1173
1174 }
1175
1176 void MainWindow::on_actionView_Code_Tab_triggered()
1177 {
1178 ui->pepCodeTraceTab->setCurrentIndex(0);
1179 }
1180
1181 void MainWindow::on_actionView_Trace_Tab_triggered()
1182 {
1183 ui->pepCodeTraceTab->setCurrentIndex(1);
1184 }
1185
1186 void MainWindow::on_actionView_Batch_I_O_Tab_triggered()
1187 {
1188 ui->pepInputOutputTab->setCurrentIndex(0);
1189 if (ui->horizontalSplitter->widget(1)->isHidden()) {
1190 on_actionView_Code_CPU_triggered();
1191 }
1192 }
1193
1194 void MainWindow::on_actionView_Terminal_Tab_triggered()
1195 {
1196 ui->pepInputOutputTab->setCurrentIndex(1);
1197 if (ui->horizontalSplitter->widget(1)->isHidden()) {
1198 on_actionView_Code_CPU_triggered();
1199 }
1200 }
1201
1202 // System MainWindow triggers
1203
1204 void MainWindow::on_actionSystem_Clear_Memory_triggered()
1205 {
1206 for (int i = 0; i < Pep::romStartAddress; i++) {
1207 Sim::Mem[i] = 0;
1208 }
1209 cpuPane->clearCpu();
1210 memoryDumpPane->refreshMemory();
1211 }
1212
1213 void MainWindow::on_actionSystem_Redefine_Mnemonics_triggered()
1214 {
1215 redefineMnemonicsDialog->show();
1216 }
1217
1218 void MainWindow::on_actionSystem_Assemble_Install_New_OS_triggered()
1219 {
1220 Pep::burnCount = 0;
1221 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingOS;
1222 Pep::listingRowChecked = &Pep::listingRowCheckedOS;
1223 if (sourceCodePane->assemble()) {
1224 if (Pep::burnCount == 0) {
1225 QString errorString = ";ERROR: .BURN required to install OS.";
1226 sourceCodePane->appendMessageInSourceCodePaneAt(0, errorString);
1227 assemblerListingPane->clearAssemblerListing();
1228 objectCodePane->clearObjectCode();
1229 listingTracePane->clearListingTrace();
1230 ui->statusbar->showMessage("Assembly failed", 4000);
1231 }
1232 else if (Pep::burnCount > 1) {
1233 QString errorString = ";ERROR: Program contain more than one .BURN.";
1234 sourceCodePane->appendMessageInSourceCodePaneAt(0, errorString);
1235 assemblerListingPane->clearAssemblerListing();
1236 objectCodePane->clearObjectCode();
1237 listingTracePane->clearListingTrace();
1238 ui->statusbar->showMessage("Assembly failed", 4000);
1239 }
1240 else {
1241 // Adjust for .BURN
1242 int addressDelta = Pep::dotBurnArgument - Pep::byteCount + 1;
1243 QMutableMapIterator <QString, int> i(Pep::symbolTable);
1244 while (i.hasNext()) {
1245 i.next();
1246 if (Pep::adjustSymbolValueForBurn.value(i.key())) {
1247 i.setValue(i.value() + addressDelta);
1248 }
1249 }
1250 sourceCodePane->adjustCodeList(addressDelta);
1251 Pep::romStartAddress += addressDelta;
1252 objectCodePane->setObjectCode(sourceCodePane->getObjectCode());
1253 assemblerListingPane->setAssemblerListing(sourceCodePane->getAssemblerListingList());
1254 listingTracePane->setListingTrace(sourceCodePane->getAssemblerListingList(), sourceCodePane->getHasCheckBox());
1255 sourceCodePane->installOS();
1256 memoryDumpPane->refreshMemory();
1257 ui->statusbar->showMessage("Assembly succeeded, OS installed", 4000);
1258 }
1259 }
1260 else {
1261 assemblerListingPane->clearAssemblerListing();
1262 objectCodePane->clearObjectCode();
1263 listingTracePane->clearListingTrace();
1264 ui->statusbar->showMessage("Assembly failed", 4000);
1265 }
1266 }
1267
1268 void MainWindow::on_actionSystem_Reinstall_Default_OS_triggered()
1269 {
1270 Pep::memAddrssToAssemblerListing = &Pep::memAddrssToAssemblerListingOS;
1271 Pep::listingRowChecked = &Pep::listingRowCheckedOS;
1272 if (sourceCodePane->installDefaultOs()) {
1273 sourceCodePane->getAssemblerListingList();
1274 listingTracePane->setListingTrace(sourceCodePane->getAssemblerListingList(), sourceCodePane->getHasCheckBox());
1275 ui->statusbar->showMessage("OS installed", 4000);
1276 }
1277 else {
1278 ui->statusbar->showMessage("OS assembly failed", 4000);
1279 }
1280 }
1281
1282 // Help MainWindow triggers
1283 void MainWindow::on_actionHelp_triggered()
1284 {
1285 if (helpDialog->isHidden()) {
1286 helpDialog->setHidden(false);
1287 }
1288 else {
1289 helpDialog->raise();
1290 helpDialog->activateWindow();
1291 }
1292 }
1293
1294 void MainWindow::on_actionHelp_Check_for_updates_triggered()
1295 {
1296 QDesktopServices::openUrl(QUrl("http://code.google.com/p/pep8-1/"));
1297 }
1298
1299 void MainWindow::on_actionHelp_Machine_Language_triggered()
1300 {
1301 helpDialog->show();
1302 helpDialog->machineLanguageClicked();
1303 }
1304
1305 void MainWindow::on_actionHelp_Assembly_Language_triggered()
1306 {
1307 helpDialog->show();
1308 helpDialog->assemblyLanguageClicked();
1309 }
1310
1311 void MainWindow::on_actionHelp_Debugging_Programs_triggered()
1312 {
1313 helpDialog->show();
1314 helpDialog->debuggingProgramsClicked();
1315 }
1316
1317 void MainWindow::on_actionHelp_Writing_Trap_Handlers_triggered()
1318 {
1319 helpDialog->show();
1320 helpDialog->writingTrapHandlersClicked();
1321 }
1322
1323 void MainWindow::on_actionHelp_Pep_8_Reference_triggered()
1324 {
1325 helpDialog->show();
1326 helpDialog->pep8ReferenceClicked();
1327 }
1328
1329 void MainWindow::on_actionHelp_Examples_triggered()
1330 {
1331 helpDialog->show();
1332 helpDialog->examplesClicked();
1333 }
1334
1335 void MainWindow::on_actionHelp_Pep_8_Operating_System_triggered()
1336 {
1337 helpDialog->show();
1338 helpDialog->operatingSystemClicked();
1339 }
1340
1341 void MainWindow::on_actionAbout_Pep8_triggered()
1342 {
1343 aboutPepDialog->exec();
1344 }
1345
1346 void MainWindow::helpCopyToSourceButtonClicked()
1347 {
1348 helpDialog->hide();
1349 if (ui->actionBuild_Stop_Debugging->isEnabled()) { // Leaving this here to safeguard.
1350 ui->statusbar->showMessage("Copy to source failed, in debugging mode", 4000);
1351 return;
1352 }
1353 Enu::EPane destPane;
1354 Enu::EPane inputDest;
1355 QString input = "";
1356 QString code = helpDialog->getCode(destPane, inputDest, input);
1357 outputPane->clearOutput();
1358 inputPane->setText("");
1359 ui->pepCodeTraceTab->setCurrentIndex(0);
1360 if (destPane == Enu::ESource) {
1361 if (maybeSaveSource()) {
1362 setCurrentFile("", Enu::ESource);
1363 sourceCodePane->setSourceCodePaneText(code);
1364 assemblerListingPane->clearAssemblerListing();
1365 objectCodePane->clearObjectCode();
1366 listingTracePane->clearListingTrace();
1367 statusBar()->showMessage("Copied to source", 4000);
1368 ui->actionBuild_Start_Debugging_Source->setEnabled(false);
1369 }
1370 }
1371 else if (maybeSaveObject()) {
1372 setCurrentFile("", Enu::EObject);
1373 objectCodePane->setObjectCodePaneText(code);
1374 sourceCodePane->clearSourceCode();
1375 assemblerListingPane->clearAssemblerListing();
1376 listingTracePane->clearListingTrace();
1377 statusBar()->showMessage("Copied to object", 4000);
1378 ui->actionBuild_Start_Debugging_Source->setEnabled(false);
1379 }
1380 if (inputDest == Enu::ETerminal) {
1381 ui->pepInputOutputTab->setCurrentIndex(1);
1382 }
1383 else {
1384 if (!input.isEmpty()) {
1385 inputPane->setText(input);
1386 ui->pepInputOutputTab->setCurrentIndex(0);
1387 outputPane->clearOutput();
1388 }
1389 }
1390 }
1391
1392 void MainWindow::on_actionAbout_Qt_triggered()
1393 {
1394 QDesktopServices::openUrl(QUrl("http://qt.nokia.com/"));
1395 }
1396
1397 void MainWindow::slotByteConverterDecEdited(const QString &str)
1398 {
1399 if (str.length() > 0) {
1400 bool ok;
1401 int data = str.toInt(&ok, 10);
1402 byteConverterHex->setValue(data);
1403 byteConverterBin->setValue(data);
1404 byteConverterChar->setValue(data);
1405 byteConverterInstr->setValue(data);
1406 }
1407 }
1408
1409 void MainWindow::slotByteConverterHexEdited(const QString &str)
1410 {
1411 if (str.length() >= 2) {
1412 if (str.startsWith("0x")) {
1413 QString hexPart = str;
1414 hexPart.remove(0, 2);
1415 if (hexPart.length() > 0) {
1416 bool ok;
1417 int data = hexPart.toInt(&ok, 16);
1418 byteConverterDec->setValue(data);
1419 byteConverterBin->setValue(data);
1420 byteConverterChar->setValue(data);
1421 byteConverterInstr->setValue(data);
1422 }
1423 else {
1424 // Exactly "0x" remains, so do nothing
1425 }
1426 }
1427 else {
1428 // Prefix "0x" was mangled
1429 byteConverterHex->setValue(-1);
1430 }
1431 }
1432 else {
1433 // Prefix "0x" was shortened
1434 byteConverterHex->setValue(-1);
1435 }
1436 }
1437
1438 void MainWindow::slotByteConverterBinEdited(const QString &str)
1439 {
1440 if (str.length() > 0) {
1441 bool ok;
1442 int data = str.toInt(&ok, 2);
1443 byteConverterDec->setValue(data);
1444 byteConverterHex->setValue(data);
1445 byteConverterChar->setValue(data);
1446 byteConverterInstr->setValue(data);
1447 }
1448 }
1449
1450 void MainWindow::slotByteConverterCharEdited(const QString &str)
1451 {
1452 if (str.length() > 0) {
1453 int data = (int)str[0].toLatin1();
1454 byteConverterDec->setValue(data);
1455 byteConverterHex->setValue(data);
1456 byteConverterBin->setValue(data);
1457 byteConverterInstr->setValue(data);
1458 }
1459 }
1460
1461 // Focus Coloring. Activates and deactivates undo/redo/cut/copy/paste actions contextually
1462 void MainWindow::mainWindowUtilities(QWidget *, QWidget *)
1463 {
1464 sourceCodePane->highlightOnFocus();
1465 objectCodePane->highlightOnFocus();
1466 assemblerListingPane->highlightOnFocus();
1467 listingTracePane->highlightOnFocus();
1468 memoryTracePane->highlightOnFocus();
1469 cpuPane->highlightOnFocus();
1470 inputPane->highlightOnFocus();
1471 outputPane->highlightOnFocus();
1472 terminalPane->highlightOnFocus();
1473 memoryDumpPane->highlightOnFocus();
1474
1475 // we use the stop debugging menu item to determine if we're currently debugging, and set action availability accordingly
1476 if (sourceCodePane->hasFocus()) {
1477 ui->actionEdit_Undo->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled() || !sourceCodePane->isUndoable());
1478 ui->actionEdit_Redo->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled() || !sourceCodePane->isRedoable());
1479 ui->actionEdit_Cut->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled());
1480 ui->actionEdit_Copy->setDisabled(false);
1481 ui->actionEdit_Paste->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled());
1482 }
1483 else if (objectCodePane->hasFocus()) {
1484 ui->actionEdit_Undo->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled() || !objectCodePane->isUndoable());
1485 ui->actionEdit_Redo->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled() || !objectCodePane->isRedoable());
1486 ui->actionEdit_Cut->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled());
1487 ui->actionEdit_Copy->setDisabled(false);
1488 ui->actionEdit_Paste->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled());
1489 }
1490 else if (assemblerListingPane->hasFocus()) {
1491 ui->actionEdit_Undo->setDisabled(true);
1492 ui->actionEdit_Redo->setDisabled(true);
1493 ui->actionEdit_Cut->setDisabled(true);
1494 ui->actionEdit_Copy->setDisabled(false);
1495 ui->actionEdit_Paste->setDisabled(true);
1496 }
1497 else if (listingTracePane->hasFocus()) {
1498 ui->actionEdit_Undo->setDisabled(true);
1499 ui->actionEdit_Redo->setDisabled(true);
1500 ui->actionEdit_Cut->setDisabled(true);
1501 ui->actionEdit_Copy->setDisabled(true);
1502 ui->actionEdit_Paste->setDisabled(true);
1503 }
1504 else if (memoryTracePane->hasFocus()) {
1505 ui->actionEdit_Undo->setDisabled(true);
1506 ui->actionEdit_Redo->setDisabled(true);
1507 ui->actionEdit_Cut->setDisabled(true);
1508 ui->actionEdit_Copy->setDisabled(true);
1509 ui->actionEdit_Paste->setDisabled(true);
1510 }
1511 else if (cpuPane->hasFocus()) {
1512 ui->actionEdit_Undo->setDisabled(true);
1513 ui->actionEdit_Redo->setDisabled(true);
1514 ui->actionEdit_Cut->setDisabled(true);
1515 ui->actionEdit_Copy->setDisabled(true);
1516 ui->actionEdit_Paste->setDisabled(true);
1517 }
1518 else if (inputPane->hasFocus()) {
1519 ui->actionEdit_Undo->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled() || !inputPane->isUndoable());
1520 ui->actionEdit_Redo->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled() || !inputPane->isRedoable());
1521 ui->actionEdit_Cut->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled());
1522 ui->actionEdit_Copy->setDisabled(false);
1523 ui->actionEdit_Paste->setDisabled(ui->actionBuild_Stop_Debugging->isEnabled());
1524 }
1525 else if (outputPane->hasFocus()) {
1526 ui->actionEdit_Undo->setDisabled(true);
1527 ui->actionEdit_Redo->setDisabled(true);
1528 ui->actionEdit_Cut->setDisabled(true);
1529 ui->actionEdit_Copy->setDisabled(false);
1530 ui->actionEdit_Paste->setDisabled(true);
1531 }
1532 else if (terminalPane->hasFocus()) {
1533 ui->actionEdit_Undo->setDisabled(true);
1534 ui->actionEdit_Redo->setDisabled(true);
1535 ui->actionEdit_Cut->setDisabled(true);
1536 ui->actionEdit_Copy->setDisabled(false);
1537 ui->actionEdit_Paste->setDisabled(true);
1538 }
1539 else if (memoryDumpPane->hasFocus()) {
1540 ui->actionEdit_Undo->setDisabled(true);
1541 ui->actionEdit_Redo->setDisabled(true);
1542 ui->actionEdit_Cut->setDisabled(true);
1543 ui->actionEdit_Copy->setDisabled(false);
1544 ui->actionEdit_Paste->setDisabled(true);
1545 }
1546 else if (helpDialog->hasFocus()) {
1547 ui->actionEdit_Undo->setDisabled(true);
1548 ui->actionEdit_Redo->setDisabled(true);
1549 ui->actionEdit_Cut->setDisabled(true);
1550 ui->actionEdit_Copy->setDisabled(false);
1551 ui->actionEdit_Paste->setDisabled(true);
1552 }
1553 }
1554
1555 void MainWindow::setUndoability(bool b)
1556 {
1557 if (sourceCodePane->hasFocus()) {
1558 ui->actionEdit_Undo->setDisabled(!b);
1559 }
1560 else if (objectCodePane->hasFocus()) {
1561 ui->actionEdit_Undo->setDisabled(!b);
1562 }
1563 else if (inputPane->hasFocus()) {
1564 ui->actionEdit_Undo->setDisabled(!b);
1565 }
1566 else if (terminalPane->hasFocus()) {
1567 ui->actionEdit_Undo->setDisabled(!b);
1568 }
1569 }
1570
1571 void MainWindow::setRedoability(bool b)
1572 {
1573 if (sourceCodePane->hasFocus()) {
1574 ui->actionEdit_Redo->setDisabled(!b);
1575 }
1576 else if (objectCodePane->hasFocus()) {
1577 ui->actionEdit_Redo->setDisabled(!b);
1578 }
1579 else if (inputPane->hasFocus()) {
1580 ui->actionEdit_Redo->setDisabled(!b);
1581 }
1582 else if (terminalPane->hasFocus()) {
1583 ui->actionEdit_Redo->setDisabled(!b);
1584 }
1585 }
1586
1587 void MainWindow::doubleClickedCodeLabel(Enu::EPane pane)
1588 {
1589 QList<int> list;
1590 if (pane == Enu::ESource) {
1591 list.append(3000);
1592 list.append(1);
1593 list.append(1);
1594 ui->codeSplitter->setSizes(list);
1595 }
1596 else if (pane == Enu::EObject) {
1597 list.append(1);
1598 list.append(3000);
1599 list.append(1);
1600 ui->codeSplitter->setSizes(list);
1601 }
1602 else if (pane == Enu::EListing) {
1603 list.append(1);
1604 list.append(1);
1605 list.append(3000);
1606 ui->codeSplitter->setSizes(list);
1607 }
1608 else if (pane == Enu::EListingTrace) {
1609 list.append(3000);
1610 list.append(1);
1611 ui->traceSplitter->setSizes(list);
1612 }
1613 else if (pane == Enu::EMemoryTrace) {
1614 list.append(1);
1615 list.append(3000);
1616 ui->traceSplitter->setSizes(list);
1617 }
1618 }
1619
1620 //void MainWindow::resizeDocWidth(int, int)
1621 //{
1622 // listingTracePane->resizeDocWidth();
1623 //}
1624
1625 void MainWindow::updateSimulationView()
1626 {
1627 listingTracePane->updateListingTrace();
1628 if (!memoryTracePane->isHidden()) {
1629 memoryTracePane->updateMemoryTrace();
1630 }
1631 if (!memoryDumpPane->isHidden()) {
1632 memoryDumpPane->updateMemory();
1633 memoryDumpPane->highlightMemory(true);
1634 }
1635 }
1636
1637 void MainWindow::vonNeumannStepped()
1638 {
1639 memoryDumpPane->cacheModifiedBytes();
1640 if (!memoryTracePane->isHidden()) {
1641 memoryTracePane->cacheChanges();
1642 memoryTracePane->cacheStackChanges();
1643 memoryTracePane->cacheHeapChanges();
1644 }
1645 }
1646
1647 void MainWindow::appendOutput(QString str)
1648 {
1649 if (ui->pepInputOutputTab->currentIndex() == 0) { // batch output
1650 outputPane->appendOutput(str);
1651 }
1652 else { // terminal output
1653 terminalPane->appendOutput(str);
1654 }
1655 }
1656
1657 // Recent files
1658 void MainWindow::openRecentFile()
1659 {
1660 QAction *action = qobject_cast<QAction *>(sender());
1661 if (action)
1662 loadFile(action->data().toString());
1663 }
1664
1665 void MainWindow::waitingForInput()
1666 {
1667 terminalPane->waitingForInput();
1668 mainWindowUtilities(0, 0);
1669 }
1670
1671 void MainWindow::inputReceived()
1672 {
1673 if (cpuPane->waitingState() == Enu::EDebugSSWaiting) {
1674 cpuPane->setButtonsEnabled(true);
1675 cpuPane->singleStepWithTerminal();
1676 }
1677 else if (cpuPane->waitingState() == Enu::EDebugResumeWaiting) {
1678 cpuPane->setButtonsEnabled(true);
1679 cpuPane->resumeWithTerminal();
1680 }
1681 else if (cpuPane->waitingState() == Enu::ERunWaiting) {
1682 cpuPane->runWithTerminal();
1683 }
1684 }
1685
1686 void MainWindow::resumeButtonClicked()
1687 {
1688 if (ui->pepInputOutputTab->currentIndex() == 0) { // batch input
1689 cpuPane->resumeWithBatch();
1690 }
1691 else { // terminal input
1692 cpuPane->resumeWithTerminal();
1693 }
1694 }
1695
1696 void MainWindow::singleStepButtonClicked()
1697 {
1698 if (ui->pepInputOutputTab->currentIndex() == 0) { // batch input
1699 cpuPane->singleStepWithBatch();
1700 }
1701 else { // terminal input
1702 cpuPane->singleStepWithTerminal();
1703 }
1704 }
1705