|
|
# -*- 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()
|
|
|
|
|
|
|
|
|
|
|
|
|