def test_hex_int(self): assert int_to_hex(5) == '5' assert int_to_hex(10) == 'a' assert int_to_hex(5, 2) == '05' assert int_to_hex(32, 2) == '20' assert hex_to_int('5') == 5 assert hex_to_int('a') == 10 assert hex_to_int('05') == 5 assert hex_to_int('20') == 32
def create_virtual_memory( self, code, code_size, data, data_size, stdin_handler=None, stdout_handler=None): u""" Išskiria virtualią atmintį ir į ją įkelia kodą bei duomenis. """ pager = Pager( self, C=code_size, D=data_size, stdin_handler=stdin_handler, stdout_handler=stdout_handler) # Įkeliamas kodo segmentas. vmcode = VirtualMemoryCode(self, pager) labels = {} clean_code = [] for i, line in enumerate(code): if ':' in line: label, command = line.split(':') labels[label.strip()] = i else: command = line clean_code.append(command.strip().decode('utf-8')) for i, command in enumerate(clean_code): if u'«' in command: label = command.split(u'«', 1)[1].split(u'»', 1)[0] command = command.replace( u'«{0}»'.format(label), int_to_hex(labels[label], 3)).encode('utf-8') else: command = command.encode('utf-8') command += ' ' * (WORD_SIZE - len(command)) vmcode[i] = command # Įkeliamas duomenų segmentas. vmdata = VirtualMemoryData(self, pager) clean_data = {} address = 0 for i, line in enumerate(data): result = re.search('^\[(?P<address>\w+)\]:', line) if result: hex_address = result.groupdict()['address'] line = line[len('[{0}]:'.format(hex_address)):] address = hex_to_int(hex_address) if not clean_data.has_key(address): clean_data[address] = [] line = line.replace('\n', '') clean_data[address].append(line) word_format = '{{0:<{0}}}'.format(WORD_SIZE) for address, lines in clean_data.items(): data = ''.join(lines) for i, j in enumerate(range(0, len(data), WORD_SIZE)): vmdata[address + i] = word_format.format( data[j:j + WORD_SIZE]) return vmcode, vmdata
def get_code_cell_address(self, virtual_address): u""" Apskaičiuoja realų ląstelės adresą pagal kodo segmento virtualų adresą. """ virtual_address = self.memory.get_address_int(virtual_address) C = self.get_C() min_address = 0 max_address = C * BLOCK_SIZE if not (min_address <= virtual_address <= max_address): raise BadAddress( u'Virtualus adresas nepriklauso kodo segmentui.') virtual_block, cell = self.memory.get_address_tuple(virtual_address) block = hex_to_int( self.get_byte(4 + 2 * virtual_block) + self.get_byte(4 + 2 * virtual_block + 1)) return block, cell
def get_data_cell_address(self, virtual_address): u""" Apskaičiuoja realų ląstelės adresą pagal duomenų segmento virtualų adresą. """ virtual_address = self.memory.get_address_int(virtual_address) C = self.get_C() D = self.get_D() min_address = 0 max_address = D * BLOCK_SIZE - 1 if not (min_address <= virtual_address <= max_address): raise BadAddress( u'Virtualus adresas nepriklauso duomenų segmentui.') virtual_block, cell = self.memory.get_address_tuple(virtual_address) block = hex_to_int( self.get_byte(4 + 2 * (C + virtual_block)) + self.get_byte(4 + 2 * (C + virtual_block) + 1)) return block, cell
def convert(proc, *args): u""" Funkcija, kuri konvertuoja komandų argumentus. """ if len(args) != len(self.signature): raise WrongOpCode( u'Netinkamas argumentų kiekis.') clean_args = [] for arg, arg_type in zip(args, self.signature): try: if arg_type == 'a': clean_args.append(hex_to_int(arg)) elif arg_type == 'i': clean_args.append(int(arg)) elif arg_type == '?': clean_args.append(arg) else: raise TypeError( u'Blogas komandos parašas.') except ValueError: raise WrongOpCode( u'Netinkami komandos argumentai.') return fn(proc, *clean_args)
def get_D(self): u""" Gražina duomenų segmento dydį blokais. """ return hex_to_int(self.get_byte(2) + self.get_byte(3))
def get_C(self): u""" Gražina kodo segmento dydį blokais. """ return hex_to_int(self.get_byte(0) + self.get_byte(1))