-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCU.cpp
More file actions
251 lines (194 loc) · 6.79 KB
/
Copy pathCU.cpp
File metadata and controls
251 lines (194 loc) · 6.79 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
#include <iostream>
#include <string>
#include <array>
#include <cctype>
#include <sstream>
using namespace std;
class Memory {
public:
static const int size = 256;
array<string, size> mem;
Memory() {
for (int i = 0; i < size; i++) {
mem[i] = "xy"; // Initialize memory cells to "xy"
}
}
string getcell(int address) {
if (address < 0 || address >= size) { // Corrected bounds check
cout << "Out of bounds. ";
return "00"; // Return a default value for out of bounds
} else {
return mem[address];
}
}
void setcell(int address, const string& value) {
bool flag = true; // Reset flag for each call
// Validate hex input
for (char c : value) {
if (!isxdigit(c)) { // Check if character is not a valid hex digit
flag = false;
break;
}
}
// Store valid two-character hex value
if (value.size() == 2 && flag) {
if (address >= 0 && address < size) {
mem[address] = value;
} else {
cout << "Address out of bounds." << endl;
}
return;
}
// Handle values longer than 2 characters
if (value.length() > 2) {
for (size_t i = 0; i < value.length(); i += 2) {
if (i + 1 < value.length()) {
string pair = value.substr(i, 2);
if (address >= 0 && address < size) {
mem[address++] = pair; // Store the pair in memory
} else {
cout << "Out of bounds for address." << endl;
return;
}
}
}
// Handle any remaining single character
if (value.length() % 2 != 0 && address < size) {
string remaining(1, value.back());
mem[address++] = remaining; // Store remaining character
}
} else if (!flag) {
cout << "Invalid hex input." << endl;
}
}
};
class Register {
public:
static const int size = 16; // Size of the register
array<string, size> memory; // Array to hold register values as strings
Register() {
for (int i = 0; i < size; i++) {
memory[i] = "00"; // Initialize memory to "00"
}
}
string getcell(int address) {
if (address < 0 || address >= size) { // Corrected range check
cout << "Out of bounds. " << endl;
return "00"; // Return a default value for out of bounds
} else {
return memory[address];
}
}
void setcell(int address, const string &value) {
// Validate hex input
bool flag = true;
for (char c : value) {
if (!isxdigit(c)) { // Check if character is not a valid hex digit
flag = false;
break;
}
}
if (!flag) {
cout << "Invalid hex input." << endl;
return;
}
// Handle valid input
if (value.length() > 2) {
// Process longer values in pairs
for (size_t i = 0; i < value.length(); i += 2) {
if (i + 1 < value.length()) {
string pair = value.substr(i, 2);
if (address >= 0 && address < size) {
memory[address++] = pair; // Store the pair as a string
} else {
cout << "Out of bounds for address." << endl;
return;
}
}
}
// Handle any remaining single character
if (value.length() % 2 != 0 && address < size) {
string remaining(1, value.back());
memory[address++] = remaining; // Store remaining character as a string
}
} else if (value.size() == 2) {
// Store valid two-character hex value
if (address >= 0 && address < size) {
memory[address] = value; // Store as string
} else {
cout << "Address out of bounds." << endl;
}
}
}
};
class CU{
public:
void load(int Regp, int Memp, Register& reg, Memory& mem){
reg.setcell(Regp,mem.getcell(Memp));
}
void store(int Regp, int Memp, Register& r, Memory& m) {
// Retrieve the value from the register and store it in the memory address specified by Memp
if (Memp == 0) {
// If Memp is 0, store register value at memory address 0
cout << r.getcell(Regp) << endl;
m.setcell(0, r.getcell(Regp));
}
else {
// Otherwise, store the register value at the specified memory address Memp
m.setcell(Memp, r.getcell(Regp));
}
}
void move (int Regp1, int Regp2, Register & reg){
reg.setcell(Regp2, reg.getcell(Regp1));
}
bool isHalted = false; // Flag to indicate if execution is halted
void halt() {
cout << "Execution halted." << endl;
isHalted = true ;
return;
}
void jump(int regIndex, int memAddress, Register& reg, int& PC){
if (reg.getcell(regIndex) == reg.getcell(0)) {
PC = memAddress;
}
else if (reg.getcell(regIndex) != reg.getcell(0)){
PC+=2;
}
}
void load(int regIndex, int value, Register& reg) {
// Convert the integer value to a two-character hex string
stringstream s;
s << hex << value;
string hexValue;
hexValue = s.str();
// Store this hex string in the specified register
reg.setcell(regIndex, hexValue);
}
};
int main() {
Memory memory;
Register reg;
CU cu;
int PC = 0;
memory.setcell(10, "1A");
cu.load(2, 10, reg, memory);
cout << "Register[2] after load from memory: " << reg.getcell(2) << endl;
cu.load(3, 27, reg);
cout << "Register[3] after immediate load: " << reg.getcell(3) << endl;
reg.setcell(5, "4C");
cu.store(5, 15, reg, memory);
cout << "Memory[15] after store from register: " << memory.getcell(15) << endl;
reg.setcell(5, "4C");
cu.store(5, 0, reg, memory);
cout << "Memory[0] after store from register: " << memory.getcell(0) << endl;
reg.setcell(7, "5D");
cu.move(7, 8, reg);
cout << "Register[8] after move from Register[7]: " << reg.getcell(8) << endl;
reg.setcell(0, "00");
reg.setcell(4, "00");
cu.jump(4, 20, reg, PC);
cout << "Program Counter after jump: " << PC << endl;
cu.halt();
cout << "Execution status (isHalted): " << (cu.isHalted ? "Yes" : "No") << endl;
return 0;
}