diff -ruN a/src/aot-cache.js b/src/aot-cache.js --- a/src/aot-cache.js 2026-11-15 18:13:49 +++ b/src/aot-cache.js 2026-11-15 18:13:49 @@ -53,6 +53,15 @@ const read = async (offset, length) => new Uint8Array(await file.slice(offset, offset + length).arrayBuffer()); const header = await read(0, 0x440); const view = new DataView(header.buffer); + // PlayAgain (J2): a Wii disc (magic at 0x18) has no DOL the planner can + // read; its key is its header and size, and its plan comes from guest RAM + // (aot-ram.js). The regions' own code hashes decide what is installed. + if (view.getUint32(0x18, false) === 0x5d1c9ea3) { + const keyed = new Uint8Array(0x448); + keyed.set(header); + new DataView(keyed.buffer).setBigUint64(0x440, BigInt(file.size), false); + return canonicalDolKey(keyed); + } if (view.getUint32(0x1c, false) !== GC_DISC_MAGIC) return null; const dolOffset = view.getUint32(0x420, false); const fstOffset = view.getUint32(0x424, false); diff -ruN a/src/aot-ram.js b/src/aot-ram.js --- a/src/aot-ram.js 1970-01-01 02:00:00 +++ b/src/aot-ram.js 2026-11-15 18:13:49 @@ -0,0 +1,181 @@ +// PlayAgain (J2, first step): the compilation plan of a game whose disc the +// planner cannot read (a Wii disc: encrypted partitions, NKit), built from +// the code as it sits in the emulated RAM once the game runs. Nothing here +// trusts what it finds: every function carries the hash of its code, and the +// core translates and installs only code that still matches it. +// +// What counts as code: RAM is cut in CHUNK-byte chunks; a chunk with at least +// MIN_BLR `blr` words is code (a text section has one every few dozen +// instructions, data almost never holds 0x4e800020). Adjacent code chunks +// form one executable segment. Roots: the targets of every `bl` found in +// those segments that land in one of them (functions as the game calls +// them), walked on trial so a stray word costs nothing, then the blocks the +// local profile saw running. +import { GuestImage, MAX_EXEC_BYTES } from "./aot-image.js"; +import { CONF_POINTER, CONF_PROFILE, discover } from "./aot-discovery.js"; +import { buildPlan, fnv1a64Hex, planModule, regionHash, regionSha256 } from "./aot-plan.js"; +import { encodeProfile, PROFILE_REAL_MODE } from "./aot-profile.js"; + +export const RAM_BASE = 0x80000000; +const CHUNK = 16 << 10; +const MIN_BLR = 6; +const BLR = 0x4e800020; + +// ram: Uint8Array copy of MEM1 (big-endian guest bytes), at RAM_BASE. +export function codeSegmentsOfRam(ram, { chunk = CHUNK, minBlr = MIN_BLR } = {}) { + const view = new DataView(ram.buffer, ram.byteOffset, ram.byteLength); + const segments = []; + let open = -1; + const chunks = Math.floor(ram.byteLength / chunk); + for (let c = 0; c <= chunks; c++) { + let blr = 0; + if (c < chunks) { + const end = (c + 1) * chunk; + for (let o = c * chunk; o < end; o += 4) if (view.getUint32(o, false) === BLR) blr++; + } + const isCode = blr >= minBlr; + if (isCode && open < 0) open = c; + if (!isCode && open >= 0) { + segments.push([open * chunk, c * chunk]); + open = -1; + } + } + return segments; +} + +// Keeps the segments the profile ran in first, then the largest, under the +// image's bound on executable bytes. +function boundSegments(segments, hot, max) { + const total = segments.reduce((n, [s, e]) => n + e - s, 0); + if (total <= max) return segments; + const weight = (seg) => hot.reduce((n, pc) => n + (pc >= seg[0] && pc < seg[1] ? 1 : 0), 0); + const ranked = segments.map((seg) => ({ seg, w: weight(seg) })).sort((a, b) => b.w - a.w || (b.seg[1] - b.seg[0]) - (a.seg[1] - a.seg[0])); + const kept = []; + let left = max; + for (const { seg } of ranked) { + const size = seg[1] - seg[0]; + if (size <= left) { + kept.push(seg); + left -= size; + } else if (left >= CHUNK && !kept.length) { + kept.push([seg[0], seg[0] + left]); + left = 0; + } + } + return kept.sort((a, b) => a[0] - b[0]); +} + +// The word before an entry ends the flow: blr, bctr, an unconditional b (not +// bl), rfi, or padding (zero). +function endsFlow(w) { + if (w === 0 || w === BLR || w === 0x4e800420 || w === 0x4c000064) return true; + return (w >>> 26) === 18 && (w & 1) === 0; +} + +// rfi, sc. (A mtmsr stays: the core's regions guard it, patch 0183, and go on when it changed EE alone.) +function changesMode(w) { + const op = w >>> 26, x = (w >>> 1) & 0x3ff; + return (op === 19 && x === 50) || op === 17; +} + +export function planRam(ram, { profile = null, gameId = null, options = {}, maxExecBytes = MAX_EXEC_BYTES } = {}) { + const view = new DataView(ram.buffer, ram.byteOffset, ram.byteLength); + const hot = []; + for (const b of profile?.blocks ?? []) if (!((b[3] ?? 0) & PROFILE_REAL_MODE)) hot.push((b[0] >>> 0) - RAM_BASE); + const code = boundSegments(codeSegmentsOfRam(ram), hot, maxExecBytes); + if (!code.length) throw new Error("ram plan: no code found in guest memory"); + // The image: code segments executable, what lies between them as data + // (jump tables, pointers). Data past the last code segment is left out: + // heaps hold stale words that only cost trials. + const segments = []; + let cursor = code[0][0]; + for (const [s, e] of code) { + if (s > cursor) segments.push({ start: RAM_BASE + cursor, bytes: ram.subarray(cursor, s), exec: false, name: `data${segments.length}` }); + segments.push({ start: RAM_BASE + s, bytes: ram.subarray(s, e), exec: true, name: `text${segments.length}` }); + cursor = e; + } + const tail = Math.min(ram.byteLength, cursor + (2 << 20)); + if (tail > cursor) segments.push({ start: RAM_BASE + cursor, bytes: ram.subarray(cursor, tail), exec: false, name: "data-tail" }); + const image = new GuestImage(segments); + // Roots: bl targets inside the code. + const targets = new Map(); + for (const [s, e] of code) { + for (let o = s; o < e; o += 4) { + const w = view.getUint32(o, false); + if ((w >>> 26) !== 18 || (w & 3) !== 1) continue; // bl, relative + const disp = ((w & 0x03fffffc) << 6) >> 6; + const t = (RAM_BASE + o + disp) >>> 0; + if (image.isExec(t)) targets.set(t, (targets.get(t) || 0) + 1); + } + } + const entryPoints = []; + for (const addr of [...targets.keys()].sort((a, b) => a - b)) entryPoints.push({ addr, source: "ram-call", confidence: CONF_POINTER, trial: true, forceEntry: true }); + const inImage = []; + const dynamic = []; + for (const b of profile?.blocks ?? []) { + const [s, e, , flags = 0] = b; + if (!(flags & PROFILE_REAL_MODE) && image.isExec(s)) { + entryPoints.push({ addr: s, source: "profile", confidence: CONF_PROFILE, trial: true }); + inImage.push(b); + } else { + dynamic.push({ start: s, end: e, mode: flags & PROFILE_REAL_MODE ? "real" : "virt", source: "profile" }); + } + } + const result = discover(image, { ...options, entryPoints }); + const read32 = (a) => image.read32(a); + const codeHash = fnv1a64Hex(new Uint8Array(code.flatMap(([s, e]) => [s >>> 24, (s >>> 16) & 255, (s >>> 8) & 255, s & 255, e >>> 24, (e >>> 16) & 255, (e >>> 8) & 255, e & 255]))); + const plan = buildPlan({ + disc: { gameId, dolHash: codeHash }, + modules: [planModule(result, { id: 0, kind: "dol", read32, profile: inImage })], + dynamic + }); + // A function whose code runs on into the next one (the _savegpr_N/_restgpr_N + // ladders, entered at any rung, which every prologue and epilogue calls) + // takes the words up to where the flow ends: the block that starts in it + // runs that far, and a region that stopped at the next rung ended its slice + // at a point no block-only run has (seen as stale stack words that differ, + // bowling replay, 2026-11-15). Left to the JIT they cost Wii Sports 20 + // million returns to the dispatcher in 30 s; inside, CPU-thread busy fell + // 0.745 -> 0.645 in bowling and 0.646 -> 0.562 in tennis, with equal + // digests (2026-11-17). One still open after 64 words is left to the JIT. + // So is a function that leaves the mode (rfi, sc: the context switch, the + // interrupt gates): blocks and regions end around those at different + // instructions (OSLoadContext, same replay, field 400). + let fallInto = 0, modeChange = 0, extended = 0; + if (options.keepFallInto !== true) { + for (const f of plan.modules[0].functions) { + // A function whose code runs on into the next one (a rung of a ladder) takes the words up to + // where the flow ends, as the block that starts in it does. + const last = f.ranges[f.ranges.length - 1]; + let end = last[1], n = 0; + while (n < 64 && image.isExec(end) && !endsFlow(read32((end - 4) >>> 0) ?? 0)) { end += 4; n++; } + const open = !endsFlow(read32((end - 4) >>> 0) ?? 0); + const mode = f.ranges.some(([s, e]) => { + for (let a = s; a < e; a += 4) if (changesMode(read32(a) ?? 0)) return true; + return false; + }); + if (open || mode) { + f.hash = null; + f.sha256 = null; + if (open) fallInto++; + else modeChange++; + continue; + } + if (end !== last[1]) { + last[1] = end; + f.hash = regionHash(read32, f.ranges); + f.sha256 = regionSha256(read32, f.ranges); + extended++; + } + } + } + plan.identity = { + dolHash: codeHash, + apploaderHash: null, + rels: [], + profileHash: profile ? fnv1a64Hex(encodeProfile(profile)) : null, + hintsHash: null + }; + plan.ram = { segments: code.map(([s, e]) => [RAM_BASE + s, RAM_BASE + e]), roots: targets.size, fallInto, modeChange, extended }; + return plan; +} diff -ruN a/src/core-host.js b/src/core-host.js --- a/src/core-host.js 2026-11-15 18:13:49 +++ b/src/core-host.js 2026-11-15 18:13:49 @@ -594,6 +594,7 @@ aotSampleMs: this.aotSampleMs, wasmEngine: this.wasmEngine, regionCap: this.regionCap, + aotRam: new URLSearchParams(window.location.search).get("aotram") === "1", regionChain: this.regionChain, aotAssembler: this.aotAssembler, fusedMadd: this.fusedMadd, diff -ruN a/src/upstream-discio-worker.js b/src/upstream-discio-worker.js --- a/src/upstream-discio-worker.js 2026-11-15 18:13:49 +++ b/src/upstream-discio-worker.js 2026-11-15 18:13:49 @@ -2293,6 +2293,7 @@ aotSampleMs: payload.aotSampleMs, wasmEngine: payload.wasmEngine, regionCap: payload.regionCap, + aotRam: payload.aotRam, regionChain: payload.regionChain, aotAssembler: payload.aotAssembler, fusedMadd: payload.fusedMadd, @@ -3068,6 +3069,7 @@ aotSampleMs = AOT_PROFILE_SAMPLE_MS, wasmEngine: requestedWasmEngine = null, regionCap = null, + aotRam = false, regionChain = true, aotAssembler = null, fusedMadd = true, @@ -3523,6 +3525,7 @@ aotProfileSampleMs = Number.isInteger(aotSampleMs) && aotSampleMs >= 0 ? aotSampleMs : AOT_PROFILE_SAMPLE_MS; wasmEngineName = resolveWasmEngineName(requestedWasmEngine); aotRegionCapRequested = Number.isInteger(regionCap) && regionCap >= 0 ? regionCap : null; + aotRamPlanForced = aotRam === true; aotAssemblerRequestedFlag = typeof aotAssembler === "boolean" ? aotAssembler : null; dolphinJitBootLoadComplete = false; dolphinJitBootLoadedEntries = 0; @@ -8880,7 +8883,7 @@ : { schema: AOT_PROGRESS_SCHEMA, state: idle, events: [] }; // The local profile's records and failures (never silent again). const profile = aotProfileReport(); - return profile ? { ...progress, profile } : progress; + return { ...(profile ? { ...progress, profile } : progress), ram: aotRamPlanReport, paInstalled: aotPaInstalled.length, paSeen: aotPaInstallCount, paFirst: aotPaInstalled.slice(0, 400) }; } default: throw new Error(`Unknown aot message: ${type}`); @@ -8970,7 +8973,7 @@ onLoadReject: (key, error) => regionLinkFailures.record(key, error), ...aotCacheCompileOptions() }), - planStore: opfs ? new OpfsPlanStore(opfs) : null, + planStore: opfs && !aotRamPlanForced ? new OpfsPlanStore(opfs) : null, // ?aotram=1 (tests): planned anew each launch // Planning reads the DOL and RELs and runs discovery (about a second): // in a worker of its own, never on this one, which serves the core's // disc reads. Without workers (tests), in place. @@ -8978,13 +8981,57 @@ const profile = opfs ? await new AotProfileStore(opfs).load(profileKey(discKey)).catch((error) => noteAotProfileReadFailure(error)) : null; aotPlanProfileForTest = profile; if (aotPlannerWorkerForTest) return planDiscInWorker(file, { profile, kind, createWorker: aotPlannerWorkerForTest }); - if (typeof Worker === "function") return planDiscInWorker(file, { profile, kind }); - return kind === "dol" ? planDolFile(fileReader(file), file.size, { profile }) : planDisc(fileReader(file), { profile }); + // PlayAgain (J2): a disc the planner cannot read (Wii, NKit) gets its + // plan from the code in the emulated RAM (aot-ram.js). ?aotram=1 forces + // it for any disc. + const fromRam = (why) => fromRamInner(why).catch((error) => { + aotRamPlanReport = { why, error: String(error?.stack || error).slice(0, 600) }; + throw error; + }); + const fromRamInner = async (why) => { + aotRamPlanReport = { why, state: "waiting" }; + await aotPaFilterReady; + if (!(await whenCoreRamReady(module))) throw new Error(`aot: no guest RAM for a plan (${why})`); + await new Promise((resolve) => setTimeout(resolve, AOT_RAM_PLAN_DELAY_MS)); + const [{ planRam }, { coreHeapBases }] = await Promise.all([import("./aot-ram.js"), import("./aot-orchestrator.js")]); + const base = coreHeapBases(module)[0]; + if (!base) throw new Error("aot: guest RAM has no base yet"); + const t0 = performance.now(); + const ram = new Uint8Array(0x01800000); + ram.set(new Uint8Array(module.HEAPU8.buffer, base, ram.byteLength)); + const plan = planRam(ram, { profile }); + // harness: { "fnLo": a, "fnHi": b } keeps only the functions whose entry is in [a, b) + if (aotPaInstallFilter && Number.isInteger(aotPaInstallFilter.fnLo)) { + for (const f of plan.modules[0].functions) if (!(f.entry >= aotPaInstallFilter.fnLo && f.entry < aotPaInstallFilter.fnHi)) f.hash = null; + } + const fns = plan.modules[0].functions.length; + aotRamPlanReport = { why, segments: plan.ram.segments, roots: plan.ram.roots, functions: fns, fallInto: plan.ram.fallInto, modeChange: plan.ram.modeChange, hashed: plan.modules[0].functions.filter((f) => f.hash && f.sha256).length, ms: Math.round(performance.now() - t0), stats: plan.modules[0].stats }; + postStatus(`aot: plan from guest RAM (${why}): ${plan.ram.segments.length} code segments, ${plan.ram.roots} call targets, ${fns} functions, ${Math.round(performance.now() - t0)} ms`); + return plan; + }; + if (aotRamPlanForced) return fromRam("?aotram=1"); + let discPlan = null, discError = null; + try { + discPlan = typeof Worker === "function" ? await planDiscInWorker(file, { profile, kind }) + : await (kind === "dol" ? planDolFile(fileReader(file), file.size, { profile }) : planDisc(fileReader(file), { profile })); + } catch (error) { + discError = String(error?.message || error).slice(0, 80); + } + // A disc plan with no function to compile (a disc read as something it + // is not) is no plan either. + const usable = discPlan?.modules?.some((m) => m.functions?.some((f) => f.hash && f.sha256)); + if (usable) return discPlan; + return fromRam(discError ?? "the disc plan holds no function"); }, compile: precompileAotRegion, // Queued once the module is also kept under its bytes' key: the CPU // thread's compile request for it then resolves from this live Module. install: (region) => { + // PlayAgain (J2, harness only): pa-j2.json beside index.html, { "from": i, "to": j }: + // only the regions whose install number is in [from, to) are installed (bisecting). + const paIndex = aotPaInstallCount++; + if (aotPaInstallFilter && (!(paIndex >= aotPaInstallFilter.from && paIndex < aotPaInstallFilter.to) || aotPaInstallFilter.skip?.includes(paIndex))) return; + aotPaInstalled.push([paIndex, Math.min(...(region.entries ?? [0])) >>> 0, Math.max(...(region.entries ?? [0])) >>> 0, (region.members ?? []).map((m) => m.ranges?.[0]?.[0] >>> 0)]); const mod = dolphinJitCacheMap.get(region.key); const keyed = mod ? regionBlockCacheKey(region.bytes).then((k) => { if (dolphinJitCacheMap.size < DOLPHIN_JIT_CACHE_MAX) dolphinJitCacheMap.set(k, mod); @@ -9215,6 +9262,16 @@ } // ?regioncap=N|0 (core-host): the region function cap forced, else null (by engine). let aotRegionCapRequested = null; +// PlayAgain (J2): ?aotram=1 (core-host): the plan from guest RAM, not the disc. +let aotRamPlanForced = false; +let aotRamPlanReport = null; +let aotPaInstallCount = 0; +let aotPaInstallFilter = null; +const aotPaInstalled = []; +const aotPaFilterReady = fetch(new URL("./pa-j2.json", import.meta.url)).then((r) => (r.ok ? r.json() : null)).then((j) => { + if (j && Number.isInteger(j.from) && Number.isInteger(j.to)) aotPaInstallFilter = j; +}).catch(() => {}); +const AOT_RAM_PLAN_DELAY_MS = 3000; // ?aotasm=0|1 (core-host), null when absent: the default by engine // (aotAssemblerDefault: off under JSC until measured on an iPhone). let aotAssemblerRequestedFlag = null;