-
Notifications
You must be signed in to change notification settings - Fork 0
/
I_Format.cpp
263 lines (238 loc) · 5.16 KB
/
I_Format.cpp
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
#include <iostream>
#include <iomanip>
#include <string.h>
#include <math.h>
#include "register.h"
#include "getNumber.h"
#include "getRegister.h"
#include "getWord.h"
#include "serial.h"
#include "getText.h"
using namespace std;
// PROTOTYPE----------------------------------------------------------
static Register reg("registerList.txt");
Serial cmd;
#pragma region Prototype
// string extractRs(string _instruction);
// string extractRt(string _instruction);
// int extractImm(string _instrution);
// string extractName(string _instruction);
class Instruction {
protected:
public:
static string extractName(string _instruction);
Instruction() {};
virtual void init(string _instruction) = 0;
virtual void execute() = 0;
virtual string getName() = 0;
virtual ~Instruction() {}
};
class I_Format : public Instruction {
protected:
string rs;
string rt;
int imm;
const string NAME;
public:
I_Format();
I_Format(string _name) : NAME(_name) {}
virtual string getName() = 0;
virtual void execute() = 0;
virtual ~I_Format() {}
void init(string _instruction);
};
#pragma endregion Prototype
#pragma region init I-Format
void I_Format::init(string _instruction){
rs = getRegister(_instruction, 1);
rt = getRegister(_instruction, 2);
imm = getNumbers(_instruction);
}
#pragma endregion init I-Format
#pragma region Prototype Subclass
class Addi : public I_Format {
protected:
public:
Addi();
string getName();
void execute();
~Addi();
};
class Andi : public I_Format {
protected:
public:
Andi();
string getName();
void execute();
~Andi();
};
class Ori : public I_Format {
protected:
public:
Ori();
string getName();
void execute();
~Ori();
};
class Slti : public I_Format {
protected:
public:
Slti();
string getName();
void execute();
~Slti();
};
class Li : public I_Format {
protected:
public:
Li();
string getName();
void execute();
~Li();
};
class Lui : public I_Format {
protected:
public:
Lui();
string getName();
void execute();
~Lui();
};
class Lw : public I_Format {
protected:
public:
Lw();
string getName();
void execute();
~Lw();
};
#pragma endregion Prototype Subclass
#pragma region Addi
Addi::Addi() : I_Format("addi") {}
string Addi::getName() {
return this->NAME;
}
void Addi::execute() {
int temp;
temp = reg.getRegisterValue(rt) + imm;
reg.setRegisterValue(rs, temp);
cmd.write(rs, reg.getRegisterValue(rs));
}
Addi::~Addi() {
cout << "Destructor Addi called\n";
}
#pragma endregion Addi
#pragma region Andi
Andi::Andi() : I_Format("andi") {}
string Andi::getName() {
return this->NAME;
}
void Andi::execute() {
int temp;
temp = reg.getRegisterValue(rt) & imm;
reg.setRegisterValue(rs, temp);
cmd.write(rs, reg.getRegisterValue(rs));
}
Andi::~Andi() {
cout << "Destructor Andi called\n";
}
#pragma endregion Andi
#pragma region Ordi
Ori::Ori() : I_Format("ori") {}
string Ori::getName() {
return this->NAME;
}
void Ori::execute() {
int temp;
temp = reg.getRegisterValue(rt) | imm;
reg.setRegisterValue(rs, temp);
cmd.write(rs, reg.getRegisterValue(rs));
}
Ori::~Ori() {
cout << "Destructor Ori called\n";
}
#pragma endregion Ordi
#pragma region Slti
Slti::Slti() : I_Format("slti") {}
string Slti::getName() {
return this->NAME;
}
void Slti::execute() {
int temp;
if (reg.getRegisterValue(rt) < imm) reg.setRegisterValue(rs, 1);
else reg.setRegisterValue(rs,0);
cmd.write(rs, reg.getRegisterValue(rs));
}
Slti::~Slti() {
cout << "Destructor Slti called\n";
}
#pragma endregion Slti
#pragma region Li
Li::Li() : I_Format("li") {}
string Li::getName() {
return this->NAME;
}
void Li::execute() {
reg.setRegisterValue(rs, imm);
cmd.write(rs, reg.getRegisterValue(rs));
}
Li::~Li() {
cout << "Destructor Li called\n";
}
#pragma endregion Li
Lui::Lui() : I_Format("lui") {}
string Lui::getName() {
return this->NAME;
}
void Lui::execute() {
long temp = imm * 65536;
reg.setRegisterValue(rs, temp);
cmd.write(rs, reg.getRegisterValue(rs));
}
Lui::~Lui() {
cout << "Destructor Lui called\n";
}
#pragma region Lw
Lw::Lw() : I_Format("lw") {}
string Lw::getName() {
return this->NAME;
}
void Lw::execute() {
// int temp;
// temp = reg.getAddressValue | imm;
// reg.setRegisterValue(rs, temp);
// cmd.write(rs, reg.getRegisterValue(rs));
}
Lw::~Lw() {
cout << "Destructor Ordi called\n";
}
#pragma endregion Lw
I_Format* navigationCommand(string _instruction) {
string name = getWord(_instruction, 1);
cout << "Name: " << name << '\n';
if(!name.compare("addi")) return new Addi;
else if(!name.compare("andi")) return new Andi;
else if(!name.compare("ori")) return new Ori;
else if(!name.compare("slti")) return new Slti;
else if(!name.compare("li")) return new Li;
else if(!name.compare("lui")) return new Lui;
else return nullptr;
}
// Replace int main() with int process()
int main() {
FileAssembly fileIn("assembly.txt");
string instruction = fileIn.getInstruction(0);
cout << getNumbers(instruction);
// Instruction* ptr = navigationCommand(instruction);
// reg.init();
// cmd.init();
// int t;
// for (int t = 0; t < 3; t++){
// instruction = fileIn.getInstruction(4*t);
// ptr->init(instruction);
// cout << "Next command: " << optimizeString(instruction) << endl;
// cmd.pause();
// ptr->execute();
// cmd.init();
return 0;
}