-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlUnit.h
291 lines (235 loc) · 8.02 KB
/
ControlUnit.h
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#pragma once
#include"Module.h"
#include"Tech.h"
#include"Skill.h"
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<memory>
namespace Design
{
// controls other modules
// uses several algorithms to evade dead-lock
// 1) Banker's Algorithm to limit the resource usage per instruction such as ALU,FPU,RAM
// 2) Round-Robin scheduling (ordering on resources) to add fairness on both resource usage and data-path usage
// 3) Re-routing of bus data when a resource input is clogged for a long time (this is implicit to hide some realism for more fun)
// players can still manage to get a dead-lock buy combining multiple control units and shared resources
class ControlUnit :public Module
{
public:
ControlUnit(int frequency, int lithography, int parallelism) :
Module(parallelism, parallelism, lithography,/*numTransistors*/ 1, ModuleType::CONTROL_UNIT,
/* thermalDissipationPower */ 1, frequency, /* failProbability*/ 0.0f)
{
_resCtr = 0;
_roundRobinIncoming = 0;
// round-robin (1 = always shortest)
_resourceSchedulingType = 0;
}
static std::shared_ptr<Module> Create(int frequency, int lithography, int parallelism)
{
return std::make_shared<ControlUnit>(frequency, lithography, parallelism);
}
// Cpu object calls this only when output is free
// also it does only single work (unless upgraded with a skill level)
void Compute(int clockCycleId) override
{
SetIdle();
for (int i = 0; i < _parallelism; i++)
{
if (GetOutput(i).GetDataType() != Design::DataType::Null)
continue;
// list of resources found (resource module ID to use) & their bus paths
std::map<int, std::map<int, bool>> validOutputResourceBus;
_roundRobinIncoming++;
std::vector<int> works;
for (int j = 0; j < 4; j++)
{
if (_input[j][i].GetDataType() != Design::DataType::Null)
{
works.push_back(j);
}
}
if (works.size() == 0)
continue;
int selectedWork = _roundRobinIncoming % works.size();
auto opcode = _input[works[selectedWork]][i];
bool computed = false;
if (opcode.GetDataType() == Design::DataType::MicroOpAlu)
{
SetBusy();
SetOutput(Design::Data(opcode.GetDataType(), Design::ModuleType::ALU, -1 /* filled when output is sent*/, -1, Design::ModuleType::CONTROL_UNIT, _id, clockCycleId), i);
computed = true;
}
if (opcode.GetDataType() == Design::DataType::MicroOpFpu)
{
SetBusy();
SetOutput(Design::Data(opcode.GetDataType(), Design::ModuleType::FPU, -1 /* filled when output is sent*/, -1, Design::ModuleType::CONTROL_UNIT, _id, clockCycleId), i);
computed = true;
}
if (opcode.GetDataType() == Design::DataType::MicroOpDecode)
{
if (_numStartedOperations < _numCompletedOperations + 10)
{
SetBusy();
SetOutput(Design::Data(opcode.GetDataType(), Design::ModuleType::DECODER, -1 /* filled when output is sent*/, -1, Design::ModuleType::CONTROL_UNIT, _id, clockCycleId), i);
computed = true;
_numStartedOperations++;
}
}
if (opcode.GetDataType() == Design::DataType::MicroOpMemRead)
{
SetBusy();
// todo: set output to cache control only if there is a cache connected. or just use ANY
SetOutput(Design::Data(opcode.GetDataType(), Design::ModuleType::CACHE_CONTROL, -1 /* filled when output is sent*/, -1, Design::ModuleType::CONTROL_UNIT, _id, clockCycleId), i);
computed = true;
}
if (opcode.GetDataType() == Design::DataType::Result)
{
if (opcode.GetValue() == Design::ModuleType::ALU)
{
SetOutput(Design::Data(Design::DataType::MicroOpAlu, Design::ModuleType::ALU, -1 /* filled when output is sent*/, -1, Design::ModuleType::CONTROL_UNIT, _id, clockCycleId), i);
SetBusy();
// todo: merge current operation to architectural state
computed = true;
}
if (opcode.GetValue() == Design::ModuleType::FPU)
{
SetOutput(Design::Data(Design::DataType::MicroOpFpu, Design::ModuleType::FPU, -1 /* filled when output is sent*/, -1, Design::ModuleType::CONTROL_UNIT, _id, clockCycleId), i);
SetBusy();
// todo: merge current operation to architectural state
computed = true;
}
if (opcode.GetValue() == -1)
{
SetBusy();
_numCompletedOperations++;
// todo: merge current operation to architectural state
computed = true;
}
}
if (computed)
_input[works[selectedWork]][i] = Data();
}
}
void SendOutput() override
{
for (int i = 0; i < _parallelism; i++)
{
if (GetOutput(i).GetDataType() != Design::DataType::Null)
{
// resource -> bus , jumps
std::map<int, std::map<int, int>> validOutputResourceBus;
// check bus connections for target module
for (int j = 0; j < 4; j++)
{
auto dConn = _directConnectedModules[j];
if (dConn.get())
{
if (dConn->GetModuleType() == Design::ModuleType::BUS)
{
auto alus = dConn->AsPtr<Design::Bus>()->GetFarConnectionsOfType(GetOutput(i).GetTargetModuleType());
for (auto& a : alus)
{
validOutputResourceBus[a.moduleId][j] = a.jumps;
}
}
}
}
// Round-Robin
if (_resourceSchedulingType == 0)
{
bool sent = false;
int resCtr = 0;
for (auto& resource : validOutputResourceBus)
{
if (!sent)
{
int busCtr = 0;
if (resCtr == _resCtr)
{
for (auto& bus : resource.second)
{
if (!sent && busCtr++ == _busCtr[resource.first])
{
for (int channel = 0; channel < _directConnectedModules[bus.first]->GetParallelism(); channel++)
{
if (_directConnectedModules[bus.first]->GetInput(bus.first, /*i*/ channel).GetDataType() == Design::DataType::Null)
{
sent = true;
auto dataToSend = GetOutput(i);
dataToSend.SetTargetModuleId(resource.first);
_directConnectedModules[bus.first]->SetInput(dataToSend, bus.first, /*i*/ channel);
SetOutput(Data(), i);
_busCtr[resource.first]++;
if (_busCtr[resource.first] >= validOutputResourceBus[resource.first].size())
{
_busCtr[resource.first] = 0;
}
break;
}
}
}
}
_resCtr++;
if (_resCtr >= validOutputResourceBus.size())
{
_resCtr = 0;
}
}
resCtr++;
}
}
}
else if (_resourceSchedulingType == 1) // shortest path (both resource & bus)
{
int distance = 1000000;
int selectedBus = -1;
int selectedResource = -1;
for (auto& resource : validOutputResourceBus)
{
for (auto& bus : resource.second)
{
if (distance > bus.second)
{
distance = bus.second;
selectedBus = bus.first;
selectedResource = resource.first;
}
}
}
for (int channel = 0; channel < _directConnectedModules[selectedBus]->GetParallelism(); channel++)
{
if (_directConnectedModules[selectedBus]->GetInput(selectedBus, /*i*/ channel).GetDataType() == Design::DataType::Null)
{
auto dataToSend = GetOutput(i);
dataToSend.SetTargetModuleId(selectedResource);
_directConnectedModules[selectedBus]->SetInput(dataToSend, selectedBus, /*i*/ channel);
SetOutput(Data(), i);
break;
}
}
}
}
}
}
void SetNearestResourceScheduling()
{
_resourceSchedulingType = 1;
}
void SetRoundRobinScheduling()
{
_resourceSchedulingType = 0;
}
private:
// 0: Round-Robing scheduling for resources & bus connections
int _resourceSchedulingType;
// to select resource & bus fairly against deadlock
int _resCtr;
std::map<int,int> _busCtr;
// to select an input fairly against starvation
int _roundRobinIncoming;
std::queue<Design::Data> _opcode;
};
}