|
|
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
|