Sony's libmpeg calls the stream callbacks inside sceMpegDemuxPss / sceMpegDemuxPssRing and Nodata inside
sceMpegGetPicture, on the caller's thread. The native library queued them for whichever thread ran next, which
deadlocks Steambot Chronicles' player (Sony's sample): its video callback waits on the IPU input ring's lock held by
the thread it was stacked on. With the option on (off by default), each callback is a handler bound to the calling
thread and the call is made again after it: the demux going on from where it was, GetPicture calling Nodata again
until a picture is there.
- Kernel::queue(handler, thread): a handler that runs only on that thread; handlerQueuedFor(thread) for the
dispatcher's checks; the bindings saved in an optional section (older states load with none).
- Libraries::queueHandlerOn and restartCall: the free host stops the thread at the call's own address.
- MpegState::demuxResuming / demuxBytes, saved as an optional tail of the mpeg section.
- Tests: thread-bound handlers (handler_stacks), the demux and Nodata loop (mpeg_callbacks).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Off the reference's layout, each stream callback's 32-byte entry came from
the engine's heap, which by default starts after the game's .bss, where a
game's own allocator works. Both handed out the same memory: Amplitude's
task manager list header was overwritten from the movie's first frame.
The entries now come from the last 64 x 32 bytes of the work area the game
gave sceMpegCreate (libmpeg's own memory, never read by the game), freed
as each handler ends. The reference's layout keeps the heap as before. A
save state from before has no in-use mask: it loads as none waiting.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A state restored the cards' contents but not their host folder, so a game's saves after
--load-state stayed in memory and were lost. Cards kept in a folder are now taken back from
the folder when a state loads (a console's memory card is not part of a save state); cards in
memory stay the state's.
Archive::end closed a section before checking its size: a reader trying an older layout (the
libraries' sif section) that failed there rewound into a nameless section and the load failed
further on. The check now comes first. Tests for both.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A loaded state brought the cards' contents back but not their host folder (it
is not saved), so every in-game save made after loading a state stayed in
memory and was lost at exit. With a host folder (a played game), loading a
state now reads the cards again from their folders and keeps them bound, as a
real memory card lives outside the console. Without one (tests, scripted
runs) the state's cards are kept as saved.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
States saved before these sections grew fail to load ("section pad ends before its fields"; then mpeg, sif) and the
game starts from the boot. JC2's states from before 2026-11-24 hit all four:
- pad: libpad2's sockets (pad2Sockets, pad2Next) came after multitapAttached; an older section ends there, and no
socket is open.
- mpeg: inputEnded (MpegOptions::sdkNodata) came before the sound; an older section has exactly the sound left, and
the input goes on.
- sif: the IOP side's software registers came before the heap; the older layout is tried first and kept only when it
reads the section exactly (Archive::end checks it), with the registers cleared; otherwise the reader rewinds.
- snd: a state from before the 989SND driver was saved has "timers" next; the driver stays as it is.
Archive::sectionLeft(): the bytes the open section has left when reading. What is written does not change.
bozo-state-tests (sectionLeft) passes, with the whole engine suite on jc2/native-irx-host (symbol_maps aside: the
build's temp directory has "game" in its path). JC2: s24300_arena.state, saved on the engine before these fields,
loads again and the run goes on in the scene it was saved in.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
13cddc4 (#132) added Libraries::gsVideo to the "libraries" section, ahead
of its "waits" sub-section, so every state saved before it failed to load
("a container count is out of range") and the game started from the boot.
Archive::nextIs(name): whether the next thing read is that section. The
libraries reader takes gsVideo only when "waits" is not next; an older
state leaves it unknown (zero: its fourth byte is always 3 once
ResetGraph set it), and the GS library's SetDefDispEnv and Z buffer
address then read ResetGraph's parameters from scratchpad 0x100, as the
library did when that state was written. What is written does not change.
bozo-state-tests (nextIs) and bozo-libs-tests pass. TTA (engine at
b6a1980 plus this): all 33 regression captures from its saved states are
identical to the baselines; without it none of them loaded.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
5030dbb made sceGsSetDefDispEnv and the Z buffer's address read the display mode back from scratchpad 0x100, where
the reference also leaves ResetGraph's parameters. The game's own code reuses that scratchpad afterwards (TMB from its
fourth frame), so SMODE2 and DISPLAY came out non-interlaced where the reference's are interlaced. TMB's pinned boot
check (tmb-check, boot.clg) diverged there at frame 4; it passes again: 3000 frames, all 4,650,558 records, draw lists
on every checked frame. Libraries::gsVideo keeps them (saved in the states); the scratchpad still gets them.
sceGsExecStoreImage's refusal names its caller.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
SndDriver::serialize saves the driver's banks, handles, slots and replies' state,
then its engine:
- the IOP memory and clocks;
- the mixer (Mixer::serialize) and libsd;
- 989SND's model (snd989::Engine::Impl). Its pointers are saved as data: a byte
of a bank as (bank, chunk, offset), a handler as (table, index), a MIDI as
(bank, index), a stream buffer as its index.
A load makes the engine when the state has one and anchors the real-time clock
again. Libraries::unsaveable no longer refuses for 989SND.
Engine::bankUnloaded destroyed the bank while busy voices still pointed into its
header: the program record that voiceVolPan reads again for SetSoundParams, a
use-after-free. Unloaded banks are kept in `retired` (headers only; 989SND reads
IOP memory, which stays until reused), so those pointers stay valid and can be
saved.
LoadedImages::images / restore let a port save the NGP files it bound.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflicts: libmpeg's Nodata end becomes a Resume record that keeps TTA's SDK callback (a[2] flags:
1 work area, 2 SDK callback; 0 is the reference's behaviour); cdvdman keeps JC2's drive commands and
TTA's sceCdSearchFile. TTA's state the archive did not have: libpad2's sockets, the IOP software
registers and libmpeg's inputEnded.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A state is taken at the top of Dispatcher::run's loop, where no guest code is on
the host stack: memory, the dispatcher, the kernel (threads, frames, queues,
deadlines), every library's state, the native IOP host (memory, tasks, kernel
objects, RPC servers, mixer, libsd) and sections ports add (state::Sections).
Parked library lambdas become data: Resume records and native deadline kinds,
rebuilt from tables on load; ports can add their own kinds. FreeHost serves
requestSave / requestLoad from any thread, rolls back a failed load, and takes
FreeOptions (gameTag, loadState[Frame], saveState[Frame]). Windows get ten slots:
F5 save, F7 load, F6 / Shift+F6 select, and a Save states tab in F2.
989SND's engine and a playing movie are not covered: a save waits, saying why.
docs/save-states.md tells a port how to use and check it; tests/runtime/state_tests.cpp
covers the archive, files and a dispatcher round trip.
Co-Authored-By: Claude Opus 5.5 <[email protected]>