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
|
#include "gui.h"
#include "./ui_gui.h"
GUI::GUI(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::GUI)
{
ui->setupUi(this);
ui->enabl_cache_checkbox->setChecked(true);
ui->enable_pipeline_checkbox->setChecked(true);
worker = new Worker();
worker->moveToThread(&workerThread);
// Connect worker thread lifecycle
connect(&workerThread, &QThread::started, worker, &Worker::doWork);
// Display clock cycles and PC
connect(worker, &Worker::clock_cycles, this, &GUI::onWorkerClockCycles);
// Display dram
connect(worker, &Worker::dram_storage, this, &GUI::onWorkerShowDram);
// Display cache
connect(worker, &Worker::cache_storage, this, &GUI::onWorkerShowCache);
// Display registers
connect(worker, &Worker::register_storage, this, &GUI::onWorkerShowRegisters);
// Refresh DRAM from worker thread
connect(this, &GUI::sendRefreshDram, worker, &Worker::refreshDram, Qt::QueuedConnection);
// Refresh Cache from worker thread
connect(this, &GUI::sendRefreshCache, worker, &Worker::refreshCache, Qt::QueuedConnection);
// Refresh Registers from worker thread
connect(this, &GUI::sendRefreshRegisters, worker, &Worker::refreshRegisters, Qt::QueuedConnection);
// Advance controller by #steps
connect(this, &GUI::sendRunSteps, worker, &Worker::runSteps, Qt::QueuedConnection);
// Advance controller by 1 step
connect(this, &GUI::sendRunStep, worker, &Worker::runStep, Qt::QueuedConnection);
// Proper cleanup when worker finishes
connect(worker, &Worker::finished, this, &GUI::onWorkerFinished);
connect(worker, &Worker::finished, &workerThread, &QThread::quit);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
workerThread.start(); // Start the worker thread
}
GUI::~GUI()
{
workerThread.quit();
workerThread.wait(); // Ensure proper cleanup
delete ui;
}
void displayArrayHTML(QTextEdit *textEdit, const std::array<int, GPR_NUM> &data) {
textEdit->setReadOnly(false);
QString tableText = "<table border='1' cellspacing='0' cellpadding='8' style='border-collapse: collapse; width: 100%; border: 2px solid black;'>";
tableText += "<tr>";
int index = 0;
for (int value : data) {
tableText += QString("<td align='center' style='border: 2px solid black; min-width: 60px; padding: 10px;'>"
"%1 <sup style='font-size: 10px; font-weight: bold; color: black;'>%2</sup>"
"</td>")
.arg(QString::asprintf("%04X", value))
.arg(index);
index++;
}
tableText += "</tr>";
tableText += "</table>";
textEdit->setHtml(tableText);
textEdit->setReadOnly(true);
}
void displayTableHTML(QTextEdit *textEdit, const std::vector<std::array<signed int, LINE_SIZE>> &data) {
textEdit->setReadOnly(false);
QString tableText = "<table border='1' cellspacing='0' cellpadding='8' style='border-collapse: collapse; width: 100%; border: 2px solid black;'>";
int index = 0;
for (const auto &row : data) {
tableText += "<tr>";
for (signed int value : row) {
tableText += QString("<td align='center' style='border: 2px solid black; min-width: 60px; padding: 10px;'>"
"%1 <sup style='font-size: 10px; font-weight: bold; color: black;'>%2</sup>"
"</td>")
.arg(QString::asprintf("%04X", value))
.arg(index);
index++;
}
tableText += "</tr>";
}
tableText += "</table>";
textEdit->setHtml(tableText);
textEdit->setReadOnly(true);
}
void browseAndUploadFile(QTextEdit *textEdit) {
QString filePath = QFileDialog::getOpenFileName(nullptr, "Open File", QDir::homePath(), "Text Files (*.txt);;All Files (*.*)");
if (filePath.isEmpty()) {
return;
}
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
textEdit->setPlainText("Error: Unable to open file!");
return;
}
QTextStream in(&file);
QString content;
int lineNumber = 0;
while (!in.atEnd()) {
QString line = in.readLine();
content += QString("<div id='line_%1' style='display: flex; justify-content: space-between; align-items: center;'>"
"<span style='font-size: 10px; font-weight: bold; color: gray;'>%1.</span>"
"<span>%2</span>"
"</div><hr>")
.arg(lineNumber)
.arg(line);
lineNumber++;
}
file.close();
textEdit->setReadOnly(false);
textEdit->setHtml(content);
textEdit->setReadOnly(true);
}
void GUI::onWorkerClockCycles(int cycles, int pc) {
ui->cycles_label->setText("Clock Cycles: " + QString::number(cycles) + "\t\t" + "PC: " + QString::number(pc));
}
void GUI::onWorkerShowDram(const std::vector<std::array<signed int, LINE_SIZE>> data) {
displayTableHTML(ui->dram_table, data);
}
void GUI::onWorkerShowCache(const std::vector<std::array<signed int, LINE_SIZE>> data) {
displayTableHTML(ui->cache_table, data);
}
void GUI::onWorkerShowRegisters(const std::array<int, GPR_NUM> &data) {
displayArrayHTML(ui->register_table, data);
}
void GUI::onWorkerFinished() {
qDebug() << "Worker has finished processing.";
}
void GUI::on_upload_intructions_btn_clicked()
{
qDebug() << "Upload intructions button clicked.";
browseAndUploadFile(ui->instruction_table);
}
void GUI::on_upload_program_state_btn_clicked()
{
//TODO:Upload and set program state ( have to decide how to use this)
qDebug() << "upload program state button is clicked.";
}
void GUI::on_refresh_dram_btn_clicked()
{
qDebug() << "Refresh DRAM button clicked.";
emit sendRefreshDram();
}
void GUI::on_refresh_cache_btn_clicked()
{
qDebug() << "Refresh cache button clicked.";
emit sendRefreshCache();
}
void GUI::on_refresh_registers_btn_clicked()
{
qDebug() << "Refresh registers button clicked.";
emit sendRefreshRegisters();
}
void GUI::on_enable_pipeline_checkbox_checkStateChanged(const Qt::CheckState &arg1)
{
//TODO: handle pipeline enabling
if(arg1 == Qt::CheckState::Checked) {
qDebug() << "enable pipeline checkbox checked.";
} else {
qDebug() << "enable pipeline checkbox unchecked.";
}
}
void GUI::on_enabl_cache_checkbox_checkStateChanged(const Qt::CheckState &arg1)
{
//TODO: handle cache enabling
if(arg1 == Qt::CheckState::Checked) {
qDebug() << "enable cache checkbox checked.";
} else {
qDebug() << "enable cache checkbox unchecked.";
}
}
void GUI::on_run_steps_btn_clicked()
{
qDebug() << "Run steps button clicked.";
emit sendRunSteps(ui->number_steps_inp->text().toInt());
}
void GUI::on_step_btn_clicked()
{
qDebug() << "Run step button clicked.";
emit sendRunStep();
}
void GUI::on_save_program_state_btn_clicked()
{
//TODO: save program state
qDebug() << "save program state button is clicked.";
}
|