Commit 76e6da2d authored by Khoi Lam's avatar Khoi Lam 💬
Browse files

working on overloading str

parent a65e47c4
# filename: fetch_decode.py
import itertools
from pydigital.memory import readmemh
from pydigital.register import Register
from riscv_isa import Instruction
# the PC register
PC = Register()
# construct a memory segment for instruction memory
# load the contents from the 32-bit fetch_test hex file (big endian)
imem = readmemh('riscv_isa/programs/fetch_test.hex',
word_size = 4, byteorder = 'big')
def display():
if pc_val == None:
return "PC: xxxxxxxx, IR: xxxxxxxx"
else:
return f"PC: {pc_val:08x}, IR: {instr.val:08x}, {instr}" # {instr} just returns the representation of the instruction that we overloaded with __str__
startup = True
# generate system clocks until we reach a stopping condition
# this is basically the run function from the last lab
for t in itertools.count():
# sample inputs
pc_val = PC.out()
# RESET the PC register
if startup:
PC.reset(imem.begin_addr)
startup = False
print(f"{t:20d}:", display())
continue
# access instruction memory
instr = Instruction(imem[pc_val], pc_val)
# print one line at the end of the clock cycle
print(f"{t:20d}:", display())
# clock logic blocks, PC is the only clocked module!
# here the next pc value is always +4
PC.clock(4 + pc_val)
# check stopping conditions on NEXT instruction
if PC.out() > 0x1100:
print("STOP -- PC is large! Is something wrong?")
break
if imem[PC.out()] == 0:
print("Done -- end of program.")
break
......@@ -16,16 +16,28 @@ def regNumToName(num):
's4', 's5', 's6', 's7', # 20..23
's8', 's9', 's10', 's11', # 24..27]
't3', 't4', 't5', 't6'][num] # 28..31
def convert(int_val):
'''
Function to convert an int value to a 32-bit binary value
Input: int value (value)
Output: 32-bit extended binary bin_value (str)
'''
bin_value = bin(int_val)
return str(bin_value)[2:].zfill(32)
class Instruction():
"represents/decodes RISCV instructions"
"represents/decodes RISCV instructions"
def __init__ (self, val, pc, symbols = {}):
"""
Decodes a risc-v instruction word
val is the machine code word
pc is the pc value used to format pc-relative assembly instructions
symbols is an optional symbol table to decode addresses in assembly output
symbols is an optional symbol table to decode addresses in assembly output
"""
self.val = val
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)
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