def directive_word(self,value): c = Convert() if not c.is_hexadecimal(value): value = int(float(value)) value = c.decimal_to_hexadecimal(value) r = Register("T") value = r.adjust_bytes(value,6,True) value = r.filter_number(value) del r return value
def make_register_d(self,list_sym,tab_sym): r =Register("X") reg = "D" for it in list_sym: if len(reg) + 12 > 73: self.list_registers_d.append(reg) reg = "D" item = self.exist_item(it,tab_sym) name = r.adjust_name(it) if item: dir = r.adjust_bytes(item.get_dir_val(),6,False) else: dir = "FFFFFF" reg+= (name+dir) if not len(reg) == 1: self.list_registers_d.append(reg)
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_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 operations_code(self,operator,m,is_index): r = Register("T") c = Convert() op = self.operations[operator] op = op+"H" op = c.to_decimal(op) op = int(op) binary = c.decimal_to_binary(op,24) binary = c.shift_binary_left(binary,16) if is_index: binary = c.mask_or(binary,"000000001000000000000000") m = c.to_decimal(m) m = int(m) m = c.decimal_to_binary(m,24) binary = c.mask_or(binary,m) val = int(binary,2) val = c.decimal_to_hexadecimal(val) val = r.filter_number(val) val = r.adjust_bytes(val,6,False) del r del c return val
class Step2: ##constructor de la clase se inicializan los codigos de operacion def __init__(self): self.operations = { 'ADD':'18','ADDF':'58','ADDR':'90','AND':'40','CLEAR':'B4','COMP':'28', 'COMPF':'88','DIV':'24','COMPR':'A0','DIVF':'64','DIVR':'9C','FIX':'C4', 'FLOAT':'C0','HIO':'F4','J':'3C','JEQ':'30','JGT':'34','JLT':'38', 'JSUB':'48','LDA':'00','LDB':'68','LDCH':'50','LDF':'70','LDL':'08','LDS':'6C', 'LDT':'74','LDX':'04','LPS':'D0','MUL':'20','MULF':'60','MULR':'98','NORM':'C8', 'OR':'44','RD':'D8','RMO':'AC','RSUB':'4C','SHIFTL':'A4','SHIFTR':'A8','SIO':'F0', 'SSK':'EC','STA':'0C','STB':'78','STCH':'54','STF':'80','STI':'D4', 'STL':'14','STS':'7C','STSW':'E8','STT':'84','STX':'10', 'SUB':'1C','SUBF':'5C','SUBR':'94','SVC':'B0','TD':'E0','TIO':'F8','TIX':'2C', 'TIXR':'B8','WD':'DC' } self.d = Displacement() self.list_registers_h = [] self.list_registers = [] self.list_registers_m = [] self.list_registers_r = [] self.list_registers_e = [] self.current_register = Register("T") self.registers = {"A":"0","X":"1","L":"2","CP":"8","SW":"9","B":"3","S":"4","T":"5","F":"6"} self.base = "1038H" self.m_register = [] self.m_modif_register = [] self.h_name = "" self.list_registers_d = [] self.list_word_m = [] self.list_op_m = [] ## inserta una cadena de bytes en el registro T actual si no cabe genera #otro registro nuevo para almacenar los datos # @param str cadena que contiene la serie de bytes # @param dir direccion donde se encontro la instruccion def insert_str(self,str,dir): if self.current_register.get_len() == 0: self.current_register.init_dir = dir self.current_register.instert_string(str) print "nuevo",str,dir else: if self.current_register.fits_in(str): self.current_register.instert_string(str) print "agrega",str,dir else: self.complete_register() self.insert_str(str,dir) ## termina el registro actual T y genera uno nuevo def complete_register(self): if not self.current_register.get_len()==0: register_t = self.current_register.make_T() self.list_registers.append(register_t) self.current_register = Register("T") ## regresa el registro m de los elementos externos que se # encontraron en un codigo de expresion def get_m_register_op(self): list_ret = [] hexa = Hexadecimal() self.elimina_repetidos(self.list_op_m) for it in self.list_op_m: reg = "M" val = hexa.plus(it[3],"1H") reg += self.current_register.adjust_bytes(val,6,False) reg += "05" reg += it[2] if it[1] == "_": name = self.current_register.adjust_name(it[0]) else: name = self.current_register.adjust_name(self.h_name) reg += name list_ret.append(reg) return list_ret ## elimina los registros m repetidos en una lista especificada # este metodo elimina los registros relativos con signo contrario y que se encuentran #en la misma direccion def elimina_repetidos(self,list_r): it = 0 re = 0 while it < len(list_r): re = it + 1 eliminado = False while re < len(list_r) and not eliminado: if list_r[it][3] == list_r[re][3]:#direccion if not list_r[it][2] == list_r[re][2]:#signos if list_r[it][1] == "_" and list_r[re][1] == "_": if list_r[it][0] == list_r[re][0]:#nombre eliminado = True elif list_r[it][1] == "relativo" and list_r[re][1] == "relativo": eliminado = True if not eliminado: re += 1 if eliminado: list_r.remove(list_r[re]) list_r.remove(list_r[it]) else: it += 1 ## regresa el registro m de los elementos externos que se # encontraron en una directiva word def get_m_register_word(self): list_ret = [] self.elimina_repetidos(self.list_word_m) for it in self.list_word_m: reg = "M" reg += self.current_register.adjust_bytes(it[3],6,False) reg += "06" reg += it[2] if it[1] == "_": name = self.current_register.adjust_name(it[0]) else: name = self.current_register.adjust_name(self.h_name) reg += name list_ret.append(reg) return list_ret ## rergesa una lista con todos los registros generados # desde el registro H hasta al E def all_registers(self): ret = [] ret += self.list_registers_h ret += self.list_registers_d ret += self.list_registers_r ret += self.list_registers ret += self.list_registers_m ret += self.get_m_register_op() ret+= self.get_m_register_word() ret += self.list_registers_e # print self.list_op_m,self.list_word_m return ret #============================================================================== # Directiva START #============================================================================== ## Crea el registro de inicio y lo inserta en la lista de registros # @param name nombre del programa # @param length tamaño del programa # @param inicial direccion de inicio del programa def directive_start(self,name,length,inicial): r = Register("H") self.list_registers_h = [] register_h = r.make_H(name.upper(),length,inicial) self.list_registers_h.append(register_h) self.h_name = r.adjust_name(name.upper()) del r #============================================================================== # Directiva END #============================================================================== ## Crea el registro de final y lo inserta en la lista de registros # @param direccion de la etiqueta de la primera instruccion a ejecutar o vacio def directive_end(self,label,dir_init): r = Register("E") self.list_registers_e = [] register_e = r.make_E(label,dir_init) self.list_registers_e.append(register_e) del r ## Crea el registro de final y lo inserta en la lista de registros de un segmento # @param direccion de la etiqueta de la primera instruccion a ejecutar o vacio def directive_end_segment(self): self.list_registers_e.append("E") ## genera el registro para los simbolos externos que se usaran en #el programa def make_register_r(self,list_symb): r = Register("X") reg = "R" for l in list_symb: name = r.adjust_name(l) reg+=name self.list_registers_r.append(reg) ## crea los registros de definicion de el segmento #si el registro sobre pasa el tamaño de 73 caracteres crea otro registro def make_register_d(self,list_sym,tab_sym): r =Register("X") reg = "D" for it in list_sym: if len(reg) + 12 > 73: self.list_registers_d.append(reg) reg = "D" item = self.exist_item(it,tab_sym) name = r.adjust_name(it) if item: dir = r.adjust_bytes(item.get_dir_val(),6,False) else: dir = "FFFFFF" reg+= (name+dir) if not len(reg) == 1: self.list_registers_d.append(reg) ## checa si existe un item con un valor de name igual a el parametro name def exist_item(self ,name,symbols): for it in symbols: if it.get_name() == name: return it return None ## genera los registros de m modificados apartir de la lista de registros def make_register_m(self,obj_list,cp_list,num_bloque,bloques): r = Register("M") hx = Hexadecimal() r.name = self.h_name it = 0 while it < len(self.m_register): index = self.m_register[it] # print "normal",index load_dir = bloques.get_load_dir_at(num_bloque[index-1]) cp = hx.plus(cp_list[index-1],load_dir) register = r.make_M(obj_list[index-1],cp) self.list_registers_m.append(register) it += 1 it = 0 while it < len(self.m_modif_register): index = self.m_modif_register[it] # print "modifi",index,len(cp_list) load_dir = bloques.get_load_dir_at(num_bloque[index-1]) cp = hx.plus(cp_list[index-1],load_dir) register = r.make_M_modificado(obj_list[index-1],cp) self.list_registers_m.append(register) it += 1 ## genera el registro de modificacion de una directiva word #y lo regresa en forma de cadena def directive_word(self,value): c = Convert() if not c.is_hexadecimal(value): value = int(float(value)) value = c.decimal_to_hexadecimal(value) r = Register("T") value = r.adjust_bytes(value,6,True) value = r.filter_number(value) del r return value #============================================================================== # Directiva BYTE #============================================================================== ## regresa los bytes que genera las constantes de BYTE # @param value argumento de la directiva BYTE # @return secuenca de bytes que genero la constante def const_BYTE(self,value): data = self.d.filter_byte(value) if self.d.is_constant_hexadecimal(value): return self.get_value_hex_BYTE(data) else: return self.get_value_cad_BYTE(data) ## genera la secuencia de bytes para constantes hexadecimales # valor de la constante a calcular su secuencia de bytes # @param data valor de la constante hexadecimal # @return regresa un numero par de bytes que representan el numero hexadecimal def get_value_hex_BYTE(self,data): if len(data) % 2 == 0: return data else: return "0"+data ## genera la secuancia de bytes para las constantes de cadena de BYTE # convirtiendo cada caracter en su codigo ascii #@param data valor de la constante de cadena # @return secuencia de codigos ascii que representan la cadena def get_value_cad_BYTE(self,data): string_out ="" c = Convert() r = Register("T") for caracter in data: car = str(ord(caracter)) car = c.decimal_to_hexadecimal(car) car = r.filter_number(car) string_out+=car del c return string_out #============================================================================== # ======= Conjunt de Instrucciones ===== #============================================================================== ## regresa una cadena de bytes que genera una operacion del conjunto de #instrucciones #@param operator instruccion la cual se generara la cadena de Bytes #@param m argumento de la instruccion #@param is_index el modo de direccionamiento True es indexado #@return regresa la cadena de bytes def operations_code(self,operator,m,is_index): r = Register("T") c = Convert() op = self.operations[operator] op = op+"H" op = c.to_decimal(op) op = int(op) binary = c.decimal_to_binary(op,24) binary = c.shift_binary_left(binary,16) if is_index: binary = c.mask_or(binary,"000000001000000000000000") m = c.to_decimal(m) m = int(m) m = c.decimal_to_binary(m,24) binary = c.mask_or(binary,m) val = int(binary,2) val = c.decimal_to_hexadecimal(val) val = r.filter_number(val) val = r.adjust_bytes(val,6,False) del r del c return val ## regresa una cadena de bytes que genera una operacion de tipo 2 # con 2 parametros que son registros # @param operator instruccion la cual se generara la cadena de bytes # @param r1 primer registro del parametro # @param r2 segundo registro del parametro # @return cadena de bytes que representa el codigo objeto de la operacion def operations_type_2(self,operator,r1,r2): operation_code = self.operations[operator] r1 = self.registers.get(r1,"") r2 = self.registers.get(r2,"0") obj_code = operation_code + r1 + r2 return obj_code ## regresa una cadena de bytes que genera una operacion de tipo 2 # con un numero del 1 - 16 como parametro # @param operator instruccion la cual se generara la cadena de bytes # @param r1 primer registro del parametro en formato de caracter # @param r2 segundo registro del parametro en formato de caracter # @return cadena de bytes que representa el codigo objeto de la operacion def operations_type2_n(self,operator,n1): operation_code = self.operations[operator] c = Convert() n1 = c.decimal_to_hexadecimal(n1) n1 = n1[:-1] del c return operation_code + str(n1) + "0" ## regresa una cadena de bytes que genera una operacion de tipo 2 # con 2 parametros que son un registro y un numero # @param operator instruccion la cual se generara la cadena de bytes # @param r el reistro al que se le aplicara la operacion # @param n segundo numero entre 1 y 16 # @return cadena de bytes que representa el codigo objeto de la operacion def operations_type_2_rn(self,operator,r,n): operation_code = self.operations[operator] r = self.registers.get(r,"") c = Convert() n = c.decimal_to_hexadecimal(n) n = n[:-1] del c obj_code = operation_code + r + str(n) return obj_code #============================================================================== # TYPE 3 & 4 #============================================================================== ## Regresa los 6 bits mas significativos de un numero # metodo usado para recuperar el valor del codigo de operacion # @param operator instruccion de tipo cadena la cual sera convertida a binario # @return cadena de bits que representan el codigo de operacion def get_binary_code(self,operator): code = self.operations[operator] c = Convert() dec_code = c.to_decimal(code+"H") binary = c.decimal_to_binary(int(dec_code),8) binary = binary[0:-2] del c return binary ##checa si es una numero menor a 4096 #@param desp constante numerica en formato hexadecimal #@return regresa True si la constante es menor a (4096)dec def is_type_c(self,desp): c = Convert() desp = c.to_decimal(desp) del c if desp < 4096: return True return False ## calcula las banderas de n, i para los casos de simple #indirecto e Inmediato def get_flags(self,type): flags = {'n':0,'i':0,'x':0,'b':0,'p':0,'e':0} n = 1 i = 1 if type == "Indirecto": n=1 i=0 elif type == "Inmediato": n = 0 i = 1 flags['n'] = n flags['i'] = i return flags ## checa si un elemento es relativo a la base si no es asi # validadndo que el resultado este entre los valores de m # regresa un valor de None def is_relative_cp(self,cp,arg): hex = Hexadecimal() c = Convert() res_hex = hex.subs_minus(arg,cp) sign = res_hex[1] res_hex = res_hex[0] res = int(c.to_decimal(res_hex)) if sign == "-": res = (res ^ 4095)+1 res = res * -1 if res <= 2047 and res >= -2048: return c.exp_to_hexadecimal(res) else: return None ## checa si un elemento es relativo a la base si no es asi # regresa un valor de None def relative_base(self,arg): hex = Hexadecimal() c = Convert() res = hex.subs(arg,self.base) res_dec = c.to_decimal(res) if res_dec >= 0 and res_dec <= 4095: return res return None ## este metodo calcula el codigo objeto de una instruccion def operation_type_3_4(self,cp,operator,arg,format_type,num_line,dir_type,type_c,is_index,valid_label): c = Convert() operator = self.get_binary_code(operator) flags = self.get_flags(dir_type) num_max = 3 res = arg entra = True if format_type == 4: flags['e']=1 num_max=5 if not type_c and valid_label == "relativo": self.m_register.append(num_line) res = self.current_register.adjust_bytes(arg,num_max,True) else: if type_c: if not self.is_type_c(arg): entra = True else: entra = False if not c.is_hexadecimal(arg): arg = c.decimal_to_hexadecimal(arg) res = self.current_register.adjust_bytes(arg,3,True) if entra: res = self.is_relative_cp(cp,arg) if not res: res = self.relative_base(arg) if res: flags['b'] = 1 else: res = arg valid_label = False else: flags['p'] = 1 res = self.current_register.adjust_bytes(res,num_max,True) if is_index: flags['x'] = 1 if not valid_label: flags['b'] = 1 flags['p'] = 1 if valid_label == "_": flags['b'] = 0 flags['p'] = 0 flags = self.flags_string(flags) val = operator + flags val = str(int(val,2)) val = c.decimal_to_hexadecimal(val) if len(val)==3: val = "0"+val val = self.current_register.adjust_bytes(val,3,True) val += str(res) del c return val ## regresa una cadena de bites que es la representacion de las banderas # utilizadas para generar el codigo objeto de las operaciones de formato # 3 y 4 def flags_string(self,dicc): string = "" string += str(dicc['n'])+str(dicc['i']) string += str(dicc['x'])+str(dicc['b']) string += str(dicc['p'])+str(dicc['e']) return string
class Cargadorx(): def __init__(self,window): self.window = window self.tabse = Tabse() self.lonsc = "0H" self.dirsc = "0H" self.dirprog = "0H" self.direj = "0H" self.hexa = Hexadecimal() self.convert = Convert() self.register = Register("A") self.error_indefinido = False self.error_duplicado = False def show_error(self,msg): ##QtGui.QMessageBox.about(self.window,"Error",msg) self.window.textEdit_Actions.setText(msg) def load_file_name(self,list_obj,dirprog): val_int = self.convert.to_decimal(dirprog) self.dirprog = self.convert.decimal_to_hexadecimal(val_int) self.dirprog = self.register.adjust_bytes(self.dirprog,6,False)+"H" self.dirsc = self.dirprog self.step_1(list_obj) if not self.error_duplicado: self.create_memory() self.dirsc = self.dirprog self.direj = self.dirprog self.step_2(list_obj) self.init_empty_rows() self.tabse.print_tabse() if self.error_indefinido: self.show_error("Error en simbolo indefinido") else: self.show_error("Error simbolo duplicado") def step_1(self,list_obj): for list_n in list_obj: for n in list_n: if len(n) > 0: if n[0] == "H": self.step_h(n,1) elif n[0] == "D": self.step_1_d(n) self.dirsc = self.hexa.plus(self.dirsc,self.lonsc) def step_h(self,n,step): name = n[1:7] name = name.strip() self.lonsc = n[13:]+"H" if step == 1: if not self.tabse.exist_node(name): self.tabse.insert_section(name,self.dirsc,self.lonsc) else: ##print name self.error_duplicado = True def step_1_d(self,n): num = (len(n)-1)/12 it = 0 # print "num",num,len(n) while it < num: index1 = (it * 12)+1 index2 = index1 + 6 index3 = index2 + 6 name = n[index1:index2].strip() #print "name",name if index3 >= len(n): len_r = n[index2:]+"H" else: len_r = n[index2:index3]+"H" if not self.tabse.exist_node(name): val = self.hexa.plus(len_r,self.dirsc) self.tabse.insert_variable(name,val) else: print "error",name self.error_duplicado = True it += 1 def step_2(self,list_obj): for list_n in list_obj: for n in list_n: if len(n) > 0: if n[0] == "H": self.step_h(n,2) elif n[0] == "T": self.charge_obj_code(n) elif n[0] == "M": self.change_m_register(n) elif n[0] == "E": self.reg_e(n) self.dirsc = self.hexa.plus(self.dirsc,self.lonsc) def reg_e(self,r): if len(r) > 1: val = r[1:] + "H" self.direj = self.hexa.plus(self.dirsc,val) def charge_obj_code(self,r): string = r[9:] index = 0 init = r[1:7] init = self.hexa.plus(init+"H",self.dirsc)[:-1] col_start = init[-1]+"H" col = int(self.convert.to_decimal(col_start) + 1) dat1 = self.get_part_dat(init) res = self.hexa.subs(dat1,self.dirprog[:-2]) dec_res = int(self.convert.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 get_part_dat(self,dat): if len(dat) == 1: return "0" else: return dat[:-1] def change_m_register(self,r): val = r[1:7] med_byte = r[7:9] sign = r[9] name = r[10:].strip() # print val,med_byte,sign,name n = self.tabse.get_register(name) if n: # print "existe" val = self.hexa.plus(val,self.dirsc)[:-1] content = self.get_content_at(val,3) # print val,content ant_content = "" if med_byte == "5": ant_content = content[0] content = content[1:] dir_es = n.get_dir() new_dir = self.make_operation(sign,content,dir_es) # print "perico",new_dir,content,sign,dir_es new_dir = self.register.adjust_bytes(new_dir,6,True) if med_byte == "5": new_dir = ant_content + new_dir[1:] new_dir = ant_content + new_dir # print new_dir col_num = val[-1] + "H" col = int(self.convert.to_decimal(col_num) + 1) val = self.get_part_dat(val) ren = self.hexa.subs(val,self.dirprog[:-2]) ren = int(self.convert.to_decimal(ren)) index = 0 while index < len(new_dir): byte = new_dir[index:index+2] item = QtGui.QTableWidgetItem(byte) self.window.tableWidget.setItem(ren,col,item) index+=2 col = (col + 1) % 17 if col == 0: col = 1 ren += 1 else: self.error_indefinido = True def make_operation(self,sign,dat1,dat2): val1 = self.get_int_operator(dat1) val2 = self.get_int_operator(dat2) if sign == "-": ret = val1-val2 else: ret = val1+val2 ret = self.exp_to_hexadecimal(ret) return ret def get_int_operator(self,dat): if dat[-1] == "H": dat = dat[:-1] if dat[0] == "F": string = "" it = 0 while it < len(dat): if not dat[it] == "F": string += dat[it] it += 1 if len(string) == 0: return -1 else: val = "1"+("0"*len(string))+"H" val = self.convert.to_decimal(val) val2 = self.convert.to_decimal(string+"H") return val2-val else: return self.convert.to_decimal(dat+"H") def get_content_at(self,dirr,num): dir_n = dirr[-1]+"H" col = int(self.convert.to_decimal(dir_n) + 1) dire = self.get_part_dat(dirr) ren = self.hexa.subs(dire,self.dirprog[:-2]) ren = int(self.convert.to_decimal(ren)) it = 0 text = "" while it < num: item = self.window.tableWidget.item(ren,col) text += str(item.text()) it += 1 col = (col + 1) % 17 if col == 0: col = 1 ren += 1 return text def exp_to_hexadecimal(self,val): if val < 0: val = int(val * -1) val = val ^ 65535 val += 1 if val == 0: val = "0H" else: val = self.convert.decimal_to_hexadecimal(val) if val[0] == "F" and len(val) > 6: val = "0"+val return val def create_memory(self): init = self.dirprog[:-1] length = self.hexa.subs(self.dirsc,self.dirprog)[:-1] star_rows= length[:-1] index = init[:-1] num_rows=star_rows+"H" num_rows=int(self.convert.to_decimal(num_rows)) self.window.tableWidget.setRowCount(num_rows+1) it = 0 while it <= num_rows: dir = index+"0H" dir = self.register.adjust_bytes(dir,6,False) item = QtGui.QTableWidgetItem(dir) self.window.tableWidget.setItem(it,0,item) it += 1 index = self.hexa.plus(index,"1H")[:-1] 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)
class Simuladorx(QtGui.QDockWidget): REG_CP = 0 REG_A = 1 REG_X = 2 REG_L = 3 REG_SW = 4 REG_B = 5 REG_S = 6 REG_T = 7 REG_F = 8 def __init__(self,parent=None): QtGui.QWidget.__init__(self) self.window = Ui_DockWidget() self.window.setupUi(self) self.add_registers() self.cargador = None self.hexa = Hexadecimal() self.convert = Convert() self.register = Register("A") self.init_registers() self.end_program = "0H" self.cc_val = "=" self.window.btnSimular.clicked.connect(self.simular) self.operations_3 = {"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","58":"ADDF","88":"COMPF","64":"DIVF","68":"LDB","70":"LDF", "6C":"LDS","74":"LDT","D0":"LPS","60":"MULF","EC":"SSK","78":"STB","80":"STF","D4":"STI","7C":"STS","E8":"STSW", "84":"STT","5C":"SUBF","E0":"TD","DC":"WD","44":"OR"} self.operations_1 = {"C4":"FIX","C0":"FLOAT","F4":"HIO","C8":"NORM","F0":"SIO","F8":"TIO"} self.operations_2 = {"90":"ADDR","B4":"CLEAR","A0":"COMPR","9C":"DIVR","98":"MULR","AC":"RMO","A4":"SHIFTL","A8":"SHIFTR", "94":"SUBR","B0":"SVC","B8":"TIXR"} self.operations = {"18":self.add,"00":self.lda,"40":self.and_op,"28":self.comp,"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,"58":self.float_operations, "88":self.float_operations, "64":self.float_operations,"68":self.ldb,"70":self.float_operations,"6C":self.lds,"74":self.ldt, "D0":self.system_operations,"60":self.float_operations,"EC":self.system_operations,"78":self.stb, "80":self.float_operations,"D4":self.system_operations,"7C":self.sts,"E8":self.system_operations, "84":self.stt,"5C":self.float_operations,"E0":self.system_operations,"DC":self.system_operations, "C4":self.float_operations, "C0":self.float_operations,"F4":self.system_operations,"C8":self.float_operations, "F0":self.system_operations,"44":self.or_op, "F8":self.system_operations,"90":self.addr,"B4":self.clear,"A0":self.compr, "9C":self.divr,"98":self.mulr,"AC":self.rmo,"A4":"SHIFTL", "A8":"SHIFTR","94":self.subr,"B0":"SVC","B8":self.tixr} self.registers = {"0":[self.REG_A,"A"],"1":[self.REG_X,"X"],"2":[self.REG_L,"L"],"8":[self.REG_CP,"CP"],"9":[self.REG_SW,"SW"],"3":[self.REG_B,"B"], "4":[self.REG_S,"S"],"5":[self.REG_T,"T"],"6":[self.REG_F,"F"]} self.operations_m = ["J","JLT","JEQ","JGT","JSUB","STA","STB","STCH","STL","STS","STSW","STT","STX","LDCH"] def simular(self): num_actions = self.get_count_next_actions() print "num actions",num_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): it = 0 while it < number: cp = self.get_register(self.REG_CP) if self.hexa.minus_than(cp,self.end_program): op = self.get_operation() type_op = op[2] inst = op[1] m = self.get_m(type_op,cp,inst) if m == "Error": self.window.textEdit_Actions.setText("El programa se termino debido a un error de operacion") else: fun = self.operations[op[0]] fun(m) self.show_next_instr() else: self.window.textEdit_Actions.setText("El programa se termino") it += 1 def get_m(self,type_op,cp,inst): if type_op == "Error": return type_op if type_op == "1": self.increment_cp(1) return "0" if type_op == "2": cp_n = self.hexa.plus(cp,"1H") self.increment_cp(2) return self.get_data_form_mem(cp_n,1) if inst in self.operations_m: print "\n\n"+inst+"\n\n" return self.get_m_type3_m() return self.get_m_type3() 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 system_operations(self,val): self.window.textEdit_Actions.setText("No tiene los permisos \n para ejecutar esta instruccion") def float_operations(self,val): self.window.textEdit_Actions.setText("Las instrucciones flotantes \n no se pueden ejecutar") def add_registers(self): self.window.tableWidget_2.setRowCount(9) item = QtGui.QTableWidgetItem() item.setText("B") self.window.tableWidget_2.setVerticalHeaderItem(5, item) item = QtGui.QTableWidgetItem() item.setText("S") self.window.tableWidget_2.setVerticalHeaderItem(6, item) item = QtGui.QTableWidgetItem() item.setText("T") self.window.tableWidget_2.setVerticalHeaderItem(7, item) item = QtGui.QTableWidgetItem() item.setText("F") self.window.tableWidget_2.setVerticalHeaderItem(8, item) def init_registers(self): it = 0 while it < 9: item_text = QtGui.QTableWidgetItem("FFFFFFFH") self.window.tableWidget_2.setItem(it,0,item_text) it += 1 def set_register(self,reg_num,val): val_hex = self.register.adjust_bytes(val,6,False) val_hex = self.hexa.change_hexadecimal(val_hex) item_text = QtGui.QTableWidgetItem(val_hex) self.window.tableWidget_2.setItem(reg_num,0,item_text) def get_register(self,reg_num): item = self.window.tableWidget_2.item(reg_num,0) return str(item.text()) def carga(self,list_obj,dirprog): self.cargador = Cargadorx(self.window) self.cargador.load_file_name(list_obj,dirprog) cp = self.cargador.direj self.set_register(self.REG_CP,cp) self.show_next_instr() self.end_program = self.cargador.dirsc if self.cargador.error_duplicado: QtGui.QMessageBox.about(self,"Error","Error simbolo duplicado") elif self.cargador.error_indefinido: QtGui.QMessageBox.about(self,"Error","Error simbolo indefinido") def increment_cp(self,num): num_hexa = self.convert.decimal_to_hexadecimal(num) cp = self.get_register(self.REG_CP) res = self.hexa.plus(num_hexa,cp) self.set_register(self.REG_CP,res) def show_next_instr(self): op = self.get_operation() self.window.label_sig.setText(op[1]) def get_operation(self): cp = self.get_register(self.REG_CP) value = self.get_data_form_mem(cp,1) val = value[0] val2 = value[1] val_d = int(self.convert.to_decimal(val2+"H")) val_d = val_d & 12 value = val + self.convert.decimal_to_hexadecimal(str(val_d))[:-1] tip = "Error" op = self.operations_1.get(value,"Error") if not op == "Error": tip = "1" else: op = self.operations_2.get(value,"Error") if not op == "Error": tip = "2" else: op = self.operations_3.get(value,"Error") if not op == "Error": tip = "3" return [value,op,tip] ##regresa los datos de una localidad y las licalidades #siguientes # @param localidad donde se obtendra el dato # @param num_loc cuantas localidades siguientes accedera def get_data_form_mem(self,location,num_loc): loc = self.register.filter_number(location) row = self.get_row_index(loc) col = self.get_column_index(loc) data = "" it = col count = 0 while count < num_loc: item = self.window.tableWidget.item(row,it) data += str(item.text()) it = (it+1)%17 if it == 0: it = 1 row+=1 count += 1 return data def get_row_index(self,value): index = str(value[0:-1])+"0" it = 0 index = self.register.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): index = str(value[-1])+"H" index = self.convert.to_decimal(index) return index+1 def get_m_type3(self): cp = self.get_register(self.REG_CP) data1 = self.get_data_form_mem(cp,1) type_d = self.get_operation_type(data1) next_loc = self.hexa.plus(cp,"1H") data = self.get_data_form_mem(next_loc,1) print data1,type_d,next_loc,data msb = data[0] msb_d = int(self.convert.to_decimal(msb+"H")) tam = 3 if (msb_d & 1) == 1: tam = 4 next_loc = self.hexa.plus(next_loc,"1H") m = data[1] + self.get_data_form_mem(next_loc,tam-2) print m,tam,next_loc cp = self.get_register(self.REG_CP) cp = self.hexa.plus(cp,str(tam)) self.set_register(self.REG_CP,cp) if type_d == "simple_s": m = str(msb_d & 7) + m type_d = "simple" elif (msb_d & 6) == 0: m = m else: if (msb_d & 4) == 4: b = self.get_register(self.REG_B) m = self.hexa.change_hexadecimal(m) print "rel base",b,m m = self.hexa.suma(m,b) if (msb_d & 2) == 2: cp = self.get_register(self.REG_CP) m = self.hexa.change_hexadecimal(m) print "rel cp",cp,m m = self.hexa.suma(m,cp) if (msb_d & 8) == 8: x = self.get_register(self.REG_X) m = self.hexa.change_hexadecimal(m) print ", X",x,m m = self.hexa.suma(m,x) print "mem",m if not type_d == "inmediato": m = self.register.adjust_bytes(m,6,False) m = self.get_data_form_mem(m,3) if not type_d == "simple": m = self.register.adjust_bytes(m,6,False) m = self.get_data_form_mem(m,3) print "return",m return m def get_m_type3_m(self): cp = self.get_register(self.REG_CP) data1 = self.get_data_form_mem(cp,1) type_d = self.get_operation_type(data1) next_loc = self.hexa.plus(cp,"1H") data = self.get_data_form_mem(next_loc,1) print data1,type_d,next_loc,data msb = data[0] msb_d = int(self.convert.to_decimal(msb+"H")) tam = 3 if (msb_d & 1) == 1: tam = 4 next_loc = self.hexa.plus(next_loc,"1H") m = data[1] + self.get_data_form_mem(next_loc,tam-2) print m,tam,next_loc cp = self.get_register(self.REG_CP) cp = self.hexa.plus(cp,str(tam)) self.set_register(self.REG_CP,cp) if type_d == "simple_s": m = str(msb_d & 7) + m type_d = "simple" elif (msb_d & 6) == 0: m = m else: if (msb_d & 4) == 4: b = self.get_register(self.REG_B) m = self.hexa.change_hexadecimal(m) print "rel base",b,m m = self.hexa.suma(m,b) if (msb_d & 2) == 2: cp = self.get_register(self.REG_CP) m = self.hexa.change_hexadecimal(m) print "rel cp",cp,m m = self.hexa.suma(m,cp) if (msb_d & 8) == 8: x = self.get_register(self.REG_X) m = self.hexa.change_hexadecimal(m) print ", X",x,m m = self.hexa.suma(m,x) print "mem",m # if not type_d == "inmediato": # m = self.register.adjust_bytes(m,6,False) # m = self.get_data_form_mem(m,3) # if not type_d == "simple": # m = self.register.adjust_bytes(m,6,False) # m = self.get_data_form_mem(m,3) # print "return",m return m def get_operation_type(self,data): d = data[1] d_d = int(self.convert.to_decimal(d+"H")) val = d_d & 3 if val == 3: return "simple" elif val == 2: return "indirecto" elif val == 1: return "inmediato" return "simple_s" def add(self,m): string = "ADD\nA <- (A) + (m..m+2)" a = self.get_register(self.REG_A) res = self.hexa.plus(a,m) string += "\n(A) = "+a+"\n(m..m+2) = "+m+"\n(A) = "+res self.set_register(self.REG_A,res) self.window.textEdit_Actions.setText(string) def addr(self,m): string ="ADDR\nr2 <- (r1) + (r2)"+"\n" r1 = self.registers[m[0]] r2 = self.registers[m[1]] string += "(r1) = "+r1[1]+"\n (r2) = "+r2[1]+"\n" dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) res = self.hexa.plus(dat1,dat2) string += "("+r2[1]+")<-"+res self.set_register(r2[0],res) self.window.textEdit_Actions.setText(string) def and_op(self,m): string = "AND\nA <- (A) & (m..m+2)\n" a = self.get_register(self.REG_A) string += "(A) : " + a +"\n" +"(m..m+2) : "+m res = self.hexa.and_op(a,m) self.set_register(self.REG_A,res) self.window.textEdit_Actions.setText(string) def clear(self,m): r = self.registers[m[0]] string = "CLEAR\nr1 <- 0\nr1:"+r[1] self.set_register(r[0],"0H") self.window.textEdit_Actions.setText(string) def comp(self,m): string = "COMP\nCC = (A) : (m..m+2)" a = self.get_register(self.REG_A) string += "\n (A) = "+a+"\n (m..m+2) = "+m self.cc_val = self.hexa.cmp_op(a,m) string += "\nCC ="+self.cc_val self.window.textEdit_Actions.setText(string) def compr(self,m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "COMPR\nCC = r1 : r2\n"+"r1 = "+r1[1]+"\nr2 = "+r2[1] self.cc_val = self.hexa.cmp_op(dat1,dat2) string += "\n CC = "+self.cc_val self.window.textEdit_Actions.setText(string) def div(self,m): string = "DIV\nA <- (A) / (m..m+2)" a = self.get_register(self.REG_A) res = self.hexa.div(a,m) string += "\n(A) = "+a+"\n(m..m+2) = "+m+"\n(A) = "+res self.set_register(self.REG_A,res) self.window.textEdit_Actions.setText(string) def divr(self,m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "DIVR\nr2 <- (r2) / (r1)\nr1 = "+r1[1] +"\nr2 = "+r2[1] res = self.hexa.div(dat2,dat1) string += "r2 = "+res self.set_register(r2[0],res) self.window.textEdit_Actions.setText(string) def j_op(self,m): string = "J: \n CP <- m\nm = "+m self.set_register(self.REG_CP,m) self.window.textEdit_Actions.setText(string) def jeq(self,m): string = "JEQ: \nsi CC == '=':\n\tCP <- m\n" string += "CC = '"+self.cc_val+"'" if self.cc_val == "=": self.set_register(self.REG_CP,m) string += "\nCP <- "+m self.window.textEdit_Actions.setText(string) def jgt(self,m): string = "JGT: \nsi CC == '>':\n\tCP <- m\n" string += "CC = '"+self.cc_val+"'" if self.cc_val == ">": self.set_register(self.REG_CP,m) string += "\nCP <- "+m self.window.textEdit_Actions.setText(string) def jlt(self,m): string = "JLT: \nsi CC == '<':\n\tCP <- m\n" string += "CC = '"+self.cc_val+"'" if self.cc_val == "<": self.set_register(self.REG_CP,m) string += "\nCP <- "+m self.window.textEdit_Actions.setText(string) def jsub(self,m): string = "JSUB:\n L <- (CP)\nCP <- m" cp = self.get_register(self.REG_CP) string += "\n(CP) = "+cp+"\nm = "+m self.set_register(self.REG_L,cp) self.set_register(self.REG_CP,m) self.window.textEdit_Actions.setText(string) def lda(self,m): string = "LDA\nA <- (m..m+2)\n(m..m+2) = "+m self.set_register(self.REG_A,m) self.window.textEdit_Actions.setText(string) def ldb(self,m): string = "LDB\nB <- (m..m+2)\n(m..m+2) = "+m self.set_register(self.REG_B,m) self.window.textEdit_Actions.setText(string) def ldch(self,m): string = "LDCH\nA[+der] <- (m)" a = self.get_register(self.REG_A) m = self.register.adjust_bytes(m,6,False) m = self.get_data_form_mem(m,1) string += "\n(A) = "+a+"\n(m) = "+m a = self.register.filter_number(a) a = a[0:-2] + m self.set_register(self.REG_A,a) self.window.textEdit_Actions.setText(string) def ldl(self,m): string = "LDL\nL <- (m..m+2)\n(m..m+2) = "+m self.set_register(self.REG_L,m) self.window.textEdit_Actions.setText(string) def lds(self,m): string = "LDS\nS <- (m..m+2)\n(m..m+2) = "+m self.set_register(self.REG_S,m) self.window.textEdit_Actions.setText(string) def ldt(self,m): string = "LDT\nT <- (m..m+2)\n(m..m+2) = "+m self.set_register(self.REG_T,m) self.window.textEdit_Actions.setText(string) def ldx(self,m): string = "LDX\nX <- (m..m+2)\n(m..m+2) = "+m self.set_register(self.REG_X,m) self.window.textEdit_Actions.setText(string) def mul(self,m): a = self.get_register(self.REG_A) string = "MUL\nA <- (A) * (m..m+2)\n(A) = "+a+"\n(m..m+2)="+m res = self.hexa.mul(a,m) string += "\n (A) <- "+res self.set_register(self.REG_A,res) self.window.textEdit_Actions.setText(string) def mulr(self,m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "MULR\nr2 <- (r2) * (r1)\nr1 = "+r1[1] +"\nr2 = "+r2[1] res = self.hexa.mul(dat2,dat1) string += "r2 = "+res self.set_register(r2[0],res) self.window.textEdit_Actions.setText(string) def or_op(self,m): string = "OR\nA <- (A) | (m..m+2)\n" a = self.get_register(self.REG_A) string += "(A) : " + a +"\n" +"(m..m+2) : "+m res = self.hexa.or_op(a,m) self.set_register(self.REG_A,res) self.window.textEdit_Actions.setText(string) def rmo(self,m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) string = "RMO\nr2 <- (r1)\n r1 = "+r1[1]+"\nr2 ="+r2[1] string += "\nr2 <- " + dat1 self.set_register(r2[0],dat1) self.window.textEdit_Actions.setText(string) def rsub(self,m): l = self.get_register(self.REG_L) string = "RSUB\nCP <- (L)\n(L) = "+l string += "CP <-"+l self.set_register(self.REG_CP,l) self.window.textEdit_Actions.setText(string) def sta(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_A) string = "STA\n m..m+2 <- (A)\nm..m+2 = "+m+"\n(A) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def sta(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_A) string = "STA\n m..m+2 <- (A)\nm..m+2 = "+m+"\n(A) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def stb(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_B) string = "STB\n m..m+2 <- (B)\nm..m+2 = "+m+"\n(B) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def stch(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_A) reg = self.register.filter_number(reg)[-2:] string = "STCH\n m <- (A)[+der]\nm..m+2 = "+m+"\n(A)[+der] = "+reg ##S rm = self.hexa.plus(m,"2H") m = self.register.adjust_bytes(m,6,False) print "stsh",reg,m self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def stl(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_L) string = "STL\n m..m+2 <- (L)\nm..m+2 = "+m+"\n(L) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def sts(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_S) string = "STS\n m..m+2 <- (S)\nm..m+2 = "+m+"\n(S) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def stsw(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_SW) string = "STSW\n m..m+2 <- (SW)\nm..m+2 = "+m+"\n(SW) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def stt(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_T) string = "STT\n m..m+2 <- (T)\nm..m+2 = "+m+"\n(T) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def stx(self,m): m = self.register.adjust_bytes(m,6,False) reg = self.get_register(self.REG_X) string = "STX\n m..m+2 <- (X)\nm..m+2 = "+m+"\n(X) = "+reg reg = self.register.filter_number(reg) self.modf_reg(reg,m) self.window.textEdit_Actions.setText(string) def modf_reg(self,reg,m): row = self.get_row_index(m) col = self.get_column_index(m) it = col count = 0 while count < len(reg): item = self.window.tableWidget.item(row,it) text = reg[count:count+2] count += 2 item.setText(text) it = (it+1)%17 if it == 0: it = 1 row+=1 def sub(self,m): string = "ADD\nA <- (A) - (m..m+2)" a = self.get_register(self.REG_A) res = self.hexa.subs(a,m) string += "\n(A) = "+a+"\n(m..m+2) = "+m+"\n(A) = "+res self.set_register(self.REG_A,res) self.window.textEdit_Actions.setText(string) def subr(self,m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "SUBR\nr2 <- (r2) - (r1)\nr1 = "+r1[1] +"\nr2 = "+r2[1] res = self.hexa.subs(dat2,dat1) string += "r2 = "+res self.set_register(r2[0],res) self.window.textEdit_Actions.setText(string) def tix(self,m): x = self.get_register(self.REG_X) string ="TIX\n(X) <- (X) + 1\n(X) : (m..m+2)\n(X)="+x x = self.hexa.plus(x,"1H") self.set_register(self.REG_X,x) string +="\n(X)<- (X) + 1\n(X) <- "+x+"\n(m..m+2)="+m self.cc_val = self.hexa.cmp_op(x,m) string += "\nCC = "+self.cc_val self.window.textEdit_Actions.setText(string) def tixr(self,m): r1 = self.registers[m[0]] dat1 = self.get_register(r1[0]) string = "TIX\n X <- (X) + 1\n(X) : (r1)\nr1 = "+r1[1] x = self.get_register(self.REG_X) x = self.hexa.plus(x,"1H") self.set_register(self.REG_X,x) self.cc_val = self.hexa.cmp_op(x,dat1) string += "CC = "+self.cc_val self.window.textEdit_Actions.setText(string)
class Cargadorx(): def __init__(self, window): self.window = window self.tabse = Tabse() self.lonsc = "0H" self.dirsc = "0H" self.dirprog = "0H" self.direj = "0H" self.hexa = Hexadecimal() self.convert = Convert() self.register = Register("A") self.error_indefinido = False self.error_duplicado = False def show_error(self, msg): ##QtGui.QMessageBox.about(self.window,"Error",msg) self.window.textEdit_Actions.setText(msg) def load_file_name(self, list_obj, dirprog): val_int = self.convert.to_decimal(dirprog) self.dirprog = self.convert.decimal_to_hexadecimal(val_int) self.dirprog = self.register.adjust_bytes(self.dirprog, 6, False) + "H" self.dirsc = self.dirprog self.step_1(list_obj) if not self.error_duplicado: self.create_memory() self.dirsc = self.dirprog self.direj = self.dirprog self.step_2(list_obj) self.init_empty_rows() self.tabse.print_tabse() if self.error_indefinido: self.show_error("Error en simbolo indefinido") else: self.show_error("Error simbolo duplicado") def step_1(self, list_obj): for list_n in list_obj: for n in list_n: if len(n) > 0: if n[0] == "H": self.step_h(n, 1) elif n[0] == "D": self.step_1_d(n) self.dirsc = self.hexa.plus(self.dirsc, self.lonsc) def step_h(self, n, step): name = n[1:7] name = name.strip() self.lonsc = n[13:] + "H" if step == 1: if not self.tabse.exist_node(name): self.tabse.insert_section(name, self.dirsc, self.lonsc) else: ##print name self.error_duplicado = True def step_1_d(self, n): num = (len(n) - 1) / 12 it = 0 # print "num",num,len(n) while it < num: index1 = (it * 12) + 1 index2 = index1 + 6 index3 = index2 + 6 name = n[index1:index2].strip() #print "name",name if index3 >= len(n): len_r = n[index2:] + "H" else: len_r = n[index2:index3] + "H" if not self.tabse.exist_node(name): val = self.hexa.plus(len_r, self.dirsc) self.tabse.insert_variable(name, val) else: print "error", name self.error_duplicado = True it += 1 def step_2(self, list_obj): for list_n in list_obj: for n in list_n: if len(n) > 0: if n[0] == "H": self.step_h(n, 2) elif n[0] == "T": self.charge_obj_code(n) elif n[0] == "M": self.change_m_register(n) elif n[0] == "E": self.reg_e(n) self.dirsc = self.hexa.plus(self.dirsc, self.lonsc) def reg_e(self, r): if len(r) > 1: val = r[1:] + "H" self.direj = self.hexa.plus(self.dirsc, val) def charge_obj_code(self, r): string = r[9:] index = 0 init = r[1:7] init = self.hexa.plus(init + "H", self.dirsc)[:-1] col_start = init[-1] + "H" col = int(self.convert.to_decimal(col_start) + 1) dat1 = self.get_part_dat(init) res = self.hexa.subs(dat1, self.dirprog[:-2]) dec_res = int(self.convert.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 get_part_dat(self, dat): if len(dat) == 1: return "0" else: return dat[:-1] def change_m_register(self, r): val = r[1:7] med_byte = r[7:9] sign = r[9] name = r[10:].strip() # print val,med_byte,sign,name n = self.tabse.get_register(name) if n: # print "existe" val = self.hexa.plus(val, self.dirsc)[:-1] content = self.get_content_at(val, 3) # print val,content ant_content = "" if med_byte == "5": ant_content = content[0] content = content[1:] dir_es = n.get_dir() new_dir = self.make_operation(sign, content, dir_es) # print "perico",new_dir,content,sign,dir_es new_dir = self.register.adjust_bytes(new_dir, 6, True) if med_byte == "5": new_dir = ant_content + new_dir[1:] new_dir = ant_content + new_dir # print new_dir col_num = val[-1] + "H" col = int(self.convert.to_decimal(col_num) + 1) val = self.get_part_dat(val) ren = self.hexa.subs(val, self.dirprog[:-2]) ren = int(self.convert.to_decimal(ren)) index = 0 while index < len(new_dir): byte = new_dir[index:index + 2] item = QtGui.QTableWidgetItem(byte) self.window.tableWidget.setItem(ren, col, item) index += 2 col = (col + 1) % 17 if col == 0: col = 1 ren += 1 else: self.error_indefinido = True def make_operation(self, sign, dat1, dat2): val1 = self.get_int_operator(dat1) val2 = self.get_int_operator(dat2) if sign == "-": ret = val1 - val2 else: ret = val1 + val2 ret = self.exp_to_hexadecimal(ret) return ret def get_int_operator(self, dat): if dat[-1] == "H": dat = dat[:-1] if dat[0] == "F": string = "" it = 0 while it < len(dat): if not dat[it] == "F": string += dat[it] it += 1 if len(string) == 0: return -1 else: val = "1" + ("0" * len(string)) + "H" val = self.convert.to_decimal(val) val2 = self.convert.to_decimal(string + "H") return val2 - val else: return self.convert.to_decimal(dat + "H") def get_content_at(self, dirr, num): dir_n = dirr[-1] + "H" col = int(self.convert.to_decimal(dir_n) + 1) dire = self.get_part_dat(dirr) ren = self.hexa.subs(dire, self.dirprog[:-2]) ren = int(self.convert.to_decimal(ren)) it = 0 text = "" while it < num: item = self.window.tableWidget.item(ren, col) text += str(item.text()) it += 1 col = (col + 1) % 17 if col == 0: col = 1 ren += 1 return text def exp_to_hexadecimal(self, val): if val < 0: val = int(val * -1) val = val ^ 65535 val += 1 if val == 0: val = "0H" else: val = self.convert.decimal_to_hexadecimal(val) if val[0] == "F" and len(val) > 6: val = "0" + val return val def create_memory(self): init = self.dirprog[:-1] length = self.hexa.subs(self.dirsc, self.dirprog)[:-1] star_rows = length[:-1] index = init[:-1] num_rows = star_rows + "H" num_rows = int(self.convert.to_decimal(num_rows)) self.window.tableWidget.setRowCount(num_rows + 1) it = 0 while it <= num_rows: dir = index + "0H" dir = self.register.adjust_bytes(dir, 6, False) item = QtGui.QTableWidgetItem(dir) self.window.tableWidget.setItem(it, 0, item) it += 1 index = self.hexa.plus(index, "1H")[:-1] 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)
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)
class Segment: def __init__(self): self.index = 0 self.list_n = [] self.conv = Convert() self.reg = Register("X") self.hexa = Hexadecimal() self.last_code = 0 ## regresa el segmento actual def get_segment(self): return self.list_n[self.index] ##crea un segmento con el nombre especificado # @param name nombre del segmento que se creara def new_segment(self,name): self.index = len(self.list_n) self.list_n.append(NodoS(name,self.index)) ##inserta una etiqueta a la tabla de simbolos y si esta ya se encuentra marca un error #@param name simbolo que se intentara insertar a la tabla #@param dir_val direccion o valor del simbolo #@param sym_type tipo del simbolo el cual puede ser relativo o absoluto #@param lineno numero de linea donde se encontro la etiqueta def insert_symbol(self,name,dir_val,sym_type,lineno,bloque_index,externo = False): if self.exist_symbol(name): self.insert_error(str(lineno),"etiqueta previamente definida") else: sym = Symbol(name,dir_val,sym_type,bloque_index,externo) self.get_segment().symbols.append(sym) def exist_symbol(self ,name): for it in self.get_segment().symbols: if it.get_name() == name: return it return None ##checa si en el diccionario de errores ya se inserto un error en esa linea de codigo #y si no es asi la inserta #@param line linea de codigo donde se encontro un error #@param text cadena que se genera en el error def insert_error(self,line,text): l = int(line) - self.last_code -1 if not str(l) in self.get_segment().errors and l >= 0: self.get_segment().errors[str(l)]= text ##checa si en el diccionario de advertencias ya se inserto un error en esa linea de codigo #y si no es asi la inserta #@param line linea de codigo donde se encontro un warnind #@param text cadena que se genera en el warning def insert_warning(self,line,text): l = int(line) - self.last_code if not str(l) in self.get_segment().warnings: self.get_segment().warnings[str(line)] = text ##aumenta el contador de programa #@param increment cantidad en hexadecimal que se le agregara al CP def increment_PC(self,increment): num1 = self.get_segment().pc[-1] if not self.conv.is_hexadecimal(str(increment)): increment = self.conv.decimal_to_hexadecimal(increment) val = self.hexa.plus(num1,increment) if val == "H": val = "0H" val = self.reg.adjust_bytes(val,6,False)+"H" self.get_segment().bloques.set_last_cp(val) self.get_segment().pc.append(val) self.get_segment().num_bloque.append(self.get_segment().bloques.get_index()) ## calcula el tamaño del programa #@return regresa en hexadecimal el tamaño del programa def get_len_program(self): return self.get_segment().bloques.get_len_program() ## genera y completa la tabla de bloques de todos los segmentos def genera_tabla_bloques(self): for it in self.list_n: it.bloques.gen_table() ## regresa un segmento en un index especificado # @param index posicion del segmento que se quiere def get_segment_at(self,index): return self.list_n[index] ## imprime los resultados de un segmento # @param extension es la extencion que tendran los archivos de salida de los segmentos def print_results(self,extension): for it in self.list_n: # print it.errors,it.errors_s it.archivo_intermedio(extension) if len(it.errors) == 0 and len(it.errors_s) == 0: it.archivo_objeto(extension) print "Errors", len(it.errors)+len(it.errors_s) def get_num_errors_all(self): cant = 0 for it in self.list_n: cant += len(it.errors_s) cant += len(it.errors) return cant
class Simuladorx(QtGui.QDockWidget): REG_CP = 0 REG_A = 1 REG_X = 2 REG_L = 3 REG_SW = 4 REG_B = 5 REG_S = 6 REG_T = 7 REG_F = 8 def __init__(self, parent=None): QtGui.QWidget.__init__(self) self.window = Ui_DockWidget() self.window.setupUi(self) self.add_registers() self.cargador = None self.hexa = Hexadecimal() self.convert = Convert() self.register = Register("A") self.init_registers() self.end_program = "0H" self.cc_val = "=" self.window.btnSimular.clicked.connect(self.simular) self.operations_3 = { "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", "58": "ADDF", "88": "COMPF", "64": "DIVF", "68": "LDB", "70": "LDF", "6C": "LDS", "74": "LDT", "D0": "LPS", "60": "MULF", "EC": "SSK", "78": "STB", "80": "STF", "D4": "STI", "7C": "STS", "E8": "STSW", "84": "STT", "5C": "SUBF", "E0": "TD", "DC": "WD", "44": "OR" } self.operations_1 = { "C4": "FIX", "C0": "FLOAT", "F4": "HIO", "C8": "NORM", "F0": "SIO", "F8": "TIO" } self.operations_2 = { "90": "ADDR", "B4": "CLEAR", "A0": "COMPR", "9C": "DIVR", "98": "MULR", "AC": "RMO", "A4": "SHIFTL", "A8": "SHIFTR", "94": "SUBR", "B0": "SVC", "B8": "TIXR" } self.operations = { "18": self.add, "00": self.lda, "40": self.and_op, "28": self.comp, "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, "58": self.float_operations, "88": self.float_operations, "64": self.float_operations, "68": self.ldb, "70": self.float_operations, "6C": self.lds, "74": self.ldt, "D0": self.system_operations, "60": self.float_operations, "EC": self.system_operations, "78": self.stb, "80": self.float_operations, "D4": self.system_operations, "7C": self.sts, "E8": self.system_operations, "84": self.stt, "5C": self.float_operations, "E0": self.system_operations, "DC": self.system_operations, "C4": self.float_operations, "C0": self.float_operations, "F4": self.system_operations, "C8": self.float_operations, "F0": self.system_operations, "44": self.or_op, "F8": self.system_operations, "90": self.addr, "B4": self.clear, "A0": self.compr, "9C": self.divr, "98": self.mulr, "AC": self.rmo, "A4": "SHIFTL", "A8": "SHIFTR", "94": self.subr, "B0": "SVC", "B8": self.tixr } self.registers = { "0": [self.REG_A, "A"], "1": [self.REG_X, "X"], "2": [self.REG_L, "L"], "8": [self.REG_CP, "CP"], "9": [self.REG_SW, "SW"], "3": [self.REG_B, "B"], "4": [self.REG_S, "S"], "5": [self.REG_T, "T"], "6": [self.REG_F, "F"] } self.operations_m = [ "J", "JLT", "JEQ", "JGT", "JSUB", "STA", "STB", "STCH", "STL", "STS", "STSW", "STT", "STX", "LDCH" ] def simular(self): num_actions = self.get_count_next_actions() print "num actions", num_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): it = 0 while it < number: cp = self.get_register(self.REG_CP) if self.hexa.minus_than(cp, self.end_program): op = self.get_operation() type_op = op[2] inst = op[1] m = self.get_m(type_op, cp, inst) if m == "Error": self.window.textEdit_Actions.setText( "El programa se termino debido a un error de operacion" ) else: fun = self.operations[op[0]] fun(m) self.show_next_instr() else: self.window.textEdit_Actions.setText("El programa se termino") it += 1 def get_m(self, type_op, cp, inst): if type_op == "Error": return type_op if type_op == "1": self.increment_cp(1) return "0" if type_op == "2": cp_n = self.hexa.plus(cp, "1H") self.increment_cp(2) return self.get_data_form_mem(cp_n, 1) if inst in self.operations_m: print "\n\n" + inst + "\n\n" return self.get_m_type3_m() return self.get_m_type3() 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 system_operations(self, val): self.window.textEdit_Actions.setText( "No tiene los permisos \n para ejecutar esta instruccion") def float_operations(self, val): self.window.textEdit_Actions.setText( "Las instrucciones flotantes \n no se pueden ejecutar") def add_registers(self): self.window.tableWidget_2.setRowCount(9) item = QtGui.QTableWidgetItem() item.setText("B") self.window.tableWidget_2.setVerticalHeaderItem(5, item) item = QtGui.QTableWidgetItem() item.setText("S") self.window.tableWidget_2.setVerticalHeaderItem(6, item) item = QtGui.QTableWidgetItem() item.setText("T") self.window.tableWidget_2.setVerticalHeaderItem(7, item) item = QtGui.QTableWidgetItem() item.setText("F") self.window.tableWidget_2.setVerticalHeaderItem(8, item) def init_registers(self): it = 0 while it < 9: item_text = QtGui.QTableWidgetItem("FFFFFFFH") self.window.tableWidget_2.setItem(it, 0, item_text) it += 1 def set_register(self, reg_num, val): val_hex = self.register.adjust_bytes(val, 6, False) val_hex = self.hexa.change_hexadecimal(val_hex) item_text = QtGui.QTableWidgetItem(val_hex) self.window.tableWidget_2.setItem(reg_num, 0, item_text) def get_register(self, reg_num): item = self.window.tableWidget_2.item(reg_num, 0) return str(item.text()) def carga(self, list_obj, dirprog): self.cargador = Cargadorx(self.window) self.cargador.load_file_name(list_obj, dirprog) cp = self.cargador.direj self.set_register(self.REG_CP, cp) self.show_next_instr() self.end_program = self.cargador.dirsc if self.cargador.error_duplicado: QtGui.QMessageBox.about(self, "Error", "Error simbolo duplicado") elif self.cargador.error_indefinido: QtGui.QMessageBox.about(self, "Error", "Error simbolo indefinido") def increment_cp(self, num): num_hexa = self.convert.decimal_to_hexadecimal(num) cp = self.get_register(self.REG_CP) res = self.hexa.plus(num_hexa, cp) self.set_register(self.REG_CP, res) def show_next_instr(self): op = self.get_operation() self.window.label_sig.setText(op[1]) def get_operation(self): cp = self.get_register(self.REG_CP) value = self.get_data_form_mem(cp, 1) val = value[0] val2 = value[1] val_d = int(self.convert.to_decimal(val2 + "H")) val_d = val_d & 12 value = val + self.convert.decimal_to_hexadecimal(str(val_d))[:-1] tip = "Error" op = self.operations_1.get(value, "Error") if not op == "Error": tip = "1" else: op = self.operations_2.get(value, "Error") if not op == "Error": tip = "2" else: op = self.operations_3.get(value, "Error") if not op == "Error": tip = "3" return [value, op, tip] ##regresa los datos de una localidad y las licalidades #siguientes # @param localidad donde se obtendra el dato # @param num_loc cuantas localidades siguientes accedera def get_data_form_mem(self, location, num_loc): loc = self.register.filter_number(location) row = self.get_row_index(loc) col = self.get_column_index(loc) data = "" it = col count = 0 while count < num_loc: item = self.window.tableWidget.item(row, it) data += str(item.text()) it = (it + 1) % 17 if it == 0: it = 1 row += 1 count += 1 return data def get_row_index(self, value): index = str(value[0:-1]) + "0" it = 0 index = self.register.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): index = str(value[-1]) + "H" index = self.convert.to_decimal(index) return index + 1 def get_m_type3(self): cp = self.get_register(self.REG_CP) data1 = self.get_data_form_mem(cp, 1) type_d = self.get_operation_type(data1) next_loc = self.hexa.plus(cp, "1H") data = self.get_data_form_mem(next_loc, 1) print data1, type_d, next_loc, data msb = data[0] msb_d = int(self.convert.to_decimal(msb + "H")) tam = 3 if (msb_d & 1) == 1: tam = 4 next_loc = self.hexa.plus(next_loc, "1H") m = data[1] + self.get_data_form_mem(next_loc, tam - 2) print m, tam, next_loc cp = self.get_register(self.REG_CP) cp = self.hexa.plus(cp, str(tam)) self.set_register(self.REG_CP, cp) if type_d == "simple_s": m = str(msb_d & 7) + m type_d = "simple" elif (msb_d & 6) == 0: m = m else: if (msb_d & 4) == 4: b = self.get_register(self.REG_B) m = self.hexa.change_hexadecimal(m) print "rel base", b, m m = self.hexa.suma(m, b) if (msb_d & 2) == 2: cp = self.get_register(self.REG_CP) m = self.hexa.change_hexadecimal(m) print "rel cp", cp, m m = self.hexa.suma(m, cp) if (msb_d & 8) == 8: x = self.get_register(self.REG_X) m = self.hexa.change_hexadecimal(m) print ", X", x, m m = self.hexa.suma(m, x) print "mem", m if not type_d == "inmediato": m = self.register.adjust_bytes(m, 6, False) m = self.get_data_form_mem(m, 3) if not type_d == "simple": m = self.register.adjust_bytes(m, 6, False) m = self.get_data_form_mem(m, 3) print "return", m return m def get_m_type3_m(self): cp = self.get_register(self.REG_CP) data1 = self.get_data_form_mem(cp, 1) type_d = self.get_operation_type(data1) next_loc = self.hexa.plus(cp, "1H") data = self.get_data_form_mem(next_loc, 1) print data1, type_d, next_loc, data msb = data[0] msb_d = int(self.convert.to_decimal(msb + "H")) tam = 3 if (msb_d & 1) == 1: tam = 4 next_loc = self.hexa.plus(next_loc, "1H") m = data[1] + self.get_data_form_mem(next_loc, tam - 2) print m, tam, next_loc cp = self.get_register(self.REG_CP) cp = self.hexa.plus(cp, str(tam)) self.set_register(self.REG_CP, cp) if type_d == "simple_s": m = str(msb_d & 7) + m type_d = "simple" elif (msb_d & 6) == 0: m = m else: if (msb_d & 4) == 4: b = self.get_register(self.REG_B) m = self.hexa.change_hexadecimal(m) print "rel base", b, m m = self.hexa.suma(m, b) if (msb_d & 2) == 2: cp = self.get_register(self.REG_CP) m = self.hexa.change_hexadecimal(m) print "rel cp", cp, m m = self.hexa.suma(m, cp) if (msb_d & 8) == 8: x = self.get_register(self.REG_X) m = self.hexa.change_hexadecimal(m) print ", X", x, m m = self.hexa.suma(m, x) print "mem", m # if not type_d == "inmediato": # m = self.register.adjust_bytes(m,6,False) # m = self.get_data_form_mem(m,3) # if not type_d == "simple": # m = self.register.adjust_bytes(m,6,False) # m = self.get_data_form_mem(m,3) # print "return",m return m def get_operation_type(self, data): d = data[1] d_d = int(self.convert.to_decimal(d + "H")) val = d_d & 3 if val == 3: return "simple" elif val == 2: return "indirecto" elif val == 1: return "inmediato" return "simple_s" def add(self, m): string = "ADD\nA <- (A) + (m..m+2)" a = self.get_register(self.REG_A) res = self.hexa.plus(a, m) string += "\n(A) = " + a + "\n(m..m+2) = " + m + "\n(A) = " + res self.set_register(self.REG_A, res) self.window.textEdit_Actions.setText(string) def addr(self, m): string = "ADDR\nr2 <- (r1) + (r2)" + "\n" r1 = self.registers[m[0]] r2 = self.registers[m[1]] string += "(r1) = " + r1[1] + "\n (r2) = " + r2[1] + "\n" dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) res = self.hexa.plus(dat1, dat2) string += "(" + r2[1] + ")<-" + res self.set_register(r2[0], res) self.window.textEdit_Actions.setText(string) def and_op(self, m): string = "AND\nA <- (A) & (m..m+2)\n" a = self.get_register(self.REG_A) string += "(A) : " + a + "\n" + "(m..m+2) : " + m res = self.hexa.and_op(a, m) self.set_register(self.REG_A, res) self.window.textEdit_Actions.setText(string) def clear(self, m): r = self.registers[m[0]] string = "CLEAR\nr1 <- 0\nr1:" + r[1] self.set_register(r[0], "0H") self.window.textEdit_Actions.setText(string) def comp(self, m): string = "COMP\nCC = (A) : (m..m+2)" a = self.get_register(self.REG_A) string += "\n (A) = " + a + "\n (m..m+2) = " + m self.cc_val = self.hexa.cmp_op(a, m) string += "\nCC =" + self.cc_val self.window.textEdit_Actions.setText(string) def compr(self, m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "COMPR\nCC = r1 : r2\n" + "r1 = " + r1[1] + "\nr2 = " + r2[1] self.cc_val = self.hexa.cmp_op(dat1, dat2) string += "\n CC = " + self.cc_val self.window.textEdit_Actions.setText(string) def div(self, m): string = "DIV\nA <- (A) / (m..m+2)" a = self.get_register(self.REG_A) res = self.hexa.div(a, m) string += "\n(A) = " + a + "\n(m..m+2) = " + m + "\n(A) = " + res self.set_register(self.REG_A, res) self.window.textEdit_Actions.setText(string) def divr(self, m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "DIVR\nr2 <- (r2) / (r1)\nr1 = " + r1[1] + "\nr2 = " + r2[1] res = self.hexa.div(dat2, dat1) string += "r2 = " + res self.set_register(r2[0], res) self.window.textEdit_Actions.setText(string) def j_op(self, m): string = "J: \n CP <- m\nm = " + m self.set_register(self.REG_CP, m) self.window.textEdit_Actions.setText(string) def jeq(self, m): string = "JEQ: \nsi CC == '=':\n\tCP <- m\n" string += "CC = '" + self.cc_val + "'" if self.cc_val == "=": self.set_register(self.REG_CP, m) string += "\nCP <- " + m self.window.textEdit_Actions.setText(string) def jgt(self, m): string = "JGT: \nsi CC == '>':\n\tCP <- m\n" string += "CC = '" + self.cc_val + "'" if self.cc_val == ">": self.set_register(self.REG_CP, m) string += "\nCP <- " + m self.window.textEdit_Actions.setText(string) def jlt(self, m): string = "JLT: \nsi CC == '<':\n\tCP <- m\n" string += "CC = '" + self.cc_val + "'" if self.cc_val == "<": self.set_register(self.REG_CP, m) string += "\nCP <- " + m self.window.textEdit_Actions.setText(string) def jsub(self, m): string = "JSUB:\n L <- (CP)\nCP <- m" cp = self.get_register(self.REG_CP) string += "\n(CP) = " + cp + "\nm = " + m self.set_register(self.REG_L, cp) self.set_register(self.REG_CP, m) self.window.textEdit_Actions.setText(string) def lda(self, m): string = "LDA\nA <- (m..m+2)\n(m..m+2) = " + m self.set_register(self.REG_A, m) self.window.textEdit_Actions.setText(string) def ldb(self, m): string = "LDB\nB <- (m..m+2)\n(m..m+2) = " + m self.set_register(self.REG_B, m) self.window.textEdit_Actions.setText(string) def ldch(self, m): string = "LDCH\nA[+der] <- (m)" a = self.get_register(self.REG_A) m = self.register.adjust_bytes(m, 6, False) m = self.get_data_form_mem(m, 1) string += "\n(A) = " + a + "\n(m) = " + m a = self.register.filter_number(a) a = a[0:-2] + m self.set_register(self.REG_A, a) self.window.textEdit_Actions.setText(string) def ldl(self, m): string = "LDL\nL <- (m..m+2)\n(m..m+2) = " + m self.set_register(self.REG_L, m) self.window.textEdit_Actions.setText(string) def lds(self, m): string = "LDS\nS <- (m..m+2)\n(m..m+2) = " + m self.set_register(self.REG_S, m) self.window.textEdit_Actions.setText(string) def ldt(self, m): string = "LDT\nT <- (m..m+2)\n(m..m+2) = " + m self.set_register(self.REG_T, m) self.window.textEdit_Actions.setText(string) def ldx(self, m): string = "LDX\nX <- (m..m+2)\n(m..m+2) = " + m self.set_register(self.REG_X, m) self.window.textEdit_Actions.setText(string) def mul(self, m): a = self.get_register(self.REG_A) string = "MUL\nA <- (A) * (m..m+2)\n(A) = " + a + "\n(m..m+2)=" + m res = self.hexa.mul(a, m) string += "\n (A) <- " + res self.set_register(self.REG_A, res) self.window.textEdit_Actions.setText(string) def mulr(self, m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "MULR\nr2 <- (r2) * (r1)\nr1 = " + r1[1] + "\nr2 = " + r2[1] res = self.hexa.mul(dat2, dat1) string += "r2 = " + res self.set_register(r2[0], res) self.window.textEdit_Actions.setText(string) def or_op(self, m): string = "OR\nA <- (A) | (m..m+2)\n" a = self.get_register(self.REG_A) string += "(A) : " + a + "\n" + "(m..m+2) : " + m res = self.hexa.or_op(a, m) self.set_register(self.REG_A, res) self.window.textEdit_Actions.setText(string) def rmo(self, m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) string = "RMO\nr2 <- (r1)\n r1 = " + r1[1] + "\nr2 =" + r2[1] string += "\nr2 <- " + dat1 self.set_register(r2[0], dat1) self.window.textEdit_Actions.setText(string) def rsub(self, m): l = self.get_register(self.REG_L) string = "RSUB\nCP <- (L)\n(L) = " + l string += "CP <-" + l self.set_register(self.REG_CP, l) self.window.textEdit_Actions.setText(string) def sta(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_A) string = "STA\n m..m+2 <- (A)\nm..m+2 = " + m + "\n(A) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def sta(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_A) string = "STA\n m..m+2 <- (A)\nm..m+2 = " + m + "\n(A) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def stb(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_B) string = "STB\n m..m+2 <- (B)\nm..m+2 = " + m + "\n(B) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def stch(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_A) reg = self.register.filter_number(reg)[-2:] string = "STCH\n m <- (A)[+der]\nm..m+2 = " + m + "\n(A)[+der] = " + reg ##S rm = self.hexa.plus(m,"2H") m = self.register.adjust_bytes(m, 6, False) print "stsh", reg, m self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def stl(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_L) string = "STL\n m..m+2 <- (L)\nm..m+2 = " + m + "\n(L) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def sts(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_S) string = "STS\n m..m+2 <- (S)\nm..m+2 = " + m + "\n(S) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def stsw(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_SW) string = "STSW\n m..m+2 <- (SW)\nm..m+2 = " + m + "\n(SW) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def stt(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_T) string = "STT\n m..m+2 <- (T)\nm..m+2 = " + m + "\n(T) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def stx(self, m): m = self.register.adjust_bytes(m, 6, False) reg = self.get_register(self.REG_X) string = "STX\n m..m+2 <- (X)\nm..m+2 = " + m + "\n(X) = " + reg reg = self.register.filter_number(reg) self.modf_reg(reg, m) self.window.textEdit_Actions.setText(string) def modf_reg(self, reg, m): row = self.get_row_index(m) col = self.get_column_index(m) it = col count = 0 while count < len(reg): item = self.window.tableWidget.item(row, it) text = reg[count:count + 2] count += 2 item.setText(text) it = (it + 1) % 17 if it == 0: it = 1 row += 1 def sub(self, m): string = "ADD\nA <- (A) - (m..m+2)" a = self.get_register(self.REG_A) res = self.hexa.subs(a, m) string += "\n(A) = " + a + "\n(m..m+2) = " + m + "\n(A) = " + res self.set_register(self.REG_A, res) self.window.textEdit_Actions.setText(string) def subr(self, m): r1 = self.registers[m[0]] r2 = self.registers[m[1]] dat1 = self.get_register(r1[0]) dat2 = self.get_register(r2[0]) string = "SUBR\nr2 <- (r2) - (r1)\nr1 = " + r1[1] + "\nr2 = " + r2[1] res = self.hexa.subs(dat2, dat1) string += "r2 = " + res self.set_register(r2[0], res) self.window.textEdit_Actions.setText(string) def tix(self, m): x = self.get_register(self.REG_X) string = "TIX\n(X) <- (X) + 1\n(X) : (m..m+2)\n(X)=" + x x = self.hexa.plus(x, "1H") self.set_register(self.REG_X, x) string += "\n(X)<- (X) + 1\n(X) <- " + x + "\n(m..m+2)=" + m self.cc_val = self.hexa.cmp_op(x, m) string += "\nCC = " + self.cc_val self.window.textEdit_Actions.setText(string) def tixr(self, m): r1 = self.registers[m[0]] dat1 = self.get_register(r1[0]) string = "TIX\n X <- (X) + 1\n(X) : (r1)\nr1 = " + r1[1] x = self.get_register(self.REG_X) x = self.hexa.plus(x, "1H") self.set_register(self.REG_X, x) self.cc_val = self.hexa.cmp_op(x, dat1) string += "CC = " + self.cc_val self.window.textEdit_Actions.setText(string)
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)