diff --git a/runtime/include/bozo/state.h b/runtime/include/bozo/state.h index 36f36b0..5fc0fdc 100644 --- a/runtime/include/bozo/state.h +++ b/runtime/include/bozo/state.h @@ -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; diff --git a/runtime/libs/libraries_state.cpp b/runtime/libs/libraries_state.cpp index 2c52c90..abbbfad 100644 --- a/runtime/libs/libraries_state.cpp +++ b/runtime/libs/libraries_state.cpp @@ -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"); @@ -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; diff --git a/tests/runtime/state_tests.cpp b/tests/runtime/state_tests.cpp index 1bd069a..65c2639 100644 --- a/tests/runtime/state_tests.cpp +++ b/tests/runtime/state_tests.cpp @@ -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"); } }