Files
psxrecomp/runtime/include/parity_trace.h
Matthew Stanley e6ce5006b4 docs: move root and stale docs into docs/ and docs/internal/
The repository root carried seven markdown files that were not onboarding
material, plus two that had been superseded by the code they described. This
sorts them: reader-facing docs to docs/, working notes to docs/internal/, and
the two superseded ones merged into the docs that replaced them.

Moved to docs/:            WIDESCREEN.md, ENHANCEMENTS.md, TCP_COMMANDS.md
Moved to docs/internal/:   PRINCIPLES.md, ISSUES.md, DEBUG.md, GTE_LM_FIX.md,
                           and from docs/: bios-hardening.md, overlay-status.md,
                           overlay-plan.md, overlay-recompilation-design.md,
                           overlay-discovery.md, TOMBA2_EXC_CONSULT_RESPONSE.md,
                           CASE_A_AOT_GAP.md, audit_inventory.md
Moved to docs/internal/upstream/: UPSTREAM_2026-08-20.md

The root keeps only what belongs there: README.md, CONTRIBUTING.md, LICENSE,
CLAUDE.md, THIRD_PARTY_ATTRIBUTION.md, RELEASE_NOTES.md, the dotfiles and
psxrecomp_cli.py.

MERGES

SPLITGEN_MIGRATION.md folded into docs/BUILDING.md as "Split generated C". Its
migration instructions were stale in a way that mattered: it told every game
repo to hand-edit its CMakeLists and add a file(GLOB ...) over
<serial>_full*.c, but runtime/runtime.cmake now does that itself. GEN_FULL_GLOB
is a multi-value argument of psxrecomp_add_game_runtime(); the loop at
runtime.cmake:1985 makes each pattern absolute against CMAKE_CURRENT_SOURCE_DIR,
expands it with file(GLOB), and concatenates the hits into
GAME_GENERATED_FULL_C. So a title names the pattern and gets however many shards
a regeneration produced. The rewritten section describes that, the two-step
fallback when the globs match nothing (GEN_FULL_FALLBACK, else <serial>_full.c
derived from the dispatch marker by substituting _dispatch.c), and the one
caveat the code actually has: that file(GLOB) is plain, not CONFIGURE_DEPENDS,
so a regeneration which changes the shard COUNT needs a re-configure to be seen.

psxrecomp-toml-readme.md folded into docs/GAME_PROJECT_SETUP.md as "EXE-only
autofill (psxrecomp-toml)", placed after "Disc autofill (probe_disc.py)" and
before "Project Studio" so the two autofill paths sit together. Its documented
flags were checked against the usage text in recompiler/src/main_toml.cpp:28-42
and against the argument parser at :150-165 -- all seven options match with no
drift, so the flag list is reproduced as-is. Added a pointer that probe_disc.py
is preferable when a cue exists, since that tool does the same EXE-header work
plus disc identity and netplay gates.

FUNCTIONAL FIXES

tools/gen_tcp_commands.py wrote to REPO/"TCP_COMMANDS.md". After the move that
path does not exist, so the generator would have created a new stray root file
and --check would have failed against it forever. Retargeted to
REPO/"docs"/"TCP_COMMANDS.md", along with the seven docstring and message
strings that name the path.

Running it surfaced two further problems, both fixed here:

  * The doc was genuinely stale -- pre-existing drift, not introduced by this
    commit (the moved file is byte-identical to origin/master, and no
    debug-server command table is touched here). Four commands registered since
    the last regeneration were missing from the index -- cdrom_timing_dump,
    savestate_status, ws_ui_groups, xprobe_watch -- and scanline had gained a
    backend. Regenerated: 307 -> 311 commands, and the change is confined to the
    AUTOGEN block (the hand-written head and tail are byte-identical to master).
    tools/gen_tcp_commands.py --check now exits 0.
  * DOC.write_text() has no newline argument, so Python translated every newline
    to os.linesep. On Windows that rewrote all 585 lines of the file to CRLF and
    turned a four-line index update into a whole-file diff -- measured: 589 CRLF
    / 0 LF after a run, against 0 CRLF / 585 LF in the repo. Now written through
    an explicit newline="" handle, verified to leave the file at 0 CRLF.

