def do_get_memory(self, args): args = split_args(args, 2, 0) if (args == None): return False memory = self.simulator.memory start = int(args[0], 0) length = int(args[1], 0) # round down to nearest multiple of 8 to keep alignment start -= start % 8 for i in range(length): loc = start + i if (loc % 8 == 0): print(long_hex(loc), end=' ') ending = ' ' if (i % 4 == 3): ending = ' ' if (i % 8 == 7): ending = '\n' value_str = bytes(memory.get(Memory.Byte, loc)).hex() print(value_str, end=ending) print('') # newline
def do_get_registers(self, args): args = split_args(args, 0, 1) if (args == None): return False if (len(args) == 0): output = '' for i, reg in enumerate(Register): value_hex = long_hex(self.simulator.get_register_value(reg)) output += '{:<16}'.format(reg.name + ': ' + value_hex) if (i % 4 == 3): output += '\n' print(output) else: # get the one passed in try: reg = Register[args[0]] value_hex = long_hex(self.simulator.get_register_value(reg)) print(value_hex) except KeyError: print('[ERROR] unrecognized register ' + args[0]) return False
def do_assemble(self, args): args = split_args(args, 1, 1) if(args == None): return False length = len(args) try: in_file = open(args[0]) out_file = open(args[1], 'w') if length == 2 else sys.stdout assembled, issues = assembler.parse(in_file.read(-1)) pretty_json = json.loads(assembled.to_json()) out_file.write(json.dumps(pretty_json, indent=4, sort_keys=True)) if not issues: return if(len(issues) != 0): print('----- ISSUES -----') for issue in issues: print('{}: {}\n'.format(issue[1], issue[0])) in_file.close() if length == 2: out_file.close() except FileNotFoundError as not_found: print('[Error] file: ' + str(not_found) + ' does not exist')
def do_dump_memory(self, args): args = split_args(args, 1, 0) if (args == None): return False out_file = open(args[0], 'wb') self.simulator.save_memory(out_file) out_file.close()
def do_set_register(self, args): args = split_args(args, 2, 0) if (args == None): return False # get the one passed in try: reg = Register[args[0]] value = int(args[1], 0) #let python automatically determine base self.simulator.set_register_value(reg, value) except KeyError: print('[ERROR] unrecognized register ' + args[0]) return False
def do_load_memory(self, args): args = split_args(args, 1, 0) if (args == None): return False try: in_file = open(args[0], 'rb') self.simulator.load_memory(in_file) in_file.close() except FileNotFoundError as not_found: print('[Error] file: ' + str(not_found) + ' does not exist')
def do_set_memory(self, args): args = split_args(args, 3, 0) if (args == None): return False memory = self.simulator.memory start = int(args[0], 0) length = int(args[1], 0) value = args[2] # trim off optional prefix if (value[0:2] == '0x'): value = value[2:] # check lengths if (len(value) % 2 != 0 or len(value) // 2 != length): print('length of value and given length do not match') return False for i in range(length): loc = start + i memory.set(Memory.Byte, loc, bytearray.fromhex(value[i * 2:i * 2 + 2]))
def do_simulate(self, args): args = split_args(args, 0, 1) if(args == None): return False subcommandline_run(None if len(args) == 0 else args[0])