Commit 54b91788 authored by Christina Yu's avatar Christina Yu
Browse files

lab7 & final project

parent 624cfefa
import math
class PHT:
def __init__(self, bits):
self.pht = {}
self.bits = bits
self.size = 2 ** bits
self.history = (1 << self.bits) - 1
self.predCorrect = 0
self.inpredCorrect = 0
self.phtInit()
def phtInit(self):
for k in range(self.size):
self.pht[k] = 1
def update(self, pc, taken):
if self.pht[self.history] == 1:
if taken:
self.predCorrect += 1
else:
self.inpredCorrect += 1
self.pht[self.history] -= 1
else:
if taken != 1:
self.predCorrect += 1
else:
self.inpredCorrect += 1
self.pht[self.history] += 1
self.history = (self.history >> 1) | (int(taken) << (self.bits - 1))
def display(self):
print(f"PHT")
accuracy = self.predCorrect / (self.predCorrect+self.inpredCorrect)
print(f"Prediction Accurancy is:\n {accuracy:.2f}\n")
class BHT:
def __init__(self, size, bits, wSize):
self.bht = {}
self.size = size
self.bits = bits
self.byteOffset = int(math.log(wSize, 2))
self.indexBits = int(math.log(size, 2))
self.predCorrect = 0
self.inpredCorrect = 0
self.bhtInit()
def bhtInit(self):
for key in range(self.size):
self.bht[key] = (1 << self.bits) - 1
def index(self, pc):
return (pc >> self.byteOffset) & ((1 << self.indexBits) - 1)
def update(self, pc, taken):
i = self.index(pc)
if self.bits == 1:
if self.bht[i] == 1:
if taken:
self.predCorrect += 1
else:
self.inpredCorrect += 1
self.bht[i] -= 1
else:
if taken != 1:
self.predCorrect += 1
else:
self.inpredCorrect += 1
self.bht[i] += 1
elif self.bits == 2:
if (self.bht[i] == 2) or (self.bht[i] == 3):
if taken:
self.predCorrect += 1
if self.bht[i] == 2:
self.bht[i] += 1
else:
self.inpredCorrect += 1
self.bht[i] -= 1
else:
if taken != 1:
self.predCorrect += 1
if self.bht[i] == 1:
self.bht[i] -= 1
else:
self.inpredCorrect += 1
self.bht[i] += 1
def display(self):
print(f"BHT")
accuracy = self.predCorrect / (self.predCorrect+self.inpredCorrect)
print(f"Prediction Accurancy is:\n {accuracy:.2f}\n")
\ No newline at end of file
......@@ -12,6 +12,7 @@ from riscv_isa import Instruction, decoder
from regfile import RegFile
from mux import make_mux
from alu import alu
from branchpred import BHT, PHT
control = decoder.control
ALU = lambda op1, op2, alu_fun: alu(op1, op2, alu_fun)
......@@ -23,6 +24,9 @@ imem = elfMem
PC = Register()
RF = RegFile()
DM = Memory(elfMem)
# branch predictors
bht = BHT(8, 2, 4)
pht = PHT(4)
#RF.clock(2, 0xEFFFF, True)
#RF.regs[2] = 0xeffff #pointer
......@@ -149,6 +153,9 @@ for t in itertools.count():
pc_des = 3
PC.clock(pc_change[pc_des])
bht.update(pc_val, takeBranch)
pht.update(pc_val, takeBranch)
# memory
rData = None
if (mem_em):
......@@ -165,12 +172,15 @@ for t in itertools.count():
print(f"{t:20d}:", display())
status = "TEST PASS" if RF.read(10) == 0 else "TEST FAIL"
print(status)
bht.display()
pht.display()
if status == "TEST FAIL":
exit(1)
if status == "TEST PASS":
exit(0)
RF.display()
print (f"SYSCALL: exit ({val>>1})")
sys.exit(val>>1)
else:
which = DM.mem[DM.mem[symbolTable['tohost']]]
......@@ -198,6 +208,9 @@ for t in itertools.count():
print(f"ECALL({RF.read(10)}): {RF.read(11)}\n")
status = "TEST PASS" if RF.read(10) == 0 else "TEST FAIL"
print(status)
RF.display()
bht.display()
pht.display()
if status == "TEST FAIL":
exit(1)
if status == "TEST PASS":
......@@ -207,6 +220,8 @@ for t in itertools.count():
status = "TEST PASS" if RF.read(10) == 0 else "TEST FAIL"
print(status)
RF.display()
bht.display()
pht.display()
if status == "TEST FAIL":
exit(1)
if status == "TEST PASS":
......@@ -216,9 +231,14 @@ for t in itertools.count():
strr = f"ECALL({RF.read(10)}): "
if RF.read(10) == 10:
print(strr + 'EXIT\n')
RF.display()
bht.display()
pht.display()
else:
print(strr + 'HALT\n')
RF.display()
bht.display()
pht.display()
break
###################################################################################
......@@ -229,11 +249,14 @@ for t in itertools.count():
if imem[PC.out()] == 0:
status = "TEST PASS" if RF.read(10) == 0 else "TEST FAIL"
print(status)
bht.display()
pht.display()
if status == "TEST FAIL":
exit(1)
if status == "TEST PASS":
exit(0)
#print("Done -- end of program.")
print("------------------------------< Final reg values >------------------------------")
RF.display()
break
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment