Merge pull request #133 from TheBozos/claude/state-before-spu2-irq

Save states: read the mixer and libsd sections written before the SPU2 interrupt
This commit is contained in:
boubou666 authored and GitHub committed 2026-11-24 02:07:42 +01:00
commit b6a19809dd
5 files changed
+93 -7

No files matched your search

+49 -7
View File
@@ -9,9 +9,47 @@ namespace bozo::audio
using state::fields;
using state::pod;
// A section read in the current layout, else (a state written before the field was added: the section's size check
// fails) again from the same place in the older one.
template <class Walk> void withOlderLayout(state::Archive &a, Walk walk, auto resetNewer)
{
if (!a.loading())
return walk(true);
const state::Archive::Mark mark = a.mark();
try
{
walk(true);
}
catch (const state::Error &)
{
a.rewind(mark);
resetNewer();
walk(false);
}
}
void Mixer::serialize(state::Archive &a, const uint8_t *inputBase, size_t inputBytes)
{
std::scoped_lock lock(m_mutex, m_queueMutex, m_digestMutex);
// A state written before the SPU2 interrupt (3b79730) has no IRQ address or enable: the cores' stay off.
withOlderLayout(a, [&](bool irq) { walk(a, inputBase, inputBytes, irq); },
[&]
{
for (Group &g : m_groups)
{
g.irqAddress = 0;
g.irqEnabled = false;
}
});
if (a.loading())
{
// What was mixed for the host belongs to the old state.
m_queueRead = m_queueFill = 0;
}
}
void Mixer::walk(state::Archive &a, const uint8_t *inputBase, size_t inputBytes, bool irq)
{
a.begin("mixer");
pod(a, m_stats);
fields(a, m_clocked, m_clockStarted, m_samplesMade, m_memory, m_digestHash, m_digestCount);
@@ -19,7 +57,9 @@ namespace bozo::audio
{
for (Voice &v : g.voices)
pod(a, v);
fields(a, g.ended, g.dry, g.wet, g.mix, g.irqAddress, g.irqEnabled);
fields(a, g.ended, g.dry, g.wet, g.mix);
if (irq)
fields(a, g.irqAddress, g.irqEnabled);
for (Volume &v : g.master)
pod(a, v);
for (Volume &v : g.effect)
@@ -42,14 +82,15 @@ namespace bozo::audio
}
}
a.end();
if (a.loading())
{
// What was mixed for the host belongs to the old state.
m_queueRead = m_queueFill = 0;
}
}
void Libsd::serialize(state::Archive &a)
{
// A state written before the SPU2 interrupt (3b79730) has no interrupt handler.
withOlderLayout(a, [&](bool handler) { walk(a, handler); }, [&] { m_state.spu2Handler = Callback{}; });
}
void Libsd::walk(state::Archive &a, bool handler)
{
a.begin("libsd");
State &s = m_state;
@@ -59,7 +100,8 @@ namespace bozo::audio
pod(a, c);
for (Callback &c : s.transHandler)
pod(a, c);
pod(a, s.spu2Handler);
if (handler)
pod(a, s.spu2Handler);
for (auto &e : s.effect)
pod(a, e);
a.end();
+1
View File
@@ -101,6 +101,7 @@ namespace bozo::audio
void serialize(state::Archive &a); // the settings and callbacks (audio_state.cpp)
private:
void walk(state::Archive &a, bool handler); // serialize, in one layout
void sync();
uint32_t batchOne(uint32_t function, uint32_t entry, uint32_t value);
void transferDone(uint32_t channel);
+1
View File
@@ -156,6 +156,7 @@ namespace bozo::audio
void serialize(state::Archive &a, const uint8_t *inputBase, size_t inputBytes);
private:
void walk(state::Archive &a, const uint8_t *inputBase, size_t inputBytes, bool irq); // serialize, in one layout
struct Volume
{
uint16_t setting = 0;
+12
View File
@@ -40,6 +40,18 @@ namespace bozo::state
// fields took exactly the bytes written.
void begin(const char *name);
void end();
// A place to read again from: a reader whose section turned out to be in an older layout goes back to the mark
// and reads it the old way (the sections' size checks tell the layouts apart).
struct Mark
{
size_t pos = 0, depth = 0;
};
Mark mark() const { return {pos_, open_.size()}; }
void rewind(const Mark &m)
{
pos_ = m.pos;
open_.resize(m.depth);
}
const std::vector<uint8_t> &data() const { return data_; }
bool atEnd() const { return pos_ == data_.size(); }
+30
View File
@@ -119,6 +119,36 @@ namespace
x.raw(big.data(), big.size());
}),
"reading past a section");
// An older layout (a field added since): the newer reader fails inside the section, the mark takes it back to
// read the old way, and what follows the section reads on.
{
state::Archive old;
uint32_t one = 1, two = 2, after = 3;
old.begin("s");
state::fields(old, one, two);
old.end();
old.begin("next");
state::fields(old, after);
old.end();
state::Archive x(old.data());
const state::Archive::Mark mark = x.mark();
uint32_t r1 = 0, rNew = 0, r2 = 0, rAfter = 0;
check(throws([&] {
x.begin("s");
state::fields(x, r1, rNew, r2);
x.end();
}),
"a newer layout read from an older section fails");
x.rewind(mark);
x.begin("s");
state::fields(x, r1, r2);
x.end();
x.begin("next");
state::fields(x, rAfter);
x.end();
check(r1 == 1 && r2 == 2 && rAfter == 3 && x.atEnd(), "rewound to the mark, read in the older layout");
}
}
void files()