Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions runtime/include/bozo/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ namespace bozo::state
Mark mark() const { return {pos_, open_.size()}; }
// Reading: whether the next thing is the section `name` (an older layout that has no fields before it).
bool nextIs(const char *name) const;
// Reading: the bytes the open section has left (an older layout, shorter by the fields added since); 0 when
// writing or with no section open.
size_t sectionLeft() const
{
return loading_ && !open_.empty() && pos_ < open_.back().second ? open_.back().second - pos_ : 0;
}
void rewind(const Mark &m)
{
pos_ = m.pos;
Expand Down
50 changes: 44 additions & 6 deletions runtime/libs/libraries_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ namespace bozo::libs
for (auto &port : pad.ports)
for (auto &slot : port)
pod(a, slot);
fields(a, pad.initialized, pad.reads, pad.socketReads, pad.multitap, pad.multitapAttached, pad.pad2Sockets, pad.pad2Next);
fields(a, pad.initialized, pad.reads, pad.socketReads, pad.multitap, pad.multitapAttached);
// A state written before libpad2's sockets were saved (67c5867) ends here: no socket is open.
if (a.loading() && a.sectionLeft() == 0)
{
pad.pad2Sockets.clear();
pad.pad2Next = 0;
}
else
fields(a, pad.pad2Sockets, pad.pad2Next);
a.end();

a.begin("mc");
Expand Down Expand Up @@ -184,16 +192,46 @@ namespace bozo::libs
m.pending, m.pendingAt, m.ring, m.ringSize, m.ringBias, m.linearBase, m.programEnd, m.aborted, m.es,
m.scanned, m.starts, m.pictureStarts, m.sequenceEnd, m.headerOpen, m.cuts, m.sent, m.flushed,
m.callbacksDue, m.waiting, m.delivered, m.base, m.lastShown, m.firstPictureNs, m.soundHeader,
m.soundCarry, m.inputEnded);
m.soundCarry);
// A state written before the SDK's Nodata end (MpegOptions::sdkNodata) has the sound left: the input goes on.
if (a.loading() && a.sectionLeft() == sizeof m.sound)
m.inputEnded = false;
else
fields(a, m.inputEnded);
pod(a, m.sound);
a.end();

a.begin("sif");
fields(a, sif.rpcId, sif.bindAttempts, sif.bound, sif.sreg, sif.dmaId, sif.modules, sif.iopSreg);
sif.heap.serialize(a);
a.end();
fields(a, sif.rpcId, sif.bindAttempts, sif.bound, sif.sreg, sif.dmaId, sif.modules);
{
// A state written before the IOP side's software registers has the heap next: read it as such when the
// registers then leave bytes over (the section's end checks the layout), with the registers cleared.
const state::Archive::Mark mark = a.mark();
bool older = false;
if (a.loading())
try
{
sif.heap.serialize(a);
a.end();
older = true;
}
catch (const state::Error &)
{
a.rewind(mark);
}
if (older)
std::fill(std::begin(sif.iopSreg), std::end(sif.iopSreg), 0u);
else
{
fields(a, sif.iopSreg);
sif.heap.serialize(a);
a.end();
}
}

snd.serialize(a, *this);
// A state written before the 989SND driver was saved goes on with the timers: the driver stays as it is.
if (!(a.loading() && a.nextIs("timers")))
snd.serialize(a, *this);

a.begin("timers");
TimerState &t = timers;
Expand Down
8 changes: 8 additions & 0 deletions tests/runtime/state_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ namespace
check(y.nextIs("s") && !y.nextIs("next") && !y.nextIs("ss"), "nextIs names the coming section");
y.begin("s");
check(!y.nextIs("next"), "nextIs: fields come first");
uint32_t z1 = 0, z2 = 0;
check(y.sectionLeft() == 8, "sectionLeft: the section's fields");
state::fields(y, z1);
check(y.sectionLeft() == 4, "sectionLeft: a field is left");
state::fields(y, z2);
check(y.sectionLeft() == 0 && z2 == 2, "sectionLeft: the section's fields are all read");
y.end();
check(y.sectionLeft() == 0, "sectionLeft: no section open");
}
}

Expand Down
Loading