Skip to content

FFI: switch games with load_with_save - #8

Merged
raphamorim merged 1 commit into
mainfrom
switch-games
Oct 23, 2026
Merged

raphamorim merged 1 commit into
mainfrom
switch-games

Conversation

@raphamorim

Copy link
Copy Markdown
Owner

Summary

load_with_save can now be called again to switch games without restarting the host process. Before, it returned 1 once a game was loaded, because the global GAMEBOY was only ever set once.

  • Switching: the running game is replaced. Dropping it writes its .gbsave, as before.
  • Sound follows the game: if enable_audio was called, the core opens a fresh output stream and attaches it to the new game. Hosts don't need to call enable_audio again; calling it is harmless and returns the sample rate. A fresh stream also picks up the current default output device.
  • Rejected cartridges don't stop play: if the new ROM fails to load, the running game is left untouched and -2 plus last_error are returned.
  • API change: status 1 ("already loaded") no longer exists. gameboy.h is updated.

Testing

  • make lint, cargo test --release, the wasm build and the FFI build all pass locally (Rust 1.99.1)
  • A Swift harness linked against libgameboy.a switched Pokémon Silver → Blargg cpu_instrs → Silver:
    • silver.gbsave (32,776 bytes) is written on switch
    • audio queue drains, then refills from the new game without calling enable_audio again
    • a junk cartridge returns -2 / "Unsupported MBC type", and Silver's intro keeps animating
    • side note: cpu_instrs shows "Passed all tests"

load_with_save used to refuse a second game (the global instance was set
once). Calling it again now replaces the running game:

- the previous game is dropped, which writes its .gbsave
- if sound is on, a fresh output stream is opened and attached to the new
  game, so hosts don't need to call enable_audio again
- if the new cartridge is rejected, the running game keeps going

The "already loaded" status code (1) is gone.
@raphamorim
raphamorim merged commit b89e48b into main Oct 23, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant