-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassemblerApp.py
435 lines (376 loc) · 18.6 KB
/
assemblerApp.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python3
import os
import yaml
import tkinter as tk
from tkinter.font import Font
import tkinter.filedialog
from widgets.CodeTextWidget import CodeTextWidget
from assembler.AssemblerChecker import AssemblerChecker
######################################################################################################################
FONT_DEFAULT = None
FONT_LABEL = None
TABULATOR_INDENT_COUNT = 2
exportTypes = {
0: ".bin",
1: ".ascii",
#2: ".elf",
#3: ".hex",
}
class AssemblerGui(object):
def __init__(self):
### create necessary utilities
self.checker = AssemblerChecker(self.loadInstructionSet())
### create the main window
self.root = tk.Tk()
self.root.title("My CPU Assembler")
### create stuff
self.createViews()
### pack stuff
self.drawViews()
self.cachedPath = None
def createViews(self):
### creates special fonts for stuff
FONT_DEFAULT = Font(family="Consolas", size=12)
FONT_LABEL = Font(family="Consolas", size=12, weight="bold", underline=1)
FONT_ADDRESS = Font(family="Consolas", size=12, slant="italic", underline=1)
### add code widget
self.code = CodeTextWidget(self.root, width=50, height=30)
self.code.text.configure(wrap="none")
self.code.text.configure(font=FONT_DEFAULT)
### add on keypress event
self.code.text.bind('<KeyRelease>', self.onCodeChanged)
### change tabulator key
self.code.text.bind("<Tab>", self.onTabPressed)
self.code.text.bind("<Shift-KeyPress-Tab>", self.onShiftTabPressed)
self.code.text.bind("<Control-s>", self.onSaveClicked)
### custom tags for colored text and stuff
self.code.text.tag_configure("default", font=FONT_DEFAULT)
self.code.text.tag_configure("label", font=FONT_LABEL, foreground="orange")
self.code.text.tag_configure("address", font=FONT_ADDRESS, background="lightblue")
self.code.text.tag_configure("symbol", font=FONT_DEFAULT, foreground="gray")
self.code.text.tag_configure("comment", font=FONT_DEFAULT, foreground="green")
self.code.text.tag_configure("error", font=FONT_DEFAULT, foreground="black", background="red")
### add default code and add some special color things
self.code.text.insert("1.0", "#0x0000\n", "address")
self.code.text.insert("2.0", "main:\n", "label")
### focus
self.code.text.focus_set()
### side panel
self.sidepanel = tk.Frame(self.root)
### buttons
self.buttonCpuInfo = tk.Button(self.sidepanel, text="CPU Info", width=15, height=5, command=self.onCpuInfoClicked)
self.buttonLoad = tk.Button(self.sidepanel , text="Load", width=15, height=2, command=self.onLoadClicked)
self.buttonSave = tk.Button(self.sidepanel , text="Save", width=15, height=2, command=self.onSaveClicked)
self.buttonCheck = tk.Button(self.sidepanel , text="Check", width=15, height=5, command=self.onCheckClicked)
self.buttonExport = tk.Button(self.sidepanel , text="Export", width=15, height=5, command=self.onExportClicked)
### create a variable to store options for the radiobuttons
self.exportMode = tk.IntVar()
self.exportMode.set(0)
self.radiobuttons = []
for v, t in exportTypes.items():
rb = tk.Radiobutton(self.sidepanel, text=t, variable=self.exportMode, value=v, anchor="w")
self.radiobuttons.append(rb)
def drawViews(self):
tk.Frame(height=4, bd=4, relief=tk.SUNKEN).pack(fill=tk.X, padx=5, pady=5)
self.code.pack(fill="both", expand=True, side=tk.LEFT)
self.sidepanel.pack(fill="both", expand=True, side=tk.RIGHT)
self.buttonCpuInfo.pack(padx=5, pady=5)
tk.Frame(self.sidepanel, height=4, bd=4, relief=tk.SUNKEN).pack(fill=tk.X, padx=5, pady=5)
self.buttonLoad.pack(padx=5, pady=5)
self.buttonSave.pack(padx=5, pady=5)
tk.Frame(self.sidepanel, height=4, bd=4, relief=tk.SUNKEN).pack(fill=tk.X, padx=5, pady=5)
self.buttonCheck.pack(padx=5, pady=5)
tk.Frame(self.sidepanel, height=4, bd=4, relief=tk.SUNKEN).pack(fill=tk.X, padx=5, pady=5)
self.buttonExport.pack(padx=5, pady=5)
for radioBtn in self.radiobuttons:
radioBtn.pack(padx=5, pady=5)
tk.Frame(self.sidepanel, height=4, bd=4, relief=tk.SUNKEN).pack(fill=tk.X, padx=5, pady=5)
def run(self):
self.root.mainloop()
def loadInstructionSet(self):
d = None
### load instruction set info
currentDir = os.path.dirname(__file__)
filename = "instruction_set.yml"
path = os.path.join(currentDir, filename)
with open(path, 'r') as stream:
try:
d = yaml.safe_load(stream)
except yaml.YAMLError as e:
print(e)
return d
def onCpuInfoClicked(self):
d = self.checker.getInstructionSet()
### read data and interprete table entries
firstEntry = next(iter(d.values()))
headers = ["command"] + list(firstEntry.keys()) ### grab first element and get our headers
contents = [headers]
rows = len(d)
columns = len(headers)
for i, data in d.items():
l = []
for k in headers:
if k == "command":
l.append(i)
else:
l.append(data[k])
contents.append(l)
showRows = min(15,rows) ### only so much rows will be shown before scrolling is necessary
### open popup window
win = tk.Toplevel()
win.wm_title("CPU Info")
rowCount = 0
### contents
### see
### https://stackoverflow.com/questions/43731784/tkinter-canvas-scrollbar-with-grid
### ...
frame_main = tk.Frame(win)
frame_main.grid(sticky='news')
label1 = tk.Label(frame_main, text="Instruction Set")
label1.grid(row=0, column=0, pady=(5, 0), sticky='nw')
#label2 = tk.Label(frame_main, text="Label 2", fg="blue")
#label2.grid(row=1, column=0, pady=(5, 0), sticky='nw')
#label3 = tk.Label(frame_main, text="Label 3", fg="red")
#label3.grid(row=3, column=0, pady=5, sticky='nw')
# Create a frame for the canvas with non-zero row&column weights
frame_canvas = tk.Frame(frame_main)
frame_canvas.grid(row=2, column=0, pady=(5, 0), sticky='nw')
frame_canvas.grid_rowconfigure(0, weight=1)
frame_canvas.grid_columnconfigure(0, weight=1)
# Set grid_propagate to False to allow 5-by-5 buttons resizing later
frame_canvas.grid_propagate(False)
# Add a canvas in that frame
canvas = tk.Canvas(frame_canvas)
canvas.grid(row=0, column=0, sticky="news")
# Link a scrollbar to the canvas
vsb = tk.Scrollbar(frame_canvas, orient="vertical", command=canvas.yview)
vsb.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=vsb.set)
# Create a frame to contain the buttons
frame_buttons = tk.Frame(canvas, bg="blue")
canvas.create_window((0, 0), window=frame_buttons, anchor='nw')
# Add buttons to the frame
contentEntries = [[tk.Entry() for j in range(columns)] for i in range(rows)]
for i in range(0, rows):
for j in range(0, columns):
string = contents[i][j]
if(i == 0): ### header line
contentEntries[i][j] = tk.Entry(frame_buttons, width=len(string)*3, fg='blue', font=('Arial',10,"bold"))
else:
contentEntries[i][j] = tk.Entry(frame_buttons, fg='black', font=('Arial',8))
contentEntries[i][j].grid(row=i, column=j, sticky='news')
contentEntries[i][j].insert(tk.END, string)
contentEntries[i][j].configure(state='readonly')
# Update buttons frames idle tasks to let tkinter calculate buttons sizes
frame_buttons.update_idletasks()
# Resize the canvas frame to show exactly n-by-n buttons and the scrollbar
show_columns_width = sum([contentEntries[0][j].winfo_width() for j in range(columns)])
show_columns_height = sum([contentEntries[i][0].winfo_height() for i in range(showRows)])
frame_canvas.config(width=show_columns_width + vsb.winfo_width(),
height=show_columns_height)
# Set the canvas scrolling region
canvas.config(scrollregion=canvas.bbox("all"))
### other stuff
b = tk.Label(frame_main, text="")
b.grid(row=11, column=0, sticky="we")
b = tk.Button(frame_main, text="Close", bd=6, command=win.destroy)
b.grid(row=12, column=0, sticky="we")
def onLoadClicked(self):
path = tkinter.filedialog.askopenfilename(initialdir="\%userprofile\%", filetypes =(("Assembler File", "*.asm"),("All Files","*.*")), title = "Choose a file.")
### ...
if path:
with open(path, 'r') as f:
self.code.text.delete(1.0, tk.END)
self.code.text.insert(1.0, f.read())
self.refreshCode()
self.cachedPath = path
def onSaveClicked(self, event=None):
### event is only relevant if we call this method via keypress (for example ctrl+s)
if not self.cachedPath:
path = tkinter.filedialog.asksaveasfilename(defaultextension=".asm")
else:
path = self.cachedPath
### ...
if path:
code = self.code.text.get(1.0, tk.END)
with open(path, 'w') as f:
f.write(code)
self.cachedPath = path
def onCheckClicked(self):
fullText = self.code.text.get("1.0", tk.END)
success, errors, pseudocode = self.checker.checkCode(fullText)
print(success)
print(errors)
print("================")
print(pseudocode)
### do stuff with the errors and shit ...
for error in errors:
lineNumber, lineType, lineKey, code, py = error
startIndex, endIndex = self.getLineIndex(lineNumber)
self.markErrorText("", startIndex, endIndex)
def onExportClicked(self):
code = self.code.text.get("1.0", tk.END)
success, errors, binary, lastWrittenByte = self.checker.assemble(code)
print("Assembly Result:")
print(" Success: %s" % success)
print(" Errors: %s" % errors)
print(" ROM: %s" % binary[:lastWrittenByte])
exportMode = self.exportMode.get()
extension = ""
extension = exportTypes[exportMode]
path = tkinter.filedialog.asksaveasfilename(defaultextension=extension)
if path:
if exportMode == 0:
# Create bytearray from list of integers.
byteArr = bytearray(binary)
with open(path, "wb") as f:
f.write(byteArr)
print("Binary file written [%s bytes]" % len(byteArr))
elif exportMode == 1:
### ascii format export
with open(path, 'w') as f:
for i, byte in enumerate(binary):
if i > 0 and i % 10 == 0:
f.write("\n")
f.write('%.2X ' % byte)
print("Ascii file written [%s bytes]" % len(binary))
#elif exportMode == 2:
# print("Export mode %s not supported" % exportMode)
#else:
# print("Export mode %s not supported" % exportMode)
def onTabPressed(self, event):
self.code.text.insert(tk.INSERT, " " * TABULATOR_INDENT_COUNT)
return 'break'
def onShiftTabPressed(self, event):
index = self.getCurrentIndex()
deletedLine = self.deleteLine(index)
spaces = self.getLineIndents(deletedLine)
if spaces >= 2:
deletedLine = deletedLine[2:]
else:
deletedLine = deletedLine.lstrip(" ")
self.code.text.insert(index, deletedLine)
#currentIndex = self.getCurrentIndex()
#currentLine, currentChar = self.getIndexCoordinates(currentIndex)
#targetCharIndex = int(currentChar) - TABULATOR_INDENT_COUNT
#targetCharIndex = 0 if targetCharIndex < 0 else targetCharIndex
#self.code.text.delete( "%s.%s" % (currentLine, targetCharIndex), tk.INSERT) ### indieces have to be reverted when deleting ... example (6.0, 6.2) -.-
return 'break'
### iterate through teh code and yield each and every line one after another
def codeLineIterator(self):
lastLine = int(self.code.text.index("end").split(".")[0])
for lineIndex in range(0, lastLine+1):
### defien inieces
startIndex = "%s.0" % lineIndex
endIndex = "%s.end" % lineIndex
#### grab current
line = self.code.text.get(startIndex, endIndex)
yield startIndex, endIndex, line
def refreshCode(self):
### go through all lines of our text widget and re-process each and every line
### so that things get marked and colored properly again
for startIndex, endIndex, line in self.codeLineIterator():
### re-process line
self.processLine(line, startIndex, endIndex)
def onCodeChanged(self, args):
#print(args)
## allow cahracters and some specific control keys to start the parsign of teh line (to color text and stuff)
if args.keysym == "BackSpace" or args.keysym == "Delete" or args.keysym == "Return" or args.keysym == "Tab" or args.keysym == "Control_L" or args.char and args.char.isprintable():
#print("onCodeChanged()")
#print("compare", self.text.get(1.0, tk.END))
### go through current line and check for labels to change their color
lineStartIndex, lineEndIndex = self.getCurrentLineIndieces()
line = self.getLineTextBetween(lineStartIndex, lineEndIndex)
lineType, lineKey = self.processLine(line, lineStartIndex, lineEndIndex)
### remember indent when going to next line
if(args.keysym == "Return"):
currentLine, currentChar = self.getIndexCoordinates(lineStartIndex)
lastLine = self.getPreviousLineText()
lastSpaces = self.getLineIndents(lastLine)
self.code.text.insert(lineStartIndex, " " * lastSpaces)
def processLine(self, line, lineStartIndex, lineEndIndex):
lineType, lineKey = self.checker.checkLine(line)
if lineType == AssemblerChecker.LINETYPE.LABEL:
self.markLabelText(lineKey, lineStartIndex, lineEndIndex)
elif lineType == AssemblerChecker.LINETYPE.ADDRESS:
self.markAddressText(lineKey, lineStartIndex, lineEndIndex)
elif lineType == AssemblerChecker.LINETYPE.SYMBOLDEF:
self.markSymbolDefinitionText(lineKey, lineStartIndex, lineEndIndex)
elif lineType == AssemblerChecker.LINETYPE.COMMENT:
self.markCommentText(lineKey, lineStartIndex, lineEndIndex)
### check others ...
else:
self.unmarkText(line, lineStartIndex, lineEndIndex)
return lineType, lineKey
def markLabelText(self, name, indexStart, indexEnd):
### mark text given by indieces as label
self.code.text.tag_add("label", indexStart, indexEnd)
def markAddressText(self, name, indexStart, indexEnd):
### mark text given by indieces as label
currentLine, currentChar = self.getIndexCoordinates(indexStart)
lastLine, lastChar = self.getIndexCoordinates(indexEnd)
pre1 = indexStart
self.code.text.tag_add("address", indexStart, "%s.0" % str(int(currentLine)+1))
def markSymbolDefinitionText(self, name, indexStart, indexEnd):
self.code.text.tag_add("symbol", indexStart, indexEnd)
def markCommentText(self, name, indexStart, indexEnd):
self.code.text.tag_add("comment", indexStart, indexEnd)
def unmarkText(self, text, indexStart, indexEnd):
currentLine, currentChar = indexStart.split(".")
#lastLine, lastChar = indexEnd.split(".")
### remove all tags and marks and return the text to normal
for tag in self.code.text.tag_names():
#self.code.text.tag_remove(tag, indexStart, indexEnd)
self.code.text.tag_remove(tag, indexStart, "%s.0" % str(int(currentLine)+1))
def markErrorText(self, text, indexStart, indexEnd):
### remove tags from the line in question
self.unmarkText(text,indexStart, indexEnd)
### mark line as error (red backgroudn and stuff)
self.code.text.tag_add("error", indexStart, indexEnd)
def getPreviousLineIndieces(self):
start, end = self.getCurrentLineIndieces()
return self.jumpIndex(start, -1, 0)
def getCurrentLineIndieces(self):
lineStartIndex = self.code.text.index("insert linestart")
lineEndIndex = self.code.text.index("insert lineend")
return lineStartIndex, lineEndIndex
def getCurrentIndex(self):
return self.code.text.index("insert")
def getIndexCoordinates(self, index):
return index.split(".") # currentLine, currentChar
def getLineIndex(self, lineNumber):
startIndex = "%s.0" % lineNumber
endIndex = "%s.end" % lineNumber
return startIndex, endIndex
def getPreviousLineText(self):
currentLine, currentChar = self.getIndexCoordinates(self.getCurrentIndex())
prev = self.getPreviousLineIndieces()
return self.getFullLineText(prev)
def getCurrentLineText(self):
start, end = self.getCurrentLineIndieces()
return self.getLineTextBetween(start,end)
def getFullLineText(self, index):
line, char = self.getIndexCoordinates(index)
return self.code.text.get("%s.0" % line, "%s.end" % line)
def getLineTextBetween(self, start, end):
return self.code.text.get(start, end)
def getLineIndents(self, line):
return len(line) - len(line.lstrip(" "))
def jumpIndex(self, index, jumpy, jumpx):
line, char = self.getIndexCoordinates(index)
return "%s.%s" % ( int(line) + jumpy, int(char)+jumpx)
def deleteCurrentLine(self):
index = self.getCurrentIndex()
deletedText = self.deleteLine(index)
return deletedText
def deleteLine(self, index):
line, char = self.getIndexCoordinates(index)
deletedText = self.getFullLineText(index)
self.code.text.delete("%s.0" % line, "%s.end" % line)
return deletedText
######################################################################################################################
def main():
a = AssemblerGui()
a.run()
if __name__ == "__main__":
main()