-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVole_Machine.cpp
More file actions
188 lines (175 loc) · 4.26 KB
/
Copy pathVole_Machine.cpp
File metadata and controls
188 lines (175 loc) · 4.26 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
#include "Vole_Machine.h"
void Register::setRegister(string location, string value)
{
int i=stoi(location,0,16);
if(i>=R.size())
{
cout<<"the Register location is not found\n";
}
else
{
R[i]=value;
}
}
void Register::clearRegister() {
for(string &value:R)
{
value="00";
}
}
void Register::DisplayRegisters() {
cout<<"The Registers: \n";
for (int i = 0; i < R.size(); ++i) {
stringstream stream;
stream << hex << i;
cout<<stream.str()<<' '<<R[i]<<endl;
}
}
string Register::getRegister(string location) {
int i=stoi(location,0,16);
if(i<R.size())
return R[i];
else
cout<<"the Register is not found\n";
}
Register::Register(int size,string InitValue)
{
for(int i=0;i<size;i++)
{
R.push_back(InitValue);
}
}
void Memory::setMemory(string location, string value) {
int i=stoi(location,0,16);
if(i>=M.size())
{
cerr<<"the Memory location is not found\n";
}
else
{
M[i]=value;
}
}
void Memory::DisplayMemory() {
cout<<"The Memory cells: "<<endl;
for (int i = 0; i < M.size(); ++i) {
stringstream stream;
stream << hex << i;
cout<<stream.str()<<' '<<M[i]<<endl;
}
}
Memory::Memory(int size, string InitValue) {
for(int i=0;i<size;i++)
{
M.push_back(InitValue);
}
}
void Memory::clearMemory() {
for(string&value:M)
{
value="00";
}
}
string Memory::getMemory(string location) {
int i=stoi(location,0,16);
if(i<M.size())
return M[i];
}
Machine::Machine(int RegisterSize, string initValue1, int MemorySize, string initValue)
: Register(RegisterSize, initValue1),
Memory(MemorySize, initValue),counter(0),Exit(false) {
}
void Machine::Instruction(int i) {
switch (i)
{
case 1: {
setMemory(B[counter][2] + B[counter][3], getRegister(B[counter][1]));
}
break;
case 2: {
setRegister(B[counter][1], B[counter][2] + B[counter][3]);
}
break;
case 3:
{
string value = getRegister(B[counter][1]);
setMemory(B[counter][2]+B[counter][3], value);
}
break;
case 4:
setRegister(B[counter][3], getRegister(B[counter][2]));
break;
case 5:
{
const int a = stoi(getRegister(B[counter][2]), 0, 16);
const int b = stoi(getRegister(B[counter][3]), 0, 16);
bitset<16> bitsetA(a);
bitset<16> bitsetB(b);
bitset<16 + 1> resultBitset = bitsetA.to_ullong() + bitsetB.to_ullong();
if (resultBitset[16]) {
resultBitset &= ~(1 << 16);
}
int result = resultBitset.to_ulong();
stringstream stream;
stream << hex << result;
string s=stream.str();
if(s.size()==1)
s='0'+s;
setRegister(B[counter][1], s);
}
break;
case 6:
break;
case 11:
{
if (getRegister("0") == getRegister(B[counter][1])) {
counter = stoi(B[counter][2] + B[counter][3], 0, 16)-1;
Instruction(stoi(B[counter][0], 0, 16));
}
}
break;
case 12:
{
Exit = true;
}
break;
default:
cerr<<"Error, the instruction is not valid"<<endl;
}
}
void Machine::RunMachine(string filename)
{
ifstream inputFile(filename);
if (!inputFile.is_open()) {
cerr << "Error opening the file!" << endl;
return;
}
int cnt=0;
vector<string> tmp(4);
while (inputFile>>tmp[cnt])
{
tmp[cnt]=tmp[cnt].substr(2);
if(tmp[cnt].size()==2)
{
tmp[cnt+1]=tmp[cnt].back();;
tmp[cnt].pop_back();
cnt++;
}
cnt++;
if(cnt==4)
{
cnt=0;
B.push_back(tmp);
counter_sz++;
}
}
for (; counter < counter_sz; ++counter) {
int i=stoi(B[counter][0],0,16);
Instruction(i);
if(Exit)
break;
}
inputFile.close();
DisplayMemory();
DisplayRegisters();
}