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

3 years ago
from binandint import *
3 years ago
class data_mem:
3 years ago
memory = [8 * '0'] * 4 * 1024 * 16
3 years ago
def __init__(self) -> None:
pass
3 years ago
def mem(self, write_en, read_en, address, data_in, funct3
): # 写使能,读使能,(使能为int的0或1)地址输入数据func3 #输出为data_out
3 years ago
if write_en:
if funct3 == '000': # sb
3 years ago
self.memory[address] = InttoBin(data_in % 2**8, 8)
elif funct3 == '001': # sh
3 years ago
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]
3 years ago
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])
3 years ago
else:
3 years ago
data_out = '0' * 32
3 years ago
return data_out