-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPUClasses.cpp
More file actions
255 lines (229 loc) · 7.43 KB
/
CPUClasses.cpp
File metadata and controls
255 lines (229 loc) · 7.43 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
//
// CPUClasses.cpp
// CoreWar01
//
// Created by Tim Cowley on 9/26/20.
// Copyright © 2020 Tim Cowley. All rights reserved.
//
#include "CPUClasses.hpp"
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
Instruction::Instruction(Operation a, Data arg, Data mYsecond)
{
operation = a;
argument = arg;
second = mYsecond;
}
Instruction::Instruction(Operation a, Data arg)
{
operation = a;
argument = arg;
second = arg;
// specifically not init'ing second
}
Instruction::Instruction(Operation a)
{
operation = a;
argument = Data();
}
Instruction::Instruction()
{
operation = Operation::noop;
argument = Data();
}
Program::Program()
{
text.clear();
text.resize(1);
text.emplace_back(Instruction(Operation::noop));
}
Program::Program(std::vector<Instruction> a)
{
text = a;
}
void Program::add(Instruction a)
{
text.emplace_back(a);
}
void Program::reset()
{
text.clear();
text.resize(1);
text.emplace_back(Instruction(Operation::noop));// whenever I use this I should really be making a new object
}
Process::Process(Program a)
{
this->program = a;
size = (int)program.text.size();
programCounter = 0;
os = false;
}
void Memory::set(int addr, Instruction i)
{
core[addr].operation = i.operation;
}
void Memory::set(int addr, Data a)
{
core[addr].argument = a;
core[addr].second = {0,false};
}
void Memory::set(int addr, Data a, Data b)
{
core[addr].argument = a;
core[addr].second = b;
}
std::string* Memory::display(std::vector<Process*> programCounters)
{
std::string *out = new std::string();
for(int i=0; i<Memory::memorySize; i++)
{
Instruction instruction = core[i];
for(Process *pc : programCounters)
{
if(i == pc->location + pc->programCounter)
{
out->append("PC");
continue;
}
}
if(instruction.operation == Operation::noop)out->append(".");
else if(instruction.operation == Operation::mov)out->append("M");
else if(instruction.operation == Operation::cmp)out->append("c");
else if(instruction.operation == Operation::set)out->append("e");
else if(instruction.operation == Operation::add)out->append("a");
else if(instruction.operation == Operation::sub)out->append("s");
else if(instruction.operation == Operation::mult)out->append("m");
else if(instruction.operation == Operation::branch)out->append("b");
else if(instruction.operation == Operation::branch0)out->append("0");
else if(instruction.operation == Operation::rand)out->append("r");
else if(instruction.operation == Operation::fork)out->append("f");
else out->append(".");
}
return out;
}
std::string* System::status()
{
std::string *out = new std::string();
int ID=0;
for(Process *a : processes)
{
out->append("ID[" + std::to_string(ID) + "] ");
out->append("Size[" + std::to_string(a->size) + "]");
// out->append("Location["+ std::to_string(a->size) + "]");
out->append("PC["+ std::to_string(a->programCounter) + "]");
out->append("/r/n");
}
return out;
}
// loads you somewhere in memory, returns -1 if can't fit
size_t System::load(Program *a, Memory *m)
{
// Convert this program to a running process
Process* proc = new Process(*a);
proc->size = (int)a->text.size(); // two noops
processes.emplace_back(proc); // welcome to the process table
int loadLocation = 0;
// now, place you somewhere near the bottom of memory
loadLocation = Memory::memorySize;
for(Process *p : processes)
{
loadLocation -= p->size;
}
loadLocation -= proc->size;
if(loadLocation <= 0) return -1; // if it doesn't fit, it doesn't fit.
processes.back()->location = loadLocation;
processes.back()->size = proc->size;
//Now, copy the program into its reserved space
for(int i=0; i<proc->size; i++)
{
m->set(loadLocation + i, proc->program.text[i]);
m->set(loadLocation + i, proc->program.text[i].argument, proc->program.text[i].second);
}
return proc->location;
}
void CPU::execute(Memory* m, System* os)
{
for(Process* p : os->processes)
{
//Instruction i = m->core[p->location + p->programCounter];
Operation o = m->core[p->location + p->programCounter].operation;
Data a = m->core[p->location + p->programCounter].argument;
Data b = m->core[p->location + p->programCounter].second;
//we've loaded the reference and the reality: any differences mean damage!
//if (i.operation != o || i.arguments != a)
{
// someting ... maybe
//std::cout << "Damage detected to process /r/n" << std::endl;
}
// The big switch statement you knew was coming. This could be dynamically constructed...
switch (o)
{
case Operation::fork:{
//std::cout << "fork operation" << std::endl;
os->load(&p->program, m);
//bwaha now we are LEGION
break;
}
case Operation::noop:{
//std::cout << "noop" << std::endl;
//Actually lots of stuff happens here!
break;
}
case Operation::mov:{
// we're copying some data from one location to CPU.x
// or, we're not in immediate mode and we're moving from x to core
// all relative values based off of the program location
if(a.immediate){
m->set(p->location + a.value, {x,false});
} else {
// m->set(b.value, {m->core[p->location + a.value].argument.value, false});// ? needed
m->core[p->location + p->programCounter + b.value] = m->core[p->location + p->programCounter + a.value];
}
}
break;
case Operation::set:{
if(a.immediate)
x = a.value;
else
m->core[a.value].argument.value = x;
break;}
case Operation::get:{
x = m->core[a.value].argument.value;
break;}
case Operation::branch:{
p->programCounter += a.value; // argument data is how far back to loop
break;}
case Operation::branch0:{ // conditionals
if( x == 0)
p->programCounter += a.value;
break;}
case Operation::mult:{
x *= a.value;
break;
}
case Operation::rand:{
x = Memory::memorySize & std::rand(); // random address on this system
break;
}
case Operation::add:{
x += a.value;
break;
}
case Operation::cmp:{
if (x > a.value) x = 1;
if (x == a.value) x = 0;
if (x < a.value) x = -1;
break;
}
case Operation::sub:{
x -= a.value;
break;
}
}
// Phew, okay, I lied, it wasn't that big
p->programCounter++;
if(p->programCounter > 0 && p->programCounter > p->program.text.size()){p->programCounter = 0;}
}
}