This repository was archived by the owner on Jan 18, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathapplication.py
More file actions
241 lines (208 loc) · 7.44 KB
/
Copy pathapplication.py
File metadata and controls
241 lines (208 loc) · 7.44 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import program as mips
import tkinter as tk
import conversion as conv
import instruction as ins
from tkinter import filedialog
import time
class MIPSApplication(tk.Frame):
def __init__(self, master = None):
tk.Frame.__init__(self, master)
self.pack()
self.create_widgets()
# default configuration of window
root.resizable(0,0)
root.geometry("1200x600")
root.wm_title("MIPS Simulator")
self.job = None
def create_widgets(self):
# create menu
self.menu = tk.Menu(self)
root["menu"] = self.menu
# add file menu
file_menu = tk.Menu(self.menu)
file_menu.add_command(label = "Open", command = self.load_input_file)
file_menu.add_command(label = "Exit", command = root.destroy)
self.menu.add_cascade(menu = file_menu, label = "File")
# create grid layout for progam
self.grid(column = 0, row = 0)
# create text areas
self.input_text = tk.Text(self, width = 43, height = 35)
self.input_text["background"] = "grey"
self.input_text.grid(column = 10, row = 0)
self.bin_text = tk.Text(self, width = 42, height = 35)
self.bin_text["background"] = "grey"
self.bin_text.grid(column = 15, row = 0)
self.stack_text = tk.Text(self, width = 24, height = 35)
self.stack_text["background"] = "grey"
self.stack_text.grid(column = 20, row = 0)
self.register_text = tk.Text(self, width = 19, height = 35)
self.register_text["background"] = "grey"
self.register_text.grid(column = 25, row = 0)
# lock text areas
self.lock_text()
# create controls
# Control Panel
self.control_panel = tk.Frame(self, width=400, height=800)
button_panel = tk.Frame(self.control_panel)
self.control_panel.grid(column = 1, row = 0)
# Slider
self.speed_slider = tk.Scale(self.control_panel, orient="horizontal")
self.speed_slider["from"] = 1
self.speed_slider["to"] = 60
self.speed_slider.grid(column=0, row=0)
# Buttons
button_panel.grid(column=0,row=2, columnspan=2)
self.step_button = tk.Button(button_panel, text="Step", command=self.step_once, state = "disabled")
self.step_button.grid(column=0, row=0)
self.run_button = tk.Button(button_panel, text="Run", command=self.run_prog, state = "disabled")
self.run_button.grid(column = 1, row = 0)
self.stop_button = tk.Button(button_panel, text="Stop", command=self.stop_prog, state = "disabled")
self.stop_button.grid(column = 0, row = 1)
self.reset_button = tk.Button(button_panel, text="Reset", command=self.reset, state = "disabled")
self.reset_button.grid(column = 1, row = 1)
def lock_text(self):
self.input_text["state"] = "disabled"
self.bin_text["state"] = "disabled"
self.stack_text["state"] = "disabled"
self.register_text["state"] = "disabled"
def unlock_text(self):
self.input_text["state"] = "normal"
self.bin_text["state"] = "normal"
self.stack_text["state"] = "normal"
self.register_text["state"] = "normal"
def get_file(self):
filename = tk.filedialog.askopenfilename()
if filename == "": return
# allow editing on our text fields and wipe them
self.input_text["state"] = "normal"
self.bin_text["state"] = "normal"
self.input_text.delete("1.0", "end")
self.bin_text.delete("1.0", "end")
return filename
def get_lines(self, filename):
# create a list to contain the lines
lines = []
# iterate through all the lines in filename
with open(filename) as f:
for line in f:
lines.append(line)
return lines
def update_input(self, lines=None):
if lines != None:
print(">Updating input")
i = 1
for line in lines:
content = str(i) + ":\t" + str(line)
self.input_text.insert("end", content)
i += 1
# remove old contents and any highlighting bin_text may have had
if "highlight" in self.input_text.tag_names():
self.input_text.tag_delete("highlight")
# highlight line indicated by the program counter
# this takes a bit more work, because you need to account for labels
# and the occasional empty line, so I store the line numbe in
# instructions objects as a bit of a workaround
if not self.program.is_finished():
next_inst = self.program.instructions[self.program.pc // ins.Instruction.SIZE]
self.highlight(self.input_text, next_inst.line_number + 1)
def update_bin(self):
print(">Updating binary")
# remove old contents and any highlighting bin_text may have had
self.bin_text.delete("1.0", "end")
if "highlight" in self.bin_text.tag_names():
self.bin_text.tag_delete("highlight")
i = 0
for line in self.program.machine_code:
content = str(hex(i)) + ":\t" + str(line)
self.bin_text.insert("end", content + "\n")
i += 4
# highlight line indicated by the program counter
self.highlight(self.bin_text, self.program.pc // ins.Instruction.SIZE + 1)
# given a text area, highlight a given line
def highlight(self, textarea, line_number):
# remove previous highlighting
if "highlight" in textarea.tag_names():
textarea.tag_delete("highlight")
# now apply highlighting on given line
textarea.tag_add("highlight", str(line_number) + ".0", str(line_number) + ".end")
textarea.tag_config("highlight", background="yellow")
print("highlighting")
def update_stack(self):
print(">Updating stack")
self.stack_text.delete("1.0", "end")
stack = self.program.get_stack()
contents = stack.get_contents()
sp = self.program.get_register(29)
if not(sp in contents):
stack.store_word(sp, "-----")
sorted_stack = sorted(contents, reverse = True)
for item in sorted_stack:
output = str(hex(item)) + ":\t" + str(contents[item])
self.stack_text.insert("end", output)
if(sp == item):
self.stack_text.insert("end", "\t<--$sp")
self.stack_text.insert("end", "\n")
def update_registers(self):
print(">Updating registers")
self.register_text.delete("1.0", "end")
registers = self.program.get_all_registers()
self.register_text["state"] = "normal"
regnum = 0
for register in registers:
output = "$" + str(regnum) + ":\t" + str(register) + "\n"
self.register_text.insert("end", output)
regnum += 1
self.register_text.insert("end", "$HI:\t" + str(self.program.get_hi()) + "\n")
self.register_text.insert("end", "$LO:\t" + str(self.program.get_lo()) + "\n")
def step_once(self):
print(">Stepping once")
self.reset_button["state"] = "normal"
self.program.step_once()
self.unlock_text()
self.update_input()
self.update_bin()
self.update_stack()
self.update_registers()
self.lock_text()
def run_prog(self):
print(">Running program")
self.reset_button["state"] = "normal"
self.stop_button["state"] = "normal"
self.step_once()
if not self.program.is_finished():
self.job = root.after(1000//self.speed_slider.get(), self.run_prog)
else:
self.job = None
def stop_prog(self):
print(">Stopping program")
self.stop_button["state"] = "disabled"
if self.job is not None:
root.after_cancel(self.job)
self.job = None
def reset(self):
print(">Resetting simulator")
self.reset_button["state"] = "disabled"
self.stop_button["state"] = "disabled"
self.program.reset()
self.unlock_text()
self.update_input()
self.update_bin()
self.update_stack()
self.update_registers()
self.lock_text()
def load_input_file(self):
filename = self.get_file()
if filename == None: return
lines = self.get_lines(filename)
self.program = mips.Program(lines)
self.unlock_text()
self.update_input(lines)
self.update_bin()
self.update_stack()
self.update_registers()
self.lock_text()
self.step_button["state"] = "normal"
self.run_button["state"] = "normal"
root = tk.Tk()
app = MIPSApplication(master = root)
app.mainloop()