-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual_machine_gui.hpp
171 lines (161 loc) · 4.59 KB
/
virtual_machine_gui.hpp
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
#ifndef VIRTUAL_MACHINE_GUI_HPP
#define VIRTUAL_MACHINE_GUI_HPP
#include <QString>
#include <QWidget>
#include <qlabel.h>
#include <QPlainTextEdit>
#include <QHeaderView>
#include <QTableView>
#include <QLineEdit>
#include <QStandardItemModel>
#include <QPushButton>
#include <QLayout>
#include <token.hpp>
#include "parser.hpp"
#include "virtualMachine.hpp"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <QTextBlock>
#include <QTextEdit>
// TODO define the GUI class
class VirtualMachineGUI: public QWidget{
Q_OBJECT
public:
VirtualMachineGUI();
~VirtualMachineGUI();
void load(QString filename);
void setregister();
void closeEvent(QCloseEvent *event);
void update();
bool setMemory(MemorySet & mem);
public slots:
void changeReg() {
if (myvirtual.getPc() == myvirtual.getInstructionSize()-1) {
myvirtual.pcSimulate();
if (myvirtual.getPc() != myvirtual.getInstructionSize()) {
cursor.setBlockFormat(white);
size_t num = GuiInstructions[myvirtual.getPc()].line;
cursor.movePosition(QTextCursor::Start);
for (size_t index = 0; index != num; index++) {
cursor.movePosition(QTextCursor::Down);
}
cursor.setBlockFormat(background);
}
std::stringstream ss;
string registerName;
QString QregisterValue;
string registerValue;
for (size_t i = 0; i < 35; i++) {
QregisterValue = regModel->item(i, 1)->text();
registerName = QregisterValue.toStdString();
ss << "0x" << std::setfill('0') << std::setw(8) << std::hex << myvirtual.printRegisters(registerName);
ss >> registerValue;
regModel->setItem(i, 2, new QStandardItem(QString::fromStdString(registerValue)));
ss.clear();
}
string address;
QString Qaddress;
for (size_t i = 0; i < 512; i++) {
Qaddress = memModel->item(i, 0)->text();
address = Qaddress.toStdString();
istringstream buffer{ address };
uint32_t memoryadd;
buffer >> std::hex >> memoryadd;
ss << "0x";
ss << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(myvirtual.getMemoryAdd(memoryadd));
ss >> registerValue;
memModel->setItem(i, 1, new QStandardItem(QString::fromStdString(registerValue)));
ss.clear();
}
}
else if (myvirtual.getPc() < myvirtual.getInstructionSize()-1) {
myvirtual.pcSimulate();
cursor.setBlockFormat(white);
size_t num = GuiInstructions[myvirtual.getPc()].line;
cursor.movePosition(QTextCursor::Start);
for (size_t index = 0; index != num; index++) {
cursor.movePosition(QTextCursor::Down);
}
cursor.setBlockFormat(background);
std::stringstream ss;
string registerName;
QString QregisterValue;
string registerValue;
for (size_t i = 0; i < 35; i++) {
QregisterValue = regModel->item(i, 1)->text();
registerName = QregisterValue.toStdString();
ss << "0x" << std::setfill('0') << std::setw(8) << std::hex << myvirtual.printRegisters(registerName);
ss >> registerValue;
regModel->setItem(i, 2, new QStandardItem(QString::fromStdString(registerValue)));
ss.clear();
}
string address;
QString Qaddress;
for (size_t i = 0; i < 512; i++) {
Qaddress = memModel->item(i, 0)->text();
address = Qaddress.toStdString();
istringstream buffer{ address };
uint32_t memoryadd;
buffer >> std::hex >> memoryadd;
ss << "0x";
ss << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(myvirtual.getMemoryAdd(memoryadd));
ss >> registerValue;
memModel->setItem(i, 1, new QStandardItem(QString::fromStdString(registerValue)));
ss.clear();
}
}
else {
cursor.setBlockFormat(white);
status->setText("Error: invalid Step(End of instructions)");
}
}
void setUpThread() {
string m;
mq.push("run");
if (!isThreadUp) {
myvirtual.threadSet(&mq);
isThreadUp = true;
}
text->setDisabled(true);
step->setDisabled(true);
status->setDisabled(true);
registers->setDisabled(true);
memory->setDisabled(true);
run->setDisabled(true);
runBreak->setEnabled(true);
}
void runStop() {
mq.push("break");
text->setEnabled(true);
step->setEnabled(true);
status->setEnabled(true);
registers->setEnabled(true);
memory->setEnabled(true);
run->setEnabled(true);
runBreak->setDisabled(true);
update();
}
private:
size_t beginLine;
size_t currentLine;
QPlainTextEdit *text;
QTableView *registers;
QTableView *memory;
QLineEdit *status;
QPushButton *step;
QPushButton *run;
QPushButton *runBreak;
QLabel *statusShow;
QStandardItemModel *regModel;
QStandardItemModel *memModel;
VirtualMachine myvirtual;
bool isFileOk = true;
bool isThreadUp = false;
QTextCursor cursor;
QTextBlockFormat background;
QTextBlockFormat white;
instructionSet GuiInstructions;
MessageQueue mq;
};
#endif