-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchPrediction.cs
More file actions
126 lines (126 loc) · 4.11 KB
/
Copy pathBranchPrediction.cs
File metadata and controls
126 lines (126 loc) · 4.11 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
using System;
using System.Collections.Generic;
using static MyProcessor.Processor;
using command = MyProcessor.command;
static class BranchPrediction
{
//Type of branch predictor determines how we will assign a result
//Type 0 (fixed: take)
//Type 1 (fixed: dont take)
//Type 2 (static: if forward take)
//Type 3 (static: if backward take)
//Type 4 (one state)
//Type 5 (two state)
static int typeOfBranchPrediction = 5;
public static bool oneState = true;
//0 strongly not take //1 weakly not taken //2 weakly taken //3 strongly taken
public static int twoState = 0;
public static int correctGuesses = 0;
public static int incorrectGuesses = 0;
public static void SendBranchToPrediction(string instruction, int pc)
{
string assemblyCode = instruction;
string opCodeBranch = getNextPartFromText(instruction);
instruction = instruction.Remove(0, opCodeBranch.Length + 1);
string destination = getNextPartFromText(instruction);
//We dont need to track dependencies as we will only commit this when we have the real version in and it's result is the same as ours
//We also dont need to track stuff like pc
//This command is more a storage for result
command branchCommand = new command
{
assemblyCode = assemblyCode,
opCode = opCodeBranch,
destination = destination,
dependencies = new List<string>(),
PC = pc,
cycleCalculatedIn = 0,
specBranch = 1,
result = 0
};
branchCommand.specBranch = 1;
if (typeOfBranchPrediction == 0)
{
//Fixed Take
branchCommand.result = 0;
}
else if (typeOfBranchPrediction == 1)
{
//Fixed Not Take
branchCommand.result = 1;
}
else if (typeOfBranchPrediction == 2)
{
//Type 2 (static: if forward take)
//if destination is bigger then it's forward
if (Int32.Parse(branchCommand.destination) > ProgramCounter)
{
branchCommand.result = 1;
}
else branchCommand.result = 0;
}
else if (typeOfBranchPrediction == 3)
{
//Type 3 (stastic: if backward take)
//if destination is smaller then it's backward
if (Int32.Parse(branchCommand.destination) < ProgramCounter)
{
branchCommand.result = 1;
}
else branchCommand.result = 0;
}
else if (typeOfBranchPrediction == 4)
{
//Type 4 (one state)
if (oneState == true)
{
branchCommand.result = 1;
}
else branchCommand.result = 0;
}
else if (typeOfBranchPrediction == 5)
{
//Type 5 (two state)
if (twoState == 0 || twoState == 1)
{
branchCommand.result = 0;
}
else branchCommand.result = 1;
}
ReOrderBuffer.addCommand(branchCommand);
ReOrderBuffer.SendPrediction(branchCommand);
//Get pipe to fetch these commands when they are'ny busy with none speculative commands
if (branchCommand.result == 1)
{
Pipe.LoadSpeculativeCommands(Int32.Parse(branchCommand.destination));
} else Pipe.LoadSpeculativeCommands(branchCommand.PC);
}
public static void PredictionResult(bool taken)
{
if (typeOfBranchPrediction == 4)
{
if (taken == true)
{
oneState = true;
}
else
{
oneState = false;
}
}
else if (typeOfBranchPrediction == 5)
{
if (taken == true)
{
if (twoState != 3) twoState++;
}
else
{
if (twoState != 0) twoState--;
}
}
}
public static void BranchDebug(string debug)
{
if (BranchPredictorDebug == true) Console.WriteLine(debug);
}
}