-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCPU.cpp
More file actions
111 lines (101 loc) · 2.54 KB
/
Copy pathCPU.cpp
File metadata and controls
111 lines (101 loc) · 2.54 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
#include "CPU.h"
#include <cmath>
#include <cctype>
// Helper function to convert a hexadecimal character to an integer
int hexToInt(char hex) {
if (hex >= '0' && hex <= '9') {
return hex - '0';
} else {
return tolower(hex) - 'a' + 10;
}
}
void CPU::fetch(Memory& mem) {
ir = mem.get_value(pc);
ir += mem.get_value(pc + 1);
}
vector<int> CPU::decode() {
vector<int> decoded;
for (int i = 0; i < ir.size(); ++i) {
decoded.push_back(hexToInt(ir[i]));
}
return decoded;
}
bool CPU::IsValid(const string& instruction) {
string hex_chars = "0123456789ABCDEF";
if (instruction.size() != 4) {
return false;
}
for (char c : instruction) {
if (hex_chars.find(toupper(c)) == string::npos) {
return false;
}
}
return true;
}
void CPU::runNextInstruction(Memory& mem) {
fetch(mem);
pc += 2;
if (IsValid(ir)) {
vector<int> decoded_instructions = decode();
execute(decoded_instructions,mem);
}
}
void CPU::reset() {
pc = 16;
ir = "";
reg.reset();
cu.reset();
}
void CPU::execute(const vector<int>& decoded,Memory& mem) {
int opcode = decoded[0];
int r = decoded[1], x = decoded[2], y = decoded[3];
int address = x * 16 + y;
string hex_chars = "0123456789ABCDEF";
string val = "";
val += hex_chars[x];
val += hex_chars[y];
switch (opcode) {
case 1:
cu.load(r, address, reg, mem);
break;
case 2:
cu.load(r, val, reg);
break;
case 3:
cu.store(r, address, reg, mem);
if (x == 0 && y == 0) {
cout << mem.get_value(address) << '\n';
}
break;
case 4:
cu.mov(y, x, reg);
break;
case 5:
alu.twosComp(x, y, r, reg);
break;
case 6:
alu.add(x, y, r, reg);
break;
case 7:
alu.bitwiseOr(x,y,r,reg);
break;
case 8:
alu.bitwiseAnd(x,y,r,reg);
break;
case 9:
alu.bitwiseXor(x,y,r,reg);
break;
case 10:
alu.rotateRight(r,x,y,reg);
break;
case 11:
cu.jump(r, address, reg, pc);
break;
case 12:
cu.halt();
break;
}
}
void CPU::set_pc(int initialValue){
this->pc = initialValue;
}