summaryrefslogtreecommitdiff
path: root/gui/gui.cc
blob: 496a443877f7a1c2763152173abcd0476f99c3f9 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Simulator for the RISC-V[ECTOR] mini-ISA
// Copyright (C) 2025 Siddarth Suresh
// Copyright (C) 2025 bdunahu

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#include "gui.h"
#include "./ui_gui.h"
#include "dynamicwaysentry.h"
#include "messages.h"
#include <QPixmap>
#include <QString>

GUI::GUI(QWidget *parent) : QMainWindow(parent), ui(new Ui::GUI)
{
	ui->setupUi(this);

	/* setup the status bar */
	ui->statusBar->setFixedHeight(20);

	this->avatar = new QLabel(this);
	this->status_label = new QLabel("", this);
	QLabel *risc_vector =
		new QLabel("RISC V[ECTOR], CS535 UMASS AMHERST", this);

	avatar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	avatar->setObjectName("avatar_label");
	status_label->setObjectName("msg_label");
	risc_vector->setObjectName("info_label");
	ui->statusBar->setSizeGripEnabled(false);

	this->set_status(get_waiting, "idle.png");
	ui->statusBar->addWidget(avatar);
	ui->statusBar->addWidget(status_label);
	ui->statusBar->addPermanentWidget(risc_vector);

	worker = new Worker();
	worker->moveToThread(&workerThread);

	// find all the labels
	QList<DigitLabel *> labels = this->findChildren<DigitLabel *>();
	for (DigitLabel *label : labels) {
		connect(this, &GUI::hex_toggled, label, &DigitLabel::on_hex_toggle);
	}
	emit this->hex_toggled(this->is_hex);

	// display clock cycles and PC
	connect(worker, &Worker::clock_cycles, this, &GUI::on_worker_refresh_gui);

	connect(worker, &Worker::if_info, this, &GUI::onWorkerFetchInfo);

	connect(worker, &Worker::id_info, this, &GUI::onWorkerDecodeInfo);

	connect(worker, &Worker::ex_info, this, &GUI::onWorkerExecuteInfo);

	connect(worker, &Worker::mm_info, this, &GUI::onWorkerMemoryInfo);

	connect(worker, &Worker::wb_info, this, &GUI::onWorkerWriteBackInfo);

	// Display cache
	connect(worker, &Worker::storage, this, &GUI::onWorkerShowStorage);

	// Display registers
	connect(
		worker, &Worker::register_storage, this, &GUI::onWorkerShowRegisters);

	// Configure pipeline
	connect(
		this, &GUI::sendConfigure, worker, &Worker::configure,
		Qt::QueuedConnection);

	// Advance controller by some steps
	connect(
		this, &GUI::sendRunSteps, worker, &Worker::runSteps,
		Qt::QueuedConnection);

	// Update the step button with step amount
	connect(ui->step_slider, &QSlider::valueChanged, this, [=](int index) {
		int value = step_values[index];
		ui->step_btn->setText(QString("Step %1").arg(value));
	});

	// 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 GUI::on_worker_refresh_gui(int cycles, int pc)
{
	ui->p_counter->set_value(pc);
	ui->cycle_counter->set_value(cycles);
}

void GUI::onWorkerFetchInfo(const std::vector<int> info)
{
	if (!info.empty()) {
		ui->fetch_squashed->setText(QString::number(info[0]));
		ui->fetch_bits->set_value(info[1]);
	} else {
		ui->fetch_squashed->clear();
		ui->fetch_bits->clear();
	}
}

void GUI::onWorkerDecodeInfo(const std::vector<int> info)
{
	if (!info.empty()) {
		ui->decode_squashed->setText(QString::number(info[0]));
		ui->decode_bits->set_value(info[1]);
	} else {
		ui->decode_squashed->clear();
		ui->decode_bits->clear();
	}
}

void GUI::onWorkerExecuteInfo(const std::vector<int> info)
{
	if (!info.empty()) {
		ui->execute_mnemonic->setText(mnemonicToString((Mnemonic)info[0]));
		ui->execute_squashed->setText(QString::number(info[1]));
		ui->execute_s1->set_value(info[2]);
		ui->execute_s2->set_value(info[3]);
		ui->execute_s3->set_value(info[4]);
	} else {
		ui->execute_mnemonic->clear();
		ui->execute_squashed->clear();
		ui->execute_s1->clear();
		ui->execute_s2->clear();
		ui->execute_s3->clear();
	}
}

void GUI::onWorkerMemoryInfo(const std::vector<int> info)
{
	if (!info.empty()) {
		std::cout << "this " << info[3] << std::endl;
		ui->memory_mnemonic->setText(mnemonicToString((Mnemonic)info[0]));
		ui->memory_squashed->setText(QString::number(info[1]));
		ui->memory_s1->set_value(info[2]);
		ui->memory_s2->set_value(info[3]);
		ui->memory_s3->set_value(info[4]);
	} else {
		ui->memory_mnemonic->clear();
		ui->memory_squashed->clear();
		ui->memory_s1->clear();
		ui->memory_s2->clear();
		ui->memory_s3->clear();
	}
}

void GUI::onWorkerWriteBackInfo(const std::vector<int> info)
{
	if (!info.empty()) {
		ui->write_mnemonic->setText(mnemonicToString((Mnemonic)info[0]));
		ui->write_squashed->setText(QString::number(info[1]));
		ui->write_s1->set_value(info[2]);
		ui->write_s2->set_value(info[3]);
		ui->write_s3->set_value(info[4]);
	} else {
		ui->write_mnemonic->clear();
		ui->write_squashed->clear();
		ui->write_s1->clear();
		ui->write_s2->clear();
		ui->write_s3->clear();
	}
}

void GUI::onWorkerShowStorage(
	const std::vector<std::array<signed int, LINE_SIZE>> data, int i)
{
	std::cout << this->tab_text_boxes.size() << std::endl;
	displayTableHTML(this->tab_text_boxes.at(i), data);
}

void GUI::onWorkerShowRegisters(const std::array<int, GPR_NUM> &data)
{
	displayArrayHTML(this->tab_text_boxes.at(0), data);
}

void GUI::onWorkerFinished() { qDebug() << "Worker has finished processing."; }

void GUI::on_upload_intructions_btn_clicked()
{
	qDebug() << "Upload intructions button clicked.";

	// why ui->register_table, or now ui->storage
	QString filePath = QFileDialog::getOpenFileName(
		ui->storage, "Open Binary File", QDir::homePath(),
		"Binary Files (*.bin *.rv);;All Files (*.*)");
	QFile file(filePath);
	if (filePath.isEmpty() || !file.open(QIODevice::ReadOnly)) {
		this->set_status(get_no_instructions, "angry");
		return;
	}

	this->p.clear();
	while (!file.atEnd()) {
		char bytes[4];
		if (file.read(bytes, 4) == 4) {
			uint32_t word = (static_cast<uint8_t>(bytes[0]) << 24) |
							(static_cast<uint8_t>(bytes[1]) << 16) |
							(static_cast<uint8_t>(bytes[2]) << 8) |
							(static_cast<uint8_t>(bytes[3]));

			this->p.push_back(static_cast<signed int>(word));
		}
	}

	if (this->p.empty())
		this->set_status(get_no_instructions, "angry");
	else
		this->set_status(get_load_file, "happy");

	file.close();
}

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_enable_pipeline_checkbox_checkStateChanged(
	const Qt::CheckState &arg1)
{
	this->is_pipelined = (arg1 == Qt::CheckState::Checked) ? true : false;
}

void GUI::on_base_toggle_checkbox_checkStateChanged(const Qt::CheckState &state)
{
	this->is_hex = (state == Qt::CheckState::Checked) ? false : true;
	emit this->hex_toggled(this->is_hex);
}

void GUI::on_step_btn_clicked()
{
	qDebug() << "Run step button clicked.";
	// try to configure first
	if (!this->ready)
		this->on_config_clicked();
	// try again
	if (!this->ready)
		return;

	this->set_status(get_running, "busy");
	int steps = step_values[ui->step_slider->value()];
	emit sendRunSteps(steps);
	this->set_status(get_waiting, "idle");
}

void GUI::on_save_program_state_btn_clicked()
{
	// TODO: save program state
	qDebug() << "save program state button is clicked.";
}

void GUI::on_config_clicked()
{
	std::vector<unsigned int> ways;
	QStringList entries;
	signed int i;
	DynamicWaysEntry *dwe = ui->cache_way_selector;

	for (const QString &s : dwe->get_entries()) {

		if (s.isEmpty())
			continue;

		i = dwe->parse_valid_way(s);
		if (i >= 0) {
			ways.push_back((unsigned int)i);
		} else {
			this->set_status(get_bad_cache, "angry");
			return;
		}
	}

	if (this->p.empty()) {
		this->set_status(get_no_instructions, "angry");
		return;
	}

	this->ready = true;

	// say something snarky
	if (!is_pipelined)
		this->set_status(get_no_pipeline, "angry");
	else if (ways.size() == 0)
		this->set_status(get_no_cache, "angry");
	else
		this->set_status(get_initialize, "happy");

	emit sendConfigure(ways, this->p, is_pipelined);
	make_tabs(2 + ways.size());
}

void GUI::make_tabs(int num)
{
	int i;
	QStringList names;
	QTextEdit *e;
	QString n;

	names = {"Registers", "DRAM"};

	ui->storage->clear();
	this->tab_text_boxes.clear();

	for (i = 0; i < num; ++i) {
		e = new QTextEdit();
		e->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

		// make the name
		if (i < names.size())
			n = names[i];
		else
			n = QString("Level %1").arg(i - 1);

		ui->storage->addTab(e, n);
		this->tab_text_boxes.push_back(e);
	}
}

void GUI::set_status(
	const std::function<std::string()> &func, const QString &img)
{
	this->status_label->setText(
		"-> \"" + QString::fromStdString(func()) + "\"");

	QString img_path = ":resources/" + img;
	QPixmap pixmap(img_path);

	if (!pixmap) {
		return;
	}

	this->avatar->setPixmap(pixmap);
	this->avatar->show();
}