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.

478 lines
20 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.

import tkinter
from Print import Print_line
from ALU import ALU
from Clock import *
from SIG_ctrl import SIG
from memory import MEM
#初始化
cl = ClockStep()
alu = ALU()
si = SIG()
mem = MEM()
line = Print_line()
def init(cv:Canvas):
cl.init(cv, root)
alu.init(cv, root)
si.init(cv, root)
mem.show(cv, root)
line.init()
init(cv)
class RightPanel:
def R_set(self, num):
result = tkinter.simpledialog.askstring(title='修改R' + str(num + 1), prompt='请输入修改后的R值',
initialvalue=cpu.R(num))#显示输入提示
if not result:#若没有输入则直接返回
return
if not len(result) == len(cpu.R(num)):#输入错误则警告
messagebox.showwarning('警告', 'R值不合法')
return
for i in range(16):#输入错误则警告
if (not result[i] == '0' and not result[i] == '1'):
messagebox.showwarning('警告', 'R值不合法')
return
sum = 0#计算输入的值
for i in range(16):
sum *= 2
sum += int(result[i])
cpu.R_int[num] = sum# 修改相应寄存器的值
self.CAL_display()#重新显示面板
alu.init(cv, root)#修改仿真界面
cv.update()#更新画布
def op_set(self, num):
cpu.alu = num#修改cpu记录中的符号
if num == 1:#修改为+
alu.opa(cv)
elif num == 2:#修改为-
alu.opb(cv)
elif num == 3:#修改为*
alu.opc(cv)
elif num == 4:#修改为/
alu.opd(cv)
else:#修改为空
alu.no(cv)
self.CAL_display()#重新显示面板
def MEM_display(self):# 存储器操作面板
for w in root_right.winfo_children():
w.destroy()# 清除所有组件
# 显示标题
lab = tk.Label(root_right, text="存储器", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
lab.place(relx=0.15, rely=0.02, width=200, height=30)
y0 = 0.1
# 显示Mcontent与更改按钮
lab = tk.Label(root_right, text="当前Mcontent:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.05 + y0, width=120, height=30)
lab2 = tk.Label(root_right, text=cpu.MCO(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.1 + y0, width=200, height=30)
B = tk.Button(root_right, text="更改", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), command=lambda: self.MCO_set())
B.place(relx=0.7, rely=0.05 + y0, width=50, height=30)
# 显示Maddr与更改按钮
lab = tk.Label(root_right, text="当前Maddr:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.15 + y0, width=120, height=30)
lab2 = tk.Label(root_right, text=cpu.MAD(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.2 + y0, width=200, height=30)
B = tk.Button(root_right, text="更改", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), command=lambda: self.MAD_set())
B.place(relx=0.7, rely=0.15 + y0, width=50, height=30)
# 逐个设置存储单元按钮
B = tk.Button(root_right, text="设置存储器", command=lambda: self.set_memory())
B.place(relx=0.1, rely=0.2 + y0, width=200, height=30)
# 装载一段程序按钮
B = tk.Button(root_right, text="装载一段程序", command=lambda: self.write_memory())
B.place(relx=0.1, rely=0.25 + y0, width=200, height=30)
def CAL_display(self): # 运算器操作面板
for w in root_right.winfo_children():
w.destroy() # 清除右侧组件
lab = tk.Label(root_right, text="运算器", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
lab.place(relx=0.15, rely=0.02, width=200, height=30)
y0 = 0.1
# 显示运算符与更改按钮
PC = tk.Label(root_right, text="当前运算:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
PC.place(relx=0.1, rely=0.05 + y0, width=80, height=30)
lab2 = tk.Label(root_right, text=cpu.ALU(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.5, rely=0.05 + y0, width=20, height=30)
lab2 = tk.Label(root_right, text="更改运算符", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab2.place(relx=0.1, rely=0.1 + y0, width=120, height=30)
B = tk.Button(root_right, text='+', command=lambda: self.op_set(1), state=NORMAL) # 更改为+
B.place(relx=0.05 + 0 * 0.15, rely=0.15 + y0, relwidth=0.15, relheight=0.05)
B = tk.Button(root_right, text='-', command=lambda: self.op_set(2), state=NORMAL) # 更改为-
B.place(relx=0.05 + 1 * 0.15, rely=0.15 + y0, relwidth=0.15, relheight=0.05)
B = tk.Button(root_right, text='x', command=lambda: self.op_set(3), state=NORMAL) # 更改为*
B.place(relx=0.05 + 2 * 0.15, rely=0.15 + y0, relwidth=0.15, relheight=0.05)
B = tk.Button(root_right, text='÷', command=lambda: self.op_set(4), state=NORMAL) # 更改为/
B.place(relx=0.05 + 3 * 0.15, rely=0.15 + y0, relwidth=0.15, relheight=0.05)
B = tk.Button(root_right, text='', command=lambda: self.op_set(0), state=NORMAL) # 更改为无
B.place(relx=0.05 + 4 * 0.15, rely=0.15 + y0, relwidth=0.15, relheight=0.05)
R_text = []
for i in range(4): # 读取寄存器的值
R_text.append(cpu.R(i))
for i in range(4): # 显示各个寄存器的值与更改按钮
RN = tk.Label(root_right, text="R" + str(i + 1) + ':', bg=WHITE, fg=BLACK, font=('微软雅黑', 12))
RN.place(relx=0.05, rely=0.2 + y0 + 0.1 * i, width=80, height=30, anchor=NW)
lab1 = tk.Label(root_right, text=R_text[i], bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab1.place(relx=0.1, rely=0.25 + y0 + 0.1 * i, width=150, height=30, anchor=NW)
B = tk.Button(root_right, text='设置R' + str(i + 1), command=lambda num=i: self.R_set(num), state=NORMAL)
B.place(relx=0.3, rely=0.2 + y0 + 0.1 * i, relwidth=0.2, relheight=0.05)
def SIG_display(self):#控制器操作面板
for w in root_right.winfo_children():
w.destroy()# 清除右侧组件
lab = tk.Label(root_right, text="控制器", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
lab.place(relx=0.15, rely=0.02, width=200, height=30)
y0 = 0.1
# 显示PC值与更改按钮
PC = tk.Label(root_right, text="当前PC:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
PC.place(relx=0.1, rely=0.05 + y0, width=80, height=30)
lab2 = tk.Label(root_right, text=cpu.PC(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.1 + y0, width=200, height=30)
B = tk.Button(root_right, text="设置PC值", command=lambda: self.pc_set())
B.place(relx=0.5, rely=0.05 + y0, width=80, height=30)
# 显示IR值与更改按钮
IR = tk.Label(root_right, text="当前IR:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
IR.place(relx=0.1, rely=0.15 + y0, width=80, height=30)
lab2 = tk.Label(root_right, text=cpu.IR(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.2 + y0, width=200, height=30)
B = tk.Button(root_right, text="设置IR值", command=lambda: self.ir_set())
B.place(relx=0.5, rely=0.15 + y0, width=80, height=30)
# 显示CLOCK与更改按钮
CLO = tk.Label(root_right, text="当前时钟:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
CLO.place(relx=0.1, rely=0.25 + y0, width=80, height=30)
lab2 = tk.Label(root_right, text=str(cpu.clostep + 1), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.45, rely=0.25 + y0, width=50, height=30)
lab = tk.Label(root_right, text="设置时钟刻:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12))
lab.place(relx=0.1, rely=0.3 + y0, width=150, height=30)
for i in range(6):#遍历时钟刻
if cpu.clostep == i:#若是当前时钟刻则高亮显示,按钮禁用
B = tk.Button(root_right, text=str(i + 1), command=lambda n=i: self.clo_set(n), bg=YELLOW, state=DISABLED)
else:#若不是当前时钟刻则正常显示,按钮启用
B = tk.Button(root_right, text=str(i + 1), command=lambda n=i: self.clo_set(n), state=NORMAL)
B.place(relx=0.05 + i * 0.15, rely=0.35 + y0, relwidth=0.15, relheight=0.05)
# 显示Inscode与更改按钮
INS = tk.Label(root_right, text="当前Inscode:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
INS.place(relx=0.1, rely=0.4 + y0, width=130, height=30)
lab2 = tk.Label(root_right, text=cpu.CODE(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.45 + y0, width=100, height=30)
B = tk.Button(root_right, text="设置Inscode", command=lambda: self.code_set())
B.place(relx=0.55, rely=0.45 + y0, width=80, height=30)
def write_memory(self):
init(cv)
top = tk.Toplevel()
top.title('载入程序')
width = 1000
height = 700
top.geometry(f'{width}x{height}')
top.config(bg=WHITE)
lab = tk.Label(top, text="载入程序,用逗号隔开指令", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
lab.place(relx=0.05, rely=0.05, relwidth=0.4, relheight=0.1)
E = tk.Text(top, bg=GREY, fg=BLACK, font=('微软雅黑', 12))
E.place(relx=0.05, rely=0.15, relheight=0.6, relwidth=0.4)
B = tk.Button(top, text="载入程序", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), command=lambda: get_text())
B.place(relx=0.05, rely=0.75, relwidth=0.4, relheight=0.05)
user = E.get(1.0, 'end')
user_command = []
user.replace(' ', '')
user.replace('\n', '')
t = list(user)
for i in range(len(t)):
if t[i] == '':
t[i] = ','
elif not t[i] == '0' and not t[i] == '1' and not t[i] == ',':
t[i] = ''
user = ''.join(t)
users = user.split(',')
# print(users)
for u in users:
if len(u) == 16:
user_command.append(u)
# print(user_command)
lab = tk.Label(top, text="程序载入预览", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
lab.place(relx=0.55, rely=0.05, relwidth=0.4, relheight=0.1)
u_text = ''
for u in user_command:
u_text += u
u_text += '\n'
L = tk.Label(top, text=u_text, bg=GREY, fg=BLACK, font=('微软雅黑', 12))
L.place(relx=0.55, rely=0.15, relheight=0.6, relwidth=0.4)
B = tk.Button(top, text="确认载入程序", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), command=lambda: write())
B.place(relx=0.55, rely=0.75, relwidth=0.4, relheight=0.05)
def w_init():
for w in top.winfo_children():
w.destroy()
# 清除原有组件
lab = tk.Label(top, text="载入程序,用逗号隔开指令", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
lab.place(relx=0.05, rely=0.05, relwidth=0.4, relheight=0.1)
E = tk.Text(top, bg=GREY, fg=BLACK, font=('微软雅黑', 12))
E.place(relx=0.05, rely=0.15, relheight=0.6, relwidth=0.4)
B = tk.Button(top, text="载入程序", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), command=lambda: get_text())
B.place(relx=0.05, rely=0.75, relwidth=0.4, relheight=0.05)
def get_text():
# L.destroy()
user = E.get(1.0, 'end')
user_command = []
user.replace(' ', '')
user.replace('\n', '')
t = list(user)
for i in range(len(t)):
if t[i] == '':
t[i] = ','
elif not t[i] == '0' and not t[i] == '1' and not t[i] == ',':
t[i] = ''
user = ''.join(t)
users = user.split(',')
# print(users)
for u in users:
if len(u) == 16:
user_command.append(u)
# print(user_command)
# lab = tk.Label(top, text="程序载入预览", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
# lab.place(relx=0.55, rely=0.05, relwidth=0.4, relheight=0.1)
u_text = ''
i = 0
for u in user_command:
u_text += bin(i).replace('0b', '').zfill(16)
u_text += ' '
u_text += u
u_text += '\n'
i += 1
L = tk.Label(top, text=u_text, bg=GREY, fg=BLACK, font=('微软雅黑', 12), anchor=N)
L.place(relx=0.55, rely=0.15, relheight=0.6, relwidth=0.4)
def write():
user = E.get(1.0, 'end')
user_command = []
user.replace(' ', '')
user.replace('\n', '')
t = list(user)
for i in range(len(t)):
if t[i] == '':
t[i] = ','
elif not t[i] == '0' and not t[i] == '1' and not t[i] == ',':
t[i] = ''
user = ''.join(t)
users = user.split(',')
# print(users)
for u in users:
if len(u) == 16:
user_command.append(u)
# print(user_command)
for i in range(20):
cpu.SimMem[i] = bin(0).replace('0b', '').zfill(16)
for i in range(len(user_command)):
cpu.SimMem[i] = user_command[i]
mem.show(cv, root)
top.destroy()
def set_memory(self):
init(cv)
top = tk.Toplevel()
top.title('编辑存储器')
width = 500
height = 600
top.geometry(f'{width}x{height}')
top.config(bg=WHITE)
Texts = []
def show():
for w in top.winfo_children():
w.destroy()
# 清除原有组件
Texts.clear()
lab = tk.Label(top, text="设置存储器", bg=WHITE, fg=BLACK, font=('微软雅黑', 20))
lab.place(relx=0.25, rely=0.02, width=200, height=30)
y0 = 50
lab = tk.Label(top, text="当前存储器:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=CENTER)
lab.place(y=y0, width=120, height=30)
for i in range(20):
lab = tk.Label(top, text="00000000 " + bin(i).replace('0b', '').zfill(8), bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=CENTER)
lab.place(x=30, y=30 + y0 + i * 20, width=180, height=20)
text = tkinter.Entry(top, bg=GREY, fg=BLACK, font=('微软雅黑', 12), bd=2)
text.place(x=200, y=30 + y0 + i * 20, width=180, height=20)
text.insert(0, cpu.SimMem[i])
Texts.append(text)
B = tk.Button(top, text="设置Memory", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), command=lambda: set_mem())
B.place(x=100, y=450 + y0, width=280, height=20)
show()
def set_mem():
for T in Texts:
txt = T.get()
# print(txt)
if not len(txt) == len(cpu.SimMem[0]):
messagebox.showwarning('警告', '修改不合法')
show()
return
for l in range(len(txt)):
if (not txt[l] == '0') and (not txt[l] == '1'):
messagebox.showwarning('警告', '修改不合法')
show()
return
i = 0
for T in Texts:
txt = T.get()
# print (Texts[i].get())
cpu.SimMem[i] = txt
i += 1
show()
mem.show(cv, root)
def SIG_info(self):
pass
def MCO_set(self):
result = tkinter.simpledialog.askstring(title='修改Mcontent', prompt='请输入修改后的Mcontent', initialvalue=cpu.MCO())
if not result:
return
if not len(result) == len(cpu.MCO()):
messagebox.showwarning('警告', 'Mcontent不合法')
return
for i in range(8):
if (not result[i] == '0' and not result[i] == '1') or (not result[i + 9] == '0' and not result[i + 9] == '1'):
messagebox.showwarning('警告', 'Mcontent不合法')
return
sum = 0
for i in range(8):
sum *= 2
sum += int(result[i])
for i in range(8):
sum *= 2
sum += int(result[i + 9])
# 将更改后的的值放到Mcontent中
cpu.MCO_int = sum
self.MEM_display()
mem.show(cv, root)
cv.update()
def MAD_set(self):
result = tkinter.simpledialog.askstring(title='修改Maddr', prompt='请输入修改后的Maddr',
initialvalue=cpu.MAD())
if not result:
return
if not len(result) == len(cpu.MAD()):
messagebox.showwarning('警告', 'Maddr不合法')
return
for i in range(8):
if (not result[i] == '0' and not result[i] == '1') or (
not result[i + 9] == '0' and not result[i + 9] == '1'):
messagebox.showwarning('警告', 'Maddr不合法')
return
sum = 0
for i in range(8):
sum *= 2
sum += int(result[i])
for i in range(8):
sum *= 2
sum += int(result[i + 9])
cpu.MAD_int = sum
self.MEM_display()
mem.show(cv, root)
cv.update()
def code_set(self):
result = tkinter.simpledialog.askstring(title='修改Inscode', prompt='请输入修改后的Inscode', initialvalue=cpu.CODE())
if not result:
return
if not len(result) == len(cpu.CODE()):
messagebox.showwarning('警告', 'Inscode不合法')
return
for i in range(6):
if (not result[i] == '0' and not result[i] == '1'):
messagebox.showwarning('警告', 'Inscode不合法')
return
sum = 0
for i in range(6):
sum *= 2
sum += int(result[i])
# 修改INSCODE值
cpu.code = sum
self.SIG_display()
si.init(cv, root)
cv.update()
def pc_set(self):
result = tkinter.simpledialog.askstring(title='修改PC值', prompt='请输入修改后的PC值', initialvalue=cpu.PC())
if not result:
return
if not len(result) == len(cpu.PC()):
messagebox.showwarning('警告', 'PC值不合法')
return
for i in range(8):
if (not result[i] == '0' and not result[i] == '1') or (not result[i + 9] == '0' and not result[i + 9] == '1'):
messagebox.showwarning('警告', 'PC值不合法')
return
sum = 0
for i in range(8):
sum *= 2
sum += int(result[i])
for i in range(8):
sum *= 2
sum += int(result[i + 9])
cpu.PC_int = sum
self.SIG_display()
cv.update()
def ir_set(self):
result = tkinter.simpledialog.askstring(title='修改IR值', prompt='请输入修改后的IR值', initialvalue=cpu.IR())
if not result:
return
if not len(result) == len(cpu.IR()):
messagebox.showwarning('警告', 'IR值不合法')
return
for i in range(8):
if (not result[i] == '0' and not result[i] == '1') or (not result[i + 9] == '0' and not result[i + 9] == '1'):
messagebox.showwarning('警告', 'IR值不合法')
return
sum = 0
for i in range(8):
sum *= 2
sum += int(result[i])
for i in range(8):
sum *= 2
sum += int(result[i + 9])
cpu.IR_int = sum
self.SIG_display()
cv.update()
def clo_set(self, clo):
cpu.CLO(clo)
self.SIG_display()
if __name__ == "__main__":
r = RightPanel()#实例化类
r.MEM_display()#显示存储器操作面板
# r.CAL_display()
# r.SIG_display()
# r.set_memory()
# r.write_memory()
# init(cv)
root.mainloop()