Skip to content

feat: PS1 recompiler (#91) - #112

Merged
OmarAglan merged 5 commits into
mainfrom
phase2/ps1-recompiler
Nov 28, 2026
Merged

OmarAglan merged 5 commits into
mainfrom
phase2/ps1-recompiler

Conversation

@OmarAglan

Copy link
Copy Markdown
Contributor

Closes #91

The PS1 recompiler (ADR 0021): a game's EXE becomes a C module that the runtime runs wherever it has code, with our interpreter for the rest. Recompiled code ends every program in the same state as the interpreter, cycle for cycle.

What's in it

  • recompilers/ps1/analysis.{h,cpp}: functions from the entry point, jal/bal targets and --entry; blocks; tail calls; jump tables (the lui/addiu table address, the index added, the load, a sltiu bound); branches that always or never go.
  • recompilers/ps1/emit_c.{h,cpp} and ps1_recomp <exe | cue> -o out.c [--entry hex]: one C function per guest function with a switch on pc into its blocks, exact cycle counts, static load delays, an inlined RAM/scratchpad fast path, C calls for jal.
  • runtimes/ps1/recomp/guest.h (the C ABI; ps1_cpu is now the interpreter's own state), helpers.h (what generated code inlines), dispatch.{h,cpp} (Recompiled: entry table, hash check of each function's words, the bus stamps code pages on every RAM write from the CPU or DMA, the interpreter for everything else, logging of misses and changed code).
  • System::use_module, the plugin's --module, ps1_shot --module.

Tests

  • ps1_programs_recompiled: every test program recompiled and run both ways; CPU state, exceptions, RAM and scratchpad must match. New programs recomp_calls (a jump table, recursion, function pointers) and recomp_smc (code copied and patched at run time).
  • ps1_recomp_fuzz: 32 random programs (ps1_fuzz_exe): every instruction form, I/O reads and writes, stores with the cache isolated, exceptions, timer IRQs, loads in flight at branch targets and returns.
  • ps1_analysis_test: flow, registers, functions, blocks, tail calls, jump tables, EXE loading.
  • ps1_runtime_rom: a module loads through the plugin, and a file that isn't one fails the load.

Mutants (run locally)

27 mutants of emit_c.cpp, dispatch.cpp, helpers.h and analysis.cpp; 26 caught:

  • emitter: cycle accounting, every load-delay case (commit on exit, at raise, at branch targets, at calls, lwl/lwr merge), sra/slti signedness, bgez, the deadline check (the test hangs), the code-page check on stores, jalr's link, after_call's load and exit checks;
  • helpers: divide by zero, stores with the cache isolated, the code-page stamp;
  • dispatcher: entering with a load in flight or an interrupt due, the first hash check, after_access's interrupt and code checks;
  • analysis: tail calls, the block after a call, the jump table bound.

The first runs left 7 alive, and the random programs were extended for them: DMA that raises its IRQ inside the access and clears a word of the code ahead, loads in flight at targets, returns and I/O accesses, near branch targets so most blocks run. One survives: the fast path's user-mode check on loads. Recompiled code is never entered in user mode, and mtc0/rfe always return to the interpreter, so the check can't be reached; it stays as a safety check.

Not covered: DMA writing over the code of the function that is running, except at its very end (the random programs clear a word just ahead of the pc there).

Ridge Racer (USA)

Recompiled from the owner's dump read in place, output kept local (not committed): 214 functions, 2569 blocks. At 3000 frames on Linux (release build) the frame hash matches the interpreter's; about half the time (27.5 s against 13.9 s; 30.9 s against 15.5 s in a second run); about 85% of the cycles after boot run recompiled. The rest is the BIOS's kernel calls and code reached through pointers, for #92's --entry list. Overlays aren't recompiled yet.

Checks

  • Build, all tests (ANQA_REQUIRE_VULKAN=1), license audit, :launcher:assembleDebug
  • PROGRESS.md, PLAN.md, architecture.md, ADR 0021
  • No game data or generated game code committed

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-28T14:10:48.634542Z
We couldn’t complete this review. Comment @codex review to try again.
476392a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@OmarAglan

Copy link
Copy Markdown
Contributor Author

/code-review (high): one bug fixed in the last commit: a second use_module left the bus without code pages (the old Recompiled's destructor ran after the new one was built). ps1_programs_recompiled and ps1_recomp_fuzz now load the module twice; without the fix ps1_recomp_fuzz fails.

No change for the rest:

  • A module that fails to load stays open until the next load: p_load closes it first.
  • Guest recursion is C recursion: the slice's deadline bounds it.
  • Jump-table scans may take data for a table: extra entries are hash-checked and only cost coverage.

@OmarAglan
OmarAglan merged commit d867e66 into main Nov 28, 2026
4 checks passed
@OmarAglan
OmarAglan deleted the phase2/ps1-recompiler branch November 28, 2026 16:03
OmarAglan added a commit that referenced this pull request Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PS1 recompiler

1 participant