def _handle_events(self, events): # This feeds events into the tick-loop from the window. There might already be events in the list from the API. events = self.plugin_manager.handle_events(events) for event in events: if event == WindowEvent.QUIT: self.quitting = True elif event == WindowEvent.RELEASE_SPEED_UP: # Switch between unlimited and 1x real-time emulation speed self.target_emulationspeed = int(bool(self.target_emulationspeed) ^ True) logger.info("Speed limit: %s" % self.target_emulationspeed) elif event == WindowEvent.STATE_SAVE: with open(self.gamerom_file + ".state", "wb") as f: self.mb.save_state(IntIOWrapper(f)) elif event == WindowEvent.STATE_LOAD: state_path = self.gamerom_file + ".state" if not os.path.isfile(state_path): logger.error(f"State file not found: {state_path}") continue with open(state_path, "rb") as f: self.mb.load_state(IntIOWrapper(f)) elif event == WindowEvent.PASS: pass # Used in place of None in Cython, when key isn't mapped to anything elif event == WindowEvent.PAUSE_TOGGLE: if self.paused: self._unpause() else: self._pause() elif event == WindowEvent.PAUSE: self._pause() elif event == WindowEvent.UNPAUSE: self._unpause() elif event == WindowEvent._INTERNAL_RENDERER_FLUSH: self.plugin_manager._post_tick_windows() else: self.mb.buttonevent(event)
def save_state(self, file_like_object): """ Saves the complete state of the emulator. It can be called at any time, and enable you to revert any progress in a game. You can either save it to a file, or in-memory. The following two examples will provide the file handle in each case. Remember to `seek` the in-memory buffer to the beginning before calling `PyBoy.load_state`: # Save to file file_like_object = open("state_file.state", "wb") # Save to memory import io file_like_object = io.BytesIO() file_like_object.seek(0) Args: file_like_object (io.BufferedIOBase): A file-like object for which to write the emulator state. """ if isinstance(file_like_object, str): raise Exception( "String not allowed. Did you specify a filepath instead of a file-like object?" ) self.mb.save_state(IntIOWrapper(file_like_object))
def __init__(self, filename, rombanks, external_ram_count, carttype, sram, battery, rtc_enabled): self.filename = filename + ".ram" self.rombanks = rombanks self.carttype = carttype self.battery = battery self.rtc_enabled = rtc_enabled if self.rtc_enabled: self.rtc = RTC(filename) self.rambank_initialized = False self.external_rom_count = len(rombanks) self.external_ram_count = external_ram_count self.init_rambanks(external_ram_count) self.gamename = self.getgamename(rombanks) self.memorymodel = 0 self.rambank_enabled = False self.rambank_selected = 0 self.rombank_selected = 1 if not os.path.exists(self.filename): logger.info("No RAM file found. Skipping.") else: with open(self.filename, "rb") as f: self.load_ram(IntIOWrapper(f))
def runCPUTest(rom_path, rom_name): print("Generating expected for " + rom_name) rom_file = os.path.join(rom_path, rom_name) pyboy = PyBoy(rom_file, bootrom_file=None, disable_renderer=False, sound=False) pyboy.set_emulation_speed(0) state_file = os.path.join(dirname, "generated", rom_name + ".state") if os.path.exists(state_file): os.remove(state_file) cpu_state = IntIOWrapper(open(state_file, "wb")) while not pyboy.tick(cpu_state): pass pyboy.stop() print("Finished")
def __init__(self, filename): self.filename = filename + ".rtc" if not os.path.exists(self.filename): logger.info("No RTC file found. Skipping.") else: with open(self.filename, "rb") as f: self.load_state(IntIOWrapper(f), STATE_VERSION) self.latch_enabled = False self.timezero = time.time() self.sec_latch = 0 self.min_latch = 0 self.hour_latch = 0 self.day_latch_low = 0 self.day_latch_high = 0 self.day_carry = 0 self.halt = 0
def load_state(self, file_like_object): """ Restores the complete state of the emulator. It can be called at any time, and enable you to revert any progress in a game. You can either load it from a file, or from memory. See `PyBoy.save_state` for how to save the state, before you can load it here. To load a file, remember to load it as bytes: # Load file file_like_object = open("state_file.state", "rb") Args: file_like_object (io.BufferedIOBase): A file-like object for which to read the emulator state. """ if isinstance(file_like_object, str): raise Exception("String not allowed. Did you specify a filepath instead of a file-like object?") self.mb.load_state(IntIOWrapper(file_like_object))
def stop(self): with open(self.filename, "wb") as f: self.save_ram(IntIOWrapper(f)) if self.rtc_enabled: self.rtc.stop()
def stop(self): with open(self.filename, "wb") as f: self.save_state(IntIOWrapper(f))