-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmulator.cpp
More file actions
327 lines (304 loc) · 7.81 KB
/
Emulator.cpp
File metadata and controls
327 lines (304 loc) · 7.81 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
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
#include "Emulator.h"
#include "Command.h"
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
union Union
{
char c;
int i;
float f;
Word32 w;
};
void Emulator::showRegester(Word32 id)
{
cout << "寄存器" << id << ": " << memory.getReg(id) << endl;
}
void Emulator::showByte(Word32 id)
{
Word32 t = memory.getByte(id);
cout << "字节" << id << ": " << t << " (";
for (int i = 7; i >= 0; i--)
cout << ((t >> i) & 1);
cout << ")" << endl;
}
void Emulator::showWord(Word32 id)
{
Word32 t = memory.getWord(id);
cout << "字" << id << ": " << t << " (";
for (int i = 31; i >= 0; i--)
cout << ((t >> i) & 1);
cout << ")" << endl;
}
void Emulator::showCommand(Word32 id)
{
cout << "指令" << setw(10) << id << ": " << setw(0);
cout << Translator::c2s(memory.getWord(id)) << endl;
}
void Emulator::writeMemory(Word32 id, Command cmd)
{
memory.setWord(id, cmd);
}
void Emulator::loadMIPS(Word32 id, string filename)
{
CommandLoader cl;
auto prog = cl.load(filename);
for(auto &x : prog)
{
memory.setWord(id, x);
id += 4;
}
}
bool Emulator::run(bool show)
{
Word32 id = memory.getPC();
Command s = memory.getWord(id);
Word32 val = Translator::getType(s);
/*
老版本退出
if (s == 0xFFFFFFFFu)
return false;
*/
if (show)
showCommand(id);
memory.step();
// 系统调用
if (s == 0x0000000c)
{
Word32 action = memory.getReg(2); // $v0
Union t;
memory.getReg(4);
if (action == 1)
{
t.w = memory.getReg(4);
printf("%d", t.i);
}
else if (action == 2)
{
t.w = memory.getReg(4);
printf("%f", t.f);
}
else if (action == 4)
{
t.w = memory.getReg(4);
printf("%s", memory.getString(t.w));
}
else if (action == 5)
{
scanf("%d", &t.i);
memory.setReg(4, t.w);
}
else if (action == 6)
{
scanf("%f", &t.f);
memory.setReg(4, t.w);
}
else if (action == 8)
{
t.w = memory.getReg(4);
scanf("%s", memory.getString(t.w));
}
else if (action == 10)
{
return false;
}
else if (action == 11)
{
t.w = memory.getReg(4);
printf("%c", t.c);
}
else if (action == 12)
{
t.w = 0;
scanf("%c", &t.c);
memory.setReg(4, t.w);
}
}
// I 类型
// 存取
else if(val == LW) // lw
memory.setReg(Translator::getR2(s), memory.getWord(memory.getReg(Translator::getR1(s)) + Translator::getIm(s)));
else if(val == SW) // sw
memory.setWord(memory.getReg(Translator::getR1(s)) + Translator::getIm(s), memory.getReg(Translator::getR2(s)));
else if(val == LUI) // lui
memory.setReg(Translator::getR2(s), Translator::getIm(s) << 16);
// 跳跃
else if(val == BEQ) // beq
{
if(memory.getReg(Translator::getR1(s)) == memory.getReg(Translator::getR2(s)))
memory.setPC(memory.getPC() + (((int)(short)Translator::getIm(s)) << 2));
}
// 运算
else if(val == ORI) // ori
memory.setReg(Translator::getR2(s), memory.getReg(Translator::getR1(s)) | Translator::getIm(s));
else if(val == ADDI) // addi
memory.setReg(Translator::getR2(s), memory.getReg(Translator::getR1(s)) + Translator::getIm(s));
// R类型
else if(val == R)
{
Word32 type = Translator::getOp(s);
if(type == ADD)
memory.setReg(Translator::getR3(s), memory.getReg(Translator::getR1(s)) + memory.getReg(Translator::getR2(s)));
else if(type == SUB)
memory.setReg(Translator::getR3(s), memory.getReg(Translator::getR1(s)) - memory.getReg(Translator::getR2(s)));
else if(type == SLT)
memory.setReg(Translator::getR3(s), memory.getReg(Translator::getR1(s)) < memory.getReg(Translator::getR2(s)));
}
// J类型
else if(val == J) // j
memory.setPC((memory.getPC() & 0xF0000000) | (Translator::getVal(s, 25, 0) << 2) );
return true;
}
void Emulator::mainLoop()
{
string cmd, c, tmp;
Word32 id, t;
while(true)
{
cout << ">>> ";
getline (cin, tmp);
stringstream ss(tmp);
ss >> cmd;
if (cmd == "R")
{
if (!ss.good())
{
cout << "错误的指令。" << endl;
continue;
}
ss >> c;
}
else if(cmd != "Q" && cmd != "H" && cmd != "T")
{
if (!ss.good())
{
cout << "错误的指令。" << endl;
continue;
}
ss >> id;
}
if(cmd == "R")
{
if (Translator::notID(c))
{
cout << "错误的指令。" << endl;
continue;
}
id = Translator::getID(c);
showRegester(id);
}
else if(cmd == "D")
{
if((id & 3) == 0) // 对齐
showWord(id);
showByte(id);
}
else if(cmd == "U")
{
if(id & 3)
cout << "地址无意义,请检查是否对齐。" << endl;
else
showCommand(id);
}
else if(cmd == "A")
{
getline(ss, c);
if(id & 3)
cout << "地址无意义,请检查是否对齐。" << endl;
else
{
auto cmds = Translator::s2c(c);
for (auto &t : cmds)
{
writeMemory(id, t);
id += 4;
}
}
}
else if(cmd == "W")
{
ss >> t;
if (ss.good())
{
cout << "错误的指令。" << endl;
continue;
}
if(id & 3)
cout << "地址无意义,请检查是否对齐。" << endl;
else
writeMemory(id, t);
}
else if(cmd == "L")
{
ss >> c;
if (ss.good())
{
cout << "错误的指令。" << endl;
continue;
}
if(id & 3)
cout << "地址无意义,请检查是否对齐。" << endl;
else
loadMIPS(id, c);
}
else if(cmd == "T")
{
if (ss.good())
{
ss >> id;
if(id & 3)
{
cout << "地址无意义,请检查是否对齐。" << endl;
continue;
}
memory.setPC(id);
}
run(1);
}
else if(cmd == "G")
{
if(id & 3)
cout << "地址无意义,请检查是否对齐。" << endl;
else
{
memory.setPC(id);
while (run(0));
getline (cin, tmp);
}
}
else if(cmd == "Q")
{
break;
}
else if(cmd == "H")
{
cout << "\
======================================================\n\
R <寄存器号> 看寄存器\n\
D <内存号> 看内存\n\
U <内存号> 看指令\n\
W <内存号> <内容> 写入32位数据\n\
A <内存号> <指令> 写入指令(注意是字符指令不是二进制)\n\
L <内存号> <文件名> 从文件加载指令序列\n\
T <内存号> 执行指令\n\
G <内存号> 执行指令直到遇到结束符0xFFFFFFFFu\n\
H 查看本帮助\n\
Q 退出\n\
======================================================\n" << endl;
}
else
{
cout << "错误的指令。" << endl;
}
}
}
void Emulator::start()
{
mainLoop();
}
Emulator::Emulator() :
memory()
{
Translator::init();
}