-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
277 lines (250 loc) · 9.31 KB
/
widget.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <algorithm>
#include <QApplication>
#include "widget.h"
// Related to "Reload MC" button and other places.
// Load the Machine Code from the file to the displaying area.
void Widget::loadMachineCode() {
machineCodeArea->clear();
for (std::bitset<SIZE_32_BIT> line: baby->memory) {
QString str = QString::fromStdString(line.to_string());
machineCodeArea->append(str);
}
mainWindow.update();
}
// Related to "Run" button.
// Prepare to execute the Machine Code.
void Widget::run() {
// Avoid collusion
if (running) {
return;
}
running = true;
// Recover the Manchester Baby if it is currently
baby->setHalt(false);
try {
// Execute MC
execute();
} catch (const std::runtime_error &e) {
std::cerr << "An error occurred: " << e.what() << std::endl;
}
}
// A recursive function. Execute instructions one after another, and wait for a while between each.
void Widget::execute() {
if (!baby->isHalted()) {
// Fetch, Decode and Execute
baby->fetch();
baby->decodeAndExecute();
baby->increment_ci();
// Update GUI information panel
round->setText(QString::fromStdString(std::to_string(baby->curRound)));
prev_ci->setText(QString::fromStdString(std::to_string(baby->prev_ci)));
pi->setText(QString::fromStdString(baby->pi.to_string()));
ci->setText(QString::fromStdString(std::to_string(baby->ci)));
opcode->setText(QString::fromStdString(std::to_string(baby->curOpCode)));
operand->setText(QString::fromStdString(std::to_string(baby->curOperand)));
QString inImAddressing = baby->curImAddressing ? "Immediate Addressing" : "Default";
address_mode->setText(inImAddressing);
accumulator->setText(QString::fromStdString(baby->accumulator.to_string()));
accumulatorDec->setText(QString::fromStdString(
std::to_string(ManchesterBaby::binToDec(ManchesterBaby::convertInstruction(baby->accumulator)))));
QString expString; // For Explanation
switch (baby->curOpCode) {
case 0:
expString = "JMP: CI = ";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 1:
expString = "JRP: CI += ";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 2:
expString = "LDN: A = -";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 3:
expString = "STO: S = A";
break;
case 4:
expString = "SUB: A -= ";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 6:
expString = "CMP: CI += 1 if A < 0";
break;
case 7:
expString = "STOP";
break;
case 8:
expString = "LDP: A = ";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 9:
expString = "ADD: A += ";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 10:
expString = "DIV: A /= ";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 11:
expString = "MOD: A %= ";
if (baby->curImAddressing) {
expString.append("OPERAND");
} else {
expString.append("S");
}
break;
case 12:
expString = "LAN: A = A & S";
break;
case 13:
expString = "LOR: A = A | S";
break;
case 14:
expString = "LNT: A = ~A";
break;
case 15:
expString = "SHL: A <<= 1";
break;
case 16:
expString = "SHR: A >>= 1";
break;
default:
expString = "Invalid OPCODE!";
break;
}
explanation->setText(expString);
loadMachineCode();
// Wait for a while
QTimer::singleShot(1000, this, SLOT(execute()));
} else {
baby->reset();
}
}
// Related to "Stop" button.
// Terminates the MC execution progress, but not the program, unlike the console mode.
void Widget::stop() {
running = false;
baby->setHalt(true);
baby->reset();
}
// Constructor
Widget::Widget(ManchesterBaby *baby, QWidget *parent)
: QWidget(parent) {
this->baby = baby;
// Initializes and sets attributes of all the components.
splitter = new QSplitter(&mainWindow);
machineCodeArea = new QTextEdit();
rightLayout = new QVBoxLayout();
rightWidget = new QWidget();
buttonLayout = new QHBoxLayout();
scrollArea = new QScrollArea();
infoLayout = new QFormLayout();
infoWidget = new QWidget();
loadButton = new QPushButton("Reload MC");
runButton = new QPushButton("Run");
stopButton = new QPushButton("Stop");
// Window Frame
mainWindow.setCentralWidget(splitter);
mainWindow.resize(1000, 750);
// Machine Code displaying area
machineCodeArea->setFixedWidth(QFontMetrics(machineCodeArea->font()).averageCharWidth() * 32);
machineCodeArea->setReadOnly(true);
splitter->addWidget(machineCodeArea);
// Widget on the right
rightWidget->setLayout(rightLayout);
splitter->addWidget(rightWidget);
rightLayout->addLayout(buttonLayout);
buttonLayout->addWidget(loadButton);
buttonLayout->addWidget(runButton);
buttonLayout->addWidget(stopButton);
scrollArea->setWidgetResizable(true);
rightLayout->addWidget(scrollArea);
infoWidget->setLayout(infoLayout);
scrollArea->setWidget(infoWidget);
// Action Listener for the buttons
connect(loadButton, &QPushButton::pressed, this, &Widget::loadMachineCode);
connect(runButton, &QPushButton::pressed, this, &Widget::run);
connect(stopButton, &QPushButton::pressed, this, &Widget::stop);
// Information Panel display setting
QStringList infoLabels = {"Round", "CI", "PI", "New CI", "OPCODE", "OPERAND", "Address Mode", "Accumulator",
"Accumulator (DEC)",
"Explanation"};
// Round
roundTitle = new QLabel(infoLabels[0] + ":");
round = new QLabel(QString::fromStdString(std::to_string(baby->curRound)));
infoLayout->addRow(roundTitle, round);
// CI
prevCiTitle = new QLabel(infoLabels[1] + ":");
prev_ci = new QLabel(QString::fromStdString(std::to_string(baby->prev_ci)));
infoLayout->addRow(prevCiTitle, prev_ci);
// PI
piTitle = new QLabel(infoLabels[2] + ":");
pi = new QLabel(QString::fromStdString(baby->pi.to_string()));
infoLayout->addRow(piTitle, pi);
// New CI
ciTitle = new QLabel(infoLabels[3] + ":");
ci = new QLabel(QString::fromStdString(std::to_string(baby->ci)));
infoLayout->addRow(ciTitle, ci);
// OPCODE
opcodeTitle = new QLabel(infoLabels[4] + ":");
opcode = new QLabel(QString::fromStdString(std::to_string(baby->curOpCode)));
infoLayout->addRow(opcodeTitle, opcode);
// OPERAND
operandTitle = new QLabel(infoLabels[5] + ":");
operand = new QLabel(QString::fromStdString(std::to_string(baby->curOperand)));
infoLayout->addRow(operandTitle, operand);
// Address Mode
addressModeTitle = new QLabel(infoLabels[6] + ":");
QString inImAddressing = baby->curImAddressing ? "Immediate Addressing" : "Default";
address_mode = new QLabel(inImAddressing);
infoLayout->addRow(addressModeTitle, address_mode);
// Accumulator
accumulatorTitle = new QLabel(infoLabels[7] + ":");
accumulator = new QLabel(QString::fromStdString(baby->accumulator.to_string()));
infoLayout->addRow(accumulatorTitle, accumulator);
// Accumulator (DEC)
accumulatorDecTitle = new QLabel(infoLabels[8] + ":");
accumulatorDec = new QLabel(QString::fromStdString(
std::to_string(ManchesterBaby::binToDec(ManchesterBaby::convertInstruction(baby->accumulator)))));
infoLayout->addRow(accumulatorDecTitle, accumulatorDec);
// Explanation
explanationTitle = new QLabel(infoLabels[9] + ":");
QString expString = "--";
explanation = new QLabel(expString);
infoLayout->addRow(explanationTitle, explanation);
// Load the Machine Code
loadMachineCode();
}
// Destructor
Widget::~Widget()
= default;