-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachine.cpp
More file actions
289 lines (256 loc) · 8.17 KB
/
Copy pathMachine.cpp
File metadata and controls
289 lines (256 loc) · 8.17 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
#include <bits/stdc++.h>
#include "Machine.h"
Memory::Memory() : memo(256){
for (int j = 0; j < memo.size(); ++j) {
memo[j] = "00";
}
string filee = "Instruction.txt";
ifstream file(filee);
string line;
int i = 0;
while (getline(file, line)) {
string first = "", second = "", third = "";
istringstream iss(line);
if (!(iss >> first >> second >> third)) {
break;
}
string str = "", str2 = "";
str = str + first[2] + second[2];
str2 = str2 + third[2] + third[3];
memo[i] = str;
memo[++i] = str2;
++i;
}
file.close();
}
Memory::Memory(string filee) :memo(256) {
for (int j = 0; j < memo.size(); ++j) {
memo[j] = "00";
}
ifstream file(filee);
string line;
int i = 0;
while (getline(file, line)) {
string first = "", second = "", third = "";
istringstream iss(line);
if (!(iss >> first >> second >> third)) {
break;
}
string str = "", str2 = "";
str = str + first[2] + second[2];
str2 = str2 + third[2] + third[3];
memo[i] = str;
memo[++i] = str2;
++i;
}
file.close();
}
string Machine::getNextInstruction(const string &first, const string &second, const string &third) {
string instruction = first.substr(2, 1) + second.substr(2, 1) + third.substr(2, 1) + third.substr(3, 1);
return instruction;
}
void Instruction::execute() {
vector<string> instruction;
ifstream file("Instruction.txt");
string line;
while (getline(file, line)) {
string first = "", second = "", third = "";
istringstream iss(line);
if (!(iss >> first >> second >> third)) {
break;
}
instruction.emplace_back(machine.getNextInstruction(first, second, third));
}
file.close();
for (int i = 0; i < instruction.size(); ++i) {
programCounter += 2;
char inst = instruction[i][0];
string r = "", a = ""; //register and address
r = r + instruction[i][1];
a = a + instruction[i][2] + instruction[i][3];
if (inst == '1') {
Load1(r, a);
} else if (inst == '2') {
Load2(r, a);
} else if (inst == '3') {
if (a == "00") {
int index_reg = stoi(r, nullptr, 16);
cout << regi.R[index_reg] << "\n";
} else
Store(r, a);
} else if (inst == '4') {
string rr = "", ss = "";
rr = rr + instruction[i][2];
ss = ss + instruction[i][3];
move(rr, ss);
} else if (inst == '5') {
string rr = "", ss = "";
rr = rr + instruction[i][2];
ss = ss + instruction[i][3];
add(r, rr, ss);
} else if (inst == 'B') {
int index = jump(r, a, i);
if (index == i) {
continue;
} else {
i = index - 1;
programCounter = index * 2;
}
} else if (inst == 'C') {
return;
}
}
}
void Instruction::Load1(const string ®, const string &address) {
int index_reg = stoi(reg, nullptr, 16);
int index_address = stoi(address, nullptr, 16);
regi.R[index_reg] = memory.memo[index_address];
}
void Instruction::Load2(const string ®, const string &address) {
int index_reg = stoi(reg, nullptr, 16);
regi.R[index_reg] = address;
}
void Instruction::Store(const string ®, const string &address) {
int index_reg = stoi(reg, nullptr, 16);
int index_address = stoi(address, nullptr, 16);
memory.memo[index_address] = regi.R[index_reg];
}
void Instruction::move(const string& R, const string& S) {
int index_reg1 = stoi(R, nullptr, 16);
int index_reg2 = stoi(S, nullptr, 16);
regi.R[index_reg2] = regi.R[index_reg1];
// regi.R[index_reg1] = "00";
}
void Instruction::add(const string &R, const string &S, const string &T) {
int index_reg = stoi(R, nullptr, 16);
int index_reg1 = stoi(S, nullptr, 16);
int index_reg2 = stoi(T, nullptr, 16);
string patternS = regi.R[index_reg1], patternT = regi.R[index_reg2];
string binaryS = "";
char c;
for (int j = 0; j < patternS.length(); j++) {
c = patternS[j];
if (c >= '0' && c <= '9') {
binaryS += bitset<4>(c - '0').to_string();
} else if (c >= 'A' && c <= 'F') {
binaryS += bitset<4>(c - 'A' + 10).to_string();
} else if (c >= 'a' && c <= 'f') {
binaryS += bitset<4>(c - 'a' + 10).to_string();
}
}
string binaryT = "";
for (int i = 0; i < patternT.length(); i++) {
c = patternT[i];
if (c >= '0' && c <= '9') {
binaryT += bitset<4>(c - '0').to_string();
} else if (c >= 'A' && c <= 'F') {
binaryT += bitset<4>(c - 'A' + 10).to_string();
} else if (c >= 'a' && c <= 'f') {
binaryT += bitset<4>(c - 'a' + 10).to_string();
}
}
string sum = "";
int len1, len2, i, j, ds = 0;
len1 = binaryS.size();
len2 = binaryT.size();
i = len1 - 1;
j = len2 - 1;
while (i >= 0 || j >= 0 || ds == 1) {
ds = ds + ((i >= 0) ? binaryS[i] - '0' : 0);
ds = ds + ((j >= 0) ? binaryT[j] - '0' : 0);
sum = char(ds % 2 + '0') + sum;
ds = ds / 2;
i--;
j--;
}
string hexa = "";
for (int i = 0; i < sum.length(); i += 4) {
string b = sum.substr(i, 4);
c = (char) stoi(b, nullptr, 2);
if (c >= 10 && c <= 15) {
hexa += char(c - 10 + 'A');
} else {
hexa += to_string(c);
}
}
regi.R[index_reg] = hexa;
}
int Instruction::jump(const string& reg, const string& address, int currentIndex) {
int index_reg = stoi(reg, nullptr, 16); //register index in registers //01
int index_address = stoi(address, nullptr, 16); //address in memory //10 index 16(val)
if (regi.R[index_reg] == regi.R[0]) {
int jump_index = (index_address / 2); //0xB 0x1 0x10
return jump_index;
} else {
return currentIndex;
}
}
void Instruction::setR(Register ®) {
for (int i = 0; i < 16; ++i) {
reg.R[i] = regi.R[i];
}
}
void Instruction::setMemo(Memory &memory1) {
for (int i = 0; i < 256; ++i) {
memory1.memo[i] = memory.memo[i];
}
}
void Machine::output(Memory &memory, Register ®ister1) {
cout << "Memory: " << '\n';
for (int i = 0; i < memory.memo.size(); ++i) {
cout << memory.memo[i] << '\n';
}
cout << "Registers: " << '\n';
for (int i = 0; i < 16; ++i) {
cout << register1.R[i] << '\n';
}
cout<<"Program Counter: "<<hex<< programCounter <<'\n';
}
void Machine::displayMenu() {
int n;
Instruction I;
cout << "What would you like to choose:\n"
"(1) Load Program File\n"
"(2) Run Program File\n"
"(3) Print contents of Registers and Memory addresses\n"
"(4) Exit: ";
while (cin >> n) {
if (n == 1) {
cout << "Enter file name: ";
string file;
cin >> file;
Memory memory1(file);
} else if (n == 2) {
I.execute();
} else if (n == 3){
I.output(memory, R);
} else if (n == 4){
return;
} else{
cout << "Enter a valid number: ";
}
cout << "What would you like to choose:\n"
"(1) Load Program File\n"
"(2) Run Program File\n"
"(3) Print contents of Registers and Memory addresses\n"
"(4) Exit: ";
}
}
void Instruction::output(Memory &memory, Register ®) {
setR(reg);
setMemo(memory);
cout << "Memory: " << '\n';
for (int i = 0; i < memory.memo.size(); ++i) {
cout << memory.memo[i] << '\n';
}
cout << "Registers: " << '\n';
for (int i = 0; i < 16; ++i) {
cout << regi.R[i] << '\n';
}
cout<<"Program Counter: "<<hex<< programCounter <<'\n';
}
Register::Register() : R(16){
for (int j = 0; j < R.size(); ++j) {
R[j] = "00";
}
}