Skip to content

Commit e8f6d38

Browse files
committed
Add support for uncached memory for pointers and memory editor
This mainly applies to memory watches and the memory editor. Several other changes are included to account for this change: 1. checkIfPointerIsValid now checks if the pointer passed in is in the cached or uncached memory, and returns a value to specify which. It still returns 0 if the pointer is invalid, so the function return value can still be used like a bool. 2. The upper and lower bounds are no longer applied when working with pointers for memory watches and the memory editor. For memory watches, trying to use an invalid address will result in nothing happening, and the window for selecting the address will not close. 3. The memory editor now allows for scrolling up/down into invalid memory, as the previous restriction on this is much harder to implement when allowing to move to the uncached memory since it is not directly next to the cached memory. Along with these changes, in main.cpp, backtraceScreenIncrementYPos was modified slightly and moved above jumpOnWater, as they are ordered this way in assembly.h.
1 parent f0874b2 commit e8f6d38

File tree

6 files changed

+101
-82
lines changed

6 files changed

+101
-82
lines changed

ttyd-tools/rel/include/commonfunctions.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
namespace mod {
1212

13+
enum PointerVerificationType : uint32_t
14+
{
15+
PTR_INVALID = 0,
16+
PTR_CACHED,
17+
PTR_UNCACHED,
18+
};
19+
1320
extern "C" {
1421

1522
bool checkButtonCombo(uint32_t combo);
@@ -49,7 +56,7 @@ uint32_t getCurrentPitFloor();
4956
void setPitFloor(int32_t floor);
5057
uint32_t getCurrentFPS();
5158
void clearGSWFsRange(uint32_t lowerBound, uint32_t upperBound);
52-
bool checkIfPointerIsValid(const void *ptr);
59+
uint32_t checkIfPointerIsValid(const void *ptr);
5360
void *getLastPointerFromPath(void *address, int32_t *offset, uint32_t offsetAmount);
5461

5562
}

ttyd-tools/rel/include/memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void addMemoryWatch(int32_t slot);
1414
void deleteWatch(int32_t slot);
1515
void duplicateWatch(int32_t currentSlot, int32_t emptySlot);
1616
uint32_t adjustWatchValueControls(int32_t slot);
17-
void adjustWatchTempValueAndBounds(int32_t slot, uint32_t highestDigit, int32_t valueChangedBy);
17+
void adjustWatchTempValueAndBounds(/* int32_t slot, */ uint32_t highestDigit, int32_t valueChangedBy);
1818
void *fixBaseAddress(int32_t slot, void *address);
1919

2020

ttyd-tools/rel/source/commonfunctions.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,23 @@ void clearGSWFsRange(uint32_t lowerBound, uint32_t upperBound)
392392
}
393393
}
394394

395-
bool checkIfPointerIsValid(const void *ptr)
395+
uint32_t checkIfPointerIsValid(const void *ptr)
396396
{
397397
uint32_t ptrRaw = reinterpret_cast<uint32_t>(ptr);
398-
return ((ptrRaw >= 0x80000000) && (ptrRaw < 0x81800000));
398+
399+
// Cached memory
400+
if ((ptrRaw >= 0x80000000) && (ptrRaw < 0x81800000))
401+
{
402+
return PTR_CACHED;
403+
}
404+
405+
// Unached memory
406+
if ((ptrRaw >= 0xC0000000) && (ptrRaw < 0xC1800000))
407+
{
408+
return PTR_UNCACHED;
409+
}
410+
411+
return PTR_INVALID;
399412
}
400413

401414
void *getLastPointerFromPath(void *address, int32_t *offset, uint32_t offsetAmount)

ttyd-tools/rel/source/main.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,33 +241,20 @@ const char *replaceJumpFallAnim(char *jumpFallString)
241241
return jumpFallString;
242242
}
243243

244-
void *jumpOnWater(void *ptr)
245-
{
246-
// Allow jumping on water if the Bridge Skip display is active
247-
if (Displays[BRIDGE_SKIP])
248-
{
249-
return nullptr;
250-
}
251-
else
252-
{
253-
// Return the original value
254-
return ptr;
255-
}
256-
}
257-
258244
int32_t backtraceScreenIncrementYPos()
259245
{
246+
constexpr int32_t kPosYIncrementAmount = 50;
260247
static int16_t IncrementCount = 0;
261248
int32_t tempIncrementCount = IncrementCount;
262249

263-
int32_t TextPosY = tempIncrementCount + 50;
250+
int32_t TextPosY = tempIncrementCount + kPosYIncrementAmount;
264251
tempIncrementCount--;
265252

266253
if (tempIncrementCount < -960)
267254
{
268255
// Simulate incrementing exactly once to match the US/PAL code
269256
IncrementCount = -1;
270-
return 50;
257+
return kPosYIncrementAmount;
271258
}
272259
else
273260
{
@@ -276,6 +263,20 @@ int32_t backtraceScreenIncrementYPos()
276263
}
277264
}
278265

