Skip to content

Commit 16789a5

Browse files
committed
Memory watch bug fix
Fixed the game crashing when attempting to read from memory addresses beyond 0x817FFFFF. The menu will now automatically adjust the address to prevent the crash.
1 parent 2620149 commit 16789a5

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

ttyd-tools/rel/include/memorywatch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ void addMemoryWatch(int32_t slot);
1313
void deleteWatch(int32_t slot);
1414
uint32_t adjustWatchValueControls(int32_t slot);
1515
void adjustWatchTempValueAndBounds(int32_t slot, uint32_t highestDigit, int32_t valueChangedBy);
16+
void *fixBaseAddress(int32_t slot, void *address);
1617

1718
}

ttyd-tools/rel/source/memorywatch.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,10 @@ uint32_t adjustWatchValueControls(int32_t slot)
508508
case 0:
509509
{
510510
// Modifying the address
511-
MemoryWatch[slot].Address = MemoryWatchSecondaryValue;
511+
// Make sure the addresses being read does not exceed 0x817FFFFF
512+
MemoryWatch[slot].Address = reinterpret_cast<uint32_t>(
513+
fixBaseAddress(slot, reinterpret_cast<void *>(MemoryWatchSecondaryValue)));
514+
512515
MenuSelectionStates = 0;
513516

514517
FrameCounter = 1;
@@ -591,7 +594,10 @@ void adjustWatchTempValueAndBounds(int32_t slot, uint32_t highestDigit, int32_t
591594
case 0:
592595
{
593596
// Modifying the address
594-
UpperBoundUnsigned = 0x817FFFFF;
597+
// Make sure the upper bound does not exceed 0x817FFFFF
598+
UpperBoundUnsigned = reinterpret_cast<uint32_t>(
599+
fixBaseAddress(slot, reinterpret_cast<void *>(0x817FFFFF)));
600+
595601
LowerBoundUnsigned = 0x80000000;
596602

597603
if (tempMemoryWatchSecondaryValue > UpperBoundUnsigned)
@@ -635,4 +641,49 @@ void adjustWatchTempValueAndBounds(int32_t slot, uint32_t highestDigit, int32_t
635641
}
636642
}
637643

644+
void *fixBaseAddress(int32_t slot, void *address)
645+
{
646+
uint32_t CurrentTypeSize;
647+
switch (MemoryWatch[slot].Type)
648+
{
649+
case time:
650+
case s64:
651+
case u64:
652+
case f64:
653+
{
654+
CurrentTypeSize = 8;
655+
break;
656+
}
657+
case s32:
658+
case u32:
659+
case f32:
660+
{
661+
CurrentTypeSize = 4;
662+
break;
663+
}
664+
case s16:
665+
case u16:
666+
{
667+
CurrentTypeSize = 2;
668+
break;
669+
}
670+
case s8:
671+
case u8:
672+
default:
673+
{
674+
CurrentTypeSize = 1;
675+
break;
676+
}
677+
}
678+
679+
// Make sure the address does not exceed 0x817FFFFF
680+
uint32_t tempAddress = reinterpret_cast<uint32_t>(address);
681+
while ((tempAddress + CurrentTypeSize - 1) >= 0x81800000)
682+
{
683+
tempAddress--;
684+
}
685+
686+
return reinterpret_cast<void *>(tempAddress);
687+
}
688+
638689
}

ttyd-tools/rel/source/menufunctions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "menufunctions.h"
22
#include "global.h"
33
#include "commonfunctions.h"
4+
#include "memorywatch.h"
45
#include "codes.h"
56
#include "items.h"
67

@@ -1185,6 +1186,12 @@ uint32_t memoryAddressTypeButtonControls()
11851186
{
11861187
uint32_t tempMenuSelectedOption = MenuSelectedOption;
11871188
MemoryWatch[tempMenuSelectedOption].Type = tempSecondaryMenuOption;
1189+
1190+
// Make sure the addresses being read does not exceed 0x817FFFFF
1191+
MemoryWatch[tempMenuSelectedOption].Address = reinterpret_cast<uint32_t>(
1192+
fixBaseAddress(tempMenuSelectedOption, reinterpret_cast<void *>(
1193+
MemoryWatch[tempMenuSelectedOption].Address)));
1194+
11881195
SelectedOption = 0;
11891196

11901197
// Adjust the hex setting if necessary

0 commit comments

Comments
 (0)