upload cpu project

master
bettleChen 1 year ago
parent dacfad9585
commit 8fab49b213

350
ALU.py

@ -0,0 +1,350 @@
from UI import *
# import tkinter as tk
class ALU:
def __init__(self):
round_rectangle(74, 48, 533, 415, r=25, fill=BACK, width=0) # 绘制ALU蓝色背景
Point = [90, 190, 120, 190, 130, 215, 205, 215, 215, 190, 245, 190, 220, 270, 115, 270]
cv.create_polygon(Point, fill=BLACK) # 绘制运算器
self.line_init()# 在图中标记各个部件的名字
def show(self):
lines = ['ALU_R1', 'ALU_R2', 'ALU_R3', 'ALU_R4', 'R1_ALU1', 'R2_ALU1',
'R3_ALU1', 'R4_ALU1', 'R1_ALU2', 'R2_ALU2', 'R3_ALU2', 'R4_ALU2']
for l in lines:
if cpu.ALUio[l] == False: #如果ALUio中该线为False则非高亮显示
eval('self.' + l + '(cv, 0)')
for l in lines:
if cpu.ALUio[l] == True: #如果ALUio中该线为True则高亮显示
eval('self.' + l + '(cv, 1)')
if cpu.ALUop['+'] == True:
self.opa(cv)
if cpu.ALUop['-'] == True:
self.opb(cv)
if cpu.ALUop['*'] == True:
self.opc(cv)
if cpu.ALUop['/'] == True:
self.opd(cv)
def line_init(self):
cv.create_text(140, 183, text='ALU', font=('微软雅黑', 20), anchor=NW, fill=WORD)
cv.create_text(345, 95, text="R1", font=('微软雅黑', 18), fill=WORD)
cv.create_text(345, 185, text="R2", font=('微软雅黑', 18), fill=WORD)
cv.create_text(345, 275, text="R3", font=('微软雅黑', 18), fill=WORD)
cv.create_text(345, 365, text="R4", font=('微软雅黑', 18), fill=WORD)
lines = ['ALU_R1', 'ALU_R2', 'ALU_R3', 'ALU_R4', 'R1_ALU1', 'R2_ALU1',
'R3_ALU1', 'R4_ALU1', 'R1_ALU2', 'R2_ALU2', 'R3_ALU2', 'R4_ALU2' ]
for l in lines:
eval('self.' + l + '(cv, 0)')
# for l in lines:
# eval('self.' + l + '(cv, -1)')
def mark(self, lines):
for i in range(5):
if i:
time.sleep(cpu.time)
if i % 2:
for l in lines:
# print('self.' + l + '(cv, 1)')
# self.init()
eval('self.' + l + '(cv, 1)')
else:
for l in lines:
# print('self.' + l + '(cv, 0)')
# self.init()
eval('self.' + l + '(cv, 0)')
cv.update()
'''
def mark(self, lines):
for i in range(5):
if i:
time.sleep(cpu.time)
if i % 2:
for l in lines:
cpu.ALUio[l] = True
self.show()
# print('self.' + l + '(cv, 1)')
# eval('self.' + l + '(cv, 1)')
else:
for l in lines:
cpu.ALUio[l] = False
self.show()
# print('self.' + l + '(cv, 0)')
# eval('self.' + l + '(cv, 0)')
cv.update()
'''
def init(self, cv: Canvas, root):
self.no(cv)
R_text = []
for i in range(4):
R_text.append(cpu.R(i))
R1 = tk.Label(root, text=R_text[0], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R1.place(x=365, y=85, height=30, width=150)
R2 = tk.Label(root, text=R_text[1], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R2.place(x=365, y=175, height=30, width=150)
R3 = tk.Label(root, text=R_text[2], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R3.place(x=365, y=265, height=30, width=150)
R4 = tk.Label(root, text=R_text[3], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R4.place(x=365, y=355, height=30, width=150)
# time.sleep(cpu.time)
# R1['text'] = 'RRRRRRRRRRRRRR1'
cv.update()
def ALU_R1(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 100, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 100, 270, 100, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 100, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 100, 270, 100, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def ALU_R2(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 180, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 180, 270, 180, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 180, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 180, 270, 180, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def ALU_R3(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 275, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 275, 270, 275, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 275, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 275, 270, 275, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def ALU_R4(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 355, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 355, 270, 355, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 355, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 355, 270, 355, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def R1_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 80, 340, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 340, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 80, 340, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 340, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R2_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 165, 340, 155, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 155, 340, 155, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 155, 310, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 165, 340, 155, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 155, 340, 155, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 155, 310, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R3_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 245, 340, 235, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 235, 340, 235, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 235, 310, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 245, 340, 235, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 235, 340, 235, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 235, 310, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R4_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 335, 340, 325, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 325, 340, 325, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 325, 310, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 335, 340, 325, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 325, 340, 325, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 325, 310, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R1_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 110, 340, 130, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 130, 340, 130, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 130, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 110, 340, 130, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 130, 340, 130, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 130, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def R2_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 200, 340, 220, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 220, 340, 220, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 220, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 200, 340, 220, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 220, 340, 220, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 220, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def R3_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 290, 340, 310, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 310, 340, 310, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 310, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 290, 340, 310, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 310, 340, 310, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 310, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def R4_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 380, 340, 395, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 395, 340, 395, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 395, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 380, 340, 395, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 395, 340, 395, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 395, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def opa(self, cv: Canvas):
#加号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.update()
def opb(self,cv:Canvas):
#减号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+ -', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def opc(self,cv:Canvas):
#乘号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+ - x', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+ -', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def opd(self,cv:Canvas):
#除号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+ - x ÷', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+ - x', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def no(self, cv:Canvas):
bx2, by2, bw2, bh2 = 115, 245, 100, 20
cv.create_rectangle(bx2, by2, bx2 + 100, by2 + 20, fill=BLACK, width=0)
cv.create_text(bx2, by2 - 25, text='完 成 运 算', font=('微软雅黑', 12), anchor=NW, fill=GREY)
cv.create_text(bx2, by2 - 5, text='+ - x ÷', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def finish(self, cv:Canvas):
bx2, by2, bw2, bh2 = 115, 245, 100, 20
cv.create_rectangle(bx2, by2, bx2 + 100, by2 + 20, fill=BLACK, width=0)
cv.create_text(bx2, by2 - 25, text='完 成 运 算', font=('微软雅黑', 12), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+ - x ÷', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
if __name__ == "__main__":
al = ALU()#实例化
al.init(cv, root)
al.line_init()
cv.update()
al.mark(['R3_ALU1'])
al.show()
# al.ALU_R1(cv, 1)
# al.ALU_R2(cv, 1)
# al.ALU_R3(cv, 1)
# al.ALU_R4(cv, 1)
# al.R1_ALU1(cv, 1)
# al.R2_ALU1(cv, 1)
# al.R3_ALU1(cv, 1)
# al.R4_ALU1(cv, 1)
# al.R1_ALU2(cv, 1)
# al.R2_ALU2(cv, 1)
# al.R3_ALU2(cv, 1)
# al.R4_ALU2(cv, 1)
def left1(event):
print(event.x,event.y)
cv.bind('<Button-1>', left1)
# cv.create_line(485,469+4.5,485+120+12,469+4.5,width=9,fill=RED,arrow=LAST)
# cv.create_rectangle(485, 469, 485 + 120, 469 + 9, fill=RED)
root.mainloop()

@ -0,0 +1,109 @@
from UI import *
# import tkinter as tk
# from Instruction4 import SimMem
from tkinter import messagebox
class ClockStep:
def __init__(self):# 绘制控制器背景板
round_rectangle(74, 434, 533, 642, r=25, fill=BACK, width=0)
round_rectangle(97, 525, 276, 627, r=25, fill=BLACK, width=0)
self.dx2, self.dy2, self.dw2, self.dh2 = 95, 455, 200, 40# 时钟数字间隔
self.R, self.x0, self.y0, self.dx = 15, 113, 473, 35#时钟大小
def clo_mark(self, cv:Canvas):# 依据ClockStep[]的值显示时钟元素
for i in range(6):#遍历六个时钟刻,绘制各个时钟
cv.create_oval(i * self.dx + self.x0 - self.R, self.y0 - self.R, i * self.dx + self.x0 + self.R,
self.y0 + self.R, width=1)#绘制时钟圆圈
if cpu.clo[i] == True:# 如果存储结构中clo[i]为1则该时钟高亮显示
cv.create_oval(self.x0 - self.R + self.dx * i, self.y0 - self.R,
self.x0 + self.R + self.dx * i, self.y0 + self.R, width=1, fill=RED)
else:# 如果存储结构中clo[i]为1则该时钟正常显示
cv.create_oval(self.x0 - self.R + self.dx * i, self.y0 - self.R,
self.x0 + self.R + self.dx * i, self.y0 + self.R,
width=1, fill=WHITE)
cv.create_text(self.dx2 + 12, self.dy2 + 3, text='1 2 3 4 5 6', font=('微软雅黑', 18), anchor=NW)# 重新绘制各时钟编号
cv.update()#更新画布
# 实现仿真控制器的各个屏幕元素同时依据ClockStep[]的值显示节拍元素
def show(self, cv: Canvas, root):
# 标记控制器各个部件名称
cv.create_text(95, 434, text='Clock', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
cv.create_text(342, 434, text='PC', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
cv.create_text(104, 500, text='SigCtrl', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
cv.create_text(342, 540, text='IR', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
# 根据当前控制器存储的值显示PC值
PC = tk.Label(root, text=cpu.PC(), fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
PC.place(x=335, y=455, height=30, width=150)
# 根据当前控制器存储的值显示IR值
IR = tk.Label(root, text=cpu.IR(), fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
IR.place(x=335, y=565, height=30, width=150)
# 绘制Clock背景板
bx2, by2, bw2, bh2 = 95, 455, 200, 40
round_rectangle(bx2, by2, bx2 + 215, by2 + 35, r=20, fill=WHITE, width=0)
# 绘制时钟名称
cv.create_text(bx2 + 12, by2 + 3, text='1 2 3 4 5 6', font=('微软雅黑', 18), anchor=NW, )
R = 15
x0 = 113
y0 = 473
dx = 35
# 绘制各个时钟
for i in range(6):
cv.create_oval(i * dx + x0 - R, y0 - R, i * dx + x0 + R, y0 + R, width=1)
self.clo_mark(cv)
# 更新画布
cv.update()
# 实现仿真控制器的各个屏幕元素同时依据ClockStep[]的值显示节拍元素
def init(self, cv:Canvas, root):
cv.create_text(95, 434, text='Clock', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)# 标记PC名称
cv.create_text(342, 434, text='PC', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)# 标记PC名称
cv.create_text(104, 500, text='SigCtrl', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)# 标记sig_ctrl名称
cv.create_text(342, 540, text='IR', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)# 标记IR名称
PC = tk.Label(root, text=cpu.PC(), fg=BLACK, font=('微软雅黑', 12), bg=WHITE)# 根据cpu存储的值显示PC值
PC.place(x=335, y=455, height=30, width=150)#放置PC值
IR = tk.Label(root, text=cpu.IR(), fg=BLACK, font=('微软雅黑', 12), bg=WHITE)# 根据cpu存储的值显示IR值
IR.place(x=335, y=565, height=30, width=150)#放置IR值
bx2, by2, bw2, bh2 = 95, 455, 200, 40# Clock背景板大小
round_rectangle(bx2, by2, bx2+215, by2+35, r=20, fill=WHITE, width=0)# 绘制Clock背景板
self.clo_mark(cv)#绘制六个时钟
cv.update()#更新面板
def jp(self, cv:Canvas, flag, num):
if flag:
cv.create_oval(self.x0 - self.R + self.dx * (num - 1), self.y0 - self.R, self.x0 + self.R + self.dx * (num - 1), self.y0 + self.R, width=1, fill=RED)
else:
cv.create_oval(self.x0 - self.R + self.dx * (num - 1), self.y0 - self.R, self.x0 + self.R + self.dx * (num - 1), self.y0 + self.R,
width=1, fill=WHITE)
cv.create_oval(self.x0 - self.R + self.dx * (num - 1), self.y0 - self.R, self.x0 + self.R + self.dx * (num - 1), self.y0 + self.R,
width=1)
cv.create_text(self.dx2 + 12, self.dy2 + 3, text='1 2 3 4 5 6', font=('微软雅黑', 18), anchor=NW)
cv.update()
if __name__ == "__main__":
cl=ClockStep()
# cl.clo_mark(cv)
cl.init(cv,root)
'''
def add_program():
cl.init(cv, root)
# cl.show(cv, 1)
# cl.show(cv, 2)
# cl.show(cv, 3)
# cl.show(cv, 4)
# cl.show(cv, 5)
# cl.show(cv, 6)
# si.np()
# cv.create_line(485,469+4.5,485+120+12,469+4.5,width=9,fill=RED,arrow=LAST)
# cv.create_rectangle(485, 469, 485 + 120, 469 + 9, fill=RED)
def left1(event):
print(event.x,event.y)
cv.bind('<Button-1>', left1)
'''
mainloop()

@ -0,0 +1,609 @@
# -*- encoding: utf-8 -*-
"""
@Author: packy945
@FileName: ExecInstructWithAnim.py
@DateTime: 2023/7/5 11:46
@SoftWare: PyCharm
"""
from RightPanel import *
from tkinter import scrolledtext
import collections
class ExecInstructWithAnim:# 自动执行指令带UI
def step_run(self):
'''
self.SimInstrCtrl中为SimInstrCtrl
SimInstrCtrl[] = {<opcode, ClockSeq, BasicSig>}
表示<指令码节拍该节拍下的一条信号>SimInstrCtrl[]用于保存一条指令在各个节拍下的信号
self.SimInstrCtrl为待执行的指令集合
:return:
'''
cur_clock = -1
while len(self.SimInstrCtrl): # 逐个取出指令集的指令并且执行
time.sleep(cpu.time) # 等待一段时间
self.auto_display() # 更新操作面板
cur_step = self.SimInstrCtrl.pop() # 取出下一条指令
if cur_clock == -1:# 记录当前运行的时钟刻
cur_clock = cur_step[1]
else: # 若时钟刻与记录的时钟刻不同则暂停程序的运行,等待下一次操作
if not cur_clock == cur_step[1]:
self.SimInstrCtrl.append(cur_step)
self.play = 0
break
cpu.CLO(cur_step[1] - 1) # 根据指令设置时钟刻
cl.clo_mark(cv) # 显示当前时钟刻
print(cur_step[2])
eval(cur_step[2]) # 执行当前指令
self.auto_display() # 更新操作面板
if self.play: # 若没有停机,解析当前指令
self.solve_step()
def SimGoto(self):
# 001001跳转指令 SimGoto
self.SimInstrCtrl.appendleft(['001001', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['001001', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['001001', 4, 'self.SimMaddr_Memory()']) # Maddr_Memory
self.SimInstrCtrl.appendleft(['001001', 4, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['001001', 4, 'self.SimSigCtrl_PC()'])
self.SimInstrCtrl.appendleft(['001001', 4, 'self.SimMcontent_PC()'])
self.SimInstrCtrl.appendleft(['001001', 4, 'self.ShowSimGoto()'])
def solve(self):
'''
解析一条指令
并将解析后的步骤加入到self.SimInstrCtrl中
:return:
'''
if self.play == 0:
return
self.auto_display()# 更新自动运行面板
code = cpu.code# 读取当前指令
if code == 0:# 000000取指令
self.get()
elif code % 16 == 1:# 000001取数指令(R1)
self.SimGet(code // 16)
elif code % 16 == 2:# 000010存数指令(R1)
self.SimPut(code // 16)
elif code % 16 == 3:# 000011加法指令
self.SimAddr(code // 16)
elif code % 16 == 4:# 000100乘法指令
self.SimProduct(code // 16)
elif code == 5:# 打印指令000101
self.SimPrint()
elif code == 6:# 停机指令000110
self.SimStop()
elif code % 16 == 7:# 000111减法指令 op = 1(R1)
self.SimMinus(code // 16)
elif code % 16 == 8:# 001000除法指令 op = 3(R1)
self.SimDiv(code // 16)
elif code == 9:# 001001跳转指令Simgoto
self.SimGoto()
else:#无法解析指令视为停机指令
self.SimStop()
# 更新画布
si.init(cv, root)
self.exec_instruct_with_anim()#开始执行指令
def exec_instruct_with_anim(self):
'''
self.SimInstrCtrl中为SimInstrCtrl
SimInstrCtrl[] = {<opcode, ClockSeq, BasicSig>}
表示<指令码节拍该节拍下的一条信号>SimInstrCtrl[]用于保存一条指令在各个节拍下的信号
self.SimInstrCtrl为待执行的指令集合
:return:
'''
while len(self.SimInstrCtrl):# 逐个取出指令集的指令并且执行
time.sleep(cpu.time)# 等待一段时间
self.auto_display()# 更新操作面板
cur_step = self.SimInstrCtrl.pop()# 取出下一条指令
cpu.CLO(cur_step[1] - 1)# 根据指令设置时钟刻
cl.clo_mark(cv)# 显示当前时钟刻
eval(cur_step[2])# 执行当前指令
self.auto_display()# 更新操作面板
if self.play:# 若没有停机,解析当前指令
self.solve()
def get(self):# 000000为取指令
self.SimInstrCtrl.appendleft(['000000', 1, 'self.PC_Maddr()']) # PC->Maddr
self.SimInstrCtrl.appendleft(['000000', 1, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['000000', 2, 'self.SimMaddr_Memory()']) # Maddr->Memory
self.SimInstrCtrl.appendleft(['000000', 2, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['000000', 2, 'self.SimSigCtrl_IR()']) # SigCtrl->IR
self.SimInstrCtrl.appendleft(['000000', 2, 'self.SimMcontent_IR()']) # Mcontent->IR
self.SimInstrCtrl.appendleft(['000000', 2, 'self.SimIR_SigCtrl()']) # IR->SigCtrl
self.SimInstrCtrl.appendleft(['000000', 3, 'self.SimPC_1()']) # PC+1
self.SimInstrCtrl.appendleft(['000000', 3, 'self.Showget()']) # 显示输出信息
def SimGet(self, n):
# 000001取数指令(R1) n = 0
# 010001取数指令(R2) n = 1
# 100001取数指令(R3) n = 2
# 110001取数指令(R4) n = 3
self.SimInstrCtrl.appendleft(['000001', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['000001', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['000001', 4, 'self.SimMaddr_Memory()']) # Maddr->Memory
self.SimInstrCtrl.appendleft(['000001', 4, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['000001', 4, 'self.SimSigCtrl_R(' + str(n) + ')']) # SigCtrl->Rn
self.SimInstrCtrl.appendleft(['000001', 4, 'self.SimMcontent_R(' + str(n) + ')']) # Mcontent->Rn
self.SimInstrCtrl.appendleft(['000001', 4, 'self.ShowSimget(' + str(n) + ')']) # 显示信息到输出框
def SimPut(self, n):
# 000010存数指令(R1)
# 010010存数指令(R2)
# 100010存数指令(R3)
# 110010存数指令(R4)
self.SimInstrCtrl.appendleft(['000010', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['000010', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['000010', 4, 'self.SimMaddr_Memory()']) # Maddr_Memory
self.SimInstrCtrl.appendleft(['000010', 4, 'self.SimR_Mcontent(' + str(n + 1) + ')']) # Rn->Mcontent
self.SimInstrCtrl.appendleft(['000010', 4, 'self.SimMcontent_Memory()']) # Mcontent_Memory
self.SimInstrCtrl.appendleft(['000010', 4, 'self.ShowSimPut(' + str(n + 1) + ')']) # 显示信息到输出框
def SimAddr(self, n):
# 加法指令000011
self.SimInstrCtrl.appendleft(['000011', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['000011', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['000011', 4, 'self.SimMaddr_Memory()']) # Maddr_Memory
self.SimInstrCtrl.appendleft(['000011', 4, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['000011', 4, 'self.SimMcontent_R(' + str(n + 1) + ')']) # Mcontent->Rn+1
self.SimInstrCtrl.appendleft(['000011', 5, 'self.SimR_ALU1(' + str(n) + ')']) # Rn->ALU1
self.SimInstrCtrl.appendleft(['000011', 5, 'self.SimR_ALU2(' + str(n) + ')']) # Rn+1->ALU2
self.SimInstrCtrl.appendleft(['000011', 5, 'self.SimSigCtrl_ALU']) # SigCtrl->ALU
self.SimInstrCtrl.appendleft(['000011', 6, 'self.SimALU_R("+",' + str(n) + ')']) # ALU->Rn,并计算Rn+Rn+1
self.SimInstrCtrl.appendleft(['000011', 6, 'self.ShowSimAddr(' + str(n) + ')']) # 输出信息到综合输出框
def SimProduct(self, n):
# 乘法指令000100
self.SimInstrCtrl.appendleft(['000100', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['000100', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['000100', 4, 'self.SimMaddr_Memory()']) # Maddr_Memory
self.SimInstrCtrl.appendleft(['000100', 4, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['000100', 4, 'self.SimMcontent_R(' + str(n + 1) + ')']) # Mcontent->Rn+1
self.SimInstrCtrl.appendleft(['000100', 5, 'self.SimR_ALU1(' + str(n) + ')']) # Rn->ALU1
self.SimInstrCtrl.appendleft(['000100', 5, 'self.SimR_ALU2(' + str(n) + ')']) # Rn+1->ALU2
self.SimInstrCtrl.appendleft(['000100', 5, 'self.SimSigCtrl_ALU']) # SigCtrl->ALU
self.SimInstrCtrl.appendleft(['000100', 6, 'self.SimALU_R("*",' + str(n) + ')']) # ALU->Rn,并计算Rn*Rn+1
self.SimInstrCtrl.appendleft(['000100', 6, 'self.ShowSimAddr(' + str(n) + ')']) # 输出信息到综合输出框
def __init__(self):
self.out = ''
self.play = 0
self.SimInstrCtrl = collections.deque()
def solve_step(self):
'''
解析一条指令
并将解析后的步骤加入到self.SimInstrCtrl中
:return:
'''
if self.play == 0:
return
self.auto_display()# 更新自动运行面板
code = cpu.code# 读取当前指令
if code == 0:# 000000取指令
self.get()
elif code % 16 == 1:# 000001取数指令(R1)
self.SimGet(code // 16)
elif code % 16 == 2:# 000010存数指令(R1)
self.SimPut(code // 16)
elif code % 16 == 3:# 000011加法指令
self.SimAddr(code // 16)
elif code % 16 == 4:# 000100乘法指令
self.SimProduct(code // 16)
elif code == 5:# 打印指令000101
self.SimPrint()
elif code == 6:# 停机指令000110
self.SimStop()
elif code % 16 == 7:# 000111减法指令 op = 1(R1)
self.SimMinus(code // 16)
elif code % 16 == 8:# 001000除法指令 op = 3(R1)
self.SimDiv(code // 16)
elif code == 9:# 001001跳转指令Simgoto
self.SimGoto()
else:#无法解析指令视为停机指令
self.SimStop()
# 更新画布
si.init(cv, root)
self.step_run()#开始执行指令
def out_clear(self):
self.out = ''
self.auto_display()
def PC_Maddr(self):
# PC_SIG
line.mark(['PC_SIG'])
PC = cpu.PC()
code = 0
for i in range(6):
code *= 2
code += int(PC[i])
cpu.code = code
si.init(cv, root)
line.mark(['PC_MAD'])
time.sleep(cpu.time)
PC = cpu.PC()
sum = 0
for i in range(11):
if PC[i + 6] == ' ':
continue
sum *= 2
sum += int(PC[i + 6])
cpu.MAD_int = sum
mem.show(cv, root)
si.init(cv, root)
def SimSigCtrl_Memory(self):
# 将地址传到MAD并且SIG发出信号
si.MEM(cv, 1)
line.mark(['SIG_MEM'])
time.sleep(cpu.time)
si.MEM(cv, 0)
def SimMaddr_Memory(self):
# 将MAD指定地址的内容取到MCO中
num = cpu.MAD_int
line.mark(["MAD_MEM"])
for j in range(20):
cpu.SimMARK[j] = False
cpu.SimMARK[num] = True
mem.mark(cv)
cpu.SimMARK[num] = False
mem.mark(cv)
def SimMemory_Mcontent(self):
line.mark(["MEM_MCO"])
cpu.MCO_int = cpu.str_int(cpu.SimMem[cpu.MAD_int])
mem.show(cv, root)
def SimSigCtrl_IR(self):
si.IR(cv, 1)
line.mark(['SIG_IR'])
time.sleep(cpu.time)
si.IR(cv, 0)
def SimMcontent_IR(self):
line.mark(['MCO_IR'])
cpu.IR_int = cpu.MCO_int
si.init(cv, root)
cl.init(cv, root)
def SimIR_SigCtrl(self):
line.mark(['IR_SIG'])
time.sleep(cpu.time)
IR = cpu.IR()
code = 0
for i in range(6):
code *= 2
code += int(IR[i])
# print(code)
cpu.code = code
si.init(cv, root)
def SimPC_1(self):
# PC+1
si.PC(cv, 1)
time.sleep(cpu.time)
line.mark(['SIG_PC'])
time.sleep(cpu.time)
si.PC(cv, 0)
cpu.PC_int += 1
cl.init(cv, root)
def Showget(self):
self.out += '取指令:' + '\n' + cpu.CODE() + '\n' # 添加打印信息到综合输出框
self.auto_display() # 显示输出信息
def SimIR_Maddr(self):
line.mark(['IR_MAD'])
IR = cpu.IR()
sum = 0
for i in range(11):
if IR[i + 6] == ' ':
continue
sum *= 2
sum += int(IR[i + 6])
# 将IR的后六位(操作对应的存储单元地址)取到Maddr中
cpu.MAD_int = sum
mem.show(cv, root)
si.init(cv, root)
def SimSigCtrl_R(self, n):
# SIG通知R1接受数据
eval('si.R' + str(n + 1) + '(cv, 1)')
eval("line.mark(['SIG_R" + str(n + 1) + "', 'MCO_R" + str(n + 1) + "'])")
time.sleep(cpu.time)
eval('si.R' + str(n + 1) + '(cv, 0)')
si.init(cv, root)
def SimMcontent_R(self, n):
# 将Mcontent中的数字存到R中
cpu.R_int[n] = cpu.MCO_int
alu.init(cv, root)
def ShowSimget(self, n):
# 输出信息到综合输出框
self.out += '取数指令:' + '\n'
self.out += 'SimMem[' + str(cpu.MAD_int) + '] -> R' + str(n + 1) + '\n'
cpu.code = 0
def SimR_Mcontent(self, n):
# R1->MCO
eval('line.mark(["R' + str(n + 1) + '_MCO"])')
# line.mark(["R1_MCO"])
cpu.MCO_int = cpu.R_int[n]
mem.show(cv, root)
alu.init(cv, root)
def SimMcontent_Memory(self):
time.sleep(cpu.time)
line.mark(["MCO_MEM"])
cpu.SimMem[cpu.MAD_int] = bin(cpu.MCO_int).replace('0b', '').zfill(16)
mem.show(cv, root)
time.sleep(cpu.time)
cpu.SimMARK[cpu.MAD_int] = False
mem.mark(cv)
def ShowSimPut(self, n):
# 输出信息到综合输出框
self.out += '存数指令:' + '\n'
self.out += 'R' + str(n + 1) + ' -> SimMem[' + str(cpu.MAD_int) + ']' + '\n'
cpu.code = 0
def SimR_ALU1(self, n):
# 高亮显示R1->ALU
eval('alu.mark(["R' + str(n + 1) + '_ALU1"])')
def SimR_ALU2(self, n):
# 高亮显示R2->ALU
eval('alu.mark(["R' + str(n + 2) + '_ALU2"])')
def SimSigCtrl_ALU(self):
# SIG向ALU运算器发出指令
si.ALU(cv, 1)
line.mark(["SIG_ALU"])
time.sleep(cpu.time)
si.ALU(cv, 0)
def SimALU_R(self, op, n):
#op运算符
#n存储器
eval('alu.mark(["ALU_R' + str(n + 1) + '"])')
# alu.mark(["ALU_R1"])
time.sleep(cpu.time)
# 将运算结果存入R1中
cpu.R_int[n] = eval('cpu.R_int[n]' + op + 'cpu.R_int[n + 1]')
alu.init(cv, root)
def ShowSimAddr(self, n):
# 输出信息到综合输出框
self.out += '加法指令:' + '\n'
self.out += 'R' + str(n + 1) + ' + R' + str(n + 2) + ' -> R' + str(n + 1) + '\n'
cpu.code = 0
def ShowSimProduct(self, n):
# 输出信息到综合输出框
self.out += '乘法指令:' + '\n'
self.out += 'R' + str(n + 1) + ' x R' + str(n + 2) + ' -> R' + str(n + 1) + '\n'
cpu.code = 0
def ShowPrint(self):
# 添加信息到综合输出框
self.out += '打印指令:' + '\n'
self.out += cpu.MCO() + '\n'
cpu.code = 0
def SimPrint(self):
# 打印指令000101
self.SimInstrCtrl.appendleft(['000101', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['000101', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['000101', 4, 'self.SimMaddr_Memory()']) # Maddr_Memory
self.SimInstrCtrl.appendleft(['000101', 4, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['000101', 4, 'self.ShowPrint()'])
def ShowStop(self):
self.out += '停机指令:\n'
self.out += '停机\n'
cpu.__init__()
self.play = 0
cpu.code = 0
def SimStop(self):
# 停机指令000110
self.SimInstrCtrl.appendleft(['000110', 4, 'self.ShowStop()'])
def ShowSimMinus(self, n):
# 输出信息到综合输出框
self.out += '减法指令:' + '\n'
self.out += 'R' + str(n + 1) + ' - R' + str(n + 2) + ' -> R' + str(n + 1) + '\n'
cpu.code = 0
def SimMinus(self, n):
# 减法指令000111
self.SimInstrCtrl.appendleft(['000111', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['000111', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['000111', 4, 'self.SimMaddr_Memory()']) # Maddr_Memory
self.SimInstrCtrl.appendleft(['000111', 4, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['000111', 4, 'self.SimMcontent_R(' + str(n + 1) + ')']) # Mcontent->Rn+1
self.SimInstrCtrl.appendleft(['000111', 5, 'self.SimR_ALU1(' + str(n) + ')']) # Rn->ALU1
self.SimInstrCtrl.appendleft(['000111', 5, 'self.SimR_ALU2(' + str(n) + ')']) # Rn+1->ALU2
self.SimInstrCtrl.appendleft(['000111', 5, 'self.SimSigCtrl_ALU']) # SigCtrl->ALU
self.SimInstrCtrl.appendleft(['000111', 6, 'self.SimALU_R("-",' + str(n) + ')']) # ALU->Rn,并计算Rn-Rn+1
self.SimInstrCtrl.appendleft(['000111', 6, 'self.ShowSimMinus(' + str(n) + ')']) # 输出信息到综合输出框
def ShowSimDiv(self, n):
# 输出信息到综合输出框
self.out += '除法指令:' + '\n'
self.out += 'R' + str(n + 1) + ' / R' + str(n + 2) + ' -> R' + str(n + 1) + '\n'
cpu.code = 0
def SimDiv(self, n):
# 除法指令001000
self.SimInstrCtrl.appendleft(['001000', 3, 'self.SimIR_Maddr()']) # IR->Maddr
self.SimInstrCtrl.appendleft(['001000', 3, 'self.SimSigCtrl_Memory()']) # SigCtrl->Memory
self.SimInstrCtrl.appendleft(['001000', 4, 'self.SimMaddr_Memory()']) # Maddr_Memory
self.SimInstrCtrl.appendleft(['001000', 4, 'self.SimMemory_Mcontent()']) # Memory->Mcontent
self.SimInstrCtrl.appendleft(['001000', 4, 'self.SimMcontent_R(' + str(n + 1) + ')']) # Mcontent->Rn+1
self.SimInstrCtrl.appendleft(['001000', 5, 'self.SimR_ALU1(' + str(n) + ')']) # Rn->ALU1
self.SimInstrCtrl.appendleft(['001000', 5, 'self.SimR_ALU2(' + str(n) + ')']) # Rn+1->ALU2
self.SimInstrCtrl.appendleft(['001000', 5, 'self.SimSigCtrl_ALU']) # SigCtrl->ALU
self.SimInstrCtrl.appendleft(['001000', 6, 'self.SimALU_R("/",' + str(n) + ')']) # ALU->Rn,并计算Rn/Rn+1
self.SimInstrCtrl.appendleft(['001000', 6, 'self.ShowSimDiv(' + str(n) + ')']) # 输出信息到综合输出框
def SimSigCtrl_PC(self):
# SIG通知PC接受数据
si.PC(cv, 1)
line.mark(['SIG_PC'])
time.sleep(cpu.time)
si.PC(cv, 0)
def SimMcontent_PC(self):
line.mark(['MCO_PC'])
cpu.PC_int = cpu.MCO_int
si.init(cv, root)
def ShowSimGoto(self):
# 添加信息到综合输出框
self.out += '跳转指令:' + '\n'
self.out += 'SimMem[' + str(cpu.MAD_int) + ']' + '\n'
cpu.code = 0
def Begin_step(self):
self.play = 1
self.step_run()
# 执行一条指令(不仅改变寄存器,并且使得信号线高亮显示)
def Begin(self):
self.play = 1
self.exec_instruct_with_anim()
def auto_display(self):
# 自动运行窗口
for w in root_right.winfo_children():
w.destroy()
# 清除右侧组件
lab = tk.Label(root_right, text="执行指令(带UI)", bg=WHITE, fg=BLACK, font=('微软雅黑', 18))
lab.place(relx=0, rely=0.02, width=250, height=30)
y0 = 0.1
start = tk.Button(root_right, text="自动运行程序(连续)", bg=WHITE, fg=BLACK, font=('微软雅黑', 12),
command=lambda: self.Begin())
start.place(relx=0.1, rely=0.0 + y0, width=200, height=30)
start = tk.Button(root_right, text="自动运行程序(分节拍)", bg=WHITE, fg=BLACK, font=('微软雅黑', 12),
command=lambda: self.Begin_step())
start.place(relx=0.1, rely=0.05 + y0, width=200, height=30)
lab = tk.Label(root_right, text="当前节拍:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.1 + y0, width=120, height=30)
clo = 0
for i in range(6):
if cpu.clo[i]:
clo = i
break
lab2 = tk.Label(root_right, text=str(clo + 1), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.4, rely=0.1 + y0, width=50, height=30)
y0 += 0.1
lab = tk.Label(root_right, text="当前操作码:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.1 + y0, width=120, height=30)
lab2 = tk.Label(root_right, text=cpu.CODE(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.15 + y0, width=200, height=30)
command = ['取指令', '取数指令', '存数指令', '加法指令', '乘法指令', '打印指令', '停机指令', '减法指令',
'除法指令', '跳转指令']
lab = tk.Label(root_right, text="正在执行指令:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.2 + y0, width=120, height=30)
lab2 = tk.Label(root_right, text=command[cpu.code % 16], bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.25 + y0, width=200, height=30)
lab = tk.Label(root_right, text="综合输出框:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.3 + y0, width=120, height=30)
scr = scrolledtext.ScrolledText(root_right, fg=BLACK, font=('微软雅黑', 12,))
scr.place(relx=0.1, rely=0.35 + y0, width=200, height=300)
scr.insert('end', self.out) # 末尾插入
scr.config(state=DISABLED) # 设置文本框为 “不能编辑”
scr.see(END) # 设置视图为最后一行
B = tk.Button(root_right, text="清空", bg=WHITE, fg=BLACK, font=('微软雅黑', 12),
command=lambda: self.out_clear())
B.place(relx=0.5, rely=0.3 + y0, width=50, height=30)
def get_mem(self, num):
# 将MAD指定地址的内容取到MCO中
line.mark(["MAD_MEM"])
for j in range(20):
cpu.SimMARK[j] = False
cpu.SimMARK[num] = True
mem.mark(cv)
cpu.SimMARK[num] = False
mem.mark(cv)
def exec_test_program():
Program = [
'0000010000001000',
'0000110000001010',
'0001000000001001',
'0001000000001000',
'0000110000001011',
'0000100000001100',
'0001010000001100',
'0001100000000000',
'0000000000000101',
'0000000000001001',
'0000000000000110',
'0000000000001000'
] # 输入题目要求的程序
for i in range(len(Program)):
cpu.SimMem[i] = Program[i] # 将上述程序装载入仿真存储器中
cpu.PC_int = 0 # 设置PC值为“00000000 00000000”
init(cv) # 初始化cpu
a = ExecInstructWithAnim()
a.auto_display()
root.mainloop()
if __name__ == '__main__':
cpu.time = 0.1
exec_test_program()

@ -0,0 +1,429 @@
# -*- encoding: utf-8 -*-
"""
@Author: packy945
@FileName: ExecInstructWithText.py
@DateTime: 2023/6/30 16:49
@SoftWare: PyCharm
"""
from RightPanel import *
from tkinter import scrolledtext
class ExecInstructWithText:
def __init__(self):
self.out = '' # 初始化综合输出框
self.play = 0 # 初始化程序运行状态
def SimDiv(self, n):
# 除法指令001000
self.clock(3) # 第三个节拍
self.SimIR_MAD() # 将地址从IR传到MAD并且SIG发出信号
self.clock(4) # 第四个节拍
self.get_mem(cpu.MAD_int) # 将MAD指定地址的内容取到MCO中
self.SimSIG_Rn(n + 2) # SIG通知R2接受数据
# 开始计算
self.clock(5) # 第五个节拍
self.SimR_ALU(n) # R1 R2 进入ALU中
self.clock(6) # 第六个节拍
ans = cpu.R_int[n] // cpu.R_int[n + 1] # 进行计算
self.SimANS_Rn(ans, n) # ALU将计算结果存到R[n]
self.out += '除法指令:' + '\n' # 输出信息到综合输出框
self.out += 'R' + str(n + 1) + ' / R' + str(n + 2) + ' -> R' + str(n + 1) + '\n' # 输出信息到综合输出框
def SimMinus(self, n):
# 减法指令000111
self.clock(3) # 第三个节拍
self.SimIR_MAD() # 将地址从IR传到MAD并且SIG发出信号
self.clock(4) # 第四个节拍
self.get_mem(cpu.MAD_int) # 将MAD指定地址的内容取到MCO中
self.SimSIG_Rn(n + 2) # SIG通知R2接受数据
# 开始计算
self.clock(5) # 第五个节拍
self.SimR_ALU(n) # R1 R2 进入ALU中
self.clock(6) # 第六个节拍
ans = cpu.R_int[n] - cpu.R_int[n + 1] # 进行计算
self.SimANS_Rn(ans, n) # ALU将计算结果存到R[n]
self.out += '减法指令:' + '\n' # 输出信息到综合输出框
self.out += 'R' + str(n + 1) + ' - R' + str(n + 2) + ' -> R' + str(n + 1) + '\n' # 输出信息到综合输出框
def SimStop(self):
# 停机指令000110
self.out += '停机指令:\n' + '停机\n' # 添加打印信息到综合输出框
cpu.__init__() # 重置cpu各项数据
init(cv) # 显示复位
self.play = 0 # 标记为未运行
self.auto_display() # 显示输出信息
def Simprint(self):
# 打印指令000101
self.clock(3) # 第三个节拍
self.SimIR_MAD() # 将地址从IR传到MAD并且SIG发出信号
self.clock(4) # 第四个节拍
self.get_mem(cpu.MAD_int) # 将MAD指定地址的内容取到MCO中
self.out += '打印指令:' + '\n' + cpu.MCO() + '\n' # 添加打印信息到综合输出框
self.auto_display() # 显示输出信息
def SimProduct(self, n):
# 乘法指令000100
self.clock(3) # 第三个节拍
self.SimIR_MAD() # 将地址从IR传到MAD并且SIG发出信号
self.clock(4) # 第四个节拍
self.get_mem(cpu.MAD_int) # 将MAD指定地址的内容取到MCO中
self.SimSIG_Rn(n + 2) # SIG通知R2接受数据
# 开始计算
self.clock(5) # 第五个节拍
self.SimR_ALU(n) # R1 R2 进入ALU中
self.clock(6) # 第六个节拍
ans = cpu.R_int[n] * cpu.R_int[n + 1] # 进行计算
self.SimANS_Rn(ans, n) # ALU将计算结果存到R[n]
self.out += '乘法指令:' + '\n' # 输出信息到综合输出框
self.out += 'R' + str(n + 1) + ' x R' + str(n + 2) + ' -> R' + str(n + 1) + '\n' # 输出信息到综合输出框
def SimAddr(self, n):
# 加法指令000011
self.clock(3) # 第三个节拍
self.SimIR_MAD() # 将地址从IR传到MAD并且SIG发出信号
self.clock(4) # 第四个节拍
self.get_mem(cpu.MAD_int) # 将MAD指定地址的内容取到MCO中
self.SimSIG_Rn(n + 2) # SIG通知Rn+1接受数据
# 开始计算
self.clock(5) # 第五个节拍
self.SimR_ALU(n) # R[n] R[n+1] 进入ALU中
self.clock(6) # 第六个节拍
# 记录计算结果到ans中
ans = cpu.R_int[n] + cpu.R_int[n + 1]
self.SimANS_Rn(ans, n) # ALU将计算结果存到Rn
self.out += '加法指令:' + '\n' # 输出信息到综合输出框
self.out += 'R' + str(n + 1) + ' + R' + str(n + 2) + ' -> R' + str(n + 1) + '\n' # 输出信息到综合输出框
def SimPut(self, n):
# 000010存数指令(R1)
# 010010存数指令(R2)
# 100010存数指令(R3)
# 110010存数指令(R4)
self.clock(3) # 第三个节拍
self.SimIR_MAD() # 将地址从IR传到MAD并且SIG发出信号
self.clock(4) # 第四个节拍
self.mark_MEM() # 标记MAD对应的存储单元
self.SinRn_MCO(n) # Rn->MCO
self.SimMCO_MEM() # MCO->对应的存储单元
self.out += '存数指令:' + '\n' # 输出信息到综合输出框
self.out += 'R' + str(n + 1) + ' -> SimMem[' + str(cpu.MAD_int) + ']' + '\n' # 输出信息到综合输出框
def SimGet(self, n):
# 000001取数指令(R1) n = 0
# 010001取数指令(R2) n = 1
# 100001取数指令(R3) n = 2
# 110001取数指令(R4) n = 3
self.clock(3) # 第三个节拍
self.SimIR_MAD() # 将地址从IR传到MAD并且SIG发出信号
self.clock(4) # 第四个节拍
self.get_mem(cpu.MAD_int) # 将MAD指定地址的内容取到MCO中
self.SimSIG_Rn(n + 1) # SIG通知R1接受数据
self.out += '取数指令:' + '\n' # 输出信息到综合输出框
self.out += 'SimMem[' + str(cpu.MAD_int) + '] -> R' + str(n + 1) + '\n' # 输出信息到综合输出框
def clock(self, n):
cpu.CLO(n - 1) # 更改cpu内时钟刻的信息
cl.clo_mark(cv) # 根据cpu显示时钟刻
time.sleep(cpu.time) # 定格一段时间
def print_mark(self, lines):
for l in lines: # 遍历要输出的线的信息
self.out += l.replace('_', '->') + '\n' # 将信息显示到综合输出框
self.auto_display() # 显示综合输出框
time.sleep(cpu.time) # 定格一段时间
def auto_display(self):
# 自动运行窗口
for w in root_right.winfo_children():
w.destroy()
# 清除右侧组件
# 右侧窗口标题
lab = tk.Label(root_right, text="执行指令(仅寄存器)", bg=WHITE, fg=BLACK, font=('微软雅黑', 18))
lab.place(relx=0, rely=0.02, width=250, height=30)
y0 = 0.1
# 开始运行程序按钮
start = tk.Button(root_right, text="自动运行程序", bg=WHITE, fg=BLACK, font=('微软雅黑', 12),
command=lambda: self.exec_instruct_with_text())
start.place(relx=0.1, rely=0.05 + y0, width=200, height=30)
if self.play == 1:
start['state'] = DISABLED
else:
start['state'] = NORMAL
# 显示当时正在进行的操作码(INSCODE)
lab = tk.Label(root_right, text="当前操作码:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.1 + y0, width=120, height=30)
lab2 = tk.Label(root_right, text=cpu.CODE(), bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.15 + y0, width=200, height=30)
# 显示当时正在进行的操作名称
command = ['取指令', '取数指令', '存数指令', '加法指令', '乘法指令', '打印指令', '停机指令', '减法指令',
'除法指令', '跳转指令']
lab = tk.Label(root_right, text="正在执行指令:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.2 + y0, width=120, height=30)
lab2 = tk.Label(root_right, text=command[cpu.code % 16], bg=BLUE, fg=BLACK, font=('微软雅黑', 12))
lab2.place(relx=0.1, rely=0.25 + y0, width=200, height=30)
# 综合输出框,进行打印指令与停机指令的相应输出
lab = tk.Label(root_right, text="综合输出框:", bg=WHITE, fg=BLACK, font=('微软雅黑', 12), anchor=W)
lab.place(relx=0.1, rely=0.3 + y0, width=120, height=30)
scr = scrolledtext.ScrolledText(root_right, fg=BLACK, font=('微软雅黑', 12,))
scr.place(relx=0.1, rely=0.35 + y0, width=200, height=300)
scr.insert('end', self.out) # 末尾插入
scr.config(state=DISABLED) # 设置文本框为 “不能编辑”
scr.see(END) # 设置视图为最后一行
# lab2 = tk.Label(root_right, text=self.out, bg=BLUE, fg=BLACK, font=('微软雅黑', 12), anchor=N)
# lab2.place(relx=0.1, rely=0.35 + y0, width=200, height=150)
# 清空中和输出框按钮
B = tk.Button(root_right, text="清空", bg=WHITE, fg=BLACK, font=('微软雅黑', 12),
command=lambda: self.out_clear())
B.place(relx=0.5, rely=0.3 + y0, width=50, height=30)
def out_clear(self):
self.out = ''
self.auto_display()
# 执行一条指令
def exec_instruct_with_text(self):
while 1:
self.play = 1 # 更新系统状态为正在运行
self.auto_display() # 显示自动执行的输出框
self.get() # 执行取数指令时钟前2个节拍
code = cpu.code # 读取当前指令
self.auto_display() # 显示自动执行的输出框
if code % 16 == 1: # 000001取数指令(R1)
self.SimGet(code // 16)
elif code % 16 == 2: # 000010存数指令(R1)
self.SimPut(code // 16)
elif code % 16 == 3: # 000011加法指令 op = 0(R1)
self.SimAddr(code // 16)
elif code % 16 == 4: # 000100乘法指令 op = 2
self.SimProduct(code // 16)
elif code == 5: # 打印指令000101
self.Simprint()
elif code == 6: # 停机指令000110
self.SimStop()
break
elif code % 16 == 7: # 000111减法指令 op = 1
self.SimMinus(code // 16)
elif code % 16 == 8: # 001000除法指令 op = 3
self.SimDiv(code // 16)
else:
self.SimStop()
break
cpu.code = 0 # 更新指令为取数指令
si.init(cv, root)
def SimIR_MAD(self):
si.MEM(cv, 1)
self.print_mark(['IR_MAD', 'SIG_MEM'])
time.sleep(cpu.time)
si.MEM(cv, 0)
IR = cpu.IR()
sum = 0
for i in range(11):
if IR[i + 6] == ' ':
continue
sum *= 2
sum += int(IR[i + 6])
cpu.MAD_int = sum
mem.show(cv, root)
si.init(cv, root)
def SimMEM_MCO(self):
cpu.MCO_int = cpu.str_int(cpu.SimMem[cpu.MAD_int])
mem.show(cv, root)
def SimPC_SIG(self):
self.print_mark(['PC_SIG'])
PC = cpu.PC()
code = 0
for i in range(6):
code *= 2
code += int(PC[i])
cpu.code = code
si.init(cv, root)
def SimPC_MAD(self):
PC = cpu.PC()
sum = 0
for i in range(11):
if PC[i + 6] == ' ':
continue
sum *= 2
sum += int(PC[i + 6])
cpu.MAD_int = sum
mem.show(cv, root)
si.init(cv, root)
def SimMCO_IR(self):
time.sleep(cpu.time)
si.IR(cv, 1)
self.print_mark(['MCO_IR', 'SIG_IR'])
time.sleep(cpu.time)
si.IR(cv, 0)
cpu.IR_int = cpu.MCO_int
si.init(cv, root)
cl.init(cv, root)
def SimIR_SIG(self):
self.print_mark(['IR_SIG'])
time.sleep(cpu.time)
IR = cpu.IR()
code = 0
for i in range(6):
code *= 2
code += int(IR[i])
# print(code)
cpu.code = code
si.init(cv, root)
def PC_1(self):
si.PC(cv, 1)
time.sleep(cpu.time)
self.print_mark(['SIG_PC'])
time.sleep(cpu.time)
si.PC(cv, 0)
cpu.PC_int += 1
cl.init(cv, root)
def SimSIG_MAD(self):
si.MEM(cv, 1)
self.print_mark(['PC_MAD', 'SIG_MEM'])
time.sleep(cpu.time)
si.MEM(cv, 0)
def get(self): # 000000为取指令
self.clock(1) # 第一个节拍
self.SimPC_SIG() # PC->SIG
self.SimSIG_MAD() # 将地址传到MAD并且SIG发出信号
self.SimPC_MAD() # PC传参数给MAD
self.clock(2) # 第二个节拍
self.get_mem(cpu.MAD_int) # 将MAD指定地址的内容取到MCO中
self.SimMCO_IR() # mco把值传给IR
self.SimIR_SIG() # IR把CODE值传给SIG
self.clock(3) # 第三个节拍
self.PC_1() # PC+1
self.out += '取指令:' + '\n' + cpu.CODE() + '\n' # 添加打印信息到综合输出框
self.auto_display() # 显示输出信息
def SimSIG_Rn(self, n):
# SIG->R[n]
eval('si.R' + str(n) + '(cv, 1)')
eval("self.print_mark(['SIG_R" + str(n) + "', 'MCO_R" + str(n) + "'])")
time.sleep(cpu.time)
eval('si.R' + str(n) + '(cv, 0)')
si.init(cv, root)
# 将Mcontent中的数字存到R中
cpu.R_int[n - 1] = cpu.MCO_int
alu.init(cv, root)
def mark_MEM(self):
self.print_mark(["MAD_MEM"])
for j in range(20):
cpu.SimMARK[j] = False
cpu.SimMARK[cpu.MAD_int] = True
mem.mark(cv)
def SinRn_MCO(self, n):
eval('self.print_mark(["R' + str(n + 1) + '_MCO"])')
# self.print_mark(["R1_MCO"])
cpu.MCO_int = cpu.R_int[n]
mem.show(cv, root)
alu.init(cv, root)
time.sleep(cpu.time)
self.print_mark(["MCO_MEM"])
def SimMCO_MEM(self):
cpu.SimMem[cpu.MAD_int] = bin(cpu.MCO_int).replace('0b', '').zfill(16)
mem.show(cv, root)
time.sleep(cpu.time)
cpu.SimMARK[cpu.MAD_int] = False
mem.mark(cv)
def SimR_ALU(self, n):
# 高亮显示R1->ALU, R2->ALU
eval('self.print_mark(["R' + str(n + 1) + '_ALU1", "R' + str(n + 2) + '_ALU2"])')
time.sleep(cpu.time)
alu.init(cv, root)
time.sleep(cpu.time)
# SIG向ALU运算器发出指令
self.print_mark(["SIG_ALU"])
def SimANS_Rn(self, ans, n):
eval('self.print_mark(["ALU_R' + str(n + 1) + '"])')
time.sleep(cpu.time)
# 将运算结果存入R1中
cpu.R_int[n] = ans
alu.init(cv, root)
def get_mem(self, num):
# 将MAD指定地址的内容取到MCO中
self.print_mark(["MAD_MEM"])
for j in range(20):
cpu.SimMARK[j] = False
cpu.SimMARK[num] = True
mem.mark(cv)
self.print_mark(["MEM_MCO"])
self.SimMEM_MCO()
cpu.SimMARK[num] = False
mem.mark(cv)
def exec_test_program():
Program = [
'0000010000001000',
'0000110000001010',
'0001000000001001',
'0001000000001000',
'0000110000001011',
'0000100000001100',
'0001010000001100',
'0001100000000000',
'0000000000000101',
'0000000000001001',
'0000000000000110',
'0000000000001000'
] # 输入题目要求的程序
for i in range(len(Program)):
cpu.SimMem[i] = Program[i] # 将上述程序装载入仿真存储器中
cpu.PC_int = 0 # 设置PC值为“00000000 00000000”
init(cv) # 初始化cpu
run_text = ExecInstructWithText() # 实例化类
run_text.auto_display() # 开始执行完整一段程序
root.mainloop() # 执行可视化
if __name__ == "__main__":
cpu.time = 0.05
exec_test_program()

@ -0,0 +1,463 @@
from UI import *
# import tkinter as tk
class Print_line:
def show(self):
lines = ['PC_MAD', 'SIG_MEM', 'SIG_IR', 'IR_SIG', 'IR_MCO', 'MCO_IR', 'MCO_MEM', 'MEM_MCO', 'MAD_MEM',
'PC_SIG', 'SIG_PC', 'MCO_PC', 'IR_MAD', 'SIG_R1', 'SIG_R2', 'SIG_R3', 'SIG_R4', 'MCO_R1',
'MCO_R2', 'MCO_R3', 'MCO_R4', 'R1_MCO', 'R2_MCO', 'R3_MCO', 'R4_MCO', 'SIG_ALU']
for l in lines:
if cpu.SigObj[l][1] == False:
eval('self.' + l + '(cv, -1)')
for l in lines:
if cpu.SigObj[l][1] == True:
eval('self.' + l + '(cv, 1)')
# print(cpu.SigObj[l][1])
def all(self):
lines = ['PC_MAD', 'SIG_MEM', 'SIG_IR', 'IR_SIG', 'IR_MCO', 'MCO_IR', 'MCO_MEM', 'MEM_MCO', 'MAD_MEM',
'PC_SIG', 'SIG_PC', 'MCO_PC', 'IR_MAD', 'SIG_R1', 'SIG_R2', 'SIG_R3', 'SIG_R4', 'MCO_R1',
'MCO_R2', 'MCO_R3', 'MCO_R4', 'R1_MCO', 'R2_MCO', 'R3_MCO', 'R4_MCO', 'SIG_ALU']
for l in lines:
eval('self.' + l + '(cv, 1)')
def init(self):
lines = ['PC_MAD', 'SIG_MEM', 'SIG_IR', 'IR_SIG', 'IR_MCO', 'MCO_IR', 'MCO_MEM', 'MEM_MCO', 'MAD_MEM',
'PC_SIG', 'SIG_PC', 'MCO_PC', 'IR_MAD', 'SIG_R1', 'SIG_R2', 'SIG_R3', 'SIG_R4', 'MCO_R1',
'MCO_R2', 'MCO_R3', 'MCO_R4', 'R1_MCO', 'R2_MCO', 'R3_MCO', 'R4_MCO', 'SIG_ALU']
# for l in lines:
# eval('self.' + l + '(cv, 0)')
for l in lines:
eval('self.' + l + '(cv, -1)')
def mark(self, lines):
for i in range(5):
if i:
time.sleep(cpu.time)
if i % 2:
for l in lines:
# print('self.' + l + '(cv, 1)')
# self.init()
eval('self.' + l + '(cv, 1)')
else:
for l in lines:
# print('self.' + l + '(cv, 0)')
# self.init()
eval('self.' + l + '(cv, -1)')
cv.update()
def PC_MAD(self, cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(500, 469 + 4.5, 480 + 120 + 12, 469 + 4.5, width=9, arrow=LAST, capstyle=ROUND, fill=color1)
else:
cv.create_line(500, 469 + 4.5, 480 + 120 + 12, 469 + 4.5, width=9, arrow=LAST, capstyle=ROUND, fill=NOMARK)
cv.update()
def SIG_MEM(self, cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(185 + 4.5, 635, 185 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(185 + 4.5, 660, 635, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(635, 550 - 5, 635, 660, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(185 + 4.5, 635, 185 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(185 + 4.5, 660, 635, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(635, 550 - 5, 635, 660, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def SIG_IR(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(281 + 10, 575 + 4.5, 334, 575 + 4.5, width=9, arrow=LAST, capstyle=ROUND, fill=color1)
else:
cv.create_line(281 + 10, 575 + 4.5, 334, 575 + 4.5, width=9, arrow=LAST, capstyle=ROUND, fill=NOMARK)
cv.update()
def IR_SIG(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(281, 575 + 4.5, 334 - 10, 575 + 4.5, width=9, arrow=FIRST, capstyle=ROUND, fill=color1)
else:
cv.create_line(281, 575 + 4.5, 334 - 10, 575 + 4.5, width=9, arrow=FIRST, capstyle=ROUND, fill=NOMARK)
cv.update()
def IR_MCO(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(500, 580, 580, 580, width=9, capstyle=ROUND, fill=color1)
cv.create_line(580, 180 + 4.5, 580, 580, width=9, capstyle=ROUND, fill=color1)
cv.create_line(580, 180 + 4.5, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 145, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(500, 580, 580, 580, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(580, 180 + 4.5, 580, 580, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(580, 180 + 4.5, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 145, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def MCO_IR(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(490, 580, 580, 580, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
cv.create_line(580, 180 + 4.5, 580, 580, width=9, capstyle=ROUND, fill=color1)
cv.create_line(580, 180 + 4.5, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 145, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=color1)
else:
cv.create_line(490, 580, 580, 580, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.create_line(580, 180 + 4.5, 580, 580, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(580, 180 + 4.5, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 145, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.update()
def MCO_MEM(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(780, 245, 780, 273, width=9, capstyle=ROUND, arrow=LAST, fill=color1)
else:
cv.create_line(780, 245, 780, 273, width=9, capstyle=ROUND, arrow=LAST, fill=NOMARK)
cv.update()
def MEM_MCO(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(820, 245, 820, 273, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(820, 245, 820, 273, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def PC_SIG(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(230 + 5, 510, 230 + 5, 525, width=10, arrow=LAST, capstyle=ROUND, fill=color1)
cv.create_line(230 + 5, 505 + 5, 230 + 180, 505 + 5, width=10, capstyle=ROUND, fill=color1)
cv.create_line(405 + 5, 497, 405 + 5, 510, width=10, capstyle=ROUND, fill=color1)
else:
cv.create_line(230 + 5, 510, 230 + 5, 525, width=10, arrow=LAST, capstyle=ROUND, fill=NOMARK)
cv.create_line(230 + 5, 505 + 5, 230 + 180, 505 + 5, width=10, capstyle=ROUND, fill=NOMARK)
cv.create_line(405 + 5, 497, 405 + 5, 510, width=10, capstyle=ROUND, fill=NOMARK)
cv.update()
def SIG_PC(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(230 + 5, 510, 230 + 5, 518, width=10, capstyle=ROUND, fill=color1)
cv.create_line(230 + 5, 505 + 5, 230 + 180, 505 + 5, width=10, capstyle=ROUND, fill=color1)
cv.create_line(405 + 5, 490, 405 + 5, 490 + 20, width=10, arrow=FIRST, capstyle=ROUND, fill=color1)
else:
cv.create_line(230 + 5, 510, 230 + 5, 518, width=10, capstyle=ROUND, fill=NOMARK)
cv.create_line(230 + 5, 505 + 5, 230 + 180, 505 + 5, width=10, capstyle=ROUND, fill=NOMARK)
cv.create_line(405 + 5, 490, 405 + 5, 490 + 20, width=10, arrow=FIRST, capstyle=ROUND, fill=NOMARK)
cv.update()
def IR_MAD(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(500, 580, 580, 580, width=9, capstyle=ROUND, fill=color1)
cv.create_line(580, 510, 580, 580, width=9, capstyle=ROUND, fill=color1)
cv.create_line(580, 510, 610, 510, width=9, capstyle=ROUND, fill=color1, arrow=LAST)
else:
cv.create_line(500, 580, 580, 580, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(580, 510, 580, 580, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(580, 510, 610, 510, width=9, capstyle=ROUND, fill=NOMARK, arrow=LAST)
cv.update()
def SIG_R1(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 30, 620, 103, width=9, capstyle=ROUND, fill=color1)
cv.create_line(515, 103, 620, 103, width=10, arrow=FIRST, capstyle=ROUND, fill=color1)
else:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 30, 620, 103, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(515, 103, 620, 103, width=10, arrow=FIRST, capstyle=ROUND, fill=NOMARK)
cv.update()
def SIG_R2(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 30, 620, 120, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 120, 620, 120, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 120, 538, 190, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 190, 515, 190, width=9, capstyle=ROUND, arrow=LAST, fill=color1)
else:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 30, 620, 120, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 120, 620, 120, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 120, 538, 190, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 190, 515, 190, width=9, capstyle=ROUND, arrow=LAST, fill=NOMARK)
cv.update()
def SIG_R3(self, cv: tk.Canvas, flag):
if flag == 1:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 30, 620, 140, width=9, capstyle=ROUND, fill=color1)
cv.create_line(550, 140, 620, 140, width=9, capstyle=ROUND, fill=color1)
cv.create_line(550, 140, 550, 280, width=9, capstyle=ROUND, fill=color1)
cv.create_line(515, 280, 550, 280, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 30, 620, 140, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(550, 140, 620, 140, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(550, 140, 550, 280, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(515, 280, 550, 280, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def SIG_R4(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=color1)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 30, 620, 160, width=9, capstyle=ROUND, fill=color1)
cv.create_line(565, 160, 620, 160, width=9, capstyle=ROUND, fill=color1)
cv.create_line(565, 160, 565, 370, width=9, capstyle=ROUND, fill=color1)
cv.create_line(515, 370, 565, 370, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(120 + 4.5, 635, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 660, 120 + 4.5, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 40, 660, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(40, 30, 620, 30, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 30, 620, 160, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(565, 160, 620, 160, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(565, 160, 565, 370, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(515, 370, 565, 370, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def MCO_R1(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 145, 620, 103, width=9, capstyle=ROUND, fill=color1)
cv.create_line(515, 103, 620, 103, width=10, arrow=FIRST, capstyle=ROUND, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 145, 620, 103, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(515, 103, 620, 103, width=10, arrow=FIRST, capstyle=ROUND, fill=NOMARK)
cv.update()
def R1_MCO(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=color1, arrow=FIRST)
cv.create_line(620, 145, 620, 103, width=9, capstyle=ROUND, fill=color1)
cv.create_line(515 + 10, 103, 620, 103, width=10, capstyle=ROUND, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=NOMARK, arrow=FIRST)
cv.create_line(620, 145, 620, 103, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(515 + 10, 103, 620, 103, width=10, capstyle=ROUND, fill=NOMARK)
cv.update()
def MCO_R2(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 145, 620, 120, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 120, 620, 120, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 120, 538, 190, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 190, 515, 190, width=9, capstyle=ROUND, arrow=LAST, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 145, 620, 120, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 120, 620, 120, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 120, 538, 190, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 190, 515, 190, width=9, capstyle=ROUND, arrow=LAST, fill=NOMARK)
cv.update()
def R2_MCO(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=color1, arrow=FIRST)
cv.create_line(620, 145, 620, 120, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 120, 620, 120, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 120, 538, 190, width=9, capstyle=ROUND, fill=color1)
cv.create_line(538, 190, 525, 190, width=9, capstyle=ROUND, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=NOMARK, arrow=FIRST)
cv.create_line(620, 145, 620, 120, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 120, 620, 120, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 120, 538, 190, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(538, 190, 525, 190, width=9, capstyle=ROUND, fill=NOMARK)
cv.update()
def MCO_R3(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 145, 620, 140, width=9, capstyle=ROUND, fill=color1)
cv.create_line(550, 140, 620, 140, width=9, capstyle=ROUND, fill=color1)
cv.create_line(550, 140, 550, 280, width=9, capstyle=ROUND, fill=color1)
cv.create_line(515, 280, 550, 280, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 145, 620, 140, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(550, 140, 620, 140, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(550, 140, 550, 280, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(515, 280, 550, 280, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def R3_MCO(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=color1, arrow=FIRST)
cv.create_line(620, 145, 620, 140, width=9, capstyle=ROUND, fill=color1)
cv.create_line(550, 140, 620, 140, width=9, capstyle=ROUND, fill=color1)
cv.create_line(550, 140, 550, 280, width=9, capstyle=ROUND, fill=color1)
cv.create_line(525, 280, 550, 280, width=9, capstyle=ROUND, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=NOMARK, arrow=FIRST)
cv.create_line(620, 145, 620, 140, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(550, 140, 620, 140, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(550, 140, 550, 280, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(525, 280, 550, 280, width=9, capstyle=ROUND, fill=NOMARK)
cv.update()
def MCO_R4(self, cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 145, 620, 160, width=9, capstyle=ROUND, fill=color1)
cv.create_line(565, 160, 620, 160, width=9, capstyle=ROUND, fill=color1)
cv.create_line(565, 160, 565, 370, width=9, capstyle=ROUND, fill=color1)
cv.create_line(515, 370, 565, 370, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 220 - 10, 770, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 145, 620, 160, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(565, 160, 620, 160, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(565, 160, 565, 370, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(515, 370, 565, 370, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def R4_MCO(self,cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=color1, arrow=FIRST)
cv.create_line(620, 145, 620, 160, width=9, capstyle=ROUND, fill=color1)
cv.create_line(565, 160, 620, 160, width=9, capstyle=ROUND, fill=color1)
cv.create_line(565, 160, 565, 370, width=9, capstyle=ROUND, fill=color1)
cv.create_line(525, 370, 565, 370, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
else:
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 215, 770, 145, width=9, capstyle=ROUND, fill=NOMARK, arrow=FIRST)
cv.create_line(620, 145, 620, 160, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(565, 160, 620, 160, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(565, 160, 565, 370, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(525, 370, 565, 370, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.update()
def SIG_ALU(self, cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(85, 578, 55, 578, width=9, capstyle=ROUND, fill=color1)
cv.create_line(55, 232, 55, 578, width=9, capstyle=ROUND, fill=color1)
cv.create_line(55, 232, 90, 232, width=9, capstyle=ROUND, fill=color1, arrow=LAST)
else:
cv.create_line(85, 578, 55, 578, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(55, 232, 55, 578, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(55, 232, 90, 232, width=9, capstyle=ROUND, fill=NOMARK, arrow=LAST)
cv.update()
def MAD_MEM(self, cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(635, 353, 635, 320, width=9, capstyle=ROUND, fill=color1)
cv.create_line(670, 320, 635, 320, width=9, capstyle=ROUND, fill=color1, arrow=FIRST)
else:
cv.create_line(635, 353, 635, 320, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(670, 320, 635, 320, width=9, capstyle=ROUND, fill=NOMARK, arrow=FIRST)
cv.update()
def MCO_PC(self, cv:tk.Canvas, flag):
if flag == 1:
cv.create_line(490, 469 + 4.5, 580, 469 + 4.5, width=9, capstyle=ROUND, arrow=FIRST, fill=color1)
cv.create_line(580, 180 + 4.5, 580, 469 + 4.5, width=9, capstyle=ROUND, fill=color1)
cv.create_line(580, 180 + 4.5, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=color1)
cv.create_line(620, 145, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=color1)
cv.create_line(770, 220 - 15, 770, 145, width=9, capstyle=ROUND, fill=color1)
else:
cv.create_line(490, 469 + 4.5, 580, 469 + 4.5, width=9, capstyle=ROUND, arrow=FIRST, fill=NOMARK)
cv.create_line(580, 180 + 4.5, 580, 469 + 4.5, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(580, 180 + 4.5, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(620, 145, 620, 180 + 4.5, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 145, 620, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.create_line(770, 220 - 15, 770, 145, width=9, capstyle=ROUND, fill=NOMARK)
cv.update()
if __name__ == '__main__':
from ALU import *
# al = ALU()
# al.init(cv, root)
co = Print_line()
# co.all()
# co.init()
# co.MCO_IR(cv, 1)
cpu.SigObj['MCO_PC'][1] = True
cpu.SigObj['MCO_IR'][1] = True
# co.mark(['PC_SIG'])
co.show()
# co.mark(['SIG_PC'])
# co.init()
def left1(event):
print(event.x,event.y)
cv.bind('<Button-1>', left1)
root.mainloop()

@ -0,0 +1,478 @@
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()

@ -0,0 +1,106 @@
from UI import *
import tkinter as tk
from Clock import ClockStep
class SIG:
def __init__(self):
self.bx2, self.by2, self.dx, self.dy = 105, 558, 120, 16#设置sig_ctrl文字间隔
def show(self):# 依据SigObj[]的值显示控制信号SigCtrl部件内的信号
words = ['SigCtrl->ALU', 'SigCtrl->IR', 'SigCtrl->PC', 'SigCtrl->MEMORY',
'SigCtrl->R1', 'SigCtrl->R2', 'SigCtrl->R3', 'SigCtrl->R4']# 对于每个控制信号进行判断
for w in words:# 如果SigObj中该文字为False则该文字非高亮显示
if cpu.SigObj[w][1] == False:
eval('self.' + w + '(cv, 0)')
for w in words:# 如果SigObj中该文字为True则该文字高亮显示
if cpu.SigObj[w][1] == True:
eval('self.' + w + '(cv, 1)')
cv.update()# 更新画布
def init(self, cv: Canvas, root):
INS = tk.Label(root, text=cpu.CODE(), fg=BLACK, font=('微软雅黑', 12), bg=WHITE)# 根据参数显示INSCODE
INS.place(x=184, y=535, height=20, width=80, anchor=NW)#放置INSCODE
# 显示sig_ctrl各部分的名字
cv.create_text(110, 535, text="insCode", font=('微软雅黑', 12), fill=GOLD, anchor=NW)
cv.create_text(self.bx2, self.by2, text='->ALU', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.create_text(self.bx2, self.by2 + self.dy, text='->IR', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.create_text(self.bx2, self.by2 + self.dy * 2, text='->PC + 1', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.create_text(self.bx2, self.by2 + self.dy * 3, text='->MEMORY', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.create_text(self.bx2 + self.dx, self.by2, text='->R1', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy, text='->R2', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy * 2, text='->R3', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy * 3, text='->R4', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()#更新画布
def ALU(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2, self.by2, text='->ALU', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2, self.by2, text='->ALU', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
def IR(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2, self.by2 + self.dy, text='->IR', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2, self.by2 + self.dy, text='->IR', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
def PC(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2, self.by2 + self.dy * 2, text='->PC + 1', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2, self.by2 + self.dy * 2, text='->PC + 1', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
def MEM(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2, self.by2 + self.dy * 3, text='->MEMORY', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2, self.by2 + self.dy * 3, text='->MEMORY', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
def R1(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2 + self.dx, self.by2, text='->R1', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2 + self.dx, self.by2, text='->R1', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
def R2(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy, text='->R2', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy, text='->R2', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
def R3(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy * 2, text='->R3', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy * 2, text='->R3', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
def R4(self, cv: Canvas, flag):
if flag:
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy * 3, text='->R4', font=('微软雅黑', 10), anchor=NW, fill=GOLD)
else:
cv.create_text(self.bx2 + self.dx, self.by2 + self.dy * 3, text='->R4', font=('微软雅黑', 10), anchor=NW, fill=GREY)
cv.update()
if __name__ == "__main__":
cpu = CPU()
cl = ClockStep()
cl.init(cv, root)
si=SIG()
# si.show()
# si.SIG_display()
si.init(cv, root)
mainloop()
time.sleep(cpu.time)
si.R4(cv,1)
time.sleep(cpu.time)
si.MEM(cv,1)
# bx2, by2, bw2, bh2 = 105, 560, 100, 20
# cv.create_rectangle(bx2, by2, bx2 + 170, by2 + 65, fill=BLACK, width=0)
# cv.create_rectangle(485, 469, 485 + 120, 469 + 9, fill=RED)
def left1(event):
print(event.x,event.y)
cv.bind('<Button-1>', left1)
root.mainloop()

98
UI.py

@ -0,0 +1,98 @@
import tkinter as tk
from tkinter import *
from data import CPU
import time
cpu = CPU()
WORD = '#00f5ff'
color1 = '#ee28a3'
color2 = '#668B8B'
NOMARK = '#c5c9c5'
NOMARK1 = NOMARK
BLUE = "#0080FF"
BLACK = "#000000"
WHITE = "#FFFFFF"
RED = "#FFAAAA"
NORED = NOMARK1 # '#ffdddd'
YELLOW = "#FFFACD"
GREY = '#808080'
GOLD = '#FFD700'
GREEN = '#00FA9A'
NOGREEN = NOMARK1 # '#00321f'
BACK = '#0153d2'
ORANGE = '#d3b719'
NOORANGE = NOMARK1 # '#ede2a3'
MEMBACK = '#fba651'
MEMMARK = '#fd280f'
MEMWORD = '#000000'
width = 1000 #设置窗口宽度
height = 700 #设置窗口高度
root = tk.Tk() #设置主框架
root.title("CPU可视化") #设置标题
root.config(bg=BLACK) #设置背景颜色
root.geometry(f'{width + 300}x{height}') #设置大小
cv = tk.Canvas(root, bg='white', bd=2, relief="sunken", width=width + 300, height=height, background=BLACK,
borderwidth=0, highlightthickness=0) #设置画布
cv.place(x=0, y=0, anchor=NW) #放置画布
def round_rectangle(x1, y1, x2, y2, r=25, **kwargs):
points = (
x1 + r, y1, x1 + r, y1, x2 - r, y1, x2 - r, y1, x2, y1, x2, y1 + r, x2, y1 + r, x2, y2 - r, x2, y2 - r, x2, y2,
x2 - r, y2, x2 - r, y2, x1 + r, y2, x1 + r, y2, x1, y2, x1, y2 - r, x1, y2 - r, x1, y1 + r, x1, y1 + r, x1, y1)
return cv.create_polygon(points, **kwargs, smooth=True)
# cv.grid(row=1,column=0)
# cv.create_oval(20,20,40, 40, fill='red',width=0)
# image = Image.open('image/CPU2.png')
# im = ImageTk.PhotoImage(image)
root_right = tk.Frame(root, bg=WHITE, relief="groove", width=250, height=height - 80)
root_right.place(x=width + 20, y=40, anchor=NW) # 右边栏
round_rectangle(width + 20, 15, width + 270, height - 25, r=25, fill=WHITE, width=0)
round_rectangle(20, 15, 970, height - 25, r=25, fill=WHITE, width=0)
# cv.create_image(0, 0, anchor=NW, image=im)
# cv.create_rectangle(0, 0, width, 10, fill=WHITE, width=0)
# cv.create_rectangle(20, 14, 30, 674, fill=WHITE, width=0)
#
# cv.create_rectangle(710, 24, 925, 140, fill=WHITE, width=0)
# cv.create_rectangle(77, 434, 533, 642, fill=color1, width=0)
def use_rgb(rgb):
"""
将rgb转十六进制
Args:
rgb: rgb颜色
Returns: 十六进制
"""
rgb = str(rgb)
RGB = rgb.replace('(', '').replace(")", '').split(',') # 将RGB格式划分开来
color = "#"
for i in RGB:
num = int(i)
# 将R、G、B分别转化为16进制拼接转换并大写 hex() 函数用于将10进制整数转换成16进制以字符串形式表示
color += str(hex(num))[-2:].replace('x', '0')
# for i in range(len(color)):
# if color[i] >= 'a' and color[i] <= 'z':
# c = int(color[i]) + int('A') - int('a')
# color[i] = str[c]
return color.upper()
if __name__ == "__main__":
# print(use_rgb((225, 225, 225)))
def left1(event):
print(event.x, event.y)
cv.bind('<Button-1>', left1)
mainloop()#执行GUI界面

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 KiB

@ -0,0 +1,152 @@
import random
class CPU:
def __init__(self):
self.time = 0.2
# 每步间隔时间
self.ALUio, self.ALUop = self.ALU_init()
self.clo, self.SigObj = self.Ctrl_init()
self.Sim_init()
self.SimMARK = []
for i in range(20):
self.SimMARK.append(False)
self.PC_int = 0
self.IR_int = 0
self.clostep = 0
self.alu = 0
self.code = 0
# self.code = '000000'
# self.R_int = [0, 0, 0, 0]
self.R_int = []
for i in range(4):
self.R_int.append(random.randint(0, pow(2, 16) - 1))
self.MAD_int = 0
self.MCO_int = 0
def ALU_init(self):
ALUio = {'ALU_R1': False, 'ALU_R2': False, 'ALU_R3': False, 'ALU_R4': False,
'R1_ALU1': False, 'R2_ALU1': False, 'R3_ALU1': False, 'R4_ALU1': False,
'R1_ALU2': False, 'R2_ALU2': False, 'R3_ALU2': False, 'R4_ALU2': False}
ALUop = {'+': False, '-': False, '*': False, '/': False}
return ALUio, ALUop
def Ctrl_init(self):
ClockStep = [1, 0, 0, 0, 0, 0]
SigObj = {'PC->Maddr': ['PC->Maddr', False, 'DataSig'],
'SigCtrl->Memory': ['SigCtrl->Memory', False, 'CtrlSig'],
'SigCtrl->IR': ['SigCtrl->IR', False, 'CtrlSig'],
'IR->SigCtrl': ['IR->SigCtrl', False, 'CtrlSig'],
'IR->Mcontent': ['IR->Mcontent', False, 'DataSig'],
'Maddr->IR': ['Maddr->IR', False, 'DataSig'],
'Mcontent->Memory': ['Mcontent->Memory', False, 'DataSig'],
'Memory->Mcontent': ['Memory->Mcontent', False, 'DataSig'],
'PC->SigCtrl': ['PC->SigCtrl', False, 'CtrlSig'],
'SigCtrl->PC': ['SigCtrl->PC', False, 'CtrlSig'],
'Mcontent_PC': ['Mcontent_PC', False, 'DataSig'],
'IR->Maddr': ['IR->Maddr', False, 'DataSig'],
'SigCtrl->R1': ['SigCtrl->R1', False, 'CtrlSig'],
'SigCtrl->R2': ['SigCtrl->R2', False, 'CtrlSig'],
'SigCtrl->R3': ['SigCtrl->R3', False, 'CtrlSig'],
'SigCtrl->R4': ['SigCtrl->R4', False, 'CtrlSig'],
'Mcontent->R1': ['Mcontent->R1', False, 'DataSig'],
'Mcontent->R2': ['Mcontent->R2', False, 'DataSig'],
'Mcontent->R3': ['Mcontent->R3', False, 'DataSig'],
'Mcontent->R4': ['Mcontent->R4', False, 'DataSig'],
'R1->Mcontent': ['R1->Mcontent', False, 'DataSig'],
'R2->Mcontent': ['R2->Mcontent', False, 'DataSig'],
'R3->Mcontent': ['R3->Mcontent', False, 'DataSig'],
'R4->Mcontent': ['R4->Mcontent', False, 'DataSig'],
'SigCtrl->ALU': ['SigCtrl->ALU', False, 'CtrlSig'],
}
return ClockStep, SigObj
def Sim_init(self):
try:
self.SimMem
# 如果SimMem存在则说明已经初始化完毕不做操作
except AttributeError:
# 如果SimMem不存在则说明未初始化则将存储器所有值初始为0000000000000000
self.SimMem = []
for i in range(20):
self.SimMem.append('0000000000000000')
def CODE(self):
return bin(self.code).replace('0b', '').zfill(6)
def PC(self):
ans = bin(self.PC_int).replace('0b', '').zfill(16)
# print(ans)
pc = ''
for i in range(8):
pc += ans[i]
pc += ' '
for i in range(8):
pc += ans[i + 8]
# print(pc)
return pc
def IR(self):
ans = bin(self.IR_int).replace('0b', '').zfill(16)
# print(ans)
ir = ''
for i in range(8):
ir += ans[i]
ir += ' '
for i in range(8):
ir += ans[i + 8]
# print(pc)
return ir
def CLO(self, num):
self.clostep = num
for i in range(6):
self.clo[i] = False
self.clo[self.clostep] = True
def ALU(self):
AL = ['', '+', '-', 'x', '÷']
return AL[self.alu]
def R(self, num):
return bin(self.R_int[num]).replace('0b', '').zfill(16)
def MAD(self):
sum = bin(self.MAD_int).replace('0b', '').zfill(16)
# print(ans)
ans = ''
for i in range(8):
ans += sum[i]
ans += ' '
for i in range(8):
ans += sum[i + 8]
return ans
def MCO(self):
sum = bin(self.MCO_int).replace('0b', '').zfill(16)
# print(ans)
ans = ''
for i in range(8):
ans += sum[i]
ans += ' '
for i in range(8):
ans += sum[i + 8]
return ans
def str_int(self, ans):
if ans[0] == ' ':
return -1
sum = 0
for i in range(len(ans)):
if ans[i] == ' ':
continue
sum *= 2
sum += int(ans[i])
return sum
if __name__ == "__main__":
cpu = CPU()
cpu.PC()

@ -0,0 +1,72 @@
# from program import *
from ExecInstructWithAnim import *
from ExecInstructWithText import *
r = RightPanel()
run_ani = ExecInstructWithAnim()
run_text = ExecInstructWithText()
bw, bh = 150, 45
# 按钮1
bx1, by1 = 750, 30
Button1 = tk.Button(root, text='自动执行指令(仅寄存器)', background=GOLD, command=lambda: Program1(), state=NORMAL)
Button1.place(x=bx1, y=by1, width=bw, height=bh)
def Program1():
if run_text.play == 1 or run_ani.play == 1:
messagebox.showwarning('警告', '程序正在运行')
return
run_text.auto_display()
# 按钮2
bx2, by2 = 750, 90
Button2 = tk.Button(root, text='自动执行指令(带UI)', background=GOLD, command=lambda: Program2(), state=NORMAL)
Button2.place(x=bx2, y=by2, width=bw, height=bh)
def Program2():
if run_text.play == 1 or run_ani.play == 1:
messagebox.showwarning('警告', '程序正在运行')
return
run_ani.auto_display()
# 按钮3
bx2, by2 = 820, 150
WR = tk.Button(root, text='装载程序', background=RED, command=lambda: r.write_memory(), state=NORMAL)
WR.place(x=bx2, y=by2, width=80, height=bh)
def main():
# 右键菜单
def onRightButtonUp(event):
menu.post(event.x_root, event.y_root)
menu = tk.Menu(root, tearoff=0)
menu.add_command(label='运算器', command=lambda: r.CAL_display())
menu.add_command(label='控制器', command=lambda: r.SIG_display())
menu.add_command(label='存储器', command=lambda: r.MEM_display())
cv.bind('<ButtonRelease-3>', onRightButtonUp)
root.mainloop()
if __name__ == "__main__":
from pycallgraph2 import PyCallGraph
from pycallgraph2.output import GraphvizOutput
from pycallgraph2 import Config
from pycallgraph2 import GlobbingFilter
output = GraphvizOutput(font_size=30)
output.output_file = "basic.png"
output.group_font_size = 40
config = Config()
config.trace_filter = GlobbingFilter(include=[
'RightPanel.*',
'ExecInstructWithAnim.*',
'ExecInstructWithText.*',
])
with PyCallGraph(output=output, config=config):
main()

@ -0,0 +1,99 @@
import random
from UI import *
# import tkinter as tk
class MEM:
def __init__(self):
# 绘制存储器背景板
round_rectangle(597, 205, 928, 670, r=25, fill=BACK, width=0)
round_rectangle(675, 275, 910, 660, r=25, fill=BLACK, width=0)
def mark(self, cv:Canvas):
for i in range(20):
# 如果SimMARK中该存储单元为False则非高亮显示
if cpu.SimMARK[i] == False:
cv.create_rectangle(810, 282 + 18.5 * i, 897, 298 + 18.5 * i, width=3, outline=MEMBACK)
for i in range(20):
# 如果SimMARK中该存储单元为True则高亮显示
if cpu.SimMARK[i] == True:
cv.create_rectangle(810, 282 + 18.5 * i, 897, 298 + 18.5 * i, width=3, outline=MEMMARK)
cv.update()
# 根据当前的存储数据绘制当前的存储器
def show(self, cv: Canvas, root):
# 标记cpu各部分的名称
cv.create_text(670, 219, text='Mcontent', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
cv.create_text(611, 356, text='Maddr', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
cv.create_text(690, 255, text='地址(编号)', font=('time', 12,), anchor=NW, fill='#ffd700')
cv.create_text(830, 255, text='在选单元', font=('time', 12,), anchor=NW, fill='#ffd700')
# 根据存储的数据显示当前Maddr的值
cv.create_rectangle(619, 383, 648, 541, fill=WHITE, width=0)
cv.create_text(620, 390, text=cpu.MAD(), font=('微软雅黑', 12), angle=90, anchor=NE, fill=BLACK)
# 根据存储的数据显示当前Mcotent的值
cv.create_rectangle(753, 218, 910, 242, fill=WHITE, width=0)
cv.create_text(830, 230, text=cpu.MCO(), fill=BLACK, font=('微软雅黑', 12))
# 根据存储的数据显示当前Memory中各个存储单元的值
cv.create_rectangle(685, 281, 791, 647, fill=BLACK)
for i in range(20):# 显示存储器各个单元的地址
cv.create_text(688, 282 + 18.5 * i, text="00000000 " + bin(i).replace('0b', '').zfill(8),
font=('微软雅黑', 8), anchor=NW, fill=GOLD)
cv.create_rectangle(808, 281, 897, 649, fill=MEMBACK, width=0)
for i in range(20):# 显示存储器各个单元的内容
cv.create_text(852, 290 + 18.5 * i, text=cpu.SimMem[i], font=('微软雅黑', 6), anchor=CENTER,
fill=MEMWORD)
self.mark(cv)# 更新画布
# 根据当前的存储数据绘制当前的存储器
def init(self, cv:Canvas, root):
# 标记cpu各部分的名称
cv.create_text(670, 219, text='Mcontent', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
cv.create_text(611, 356, text='Maddr', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD)
cv.create_text(690, 255, text='地址(编号)', font=('time', 12, ), anchor=NW, fill='#ffd700')
cv.create_text(830, 255, text='在选单元', font=('time', 12, ), anchor=NW, fill='#ffd700')
# 根据存储的数据显示当前Maddr的值
cv.create_rectangle(619, 383, 648, 541, fill=WHITE, width=0)
cv.create_text(620, 390, text=cpu.MAD(), font=('微软雅黑', 12), angle=90, anchor=NE, fill=BLACK)
# 根据存储的数据显示当前Mcotent的值
cv.create_rectangle(753, 218, 910, 242, fill=WHITE, width=0)
cv.create_text(830, 230, text=cpu.MCO(), fill=BLACK, font=('微软雅黑', 12))
# 根据存储的数据显示当前Memory中各个存储单元的值
# 显示存储器各个单元的地址
cv.create_rectangle(685, 281, 791, 647, fill=BLACK)
for i in range(20):
cv.create_text(688, 282 + 18.5 * i, text="00000000 " + bin(i).replace('0b', '').zfill(8), font=('微软雅黑', 8), anchor=NW, fill=GOLD)
# 显示存储器各个单元的内容
cv.create_rectangle(808, 281, 897, 649, fill=MEMBACK, width=0)
for i in range(20):
cv.create_text(852, 290 + 18.5 * i, text=cpu.SimMem[i], font=('微软雅黑', 6), anchor=CENTER, fill=MEMWORD)
# 更新画布
self.mark(cv)
if __name__ == "__main__":
mem = MEM()#实例化
mem.show(cv, root)#显示存储器界面
bw, bh = 150, 45
# 按钮1
bx1, by1 = 750, 95
B1 = tk.Button(root, text='添加程序', background=BLUE, command=lambda: test(), state=NORMAL)
B1.place(x=bx1, y=by1, width=bw, height=bh)
def test():
for i in range(20):
time.sleep(cpu.time)
for j in range(20):
cpu.SimMARK[j] = False
cpu.SimMARK[random.randint(0,19)] = True
mem.mark(cv)
cv.update()
def left1(event):
print(event.x,event.y)
cv.bind('<Button-1>', left1)
root.mainloop()

Binary file not shown.
Loading…
Cancel
Save