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

42 lines
1.8 KiB
Python

import sys
sys.path.insert(0, 'F:/Projects/psxrecomp-v4/tools/_options_session')
import _dbg
print(f"{'thing':>40} {'runtime':>12} {'beetle':>12}")
for label, addr, width in [
("scratchpad+0x1D4 (ptr)", 0x1F8001D4, 'w'),
("ptr+0x4C (full word w/ state)", 0, 'special_state'),
("0x8009B3A0+0x34", 0x8009B3D4, 'w'),
("0x8009B3A0+0x49 (byte at 0x49)", 0x8009B3E9, 'b'),
("0x8009EB58", 0x8009EB58, 'w'),
("frame count", -1, 'frame'),
]:
if width == 'frame':
rt = _dbg.call(4470, "ping", timeout=2).get('frame','?')
be = _dbg.call(4380, "ping", timeout=2).get('frame','?')
print(f" {label:>40} {rt:>12} {be:>12}")
continue
if width == 'special_state':
# state is halfword at ptr+0x4E
rt_ptr = _dbg.read_word(4470, 0x1F8001D4)
be_ptr = _dbg.read_word(4380, 0x1F8001D4)
rt_state_word = _dbg.read_word(4470, rt_ptr + 0x4C)
be_state_word = _dbg.read_word(4380, be_ptr + 0x4C)
rt_state = (rt_state_word >> 16) & 0xFFFF
be_state = (be_state_word >> 16) & 0xFFFF
print(f" {'state @ ptr+0x4E':>40} {rt_state:>12} {be_state:>12}")
continue
if width == 'w':
rt = _dbg.read_word(4470, addr)
be = _dbg.read_word(4380, addr)
mark = "" if rt == be else " *DIFF*"
print(f" {label:>40} 0x{rt:08X} 0x{be:08X}{mark}")
elif width == 'b':
rt = _dbg.read_word(4470, addr & ~3)
be = _dbg.read_word(4380, addr & ~3)
byte_off = addr & 3
rt_byte = (rt >> (byte_off * 8)) & 0xFF
be_byte = (be >> (byte_off * 8)) & 0xFF
mark = "" if rt_byte == be_byte else " *DIFF*"
print(f" {label:>40} 0x{rt_byte:02X}{' '} 0x{be_byte:02X}{' '}{mark}")