Commit b34b8ff7 authored by Khoi Lam's avatar Khoi Lam 💬
Browse files

succesfully decoded the instruction

parent 76e6da2d
......@@ -4,6 +4,31 @@ class BadInstruction(Exception):
# build csr lookup
csrd = {k:v for k,v in csrs}
r_type ={
"0000000": {
"000": "add",
"001": "sll",
"010": "slt",
"011": "sltu",
"100": "xor",
"101": "srl",
"110": "or",
"111": "and"
},
"0100000": {
"000": "sub",
"101": "sra"
}
}
# There is no func7 for i types, only func3. Set default "func7" to be "NA" literal
i_type ={
"NA": {
"000": "addi",
"010": "slti",
#to be added
}
}
def regNumToName(num):
if type(num) != int or num < 0 or num > 31:
......@@ -17,7 +42,8 @@ def regNumToName(num):
's8', 's9', 's10', 's11', # 24..27]
't3', 't4', 't5', 't6'][num] # 28..31
def convert(int_val):
def convert_to_string(int_val):
'''
Function to convert an int value to a 32-bit binary value
Input: int value (value)
......@@ -26,6 +52,39 @@ def convert(int_val):
bin_value = bin(int_val)
return str(bin_value)[2:].zfill(32)
def decode_type(opcode):
'''
function that determines what type the instruction is
return type
'''
return {"0110011": "r", "0010011": "i", "1110011": "sys"}[opcode]
def decode_r_instruction(func3, func7, rd, rs1, rs2):
"""
return a full string that represents the r instruction
"""
assert (len(func3) == 3 and len(func7) == 7 and len(rd) == 5 and len(rs1) == 5 and len(rs2) == 5), "one of the sizes is incorrect"
instr_name = r_type[func7][func3]
rd_name = regNumToName(int(rd, 2))
rs1_name = regNumToName(int(rs1, 2))
rs2_name = regNumToName(int(rs2, 2))
return instr_name + " " + rd_name + " " + rs1_name + " " + rs2_name
def decode_i_instruction(func3, rd, rs1, imm):
"""
return a full string that represents the i instruction
"""
assert (len(func3) == 3 and len(imm) == 12 and len(rd) == 5 and len(rs1) == 5), "one of the sizes is incorrect"
instr_name = i_type["NA"][func3]
rd_name = regNumToName(int(rd, 2))
rs1_name = regNumToName(int(rs1, 2))
imm_value = str(int(imm,2))
if rs1_name == "zero":
return "li" + " " + rd_name + " " + imm_value
return instr_name + " " + rd_name + " " + rs1_name + " " + imm_value
class Instruction():
"represents/decodes RISCV instructions"
def __init__ (self, val, pc, symbols = {}):
......@@ -36,8 +95,28 @@ class Instruction():
symbols is an optional symbol table to decode addresses in assembly output
"""
self.val = val #machine coded word
#print(self.val)
self.pc = pc # pc relative instrs need pc to compute targets for display
self.symbols = symbols
def __str__(self):
return convert(self.val)
bin_val = convert_to_string(self.val)
rs1= bin_val[12:17] # 5 bits
func3 = bin_val[17:20] # 3 bits
rd= bin_val[20:25] # 5 bits
opcode = bin_val[25:] # 7 bits
type = decode_type(opcode)
if type == "r":
#strip the needed args for r type here
func7 = bin_val[0:7] # 7 bits
rs2 = bin_val[7:12] # 5 bits
return decode_r_instruction(func3, func7, rd, rs1, rs2)
elif type == "i":
#strip the needed args here
imm = bin_val[:12]
return decode_i_instruction(func3, rd, rs1, imm)
else:
return "ecall"
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