-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (66 loc) · 2.26 KB
/
main.py
File metadata and controls
80 lines (66 loc) · 2.26 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
import os
from Compiler.Compiler import Compiler
from CPU.Processor import Processor
import CPU.DirectionPredictors
import matplotlib.pyplot as plt
C = Compiler()
CURRENT_PATH = os.curdir
ALGORITHMS_PATH = os.path.join(CURRENT_PATH, "Algorithms")
## Allow user to select algorithm
# Display files in algorithms folder
AvailableAlgorithms = os.listdir(ALGORITHMS_PATH)
print("Select which algorithm you would like to run: ")
for index, alg in enumerate(AvailableAlgorithms):
print(f"{index}. {alg.removesuffix(".txt")}")
SelectedAlgorithm = AvailableAlgorithms[int(input())]
# Open correct file and compile it
with open(os.path.join(ALGORITHMS_PATH, f"{SelectedAlgorithm}"), 'r') as f:
print("Compiling...")
executable = C.Compile(f)
print("Compiled to executable!")
## Allow user to select branch predictor to run
while True:
print("Select Branch Predictor to run: ")
print("""0. Always Not Taken
1. Always Taken
2. One-Bit Last Time
3. Two-Bit Last Time
4. gshare""")
match input():
case '0':
predictor = CPU.DirectionPredictors.AlwaysNotTaken
break
case '1':
predictor = CPU.DirectionPredictors.AlwaysTaken
break
case '2':
predictor = CPU.DirectionPredictors.OneBitLastTime
break
case '3':
predictor = CPU.DirectionPredictors.TwoBitLastTime
break
case '4':
predictor = CPU.DirectionPredictors.gshare
break
case _:
continue
P = Processor(predictor)
## Run on processor
debug = None
while type(debug) != bool:
debug = input("Run in debug mode? (y/n): ").upper()
if debug == "Y":
debug = True
elif debug == "N":
debug = False
predictionResults = P.Compute(executable, debug)
## Display graph of mispredictions/cycles
predictedY = [i for i in range(1, len(predictionResults["Predicted"]) + 1)]
mispredictedY = [i for i in range(1, len(predictionResults["Mispredicted"]) + 1)]
plt.title(SelectedAlgorithm)
plt.xlabel("Cycles")
plt.ylabel("Total")
plt.plot(predictionResults["Mispredicted"], mispredictedY, "o-", color="blue", label="Mispredicted")
plt.plot(predictionResults["Predicted"], predictedY, "o-", color="green", label="Predicted")
plt.legend()
plt.show()