From 4e83a497dfddeff730607989b3c377d07636d5a6 Mon Sep 17 00:00:00 2001 From: Snesrev Date: Mon, 6 Mar 2023 18:55:05 +0100 Subject: [PATCH] Orig game reads uninitialized memory in xray --- src/sm_91.c | 3 +++ src/sm_cpu_infra.c | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/sm_91.c b/src/sm_91.c index 2528bf7..c6cb450 100644 --- a/src/sm_91.c +++ b/src/sm_91.c @@ -1985,6 +1985,9 @@ void Xray_SetupStage4_Func1(void) { // 0x91CCF1 } void Xray_SetupStage4_Func2(void) { // 0x91CD42 + // bug: passing 0xffff to this function is invalid and will read invalid memory. + if (R34 == 0) + return; unsigned int v2; int16 v3; unsigned int v7; diff --git a/src/sm_cpu_infra.c b/src/sm_cpu_infra.c index b2496a4..fbefe7a 100644 --- a/src/sm_cpu_infra.c +++ b/src/sm_cpu_infra.c @@ -282,6 +282,10 @@ void PatchBugs(uint32 mode, uint32 addr) { } else if (FixBugHook(0xA7CEB2)) { // Phantoon_Main forgots to reload x g_cpu->x = cur_enemy_index; + } else if (FixBugHook(0x91CD44)) { + // Xray_SetupStage4_Func2 passes a bad value to Xray_HandleXrayedBlock + if (g_cpu->x == 0) + g_cpu->pc = 0xCD52; } }