runtime/include/debug_server.h:9 and runtime/src/debug_server.c:8 cited "TCP.md
and DEBUG.md". TCP.md has never existed in this repo under that name; both now
name docs/TCP_COMMANDS.md and docs/internal/DEBUG.md.

LINK AND REFERENCE FIXES

Every inbound reference to a moved file was located by grepping each basename
across the whole tracked tree, not just the markdown. Markdown hrefs in
README.md, CONTRIBUTING.md, docs/ARCHITECTURE.md, docs/COMPILING_OVERLAYS.md,
docs/AOT_OVERLAY_PLAN.md, docs/RELOCATION_MANIFEST_FORMAT.md,
docs/config_schema.md, docs/OVERLAY_XREF.md, docs/TCP_COMMANDS.md and several
docs/internal/ files now point at the new locations; where two files became
siblings under docs/internal/ the path was shortened rather than lengthened
(bios-hardening.md's reference to overlay-discovery.md, for instance).
docs/COMPILING_OVERLAYS.md's "in this folder" phrasing was wrong once two of the
three files it listed moved, so it now names internal/ explicitly.

24 source-comment mentions of ENHANCEMENTS.md sections G1.x across 19 files
(recompiler, runtime, cmake) were retargeted to docs/ENHANCEMENTS.md with the
section numbers untouched. docs/SHADOW_ENHANCEMENTS.md is a DIFFERENT file and
was deliberately excluded.

20 rationale strings citing docs/CASE_A_AOT_GAP.md across the four
recompiler/seeds/*.json files were updated. Done as a byte-level substring
replacement rather than a JSON round-trip: two of those files are stored with
CRLF endings and two with LF, and re-serializing them reformatted 5,700 lines.
The byte-level version changes exactly 20 lines and each file still parses.

References that look similar but point elsewhere were deliberately left alone,
and are worth naming so a future pass does not "fix" them:

  * "recomp-template PRINCIPLES.md" / "NES recomp template PRINCIPLES.md" in
    CLAUDE.md:43, docs/SHADOW_ENHANCEMENTS.md:28,
    docs/internal/HLE_SCHEDULER_CARVEOUT_PLAN.md:5,
    runtime/src/debug_server.c:4934 and tools/parity_diff.py:8 are a different
    repository's file. Only the five that mean this repo's PRINCIPLES.md were
    updated.
  * Every remaining bare ISSUES.md mention is a GAME repo's tracker
    (MegaManX6Recomp 2/7/8/9, TombaRecomp 6), not this repo's.
  * docs/disc-speed.md:4,54,56 and docs/internal/bios-hardening.md:5 name
    overlay-discovery as a BRANCH, not a file, so no path applies.

INDEXES

README.md's "New here?" table gains rows for docs/WIDESCREEN.md,
docs/ENHANCEMENTS.md and docs/TCP_COMMANDS.md. docs/internal/README.md gains
sections indexing all fourteen documents that moved in, plus an "Upstream
contributor reviews" section describing upstream/ and the new sweep file, plus a
"Removed" bullet for CYCLE_TIMING_ARCH.md (deleted in the previous commit)
pointing at FAITHFUL_TIMING_PLAN.md, CYCLE_MODEL_BEETLE.md and
PRECISE_IRQ_SLICE.md -- following the SLJIT_PERSIST_CACHE.md precedent already
in that file.

docs/ENHANCEMENTS.md:7 claimed framework bugs live in "game repos' ISSUES.md
until a framework tracker exists". A framework tracker does exist; it now says
so and names the Beads epic.

VERIFICATION

A link checker was written to scan every tracked *.md for relative links,
resolving each against the tree being scanned. Baseline at origin/master: 129
files, 137 relative links, 3 broken. After both commits: 127 files, 159 relative
links, 3 broken -- the SAME three, all pre-existing and untouched here:

    docs/DATA_SHARDS.md -> INSTANT_LOADS_PARKED.md   (never existed)
    docs/DATA_SHARDS.md -> INSTANT_LOADS_SPIKE2.md   (never existed)
    docs/NETPLAY.md     -> ../lib/recomp-net/README.md  (inside a submodule)

Zero new broken links. The checker's own coverage was checked against a raw grep
for markdown link openers: the 3-link difference is C++ lambdas inside code
blocks, correctly not treated as links.

Also verified: py_compile over all 113 tracked tools/*.py plus psxrecomp_cli.py;
all four seed JSONs still parse; the test-registration guard still reports 123
test files all registered; recompiler/tests/test_coverage_vault.py still passes;
and no file in this commit had its line endings flipped (checked mechanically
across all 50 modified files, after two files were caught and redone).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-03 07:39:07 -07:00

128 lines
6.5 KiB
C

/* parity_trace.h — general two-process control-flow parity trace.
*
* Purpose: end the "somewhere upstream" blindness. A thread/TCB-scoped,
* freeze-on-trigger ring with an IDENTICAL struct + JSON wire format on BOTH
* psx-runtime (code under test) and psx-beetle (independent oracle), so a
* generic sequence-diff tool (tools/parity_diff.py) can locate the FIRST logical
* divergence between the two timelines — not a frame diff (docs/internal/PRINCIPLES.md "find
* the first divergence" + "is the value WRONG or is the behavior MISSING").
*
* Model (CLAUDE.md ring-buffer doctrine): armed from boot (so the pre-divergence
* window is always covered — never arm-then-time), records one row per relevant
* control event while current_tcb == watched_tcb (or all TCBs if watched==0),
* and LATCHES (freezes) the moment a configured trigger target is dispatched, so
* the lead-up to the wedge survives instead of being evicted by the wedge loop.
*
* Generality: point it at any watched TCB + freeze trigger + up to six watch-word
* addresses. The MMX6 cutscene→gameplay freeze is the first user, but nothing
* here is MMX6-specific — it is the reusable substrate for a functional-parity
* audit between recomp and oracle.
*/
#ifndef PARITY_TRACE_H
#define PARITY_TRACE_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
PARITY_KIND_DISPATCH = 0, /* psx_dispatch iteration (per-leader) */
PARITY_KIND_CHANGETHREAD, /* syscall-3 ChangeThread decision point */
PARITY_KIND_RFE, /* ReturnFromException */
PARITY_KIND_YIELD, /* scheduler cross-thread switch committed */
PARITY_KIND_SAVE, /* context saved to a TCB */
PARITY_KIND_RESTORE, /* context restored from a TCB */
PARITY_KIND_TRIGGER, /* the freeze trigger fired (final row) */
} parity_kind_t;
#define PARITY_WATCH_MAX 6
typedef struct {
uint64_t seq;
uint32_t frame;
uint32_t kind;
uint64_t cycle; /* absolute guest CPU cycles since boot (the deterministic
* ruler; native psx_get_cycle_count / Beetle
* beetle_core_get_guest_cycles — directly comparable) */
uint32_t current_tcb;
uint32_t pc;
uint32_t ra;
uint32_t sp;
uint32_t epc; /* resume PC read from watched TCB (epc slot) */
uint32_t tcb_state; /* state/status word read from watched TCB */
uint32_t target; /* dispatch / switch target */
uint32_t watch[PARITY_WATCH_MAX];
/* Per-watch-word LAST-WRITER provenance (set by parity_trace_note_write):
* the exact store PC, guest cycle, frame, and (approx) writing TCB of the
* most recent write to each watch word. This is what turns "the value
* diverged" into "who/when produced the divergent value" — diff these across
* native vs Beetle to name the early producer of a gate input. */
uint32_t watch_wpc[PARITY_WATCH_MAX];
uint64_t watch_wcycle[PARITY_WATCH_MAX];
uint32_t watch_wframe[PARITY_WATCH_MAX];
uint32_t watch_wtcb[PARITY_WATCH_MAX];
} ParityEntry;
/* Each process supplies its own RAM reader (recomp: cpu->read_word; Beetle:
* PS_CPU peek). ctx is passed back verbatim. */
typedef uint32_t (*parity_read_word_fn)(void* ctx, uint32_t addr);
/* Configure. watched_tcb==0 => record all TCBs. trigger_target==0 => no freeze.
* tcb_epc_off / tcb_state_off are watched-TCB-relative byte offsets (PSX BIOS:
* epc slot = tcb+0x88, state word = tcb+0x00). watch_addrs/watch_count: up to
* PARITY_WATCH_MAX guest word addresses snapshotted on every row. Does not arm. */
void parity_trace_config(uint32_t watched_tcb, uint32_t trigger_target,
uint32_t tcb_epc_off, uint32_t tcb_state_off,
const uint32_t* watch_addrs, int watch_count);
void parity_trace_arm(int on); /* on=1 begins recording (idempotent) */
void parity_trace_reset(void); /* clear ring + unfreeze (keeps config) */
int parity_trace_is_armed(void); /* hot-path gate: cheap one-branch check */
int parity_trace_is_frozen(void);
/* Record one event. read_word+ctx source current_tcb (via TCBH 0x108), the
* watched-TCB epc/state, and the watch words. No-op unless armed, not frozen,
* and (watched==0 || current_tcb==watched). Latches on target==trigger. */
void parity_trace_record(parity_kind_t kind, uint32_t pc, uint32_t ra,
uint32_t sp, uint32_t target,
parity_read_word_fn read_word, void* ctx);
/* Note a guest memory write so the per-watch-word last-writer table stays
* current. addr may be guest-virtual or physical (masked internally); width is
* the access size in bytes (1/2/4); writer_pc is the EXACT store instruction PC
* (native: g_debug_last_store_pc; Beetle: the store pc from the wtrace hook).
* Updates a watch slot iff [addr,addr+width) overlaps that watch word. No-op
* unless armed and not frozen. Called from BOTH processes' write paths so the
* provenance is directly comparable. The writing TCB is taken from the most
* recent recorded control event (a cheap, accurate-in-practice approximation —
* the writer is the running thread). */
void parity_trace_note_write(uint32_t addr, uint32_t width, uint32_t writer_pc);
/* Read-side counterpart of parity_trace_note_write: record the EXACT load PC that
* last consumed each watch word (read provenance), so a diverging value can be
* traced to the last reader as well as the last writer. addr may be guest-virtual
* or physical (masked internally); value is the loaded word; reader_pc is the load
* instruction PC. No-op unless armed and not frozen. Called from both processes'
* read paths so read provenance is directly comparable. */
void parity_trace_note_read(uint32_t addr, uint32_t value, uint32_t reader_pc);
/* Readback (matches the beetle_fntrace_get pattern): copies up to max_rows
* NEWEST entries, oldest-first, into out[]; returns the count copied. */
uint32_t parity_trace_get(ParityEntry* out, uint32_t max_rows);
uint64_t parity_trace_total(void);
const char* parity_kind_str(uint32_t kind);
/* Readback for the dedicated READ ring (parity_trace_note_read). Same shape as
* parity_trace_get/total: newest rows oldest-first. Each row's pc = load PC,
* epc = read address, target/watch[slot] = loaded value, watch_w* = the last
* writer of that word at read time. Backs the debug server's parity_dump reads=1. */
uint32_t parity_trace_reads_get(ParityEntry* out, uint32_t max_rows);
uint64_t parity_trace_reads_total(void);
#ifdef __cplusplus
}
#endif
#endif /* PARITY_TRACE_H */