def main() -> int: parser = argparse.ArgumentParser() parser.add_argument('elf') parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument( '--dump-dmem', metavar="FILE", type=argparse.FileType('wb'), help=("after execution, write the data memory contents to this file. " "Use '-' to write to STDOUT.")) parser.add_argument( '--dump-regs', metavar="FILE", type=argparse.FileType('w'), help=("after execution, write the GPR and WDR contents to this file. " "Use '-' to write to STDOUT.")) parser.add_argument( '--dump-stats', metavar="FILE", type=argparse.FileType('w'), help=("after execution, write execution statistics to this file. " "Use '-' to write to STDOUT.")) args = parser.parse_args() collect_stats = args.dump_stats is not None sim = OTBNSim() exp_end_addr = load_elf(sim, args.elf) sim.state.ext_regs.write('START_ADDR', 0, False) sim.state.ext_regs.commit() sim.start() sim.run(verbose=args.verbose, collect_stats=collect_stats) if exp_end_addr is not None: if sim.state.pc != exp_end_addr: print('Run stopped at PC {:#x}, but _expected_end_addr was {:#x}.'. format(sim.state.pc, exp_end_addr), file=sys.stderr) return 1 if args.dump_dmem is not None: args.dump_dmem.write(sim.dump_data()) if args.dump_regs is not None: for idx, value in enumerate(sim.state.gprs.peek_unsigned_values()): args.dump_regs.write(' x{:<2} = 0x{:08x}\n'.format(idx, value)) for idx, value in enumerate(sim.state.wdrs.peek_unsigned_values()): args.dump_regs.write(' w{:<2} = 0x{:064x}\n'.format(idx, value)) if collect_stats: assert sim.stats is not None stat_analyzer = ExecutionStatAnalyzer(sim.stats, args.elf) args.dump_stats.write(stat_analyzer.dump()) return 0
def on_start(sim: OTBNSim, args: List[str]) -> Optional[OTBNSim]: '''Jump to an address given as the (only) argument and start running''' check_arg_count('start', 0, args) print('START') sim.state.ext_regs.commit() sim.start(collect_stats=False) return None
def on_start(sim: OTBNSim, args: List[str]) -> Optional[OTBNSim]: '''Jump to an address given as the (only) argument and start running''' if len(args) != 0: raise ValueError('start expects zero arguments. Got {}.'.format(args)) print('START') sim.state.ext_regs.commit() sim.start(collect_stats=False) return None
def on_start(sim: OTBNSim, args: List[str]) -> None: '''Jump to an address given as the (only) argument and start running''' if len(args) != 1: raise ValueError('start expects exactly 1 argument. Got {}.' .format(args)) addr = read_word('addr', args[0], 32) if addr & 3: raise ValueError('start address must be word-aligned. Got {:#08x}.' .format(addr)) print('START {:#08x}'.format(addr)) sim.state.ext_regs.write('START_ADDR', addr, False) sim.state.ext_regs.commit() sim.start()
def prepare_sim_for_asm_file(asm_file: str, tmpdir: py.path.local, start_addr: int = 0) -> OTBNSim: '''Set up the simulation of a single assembly file. The returned simulation is ready to be run through the run() method. ''' assert os.path.exists(asm_file) elf_file = asm_and_link_one_file(asm_file, tmpdir) sim = OTBNSim() load_elf(sim, elf_file) sim.state.ext_regs.write('START_ADDR', start_addr, False) sim.state.ext_regs.commit() sim.start() return sim