-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpu.cpp
More file actions
229 lines (213 loc) · 5.79 KB
/
Copy pathcpu.cpp
File metadata and controls
229 lines (213 loc) · 5.79 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
#include "cpu.h"
#include "qdebug.h"
#include <iomanip>
#include <sstream>
CPU::CPU(QObject *parent) : QObject(parent) {
Instr::prepareDict();
updateDisplay();
execThread = new CPUExecThreads(this);
m_PC = 0;
}
CPU::~CPU() { delete execThread; }
uint32_t &CPU::readMem(uint32_t mem) {
if (mem < MEM_SIZE) {
return m_mem[mem];
} else {
m_run = false;
Instr instr;
instr.decode(m_mem[m_nextPC]);
showMsg(QString::fromStdString(
"[ERROR] Memory access at " + instr.disasm() + " [" +
instr.dumpRegs(this) + "] beyond the simulator's memory"));
dumpStatus();
dumpMem();
}
return m_mem[0];
}
static bool isDigit(std::string &std) {
if (std::find_if(std.begin(), std.end(), [](unsigned char c) {
return !std::isdigit(c);
}) == std.end()) {
return true;
}
return false;
}
std::string CPU::processLabels(std::string input_string,
std::stringstream &labels) {
std::stringstream input;
input.str(input_string);
std::map<std::string, uint32_t> label_map;
Instr instr;
uint32_t pc = 0;
while (input.rdbuf()->in_avail()) {
std::string input_line;
std::getline(input, input_line);
if (input_line.empty()) {
continue;
}
std::size_t found = input_line.find(":");
if (found != std::string::npos) {
label_map[input_line.substr(0, found)] = pc;
continue;
}
try {
std::stringstream instr_stream(input_line);
std::string label = instr.assembler(instr_stream);
if (label.empty()) {
pc++;
} else {
if (isDigit(label)) {
showMsg(QString("[ERROR] invalid instruction at ") +
QString::number(pc));
return std::string();
}
}
} catch (std::invalid_argument const &) {
pc++;
} catch (...) {
showMsg(QString("[ERROR] invalid instruction at ") + QString::number(pc));
return std::string();
}
}
for (auto &labelRec : label_map) {
std::string label = labelRec.first;
std::string targetPC = std::to_string(labelRec.second);
labels << " " << label << "[" << targetPC << "]";
}
std::stringstream final_input;
final_input.str(input_string);
std::stringstream output;
pc = 0;
while (final_input.rdbuf()->in_avail()) {
std::string input_line;
std::getline(final_input, input_line);
if (input_line.empty()) {
continue;
}
std::size_t found = input_line.find(":");
if (found != std::string::npos) {
continue;
}
std::size_t last_space = input_line.find_last_of(" ") + 1;
std::string label = input_line.substr(last_space);
if (label_map.find(label) != label_map.end()) {
input_line.replace(last_space, label.size(),
std::to_string(label_map[label]));
}
pc++;
output << input_line << "\n";
}
return output.str();
}
void CPU::readInstrs(QString input_string) {
if (m_run) {
return;
}
std::stringstream input;
std::stringstream labels;
input.str(processLabels(input_string.toStdString(), labels));
if (!input.rdbuf()->in_avail()) {
return;
}
Instr instr;
for (uint32_t mem = 0; mem < MEM_SIZE; mem++) {
if (input.rdbuf()->in_avail()) {
std::string label;
try {
label = instr.assembler(input);
if (!label.empty()) {
showMsg(QString("[ERROR] invalid instruction ") +
QString::number(mem) + QString(" : ") +
QString::fromStdString(label));
return;
}
m_mem[mem] = instr.code();
} catch (std::invalid_argument const &) {
showMsg(QString("[ERROR] invalid instruction ") + QString::number(mem));
return;
}
} else {
m_mem[mem] = 0;
}
}
m_PC = 0;
dumpStatus();
dumpMem();
updateDisplay();
showMsg(QString("Code loaded. Labels: ") +
QString::fromStdString(labels.str()));
}
void CPU::run() {
if (m_run) {
return;
}
m_run = true;
showMsg("CPU running...");
execThread->start();
}
void CPU::pause() {
m_run = false;
Instr instr;
instr.decode(m_mem[m_nextPC]);
showMsg(QString::fromStdString("CPU paused at " + instr.disasm() + " [" +
instr.dumpRegs(this) + "]"));
dumpStatus();
dumpMem();
}
void CPU::stop() {
m_run = false;
m_PC = 0;
showMsg("CPU stoped");
dumpStatus();
dumpMem();
}
void CPU::step() {
if (m_run) {
return;
}
Instr instr;
instr.decode(m_mem[m_PC]);
m_nextPC = m_PC + 1;
instr.execute(this);
m_PC = m_nextPC;
instr.decode(m_mem[m_PC]);
showMsg(QString::fromStdString("CPU paused at " + instr.disasm() + " [" +
instr.dumpRegs(this) + "]"));
dumpStatus();
dumpMem();
}
void CPUExecThreads::run() {
Instr instr;
while (cpu->m_run) {
cpu->m_nextPC = cpu->m_PC + 1;
instr.executeCode(cpu, cpu->m_mem[cpu->m_PC]);
if (cpu->m_run) {
cpu->m_PC = cpu->m_nextPC;
}
}
cpu->dumpStatus();
cpu->dumpMem();
}
void CPU::dumpStatus() {
std::stringstream dump;
dump << "PC : " << m_PC << "\n\n";
for (uint32_t i = 0; i < REG_SIZE; i++) {
dump << "r" << i << " : " << m_regFile[i] << "\n";
}
emit statusUpd(QString::fromStdString(dump.str()));
}
void CPU::dumpMem() {
std::stringstream dump;
for (uint32_t mem = 0; mem < MEM_SIZE; mem++) {
dump << "0x" << std::setfill('0') << std::setw(4) << std::hex << mem
<< " : " << std::setw(2) << (m_mem[mem] >> 24) << " " << std::setw(2)
<< ((m_mem[mem] >> 16) & 0xFF) << " " << std::setw(2)
<< ((m_mem[mem] >> 8) & 0xFF) << " " << std::setw(2)
<< (m_mem[mem] & 0xFF) << "\n";
}
emit memUpd(QString::fromStdString(dump.str()));
}
void CPU::updateDisplay() {
emit displayUpd(m_mem + (MEM_SIZE / 2), DIS_WIDTH, DIS_HEIGHT, DIS_SCALE);
}
void CPU::showMsg(QString msg) { emit sendMsg(msg); }