libretro: include SMS FM state in retro_serialize_size - #266
Open
ArjunKdaf wants to merge 1 commit into
Open
Conversation
The YM2413 chunk is only written once a game has accessed the FM port (PMS_HW_FMUSED), so retro_serialize_size() grows mid-session the first time that happens (+321 bytes) — breaking frontends that size buffers from an early query. RetroArch's rewind ring then silently accumulates stale pre-FM snapshots (task_save.c overflow check is DEBUG-logged only), run-ahead permanently self-disables, and netplay disconnects; minarch-family frontends cache the size at load and persist truncated state files. Declaring RETRO_SERIALIZATION_QUIRK_CORE_VARIABLE_SIZE would not help: RetroArch stores the flag and never reads it. Fix like the existing 32X worst-case handling in the same function: force PMS_HW_FMUSED for the size run only, so the reported size is stable from frame one. The trailing slack for games that never touch FM is loaded fine (unknown-chunk skip), as with the 32X padding. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
libretro: include SMS FM state in retro_serialize_size
Problem
For SMS, the YM2413 state chunk is only written once a game has accessed the FM port —
state_save()gates it onPMS_HW_FMUSED(pico/state.c), and that flag is set on the first write to port 0xF0 (pico/sms.c,z80_sms_out). As a resultretro_serialize_size()grows mid-session the first time a game touches FM:Frontends commonly treat the size as stable per session and size long-lived buffers from an early query:
state_manager.c:662). After FM use the per-frame serialize is skipped (overflow check intasks/task_save.c:509-519— logged only under#ifdef DEBUG) but the scratch block is still pushed (state_manager.c:861-863), so the ring silently fills with stale pre-FM snapshots; pressing rewind jumps the game back to the last pre-FM frame with no message.runahead.c:1359-1367); the first failed serialize permanently disables run-ahead for the session (runahead.c:1385-1387,1349-1356).netplay_frontend.c:7190-7228); a failed local snapshot tears the session down (netplay_frontend.c:3801-3805→netplay_disconnect).retro_serialize_size()at content load and don't check theretro_serializereturn: after FM use they persist truncated state files that corrupt on load.Declaring
RETRO_SERIALIZATION_QUIRK_CORE_VARIABLE_SIZEwould not help: RetroArch stores the quirk flags verbatim (runloop.c:3075-3082) andCORE_VARIABLE_SIZEis never read anywhere in the frontend, nor isFRONT_VARIABLE_SIZEever set for the core to detect. A stable size is the only behavior fixed-size consumers actually support.Fix
retro_serialize_size()already runs the full state writer against a byte-counting sink and already reserves worst case for MD/MCD by including 32X. Do the same for SMS: forcePMS_HW_FMUSEDfor the size run only (restoringPico.m.hardwareafter), so the reported size is stable from frame one and always sufficient.For games that never touch FM, the buffer simply carries trailing slack, which
state_load()tolerates the same way it tolerates the 32X padding on MD states (unknown-chunk skip until EOF).Validation
Automated headless harness (loads the core via dlopen, runs frames with input mashing, serializes repeatedly, verifies restores by framebuffer progression):