-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
280 lines (230 loc) · 8.78 KB
/
mainwindow.cpp
File metadata and controls
280 lines (230 loc) · 8.78 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Vole.h"
using namespace std;
string SCR_Next = "";
string LASTHEX_Next = "";
Machine machine;
Memory* memory1 = machine.getmem();
CPU* cpu = machine.getcpu();
Register* register1= cpu->getreg();
CU* cu = cpu->getcu();
ALU* alu = cpu->getAlu();
void MainWindow::print_mem(Ui::MainWindow* ui){
// Iterate over the rows to populate the QTableWidget
for (int row = 0; row < 255; ++row) {
string address = Memory::intToHex(row);
string cellData = memory1->getCell(address);
string binValue = memory1->hexatoBinary(cellData);
int intVlaue = cpu->hexStringToInt(cellData);
double floatValue = alu->floatextract(cellData);
qDebug() << "after creating loop"<<row;
ui->Memory_T->setItem(row, 0,new QTableWidgetItem(QString::fromStdString(binValue))); // Binary data
ui->Memory_T->setItem(row, 1, new QTableWidgetItem(QString::fromStdString(cellData))); // Hex data
ui->Memory_T->setItem(row, 2, new QTableWidgetItem(QString::number(intVlaue))); // int data
ui->Memory_T->setItem(row, 3, new QTableWidgetItem(QString::number(floatValue,'f', 6))); // float data
}
}
void MainWindow::print_reg(Ui::MainWindow* ui){
for (int row = 0; row < 16; ++row) {
string address = register1->getHexadecimalRange(row);
string cellData = register1->getCell(address);
string binValue = register1->hexatoBinary(cellData);
int intVlaue = cpu->hexStringToInt(cellData);
double floatValue = alu->floatextract(cellData);
//string bin = ALU::;
qDebug() << "after creating loop"<<row;
ui->Register_T->setItem(row, 0,new QTableWidgetItem(QString::fromStdString(binValue))); // Binary data
ui->Register_T->setItem(row, 1, new QTableWidgetItem(QString::fromStdString(cellData))); // Hex data
ui->Register_T->setItem(row, 2, new QTableWidgetItem(QString::number(intVlaue))); // int data
ui->Register_T->setItem(row, 3, new QTableWidgetItem(QString::number(floatValue,'f', 6))); // float data
}
}
void MainWindow::print_scr(Ui::MainWindow* ui,string hex){
if(hex == "00" || hex == LASTHEX_Next){
return;
}
LASTHEX_Next = hex;
string ascii = "";
for (size_t i = 0; i < hex.length(); i += 2) {
string part = hex.substr(i, 2);
char ch = stoul(part, nullptr, 16);
ascii += ch;
}
SCR_Next += "ascii = "+ascii + ", hex = " + hex + "\n";
ui->textEdit->setText(QString::fromStdString(SCR_Next));
qDebug() << "Memory content at address '00': " << QString::fromStdString(hex); // Debugging line
}
void MainWindow::showTemporaryText() {
// Set the text on the label
ui->done->show();
// Create a QTimer to clear the text after 3 seconds (3000 ms)
QTimer::singleShot(3000, this, [=]() {
ui->done->hide(); // Hide the label text
});
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug() << "Before creating memory1";
// Ensure tableWidget is already initialized and set up with rows and columns
print_mem(ui);
print_reg(ui);
print_scr(ui,memory1->getCell("00"));
ui->done->hide();
machine.setPC("0A");
ui->start_line->setText("0A");
ui->Memory_T->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->Memory_T->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->Memory_T->setColumnWidth(0, 125);
ui->Memory_T->setRowHeight(0, 24);
ui->Register_T->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->Register_T->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->Register_T->setColumnWidth(0, 125);
ui->Register_T->setRowHeight(0, 24);
qDebug() << "after creating memory1";
//cpu->setPC("0A");
string pc_str = cpu->getPC();
string pc_str2 = " "+pc_str;
ui->pc_line->setText(QString::fromStdString(pc_str2));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_store_btn_clicked()
{
QString instruction = ui->inst_kine->text();
string inst_str = instruction.toStdString();
if(inst_str.length()<4){
QMessageBox::critical(this,"Error","Please enter a valid instruction");
}
else if(!alu->isValid(inst_str)){
QMessageBox::critical(this,"Error","Please enter a valid instruction");
}else{
machine.loadProgramNormalWay(inst_str);
print_mem(ui);
print_reg(ui);
print_scr(ui,memory1->getCell("00"));
showTemporaryText();
string pc_str = cpu->getPC();
string pc_str2 = " "+pc_str;
ui->pc_line->setText(QString::fromStdString(pc_str2));
}
ui->inst_kine->clear();
}
void MainWindow::on_Add_btn_clicked()
{
QString file_name = QFileDialog::getOpenFileName(this,tr("Select text file"),QCoreApplication::applicationDirPath(),tr("Text file(*.txt)"));
qDebug() << file_name;
if(file_name.isEmpty() || file_name.isNull()){
QMessageBox::critical(this,"Error","Please enter a valid file");
}else{
on_C_M_btn_clicked();
machine.clearInstructions();
string file_str = file_name.toStdString();
machine.loadProgramFile(file_str);
machine.sendInstructionsToMemory();
//machine.sendInstructionsToCPU();
print_mem(ui);
print_reg(ui);
print_scr(ui,memory1->getCell("00"));
string pc_str = cpu->getPC();
string address = (ui->start_line->text()).toStdString();
if(address != pc_str){
string pc_str2 = " "+address;
ui->pc_line->setText(QString::fromStdString(pc_str2));
machine.setPC(address);
}else{
string pc_str2 = " "+pc_str;
ui->pc_line->setText(QString::fromStdString(pc_str2));
}
}
}
void MainWindow::on_C_M_btn_clicked()
{
memory1->clearMemory();
machine.clearInstructions();
qDebug() << "done";
print_mem(ui);
cpu->setend();
cpu->setPC("0A");
memory1->setStart("0A");
string pc_str = cpu->getPC();
string pc_str2 = " "+pc_str;
ui->pc_line->setText(QString::fromStdString(pc_str2));
}
void MainWindow::on_C_R_btn_clicked()
{
register1->clearRegister();
qDebug() << "done";
print_reg(ui);
}
void MainWindow::on_run_halt_btn_clicked()
{
on_C_S_btn_clicked();
machine.resetExecutionState();
machine.runTillHalt(ui);
print_mem(ui);
print_reg(ui);
cpu->display_screen(ui);
//print_scr(ui,memory1->getCell("00"));
QMessageBox::information(this,"Execution","The Execution has reached C000");
cpu->setend();
cpu->setPC("0A");
memory1->setStart("0A");
string pc_str = cpu->getPC();
string pc_str2 = " "+pc_str;
ui->pc_line->setText(QString::fromStdString(pc_str2));
}
void MainWindow::on_run_next_btn_clicked()
{
machine.runNextStep();
print_mem(ui);
print_reg(ui);
print_scr(ui,memory1->getCell("00"));
qDebug () << "screen -----------------------------------------\n";
if(cpu->getend()){
QMessageBox::information(this,"Execution","The Execution has reached the end of the memory");
cpu->setend();
cpu->setPC("0A");
memory1->setStart("0A");
}
//machine.setPC("0A");
string pc_str = cpu->getPC();
string pc_str2 = " "+pc_str;
ui->pc_line->setText(QString::fromStdString(pc_str2));
}
void MainWindow::on_C_S_btn_clicked()
{
ui->textEdit->clear();
memory1->setCell("00","00");
SCR_Next = "";
LASTHEX_Next = "";
cpu->clear_screen();
}
void MainWindow::on_Fetch_btn_clicked()
{
string address = (ui->start_line->text()).toStdString();
if(address == "00"){
QMessageBox::critical(this,"Error","You can't start from 00 address");
return;
}
if(address.size() != 2 || address == ""){
QMessageBox::critical(this,"Error","Please enter a valid address");
return;
}
else if(address[0] < '0' && address[0] > '9' && address[0] < 'A' && address[0] > 'F'){
QMessageBox::critical(this,"Error","Please enter a valid address");
return;
}
else if(address[1] < '0' && address[1] > '9' && address[1] < 'A' && address[1] > 'F'){
QMessageBox::critical(this,"Error","Please enter a valid address");
return;
}
machine.setPC(address);
string pc_str2 = " "+address;
ui->pc_line->setText(QString::fromStdString(pc_str2));
}