def test_rom(rom): try: logger.info(rom) window = Window(scale=1) window.enable_title = False pyboy = PyBoy(window, rom) serial_output = "" t = time.time() while not pyboy.tick(): b = pyboy.mb.get_serial() if b != "": serial_output += b # print b, t = time.time() if "Passed" in serial_output: return ("Passed") break elif "Failed" in serial_output: return (serial_output) break if time.time() - t > timeout: return ("Timeout:\n" + serial_output) break print serial_output pyboy.stop(save=False) except Exception as ex: print ex return str(ex)
def main(): rom_file = os.path.join('ROMs', 'mario.gb') state_file = os.path.join('saveStates', 'mario_save') env = None try: logger.info('Starting environment') env = MarioEnv(rom_file, state_file) # env.reset() except Exception as e: logger.error('Failed to start environment') logger.error(e) traceback.print_exc() sys.exit() totals = [] try: for episode in range(3): episode_rewards = 0 obs = env.reset() for step in range(500): # 500 steps max action = basic_policy(obs) obs, reward, done, _ = env.step(action=action) if step % 10 == 0: logger.debug('reward: {}, obs: {}'.format(reward, obs)) episode_rewards += reward if done: break totals.append(episode_rewards) except KeyboardInterrupt: print("Interrupted by keyboard") except Exception as e: logger.error('Failed during run') logger.error(e) traceback.print_exc() logger.info('Totals: {}'.format(totals))
if __name__ == "__main__": ROMdir = "ROMs/" for rom in [ "TestROMs/instr_timing/instr_timing.gb", "TestROMs/mem_timing/mem_timing.gb", "TestROMs/oam_bug/rom_singles/1-lcd_sync.gb", "TestROMs/cpu_instrs/individual/01-special.gb", "TestROMs/cpu_instrs/individual/02-interrupts.gb", "TestROMs/cpu_instrs/individual/03-op sp,hl.gb", "TestROMs/cpu_instrs/individual/04-op r,imm.gb", "TestROMs/cpu_instrs/individual/05-op rp.gb", "TestROMs/cpu_instrs/individual/06-ld r,r.gb", "TestROMs/cpu_instrs/individual/07-jr,jp,call,ret,rst.gb", "TestROMs/cpu_instrs/individual/08-misc instrs.gb", "TestROMs/cpu_instrs/individual/09-op r,r.gb", "TestROMs/cpu_instrs/individual/10-bit ops.gb", "TestROMs/cpu_instrs/individual/11-op a,(hl).gb", ]: try: logger.info(rom) window = Window(scale=1) pyboy = PyBoy(window, rom) while not pyboy.tick(): pass pyboy.stop(save=False) except Exception as ex: print ex time.sleep(1)
def find_increasing_mem_locations(): """ Find memory locations that have values that increase over time """ try: filename = getROM(rom_dir) # Start PyBoy and run loop pyboy = PyBoy(Window(scale=scale), filename, boot_rom) frame = 0 candidate_mem_locs = {} while not pyboy.tick(): if (frame % 10 == 0): logger.info("frame: {}".format(frame)) if frame == 10: pyboy.mb.loadState(save_file) pyboy.sendInput([WindowEvent.PressArrowRight]) elif frame == 50: pyboy.sendInput([WindowEvent.PressButtonA]) elif frame == 52: pyboy.sendInput([WindowEvent.ReleaseButtonA]) if frame == 55: candidate_mem_locs = pyboy.mb.get_mem_array() if frame == 100: pyboy.sendInput([WindowEvent.PressButtonB]) if frame > 20: if (frame % 10 == 0): pyboy.sendInput([WindowEvent.PressButtonA]) elif (frame % 10 == 5): pyboy.sendInput([WindowEvent.ReleaseButtonA]) # if (frame % 20 == 0): # pyboy.window.dump(filename='/dev/shm/mario_{}.bmp'.format(frame), dump_all=False) # # for n in range(40): # # sprite = pyboy.getSprite(n) # # if sprite.is_on_screen(): # # print('Sprite: {} {}'.format(n, sprite.get_attributes())) if (frame > 79) and (frame % 10 == 0): mem_list = pyboy.mb.get_mem_array(bits=16) candidate_mem_locs = filter_mem_locs(candidate_mem_locs, mem_list) logger.info('-- Mem Pass {}: {}'.format( frame, len(candidate_mem_locs))) for k in candidate_mem_locs: logger.info('{}: {}'.format(hex(k), hex(candidate_mem_locs[k]))) if frame == 500: pyboy.stop() frame += 1 pyboy.stop() except KeyboardInterrupt: print("Interrupted by keyboard") except Exception as ex: traceback.print_exc()