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.

133 lines
3.7 KiB

7 months ago
def alu_op():
ALUop = dict()
for i in '+-x÷':
ALUop[i] = False
return ALUop
def alu_io():
ALUio = dict()
Register = ["R1", "R2", "R3", "R4"]
for reg in Register:
ALUio[f"{reg}_ALU_1"] = False
ALUio[f"{reg}_ALU_2"] = False
ALUio[f"ALU_{reg}"] = False
return ALUio
def clock_step():
ClockStep = dict()
for i in range(6):
ClockStep[str(i + 1)] = False
return ClockStep
def sig_obj():
SigObj = dict()
Register = ["R1", "R2", "R3", "R4", "IR"]
ctrl = ["ALU","ALU_1", "ALU_2", "PC+1", "Memory"]
for r in Register:
SigObj[f"Mcontent->{r}"] = [f"Mcontent->{r}", False, "DataSig"]
SigObj[f"->{r}"] = [f"->{r}", False, "CtrlSig"]
for ct in ctrl:
SigObj[f"->{ct}"] = [f"->{ct}", False, "CtrlSig"]
SigObj["IR->SigCtrl"] = ["IR->SigCtrl", False, "CtrlSig"]
SigObj["PC->Maddr"] = ["PC->Maddr", False, "DataSig"]
SigObj["IR->Maddr"] = ["IR->Maddr", False, "DataSig"]
SigObj["Memory->Mcontent"] = ["Memory->Mcontent", False, "DataSig"]
SigObj["Mcontent->PC"] = ["Mcontent->PC", False, "DataSig"]
SigObj["Mcontent->Memory"] = ["Mcontent->Memory", False, "DataSig"]
return SigObj
def sim_mem():
sim_mem_ls = []
for i in range(20):
sim_mem_ls.append(f"{0:016b}")
return sim_mem_ls
def bin_to_dec(binary_string, bit_width=16):
"""
将补码二进制字符串转换为有符号数10进制处理溢出
参数
binary_string (str): 补码二进制字符串例如 "1101100101101110"
bit_width (int): 位宽默认为16位
返回
int: 转换后的有符号数10进制
"""
if len(binary_string) > bit_width:
# 如果二进制字符串长度超出位宽,截断超出部分
binary_string = binary_string[-bit_width:]
# 如果最高位为1说明是负数先取反再加1得到绝对值的原码
if binary_string[0] == '1':
inverted_string = ''.join('1' if bit == '0' else '0' for bit in binary_string)
absolute_value = -(int(inverted_string, 2) + 1)
else:
# 如果最高位是0则直接转换为正数
absolute_value = int(binary_string, 2)
return absolute_value
def dec_to_bin(decimal_value, bit_width=16):
"""
将有符号数10进制转换为补码二进制字符串处理溢出
参数
decimal_value (int): 有符号数10进制
bit_width (int): 位宽默认为16位
返回
str: 转换后的补码二进制字符串
"""
# 如果输入数据超出位宽,截断超出部分
decimal_value = decimal_value & ((1 << bit_width) - 1)
# 如果是负数,获取其绝对值的原码
if decimal_value < 0:
absolute_value_binary = bin(abs(decimal_value))[2:]
# 如果位数不足需要在左侧补0
if len(absolute_value_binary) < bit_width:
absolute_value_binary = '0' * (bit_width - len(absolute_value_binary)) + absolute_value_binary
# 对绝对值的原码进行按位取反
inverted_representation = ''.join('1' if bit == '0' else '0' for bit in absolute_value_binary)
# 将取反后的二进制表示加上1得到补码表示
binary_representation = format(int(inverted_representation, 2) + 1, f'0{bit_width}b')
else:
# 如果是正数,直接转换为二进制
binary_representation = bin(decimal_value)[2:]
# 如果位数不足需要在左侧补0
if len(binary_representation) < bit_width:
binary_representation = '0' * (bit_width - len(binary_representation)) + binary_representation
return binary_representation
ALUio = alu_io()
ALUop = alu_op()
ClockStep = clock_step()
SigObj = sig_obj()
SimMem = sim_mem()
if __name__ == '__main__':
ALUio = alu_io()
print(ALUio)
ALUop = alu_op()
print(ALUop)
ClockStep = clock_step()
print(ClockStep)
SigObj = sig_obj()
print(SigObj)
SimMem = sim_mem()
print(SimMem)