--- a/src/huc6280_opcodes.cpp 2026-10-21 14:13:49.064624800 +0900 +++ b/src/huc6280_opcodes.cpp 2026-10-21 14:14:00.185369800 +0900 @@ -20,6 +20,18 @@ #include "huc6280.h" #include "memory.h" +// VERIFICATION-ONLY pre-write guard for ST0/ST1/ST2 (Issue #365 / #173). +// NOT upstream code. Null hook = upstream behaviour (every ST writes). +// The guard runs BEFORE WriteFromCPU, so a refused store has no VDC side effect. +// See tests/integration/patches/README.md for the patch and its upstream pin. +bool (*gg_verify_st_write_guard)(void* context, unsigned st_index, unsigned value) = nullptr; +void* gg_verify_st_write_context = nullptr; + +static bool GG_VerifyST_WriteAllowed(unsigned st_index, unsigned value) +{ + return !gg_verify_st_write_guard || gg_verify_st_write_guard(gg_verify_st_write_context, st_index, value); +} + void HuC6280::OPCode0x00() { // BRK @@ -43,7 +55,8 @@ // ST0 #nn u8 value = ImmediateAddressing(); ClockCountedCycles(2); - m_huc6202->WriteFromCPU(0, value); + if (GG_VerifyST_WriteAllowed(0, value)) + m_huc6202->WriteFromCPU(0, value); ClockCountedCycles(1); } @@ -141,7 +154,8 @@ // ST1 #nn u8 value = ImmediateAddressing(); ClockCountedCycles(2); - m_huc6202->WriteFromCPU(2, value); + if (GG_VerifyST_WriteAllowed(2, value)) + m_huc6202->WriteFromCPU(2, value); ClockCountedCycles(1); } @@ -246,7 +260,8 @@ // ST2 #nn u8 value = ImmediateAddressing(); ClockCountedCycles(2); - m_huc6202->WriteFromCPU(3, value); + if (GG_VerifyST_WriteAllowed(3, value)) + m_huc6202->WriteFromCPU(3, value); ClockCountedCycles(1); }