Commit 68e3de8b authored by Christina Yu's avatar Christina Yu
Browse files

lab5 fixed

parent 563779f2
......@@ -15,7 +15,6 @@ from alu import alu
PC = Register()
RF = RegFile()
DM = Memory(MemorySegment(0xE0000, 0x4000)) #0xe0000, 0x10000
print(str(DM.mem) + '\n')
RF.clock(2, 0xEFFFF, True)
control = decoder.control
......@@ -32,8 +31,10 @@ RF.regs[0] = 0x00000
imem = readmemh(sys.argv[1], word_size = 4, byteorder = 'big')
def display():
print("--------------------------------------------------------------------------------")
if pc_val == None:
return "PC: xxxxxxxx, IR: xxxxxxxx\n"
print("------------------------------< reset >------------------------------")
return "PC: xxxxxxxx, IR: xxxxxxxx"
else:
if instr.imm != None:
rs2imm_str = f"rs2: xxxxxxxx [xx] imm: {int(instr.immb, 2):04x}"
......@@ -56,7 +57,7 @@ def display():
return f"PC: {pc_val:08x}, IR: {instr.val:08x}, {instr}\n" + \
rd_str + rs1_str + rs2imm_str + \
f" op: {instr.opcode} f3: {instr.funct3} f7: {instr.funct7}" + \
f" alu_fun: {alu_str}\n"
f" alu_fun: {alu_str}"
#f"rd: {RF.read(int(instr.rdb, 2))} [{int(instr.rdb, 2)}] " + rs1_str + rs2imm_str + \
#f" op: {int(instr.opcode, 2):x} f3: {int(instr.funct3)} f7: {int(instr.funct7)}" + \
......@@ -83,11 +84,16 @@ for t in itertools.count():
rs1Val = None
rs2Val = None
rs1Val = lambda: RF.read(int(instr.rs1b, 2)) if RF.read(int(instr.rs1b, 2)) else 0
rs2Val = lambda: RF.read(int(instr.rs2b, 2)) if RF.read(int(instr.rs2b, 2)) else 0
#rs1Val = lambda: RF.read(int(instr.rs1b, 2)) if RF.read(int(instr.rs1b, 2)) else 0
#rs2Val = lambda: RF.read(int(instr.rs2b, 2)) if RF.read(int(instr.rs2b, 2)) else 0
if instr.name == "li":
instr.name = "addi"
if instr.rs1 != None:
rs1Val = RF.read(int(instr.rs1b, 2))
if instr.rs2 != None:
rs2Val = RF.read(int(instr.rs2b, 2))
#if instr.name == "li":
# instr.name = "addi"
iType = lambda: twos_comp(int(instr.binVal[0:12], 2), 12)
sType = lambda: twos_comp(int(instr.binVal[0:7] + instr.binVal[20:25], 2), 12)
......@@ -96,7 +102,6 @@ for t in itertools.count():
instr.binVal[11] + instr.binVal[1:11], 2), 20) << 1
#decoding
print(instr.name)
alu_fun = control[instr.name].ALU_fun
alu_str = control[instr.name].renums["ALU_fun"][getattr(control[instr.name], "ALU_fun")]
op1_sel = control[instr.name].op1_sel
......@@ -106,21 +111,21 @@ for t in itertools.count():
wb_sel = control[instr.name].wb_sel
#execute
op1Mux = make_mux(rs1Val, uType)
op2Mux = make_mux(rs2Val, sType, iType, pc_val)
op1Mux = make_mux(lambda: rs1Val, uType)
op2Mux = make_mux(lambda: rs2Val, sType, iType, lambda: pc_val)
op1 = op1Mux(op1_sel)
op2 = op2Mux(op2_sel)
aluVal = ALU(op1, op2, alu_fun)
rData = DM.out(aluVal)
DM.clock(aluVal, rs2Val(), mem_wr)
DM.clock(aluVal, rs2Val, mem_wr)
if instr.name == "sw":
print(DM.mem[aluVal])
wdMux = lambda x: [pc_val + 4, aluVal, rData][x]
wd = wdMux(wb_sel)
print(wb_sel)
#print(wb_sel)
# writeback
rd = int(instr.binVal[20:25], 2)
......@@ -141,11 +146,11 @@ for t in itertools.count():
print(strr + 'HALT\n')
RF.display()
break
if rs1Val == None: rs1Val = 0
jalr = lambda i_imm, rs1Val: rs1Val + i_imm
branch = lambda pc, imm: pc + imm
jump = lambda pc, imm: pc + imm
jalrVal = jalr(i_imm = iType(), rs1Val = rs1Val())
jalrVal = jalr(i_imm = iType(), rs1Val = rs1Val)
branchVal = branch(pc_val, twos_comp(int(instr.binVal[0:7] + instr.binVal[20:25], 2), 12))
jumpVal = jump(pc_val, ujType())
pc_change = [pc_val + 4, jalrVal, branchVal, jumpVal]
......@@ -156,19 +161,18 @@ for t in itertools.count():
#PC.clock(4 + pc_val)
takeBranch = False
if instr.name == "beq":
takeBbranch = (rs1Val == rs2Val)
takeBranch = (rs1Val == rs2Val)
elif instr.name == "bne":
takeBbranch = (rs1Val != rs2Val)
takeBranch = (rs1Val != rs2Val)
elif instr.name == "blt":
takeBbranch = (rs1Val < rs2Val)
takeBranch = (rs1Val < rs2Val)
elif instr.name == "bge":
takeBbranch = (rs1Val >= rs2Val)
takeBranch = (rs1Val >= rs2Val)
elif instr.name == "bltu":
takeBbranch = (rs1Val >= rs2Val)
takeBranch = (rs1Val >= rs2Val)
elif instr.name == "bgeu":
takeBbranch = (rs1Val >= rs2Val)
takeBranch = (rs1Val >= rs2Val)
print()
if instr.name == "jalr":
pc_des = 1
elif instr.type == "SB" and takeBranch:
......@@ -182,6 +186,7 @@ for t in itertools.count():
print("STOP -- PC is large! Is something wrong?")
break
if imem[PC.out()] == 0:
print("Done -- end of program.")
#print("Done -- end of program.")
print("------------------------------< Final reg values >------------------------------")
RF.display()
break
\ No newline at end of file
......@@ -29,17 +29,18 @@ instrDict = {
"UJ": ["jal"],
}
"sltiu", "xori", "srli", "srai"
opcodeDict = { #opcode, funct3, funct7/imm, inst
"0000011": {"010": "lw"},
"0100011": {"010": "sw"},
"1100011": {"000": "beq", "001": "bne", "100": "blt", "101": "bge", "110": "bltu", "111": "bgeu"},
"0010011": {"000": "addi", "010": "slti", "110": "ori", "111": "andi", "001": "slli"},
"0010011": {"000": "addi", "010": "slti", "110": "ori", "111": "andi", "001": "slli", "011": "sltiu", "100": "xori", "101": {
"0000000": "srli", "0100000": "srai"}},
"0110111": "lui",
"1101111": "jal",
"0110011": {"000": {
"0100000": "sub",
"0000000": "add"}, "001": "sll"},
"0000000": "add"}, "001": "sll", "100": "xor", "110": "or"},
"1110011": {"000": {
"000000000001": "ebreak",
"000000000000": "ecall"}}}
......@@ -91,7 +92,7 @@ class Instruction():
return "S"
if self.opcode == "1100011":
return "SB"
if self.opcode == "1101111 ":
if self.opcode == "1101111":
return "UJ"
return "error getting type"
......@@ -99,7 +100,7 @@ class Instruction():
# return instruction name given binary representation
if self.type == "I":
#self.funct7 = "0"
#selfç.funct7 = "0"
self.rdb = self.binVal[20:25] #rd reg in binary
self.rd = regNumToName(int(self.rdb, 2)) #rd reg
self.immb = self.binVal[:12] #immediate in binary
......@@ -108,21 +109,34 @@ class Instruction():
self.rs1 = regNumToName(int(self.rs1b, 2)) #reg rs1
self.funct3 = self.binVal[17:20] #funct3 in binary
#self.imm = str(int(self.immb, 2)) #convert integer binary to string
if self.funct3 == "010":
return "slti"
elif self.funct3 == "110":
return "ori"
elif self.funct3 == "111":
return "andi"
elif (self.opcode == "1110011") and (self.funct3 == "000"):
return "ecall"
if self.funct3 == "000":
# if self.rs1 == "zero":
# return "li"
# else:
return "addi"
elif self.funct3 == "001":
return "slli"
if (self.opcode == "1110011") and (self.funct3 == "000"):
return "ecall"
elif self.opcode == "0010011":
if self.funct3 == "010":
return "slti"
elif self.funct3 == "110":
return "ori"
elif self.funct3 == "111":
return "andi"
elif self.funct3 == "000":
# if self.rs1 == "zero":
# return "li"
# else:
return "addi"
elif self.funct3 == "001":
return "slli"
elif self.funct3 == "011":
return "sltiu"
elif self.funct3 == "100":
return "xori"
elif self.funct3 == "101":
if self.funct7 == "0000000":
return "srli"
elif self.funct7 == "0100000":
return "srai"
elif self.opcode == "0000011":
if self.funct3 == "010":
return "lw"
elif self.type == "R":
self.funct7 = self.binVal[:7] #funct7 in binary
......@@ -133,11 +147,19 @@ class Instruction():
self.funct3 = self.binVal[17:20] #funct3 in binary
self.rdb = self.binVal[20:25] #rd reg in binary
self.rd = regNumToName(int(self.rdb, 2)) #rd reg
if self.funct7 == "0100000":
if self.funct3 == "000":
return "sub"
if self.funct7 == "0000000":
if self.funct3 == "000":
return "add"
elif self.funct3 == "001":
return "sll"
elif self.funct3 == "100":
return "xor"
elif self.funct3 == "110":
return "or"
elif self.type == "U":
self.rdb = self.binVal[20:25] #rd reg in binary
......@@ -156,10 +178,8 @@ class Instruction():
self.immb = self.binVal[0] + self.binVal[12:20] + self.binVal[11] + self.binVal[1:11]
self.imm = twos_comp(int(self.immb,2), len(self.immb)) << 1
#self.imm = str(int(self.immb, 2))
if self.opcode == "0110111":
return "lui"
elif self.opcode == "0010111":
return "auipc"
return "jal"
elif self.type == "S":
self.rs2b = self.binVal[7:12] #reg rs2 in binary
......
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