Files
psxrecomp/tools/_options_session/_search_ec_bytes.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

41 lines
1.8 KiB
Python

"""Find where 'EC' bytes (0x45, 0x43) appear in runtime memory but not beetle.
That tells us if 0x43450000 at 0x8009B3D4 is from a stale buffer/string."""
import sys
sys.path.insert(0, 'F:/Projects/psxrecomp-v4/tools/_options_session')
import _dbg
# Scan a wide range of low RAM (0x80010000..0x80100000) for any place where
# runtime has bytes "45 43" but beetle doesn't.
# Too wide; let's narrow to 0x8009B000..0x8009B600 (around the diverging struct).
ranges = [
(0x80097000, 0x800A0000, "fn ptr table region + 0x8009B... region"),
(0x801FD000, 0x801FE000, "TCB region"),
]
for lo, hi, label in ranges:
print(f"=== {label}: 0x{lo:08X}..0x{hi:08X} ===")
chunk = 0x800
for base in range(lo, hi, chunk):
sz = min(chunk, hi - base)
try:
r_rt = _dbg.call(4470, 'read_ram', addr=f'0x{base:08X}', len=sz, timeout=4)
r_be = _dbg.call(4380, 'read_ram', addr=f'0x{base:08X}', len=sz, timeout=4)
if not r_rt.get('ok') or not r_be.get('ok'): continue
bt_rt = bytes.fromhex(r_rt['hex'])
bt_be = bytes.fromhex(r_be['hex'])
for i in range(len(bt_rt) - 1):
if bt_rt[i] == 0x45 and bt_rt[i+1] == 0x43:
if bt_be[i] != 0x45 or bt_be[i+1] != 0x43:
# runtime has "EC", beetle doesn't
addr = base + i
ctx_lo = max(0, i-4)
ctx_hi = min(len(bt_rt), i+8)
rt_ctx = ' '.join(f'{x:02x}' for x in bt_rt[ctx_lo:ctx_hi])
be_ctx = ' '.join(f'{x:02x}' for x in bt_be[ctx_lo:ctx_hi])
print(f" 0x{addr:08X} RT: {rt_ctx}")
print(f" BE: {be_ctx}")
except Exception as e:
print(f" err at 0x{base:08X}: {e}")
break