forked from rmoldo/xasm-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
194 lines (153 loc) · 6.69 KB
/
Copy pathmainwindow.cpp
File metadata and controls
194 lines (153 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <editor/codeeditor.h>
#include <QToolBar>
#include <QProcess>
#include <QMessageBox>
#include <cpu/cpu.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
memoryViewerDialog = new MemoryViewerDialog(this);
createActions();
cpuWindow = new CPUwindow(this);
cpu = new Cpu(this);
cpuWindow->setCpu(cpu);
memoryViewerDialog->setCpu(cpu);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions() {
// Menus
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
QMenu *executeMenu = menuBar()->addMenu(tr("&Execute"));
// Toolbars
QToolBar *fileToolBar = addToolBar(tr("File"));
QToolBar *viewToolBar = addToolBar(tr("View"));
QToolBar *executeToolBar = addToolBar(tr("Execute"));
// Open file action
QAction *openAction = new QAction(tr("&Open"), this);
openAction->setIcon(QPixmap(":/rec/resources/icons/folder.svg"));
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Open an XASM file"));
connect(openAction, &QAction::triggered, this->ui->plainTextEdit, &CodeEditor::open);
fileMenu->addAction(openAction);
fileToolBar->addAction(openAction);
// Quit action
QAction *quitAction = new QAction(tr("&Quit"), this);
quitAction->setIcon(QPixmap(":/rec/resources/icons/quit.svg"));
quitAction->setShortcut(QKeySequence::Quit);
quitAction->setStatusTip(tr("Quit XASM Simulator"));
connect(quitAction, &QAction::triggered, qApp, &QApplication::closeAllWindows, Qt::QueuedConnection);
fileMenu->addAction(quitAction);
// View processor architecture action
QAction *viewArchitectureAction = new QAction(tr("Ar&chitecture"), this);
viewArchitectureAction->setIcon(QPixmap(":/rec/resources/icons/cpu.svg"));
viewArchitectureAction->setShortcut(QKeySequence(tr("Shift+A")));
viewArchitectureAction->setStatusTip(tr("View processor architecture"));
connect(viewArchitectureAction, &QAction::triggered, this, [this]() {
cpuWindow->show();
});
viewMenu->addAction(viewArchitectureAction);
viewToolBar->addAction(viewArchitectureAction);
// View memory action
QAction *viewMemoryAction = new QAction(tr("&Memory"), this);
viewMemoryAction->setIcon(QPixmap(":/rec/resources/icons/ram.svg"));
viewMemoryAction->setShortcut(QKeySequence(tr("Shift+M")));
viewMemoryAction->setStatusTip(tr("View memory content"));
connect(viewMemoryAction, &QAction::triggered, this->memoryViewerDialog,
[this]() {
memoryViewerDialog->setMemoryViewerData({reinterpret_cast<char*>(cpu->getMemory().data()), (int)cpu->getMemory().size()});
memoryViewerDialog->show();
});
viewMemoryAction->setEnabled(false);
viewMenu->addAction(viewMemoryAction);
viewToolBar->addAction(viewMemoryAction);
// Assemble action
QAction *assembleAction = new QAction(tr("&Assemble"), this);
assembleAction->setIcon(QPixmap(":/rec/resources/icons/assembler.svg"));
assembleAction->setShortcut(QKeySequence(tr("Ctrl+A")));
assembleAction->setStatusTip(tr("Assemble the code"));
connect(this->ui->plainTextEdit, &CodeEditor::loadFinished, this, [=]() {assembleAction->setEnabled(true);});
connect(assembleAction, &QAction::triggered, this, [=]() {
QProcess process;
QString command("../xasm-simulator/resources/assembler/xasm ");
command += this->ui->plainTextEdit->getFileName();
process.start(command);
//wait forever until finished
process.waitForFinished(-1);
QString output = process.readAllStandardOutput();
QString errors = process.readAllStandardError();
QMessageBox messageBox;
if (output.endsWith("generated successfully\n")) {
//reinitialize cpu if reassembled
delete cpu;
cpu = new Cpu(this);
cpuWindow->setCpu(cpu);
memoryViewerDialog->setCpu(cpu);
messageBox.information(this, "Success", "Assembled successfully!\nNow you can start the simulation.");
stepAction->setEnabled(true);
runAction->setEnabled(true);
interruptAction->setEnabled(true);
viewMemoryAction->setEnabled(true);
QFile machineCodeFile {"output.out"};
machineCodeFile.open(QIODevice::ReadOnly);
QByteArray machineCode = machineCodeFile.readAll();
cpu->setMachineCodeInMemory(reinterpret_cast<u8 *>(machineCode.data()), machineCode.size());
}
else {
messageBox.critical(this, "Assembler Error", errors);
}
this->cpu->resetActivatedSignals();
});
assembleAction->setEnabled(false);
executeMenu->addAction(assembleAction);
executeToolBar->addAction(assembleAction);
// Step action
stepAction = new QAction(tr("S&tep"), this);
stepAction->setIcon(QPixmap(":/rec/resources/icons/step.svg"));
stepAction->setShortcut(QKeySequence(tr("F7")));
stepAction->setStatusTip(tr("Execute one impulse"));
connect(stepAction, &QAction::triggered, this, [=]() {
if(!cpu->advance()) {
QMessageBox messageBox;
messageBox.information(this, "Processor halted", cpu->getReason());
stepAction->setEnabled(false);
runAction->setEnabled(false);
}
});
stepAction->setEnabled(false);
executeMenu->addAction(stepAction);
executeToolBar->addAction(stepAction);
// Run action
runAction = new QAction(tr("&Run"), this);
runAction->setIcon(QPixmap(":/rec/resources/icons/run.svg"));
runAction->setShortcut(QKeySequence(tr("F8")));
runAction->setStatusTip(tr("Run the simulation"));
connect(runAction, &QAction::triggered, this, [=]() {
while(cpu->advance());
QMessageBox messageBox;
messageBox.information(this, "Processor halted", cpu->getReason());
stepAction->setEnabled(false);
runAction->setEnabled(false);
});
runAction->setEnabled(false);
executeMenu->addAction(runAction);
executeToolBar->addAction(runAction);
// Interrupt action
interruptAction = new QAction(tr("Trigger &IRQ"), this);
interruptAction->setIcon(QPixmap(":/rec/resources/icons/interrupt.svg"));
interruptAction->setShortcut(QKeySequence(tr("Ctrl+I")));
interruptAction->setStatusTip(tr("Trigger interrupt request"));
connect(interruptAction, &QAction::triggered, this, [=](){
cpu->setInterrupt();
});
interruptAction->setEnabled(false);
executeMenu->addAction(interruptAction);
executeToolBar->addAction(interruptAction);
}