取程序微指令, 完成

main
阳光少年 7 months ago
commit f99fe9e7b2

@ -0,0 +1,17 @@
import pin
FETCH = [
# 把 pc(当前是0) 的值 输出到偏移地址寄存器中,
pin.PC_OUT | pin.MAR_IN,
# 内存根据上方修改后的偏移地址寄存器找到数据输出到总线上, IR 写入, 给PC 增加1
pin.RAM_OUT | pin.IR_IN | pin.PC_INC,
pin.PC_OUT | pin.MAR_IN,
pin.RAM_OUT | pin.DST_IN | pin.PC_INC,
pin.PC_OUT | pin.MAR_IN,
pin.RAM_OUT | pin.SRC_IN | pin.PC_INC,
]
if __name__ == "__main__":
print(FETCH)

@ -0,0 +1,64 @@
from assembly import FETCH
import pin
META_DATA = "v3.0 hex words addressed\n"
def chunk(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
def get_hex_str(codes, width):
width = int(width)
data = []
for _code in codes:
hex_str = hex(_code)[2:]
zero_str = ""
for _ in range(width - len(hex_str)):
zero_str += "0"
data.append(zero_str + hex_str)
return data
def save_hex_str(hex_str_lis, chunk_count: int):
lines = []
for line in chunk(hex_str_lis, chunk_count):
print(line)
if __name__ == "__main__":
data_width = 32
addr_width = 16
# 我们现在只支持16个指令
code_hex_str = get_hex_str(FETCH, data_width / 4)
base_bin_data = get_hex_str([pin.HLT for _ in range(1 << addr_width)], data_width / 4)
for addr in range(1 << addr_width):
ir = addr >> 8
psw = (addr >> 4) & 0xf
cyc = addr & 0xf
if cyc < len(code_hex_str):
base_bin_data[addr] = code_hex_str[cyc]
# 写入数据到 bin文件
addr_chunk = int(64 / (32 / 4)) # bin文件每行有多少个数据
lines = [META_DATA, ]
idx = 0
for slice in chunk(base_bin_data, addr_chunk):
hex_idx_str = hex(idx)[2:]
zero_str = ""
for _ in range(4 - len(hex_idx_str)):
zero_str += "0"
hex_idx_str = zero_str + hex_idx_str
line = f"{hex_idx_str}:"
for _ in slice:
line += f" {_}"
line += "\n"
lines.append(line)
idx += addr_chunk # 下一行的地址
with open("./ROM.bin", "w") as f:
f.writelines(lines)

13723
cpu.circ

File diff suppressed because it is too large Load Diff

@ -0,0 +1,92 @@
MSR = 1
MAR = 2
MDR = 3
RAM = 4
IR = 5
DST = 6
SRC = 7
A = 8
B = 9
C = 10
D = 11
DI = 12
SI = 13
SP = 14
BP = 15
CS = 16
DS = 17
SS = 18
ES = 19
VEC = 20
T1 = 21
T2 = 22
MSR_OUT = MSR
MAR_OUT = MAR
MDR_OUT = MDR
RAM_OUT = RAM
IR_OUT = IR
DST_OUT = DST
SRC_OUT = SRC
A_OUT = A
B_OUT = B
C_OUT = C
D_OUT = D
DI_OUT = DI
SI_OUT = SI
SP_OUT = SP
BP_OUT = BP
CS_OUT = CS
DS_OUT = DS
SS_OUT = SS
ES_OUT = ES
VEC_OUT = VEC
T1_OUT = T1
T2_OUT = T2
_DST_SHIFT = 5
MSR_IN = MSR << _DST_SHIFT
MAR_IN = MAR << _DST_SHIFT
MDR_IN = MDR << _DST_SHIFT
RAM_IN = RAM << _DST_SHIFT
IR_IN = IR << _DST_SHIFT
DST_IN = DST << _DST_SHIFT
SRC_IN = SRC << _DST_SHIFT
A_IN = A << _DST_SHIFT
B_IN = B << _DST_SHIFT
C_IN = C << _DST_SHIFT
D_IN = D << _DST_SHIFT
DI_IN = DI << _DST_SHIFT
SI_IN = SI << _DST_SHIFT
SP_IN = SP << _DST_SHIFT
BP_IN = BP << _DST_SHIFT
CS_IN = CS << _DST_SHIFT
DS_IN = DS << _DST_SHIFT
SS_IN = SS << _DST_SHIFT
ES_IN = ES << _DST_SHIFT
VEC_IN = VEC << _DST_SHIFT
T1_IN = T1 << _DST_SHIFT
T2_IN = T2 << _DST_SHIFT
SRC_R = 2 ** 10
SRC_W = 2 ** 11
DST_R = 2 ** 12
DST_W = 2 ** 13
PC_WE = 2 ** 14
PC_CS = 2 ** 15
PC_EN = 2 ** 16
PC_OUT = PC_CS
PC_IN = PC_CS | PC_WE
PC_INC = PC_CS | PC_WE | PC_EN
HLT = 2 ** 31
Loading…
Cancel
Save