Files
psxrecomp/README.md
T
Matthew Stanley 5b3c7dd08d phase4: freeze_check diagnostic + accumulated overnight infra
Today's session work
- runtime/src/debug_server.c: freeze_check TCP cmd snapshots all liveness
  counters (PC, exception entries, exception_reentry_blocks, dirty_ram
  blocks/insns, fn_entry total + top-K histogram, SIO IRQ pending +
  countdown, sio_stat/i_stat/i_mask, mc_max_state) for tight-loop
  diagnosis when the runtime hangs
- runtime/src/debug_server.c: wtrace slot 12 covering [0x72F0..0x72F4]
  (data-byte counter); measured peak=1 confirming setup_data_phase resets
  the counter mid-cycle
- runtime/src/interrupts.c: exception_entries_total + exception_reentry_blocks
  counters + psx_get_freeze_diag accessor
- runtime/src/sio.c: sio_get_freeze_diag accessor for IRQ pacing internals
- memory entries: phase4_card_gate_setup_writes_2026_04_28 (refutes the
  prior \"missing [0x7568+slot] clearer\" framing — setup functions write
  LSB=0 directly), phase4_72f0_one_byte_per_vblank_2026_04_28 (peak=1
  measurement)

Overnight session work bundled in
- runtime/src/dirty_ram_interp.{c,h}: small MIPS interpreter for
  install-at-runtime kernel RAM stubs (Rule 18; e.g., SIO data-byte
  handler the BIOS writes to RAM 0xCF0)
- recompiler/src/full_function_emitter.{cpp,h}: 3057 jal/jalr continuation
  return labels added to dispatch table (continuation fix); branch
  fall-through fix for conditional-branch delay slots
- recompiler/src/strict_translator.cpp: matching translator updates
- generated/: regenerated SCPH1001 dispatch + full C output reflecting
  the recompiler changes
- runtime/include/debug_server.h, src/memory.c: card_txn ring + SIO IRQ
  ring + EvCB walk ring + per-PC dirty_ram counters + always-on wtrace
  coverage for [0x755A], [0x7514], [0x7528], [0x7520], [0x74A4],
  [0x75C0..C8], [0x74B8..C0]
- recompiler/tests/, runtime/tests/: SIO/card protocol unit tests
- tools/: 60+ Python diagnostic scripts (chain_trace analysis, SIO trace
  diff, EvCB walker, wtrace dumpers, etc.)
- docs/: psx_bios_disasm reference + dynamic_handler_install rationale
- OVERNIGHT_2026_04_28.md, SUMMARY_20260412_2.md: session logs

Phase 4 status (still blocked, root cause better understood)
- BIOS card chain runs through R-handlers correctly (chain_trace shows
  R1..R11 cycling 26x); shell IS retriggering _card_read
- Install handler at ROM 0xBFC15F1C increments [0x72F0] correctly per
  SIO IRQ; gate at <0x80 / <0x7F is correct
- Bottleneck: SIO IRQ countdown is access-paced, fires ~1× per VBlank
  vs ~128× needed; setup_data_phase (called from outer coord on VBlank)
  zeroes [0x72F0] before counter reaches 128
- Both attempted fixes today caused freezes:
  * VBlank deferral on [0x75C0]==1 — pending VBlank already in i_stat
    delivered anyway; tighter dispatch gate also locked up
  * Cycle-paced sio_tick at rates 50, 200 (and gated on [0x75C0]==1) —
    runtime freezes once data phase activates, root cause unidentified
- delay=5 narrows the threshold (peak=2 vs peak=1, no freeze) but
  doesn't get to 0x80
- Real fix likely requires deeper instrumentation of the freeze (call
  stack / dispatch state at the moment of TCP starvation)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 21:29:19 -07:00

6.7 KiB

Architecture note: install-at-runtime code

PSXRecomp v4 is a static recompiler with a small interpreter for self-modifying / install-at-runtime RAM. The PS1 BIOS writes 4-instruction dispatch stubs into kernel RAM at runtime (e.g. RAM 0xCF0 for the SIO data-byte handler) and then transfers control to those addresses. A pure static recompiler can't see those instructions because they don't exist at compile time — only the program's intent to install them does.

The runtime detects writes into kernel-RAM code regions, marks the affected pages "dirty", and routes any dispatch into a dirty page through a tiny MIPS interpreter that runs the actual installed instructions on the CPU register state. Static-recompiled C continues to handle the rest of the BIOS and all game code.

This is not HLE. We do not synthesize what the handler "would have done". We execute the BIOS author's own instructions, exactly as written, just from a different code source (RAM-at-runtime instead of ROM-at-compile-time). Mature static recompilers (N64Recomp, mednafen-PSX's dynarec) handle this the same way. See docs/dynamic_handler_install.md for the detailed analysis (kernel exception handler at RAM 0xC80 falls through to the installed stub at RAM 0xCF0 → handler at 0x641C).

The interpreter is not a fallback for code the recompiler failed to translate. Code that the recompiler can statically see must be statically recompiled. The interpreter exists solely for code that did not exist at recompile time.


Current Milestone

The authoritative milestone document is FIRST_MILESTONE.md. The authoritative phase plan is PLAN.md, Phase 1 section.

  • Phase 1a — Boot Slice: COMPLETE (2026-04-06). Reset-vector slice 0xBFC00000..0xBFC00074, 30 instructions, terminating at j 0xBFC00150. generated/boot_slice.c, generated/boot_slice_manifest.json, and an empty generated/unsupported_ops.json are the proof artifacts. Do not redo. Do not extend.
  • Phase 1b — Instruction Coverage: NEXT. Every R3000A opcode the BIOS uses must have a real implementation in strict_translator, with generated/instruction_inventory.json and generated/instruction_coverage.json as proof.
  • Phase 1c — Function Discovery Pipeline: after 1b. Bounded walk from explicit seed file, producing function_manifest.json, function_edges.json, discovery_run.log.json.
  • Phase 1d — Indirect Control Flow Recording: after 1c. Records, does not resolve. Produces indirect_jumps.json and the Ghidra cross-reference.
  • Phase 1e — Relocation Planning (HARD GATE): after 1d. Produces relocation_proofs/, address_aliases.json, normalization_rule.md. Full BIOS recompilation is forbidden until 1e is complete.

See PLAN.md → "Phase 1 — Controlled Recompiler Bring-up" for required artifacts, failure conditions, and acceptance criteria per sub-phase.


Non-Goals (Current Phase)

The following are explicitly out of scope for the entire Phase 1 window. Touching any of them is architectural drift and is not permitted regardless of how convenient it would be:

  • No runtime. There is no runtime/ directory in v4 yet, and Phase 1 does not create one.
  • No MMIO. No psx_mmio_read_* / psx_mmio_write_* entry points. generated/cpu_state.h stays compile-only.
  • No GPU. No GP0, no GP1, no software rasterizer, no VRAM.
  • No interrupts. No I_STAT, no I_MASK, no COP0 IRQ delivery hooks. (mtc0/mfc0/rfe are translated as instructions in Phase 1b — they are not wired to any runtime delivery path.)
  • No DMA, no timers, no SIO, no memcard, no CD-ROM, no SPU.
  • No full BIOS recompilation. Bounded walks only until Phase 1e closes.
  • No game loading. No PS-X EXE ingestion. No ISO. No Tomba. The game side does not exist in v4 until Phase 5.
  • No BIOS interpreter. Not as a fallback, not as a "temporary" bridge, not for "code we couldn't recompile yet".
  • No HLE BIOS functions. No bios.c, no A0/B0/C0 reimplementation in C. The recompiled BIOS is the BIOS.

Definition of Done (Current Phase)

Phase 1a (already met):

  • generated/boot_slice.c compiles cleanly with -c.
  • generated/boot_slice_manifest.json is complete.
  • generated/unsupported_ops.json is empty.
  • Every instruction in the slice is accounted for in the manifest.
  • No TODO / FIXME / XXX in any generated file.

Phase 1 as a whole (the gate that unlocks Phase 2):

  • All Phase 1a artifacts above continue to regenerate identically.
  • generated/instruction_inventory.json and generated/instruction_coverage.json exist and show every BIOS-used opcode implemented in strict_translator.
  • generated/function_manifest.json, generated/function_edges.json, and generated/discovery_run.log.json exist from a normalized Phase 1c re-run, with no duplicate functions under any normalized address.
  • generated/indirect_jumps.json, generated/indirect_jump_classes.json, and generated/indirect_jump_ghidra_xref.json exist and cover every indirect jump site in every discovered function.
  • generated/relocation_proofs/ exists with one subdirectory per proven ROM→RAM copy operation, each containing both Ghidra and DuckStation evidence.
  • generated/address_aliases.json and generated/normalization_rule.md exist and are mechanically derived from relocation_proofs/.
  • generated/unsupported_ops.json is still empty.
  • Nothing under generated/ was hand-edited.

PSXRecomp v4

A static MIPS-to-C recompiler for the PlayStation BIOS, with PS1 games as a follow-on layer. The BIOS runs as native compiled C, not as interpreted MIPS bytes.

This is a clean-slate restart after three failed prior attempts (v1, v2, v3) all collapsed into emulator-with-HLE shape instead of true static recompilation. See PLAN.md for the full background, the architectural decision, the salvage list, the phase plan, and the open questions we want outside review on.

Layout

psxrecomp-v4/
├── PLAN.md              the document, hand to outside reviewers
├── CLAUDE.md            rules for in-session work (Architecture A locked in)
├── README.md            this file
├── bios/
│   └── SCPH1001.BIN     the recompilation target, 524288 bytes
├── duckstation/         modified DuckStation, used as runtime oracle
└── recompiler/          MIPS->C translator, salvaged from v3
    ├── src/
    ├── include/
    ├── lib/             rabbitizer, fmt, toml11
    └── CMakeLists.txt

The runtime is intentionally absent at this stage — it will be built up fresh in Phase 2 once the recompiler can ingest the BIOS ROM and emit SCPH1001_full.c.