Files
psxrecomp/tools/_options_session/_capture_now.py
T
Matthew Stanley fdbd2b515f runtime: cross-fiber longjmp fix + decouple vblank-present from MMIO read
Two distinct bugs were causing OPTIONS-black hard freezes after 12K-65K
frames:

1. interrupts.c + traps.c: exception_jmpbuf is a single global. setjmp
   captures the running fiber, but longjmp may be requested from a
   different fiber via psx_exception_longjmp / psx_restore_state_escape
   during chain-handler dispatch. Cross-fiber longjmp restores RSP to
   the wrong stack while Win32 still tracks the calling fiber, corrupting
   fiber state until SwitchToFiber eventually hangs.

   Fix: track g_exception_owner_fiber at setjmp time. New
   deferred_exception_longjmp checks the current fiber; if it's not the
   owner, sets a pending flag and SwitchToFiber's back to the owner.
   psx_change_thread_fiber's post-SwitchToFiber path observes the flag
   and executes the longjmp on the correct stack.

2. gpu.c: gpu_read_gpustat fired gpu_vblank_tick() after 1000 polls,
   which invoked sdl_vblank_present (SDL_Delay -> Sleep) from inside
   an MMIO read code path. That meant recompiled MIPS spent wall time
   in Sleep deep inside the fiber call tree -- confusing to reason
   about and a contributor to fiber/longjmp interactions.

   Fix: synthetic VBLANK from a poll-storm only updates emulation state
   (LCF + I_STAT). The host-side render+sleep happens only at the proper
   cycle-paced VBLANK trigger in psx_check_interrupts.

Also adds: dirty_block ring slice to the freeze auto-dump, Win32 main-
thread stack capture for wedge_kind=1 hard freezes (SuspendThread +
StackWalk64 + SymFromAddr), and dbghelp link.

Validated: TombaRecomp psx-runtime ran 30+ minutes / 105K+ frames at
OPTIONS with no hard freeze. Previous freezes consistently hit at
11900, 20493, and 64596 frames.

Diagnostic tooling under tools/_options_session/ that produced this
diagnosis is included.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 12:49:13 -07:00

57 lines
1.9 KiB
Python

"""Capture live state on whichever backend is reachable, RIGHT NOW.
Reads:
- dispatch miss totals (runtime only)
- frame counters
- the two probe cells (0x80090CAC, 0x80090DA0) on both
- surrounding range 0x80090C80..0x80090DE0 on both
- hex diff of the surrounding range
No probes are armed — this is a pure ring/state read. Always-on rings
are preserved; we only read what's already captured.
"""
from __future__ import annotations
import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _dbg import call, read_block, read_word, RUNTIME_PORT, BEETLE_PORT
PROBE_CELLS = [0x80090CAC, 0x80090DA0]
RANGE_LO = 0x80090C80
RANGE_HI = 0x80090DE0
def safe(port, cmd, **kw):
try: return call(port, cmd, **kw)
except Exception as e: return {"error": str(e)}
print("=== ping / frame / dispatch ===")
rp = safe(RUNTIME_PORT, "ping")
bp = safe(BEETLE_PORT, "ping")
print(f"runtime: {rp}")
print(f"beetle : {bp}")
print("\n=== probe cells ===")
print(f"{'addr':<14} {'runtime':<12} {'beetle':<12} {'eq?':<4}")
for a in PROBE_CELLS:
try: rv = read_word(RUNTIME_PORT, a)
except Exception as e: rv = -1; rv_err = str(e)
try: bv = read_word(BEETLE_PORT, a)
except Exception as e: bv = -1; bv_err = str(e)
eq = "Y" if rv == bv else "N"
print(f"0x{a:08X} 0x{rv:08X} 0x{bv:08X} {eq}")
print(f"\n=== range 0x{RANGE_LO:08X}..0x{RANGE_HI:08X} (diff only) ===")
try:
r = read_block(RUNTIME_PORT, RANGE_LO, RANGE_HI - RANGE_LO)
b = read_block(BEETLE_PORT, RANGE_LO, RANGE_HI - RANGE_LO)
n_diff = 0
print(f"{'addr':<12} runtime beetle")
for i in range(0, len(r), 4):
rw = int.from_bytes(r[i:i+4], "little")
bw = int.from_bytes(b[i:i+4], "little")
if rw != bw:
n_diff += 1
print(f"0x{RANGE_LO+i:08X} {rw:08X} {bw:08X}")
print(f"\n{n_diff} divergent words out of {(RANGE_HI-RANGE_LO)//4}")
except Exception as e:
print(f"range read failed: {e}")