/
Cargador.py
610 lines (545 loc) · 20.1 KB
/
Cargador.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
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
from PyQt4 import QtCore, QtGui
from windowcargador import Ui_DockWidget
from hexadecimal import Hexadecimal
from convert import Convert
from register import Register
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Cargador(QtGui.QDockWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self)
self.window = Ui_DockWidget()
self.window.setupUi(self)
self.file_name = None
self.init = None
self.header = None
self.registers = None
self.end = None
self.rows_count = None
self.end_program = None
self.cc = "="
self.hex = Hexadecimal()
self.reg = Register("T")
self.window.btnSimular.clicked.connect(self.simular)
self.operations = {"18":self.add,"00":self.lda,"40":self.andop,
"28":self.cmp_op,"24":self.div,"3C":self.j_op,
"30":self.jeq,"34":self.jgt,"38":self.jlt,
"48":self.jsub,"50":self.ldch,"08":self.ldl,
"04":self.ldx,"20":self.mul,"4C":self.rsub,
"0C":self.sta,"54":self.stch,"14":self.stl,
"E8":self.stsw,"10":self.stx,"1C":self.sub,"2C":self.tix}
self.operations_text = {"18":"ADD","00":"LDA","40":"AND",
"28":"COMP","24":"DIV","3C":"J",
"30":"JEQ","34":"JGT","38":"JLT",
"48":"JSUB","50":"LDCH","08":"LDL",
"04":"LDX","20":"MUL","4C":"RSUB",
"0C":"STA","54":"STCH","14":"STL",
"E8":"STSW","10":"STX","1C":"SUB","2C":"TIX"}
def simular(self):
num_actions = self.get_count_next_actions()
if num_actions == -1:
self.window.textEdit_Actions.setText("Numero de simulaciones no Validas")
else:
self.make_n_simulations(num_actions)
def make_n_simulations(self,number):
cp = self.get_CP_value()
if self.hex.minus_than(cp,self.end_program):
it = 0
while it < number:
register = self.get_instruct()
operation = register[0:2]
address = register[2:]
val = self.get_address_tarjet(address)
fun = self.operations[operation]
fun(val)
it+=1
else:
self.window.textEdit_Actions.setText("El programa se ha terminado no puede continuar la simulacion")
def add(self,m):
a = self.get_a_value()
string = "ADD: \nCP = 18"+ str(m)+"\n"
string +="A = "+str(a)+"\n"
string +="m ="+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2)="+str(m)+"\n"
a = self.hex.plus(a,m)
string += "A <- A + (m...m+2)"+"\n"
a = self.reg.adjust_bytes(a,6,False)
string += "A = "+str(a)+"\n"
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def andop(self,m):
a = self.get_a_value()
string = "AND: \nCP = 40"+ str(m)+"\n"
string +="A = "+str(a)+"\n"
string +="m ="+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2)="+str(m)+"\n"
a = self.hex.and_op(a,m)
string += "A <- A & (m...m+2)"+"\n"
a = self.reg.adjust_bytes(a,6,False)
string += "A = "+str(a)+"\n"
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def cmp_op(self,m):
a = self.get_a_value()
string ="COMP: \nA = "+str(a)+"\n"
string +="m ="+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2)="+str(m)+"\n"
self.cc = self.hex.cmp_op(a,m)
string += "(A):(m..m+2)\n"
string += "CC = "+self.cc
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def div(self,m):
a = self.get_a_value()
string = "DIV: \nCP = 24"+ str(m)+"\n"
string +="A = "+str(a)+"\n"
string +="m ="+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2)="+str(m)+"\n"
a = self.hex.div(a,m)
string += "A <- A / (m...m+2)"+"\n"
a = self.reg.adjust_bytes(a,6,False)
string += "A = "+str(a)+"\n"
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def lda(self,m):
string = "LDA: \nm = "+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2) = "+str(m)+"\n"
string += "A <- (m..m+2)"
a = self.reg.adjust_bytes(m,6,False)
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def j_op(self,m):
string = "J:\nCP<-m\n"
string += "m = "+str(m)
m = self.reg.adjust_bytes(m,6,False)
m = self.hex.change_hexadecimal(m)
self.set_cp_value(m)
self.window.textEdit_Actions.setText(string)
def jeq(self,m):
string = "JEQ \nCP<-m si CC == '='\n"
string += "m = "+str(m)+"\n"
string += "cc = "+str(self.cc)
m = self.reg.adjust_bytes(m,6,False)
m = self.hex.change_hexadecimal(m)
if self.cc == "=":
self.set_cp_value(m)
else:
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def jgt(self,m):
string = "JGT \nCP<-m si CC == '>'\n"
string += "m = "+str(m)+"\n"
string += "cc = "+str(self.cc)
m = self.reg.adjust_bytes(m,6,False)
m = self.hex.change_hexadecimal(m)
if self.cc == ">":
self.set_cp_value(m)
else:
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def jlt(self,m):
string = "JLT \nCP<-m si CC == '<'\n"
string += "m = "+str(m)+"\n"
string += "cc = "+str(self.cc)
m = self.reg.adjust_bytes(m,6,False)
m = self.hex.change_hexadecimal(m)
if self.cc == "<":
self.set_cp_value(m)
else:
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def jsub(self,m):
string = "JSUB \n L->(CP);CP<-m"
cp = self.get_CP_value()
string += "CP = "+str(cp)+"\n"
cp = self.get_mem_value(cp)
string += "(CP) = "+str(cp)+"\n"
self.set_l_value(cp)
self.set_cp_value(m)
self.window.textEdit_Actions.setText(string)
def ldch(self,m):
string = "LDCH\nA[+der]<-(m)\n"
string += "m = "+str(m)+"\n"
m = self.get_mem_value(m)
m = m[0:2]
a = self.get_a_value()
string += "(m) = "+str(m)+"\n"
string += "A = "+str(a)+"\n"
a = self.reg.filter_number(a)
a = a[0:-2]+m
string += "A[+der]<-(m)\n"
string += "A = "+str(a)
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def ldl(self,m):
string = "LDL\nL<-(m..m+2)"
string += "m = "+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2) ="+str(m)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
self.set_l_value(m)
def ldx(self,m):
string = "LDX\nX<-(m..m+2)"
string += "m = "+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2) ="+str(m)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
self.set_x_value(m)
def mul(self,m):
a = self.get_a_value()
string = "MUL: \nCP = 20"+ str(m)+"\n"
string +="A = "+str(a)+"\n"
string +="m ="+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2)="+str(m)+"\n"
a = self.hex.mul(a,m)
string += "A <- A * (m...m+2)"+"\n"
a = self.reg.adjust_bytes(a,6,False)
string += "A = "+str(a)+"\n"
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def or_op(self,m):
a = self.get_a_value()
string = "OR: \nCP = 44"+ str(m)+"\n"
string +="A = "+str(a)+"\n"
string +="m ="+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2)="+str(m)+"\n"
a = self.hex.or_op(a,m)
string += "A <- A | (m...m+2)"+"\n"
a = self.reg.adjust_bytes(a,6,False)
string += "A = "+str(a)+"\n"
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def rsub(self,m):
string = "RSUB\nPC<-(L)"
l = self.get_l_value()
string += "L="+str(l)+"\n"
l = self.get_mem_value(l)
string += "(L)="+str(l)+"\n"
self.set_cp_value(l)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def sta(self,m):
string = "STA\nm..m+2<-(A)\nm="+str(m)+"\n"
it = 0
row = self.get_row_index(m)
col = self.get_column_index(m)
a = self.get_a_value()
string += "(A)="+str(a)
index = 0
while it < 3:
val = a[index:index+2]
index += 2
item_text = QtGui.QTableWidgetItem(val)
self.window.tableWidget.setItem(row,col,item_text)
col = (col+1)%17
if col == 0:
col = 1
row += 1
it += 1
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def stch(self,m):
string = "STCH\nm<-(A)[+der]\nm="+str(m)+"\n"
row = self.get_row_index(m)
col = self.get_column_index(m)
print row,col,m
a = self.get_a_value()
a = self.reg.filter_number(a)
a_der = str(a)[-2:]
string += "(A)[+der]="+ a_der
item = QtGui.QTableWidgetItem(a_der)
self.window.tableWidget.setItem(row,col,item)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def stl(self,m):
string = "STL\nm..m+2<-(L)\nm="+str(m)+"\n"
it = 0
row = self.get_row_index(m)
col = self.get_column_index(m)
l = self.get_l_value()
string += "(l)="+str(l)
index = 0
while it < 3:
val = l[index:index+2]
index += 2
item_text = QtGui.QTableWidgetItem(val)
self.window.tableWidget.setItem(row,col,item_text)
col = (col+1)%17
if col == 0:
col = 1
row += 1
it += 1
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def stsw(self,m):
string = "STSW\nm..m+2<-(SW)\nm="+str(m)+"\n"
it = 0
row = self.get_row_index(m)
col = self.get_column_index(m)
sw = self.get_sw_value()
string += "(SW)="+str(sw)
index = 0
while it < 3:
val = sw[index:index+2]
index += 2
item_text = QtGui.QTableWidgetItem(val)
self.window.tableWidget.setItem(row,col,item_text)
col = (col+1)%17
if col == 0:
col = 1
row += 1
it += 1
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def stx(self,m):
string = "STSX\nm..m+2<-(X)\nm="+str(m)+"\n"
it = 0
row = self.get_row_index(m)
col = self.get_column_index(m)
x = self.get_x_value()
string += "(X)="+str(x)
index = 0
while it < 3:
val = x[index:index+2]
index += 2
item_text = QtGui.QTableWidgetItem(val)
self.window.tableWidget.setItem(row,col,item_text)
col = (col+1)%17
if col == 0:
col = 1
row += 1
it += 1
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def sub(self,m):
a = self.get_a_value()
string = "SUB: \nCP = 1C"+ str(m)+"\n"
string +="A = "+str(a)+"\n"
string +="m ="+str(m)+"\n"
m = self.get_mem_value(m)
string += "(m..m+2)="+str(m)+"\n"
a = self.hex.subs(a,m)
string += "A <- A - (m...m+2)"+"\n"
a = self.reg.adjust_bytes(a,6,False)
string += "A = "+str(a)+"\n"
self.set_a_value(a)
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def tix(self,m):
string = "TIX\nX<-(X)+1;(X):(m,,m+2)\n"
x = self.get_x_value()
string += "X ="+str(x)+"+1\n"
x = self.hex.plus(x,"1H")
string += "X ="+str(x)+"\n"
self.set_x_value(x)
m = self.get_mem_value(m)
string += "(m..m+2) +"+str(m)+"\n"
self.cc = self.hex.cmp_op(x,m)
string += "(X):(m..m+2)\nCC="+self.cc
self.increment_cp(3)
self.window.textEdit_Actions.setText(string)
def increment_cp(self,num):
c = Convert()
num = c.decimal_to_hexadecimal(num)
cp = self.get_CP_value()
cp = self.hex.plus(cp,num)
self.set_cp_value(cp)
def get_address_tarjet(self,value):
c = Convert()
addressing = value[0]+"H"
addressing = int(c.to_decimal(addressing))
val = value
bina = c.decimal_to_binary(addressing,4)
bina = c.mask_and(bina,"1000")
if bina == "1000":
val = self.hex.subs(value,"8000H")
x = self.get_x_value()
val = self.hex.plus(val,x)
val = self.reg.filter_number(val)
return val
def set_a_value(self,value):
value = self.reg.adjust_bytes(value,6,False)
value = self.hex.change_hexadecimal(value)
item_text = QtGui.QTableWidgetItem(value)
self.window.tableWidget_2.setItem(1,0,item_text)
def get_a_value(self):
item = self.window.tableWidget_2.item(1,0)
return str(item.text())
def get_l_value(self):
item = self.window.tableWidget_2.item(3,0)
return str(item.text())
def set_l_value(self,value):
value = self.reg.adjust_bytes(value,6,False)
value = self.hex.change_hexadecimal(value)
item_text = QtGui.QTableWidgetItem(value)
self.window.tableWidget_2.setItem(3,0,item_text)
def get_x_value(self):
item = self.window.tableWidget_2.item(2,0)
return str(item.text())
def set_x_value(self,value):
value = self.reg.adjust_bytes(value,6,False)
value = self.hex.change_hexadecimal(value)
item_text = QtGui.QTableWidgetItem(value)
self.window.tableWidget_2.setItem(2,0,item_text)
def get_sw_value(self):
item = self.window.tableWidget_2.item(4,0)
return str(item.text())
def set_sw_value(self,value):
value = self.reg.adjust_bytes(value,6,False)
value = self.hex.change_hexadecimal(value)
item_text = QtGui.QTableWidgetItem(value)
self.window.tableWidget_2.setItem(4,0,item_text)
def get_CP_value(self):
item = self.window.tableWidget_2.item(0,0)
return str(item.text())
def set_cp_value(self,cp):
cp = self.reg.adjust_bytes(cp,6,False)
cp = self.hex.change_hexadecimal(cp)
item_text = QtGui.QTableWidgetItem(cp)
self.window.tableWidget_2.setItem(0,0,item_text)
self.get_next_text_instruction()
def get_next_text_instruction(self):
cp = self.get_CP_value()
mem = self.get_mem_value(cp)
cod = mem[0:2]
op = self.operations_text.get(str(cod),"FIN")
self.window.label_sig.setText(op)
def get_row_index(self,value):
index = str(value[0:-1])+"0"
it = 0
index = self.reg.adjust_bytes(index,6,False)
num_rows = self.window.tableWidget.rowCount()
while it < num_rows:
val = self.window.tableWidget.item(it,0)
if str(val.text()) == index:
return it
it += 1
return 0
def get_column_index(self,value):
c = Convert()
index = str(value[-1])+"H"
index = c.to_decimal(index)
del c
return index+1
def get_instruct(self):
cp = self.get_CP_value()
return self.get_mem_value(cp)
def get_mem_value(self,value):
value = self.reg.filter_number(str(value))
col = self.get_column_index(value)
row = self.get_row_index(value)
register = ""
it = col
cont = 0
while cont < 3:
item = self.window.tableWidget.item(row,it)
register += str(item.text())
it = (it+1)%17
if it == 0:
it = 1
row+=1
cont += 1
return register
def get_count_next_actions(self):
count_actions = str(self.window.textEdit_Num.toPlainText())
if count_actions == "":
count_actions = 1
elif not count_actions.isdigit():
return -1
return int(count_actions)
def get_list_register(self):
f = open(self.file_name)
text = f.read()
list = text.split("\n")
list.remove("")
return list
def load_file_name(self,file_name):
self.file_name = file_name+".os"
list = self.get_list_register()
self.header = list[0]
self.end = list[-1]
self.registers = list[1:-1]
self.charge_in_memory()
def charge_text(self):
c = Convert()
for r in self.registers:
string = r[9:]
index = 0
init = r[1:7]
col_start = init[-1]+"H"
col = int(c.to_decimal(col_start) + 1)
res = self.hex.subs(init[:-1],self.init[:-1])
dec_res = int(c.to_decimal(res))
while index < len(string):
byte = string[index:index+2]
item = QtGui.QTableWidgetItem(byte)
self.window.tableWidget.setItem(dec_res,col,item)
index+=2
col = (col + 1) % 17
if col == 0:
col = 1
dec_res += 1
def charge_header(self):
init = self.header[7:13]
self.init = init
length = self.header[13:]
self.end_program = self.hex.plus(init,length)
star_rows= length[:-1]
index = init[:-1]
num_rows=star_rows+"H"
c = Convert()
num_rows=int(c.to_decimal(num_rows))
self.window.tableWidget.setRowCount(num_rows+1)
it = 0
while it <= num_rows:
dir = index+"0H"
r = Register("T")
dir = r.adjust_bytes(dir,6,False)
item = QtGui.QTableWidgetItem(dir)
self.window.tableWidget.setItem(it,0,item)
it += 1
index = self.hex.plus(index,"1H")[:-1]
def charge_end_file(self):
dir = self.end[1:]
dir = self.reg.adjust_bytes(dir,6,False)
dir = self.hex.change_hexadecimal(dir)
item = QtGui.QTableWidgetItem(dir)
self.window.tableWidget_2.setItem(0,0,item)
self.get_next_text_instruction()
it = 1
while it < 5:
item = QtGui.QTableWidgetItem("FFFFFFH")
self.window.tableWidget_2.setItem(it,0,item)
it += 1
def charge_in_memory(self):
self.charge_header()
self.charge_text()
self.charge_end_file()
self.init_empty_rows()
def init_empty_rows(self):
rows_count = self.window.tableWidget.rowCount()
colum_count = self.window.tableWidget.columnCount()
for i in range(rows_count):
for j in range(colum_count):
item = self.window.tableWidget.item(i,j)
if item == None:
item_text = QtGui.QTableWidgetItem("FF")
self.window.tableWidget.setItem(i,j,item_text)