266+
void *jumpOnWater(void *ptr)
267+
{
268+
// Allow jumping on water if the Bridge Skip display is active
269+
if (Displays[BRIDGE_SKIP])
270+
{
271+
return nullptr;
272+
}
273+
else
274+
{
275+
// Return the original value
276+
return ptr;
277+
}
278+
}
279+
279280
void displayTitleScreenAndFileSelectScreenInfo()
280281
{
281282
if (checkForSpecificSeq(ttyd::seqdrv::SeqIndex::kTitle))

ttyd-tools/rel/source/memory.cpp

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const char *getAddressStringFromOffsets(int32_t slot, uint32_t maxOffset)
8282
return "???";
8383
}
8484

85-
// Make sure the address being read does not exceed 0x817FFFFF
85+
// Make sure the address being read does not exceed 0x817FFFFF/0xC17FFFFF
8686
Address = reinterpret_cast<uint32_t>(fixBaseAddress(
8787
slot, reinterpret_cast<void *>(Address)));
8888

@@ -107,7 +107,7 @@ const char *getValueString(int32_t slot)
107107
return "???";
108108
}
109109

110-
// Make sure the address being read does not exceed 0x817FFFFF
110+
// Make sure the address being read does not exceed 0x817FFFFF/0xC17FFFFF
111111
Address = reinterpret_cast<uint32_t>(fixBaseAddress(
112112
slot, reinterpret_cast<void *>(Address)));
113113

@@ -433,7 +433,7 @@ uint32_t adjustWatchValueControls(int32_t slot)
433433
IncrementAmount = -1;
434434
}
435435

436-
adjustWatchTempValueAndBounds(slot, HighestDigit, IncrementAmount);
436+
adjustWatchTempValueAndBounds(HighestDigit, IncrementAmount);
437437
MemoryWatchAdjustableValueMenu.WaitFramesToPerformIncrement = 0;
438438
return Button;
439439
}
@@ -498,15 +498,15 @@ uint32_t adjustWatchValueControls(int32_t slot)
498498
case DPADDOWN:
499499
{
500500
// Decrement the current value for the current slot
501-
adjustWatchTempValueAndBounds(slot, HighestDigit, -1);
501+
adjustWatchTempValueAndBounds(HighestDigit, -1);
502502

503503
MenuVar.FrameCounter = 1;
504504
return Button;
505505
}
506506
case DPADUP:
507507
{
508508
// Increment the current value for the current slot
509-
adjustWatchTempValueAndBounds(slot, HighestDigit, 1);
509+
adjustWatchTempValueAndBounds(HighestDigit, 1);
510510

511511
MenuVar.FrameCounter = 1;
512512
return Button;
@@ -522,14 +522,22 @@ uint32_t adjustWatchValueControls(int32_t slot)
522522
case 0:
523523
{
524524
// Modifying the address
525-
// Make sure the address being read does not exceed 0x817FFFFF
526-
MemoryWatch[slot].Address = reinterpret_cast<uint32_t>(
527-
fixBaseAddress(slot, reinterpret_cast<void *>(MenuVar.MenuSecondaryValueUnsigned)));
528-
529-
MenuVar.MenuSelectionStates = 0;
525+
// Make sure the address is valid
526+
uint32_t tempMenuSecondaryValueUnsigned = MenuVar.MenuSecondaryValueUnsigned;
527+
if (checkIfPointerIsValid(reinterpret_cast<void *>(tempMenuSecondaryValueUnsigned)))
528+
{
529+
MemoryWatch[slot].Address = tempMenuSecondaryValueUnsigned;
530+
531+
MenuVar.MenuSelectionStates = 0;
530532

531-
MenuVar.FrameCounter = 1;
532-
return Button;
533+
MenuVar.FrameCounter = 1;
534+
return Button;
535+
}
536+
else
537+
{
538+
// Tried to use an invalid address, so do nothing
539+
return 0;
540+
}
533541
}
534542
default:
535543
{
@@ -584,7 +592,7 @@ uint32_t adjustWatchValueControls(int32_t slot)
584592
}
585593
}
586594

