-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinal code.py
More file actions
2266 lines (1975 loc) · 112 KB
/
final code.py
File metadata and controls
2266 lines (1975 loc) · 112 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#add and sub instructions are right
#input pass test
#rol and ror and translated into 11 byte binary format
#hlt passes test
#no calling of set flags after each operand
#no calling of assemble operands for some instructions
import sys #for hlt button
import tkinter as tk #for interface creation
import tkinter.filedialog #for file picker button
import copy #to use method to copy dictionaries
import re #to search positio of a lower case letter in a string
class App(tk.Frame):
""""application class from wchich main program will be launched"""
def __init__(self, master):
tk.Frame.__init__(self, master)
self.pack()
self.master.title("x86 emulator") #set title
self.master.tk_setPalette(background='lavender') #set background color
# drawing the main window and its widgets
#self.master.overrideredirect(True)
#self.master.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
w, h = master.winfo_screenwidth(), master.winfo_screenheight()
master.geometry("%dx%d+0+0" % (w, h))
#label
title_label=tk.Label(master, text="x8086 emulator") #title
title_label.configure(font=("Helvetica", 12, "bold")) #configure title
#frames
editor_and_console_frame=tk.Frame(master) #I had to create this frame to position widgets as I intended to
self.text_frame= tk.LabelFrame(editor_and_console_frame, text="Code editor" )
self.text_frame.configure(font=("Arial", 11))
manipulation_button_frame = tk.Frame(self.text_frame) #for code manipulation buttons
console_frame=tk.LabelFrame(editor_and_console_frame, text="Console")
console_frame.configure(font=("Arial", 11))
speed_button_frame = tk.Frame(console_frame) #for speed control
diagram_frame=tk.LabelFrame(master, text="Architecture Diagram")
diagram_frame.configure(font=("Arial", 11))
#text editor
self.text_editor = tk.Text(self.text_frame,height=30,width=50, bg='navajo white')
DAT_text="MY_DATA SEGEMENT PARA STACK #this is a comment \nVAR DW 0 #this is a user variable \nMY_DATA ENDS \nSTART" #use 0 instead of DUP(?)
self.text_editor.insert(tk.END, DAT_text)
self.text_editor.configure(font=("Courier New", 10)) #set the font style and size in text editor (same as python)
#console
self.console_window = tk.Text(console_frame, height=20,width=50, state='disabled')
#buttons
help=tk.Button(manipulation_button_frame, text='Help', command=self.click_help)
help.configure(font=("Calibri", 12), relief="groove") #each button has same configuration
load=tk.Button(manipulation_button_frame, text='Load', command=self.file_picker)
load.configure(font=("Calibri", 12), relief="groove")
save=tk.Button(manipulation_button_frame, text='Save', command=self.click_save)
save.configure(font=("Calibri", 12), relief="groove")
compile=tk.Button(manipulation_button_frame, text='Compile', command=self.click_compile)
compile.configure(font=("Calibri", 12), relief="groove")
clear=tk.Button(manipulation_button_frame, text='Clear', comman=self.click_clear)
clear.configure(font=("Calibri", 12), relief="groove")
run=tk.Button(speed_button_frame, text='Run', command=lambda: self.wait_var.set(2))
run.configure(font=("Calibri", 12), relief="groove")
halt=tk.Button(speed_button_frame, text='Halt', command=self.click_halt)
halt.configure(font=("Calibri", 12), relief="groove")
self.wait_var=tk.IntVar()
self.step=tk.Button(speed_button_frame, text='Step', command=lambda: self.wait_var.set(1)) # change wait variable to onw
self.step.configure(font=("Calibri", 12), relief="groove")
#architecture diagram components: RAM, CPU, buses, I/O boxes and clock cycle counter box
#RAM
self.ram_frame=tk.LabelFrame(diagram_frame, text="RAM")
RAM_HEIGHT = 16
RAM_WIDTH = 8
counter=0
for i in range(0,RAM_HEIGHT): #Rows
for j in range(0,RAM_WIDTH): #Columns
if i%2==0:
self.entryfield_name=tk.Label(self.ram_frame, text=str(counter).zfill(2))
#place labels on even number of rows (row counter starts from 0)
self.entryfield_name.grid(in_=self.ram_frame, row=i, column=j)
self.entryfield_name.configure(font=("Calibri", 12))
counter+=1
else:
self.defaulttext=tk.StringVar() # default value of the field
self.entryfield = tk.Entry(self.ram_frame, width=8, borderwidth=5, textvariable=self.defaulttext)
self.defaulttext.set("0000") #fill in the box
self.entryfield.grid(in_=self.ram_frame, row=i, column=j) #place entry field boxes on odd number of rows
self.entryfield.configure(font=("Calibri", 12), justify='center', state='readonly')
#for entryfield in ram_frame.grid_slaves():
# if int(entryfield.grid_info()['row'])%2 != 0:
# entryfield.grid_remove() ####!!!!! instead of going through an new for loop and deleting the spaces
#place them in functional form
#opcode=tk.StringVar() # default value of the field
#opcode_label = tk.Entry(ram_frame, width=6, borderwidth=5, textvariable=defaulttext)
#defaulttext.set("0000") #fill in the box
#entryfield.grid(in_=ram_frame, row=i, column=j, ipady=5) #place entry field boxes on odd number of rows
#entryfield.configure(font=("Calibri", 12), justify='center', state='readonly')
#CPU components
cpu_frame=tk.LabelFrame(diagram_frame, text="CPU")
#PC
pc_frame=tk.LabelFrame(cpu_frame)
pc_label=tk.Label(pc_frame, text="PC")
pc_label.configure(font=("Calibri", 12))
self.pc_text=tk.StringVar() # default value of the field
self.pc_text.set("00") #fill in the box
self.pc_box=tk.Entry(pc_frame, width=6, borderwidth=5, textvariable=self.pc_text)
self.pc_box.configure(font=("Calibri", 12), justify='center', state='readonly')
#MAR
mar_frame=tk.LabelFrame(cpu_frame)
mar_label=tk.Label(mar_frame, text="MAR")
mar_label.configure(font=("Calibri", 12))
self.mar_text=tk.StringVar() # default value of the field
self.mar_text.set("0") #fill in the box
self.mar_box=tk.Entry(mar_frame, width=6, borderwidth=5, textvariable=self.mar_text)
self.mar_box.configure(font=("Calibri", 12), justify='center', state='readonly')
#MDR
mdr_frame=tk.LabelFrame(cpu_frame)
mdr_label=tk.Label(mdr_frame, text="MDR")
mdr_label.configure(font=("Calibri", 12))
self.mdr_text=tk.StringVar() # default value of the field
self.mdr_text.set("0") #fill in the box
self.mdr_box=tk.Entry(mdr_frame, width=6, borderwidth=5, textvariable=self.mdr_text)
self.mdr_box.configure(font=("Calibri", 12), justify='center', state='readonly')
#ALU contain: registers and flags
alu_frame=tk.LabelFrame(cpu_frame)
#registers
registers_frame=tk.LabelFrame(alu_frame, text="Registers")
registers_frame.configure(font=("Calibri", 14, 'bold'))
ax_label=tk.Label(registers_frame, text="AX")
ax_label.configure(font=("Calibri", 12))
self.ax_text=tk.StringVar() # default value of the field
self.ax_text.set("0") #fill in the box
self.ax_box=tk.Entry(registers_frame, width=6, borderwidth=5, textvariable=self.ax_text)
self.ax_box.configure(font=("Calibri", 12), justify='center', state='readonly')
bx_label=tk.Label(registers_frame, text="BX")
bx_label.configure(font=("Calibri", 12))
self.bx_text=tk.StringVar() # default value of the field
self.bx_text.set("0") #fill in the box
self.bx_box=tk.Entry(registers_frame, width=6, borderwidth=5, textvariable=self.bx_text)
self.bx_box.configure(font=("Calibri", 12), justify='center', state='readonly')
cx_label=tk.Label(registers_frame, text="CX")
cx_label.configure(font=("Calibri", 12))
self.cx_text=tk.StringVar() # default value of the field
self.cx_text.set("0") #fill in the box
self.cx_box=tk.Entry(registers_frame, width=6, borderwidth=5, textvariable=self.cx_text)
self.cx_box.configure(font=("Calibri", 12), justify='center', state='readonly')
dx_label=tk.Label(registers_frame, text="DX")
dx_label.configure(font=("Calibri", 12))
self.dx_text=tk.StringVar() # default value of the field
self.dx_text.set("0") #fill in the box
self.dx_box=tk.Entry(registers_frame, width=6, borderwidth=5, textvariable=self.dx_text)
self.dx_box.configure(font=("Calibri", 12), justify='center', state='readonly')
#flags and ALU box
flags_and_ALU_box_frame=tk.Frame(alu_frame)
ALU_box_frame=tk.LabelFrame(flags_and_ALU_box_frame)
flags_frame=tk.LabelFrame(flags_and_ALU_box_frame, text="Flags")
flags_frame.configure(font=("Calibri", 14, 'bold'))
of_label=tk.Label(flags_frame, text="OF")
self.of_text=tk.StringVar() # default value of the field
self.of_text.set("0") #fill in the box
self.of_box=tk.Entry(flags_frame, width=6, borderwidth=5, textvariable=self.of_text)
self.of_box.configure(font=("Calibri", 12), justify='center', state='readonly')
sf_label=tk.Label(flags_frame, text="SF")
self.sf_text=tk.StringVar() # default value of the field
self.sf_text.set("0") #fill in the box
self.sf_box=tk.Entry(flags_frame, width=6, borderwidth=5, textvariable=self.sf_text)
self.sf_box.configure(font=("Calibri", 12), justify='center', state='readonly')
zf_label=tk.Label(flags_frame, text="ZF")
self.zf_text=tk.StringVar() # default value of the field
self.zf_text.set("0") #fill in the box
self.zf_box=tk.Entry(flags_frame, width=6, borderwidth=5, textvariable=self.zf_text)
self.zf_box.configure(font=("Calibri", 12), justify='center', state='readonly')
self.alu_label=tk.Label(ALU_box_frame, text="ALU")
self.alu_label.configure(font=("Calibri", 14, 'bold'))
self.alu_text=tk.StringVar() # default value of the field
self.alu_text.set("0") #fill in the box
#CU contains CIR and dcode table
cu_frame=tk.LabelFrame(cpu_frame)
#CU as a separate box
cu_label_frame=tk.LabelFrame(cu_frame)
self.cu_label=tk.Label(cu_label_frame, text="CU ")
self.cu_label.configure(font=("Calibri", 14, 'bold'))
#CIR
cir_frame=tk.LabelFrame(cu_frame)
cir_label=tk.Label(cir_frame, text="CIR")
cir_label.configure(font=("Calibri", 12))
cir_boxes_frame=tk.Frame(cir_frame)
mnemonic_frame=tk.Frame(cir_boxes_frame)
mnemonic_label=tk.Label(mnemonic_frame, text="opcode")
self.mnemonic_text=tk.StringVar() # default value of the field
self.mnemonic_box=tk.Entry(mnemonic_frame, width=6, borderwidth=5, textvariable=self.mnemonic_text)
self.mnemonic_text.set("0") #fill in the box
self.mnemonic_box.configure(font=("Calibri", 12), justify='center', state='readonly')
operand1_frame=tk.Frame(cir_boxes_frame)
operand1_label=tk.Label(operand1_frame, text="operand1")
self.operand1_text=tk.StringVar() # default value of the field
self.operand1_box=tk.Entry(operand1_frame, width=6, borderwidth=5, textvariable=self.operand1_text)
self.operand1_text.set("0") #fill in the box
self.operand1_box.configure(font=("Calibri", 12), justify='center', state='readonly')
operand2_frame=tk.Frame(cir_boxes_frame)
operand2_label=tk.Label(operand2_frame, text="operand2")
self.operand2_text=tk.StringVar() # default value of the field
self.operand2_box=tk.Entry(operand2_frame, width=6, borderwidth=5, textvariable=self.operand2_text)
self.operand2_text.set("0") #fill in the box
self.operand2_box.configure(font=("Calibri", 12), justify='center', state='readonly')
#decode unit
decode_frame=tk.LabelFrame(cu_frame)
decode_label=tk.Label(decode_frame, text="Decode Unit")
decode_label.configure(font=("Calibri", 12))
self.decode_table=tk.Frame(decode_frame)
#decode table
instruction_label=tk.Label(self.decode_table, text="Instruction")
instruction_label.configure(font=("Calibri", 12), relief="groove")
opcode_label=tk.Label(self.decode_table, text="Opcode")
opcode_label.configure(font=("Calibri", 12), relief="groove")
row_counter=1
for key in decode_dic.keys():
key_label=tk.Label(self.decode_table, text=key)
key_label.configure(font=("Calibri", 11), relief="groove")
key_label.grid(row=row_counter, column=0, sticky="news")
value_label=tk.Label(self.decode_table, text=decode_dic[key])
value_label.configure(font=("Calibri", 11), relief="groove")
value_label.grid(row=row_counter, column=1, sticky="news")
row_counter+=1
#frame for I/O and
input_output_frame=tk.Frame(diagram_frame)
input_frame=tk.LabelFrame(input_output_frame)
input_label=tk.Label(input_frame, text="INP")
input_label.configure(font=("Calibri", 12))
input_box_text=tk.IntVar()
self.input_box=tk.Entry(input_frame, width=6, borderwidth=5, textvariable=input_box_text)
input_box_text.set(0)
self.input_box.configure(font=("Calibri", 12), justify='center', state='normal', bg='navajo white')
#submit button
self.var=tk.IntVar()
self.submit=tk.Button(input_frame, text='Submit', command=lambda: self.var.set(1))
self.submit.configure(font=("Calibri", 12), relief="groove")
output_frame=tk.LabelFrame(input_output_frame)
output_label=tk.Label(output_frame, text="OUT")
output_label.configure(font=("Calibri", 12))
self.output_text=tk.StringVar() # default value of the field
output_box=tk.Entry(output_frame, width=6, borderwidth=5, textvariable=self.output_text)
self.output_text.set("0") #fill in the box
output_box.configure(font=("Calibri", 12), justify='center', state='readonly')
#clock counter
cycle_frame=tk.Frame(diagram_frame)
cycle_label=tk.Label(cycle_frame, text="clock cycle counter")
cycle_label.configure(font=("Calibri", 12))
cycle_box=tk.Entry(cycle_frame, width=6, borderwidth=5)
cycle_box.configure(font=("Calibri", 12), justify='center', state='readonly')
#packing widgets
#master frames
title_label.pack(anchor='n')
editor_and_console_frame.pack(anchor='n', side='left')
diagram_frame.pack(anchor='n',side='left', fill='both', expand=True) # fill in the remaining space in the master root
#editor and console frames
self.text_frame.pack(side='top')
console_frame.pack(anchor='n')
#button frame code manipulation buttons
manipulation_button_frame.pack(side='top')
self.text_editor.pack(side='top')
load.pack(side='left', padx=5, pady=5)
save.pack(side='left', padx=5)
compile.pack(side='left', padx=5)
clear.pack(side='left', padx=5)
help.pack(side='left', padx=5)
#button frame speed buttons
speed_button_frame.pack(side='top')
self.console_window.pack(side='top')
run.pack(side='left', padx=5, pady=5)
self.step.pack(side='left', padx=5)
halt.pack(side='left', padx=5)
#frames in diagram_frames
self.ram_frame.pack(anchor='n', side='right')
cpu_frame.pack( side='left', fill='both', expand=1)
master.update() #GUI needs to be refreshed after cpu_frame is filled in with widgets
#to use new values of diagram_frame.winfo_height() and width()
#otherwise height and width equal to default value one
#bus canvas needs to be intialized after other widgets in diagram_frame root widget
bus_canvas = tk.Canvas(diagram_frame, width=300, height=diagram_frame.winfo_height())
bus_canvas.pack(side='left')
input_output_frame.place(x=700, y=600)
cycle_frame.place(x=900, y=600)
#frames inside cpu frame: pc, mar, alu, mdr, cu
#pc
pc_frame.grid(row=0, column=0)
#mar
mar_frame.grid(row=0, column=1)
cpu_frame.columnconfigure(0, weight=1)#configure columns so pc and mar frames
#occupy same amount of space in their root widget
cpu_frame.columnconfigure(1, weight=1)
#alu
alu_frame.grid(row=1, column=0, sticky="news")
#mdr
mdr_frame.grid(row=1, column=1)
cpu_frame.rowconfigure(0, weight=1)#configure rows so that they occupy same amount of space
#in their root widget
cpu_frame.rowconfigure(1, weight=1)
#cu
cu_frame.grid(row=2, column=0, sticky="news", columnspan=2)
cpu_frame.rowconfigure(2, weight=2)
#widgets inside pc_frame
pc_label.pack()
self.pc_box.pack()
#widgets inside mar frame
mar_label.pack()
self.mar_box.pack()
#widgets inside mdr frame
mdr_label.pack()
self.mdr_box.pack()
#widgets inside alu frame: registers, flags and alu frame
registers_frame.pack(side='top')
flags_and_ALU_box_frame.pack(side='top')
#widgets inside flags and alu frame
flags_frame.pack(side='left')
ALU_box_frame.pack(side='left', padx=20, pady=20)
#widgets inside registers frame
ax_label.pack(side='left')
self.ax_box.pack(side='left')
bx_label.pack(side='left')
self.bx_box.pack(side='left')
cx_label.pack(side='left')
self.cx_box.pack(side='left')
dx_label.pack(side='left')
self.dx_box.pack(side='left')
#widgets inside flags frame
of_label.pack(side='left')
self.of_box.pack(side='left')
sf_label.pack(side='left')
self.sf_box.pack(side='left')
zf_label.pack(side='left')
self.zf_box.pack(side='left')
#widgets inside alu frame
self.alu_label.pack()
#widgets inside CU frame: cir, decode grid
decode_frame.pack(side="left")
cu_label_frame.pack(side="right", anchor='n', pady=200)
cir_frame.pack(side="right", anchor='s', pady=100)
#cu label
self.cu_label.pack()
#widgets inside decode frame
decode_label.pack()
self.decode_table.pack()
#widgets insdie decode_table
instruction_label.grid(row=0, column=0, sticky="news")
opcode_label.grid(row=0, column=1, sticky="news")
self.decode_table.columnconfigure(0, weight=1)
self.decode_table.columnconfigure(1, weight=1)
#widgets insede cir frame
cir_label.pack(side='top')
cir_boxes_frame.pack(side='top')
#widgets inside cir boxes frame
mnemonic_frame.pack(side='left')
operand1_frame.pack(side='left')
operand2_frame.pack(side='left')
#widgets inside mnemonic frame
mnemonic_label.pack(side='top')
self.mnemonic_box.pack(side='top')
#widgets inside operand1 frame
operand1_label.pack(side='top')
self.operand1_box.pack(side='top')
#widgets inside operand2 frame
operand2_label.pack(side='top')
self.operand2_box.pack(side='top')
#widgets insdie canvas for buses:
bus_canvas.create_rectangle(0, 40, 300, 80, fill="light blue")
addresss_bus_box = bus_canvas.create_text((50, 60), text="Address Bus")
bus_canvas.create_rectangle(0, 140, 300, 180, fill="dodger blue")
data_bus_box = bus_canvas.create_text((50, 160), text="Data Bus")
bus_canvas.create_rectangle(0, 400, 300, 440, fill="light slate blue")
control_bus_box = bus_canvas.create_text((50, 420), text="Control Bus")
#add labels on top of buses
self.address_bus_label = tk.Label(bus_canvas, text='')
self.address_bus_label.config(font=("Calibri", 12))
self.address_bus_label.pack()
bus_canvas.create_window(50, 20, window=self.address_bus_label)
self.data_bus_label = tk.Label(bus_canvas, text='')
self.data_bus_label.config(font=("Calibri", 12))
self.data_bus_label.pack()
bus_canvas.create_window(50, 120, window=self.data_bus_label)
self.control_bus_label = tk.Label(bus_canvas, text='')
self.control_bus_label.config(font=("Calibri", 12))
self.control_bus_label.pack()
bus_canvas.create_window(50, 380, window=self.control_bus_label)
#widgets inside input&output frame
input_frame.pack(side='left')
input_label.pack(side='top')
self.input_box.pack(side='top')
self.submit.pack(side='top')
output_frame.pack(side='left')
output_label.pack(side='top')
output_box.pack(side='top')
#widgets inside cycle coutner frame
cycle_label.pack(side='top')
cycle_box.pack(side='top')
#button functions
def click_compile(self):
original=self.text_editor.get("1.0", 'end-1c')
standardize(original)
def file_picker(self, event=None):
try:
#create filedialog instance and open file in read mode
file = tk.filedialog.askopenfile(parent=root, mode='r', title='Choose a file')
#get content from the file
data = file.read()
#get rid of added characters at the end of the file
data=data.rstrip()
#insert the contents of the file to the end of the text widget
self.text_editor.insert(tk.END, data)
file.close()
except:
text="\nfile of this format cannot be loaded into text editor"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
def click_clear(self):
#renew all variables and registers
global registers_dic
registers_dic = { x:0 for x in registers_dic}
global user_vars_dic
user_vars_dic = { x:0 for x in user_vars_dic}
#empty ram representation by derawaing it
RAM_HEIGHT = 16
RAM_WIDTH = 8
counter=0
for i in range(0,RAM_HEIGHT): #Rows
for j in range(0,RAM_WIDTH): #Columns
if i%2==0:
self.entryfield_name=tk.Label(self.ram_frame, text=str(counter).zfill(2))
#place labels on even number of rows (row counter starts from 0)
self.entryfield_name.grid(in_=self.ram_frame, row=i, column=j)
self.entryfield_name.configure(font=("Calibri", 12))
counter+=1
else:
self.defaulttext=tk.StringVar() # default value of the field
self.entryfield = tk.Entry(self.ram_frame, width=8, borderwidth=5, textvariable=self.defaulttext)
self.defaulttext.set("0000") #fill in the box
self.entryfield.grid(in_=self.ram_frame, row=i, column=j) #place entry field boxes on odd number of rows
self.entryfield.configure(font=("Calibri", 12), justify='center', state='readonly')
#empty console window
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.delete('1.0', tk.END)
app.console_window.config(state="disabled") #and then change it back to disabled
print("USER DEFINED VARIABLES:", user_vars_dic)
print("FLAGS:", flags_dic)
print("REGISTERS:", registers_dic)
#clear all boxes in architecture diagra and reste background colors to default
app.pc_box.config(readonlybackground="SystemButtonFace")
app.pc_text.set(0)
app.mar_box.config(readonlybackground="SystemButtonFace")
app.mar_text.set(0)
app.mdr_box.config(readonlybackground="SystemButtonFace")
app.mdr_text.set(0)
app.operand1_box.config(readonlybackground="SystemButtonFace")
app.operand1_text.set(0)
app.operand2_box.config(readonlybackground="SystemButtonFace")
app.operand2_text.set(0)
app.mnemonic_box.config(readonlybackground="SystemButtonFace")
app.mnemonic_text.set(0)
#clear all registers
app.ax_box.config(readonlybackground="SystemButtonFace")
app.ax_text.set(0)
app.bx_box.config(readonlybackground="SystemButtonFace")
app.bx_text.set(0)
app.cx_box.config(readonlybackground="SystemButtonFace")
app.cx_text.set(0)
app.dx_box.config(readonlybackground="SystemButtonFace")
app.dx_text.set(0)
#clear all flags
app.of_box.config(readonlybackground="SystemButtonFace")
app.of_text.set(0)
app.zf_box.config(readonlybackground="SystemButtonFace")
app.zf_text.set(0)
app.sf_box.config(readonlybackground="SystemButtonFace")
app.sf_text.set(0)
#set ALU and CU boxes to default
app.cu_label.config(bg="SystemButtonFace")
app.alu_label.config(bg="SystemButtonFace")
#set all values in decode table to default color
for label in app.decode_table.grid_slaves(): #change the color back
label.config(bg='lavender')
def click_help(self):
#create a top up window
help_frame=tk.Toplevel(app.master)
help_frame.title("Help menu")
#draw a fullscreen window
w, h = help_frame.winfo_screenwidth(), help_frame.winfo_screenheight()
help_frame.geometry("%dx%d+0+0" % (w, h))
#instruction and directives
manual=tk.Message(help_frame, text="How to use: \n1.input into editor (by either pressing load button and choosing an example to load or typing directly into the editor) \n2.click compile button \n3.if there are no errors in the code the program will be assembled into ram and user notified \n4 press press run or compile button to run the program and see animation \n below are tables to explain purpose of each mnemonic and architecture diagram")
manual.pack()
#insert tables with explanations
photo = tk.PhotoImage(file="help table 1.gif")
Artwork1 = tk.Label(help_frame, image=photo)
Artwork1.photo = photo
Artwork1.pack(side='left')
photo = tk.PhotoImage(file="help table 2.gif")
Artwork2 = tk.Label(help_frame, image=photo)
Artwork2.photo = photo
Artwork2.pack(side='left')
photo = tk.PhotoImage(file="registers table.gif")
Artwork3 = tk.Label(help_frame, image=photo)
Artwork3.photo = photo
Artwork3.pack(side='left')
#old version of the module
#canvas = tk.Canvas(help_frame)
#canvas.pack()
#img=tkinter.PhotoImage(file="help table 2.gif")
#canvas.image = img
#canvas.create_image(500,500,image=img)
def click_save(self):
f = tk.filedialog.asksaveasfile(mode='w', defaultextension=".txt")
if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
return
text2save = str(app.text_editor.get(1.0, tk.END)) # starts from `1.0`, not `0.0`
f.write(text2save)
f.close() # `()` was missing.
def click_halt(self):
sys.exit()
def standardize(input):
if input=="":
error_generator(0,"empty")
#split input from code into list of separate lines
lines=input.splitlines()
#remove plane space (blank lines) from the submitted code
while '' in lines:
lines.remove('')
formatted_list=[]
for line in lines:
line_num=lines.index(line)#used to retrieve current line
#remove comments (after # charachter)
sep= '#'
line=line.split(sep, 1)[0]
words=line.split(" ")
while '' in words:
words.remove('')
#validation rules
#check if indentation is correct
for word in words:
if word.startswith("\t"):
if word=="\t": #output an error if the word is just a single indent
text="\nunexpected indent at line "+str(line_num+1)
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
elif words.index(word)==0 and word[0:2]=="\t\t": #ouput an error if the line has multiple indents
text="\nunexpected indent at line "+str(line_num+1)
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
elif words.index(word)==0 and word[0:1]=="\t": #only correct when indent is used on the first word in the line and if it is a single indent
words[words.index(word)]=word.replace("\t","") #delete \t from the word
words.insert(0,' ') #add white space before word to show the word is indented in 2D array structure
else:
text="\nunexpected indent at line "+str(line_num)
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
#error_generator(line_num, "indent")
while len(words)<4:
words.append(' ')
if len(words)>4:
text="\nunexpected operand at line "+str(line_num+1)
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
#error_generator(line_num, "operand_error")
formatted_list+=words
global ROWLENGTH
ROWLENGTH=4
global NUMBEROFROWS
NUMBEROFROWS=len(lines)
twodarray=[[' ' for row in range(ROWLENGTH)] for column in range(NUMBEROFROWS)]
counter=0
global start_row #pointer to keep track of the row from which the main program starts
start_row=0
for i in range(0,NUMBEROFROWS): #Row
for j in range(0,ROWLENGTH): #Columns
twodarray[i][j]=formatted_list[counter]
counter+=1
#the intialize_user_defined_var function is called
#at the end of standardize() function
global error_flag
error_flag=False
pc=start_row
try:
if twodarray[0]==['MY_DATA', 'SEGEMENT', 'PARA', 'STACK']:
intialize_user_defined_variables(twodarray)
global user_vars_dic
initialize_user_defined_labels(twodarray)
pc=start_row
#during syntax analysis no input is needed so variable for waiting/interrup is set to 1
while pc!=NUMBEROFROWS:
#includes translate intro RAM and detect any syntax errors
#initialize error free flag here
pc, answer=syntax_analysis(twodarray, pc, False)
pc+=1
#set all variables and registers to 0 and pc to its starting position
global registers_dic
registers_dic = { x:0 for x in registers_dic}
intialize_user_defined_variables(twodarray)
pc=start_row
except:
pass
#start function to wait for run/step button click
#if error free flage is True
#assemble into ram()
if error_flag==False:
text="\ncode has been successfully compiled"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
step_or_run(twodarray, pc, start_row)
else:
#clear RAM
RAM_HEIGHT = 16
RAM_WIDTH = 8
counter=0
for i in range(0,RAM_HEIGHT): #Rows
for j in range(0,RAM_WIDTH): #Columns
if i%2==0:
app.entryfield_name=tk.Label(app.ram_frame, text=str(counter).zfill(2))
#place labels on even number of rows (row counter starts from 0)
app.entryfield_name.grid(in_=app.ram_frame, row=i, column=j)
app.entryfield_name.configure(font=("Calibri", 12))
counter+=1
else:
app.defaulttext=tk.StringVar() # default value of the field
app.entryfield = tk.Entry(app.ram_frame, width=8, borderwidth=5, textvariable=app.defaulttext)
app.defaulttext.set("0000") #fill in the box
app.entryfield.grid(in_=app.ram_frame, row=i, column=j) #place entry field boxes on odd number of rows
app.entryfield.configure(font=("Calibri", 12), justify='center', state='readonly')
text="\ncode cannot be compiled"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
def assemble_to_ram(twodarray, pc, start_row):
pc=start_row
while pc!=NUMBEROFROWS:
line=twodarray[pc]
def step_or_run(twodarray,pc, start_row):
#wait for button click (either run or step)
app.step.wait_variable(app.wait_var)
#retrieve wait_var value as a python object (since it is a tkinter variable class)
wait_var=app.wait_var.get()
#if step button is clicked the variable is set to zero
app.console_window.tag_configure("make_bold", font=("Calibri", 12, 'bold')) #create tags to highligh specific lines of code in text and not all of the text widget
if wait_var==1:
while pc!=NUMBEROFROWS:
text="\nCURRENT LINE:\t"+str(twodarray[pc])
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text, "make_bold") #make the current insruction line bold
####at first i tried using
#app.console_window.tag_add("make_bold", "end-1c linestart", "end") #but it highlighted all of the text widtet
#
#
app.console_window.config(state="disabled") #and then change it back to disabled
app.step.wait_variable(app.wait_var) #interrupt
fetch(pc, twodarray, start_row)
decode(pc, twodarray, start_row)
global registers_dic
pc, answer=execute(pc, twodarray, start_row)
#includes translate intro RAM and detect any syntax errors
global user_vars_dic
text="\nUPDATED REGISTERS \t" + str(registers_dic)+"\nUPDATED VARIABLES \t" + str(user_vars_dic)+"\nUPDATED FLAGS \t" + str(flags_dic)
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
pc+=1
#execute the code with an interrup at the end of while loop to wait for step button click
app.step.wait_variable(app.wait_var)
#if run is clicked the variable is set to 2
else:
#code is executed without an interrupt
while pc!=NUMBEROFROWS:
text="\nCURRENT LINE:\t"+str(twodarray[pc])
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text, "make_bold")
#includes translate intro RAM and detect any syntax errors
pc, answer=syntax_analysis(twodarray, pc, True)
text="\nUPDATED REGISTERS \t" + str(registers_dic)+"\nUPDATED VARIABLES \t" + str(user_vars_dic)+"\nUPDATED FLAGS \t" + str(flags_dic)
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
pc+=1
def fetch(pc, twodarray, start_row):
app.pc_box.config(readonlybackground="MistyRose2")
address=app.pc_text.get() #copy value from accumulator
retrieve_direct_address(address)
#copy contents of MDR to CIR
#but first the temp needs to be separated into mnemonic oparand1 and operand 2 parts
address=app.mdr_text.get()
app.mnemonic_text.set(address[0:2]) #mnemonic is always first 2 digits of the address
try:
start = re.search("[a-z]", address[::]).start() #find position of the first lower case letter
end =len(address)-1-re.search("[a-z]", address[::-1]).start() #find positoin of the last lowercase letter by searching the reversed string
if start==end: #if there is only one operand
app.operand1_text.set(address[start:])
app.operand2_text.set("")
else: #if there are 2 operands
app.operand1_text.set(address[start:end])
app.operand2_text.set(address[end:])
except: #if there are no operands
app.operand1_text.set("")
app.operand2_text.set("")
text="\ncontents of MDR "+address+" are copied to CIR"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
#change color
app.mdr_box.config(readonlybackground="MistyRose2")
app.mnemonic_box.config(readonlybackground="MistyRose2")
app.operand1_box.config(readonlybackground="MistyRose2")
app.operand2_box.config(readonlybackground="MistyRose2")
app.step.wait_variable(app.wait_var) #interrupt
#change color or mdr, cir and ram boxes back to normal
app.mdr_box.config(readonlybackground="SystemButtonFace")
app.operand1_box.config(readonlybackground="SystemButtonFace")
app.operand2_box.config(readonlybackground="SystemButtonFace")
app.mnemonic_box.config(readonlybackground="SystemButtonFace")
app.pc_text.set(str(int(app.pc_text.get())+1).zfill(2)) #increment pc
text="\nprogram counter is incremented"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
#change color
app.pc_box.config(readonlybackground="MistyRose2")
def decode(pc, twodarray, start_row):
app.step.wait_variable(app.wait_var) #interrupt
#change color of pc back to normal
app.pc_box.config(readonlybackground="SystemButtonFace")
temp=app.mnemonic_text.get() #opcode part of the instruction
for label in app.decode_table.grid_slaves():
if label.cget('text')==temp:
label.config(bg='MistyRose2') #highlihgt the opcode column
elif label.cget('text')==decode_dic_rev[temp]: #i cannot get key by value in dictionary so I created a new dictionary with keys and values reversed
label.config(bg='MistyRose2')
text="\nopcode "+temp+" corresponds to "+decode_dic_rev[temp]+" instruction"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
app.step.wait_variable(app.wait_var) #interrupt
for label in app.decode_table.grid_slaves(): #change the color back
label.config(bg='lavender')
def execute(pc, twodarray, start_row):
_label=0
mnemonic=1
operand1=2
operand2=3
if twodarray[pc][mnemonic] in arithmetical_operations or twodarray[pc][mnemonic] in bitwise_operations or twodarray[pc][mnemonic] in logical_operations:
if twodarray[pc][mnemonic]=="NOT":
if app.operand1_box.get()[0:1]=="r":
address=twodarray[pc][operand1]
app.ax_text.set(registers_dic[address])
text="\ncontents of register "+address+" are copied to AX register"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
elif app.operand1_box.get()[0:1]=="d":
address=app.operand1_box.get()[1:]
retrieve_direct_address(address)
app.step.wait_variable(app.wait_var) #interrupt
#copy address from mdr to AX register
app.ax_text.set(app.mdr_text.get())
text="\nretrieved value from MDR "+app.mdr_text.get()+" is copied to AX register"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
app.step.wait_variable(app.wait_var) #interrupt
pc, answer=syntax_analysis(twodarray, pc, True) #execute instruction
app.ax_text.set(answer)
text="\nALU performs NOT operation on contents of AX"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
#store the value in first operand
try:
#need dictionary to tie names of registers with associated text variables in register boxes
registers_boxes_dic[twodarray[pc][operand1]].set(answer) #if first operand is a register
text="\nValue from AX "+str(answer)+" is stored in register "+twodarray[pc][operand1]
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
except: #otherwise it can only be direct address
write_into_ram(answer, app.operand1_text.get()[1:])
#update registers:
#when i tried referencing flag variable directly
#app.zf_text.set(str(sf))
#it would output wrong not updated results
app.zf_text.set(str(flags_dic['zf']))
app.of_text.set(str(flags_dic['of']))
app.sf_text.set(str(flags_dic['sf']))
text="\nflags are updated"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
return pc, answer
else:
#retrieve first operand
if app.operand1_box.get()[0:1]=="r":
address=twodarray[pc][operand1]
#need another dictionary to link names of registers to
#actually no i can just copy contents of the variable into the location
#copy contents of specified address to AX register
app.ax_text.set(registers_dic[address])
text="\ncontentents of register "+address+" are copied to AX register"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
elif app.operand1_box.get()[0:1]=="d":
address=app.operand1_box.get()[1:]
retrieve_direct_address(address)
app.step.wait_variable(app.wait_var) #interrupt
#copy address from mdr to AX register
app.ax_text.set(app.mdr_text.get())
text="\nretrieved value from MDR "+app.mdr_text.get()+" is copied to AX register"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled
app.step.wait_variable(app.wait_var) #interrupt
pc, answer=syntax_analysis(twodarray, pc, True)
#retrieve second operand and perform the instruction
if app.operand2_box.get()[0:1]=="r":
address=twodarray[pc][operand2]
app.ax_text.set(answer)
text="\nALU performs "+twodarray[pc][mnemonic]+" operation with AX register and "+address+" register"
app.console_window.config(state="normal") #in order to use insert method i need to change state to normal
app.console_window.insert(tk.END, text)
app.console_window.config(state="disabled") #and then change it back to disabled