Files
Thomas MartinandClaude Opus 5.5 fd67f9e899 libmpeg: callbacks on the caller's thread (MpegOptions::callbacksOnCaller)
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]>
2026-11-29 13:39:05 +01:00

863 lines
35 KiB
C++

// The free host. See bozo/host/free_host.h.
#include "bozo/host/free_host.h"
#include "bozo/app/determinism.h"
#include "bozo/hash.h"
#include "bozo/ops_mem.h"
#include "bozo/render/renderer.h"
#include "elf.h"
#include <chrono>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <format>
#include <fstream>
#include <filesystem>
#include <iterator>
namespace bozo::host
{
namespace
{
uint32_t gpr(const R5900Context *ctx, int n) { return ctx->r[n].uw[0]; }
}
FreeHost::FreeHost(const FreeOptions &options) : opt_(options) {}
FreeHost::~FreeHost() { stopSoundWatcher(); }
bool FreeHost::setUp(ee::Dispatcher &d, const BozoExternalEntry *externals, size_t count, const port::Registry *registry,
std::string &error)
{
d_ = &d;
instructionHandler_ = registry ? registry->instructionHandler() : nullptr;
if (registry)
{
vu0Programs_ = registry->vu0Programs();
vu0ByPair_.clear();
for (const BozoFunctionEntry &e : vu0Programs_)
if (e.address % 8u == 0 && e.address < kVu0TableBytes)
{
if (vu0ByPair_.empty())
vu0ByPair_.assign(kVu0TableBytes / 8u, nullptr);
if (!vu0ByPair_[e.address / 8u])
vu0ByPair_[e.address / 8u] = e.function;
}
vu0Code_ = registry->vu0CodeMemory();
transfersAlwaysDone_ = registry->transfersAreAlwaysDone();
d.setSyncVAtVblank(registry->syncVCallbackIsAtVblank());
}
if (nativeGraphics())
std::fprintf(stderr, "[graphics] native submission/completion; retired graphics registers and DMA polls are guarded\n");
// Stops jump back to the dispatcher (nothing on the way holds anything to destroy: dispatcher.h), and no frame
// keeps the context words only the call-log host compares.
d.jumpStops = true;
d.keepStopWords = false;
d.kernel().setObserver(this);
d.runtime().port = this;
libs_.attach(d);
libs_.printOutput = opt_.printGameOutput;
libs_.sndNative = true;
// Free mode's IOP time (libraries.md 15.13c): the IOP reset where the game makes it, nothing pinned.
libs_.snd.freeTiming = true;
libs_.snd.timing = libs::snd989::Timing::freeRun(0);
// In real time the sound is played as it is made: the IOP's clock follows the host's (15.13e).
libs_.snd.realTimeClock = !libs_.audioProvider && opt_.realTime && opt_.sound;
if (opt_.realTime)
libs_.timers.hostClock = [] {
return static_cast<int64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count());
};
libs_.cd.imagePath = opt_.iso;
libs_.mc.hostDirectory = opt_.cardDir;
for (int player = 0; player < 4; ++player)
{
const std::string &path = player == 0 ? opt_.padScript : opt_.playerScripts[static_cast<size_t>(player - 1)];
if (path.empty())
continue;
std::ifstream in(path, std::ios::binary);
const std::string text((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
if (!in || !padScript_.load(player, text, error))
{
error = "pad script " + path + ": " + (error.empty() ? "cannot read it" : error);
return false;
}
libs_.pad.input = &padScript_;
}
ElfFile elf;
if (!elf.load(opt_.elf))
{
error = opt_.elf + ": " + elf.error();
return false;
}
if (!opt_.symbolMap.empty() && !elf.loadSymbolMap(opt_.symbolMap))
{
error = opt_.elf + ": " + elf.error();
return false;
}
// Guest memory as the reference's boot leaves it (the call logs' INIT): the ELF's segments, and two words of
// the boot's own at 0x2f0 and 0x2f8.
uint8_t *ram = d.runtime().memory.ram();
const std::vector<uint8_t> &image = elf.image();
if (elf.imageBase() + image.size() > kRamSize)
{
error = "the ELF's image does not fit in guest RAM";
return false;
}
std::memcpy(ram + elf.imageBase(), image.data(), image.size());
const uint32_t bootWords[2] = {0x01800000u, 0x00001F80u};
std::memcpy(ram + 0x2F0, &bootWords[0], 4);
std::memcpy(ram + 0x2F8, &bootWords[1], 4);
for (const ElfSymbol &s : elf.symbols())
if (s.type == 2 && s.value != 0 && !s.name.empty())
names_.emplace(s.value, s.name);
for (size_t i = 0; i < count; ++i)
{
const uint32_t a = externals[i].address;
if (a == 0)
continue;
if (registry)
if (port::Function fn = registry->find(a))
{
portAt_[a] = fn;
d.bindLibrary(a);
continue;
}
auto n = names_.find(a);
const std::string name = n != names_.end() ? n->second : std::string();
if (const ee::KernelCall k = ee::kernelCallByName(name); k != ee::KernelCall::None)
{
d.bindKernelCall(a, k);
if (name.find("FlushCache") != std::string::npos)
flushCacheAt_.insert(a);
continue;
}
// A library BozoEngine implements; anything else (an override address the port registered nothing at, a
// stub nothing native answers) stays the game's own code there.
if (const libs::Function *f = libs_.find(name))
{
d.bindLibrary(a);
nativeAt_[a] = f;
}
}
uint32_t entry = 0;
std::memcpy(&entry, elf.bytes().data() + 24, 4);
d.boot(entry, 0x1FFFFF0u);
d.kernel().useOwnedHandlerStacks();
d.safePoint = [this] { atSafePoint(); };
if (!opt_.out.empty())
{
reference::Header h;
h.elfHash = hash::xxh64(elf.bytes().data(), elf.bytes().size(), 0);
h.run = "free";
h.build = "BozoEngine free host";
h.elfName = std::filesystem::path(opt_.elf).filename().string();
writing_ = writer_.open(opt_.out, h);
}
// The draw lists' digest is the record's `draws`: not computed when no record is written.
render::Renderer::instance().setDrawDigest(writing_);
return true;
}
uint64_t FreeHost::run()
{
if (!opt_.loadState.empty() && !opt_.loadStateFrame)
requestLoad(opt_.loadState);
startSoundWatcher();
try
{
d_->run();
}
catch (const ee::EndRun &)
{
}
stopSoundWatcher();
if (writing_)
writer_.close();
return stats_.frames;
}
void FreeHost::fault(ee::Dispatcher &, R5900Context *ctx, const std::string &what)
{
const auto *thread=d_ ? d_->currentThread() : nullptr;
for(int id=1;id<16;++id) if(const auto *t=d_->kernel().thread(id))
std::fprintf(stderr,"[native:fault] thread %d state=%u wait=%u object=%u entry=%x pc=%x\n",id,unsigned(t->state),t->waitReason,t->waitObject,t->entry,t->frames.empty()?0:t->frames.front()->ctx.pc);
const uint32_t root=thread && !thread->frames.empty() ? thread->frames.front()->ctx.pc : 0;
// BOZO_FAULT_REGISTERS=1: the faulting context's 128-bit GPRs (the pc a fault reports is the last one the
// generated code set: a call's return point, a branch target), to find the access that faulted.
if (ctx && std::getenv("BOZO_FAULT_REGISTERS"))
for (int r = 0; r < 32; ++r)
std::fprintf(stderr, "[native:fault] r%-2d %016llx%016llx%s", r, (unsigned long long)ctx->r[r].ud[1],
(unsigned long long)ctx->r[r].ud[0], r % 2 ? "\n" : " ");
endReason_ = std::format("FAULT: {} (frame {}, pc 0x{:x}, root 0x{:x} {}, clock {}, last frame {})", what, stats_.frames,
ctx ? ctx->pc : 0,root,functionAt(root),d_?d_->now(ctx):0,lastFrameCycle_);
throw ee::EndRun{};
}
// ---- Out-calls ---------------------------------------------------------------------------------------------
void FreeHost::runOverride(R5900Context *ctx, uint32_t target, port::Function fn)
{
++stats_.overrideCalls;
++perOverride_[target];
const uint32_t ra = gpr(ctx, 31);
const ee::Thread *self = d_->currentThread();
const int32_t selfId = self ? self->id : 0;
const bool outer = insideDepth_ == 0;
++insideDepth_;
d_->beginOutcall(ctx);
ctx->pc = target;
fn(d_->runtime().memory.flat(), ctx, &d_->runtime());
d_->endOutcall(ctx);
--insideDepth_;
if (!outer)
return; // a stop deferred inside reaches the outermost override
uint32_t pc = ctx->pc;
const bool deferred = d_->takeDeferredStop(ctx, pc);
d_->markOutcallEnd(ctx);
// It leaves to the dispatcher when it stopped, when its thread no longer runs, or when it goes on elsewhere than
// its return address (abi.md section 3: the dispatcher runs the thread on from there).
if (deferred || d_->kernel().running() != selfId || pc != ra)
d_->stop(ctx, pc);
}
void FreeHost::libraryCall(ee::Dispatcher &, R5900Context *ctx, uint32_t target)
{
watchdog(ctx);
d_->runtime().tailCall = false;
if (auto p = portAt_.find(target); p != portAt_.end())
{
runOverride(ctx, target, p->second);
return;
}
auto n = nativeAt_.find(target);
if (n == nativeAt_.end())
{
auto name = names_.find(target);
++stats_.unanswered[name != names_.end() ? name->second : std::format("0x{:x}", target)];
fault(*d_, ctx, std::format("a call to 0x{:x} ({}) has no native answer", target,
name != names_.end() ? name->second : "no symbol"));
return;
}
++stats_.libraryCalls;
const libs::Function &f = *n->second;
++stats_.libraryByName[f.name];
const ee::Thread *self = d_->currentThread();
const int32_t selfId = self ? self->id : 0;
const uint32_t ra = gpr(ctx, 31);
libs_.pin = libs::Pin{};
libs::Guest g(d_->runtime(), ctx);
try
{
f.handler(libs_, g);
}
catch (const libs::BadAccess &b)
{
fault(*d_, ctx, std::format("native {}: an access to 0x{:x} ({} bytes) outside guest memory", f.name, b.address, b.bytes));
}
if (g.cycles)
d_->setNow(ctx, d_->now(ctx) + g.cycles);
if (f.frameBoundary)
endFrame(ctx);
// The call is made again after its thread's handlers (Libraries::restartCall): the thread stops at the call.
if (libs_.takeRestartCall())
{
if (insideDepth_)
d_->leave(ctx, target);
else
{
d_->markOutcallEnd(ctx);
d_->stop(ctx, target);
}
return;
}
const bool blocked = d_->kernel().running() != selfId;
if (insideDepth_)
{
// Inside an override: guest code leaves it right there, the thread going on after the call once woken
// (kernel.md section 3b).
if (blocked)
d_->leave(ctx, ra);
return;
}
d_->markOutcallEnd(ctx);
if (blocked)
d_->stop(ctx, ra);
}
void FreeHost::vu0Microprogram(ee::Dispatcher &, R5900Context *ctx, uint32_t address)
{
++stats_.vu0Calls;
if (address % 8u == 0 && address < kVu0TableBytes && !vu0ByPair_.empty())
if (const RecompiledFunction f = vu0ByPair_[address / 8u])
{
f(d_->runtime().memory.ram(), ctx, &d_->runtime());
return;
}
if (!vu0Programs_.empty())
{
for (const BozoFunctionEntry &e : vu0Programs_)
if (e.address == address)
{
e.function(d_->runtime().memory.ram(), ctx, &d_->runtime());
return;
}
fault(*d_, ctx, std::format("VU0 microprogram 0x{:x}: no native program of the port's", address));
return;
}
const libs::Function *f = libs_.vu0Program(address);
if (!f)
{
fault(*d_, ctx, std::format("VU0 microprogram 0x{:x}: no native program", address));
return;
}
libs::Guest g(d_->runtime(), ctx);
f->handler(libs_, g);
}
void FreeHost::ioRead(ee::Dispatcher &, R5900Context *ctx, uint32_t address, uint32_t size, uint64_t value[2])
{
if (nativeGraphics() && libs::graphicsIo(address))
fault(*d_, ctx, std::format("native graphics reached a retired register read at 0x{:x}", address));
if (vu0Code_ && (address & 0x1FFFFFFFu) >= 0x11000000u && (address & 0x1FFFFFFFu) < 0x11004000u && size <= 16)
{
// VU0's code memory (port::Registry::vu0CodeMemory): its 4 KB, mirrored.
value[0] = value[1] = 0;
std::memcpy(value, vu0Code_ + (address & 0xFF0u) + (address & 0xFu & ~(size - 1u)), size);
return;
}
++stats_.ioAccesses;
if (opt_.profileIo) ++stats_.ioSites[{ctx->pc, address}][0];
watchdog(ctx);
value[0] = value[1] = 0;
if (!libs::ioKnown(address, size, false))
{
++stats_.ioUnknown;
fault(*d_, ctx, std::format("unhandled device read at 0x{:x} ({} bytes), pc 0x{:x}", address, size, ctx->pc));
return;
}
libs_.ioAccess = libs::IoAccess{address, size, false, 0, d_->timerPending()};
libs::Guest g(d_->runtime(), ctx);
libs::Libraries::ioFunction().handler(libs_, g);
d_->setTimerPending(libs_.ioAccess.timerPending);
value[0] = libs_.ioAccess.value;
}
void FreeHost::ioWrite(ee::Dispatcher &, R5900Context *ctx, uint32_t address, uint32_t size, const uint64_t value[2])
{
if (nativeGraphics() && libs::graphicsIo(address))
fault(*d_, ctx, std::format("native graphics reached a retired register write at 0x{:x}", address));
if (vu0Code_ && (address & 0x1FFFFFFFu) >= 0x11000000u && (address & 0x1FFFFFFFu) < 0x11004000u && size <= 16)
{
std::memcpy(vu0Code_ + (address & 0xFF0u) + (address & 0xFu & ~(size - 1u)), value, size);
return;
}
++stats_.ioAccesses;
if (opt_.profileIo) ++stats_.ioSites[{ctx->pc, address}][1];
if (!libs::ioKnown(address, size, true))
{
++stats_.ioUnknown;
fault(*d_, ctx, std::format("unhandled device write at 0x{:x} ({} bytes), pc 0x{:x}", address, size, ctx->pc));
return;
}
libs_.ioAccess = libs::IoAccess{address, size, true, value[0], d_->timerPending(), ctx->pc};
libs::Guest g(d_->runtime(), ctx);
libs::Libraries::ioFunction().handler(libs_, g);
d_->setTimerPending(libs_.ioAccess.timerPending);
}
bool FreeHost::cop0Condition(ee::Dispatcher &, R5900Context *ctx)
{
if (nativeGraphics())
fault(*d_, ctx, "native graphics reached the retired DMA completion condition");
watchdog(ctx);
if (opt_.profileIo) ++stats_.ioSites[{ctx->pc, kCop0ConditionSite}][0];
return transfersAlwaysDone_ || libs_.io.cpcond0();
}
void FreeHost::watchdog(R5900Context *ctx)
{
if (stopRequested_.load(std::memory_order_relaxed))
{
endReason_ = "stopped";
throw ee::EndRun{};
}
if (soundDue_.load(std::memory_order_relaxed))
stallSound(ctx);
if (opt_.advanceLibrariesWhileStalled && opt_.sound && libs_.audioProvider)
{
const uint64_t at = ctx ? d_->now(ctx) : d_->now(), quarter = d_->kernel().field() / 4;
if (at - lastFrameCycle_ > d_->kernel().field() && at - lastStallAdvance_ >= quarter)
{
lastStallAdvance_ = at;
libs_.audioProvider(at);
}
}
if (!opt_.stallFields)
return;
const uint64_t now = d_->now(ctx);
if (now - lastFrameCycle_ > static_cast<uint64_t>(opt_.stallFields) * d_->kernel().field())
fault(*d_, ctx, std::format("no game frame ended for {} fields: the main loop waits at 0x{:x} ({}), ra 0x{:x} ({})",
opt_.stallFields, ctx ? ctx->pc : 0, ctx ? functionAt(ctx->pc) : "?",
ctx ? ctx->r[31].uw[0] : 0, ctx ? functionAt(ctx->r[31].uw[0]) : "?"));
}
void FreeHost::kernelCallBegin(ee::Dispatcher &, R5900Context *ctx, uint32_t, uint32_t kind)
{
const ee::KernelCall call = kind == 3 ? ee::kernelCallBySyscall(ctx->r[3].sw[0]) : ee::KernelCall::None;
flushCacheSyscall_ = call == ee::KernelCall::FlushCache || call == ee::KernelCall::iFlushCache;
}
void FreeHost::kernelCallEnd(ee::Dispatcher &, R5900Context *, uint32_t target, bool)
{
++stats_.kernelCalls;
if (!nativeGraphics() && (flushCacheAt_.count(target) || flushCacheSyscall_))
libs_.io.flushCache();
flushCacheSyscall_ = false;
}
bool FreeHost::librarySyscall(ee::Dispatcher &, R5900Context *ctx, const char *name)
{
const libs::Function *f = libs_.find(name);
if (!f)
return false;
++stats_.libraryCalls;
++stats_.libraryByName[f->name];
libs_.pin = libs::Pin{};
libs::Guest g(d_->runtime(), ctx);
try
{
f->handler(libs_, g);
}
catch (const libs::BadAccess &b)
{
fault(*d_, ctx, std::format("native {}: an access to 0x{:x} ({} bytes) outside guest memory", f->name, b.address, b.bytes));
}
if (g.cycles)
d_->setNow(ctx, d_->now(ctx) + g.cycles);
return true;
}
bool FreeHost::dmacStatusSet(uint32_t cause)
{
if (nativeGraphics() && cause < 3) return libs_.io.nativeCompleted[cause];
return cause < 10 && (libs_.io.stat & (1u << cause)) != 0;
}
void FreeHost::idle(ee::Dispatcher &, uint64_t from, uint64_t to)
{
watchdog(nullptr);
if (const int64_t hold = libs_.timers.idleHold(from, to))
std::this_thread::sleep_for(std::chrono::nanoseconds(hold));
}
void FreeHost::waitCompleted(ee::Dispatcher &, ee::Thread &t)
{
if (auto run = libs_.takeContinuation(t.id))
{
libs::Guest g(d_->runtime(), &t.top().ctx);
run(libs_, g);
}
}
void FreeHost::handlerCompleted(ee::Dispatcher &, ee::Thread &t, const ee::Frame &f)
{
if (auto run = libs_.takeHandlerEnd(f.sequence))
{
libs::Guest g(d_->runtime(), &t.top().ctx);
run(libs_, g);
}
}
// ---- Frames ------------------------------------------------------------------------------------------------
void FreeHost::frameEnd(Runtime *, R5900Context *ctx) { endFrame(ctx); }
bool FreeHost::callLibrary(Runtime *, R5900Context *ctx, uint32_t address)
{
auto n = names_.find(address);
const libs::Function *f = n != names_.end() ? libs_.find(n->second) : nullptr;
if (!f)
return false;
++stats_.libraryCalls;
++stats_.libraryByName[f->name];
libs_.pin = libs::Pin{};
libs::Guest g(d_->runtime(), ctx);
f->handler(libs_, g);
if (g.cycles)
d_->setNow(ctx, d_->now(ctx) + g.cycles);
ctx->pc = ctx->r[31].uw[0]; // it returned, as the function would have
return true;
}
std::string FreeHost::functionAt(uint32_t pc) const
{
auto it = names_.upper_bound(pc);
if (it == names_.begin())
return "?";
--it;
return std::format("{}+0x{:x}", it->second, pc - it->first);
}
uint32_t FreeHost::symbol(const std::string &name) const
{
for (const auto &[a, n] : names_)
if (n == name)
return a;
return 0;
}
// The frame's sound: the engine brought up to the frame's end on the EE clock, its samples since the last frame to
// onAudio, their digest for the record.
namespace
{
int64_t steadyNs();
}
void FreeHost::frameSound(uint64_t &hash, uint32_t &count)
{
hash = 0;
count = 0;
if (!opt_.sound)
return;
audio::Mixer *mixer;
if (libs_.audioProvider) mixer=libs_.audioProvider(d_->now());
else { libs_.snd.advanceAudio(libs_, d_->now()); mixer=libs_.snd.mixer(); }
if (!mixer)
return;
mixer->takeMixed(hash, count);
mixer->setPortStream(&libs_.portStream);
audio_.resize(2u * audio::Mixer::kQueueFrames);
size_t n = mixer->take(audio_.data(), audio::Mixer::kQueueFrames);
// Real time: a frame whose EE clock leapt (a wait the console spent in a device, a disc read, that the PC did
// not: The Third Age's streaming, up to five fields in one frame) brings the sound of the whole leap, but the
// host only lived the frame's own time. Given all, the output queue overflowed and dropped hundreds of
// milliseconds at a time (heard as cuts and crackle). The sound given out is held to the host's time instead:
// a budget of samples grows with it (48 kHz) and every output spends it (stallSound's too); a frame gives at
// most the budget plus a quarter field (the frames' own jitter), its newest samples, the leap's surplus skipped
// where it was made. The budget is kept within a few fields, so neither a stall nor a run of short frames
// stores up a burst.
if (opt_.realTime)
n = holdToHostTime(n);
if (onAudio && n)
onAudio(stats_.frames, audio_.data(), n);
}
size_t FreeHost::holdToHostTime(size_t n)
{
const int64_t now = steadyNs();
if (lastSoundNs_ != 0)
{
soundBudget_ += static_cast<double>(now - lastSoundNs_) * 48e-6;
soundBudget_ = std::clamp(soundBudget_, -double(kFieldFrames), 3.0 * kFieldFrames);
const double room = soundBudget_ + kFieldFrames / 4.0;
const size_t allowed = room > 0.0 ? static_cast<size_t>(room) : 0u;
if (n > allowed)
{
const size_t surplus = n - allowed;
if (allowed > 1u && surplus * 20u <= n)
{
// Drift and jitter (the window's frames a hair off the sound card's clock: up to 5 % too much): the
// frame's sound resampled into what is allowed, its first and last samples kept, a pitch change too
// small to hear. Cut instead, a few samples a frame left a click each, every few frames: Amplitude's
// tutorial narration crackled (a steady 800 samples a frame, 1 to 16 of them dropped).
squeezed_.resize(2u * allowed);
const double step = static_cast<double>(n - 1u) / static_cast<double>(allowed - 1u);
for (size_t i = 0; i < allowed; ++i)
{
const double x = static_cast<double>(i) * step;
const size_t k = std::min(static_cast<size_t>(x), n - 1u), k1 = std::min(k + 1u, n - 1u);
const double f = x - static_cast<double>(k);
for (size_t c = 0; c < 2u; ++c)
squeezed_[2u * i + c] = static_cast<int16_t>(std::lround(audio_[2u * k + c] * (1.0 - f) + audio_[2u * k1 + c] * f));
}
std::memcpy(audio_.data(), squeezed_.data(), 2u * allowed * sizeof(int16_t));
}
else
{
// A leap: its surplus skipped where it was made (the newest samples kept), the join cross-faded over
// up to 64 samples from what would have followed the last sound given out.
const size_t skip = surplus, fade = std::min<size_t>({64u, allowed, skip});
for (size_t i = 0; i < fade; ++i)
{
const double t = static_cast<double>(i + 1u) / static_cast<double>(fade + 1u);
for (size_t c = 0; c < 2u; ++c)
audio_[2u * i + c] = static_cast<int16_t>(
std::lround(audio_[2u * i + c] * (1.0 - t) + audio_[2u * (skip + i) + c] * t));
}
std::memmove(audio_.data() + 2u * fade, audio_.data() + 2u * (skip + fade),
2u * (allowed - fade) * sizeof(int16_t));
}
leapFrames_ += surplus;
n = allowed;
}
soundBudget_ -= static_cast<double>(n);
}
lastSoundNs_ = now;
return n;
}
namespace
{
int64_t steadyNs()
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
}
// A stall's sound (real time): the engine brought up to the host's clock (SndDriver::syncClock) between two
// out-calls, its samples to onAudio. The watchdog calls it on the game's thread, never inside a library's handler.
void FreeHost::stallSound(const R5900Context *ctx)
{
soundDue_.store(false, std::memory_order_relaxed);
audio::Mixer *mixer;
const uint64_t cycle=ctx ? d_->now(ctx) : d_->now();
if (libs_.audioProvider) mixer=libs_.audioProvider(cycle);
else { libs_.snd.advanceAudio(libs_,cycle); mixer=libs_.snd.mixer(); }
if (!mixer || !onAudio) return;
mixer->setPortStream(&libs_.portStream);
audio_.resize(2u * audio::Mixer::kQueueFrames);
// Held to the host's time as a frame's sound is: brought up to the EE clock, a stall during a load (its disc
// waits counted, the PC's not) gave the leap's sound in a few bursts, up to 8613 samples each, and the output
// queue dropped about 1.2 s of it (The Third Age's first movie after the title).
const size_t n = holdToHostTime(mixer->take(audio_.data(), audio::Mixer::kQueueFrames));
if (n)
onAudio(stats_.frames, audio_.data(), n);
}
// Raises soundDue_ every few milliseconds while no frame has ended for more than a frame and a half: the game's
// thread does the work at its next out-call. Only in real time with sound and an output.
void FreeHost::startSoundWatcher()
{
if (!opt_.realTime || !opt_.sound || !onAudio || watcher_.joinable())
return;
watcherStop_.store(false);
frameEndNs_.store(steadyNs());
watcher_ = std::thread([this] {
constexpr int64_t kStallNs = 25'000'000; // 1.5 frames: the output queue (32 ms) still holds most of its lead
while (!watcherStop_.load(std::memory_order_relaxed))
{
std::this_thread::sleep_for(std::chrono::milliseconds(4));
if (steadyNs() - frameEndNs_.load(std::memory_order_relaxed) > kStallNs)
soundDue_.store(true, std::memory_order_relaxed);
}
});
}
void FreeHost::stopSoundWatcher()
{
watcherStop_.store(true);
if (watcher_.joinable())
watcher_.join();
soundDue_.store(false);
}
void FreeHost::endFrame(R5900Context *ctx)
{
const uint64_t frame = stats_.frames;
if (ctx && d_->currentDepth() == 0 && opt_.framePeriodCycles && d_->now(ctx)-lastFrameCycle_ < opt_.framePeriodCycles)
port::accountCycles(&d_->runtime(),ctx,opt_.framePeriodCycles-(d_->now(ctx)-lastFrameCycle_));
lastFrameCycle_ = d_->now();
if (writing_)
{
uint8_t *flat = d_->runtime().memory.flat();
reference::FrameRecord fr;
fr.frame = static_cast<uint32_t>(frame);
fr.thread = d_->kernel().running();
fr.ram = hash::xxh64(flat, 0x2000000, 0);
fr.scratchpad = hash::xxh64(flat + 0x2000000, 0x4000, 0);
std::vector<int32_t> order;
d_->kernel().runOrder(order);
fr.threads = reference::threadsHash(order);
fr.eeCycle = d_->now();
const render::FrameDraws draws = render::Renderer::instance().takeFrameDraws();
fr.draws = draws.hash;
fr.drawBatches = draws.batches;
fr.drawVertices = draws.vertices;
frameSound(fr.sound, fr.soundSamples);
writer_.write(fr);
}
else
{
render::Renderer::instance().takeFrameDraws();
uint64_t hash = 0;
uint32_t count = 0;
frameSound(hash, count);
}
frameEndNs_.store(steadyNs(), std::memory_order_relaxed);
soundDue_.store(false, std::memory_order_relaxed);
++stats_.frames;
framesEnded_.store(stats_.frames, std::memory_order_relaxed);
libs_.framesEnded = static_cast<uint32_t>(stats_.frames);
det::frameCounter().store(stats_.frames, std::memory_order_relaxed);
d_->frameCompleted();
if (!opt_.saveState.empty() && stats_.frames == opt_.saveStateFrame)
requestSave(opt_.saveState);
if (!opt_.loadState.empty() && opt_.loadStateFrame && stats_.frames == opt_.loadStateFrame)
{
requestLoad(opt_.loadState);
opt_.loadStateFrame = 0; // once (the loaded state may pass this frame again)
}
if (onFrame)
onFrame(frame, ctx);
if (opt_.frames && stats_.frames >= opt_.frames)
{
endReason_ = std::format("frame limit {} reached", opt_.frames);
throw ee::EndRun{};
}
if (stopRequested_.load(std::memory_order_relaxed))
{
endReason_ = "stopped";
throw ee::EndRun{};
}
}
}
namespace bozo::host
{
// ---- Save states ----
void FreeHost::requestSave(const std::string &path)
{
{
std::lock_guard lock(stateMutex_);
saveTo_ = path;
saveWaiting_.clear();
}
if (d_)
d_->safePointDue.store(true, std::memory_order_relaxed);
}
void FreeHost::requestLoad(const std::string &path)
{
{
std::lock_guard lock(stateMutex_);
loadFrom_ = path;
}
if (d_)
d_->safePointDue.store(true, std::memory_order_relaxed);
}
std::string FreeHost::unsaveable()
{
if (insideDepth_)
return "a port override is running";
if (std::string why = d_->unsaveable(); !why.empty())
return why;
if (std::string why = libs_.unsaveable(); !why.empty())
return why;
for (const state::Section &s : sections_.all())
if (s.unsaveable)
if (std::string why = s.unsaveable(); !why.empty())
return s.name + ": " + why;
return {};
}
void FreeHost::serializeAll(state::Archive &a)
{
d_->serialize(a);
libs_.serialize(a);
a.begin("host");
state::fields(a, stats_.frames, lastFrameCycle_);
a.end();
for (state::Section &s : sections_.all())
{
a.begin(s.name.c_str());
s.serialize(a);
a.end();
}
if (!a.atEnd() && a.loading())
throw state::Error("save state: the file has sections this runtime does not know");
if (a.loading())
{
framesEnded_.store(stats_.frames, std::memory_order_relaxed);
libs_.framesEnded = static_cast<uint32_t>(stats_.frames);
det::frameCounter().store(stats_.frames, std::memory_order_relaxed);
audio_.clear();
render::Renderer::instance().takeFrameDraws();
frameEndNs_.store(steadyNs(), std::memory_order_relaxed);
}
}
void FreeHost::atSafePoint()
{
std::string save, load;
{
std::lock_guard lock(stateMutex_);
std::swap(save, saveTo_);
std::swap(load, loadFrom_);
}
auto report = [this](bool ok, const std::string &message) {
if (onStateMessage)
onStateMessage(ok, message);
else
std::fprintf(stderr, "[state] %s\n", message.c_str());
};
if (!load.empty())
{
state::Archive backup;
bool backedUp = false;
try
{
state::Archive file = state::readFile(load, opt_.gameTag);
if (unsaveable().empty())
{
serializeAll(backup);
backedUp = true;
}
serializeAll(file);
report(true, std::format("loaded {} (frame {})", load, stats_.frames));
}
catch (const std::exception &e)
{
if (backedUp)
{
state::Archive restore(backup.data());
serializeAll(restore);
report(false, std::format("{}; the state was put back as it was", e.what()));
}
else
{
// Nothing to go back to (a run that loads before its first frame): the run ends.
report(false, e.what());
endReason_ = std::string("FAULT: ") + e.what();
throw ee::EndRun{};
}
}
}
if (!save.empty())
{
if (const std::string why = unsaveable(); !why.empty())
{
// Tried again at the next safe point.
std::lock_guard lock(stateMutex_);
if (saveTo_.empty())
saveTo_ = save;
if (saveWaiting_ != why)
{
saveWaiting_ = why;
report(false, std::format("save to {} waits: {}", save, why));
}
d_->safePointDue.store(true, std::memory_order_relaxed);
return;
}
try
{
state::Archive a;
serializeAll(a);
state::writeFile(save, opt_.gameTag, a);
report(true, std::format("saved {} (frame {}, {} MB)", save, stats_.frames, a.data().size() >> 20));
}
catch (const std::exception &e)
{
report(false, e.what());
}
}
}
}