587-
void adjustWatchTempValueAndBounds(int32_t slot, uint32_t highestDigit, int32_t valueChangedBy)
595+
void adjustWatchTempValueAndBounds(uint32_t highestDigit, int32_t valueChangedBy)
588596
{
589597
for (uint32_t i = 0; i < (highestDigit - MenuVar.SecondaryMenuOption - 1); i++)
590598
{
@@ -598,8 +606,8 @@ void adjustWatchTempValueAndBounds(int32_t slot, uint32_t highestDigit, int32_t
598606
{
599607
case MEMORY_WATCH_CHANGE_ADDRESS:
600608
{
601-
uint32_t UpperBoundUnsigned;
602-
uint32_t LowerBoundUnsigned;
609+
// uint32_t UpperBoundUnsigned;
610+
// uint32_t LowerBoundUnsigned;
603611
int32_t UpperBoundSigned;
604612
int32_t LowerBoundSigned;
605613

@@ -608,22 +616,7 @@ void adjustWatchTempValueAndBounds(int32_t slot, uint32_t highestDigit, int32_t
608616
case 0:
609617
{
610618
// Modifying the address
611-
// Make sure the upper bound does not exceed 0x817FFFFF
612-
UpperBoundUnsigned = reinterpret_cast<uint32_t>(
613-
fixBaseAddress(slot, reinterpret_cast<void *>(0x817FFFFF)));
614-
615-
LowerBoundUnsigned = 0x80000000;
616-
617-
if (tempMenuSecondaryValueUnsigned > UpperBoundUnsigned)
618-
{
619-
// Loop to the beginning
620-
MenuVar.MenuSecondaryValueUnsigned = LowerBoundUnsigned;
621-
}
622-
else if (tempMenuSecondaryValueUnsigned < LowerBoundUnsigned)
623-
{
624-
// Loop to the end
625-
MenuVar.MenuSecondaryValueUnsigned = UpperBoundUnsigned;
626-
}
619+
// Nothing needs to be done for this
627620
break;
628621
}
629622
default:
@@ -690,9 +683,21 @@ void *fixBaseAddress(int32_t slot, void *address)
690683
}
691684
}
692685

693-
// Make sure the address does not exceed 0x817FFFFF
686+
// Make sure the address does not exceed 0x817FFFFF/0xC17FFFFF
694687
uint32_t tempAddress = reinterpret_cast<uint32_t>(address);
695-
while ((tempAddress + CurrentTypeSize - 1) >= 0x81800000)
688+
689+
// Arbitrary check for cached memory
690+
uint32_t AddressCap;
691+
if (tempAddress < 0x90000000)
692+
{
693+
AddressCap = 0x81800000;
694+
}
695+
else
696+
{
697+
AddressCap = 0xC1800000;
698+
}
699+
700+
while ((tempAddress + CurrentTypeSize - 1) >= AddressCap)
696701
{
697702
tempAddress--;
698703
}
@@ -1271,13 +1276,9 @@ uint32_t memoryEditorButtonControls()
12711276

12721277
if (RowCheck)
12731278
{
1274-
// Only move down if there's at least one valid byte under the last row
1275-
if (checkIfPointerIsValid(CurrentAddress + EDITOR_MAX_NUM_BYTES_DISPLAYED))
1276-
{
1277-
// Move down one row
1278-
// Don't set CurrentEditorMenuOption, as it's already correct
1279-
MemoryEditor.CurrentAddress = CurrentAddress + EDITOR_BYTES_PER_ROW;
1280-
}
1279+
// Move down one row
1280+
// Don't set CurrentEditorMenuOption, as it's already correct
1281+
MemoryEditor.CurrentAddress = CurrentAddress + EDITOR_BYTES_PER_ROW;
12811282
}
12821283
else
12831284
{
@@ -1340,13 +1341,9 @@ uint32_t memoryEditorButtonControls()
13401341
// Check to see if moving up goes to the previous row
13411342
if (CurrentEditorMenuOption < 0)
13421343
{
1343-
// Only move up if there's at least one valid byte above the first row
1344-
if (checkIfPointerIsValid(CurrentAddress - 0x1))
1345-
{
1346-
// Move up one row
1347-
// Don't set CurrentEditorMenuOption, as it's already correct
1348-
MemoryEditor.CurrentAddress = CurrentAddress - EDITOR_BYTES_PER_ROW;
1349-
}
1344+
// Move up one row
1345+
// Don't set CurrentEditorMenuOption, as it's already correct
1346+
MemoryEditor.CurrentAddress = CurrentAddress - EDITOR_BYTES_PER_ROW;
13501347
}
13511348
else
13521349
{
@@ -1450,6 +1447,14 @@ uint32_t memoryEditorButtonControls()
14501447
uint32_t Start = reinterpret_cast<uint32_t>(tempAddress);
14511448
uint32_t End = Start + NumBytesBeingEdited;
14521449

1450+
// Arbitrary check for cached memory
1451+
if (checkIfPointerIsValid(tempAddress) == PTR_UNCACHED)
1452+
{
1453+
// Only need to clear cached memory, so adjust the start and end to be in cached memory
1454+
Start -= 0x40000000;
1455+
End -= 0x40000000;
1456+
}
1457+
14531458
if (Start < 0x80000000)
14541459
{
14551460
Start = 0x80000000;

ttyd-tools/rel/source/menufunctions.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,8 @@ void getUpperAndLowerBounds(int32_t arrayOut[2], uint32_t currentMenu)
739739
{
740740
if (MemoryEditor.CurrentEditorMenuOption == EDITOR_HEADER_CHANGE_ADDRESS)
741741
{
742-
LowerBound = 0x80000000;
743-
UpperBound = 0x817FFFFF;
742+
// LowerBound = 0;
743+
UpperBound = 0xFFFFFFFF;
744744
}
745745
else // Changing the amount of bytes to modify
746746
{
@@ -1776,7 +1776,7 @@ uint32_t memoryAddressTypeButtonControls()
17761776
uint32_t tempMenuSelectedOption = MenuVar.MenuSelectedOption;
17771777
MemoryWatch[tempMenuSelectedOption].Type = tempSecondaryMenuOption;
17781778

1779-
// Make sure the address being read does not exceed 0x817FFFFF
1779+
// Make sure the address being read does not exceed 0x817FFFFF/0xC17FFFFF
17801780
MemoryWatch[tempMenuSelectedOption].Address = reinterpret_cast<uint32_t>(
17811781
fixBaseAddress(tempMenuSelectedOption, reinterpret_cast<void *>(
17821782
MemoryWatch[tempMenuSelectedOption].Address)));
@@ -2267,7 +2267,7 @@ void adjustMenuItemBoundsMain(int32_t valueChangedBy, int32_t lowerBound, int32_
22672267
}
22682268
}
22692269

2270-
void adjustMenuItemBoundsMainUnsigned(int32_t valueChangedBy, uint32_t lowerBound, uint32_t upperBound)
2270+
/* void adjustMenuItemBoundsMainUnsigned(int32_t valueChangedBy, uint32_t lowerBound, uint32_t upperBound)
22712271
{
22722272
int64_t tempMenuSecondaryValue = MenuVar.MenuSecondaryValueUnsigned + valueChangedBy;
22732273
MenuVar.MenuSecondaryValueUnsigned = static_cast<uint32_t>(tempMenuSecondaryValue);
@@ -2307,7 +2307,7 @@ void adjustMenuItemBoundsMainUnsigned(int32_t valueChangedBy, uint32_t lowerBoun
23072307
MenuVar.MenuSecondaryValueUnsigned = upperBound;
23082308
}
23092309
}
2310-
}
2310+
} */
23112311

23122312
void adjustMenuItemBounds(int32_t valueChangedBy, uint32_t currentMenu)
23132313
{
@@ -2368,14 +2368,8 @@ void adjustMenuItemBounds(int32_t valueChangedBy, uint32_t currentMenu)
23682368
if ((currentMenu == MEMORY_EDITOR_MENU) &&
23692369
(MemoryEditor.CurrentEditorMenuOption == EDITOR_HEADER_CHANGE_ADDRESS))
23702370
{
2371-
// Handle everything as unsigned
2372-
int32_t UpperAndLowerBounds[2];
2373-
getUpperAndLowerBounds(UpperAndLowerBounds, currentMenu);
2374-
2375-
uint32_t LowerBound = static_cast<uint32_t>(UpperAndLowerBounds[0]);
2376-
uint32_t UpperBound = static_cast<uint32_t>(UpperAndLowerBounds[1]);
2377-
2378-
adjustMenuItemBoundsMainUnsigned(valueChangedBy, LowerBound, UpperBound);
2371+
// No adjustments are needed, so write the new value immediately and return
2372+
MenuVar.MenuSecondaryValueUnsigned += valueChangedBy;
23792373
return;
23802374
}
23812375
break;
@@ -2395,8 +2389,7 @@ void adjustMenuItemBounds(int32_t valueChangedBy, uint32_t currentMenu)
23952389
adjustMenuItemBoundsMain(valueChangedBy, LowerBound, UpperBound);
23962390
}
23972391

2398-
void adjustAddByIdValue(int32_t value, uint32_t currentMenu,
2399-
bool handleAsHex, bool handleAsUnsigned)
2392+
void adjustAddByIdValue(int32_t value, uint32_t currentMenu, bool handleAsHex, bool handleAsUnsigned)
24002393
{
24012394
int32_t multiplyAmount;
24022395
if (handleAsHex)

0 commit comments

Comments
 (0)