-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReOrderBuffer.cs
More file actions
354 lines (347 loc) · 15.8 KB
/
Copy pathReOrderBuffer.cs
File metadata and controls
354 lines (347 loc) · 15.8 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Collections.Generic;
using static MyProcessor.Processor;
using command = MyProcessor.command;
static class ReOrderBuffer
{
#region static public Vars
static public List<command> contenseOfReOrderBuffer, speculativeCommands, predictions;
static private List<dTracker> DependencyTracker;
static public int numberOfCommandsSentBack, ShadowProgramCounter = 0;
static private string HistoryInput, HistoryOutput;
struct dTracker
{
public string registerName;
public int cycleLastEditedIn;
}
#endregion
//Called at start to make the RoB
static public void makeReOrderBuffer()
{
contenseOfReOrderBuffer = new List<command>(new command[SizeOfReOrderBuffer]);
DependencyTracker = new List<dTracker>();
speculativeCommands = new List<command>();
predictions = new List<command>();
HistoryOutput = "";
HistoryInput = "";
}
//We are going to check to see if we should commit or we should keep in the reorder buffer
static public void addCommand(command newCommand)
{
//If speculative command then we add to a list
//0 means it's not speculation
if (newCommand.specBranch != 0)
{
foreach (command cmd in speculativeCommands)
{
//Console.WriteLine($"{newCommand.opCode}");
if (cmd.assemblyCode == newCommand.assemblyCode) return;
}
speculativeCommands.Add(newCommand);
BranchPrediction.BranchDebug($"Added {newCommand.opCode} to spec branch in RoB");
return;
}
//DebugLog($"Recieved {newCommand.opCode} at {newCommand.PC} with shadow:{ShadowProgramCounter}");
//No Duplicates (command has program counter)
//Console.WriteLine(newCommand.assemblyCode);
foreach (command comm in contenseOfReOrderBuffer)
{
if (comm.Equals(new command { })) break;
else if (comm.assemblyCode == newCommand.assemblyCode) return;
}
//If we load in an old command then ignore it
if(newCommand.PC < ShadowProgramCounter) {
//Console.WriteLine($"old commands sent to ROB");
return;
}
DebugLog($"Added {newCommand.assemblyCode} at {newCommand.PC} with shadow:{ShadowProgramCounter}");
//DebugLog($"Recieved command {newCommand.opCode}");
HistoryInput = HistoryInput + newCommand.opCode + " | ";
//Shadow PC makes sure we keep order after OoO excution
if (ShadowProgramCounter == newCommand.PC)
{
DebugLog($"{newCommand.opCode} straight in {newCommand.PC}");
//Send to commit unit
Commit(ref newCommand);
}
else
{
//Add to list in correct place
for (int i = 0; i < SizeOfReOrderBuffer; i++)
{
//if empty place or issued earlier
if (contenseOfReOrderBuffer[i].Equals(new command { }) || newCommand.PC < contenseOfReOrderBuffer[i].PC)
{
//Check to see if the buffer is full
if (!contenseOfReOrderBuffer[SizeOfReOrderBuffer - 1].Equals(new command { }))
{
//Send command back
SendCommandBack(ref newCommand);
//Console.WriteLine("REORDER BUFFER IS TOO FULL FOR THE NEW COMMAND");
}
//Check to see if there are true dependencies
//Check that r1 and r2 from new command aren't changed by commands above
for (int a = 0; a < i; a++)
{
if (newCommand.dependencies.Contains(contenseOfReOrderBuffer[a].destination))
{
DebugLog($"{contenseOfReOrderBuffer[a].opCode} stopped {newCommand.opCode} due to dependency");
//send new Command back
SendCommandBack(ref newCommand);
return;
}
}
//Check to see if there are true dependencies
//Check all commands below aren't changed by this new destination
for (int x = SizeOfReOrderBuffer - 1; x > i; x--)
{
//Check to see if there are commands below (stop when we hit a new command)
if (contenseOfReOrderBuffer[x].Equals(new command { })) break;
if (contenseOfReOrderBuffer[x].dependencies.Contains(newCommand.destination))
{
DebugLog($"{newCommand.opCode} removed {contenseOfReOrderBuffer[x].opCode} due to dependency");
//We need to recalulate this command
SendCommandBack(ref newCommand);
contenseOfReOrderBuffer.Remove(contenseOfReOrderBuffer[x]);
}
}
DebugLog($"Put command in to reorder buffer {newCommand.opCode} because Last PC is {ShadowProgramCounter} and issued is {newCommand.PC}");
contenseOfReOrderBuffer.Insert(i, newCommand);
break;
}
}
}
}
//We actually write back to memory (the end of the command)
static public void Commit(ref command Command)
{
if (DetectDependency(ref Command) == true)
{
SendCommandBack(ref Command);
return;
}
if (Command.destination.Contains("r") == true && DependencyTracker.Count != 0)
{
for (int i = 0; i < DependencyTracker.Count; i++)
{
if (DependencyTracker[i].registerName == Command.destination)
{
if (DependencyTracker[i].cycleLastEditedIn < Command.cycleCalculatedIn) DependencyTracker.Remove(DependencyTracker[i]);
else
{
SendCommandBack(ref Command);
return;
}
}
}
}
if (Command.destination.Contains("r") == true)
{
DependencyTracker.Add(new dTracker
{
registerName = Command.destination,
cycleLastEditedIn = Command.cycleCalculatedIn
});
}
HistoryOutput = HistoryOutput + Command.opCode + " | ";
//REGISTER COMMANDS
if (Command.opCode == "LDC")
{
Memory.PutValueInRegister(Command.destination, Command.value1);
DebugLogOutput($"Commited Load {Command.value1} to {Command.destination}");
}
else if (Command.opCode == "LD")
{
Memory.PutValueInRegister(Command.destination, Memory.GetValueFromRegister($"r{Memory.GetValueFromRegister(Command.valueString1)}"));
DebugLogOutput($"Commited Load with offset so {Command.destination} has {Memory.GetValueFromRegister(Command.destination)}");
}
else if (Command.opCode == "STR")
{
//STR this.value this.value.indexedRegister
//Confusing as valueString2 relates to r1 and valueString1 relates to r2
string index = $"r{Memory.GetValueFromRegister(Command.valueString1)}";
int valueToBeInserted = Memory.GetValueFromRegister(Command.valueString2);
Memory.PutValueInRegister(index, valueToBeInserted);
DebugLogOutput($"Commited store {valueToBeInserted} to {index}");
}
//BRANCH COMMANDS result:1 => take it || result:0 => Dont take it
else if (Command.opCode == "BEQ" || Command.opCode == "BNE")
{
foreach(command cmd in predictions){
if(cmd.assemblyCode == Command.assemblyCode) {
if(cmd.result == Command.result) BranchPrediction.correctGuesses++;
else BranchPrediction.incorrectGuesses++;
predictions.Remove(cmd);
break;
}
}
DebugLogOutput($"Commited {Command.opCode} with {Command.result}");
if (Command.result == 1)
{
BranchPrediction.PredictionResult(true);
if (speculativeCommands.Count != 0)
{
//Check to see if this is the branch predicted
if (speculativeCommands[0].assemblyCode == Command.assemblyCode)
{
BranchPrediction.BranchDebug($"Speculative branch is being check against the real excution");
if (speculativeCommands[0].result == Command.result)
{
//Send back correct result
/*
BranchPrediction.correctGuesses++;
*/
BranchPrediction.BranchDebug($"Branch Correct and being committed with {speculativeCommands.Count}");
AddCommandsToRoBWhenSpecBranchIsCorrect();
}
else
{
//Send back incorrect result
/*
BranchPrediction.incorrectGuesses++;
*/
BranchPrediction.BranchDebug($"Branch InCorrect and being destroyed");
}
CleanUpSpeculativeCommands();
}
}
//-1 Shadow counter because we add one to it at the end anyway
ProgramCounter = Int32.Parse(Command.destination);
ShadowProgramCounter = ProgramCounter - 1;
DebugLogOutput($"Commited new pc {ProgramCounter}");
//So we get rid off all commands in the reorder buffer as they're now redudent
contenseOfReOrderBuffer = new List<command>(new command[SizeOfReOrderBuffer]);
DependencyTracker = new List<dTracker>();
}
else BranchPrediction.PredictionResult(false);
}
else if (Command.opCode == "JUMP")
{
//-1 Shadow counter because we add one to it at the end anyway
ProgramCounter = Int32.Parse(Command.destination);
ShadowProgramCounter = ProgramCounter - 1;
DebugLogOutput($"Commited new jump {ProgramCounter}");
//So we get rid off all commands in the reorder buffer as they're now redudent
contenseOfReOrderBuffer = new List<command>(new command[SizeOfReOrderBuffer]);
DependencyTracker = new List<dTracker>();
}
//ARTHEMETRIC
else if (Command.opCode == "ADDI" || Command.opCode == "ADD" || Command.opCode == "SUB" ||
Command.opCode == "COMP" || Command.opCode == "MUL" || Command.opCode == "DIV")
{
Memory.PutValueInRegister(Command.destination, Command.result);
DebugLogOutput($"Commited ALU {Command.result} to {Command.destination} OPCODE: {Command.opCode}");
}
//NOP
else if (Command.opCode == "NOP")
{
DebugLogOutput($"Commited NOP");
Console.WriteLine("STOP!");
runProcessor = false;
}
else Console.WriteLine($"COMMIT GOT UNRECOGNISED OPCODE {Command.opCode}");
//Increase LastProgramCounterExcuted because we have committed again
ShadowProgramCounter++;
//Run to see if we can commit again!
for (int i = 0; i < SizeOfReOrderBuffer; i++)
{
//Check to see if the rest of the buffer is empty
if (contenseOfReOrderBuffer[i].Equals(new command { })) return;
else if (contenseOfReOrderBuffer[i].PC == ShadowProgramCounter)
{
//DebugLogOutput($"Command in reorder buffer is now commitable {contenseOfReOrderBuffer[0].opCode}");
command newCommand = contenseOfReOrderBuffer[i];
PopFromReOrderBuffer();
Commit(ref newCommand);
return;
}
}
}
//Detect dependency returns true for dependency (spend back) and false for no dependency
static bool DetectDependency(ref command Command)
{
//int to count number of dependencies detected
int dependencyDetected = 0;
//If it has no dependencies then it's not gonna detect anything
if (Command.dependencies == new List<string>()) return false;
//Putting values into holders so that we dont cause index range exceptions
string dependencyOne = "";
if (Command.dependencies.Count > 0) dependencyOne = Command.dependencies[0];
string dependencyTwo = "";
if (Command.dependencies.Count > 1) dependencyTwo = Command.dependencies[1].Remove(0, 1);
foreach (dTracker dTrack in DependencyTracker)
{
if (dTrack.registerName.Contains(dependencyOne) == true || dTrack.registerName.Contains(dependencyTwo) == true)
{
//Console.WriteLine($"Actual check on {Command.opCode} with {Command.cycleCalculatedIn} comp to dTrack{dTrack.cycleLastEditedIn}");
if (!(Command.cycleCalculatedIn > dTrack.cycleLastEditedIn))
{
dependencyDetected++;
}
}
}
if (dependencyDetected == 0) return false;
else return true;
}
//Detecting a true dependency we send it back to be recalucated
static void SendCommandBack(ref command Command)
{
numberOfCommandsSentBack++;
ExcutionUnits.AssignToExcutionUnit(Command);
}
static void PopFromReOrderBuffer()
{
if (contenseOfReOrderBuffer[0].Equals(new command { })) return;
for (int i = 0; i < SizeOfReOrderBuffer - 1; i++)
{
contenseOfReOrderBuffer[i] = contenseOfReOrderBuffer[i + 1];
}
contenseOfReOrderBuffer[SizeOfReOrderBuffer - 1] = new command { };
}
//called once speculative branch is checked
//Removes all commands until it finds a branch command
static void CleanUpSpeculativeCommands()
{
foreach (command cmd in speculativeCommands)
{
if (cmd.opCode == "BEQ" || cmd.opCode == "BNE") return;
speculativeCommands.Remove(cmd);
}
}
//Called when speculative branch is correct so we need to add the commands to the RoB
static void AddCommandsToRoBWhenSpecBranchIsCorrect()
{
//We know that the specBranch branch command is the same as the real one so we delete it!
speculativeCommands.RemoveAt(0);
foreach (command cmd in speculativeCommands)
{
if (cmd.opCode == "BEQ" || cmd.opCode == "BNE") return;
BranchPrediction.BranchDebug($"Command {cmd.opCode} is being added to RoB by branch predictor");
command acceptedSpecBranch = cmd;
acceptedSpecBranch.specBranch = 0;
addCommand(acceptedSpecBranch);
}
}
static public void SendPrediction(command prediction){
predictions.Add(prediction);
}
#region Debugging
static private void DebugLog(string debugPrint)
{
if (ReOrderBufferDebug == true) Console.WriteLine(debugPrint);
}
static private void DebugLogOutput(string debugPrint)
{
if (ReOrderBufferDebugOutput == true) Console.WriteLine(debugPrint);
}
static public void PrintOutReOrderBufferHistory()
{
Console.WriteLine("---------------- ReOrder Buffer History ----------------");
//Console.Write("--- ReOrder Buffer Input History");
//Console.WriteLine($"{HistoryInput}");
Console.Write("--- ReOrder Buffer Output History");
Console.WriteLine($"{HistoryOutput}");
Console.WriteLine($"Number of commands sent back {numberOfCommandsSentBack}");
}
#endregion
}