PlayAgain 0102: frame-locked replay and digests, harness only (design ยง5.4, J0 item 5). SPDX-License-Identifier: GPL-2.0-or-later Tree: the fork (core/upstream/dolphin_web_discio.cpp, src/upstream-discio-worker.js). What: validationPaReplay {action: arm|read|stop} arms a replay that starts at the end of the next state load, keyed to the VI outputs counted since that load (emulated time, CPU thread): a GameCube pad script on port 0 ([from, to, buttons] field ranges), a Wii Remote sample script topped up into remote N's ring (pa_wiimote_push, 64 samples ahead) at the load and at every VI output, and every K fields a digest (FNV-1a 64 of MEM1, MEM2, ARAM, CPU state, XFB; ticks, pc, downcount; MEM1 also in 64 KiB chunks for the first 64 digests, action "chunks"). validationPaPeek reads guest MEM1 bytes (for the PC histogram's loop check). Cost when not armed: one relaxed load per VI output and per pad poll. Switch: not armed = off. --- a/core/upstream/dolphin_web_discio.cpp +++ b/core/upstream/dolphin_web_discio.cpp @@ -36,6 +36,7 @@ #include "VideoCommon/WasmGpuSyncDiag.h" #include "Core/HW/EXI/EXI_DeviceIPL.h" #include "Core/HW/Memmap.h" +#include "Core/HW/DSP.h" // PlayAgain 0102: ARAM for the replay digests #if defined(DOLPHIN_WEB_DET_TRACE) && DOLPHIN_WEB_DET_TRACE #include "Core/WasmDetTrace.h" #endif @@ -1008,11 +1009,254 @@ static std::uint32_t s_harness_capture_index = 0; static std::uint64_t s_harness_capture_ticks = 0; +// ---- PlayAgain 0102: frame-locked replay and digests (harness only) ------------------------ +// Off unless a harness arms it (validationPaReplay). Armed, it starts at the end of the next +// state load, on the CPU thread, and is keyed to the VI outputs counted since that load +// (s_harness_vi_since_load: emulated time, the same on every run of one state): +// - the GameCube pad script: port 0 reads the script's buttons for this field ([from, to) +// ranges, sticks centred, analog A/B full when pressed); the page's own input is ignored; +// - the Wii Remote script: at the load and at every VI output, remote `wii_remote`'s ring is +// topped up to 64 queued samples from the script (pa_wiimote_push), so sample k plays at the +// k-th Wii Remote update after the load whatever the host does, and the ring never runs dry; +// - a digest every `every` fields (MEM1 also in 64 KiB chunks for the first 64): FNV-1a 64 (four lanes over 8-byte words) of MEM1, MEM2, +// ARAM (GameCube only), the CPU state (GPRs, FPRs, CR, XER, LR, CTR, SRR0/1, MSR, FPSCR, GQRs, +// pc) and the XFB the VI scans out, plus the emulated ticks, the pc and downcount. +// Nothing runs for a player's session: one relaxed load per VI output and per pad poll. +extern "C" int pa_wiimote_push(int i, const std::uint32_t* entries, int n); +extern "C" int pa_wiimote_queued(int i); +extern "C++" +{ +namespace PaReplay +{ +struct Digest +{ + std::uint32_t vi; + std::uint32_t pc; + std::uint64_t ticks; + std::uint64_t mem1, mem2, aram, cpu, xfb; + std::int32_t downcount; + std::uint32_t wii_played; // script samples handed to the ring so far +}; +static_assert(sizeof(Digest) == 64, "the worker reads 64-byte digests"); +constexpr std::uint32_t kMaxDigests = 4096; +constexpr int kWiiWords = 12; +constexpr int kWiiLead = 64; +std::atomic s_armed{0}; +std::atomic s_on{0}; +std::uint32_t s_every = 100; +std::vector s_digests; +std::atomic s_count{0}; +std::vector s_pad; // triplets: from, to, GC button mask +std::vector s_wii; // samples of 12 words (pa_wiimote_push's layout) +int s_wii_remote = 0; +std::uint32_t s_wii_pos = 0; +std::atomic s_wii_underruns{0}; +std::atomic s_pad_polls{0}; +// MEM1 in 64 KiB chunks for the first kChunkDigests digests: where two runs differ. +constexpr std::uint32_t kChunkShift = 16; +constexpr std::uint32_t kChunkDigests = 64; +std::vector s_chunks; +std::uint32_t s_chunks_per_digest = 0; + +std::uint64_t Hash(const std::uint8_t* p, std::size_t n) +{ + constexpr std::uint64_t kBasis = 1469598103934665603ull, kPrime = 1099511628211ull; + std::uint64_t h0 = kBasis, h1 = kBasis ^ 1, h2 = kBasis ^ 2, h3 = kBasis ^ 3; + std::size_t i = 0; + for (; i + 32 <= n; i += 32) + { + std::uint64_t w[4]; + std::memcpy(w, p + i, 32); + h0 = (h0 ^ w[0]) * kPrime; + h1 = (h1 ^ w[1]) * kPrime; + h2 = (h2 ^ w[2]) * kPrime; + h3 = (h3 ^ w[3]) * kPrime; + } + for (; i < n; ++i) + h0 = (h0 ^ p[i]) * kPrime; + std::uint64_t h = kBasis; + for (const std::uint64_t v : {h0, h1, h2, h3, static_cast(n)}) + h = (h ^ v) * kPrime; + return h; +} + +std::uint64_t CpuHash(const PowerPC::PowerPCState& s) +{ + std::array v{}; + std::size_t k = 0; + for (int r = 0; r < 32; ++r) + v[k++] = s.gpr[r]; + for (int r = 0; r < 32; ++r) + { + v[k++] = s.ps[r].PS0AsU64(); + v[k++] = s.ps[r].PS1AsU64(); + } + v[k++] = s.cr.Get(); + v[k++] = s.GetXER().Hex; + v[k++] = s.spr[SPR_LR]; + v[k++] = s.spr[SPR_CTR]; + v[k++] = s.spr[SPR_SRR0]; + v[k++] = s.spr[SPR_SRR1]; + v[k++] = s.msr.Hex; + v[k++] = s.fpscr.Hex; + v[k++] = s.pc; + for (int g = 0; g < 8; ++g) + v[k++] = s.spr[SPR_GQR0 + g]; + v[k++] = s.Exceptions; + return Hash(reinterpret_cast(v.data()), k * sizeof(std::uint64_t)); +} + +void TopUpWii() +{ + const std::uint32_t total = static_cast(s_wii.size() / kWiiWords); + if (s_wii_pos >= total) + return; + const int queued = pa_wiimote_queued(s_wii_remote); + if (queued <= 0 && s_wii_pos > 0) + s_wii_underruns.fetch_add(1, std::memory_order_relaxed); + int want = kWiiLead - std::max(0, queued); + while (want > 0 && s_wii_pos < total) + { + const int n = std::min(want, static_cast(total - s_wii_pos)); + const int got = pa_wiimote_push(s_wii_remote, &s_wii[s_wii_pos * kWiiWords], n); + if (got <= 0) + break; + s_wii_pos += static_cast(got); + want -= got; + } +} + +void OnStateLoaded() +{ + if (s_armed.load(std::memory_order_acquire) != 1) + return; + s_armed.store(2, std::memory_order_relaxed); // consumed: one load per arm + s_count.store(0, std::memory_order_relaxed); + s_wii_pos = 0; + s_wii_underruns.store(0, std::memory_order_relaxed); + s_pad_polls.store(0, std::memory_order_relaxed); + if (s_digests.size() < kMaxDigests) + s_digests.resize(kMaxDigests); + s_on.store(1, std::memory_order_release); + if (!s_wii.empty()) + TopUpWii(); +} + +void OnViOutput(std::uint32_t vi, const std::uint8_t* xfb, std::uint32_t width, std::uint32_t stride, + std::uint32_t height) +{ + if (s_on.load(std::memory_order_relaxed) != 1) + return; + if (!s_wii.empty()) + TopUpWii(); + if (s_every == 0 || vi % s_every != 0) + return; + const std::uint32_t n = s_count.load(std::memory_order_relaxed); + if (n >= s_digests.size()) + return; + auto& system = Core::System::GetInstance(); + auto& memory = system.GetMemory(); + auto& ppc = system.GetPPCState(); + Digest d{}; + d.vi = vi; + d.pc = ppc.pc; + d.ticks = system.GetCoreTiming().GetTicks(); + d.mem1 = Hash(memory.GetRAM(), memory.GetRamSizeReal()); + d.mem2 = memory.GetEXRAM() ? Hash(memory.GetEXRAM(), memory.GetExRamSizeReal()) : 0; + const std::uint8_t* aram = system.GetDSP().GetARAMPtr(); + d.aram = (aram && aram != memory.GetEXRAM()) ? Hash(aram, system.GetDSP().GetARAMSize()) : 0; + d.cpu = CpuHash(ppc); + d.xfb = (xfb && height) ? Hash(xfb, static_cast(stride) * (height - 1) + width * 2) : 0; + d.downcount = ppc.downcount; + d.wii_played = s_wii_pos; + if (n < kChunkDigests) + { + s_chunks_per_digest = memory.GetRamSizeReal() >> kChunkShift; + if (s_chunks.size() < static_cast(kChunkDigests) * s_chunks_per_digest) + s_chunks.resize(static_cast(kChunkDigests) * s_chunks_per_digest); + for (std::uint32_t c = 0; c < s_chunks_per_digest; ++c) + s_chunks[static_cast(n) * s_chunks_per_digest + c] = + Hash(memory.GetRAM() + (static_cast(c) << kChunkShift), std::size_t{1} << kChunkShift); + } + s_digests[n] = d; + s_count.store(n + 1, std::memory_order_release); +} + +// Port 0's status from the script, or false when the script does not drive it. +bool PadStatus(int pad_num, GCPadStatus* status) +{ + if (pad_num != 0 || s_pad.empty() || s_on.load(std::memory_order_relaxed) != 1) + return false; + s_pad_polls.fetch_add(1, std::memory_order_relaxed); + const std::uint32_t f = s_harness_vi_since_load; + std::uint32_t mask = 0; + for (std::size_t i = 0; i + 2 < s_pad.size(); i += 3) + if (f >= s_pad[i] && f < s_pad[i + 1]) + mask |= s_pad[i + 2]; + *status = {}; + status->isConnected = true; + status->button = static_cast(mask & 0xffff); + status->stickX = status->stickY = status->substickX = status->substickY = 0x80; + if (mask & PAD_BUTTON_A) + status->analogA = 0xff; + if (mask & PAD_BUTTON_B) + status->analogB = 0xff; + if (mask & PAD_TRIGGER_L) + status->triggerLeft = 0xff; + if (mask & PAD_TRIGGER_R) + status->triggerRight = 0xff; + return true; +} +} // namespace PaReplay +} // extern "C++" + +// Arm from the harness (any thread) while nothing runs: the scripts are copied. every = 0: no +// digests. Returns 0, or -1 when a replay is on (stop it first). +EMSCRIPTEN_KEEPALIVE int DolphinWeb_PaReplayArm(const std::uint32_t* pad, std::uint32_t pad_triplets, + const std::uint32_t* wii, std::uint32_t wii_samples, + int wii_remote, std::uint32_t every) +{ + if (PaReplay::s_on.load(std::memory_order_acquire) == 1) + return -1; + PaReplay::s_pad.assign(pad, pad + 3 * static_cast(pad ? pad_triplets : 0)); + PaReplay::s_wii.assign(wii, wii + PaReplay::kWiiWords * static_cast(wii ? wii_samples : 0)); + PaReplay::s_wii_remote = std::clamp(wii_remote, 0, 3); + PaReplay::s_every = every; + PaReplay::s_armed.store(1, std::memory_order_release); + return 0; +} +EMSCRIPTEN_KEEPALIVE void DolphinWeb_PaReplayStop() +{ + PaReplay::s_armed.store(0, std::memory_order_release); + PaReplay::s_on.store(0, std::memory_order_release); +} +EMSCRIPTEN_KEEPALIVE const void* DolphinWeb_PaReplayDigests() { return PaReplay::s_digests.data(); } +// MEM1 chunk hashes (64 KiB) of the first 64 digests, chunksPerDigest u64 each. +EMSCRIPTEN_KEEPALIVE const void* DolphinWeb_PaReplayChunks() { return PaReplay::s_chunks.data(); } +EMSCRIPTEN_KEEPALIVE std::uint32_t DolphinWeb_PaReplayChunksPerDigest() { return PaReplay::s_chunks_per_digest; } +EMSCRIPTEN_KEEPALIVE std::uint32_t DolphinWeb_PaReplayCount() +{ + return PaReplay::s_count.load(std::memory_order_acquire); +} +// [armed, on, wii samples handed over, wii samples in the script, wii underruns, pad polls] +EMSCRIPTEN_KEEPALIVE const std::uint32_t* DolphinWeb_PaReplayState() +{ + static std::array out; + out = {static_cast(PaReplay::s_armed.load(std::memory_order_acquire)), + static_cast(PaReplay::s_on.load(std::memory_order_acquire)), + PaReplay::s_wii_pos, + static_cast(PaReplay::s_wii.size() / PaReplay::kWiiWords), + PaReplay::s_wii_underruns.load(std::memory_order_relaxed), + PaReplay::s_pad_polls.load(std::memory_order_relaxed)}; + return out.data(); +} + // Called on the CPU thread at the end of a savestate load. void DolphinWeb_HarnessOnStateLoaded() { s_harness_vi_since_load = 0; s_harness_load_generation.fetch_add(1, std::memory_order_release); + PaReplay::OnStateLoaded(); // PlayAgain 0102 } #if defined(DOLPHIN_WEB_DET_TRACE) && DOLPHIN_WEB_DET_TRACE @@ -1100,6 +1344,7 @@ { ++s_harness_vi_since_boot; ++s_harness_vi_since_load; + PaReplay::OnViOutput(s_harness_vi_since_load, xfb, width, stride, height); // PlayAgain 0102 #if defined(DOLPHIN_WEB_DET_TRACE) && DOLPHIN_WEB_DET_TRACE DetTraceOnViOutput(s_harness_vi_since_boot); #endif @@ -1799,6 +2044,8 @@ { if (!status || pad_num < 0 || pad_num >= PAD_PORT_COUNT) return 0; + if (PaReplay::PadStatus(pad_num, status)) // PlayAgain 0102: the replay's pad script + return 1; const std::uint64_t ticks = Core::System::GetInstance().GetCoreTiming().GetTicks(); PadPoll poll; --- a/src/upstream-discio-worker.js +++ b/src/upstream-discio-worker.js @@ -1301,6 +1301,68 @@ return { supported: true, startVi: m._DolphinWeb_DetTraceStartVi() >>> 0, checkpoints, logCount, log, ramChunks }; } +// PlayAgain 0102 (core/upstream/dolphin_web_discio.cpp, PaReplay): arm a frame-locked replay that +// starts at the end of the next state load. pad: [[fromField, toField, gcButtonMask], ...] for +// port 0; wii: flat u32 words, 12 per sample (pa_wiimote_push's layout), played one per Wii +// Remote update of remote wiiRemote; every: a digest every N fields since the load (0: none). +// read: the digests so far and the replay's state; stop: off. +function harnessPaReplay({ action = "read", pad = [], wii = null, wiiRemote = 0, every = 100 } = {}) { + const m = moduleInstance; + if (!m?._DolphinWeb_PaReplayArm) return { supported: false, why: "core without PlayAgain 0102" }; + const state = () => { + const p = m._DolphinWeb_PaReplayState() >>> 0; + const v = new Uint32Array(m.HEAPU8.buffer, p, 6); + return { supported: true, armed: v[0], on: v[1], wiiPlayed: v[2], wiiTotal: v[3], wiiUnderruns: v[4], padPolls: v[5] }; + }; + if (action === "arm") { + if (!harnessEnabled) return { error: "harness hooks are off" }; + const padWords = Uint32Array.from(pad.flat().map((x) => x >>> 0)); + const wiiWords = wii ? Uint32Array.from(wii, (x) => x >>> 0) : new Uint32Array(0); + const pp = padWords.length ? m._malloc(padWords.byteLength) : 0; + const wp = wiiWords.length ? m._malloc(wiiWords.byteLength) : 0; + try { + if (pp) new Uint32Array(m.HEAPU8.buffer, pp, padWords.length).set(padWords); + if (wp) new Uint32Array(m.HEAPU8.buffer, wp, wiiWords.length).set(wiiWords); + const rc = m._DolphinWeb_PaReplayArm(pp, padWords.length / 3, wp, wiiWords.length / 12, wiiRemote | 0, every >>> 0); + return { rc, ...state() }; + } finally { + if (pp) m._free(pp); + if (wp) m._free(wp); + } + } + if (action === "stop") { + m._DolphinWeb_PaReplayStop(); + return state(); + } + if (action === "chunks") { + // MEM1 chunk hashes (64 KiB) of the first 64 digests: [digest][chunk] as hex. + const per = m._DolphinWeb_PaReplayChunksPerDigest?.() >>> 0; + const n = Math.min(64, m._DolphinWeb_PaReplayCount() >>> 0); + if (!per || !n) return { chunksPerDigest: per ?? 0, chunks: [] }; + const dv = new DataView(m.HEAPU8.buffer, m._DolphinWeb_PaReplayChunks() >>> 0, n * per * 8); + const chunks = []; + for (let i = 0; i < n; i += 1) { + const row = []; + for (let c = 0; c < per; c += 1) row.push(dv.getBigUint64((i * per + c) * 8, true).toString(16)); + chunks.push(row); + } + return { chunksPerDigest: per, chunks }; + } + const n = m._DolphinWeb_PaReplayCount() >>> 0; + const digests = []; + if (n) { + const dv = new DataView(m.HEAPU8.buffer, m._DolphinWeb_PaReplayDigests() >>> 0, n * 64); + const hex = (o) => dv.getBigUint64(o, true).toString(16).padStart(16, "0"); + for (let i = 0; i < n; i += 1) { + const o = i * 64; + digests.push({ vi: dv.getUint32(o, true), pc: dv.getUint32(o + 4, true).toString(16), ticks: Number(dv.getBigUint64(o + 8, true)), + mem1: hex(o + 16), mem2: hex(o + 24), aram: hex(o + 32), cpu: hex(o + 40), xfb: hex(o + 48), + downcount: dv.getInt32(o + 56, true), wiiPlayed: dv.getUint32(o + 60, true) }); + } + } + return { ...state(), count: n, digests }; +} + function readGpuSyncDiag() { const m = moduleInstance; if (!m?._DolphinWeb_GpuSyncDiag) return null; @@ -2600,6 +2662,23 @@ return pipelinePrewarm ? pipelinePrewarm.snapshot() : { enabled: false, why: !hardwareVideoBackend ? "not video=wgpu" : !wgpuPipelinePrewarmEnabled ? "wgpuprewarm=0" : "not started" }; case "validationReadDetTrace": return readDetTrace(payload); + case "validationPaReplay": + // PlayAgain 0102: frame-locked replay (pad and Wii Remote scripts) and digests. + return harnessPaReplay(payload); + case "validationPaPeek": { + // PlayAgain 0102: guest MEM1 bytes (big-endian words as in RAM) at an effective address, for + // the PC histogram's loop check. Read while the game runs: a snapshot, not a frame-exact copy. + if (!harnessEnabled) return { error: "harness hooks are off" }; + const lay = harnessHeapLayout(moduleInstance); + if (!lay?.ramBase) return { error: "no ram base (core lacks DolphinWeb_AotLayoutWords)" }; + const out = {}; + for (const [addr, len] of payload.ranges ?? []) { + const off = (addr >>> 0) & 0x01ffffff, n = Math.max(0, Math.min(4096, len | 0)); + if (off + n > lay.ramSize) { out[(addr >>> 0).toString(16)] = null; continue; } + out[(addr >>> 0).toString(16)] = Array.from(new Uint8Array(moduleInstance.HEAPU8.buffer, lay.ramBase + off, n), (b) => b.toString(16).padStart(2, "0")).join(""); + } + return { ramSize: lay.ramSize, bytes: out }; + } case "validationReadWgpuReplayState": return readWgpuReplayState(); case "validationReadPresentVsync":