-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMIPSsim.cpp
More file actions
169 lines (156 loc) · 3.85 KB
/
MIPSsim.cpp
File metadata and controls
169 lines (156 loc) · 3.85 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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> // for strtoull
#include <sstream>
#include <math.h>// for power function
using namespace std;
int registers[32];
stringstream ss;
//calculate offset
int calOffset16(string str) {
int offset = strtol(str.c_str(), NULL, 2);
if(str.substr(0, 1) == "1") {
offset = offset - pow(2, 16);
}
return offset;
}
//decoding branch instructions
string branch(string line) {
string result = "";
int rs = strtoul(line.substr(6, 5).c_str(), NULL, 2);
ss << rs;
string RS = ss.str();
ss.str(string());
int rt = strtoul(line.substr(11,5).c_str(), NULL, 2);
ss << rt;
string RT = ss.str();
ss.str(string());
int offset = calOffset16(line.substr(16, 16));
ss << offset;
string strOff = ss.str();
ss.str(string());
result = "R" + RS + ", " + "R" + RT + ", " + "#" + strOff;
return result;
}
//decoding SW and LW
string loadStore(string line) {
int base = strtoul(line.substr(6, 5).c_str(), NULL, 2);
ss << base;
string Base = ss.str();
ss.str(string());
int rt = strtoul(line.substr(11,5).c_str(), NULL, 2);
ss << rt;
string RT = ss.str();
ss.str(string());
int offset = calOffset16(line.substr(16, 16));
ss << offset;
string strOff = ss.str();
ss.str(string());
string result = "R" + RT + ", " + strOff + "(R" + Base + ")";
return result;
}
//decode cat1 instructions
string cat1(string line) {
string instruction = "";
string operation = "";
//get the opcode
string opcode = line.substr(3,3);
if(opcode == "000") {
instruction = "NOP";
}
else if(opcode == "001") {
string bits26 = line.substr(6, 26);
string bits28 = bits26.append("00");
/*for this specific project, PC is a small number, so we just calculate the 28 bits,
ignoring the four 0s in MSB. But this is not exactly what happens in processor.
To follow the logic in processor, we can let X = 0xF0000000 &(pc+4), Y = offset << 2;
then get newPC = X|Y
*/
int newPC = strtol(bits28.c_str(), NULL, 2);
operation = "J #";
ss << newPC;
instruction = operation + ss.str();
ss.str(string());
}
else if(opcode == "010") {
//BEQ
operation = "BEQ ";
instruction = operation.append(branch(line));
}
else if(opcode == "011") {
//BNE
operation = "BNE ";
instruction = operation.append(branch(line));
}
else if(opcode == "100") {
//BGTZ
operation = "BGTZ ";
int rs = strtoul(line.substr(6, 5).c_str(), NULL, 2);
ss << rs;
string RS = ss.str();
ss.str(string());
int offset = calOffset16(line.substr(16, 16));
//long long int offset = strtoll(line.substr(16, 16).c_str(), NULL, 2);
ss << offset;
instruction = operation + "R" + RS + ", " + "#" + ss.str();
ss.str(string());
}
else if(opcode == "101") {
//SW
operation = "SW ";
instruction = operation.append(loadStore(line));
}
else if(opcode == "110") {
//LW
operation = "LW ";
instruction = operation.append(loadStore(line));
}
else {
//BREAK
instruction = "BREAK";
}
return instruction;
}
int main() {
/*int main(int argc, char* argv[]) {
ifstream inFile;
if(argc == 2) {
inFile.open(argv[1]);
if(inFile.fail()) {
cerr << "Error when opening file.\n";
exit(1);
}
}
else {
cout << "\nInvalid input, please enter the executable name and the test file name\n";
}*/
//open the two output files
ofstream outFile1;
outFile1.open("disassembly.txt");
ofstream outFile2;
outFile2.open("simulation.txt");
ifstream inFile;
inFile.open("sample.txt");
//read input and decode it
string line;
int address = 64;
string instruction;
while(getline(inFile, line)) {
if(line.substr(0,3) == "001") {
instruction = cat1(line);
outFile1 << line << "\t" << address << "\t" << instruction << "\n";
}
else if(line.substr(0,3) == "010") {
//cat2(line);
}
else {
//cat3(line);
}
address += 4;
}
inFile.close();
outFile1.close();
outFile2.close();
return 0;
}