-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl Unit Logic Visualizer.py
More file actions
100 lines (82 loc) · 2.92 KB
/
Control Unit Logic Visualizer.py
File metadata and controls
100 lines (82 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import tkinter as tk
from tkinter import ttk
# =========================
# CONTROL UNIT LOGIC
# =========================
CONTROL_SIGNALS = [
"RegWrite", "MemRead", "MemWrite",
"ALUSrc", "Branch", "MemToReg", "ALUOp"
]
INSTRUCTION_SET = {
"ADD": {
"RegWrite": 1, "MemRead": 0, "MemWrite": 0,
"ALUSrc": 0, "Branch": 0, "MemToReg": 0, "ALUOp": "ADD"
},
"SUB": {
"RegWrite": 1, "MemRead": 0, "MemWrite": 0,
"ALUSrc": 0, "Branch": 0, "MemToReg": 0, "ALUOp": "SUB"
},
"LOAD": {
"RegWrite": 1, "MemRead": 1, "MemWrite": 0,
"ALUSrc": 1, "Branch": 0, "MemToReg": 1, "ALUOp": "ADD"
},
"STORE": {
"RegWrite": 0, "MemRead": 0, "MemWrite": 1,
"ALUSrc": 1, "Branch": 0, "MemToReg": "X", "ALUOp": "ADD"
},
"BEQ": {
"RegWrite": 0, "MemRead": 0, "MemWrite": 0,
"ALUSrc": 0, "Branch": 1, "MemToReg": "X", "ALUOp": "SUB"
}
}
# =========================
# GUI APPLICATION
# =========================
class ControlUnitVisualizer:
def __init__(self, root):
self.root = root
root.title("Control Unit Logic Visualizer")
root.geometry("750x500")
self.create_widgets()
def create_widgets(self):
# Instruction Selection
tk.Label(self.root, text="Select Instruction", font=("Arial", 11)).pack(pady=5)
self.instr_var = tk.StringVar()
self.instr_box = ttk.Combobox(
self.root,
textvariable=self.instr_var,
values=list(INSTRUCTION_SET.keys()),
state="readonly"
)
self.instr_box.pack()
self.instr_box.bind("<<ComboboxSelected>>", self.update_signals)
# Control Signal Table
tk.Label(self.root, text="Control Signals", font=("Arial", 11)).pack(pady=10)
self.table = ttk.Treeview(self.root, columns=("Value"), height=10)
self.table.heading("#0", text="Signal")
self.table.heading("Value", text="Value")
self.table.pack(fill="both", padx=20, expand=True)
# Description
self.desc = tk.Label(
self.root,
text="Select an instruction to visualize control unit output",
relief="sunken",
anchor="w"
)
self.desc.pack(fill="x", side="bottom")
def update_signals(self, event=None):
instr = self.instr_var.get()
signals = INSTRUCTION_SET[instr]
self.table.delete(*self.table.get_children())
for sig in CONTROL_SIGNALS:
self.table.insert("", "end", text=sig, values=(signals[sig],))
self.desc.config(
text=f"Instruction: {instr} | Control signals generated by Control Unit"
)
# =========================
# MAIN
# =========================
if __name__ == "__main__":
root = tk.Tk()
app = ControlUnitVisualizer(root)
root.mainloop()