Files
psxrecomp/runtime/include/debug_server.h
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

167 lines
5.3 KiB
C

/*
* debug_server.h -- TCP debug server for PSX recomp v4
*
* Non-blocking TCP server on localhost (default port 4370).
* JSON-over-newline protocol, polled once per vblank.
* Includes a 36000-frame ring buffer for retroactive state queries.
*
* Same public API names as nesrecomp/snesrecomp debug servers
* so TCP.md and DEBUG.md documentation is reusable across projects.
*/
#ifndef PSXRECOMP_V4_DEBUG_SERVER_H
#define PSXRECOMP_V4_DEBUG_SERVER_H
#include <stdint.h>
#include "cpu_state.h"
#ifdef __cplusplus
extern "C" {
#endif
/* ---- Ring buffer frame record ---- */
#define FRAME_HISTORY_CAP 36000 /* ~10 min @ 60fps */
#define MAX_FRAME_DIFFS 32
#define RAM_SNAPSHOT_REGIONS 4
#define RAM_SNAPSHOT_SIZE 128
typedef struct {
uint16_t addr;
uint8_t mine;
uint8_t theirs;
} FrameDiffEntry;
typedef struct {
uint32_t frame_number;
int verify_pass; /* 1=pass, 0=fail, -1=not checked */
int diff_count;
/* ---- MIPS CPU state ---- */
uint32_t gpr[32];
uint32_t hi, lo;
uint32_t cop0_sr;
uint32_t cop0_cause;
uint32_t cop0_epc;
/* ---- Interrupt state ---- */
uint32_t i_stat;
uint32_t i_mask;
/* ---- GPU display state ---- */
uint16_t display_area_x, display_area_y;
uint16_t display_w, display_h;
uint8_t display_disabled;
/* ---- SIO state ---- */
uint16_t pad_buttons;
uint16_t sio_stat;
uint16_t sio_ctrl;
/* ---- Timing ---- */
uint32_t dispatch_count;
uint64_t total_dispatches;
/* ---- Game-specific (filled by hook) ---- */
uint8_t game_data[32];
/* ---- Last function name ---- */
char last_func[32];
/* ---- Configurable RAM snapshot regions ---- */
uint32_t snapshot_addr[RAM_SNAPSHOT_REGIONS];
uint8_t snapshot_data[RAM_SNAPSHOT_REGIONS][RAM_SNAPSHOT_SIZE];
/* ---- Divergence diffs (verify mode) ---- */
FrameDiffEntry diffs[MAX_FRAME_DIFFS];
} PSXFrameRecord;
/* ---- Public API (same names as NES version) ---- */
/* Initialize the server. Call once at startup.
* port=0 uses the default (4370). */
void debug_server_init(int port);
/* Poll for incoming connections and commands. Non-blocking.
* Call once per vblank. */
void debug_server_poll(void);
/* Record the current frame's state into the ring buffer.
* Call after vblank processing. */
void debug_server_record_frame(void);
/* Block while paused, polling TCP + SDL events.
* Call from vblank callback before frame processing. */
void debug_server_wait_if_paused(void);
/* Graceful shutdown. Call at exit. */
void debug_server_shutdown(void);
/* Check if a TCP client is connected. */
int debug_server_is_connected(void);
/* ---- Tier 1 reverse debugger: write trace ---- */
/* Current dispatch target — set by psx_dispatch() before each call.
* Captured by the write-trace hook for function attribution. */
extern uint32_t g_debug_current_func_addr;
/* Multi-range write-trace check. Called from memory.c write paths.
* Iterates up to 8 configurable address ranges; records matching
* writes into the 1M-entry ring buffer with function + frame attribution. */
void debug_server_trace_write_check(uint32_t phys, uint32_t old_val,
uint32_t new_val, uint8_t width);
/* MMIO write trace — separate ring buffer for 0x1F801xxx writes. */
void debug_server_trace_mmio_write(uint32_t addr, uint32_t val, uint8_t width);
/* Dispatch trace — record every function dispatched. */
void debug_server_trace_dispatch(uint32_t func_addr);
/* Direct-call entry hook — emitted by the recompiler at the top of every
* generated function so we can see direct-jal targets that never go through
* psx_dispatch. Logs into the fn_entry ring (subject to fn_filter) without
* touching the shadow stack — the native C call/return discipline already
* handles unwinding for direct calls. */
void debug_server_log_call_entry(uint32_t func_addr);
/* Last store instruction PC — set by the recompiler before every memory
* store (sb/sh/sw/swl/swr/swc2). Read by SIO/MMIO write handlers to
* attribute the writing instruction without needing a full call-stack
* walk. Stale across non-store instructions but every store updates it. */
extern uint32_t g_debug_last_store_pc;
/* SIO write PC tracer ring — records (pc, addr, value, byte_seq) for every
* write to SIO_DATA / SIO_CTRL / SIO_STAT. Always-on; pulled via TCP cmd
* "sio_pc_trace". */
void debug_server_log_sio_write(uint32_t addr, uint32_t value, uint8_t width);
/* ---- Watchpoint notifications ---- */
void debug_server_check_watchpoints(void);
/* ---- Input override ---- */
/* Returns >= 0 if the debug server wants to override pad input,
* -1 if no override is active. Value is PS1 16-bit button mask. */
int debug_server_get_input_override(void);
/* ---- Send helpers (for extensions) ---- */
/* Set the CPU state pointer for register queries. Call after CPU init. */
void debug_server_set_cpu(CPUState *cpu);
/* Global CPU pointer — set by debug_server_set_cpu(). Available for
* hardware simulation modules that need to read CPU state (e.g. GPU
* capturing stack values during A0 uploads). */
extern CPUState *debug_cpu_ptr;
void debug_server_send_line(const char *json);
void debug_server_send_fmt(const char *fmt, ...);
#ifdef __cplusplus
}
#endif
#endif /* PSXRECOMP_V4_DEBUG_SERVER_H */