You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from binandint import *
class data_mem:
memory = [8 * '0'] * 4 * 1024 * 16
def __init__(self) -> None:
pass
def mem(self, write_en, read_en, address, data_in, funct3
): # 写使能,读使能,(使能为int的0或1)地址输入数据func3 #输出为data_out
if write_en:
if funct3 == '000': # sb
self.memory[address] = InttoBin(data_in % 2**8, 8)
elif funct3 == '001': # sh
temp = InttoBin(data_in % 2**16, 16)
self.memory[address + 1] = temp[0:8]
self.memory[address] = temp[8:16]
elif funct3 == '010': # sw
temp = InttoBin(data_in, 32)
self.memory[address + 3] = temp[0:8]
self.memory[address + 2] = temp[8:16]
self.memory[address + 1] = temp[16:24]
self.memory[address] = temp[24:32]
else:
pass
if read_en:
if funct3 == '000': # lb
data_out = BintoInt(self.memory[address])
elif funct3 == '001': # lh
data_out = BintoInt(self.memory[address + 1] +
self.memory[address])
elif funct3 == '010': # lw
data_out = BintoInt(self.memory[address + 3] +
self.memory[address + 2] +
self.memory[address + 1] +
self.memory[address])
elif funct3 == '100': # lbu
data_out = BintoUInt(self.memory[address + 1] +
self.memory[address])
elif funct3 == '101': # lhu
data_out = BintoUInt(self.memory[address + 3] +
self.memory[address + 2] +
self.memory[address + 1] +
self.memory[address])
else:
data_out = '0' * 32
return data_out