-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
338 lines (250 loc) · 11.3 KB
/
main.py
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import json
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
from PyQt5.QtCore import QDir
from functools import partial
import isa
def show_popup(fileLoadFail, notMachine):
msg = QMessageBox()
msg.setWindowTitle("Oops! error")
if fileLoadFail and not notMachine:
msg.setText("Wrong file extension.")
elif notMachine:
msg.setText("The file does not contain machine code.")
else:
msg.setText("Load the code file first.")
msg.setIcon(QMessageBox.Warning)
msg.exec()
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("main.ui", self)
self.instructions = []
self.tableWidget.setFixedHeight(940)
self.tableWidget.setFixedWidth(1090)
self.loadAsmButton.clicked.connect(partial(self.load_file, False))
self.loadMachineButton.clicked.connect(partial(self.load_file, True))
self.loadJSONButton.clicked.connect(partial(self.load_file, False))
self.visualizeButton.clicked.connect(self.visualize_pipeline)
self.clearButton.clicked.connect(self.clear)
def clear(self):
self.tableWidget.setRowCount(0)
self.tableWidget.setColumnCount(0)
rowHeaders = []
colHeaders = []
self.tableWidget.setVerticalHeaderLabels(rowHeaders)
self.tableWidget.setHorizontalHeaderLabels(colHeaders)
def handle_json(self, data):
rowHeaders = []
colHeaders = []
colCounter = 0
self.tableWidget.setRowCount(data[0][-1]['id'] + 1)
# self.tableWidget.setColumnCount(data[0][-1]['cycle'])
# for i in range(self.tableWidget.columnCount()):
# colHeaders.append("CC" + str(i + 1))
stages = []
for line in data[0]:
type = line['type']
id = line['id']
if type == "Event":
cycle = line['cycle']
stage = line['stage']
if "CC" + str(cycle) not in colHeaders:
colHeaders.append("CC" + str(cycle))
self.tableWidget.insertColumn(colCounter)
colCounter = colCounter + 1
row = id
# self.tableWidget.setItem(row, cycle - 1, QtWidgets.QTableWidgetItem(stages[stage]))
self.fill_cell(row, colCounter - 1, stages[stage])
elif type == "Stage":
description = line['description']
if description == 'Fetch':
path = "img/fetch.png"
elif description == 'Decode':
path = "img/decode.png"
elif description == 'Execute':
path = "img/execute.png"
elif description == 'Memory':
path = "img/memory.png"
elif description == 'Writeback':
path = "img/writeback.png"
stages.insert(id, path)
elif type == "Record":
disassembly = line['disassembly']
rowHeaders.append(disassembly)
self.tableWidget.setVerticalHeaderLabels(rowHeaders)
self.tableWidget.setHorizontalHeaderLabels(colHeaders)
def fetch_and_decode(self, asm, machineCode):
self.instructions = []
for line in asm:
self.instructions.append(isa.Instruction(line, machineCode))
rowCount = len(self.instructions) + 1
colCount = len(self.instructions) * 4
if len(self.instructions) == 1:
colCount = 7
self.tableWidget.setRowCount(rowCount)
self.tableWidget.setColumnCount(colCount)
rowHeaders = ["Instructions:"]
colHeaders = ["mem address:", "instr №"]
i = 1
while i <= colCount:
colHeaders.append("CC" + str(i))
i = i + 1
self.tableWidget.setHorizontalHeaderLabels(colHeaders)
row = 1
for instr in self.instructions:
self.tableWidget.setItem(row, 0, QtWidgets.QTableWidgetItem(str(instr.NPC)))
if machineCode:
rowHeaders.append(instr.decodedInstr)
self.tableWidget.setItem(row, 1, QtWidgets.QTableWidgetItem("I" + str(row - 1)))
else:
rowHeaders.append(instr.fullInstr)
self.tableWidget.setItem(row, 1, QtWidgets.QTableWidgetItem("I" + str(row - 1)))
row = row + 1
self.tableWidget.setVerticalHeaderLabels(rowHeaders)
self.tableWidget.setHorizontalHeaderLabels(colHeaders)
def load_file(self, machineCode):
self.clear()
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.ExistingFile)
dialog.setFilter(QDir.Files)
if dialog.exec_():
fileName = dialog.selectedFiles()
if fileName[0].endswith('.json'):
with open(fileName[0], 'r') as f:
data = json.load(f)
self.handle_json(data)
elif fileName[0].endswith('.txt') or fileName[0].endswith('.asm'):
with open(fileName[0], 'r') as f:
if not machineCode:
asm = [line.rstrip('\n') for line in f]
else:
asm = [line for line in f]
if (asm[0][0] != "0" and asm[0][0] != "1") and machineCode:
show_popup(True, True)
f.close()
else:
self.tableWidget.setRowCount(0)
self.fetch_and_decode(asm, machineCode)
isa.PC = - 4
f.close()
else:
show_popup(True, False)
def get_img_label(self, path):
imgLabel = QtWidgets.QLabel(self.centralWidget())
imgLabel.setText("")
imgLabel.setScaledContents(True)
pixmap = QtGui.QPixmap()
pixmap.load(path, 'png')
imgLabel.setPixmap(pixmap)
return imgLabel
def fill_cell(self, row, col, path):
img = self.get_img_label(path)
self.tableWidget.setCellWidget(row, col, img)
def fill_line(self, row, col, pipeline):
i = 0
for stage in pipeline:
img = self.get_img_label(stage)
self.tableWidget.setCellWidget(row, col + i, img)
i = i + 1
def visualize_pipeline(self):
row = 0
col = 1
stages = ["img/fetch.png", "img/decode.png", "img/execute.png", "img/memory.png", "img/writeback.png",
"img/bubble.png"]
stallCounter = 0
cycleCounter = -1
if len(self.instructions) > 0:
for i in range(len(self.instructions)):
row = row + 1
col = col + 1
if i < len(self.instructions) - 1 and \
(self.instructions[i].name == "lw" or self.instructions[i].name == "lui") and \
self.instructions[i + 1].type != "J":
if self.instructions[i].rt == self.instructions[i + 1].rs or \
self.instructions[i].rt == self.instructions[i + 1].rt:
stallCounter = stallCounter + 1
if stallCounter > 1 and cycleCounter == 3:
cycleCounter = 3
elif stallCounter > 2 and stallCounter % 2 != 0:
if self.tableWidget.item(row - 1, col).text() == "D":
cycleCounter = 2
else:
cycleCounter = 3
elif stallCounter > 2 and stallCounter % 2 == 0:
cycleCounter = 2
elif stallCounter > 1:
cycleCounter = 2
else:
cycleCounter = cycleCounter + 1
if cycleCounter == -1:
pipeline = stages[0:5]
self.fill_line(row, col, pipeline)
elif cycleCounter == 0:
pipeline = stages[0:5]
self.fill_line(row, col, pipeline)
cycleCounter = cycleCounter + 1
elif cycleCounter == 1:
pipeline = [stages[0], stages[1], stages[1], stages[2], stages[3], stages[4]]
self.fill_line(row, col, pipeline)
cycleCounter = cycleCounter + 1
elif cycleCounter == 2:
pipeline = [stages[0], stages[0], stages[1], stages[2], stages[3], stages[4]]
self.fill_line(row, col, pipeline)
if stallCounter > 3:
cycleCounter = -3
elif stallCounter > 1:
cycleCounter = -2
else:
cycleCounter = cycleCounter + 1
elif cycleCounter == 3:
pipeline = [stages[5], stages[0], stages[1], stages[2], stages[3], stages[4]]
self.fill_line(row, col, pipeline)
col = col + 1
stallCounter = stallCounter - 1
if stallCounter > 0:
cycleCounter = 1
else:
cycleCounter = -1
elif cycleCounter == 4:
pipeline = [stages[5], stages[0], stages[1], stages[2], stages[3], stages[4]]
self.fill_line(row, col, pipeline)
col = col + 1
stallCounter = stallCounter - 1
cycleCounter = 1
elif cycleCounter == -2:
if (self.instructions[i - 1].name == "lw" or self.instructions[i - 1].name == "lui") and \
self.instructions[i].type != "J":
if self.instructions[i - 1].rt == self.instructions[i].rs or \
self.instructions[i - 1].rt == self.instructions[i].rt:
pipeline = [stages[5], stages[0], stages[1], stages[1], stages[2], stages[3], stages[4]]
cycleCounter = 2
else:
pipeline = [stages[5], stages[0], stages[1], stages[2], stages[3], stages[4]]
cycleCounter = -1
self.fill_line(row, col, pipeline)
col = col + 1
stallCounter = stallCounter - 1
elif cycleCounter == -3:
pipeline = [stages[5], stages[0], stages[0], stages[1], stages[2], stages[3], stages[4]]
self.fill_line(row, col, pipeline)
col = col + 1
stallCounter = stallCounter - 1
cycleCounter = 3
else:
pipeline = stages[0:5]
self.fill_line(row, col, pipeline)
else:
show_popup(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainWindow)
widget.setFixedHeight(1000)
widget.setFixedWidth(1248)
widget.setWindowTitle("MIPS Pipeline Visualizer")
widget.show()
sys.exit(app.exec_())