-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.cpp
234 lines (193 loc) · 3.99 KB
/
CPU.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
#include "CPU.h"
#include <iostream>
const word OPCODE_MASK = 0b111100000000000000;
const word INDIR_MASK = 0b000010000000000000;
const word ZERO_MASK = 0b000001000000000000;
const word REG_MASK = 0b000000110000000000;
const word OPERAND_MASK = 0b000000001111111111;
const word EIGHTEEN_BIT_MASK = 0b111111111111111111;
const word PC_HIGH_BIT_MASK = 0b111100000000000000;
//const word MAX_VAL = 262143; // largest 18-bit number
void CPU::init()
{
//Bleep bloop
ACC = 0x00000;
PC = 0x00000;
SP = 0x3FFFF;
reg[0] = 0x00000;
reg[1] = 0x00000;
reg[2] = 0x00000;
for(int i = 0; i < MAX_VAL; i++)
{
//It gets weird if you don't
ram[i] = 0x00000;
}
}
void CPU::setMemory(word address, word data)
{
if(address > MAX_VAL)
{
//Logic for Memory mapped I/O here
printf("MMIO Write Call");
}
else
{
ram[address] = data;
}
}
word CPU::getMemory(word address)
{
if(address > MAX_VAL)
{
//Logic for MMIO here
printf("MMIO Read Call at: %i", address);
return 0x00000; // dummy logic best logic
}
else
{
return ram[address];
}
}
void CPU::dumpMem(int start, int end)
{
std::cout << "=== CPU Memory Dump ===" << std::endl;
for(int i = start; i < end; i++)
{
std::cout << std::bitset<18>(getMemory(i)) << "\n";
}
}
void CPU::printCPU()
{
std::cout << "=== CPU State ===\n"
<< "PC: " << std::bitset<18>(PC) << "\n"
<< "ACC: " << std::bitset<18>(ACC) << "\n"
<< "curword: " << std::bitset<18>(getMemory(PC)) << "\n"
<< "GP0: " << std::bitset<18>(reg[0]) << "\n"
<< "GP1: " << std::bitset<18>(reg[1]) << "\n"
<< "GP2: " << std::bitset<18>(reg[2]) << "\n"
<< std::endl;
}
void CPU::cycle()
{
word curWord = getMemory(PC);
byte opCode = (OPCODE_MASK & curWord) >> 14;
bool indirect_bit = (INDIR_MASK & curWord) >> 13;
bool zero_bit = (ZERO_MASK & curWord) >> 12;
byte regsel = (REG_MASK & curWord) >> 10;
short arg = (OPERAND_MASK & curWord);
/* std::cout << "=== Internal State ===" << "\n"
<< "opCode: " << std::bitset<4>(opCode) << "\n"
<< "indir: " << std::bitset<1>(indirect_bit) << "\n"
<< "zero: " << std::bitset<1>(zero_bit) << "\n"
<< "reg: " << std::bitset<4>(regsel) << "\n"
<< "arg: " << std::bitset<10>(arg) << "\n"
<< std::endl; */
if(indirect_bit)
{
if(zero_bit)
{
//create new address from arg and zeroes for the 12 extra bits
arg = getMemory(getMemory(arg));
}
else
{
//create new address from arg and 5 higher order bits of PC
arg = getMemory(getMemory((PC_HIGH_BIT_MASK & PC) + arg));
}
}
else
{
arg = getMemory(arg);
}
switch(opCode)
{
case 0: //NOP
PC++;
break;
case 1: //DAM R A
setMemory(arg, ACC);
ACC = 0;
PC++;
break;
case 2: //LDR R A
reg[regsel-1] = arg;
PC++;
break;
case 3: //DPR R A
setMemory(arg, reg[regsel-1]);
PC++;
break;
case 4: //JMP R A
if(regsel != 0)
reg[regsel] = PC;
PC = arg;
break;
case 5: //JEZ R A
if(ACC == 0)
{
if(regsel != 0)
reg[regsel] = PC;
PC = arg;
}
else
PC++;
break;
case 6: //JNZ R A
if(ACC != 0)
{
if(regsel != 0)
reg[regsel] = PC;
PC = arg;
}
else
PC++;
break;
case 7: //SHL A
ACC = arg << 1;
PC++;
break;
case 8: //SHR A
ACC = arg >> 1;
PC++;
break;
case 9: //AND A
ACC &= arg;
PC++;
break;
case 10: //OR A
ACC |= arg;
PC++;
break;
case 11: //ADD R A
ACC += arg;
if(regsel != 0)
ACC += reg[regsel];
PC++;
break;
case 12: //SUB R A
ACC -= arg;
if(regsel != 0)
ACC -= reg[regsel];
PC++;
break;
case 13: //POP A
setMemory(arg, getMemory(SP));
SP--;
PC++;
break;
case 14: //PSH A
setMemory(getMemory(SP), arg);
SP++;
PC++;
break;
//Don't know what to do here
default:
//Lalalalalalala
break;
}
//Don't overflow past 18 bits
if(PC >= MAX_VAL)
{
PC = 0x00000;
}
}