Skip to content

Commit

Permalink
- Allocate from high arena for BS2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Jan 26, 2024
1 parent 2214201 commit 1d90e3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
22 changes: 14 additions & 8 deletions cube/swiss/source/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,22 @@ void *installPatch(int patchId) {

void *installPatch2(const void *patch, u32 patchSize) {
void *patchLocation = NULL;
if (top_addr == 0x81800000)
top_addr -= 8;
top_addr -= patchSize;
top_addr &= patch ? ~3 : ~31;
patchLocation = Calc_Address(NULL, PATCH_OTHER, top_addr);
void *addr;
if (!top_addr) {
addr = SYS_AllocArenaMemHi(patchSize, patch ? 4 : 32);
} else {
if (top_addr == 0x81800000)
top_addr -= 8;
top_addr -= patchSize;
top_addr &= patch ? ~3 : ~31;
addr = (void *)top_addr;
}
patchLocation = Calc_Address(NULL, PATCH_OTHER, (u32)addr);
if (patch) memcpy(patchLocation, patch, patchSize);
else memset(patchLocation, 0, patchSize);
DCFlushRange(patchLocation, patchSize);
ICInvalidateRange(patchLocation, patchSize);
return (void *)top_addr;
return addr;
}

// See patchIds enum in patcher.h
Expand All @@ -139,7 +145,7 @@ void setTopAddr(u32 addr) {
top_addr = addr & ~3;
int patchId;
for (patchId = 0; patchId < PATCHES_MAX; patchId++)
if (patch_locations[patchId] < (void *)top_addr || !top_addr)
if (patch_locations[patchId] < (void *)top_addr)
patch_locations[patchId] = NULL;
}

Expand Down Expand Up @@ -16444,7 +16450,7 @@ void *Calc_Address(void *data, int dataType, u32 properAddress) {
if(properAddress >= 0x80000000 && properAddress < 0x80003100) {
return VAR_AREA+properAddress-0x80000000;
}
else if(properAddress >= 0x81700000 && properAddress < 0x81800000) {
else if(properAddress >= (u32)SYS_GetArenaHi() && properAddress < 0x81800000) {
return (void*)properAddress;
}
return NULL;
Expand Down
11 changes: 9 additions & 2 deletions cube/swiss/source/swiss.c
Original file line number Diff line number Diff line change
Expand Up @@ -1145,11 +1145,16 @@ void load_app(ExecutableFile *fileToPatch)
}

if(getTopAddr() == topAddr) {
setTopAddr(HI_RESERVE);
if(type == PATCH_BS2) setTopAddr(0);
else setTopAddr(HI_RESERVE);
}
if(fileToPatch == NULL || fileToPatch->patchFile == NULL) {
Patch_ExecutableFile(&buffer, &sizeToRead, gameID, type);
}
if(getTopAddr() == 0) {
setTopAddr(HI_RESERVE);
*(vu32*)(VAR_AREA+0x0034) = (u32)SYS_GetArenaHi() & ~31;
}

// See if the combination of our patches has exhausted our play area.
if(!install_code(0)) {
Expand Down Expand Up @@ -2077,7 +2082,8 @@ void load_game() {
*(vu8*)VAR_CARD_B_ID = 0x00;

if(getTopAddr() == 0x81800000) {
setTopAddr(HI_RESERVE);
if(fileToPatch->type == PATCH_BS2) setTopAddr(0);
else setTopAddr(HI_RESERVE);
}
// Call the special setup for each device (e.g. SD will set the sector(s))
if(!devices[DEVICE_CUR]->setupFile(&curFile, disc2File, filesToPatch, numToPatch)) {
Expand All @@ -2101,6 +2107,7 @@ void load_game() {
devices[DEVICE_PATCHES] = NULL;
}
free(filesToPatch);
setTopAddr(0x81800000);
setTopAddr(0);
fail:
gameID_unset();
Expand Down

0 comments on commit 1d90e3d

Please sign in to comment.