Skip to content

Commit

Permalink
Revert back to IPC Sync when reading ROM from SD card
Browse files Browse the repository at this point in the history
Should fix occasional freezing in Pokemon B&W 1&2
  • Loading branch information
RocketRobz committed Dec 24, 2024
1 parent 07caa07 commit 2fa79d9
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 93 deletions.
2 changes: 1 addition & 1 deletion retail/bootloaderi/include/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ typedef struct patchOffsetCacheContents {
u32 ramClearChecked;
u32* ramClearIOffset;
u32* ramClearI2Offset;
u32* swiHaltOffset;
u32* swiHaltOffset; // Unused, comment out when updating patchOffsetCacheContents
u32* a7Swi12Offset;
u16* a7Swi24Offset;
u16* a7Swi25Offset;
Expand Down
8 changes: 4 additions & 4 deletions retail/bootloaderi/source/arm7/find_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ static const u32 relocateStartSignature5Alt2[1] = {0x02FFFFFA};
static const u32 nextFunctiontSignature[1] = {0xE92D4000};
static const u32 relocateValidateSignature[1] = {0x400010C};

static const u32 swiHaltSignature1[1] = {0xE59FC004};
/* static const u32 swiHaltSignature1[1] = {0xE59FC004};
static const u32 swiHaltSignature2[1] = {0xE59FC000};
static const u16 swiHaltCmpSignature[1] = {0x2800};
static const u16 swiHaltMovSignature[1] = {0x200C};
static const u32 swiHaltConstSignature[1] = {0x4000004};
static const u32 swiHaltConstSignatureAlt[1] = {0x4000208};
static const u32 swiHaltConstSignatureAlt[1] = {0x4000208}; */

static const u32 swi12Signature[1] = {0x4770DF12}; // LZ77UnCompReadByCallbackWrite16bit
static const u16 swi24Signature[2] = {0xDF24,0x4770}; // SHA1_Init
Expand Down Expand Up @@ -325,7 +325,7 @@ bool a7GetReloc(const tNDSHeader* ndsHeader, const module_params_t* moduleParams
return true;
}

u32* findSwiHaltOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
/* u32* findSwiHaltOffset(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
dbg_printf("findSwiHaltOffset:\n");
u32* swiHaltOffset = NULL;
Expand Down Expand Up @@ -415,7 +415,7 @@ u16* findSwiHaltThumbOffset(const tNDSHeader* ndsHeader, const module_params_t*
dbg_printf("\n");
return (u16*)swiHaltOffset;
}
} */

u32* a7_findSwi12Offset(const tNDSHeader* ndsHeader) {
dbg_printf("findSwi12Offset:\n");
Expand Down
10 changes: 5 additions & 5 deletions retail/bootloaderi/source/arm7/hook_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ int hookNdsRetailArm7(
} else {*/
extern u32 iUncompressedSize;
extern bool hasVramWifiBinary;
extern u32 dataToPreloadAddr;
extern u32 dataToPreloadSize;
extern u32 dataToPreloadFrame;
// extern u32 dataToPreloadAddr;
// extern u32 dataToPreloadSize;
// extern u32 dataToPreloadFrame;
extern bool dataToPreloadFound(const tNDSHeader* ndsHeader);
const bool laterSdk = ((moduleParams->sdk_version >= 0x2008000 && moduleParams->sdk_version != 0x2012774) || moduleParams->sdk_version == 0x20029A8);

Expand Down Expand Up @@ -457,12 +457,12 @@ int hookNdsRetailArm7(
romOffset = ndsHeader->arm9overlaySource;
}
ce7->romLocation -= romOffset;
if (!ROMinRAM && dataToPreloadFound(ndsHeader) && dataToPreloadFrame) {
/* if (!ROMinRAM && dataToPreloadFound(ndsHeader) && dataToPreloadFrame) {
ce7->romPartLocation = romPartLocation;
ce7->romPartSrc = dataToPreloadAddr;
ce7->romPartSize = dataToPreloadSize;
ce7->romPartFrame = dataToPreloadFrame;
}
} */

extern u32 romMapLines;
extern u32 romMap[7][3];
Expand Down
6 changes: 3 additions & 3 deletions retail/bootloaderi/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ int hookNdsRetailArm9(
extern u32 romPaddingSize;
extern u32 dataToPreloadAddr;
extern u32 dataToPreloadSize;
extern u32 dataToPreloadFrame;
// extern u32 dataToPreloadFrame;
extern bool romLocationAdjust(const tNDSHeader* ndsHeader, const bool laterSdk, const bool dsiBios, u32* romLocation);
extern bool dataToPreloadFound(const tNDSHeader* ndsHeader);
const char* romTid = getRomTid(ndsHeader);
Expand Down Expand Up @@ -348,9 +348,9 @@ int hookNdsRetailArm9(
//ce9->romPartSrc[1] = dataToPreloadAddr[1];
ce9->romPartSize = dataToPreloadSize;
//ce9->romPartSize[1] = dataToPreloadSize[1];
if (dataToPreloadFrame) {
/* if (dataToPreloadFrame) {
ce9->valueBits |= b_waitForPreloadToFinish;
}
} */
}
if (runOverlayCheck && overlaysSize <= 0x700000) {
/*extern u8 gameOnFlashcard;
Expand Down
4 changes: 2 additions & 2 deletions retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,10 @@ static void loadROMPartIntoRAM(const tNDSHeader* ndsHeader, aFile* file) {
return;
}

if ((dataToPreloadFrame > 0) && !gameOnFlashcard && (!ROMsupportsDsiMode(ndsHeader) || !dsiModeConfirmed)) {
/* if ((dataToPreloadFrame > 0) && !gameOnFlashcard && (!ROMsupportsDsiMode(ndsHeader) || !dsiModeConfirmed)) {
dbg_printf("Part of ROM will pre-load into RAM\n");
return;
}
} */

for (int i = 0; i < romMapLines; i++) {
fileRead((char*)romMap[i][1], file, romMap[i][0], romMap[i][2]-romMap[i][1]);
Expand Down
6 changes: 3 additions & 3 deletions retail/bootloaderi/source/arm7/patch_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ u16* getOffsetFromBLThumb(const u16* blOffset) {

u32 vAddrOfRelocSrc = 0;
u32 relocDestAtSharedMem = 0;
u32 newSwiHaltAddr = 0;
/* u32 newSwiHaltAddr = 0;
// bool swiHaltPatched = false;
static void patchSwiHalt(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
Expand Down Expand Up @@ -164,7 +164,7 @@ static void patchSwiHalt(const cardengineArm7* ce7, const tNDSHeader* ndsHeader,
dbg_printf("swiHalt location : ");
dbg_hexa((u32)swiHaltOffset);
dbg_printf("\n\n");
}
} */

void patchScfgExt(const tNDSHeader* ndsHeader) {
if (ndsHeader->unitCode == 0) return;
Expand Down Expand Up @@ -796,7 +796,7 @@ u32 patchCardNdsArm7(
}*/
}

patchSwiHalt(ce7, ndsHeader, moduleParams);
// patchSwiHalt(ce7, ndsHeader, moduleParams);

if (strcmp(romTid, "UBRP") == 0) {
operaRamPatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern u32 newArm7binarySize;
extern u32 vAddrOfRelocSrc;
extern u32 relocDestAtSharedMem;
extern u32 newSwiHaltAddr;
// extern u32 newSwiHaltAddr;

//
// Subroutine function signatures ARM7
Expand Down Expand Up @@ -246,7 +246,7 @@ u32 savePatchUniversal(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, m
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");
tonccpy(eepromPageWrite, (u16*)ce7->patches->arm7FunctionsThumb->eepromPageWrite, 0x14);
newSwiHaltAddr = (u32)eepromPageWrite+0x14;
// newSwiHaltAddr = (u32)eepromPageWrite+0x14;

u16* eepromPageProgBranch = (u16*)((u32)EepromProgJump + 0x6);
dbg_printf("Eeprom page prog branch:\t");
Expand Down Expand Up @@ -285,10 +285,10 @@ u32 savePatchUniversal(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, m
*eepromRead = ce7->patches->arm7FunctionsDirect->eepromRead;

u32* eepromPageWrite = (u32*)((u32)EepromWriteJump + 0xA);
newSwiHaltAddr = *eepromPageWrite;
/* newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr -= 0x37F8000;
newSwiHaltAddr += vAddrOfRelocSrc;
newSwiHaltAddr--;
newSwiHaltAddr--; */
dbg_printf("Eeprom page write:\t");
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");
Expand Down Expand Up @@ -420,10 +420,10 @@ u32 savePatchInvertedThumb(const cardengineArm7* ce7, const tNDSHeader* ndsHeade
dbg_printf("\n");

u32* eepromPageWrite = (u32*)((u32)EepromWriteJump + 0xA);
newSwiHaltAddr = *eepromPageWrite;
/* newSwiHaltAddr = *eepromPageWrite;
newSwiHaltAddr -= 0x37F8000;
newSwiHaltAddr += vAddrOfRelocSrc;
newSwiHaltAddr--;
newSwiHaltAddr--; */
dbg_printf("Eeprom page write:\t");
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\t:\t");
Expand Down
4 changes: 2 additions & 2 deletions retail/bootloaderi/source/arm7/save_patches/save_patch_v5.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern u32 newArm7binarySize;
extern u32 vAddrOfRelocSrc;
extern u32 relocDestAtSharedMem;
extern u32 newSwiHaltAddr;
// extern u32 newSwiHaltAddr;

//
// Subroutine function signatures ARM7
Expand Down Expand Up @@ -93,7 +93,7 @@ u32 savePatchV5(const cardengineArm7* ce7, const tNDSHeader* ndsHeader, const u3
dbg_hexa((u32)eepromPageWrite);
dbg_printf("\n");
tonccpy(eepromPageWrite, (u16*)ce7->patches->arm7FunctionsThumb->eepromPageWrite, 0x14);
newSwiHaltAddr = (u32)eepromPageWrite+0x14;
// newSwiHaltAddr = (u32)eepromPageWrite+0x14;

u16* eepromPageProgBranch = (u16*)(JumpTableFunc + 0x24);
dbg_printf("Eeprom page prog branch:\t");
Expand Down
63 changes: 32 additions & 31 deletions retail/cardenginei/arm7/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -1255,17 +1255,21 @@ static void nandWrite(void) {
cardReadLED(false);
}*/

//static bool readOngoing = false;
static bool sdReadOngoing = false;
static bool ongoingIsDma = false;
static bool readOngoing = false;
//static bool sdReadOngoing = false;
//static bool ongoingIsDma = false;
//static int currentCmd=0, currentNdmaSlot=0;
//static int timeTillDmaLedOff = 0;

#ifndef TWLSDK
/* static bool start_cardRead_arm9(void) {
static bool start_cardRead_arm9(void) {
bool useApFixOverlays = false;
u32 src = sharedAddr[2];
u32 dst = sharedAddr[0];
u32 len = sharedAddr[1];
if (src >= 0x80000000) {
src -= 0x80000000;
useApFixOverlays = true;
}
#ifdef DEBUG
u32 marker = sharedAddr[3];

Expand All @@ -1287,16 +1291,15 @@ static bool ongoingIsDma = false;
dbg_hexa(marker);
#endif

//bool isDma = sharedAddr[3] == (0x0000000A & 0x0000000F);
bool isDma = true;
const bool isDma = (sharedAddr[3] == (vu32)0x025FFB09 || sharedAddr[3] == (vu32)0x025FFB0A);

//driveInitialize();
cardReadLED(true, isDma); // When a file is loading, turn on LED for card read indicator
#ifdef DEBUG
nocashMessage("fileRead romFile");
#endif
if ((dst % 4) == 0) {
if(!fileReadNonBLocking((char*)dst, romFile, src, len))
if(!fileReadNonBLocking((char*)dst, useApFixOverlays ? apFixOverlaysFile : romFile, src, len))
{
readOngoing = true;
return false;
Expand All @@ -1309,7 +1312,7 @@ static bool ongoingIsDma = false;
return true;
}
} else {
fileRead((char*)dst, romFile, src, len);
fileRead((char*)dst, useApFixOverlays ? apFixOverlaysFile : romFile, src, len);
cardReadLED(false, isDma); // After loading is done, turn off LED for card read indicator
return true;
}
Expand All @@ -1325,8 +1328,7 @@ static bool ongoingIsDma = false;
}

static bool resume_cardRead_arm9(void) {
//bool isDma = sharedAddr[3] == (0x0000000A & 0x0000000F);
bool isDma = true;
const bool isDma = (sharedAddr[3] == (vu32)0x025FFB09 || sharedAddr[3] == (vu32)0x025FFB0A);
if(resumeFileRead())
{
readOngoing = false;
Expand All @@ -1337,8 +1339,9 @@ static bool resume_cardRead_arm9(void) {
{
return false;
}
} */
}

#ifndef TWLSDK
/* static bool gsddFix(void) {
if (sharedAddr[4] != 0x44445347) {
return false;
Expand All @@ -1358,7 +1361,7 @@ static bool resume_cardRead_arm9(void) {
return true;
} */

bool romLocationAdjust(const tNDSHeader* ndsHeader, const bool laterSdk, const bool dsiBios, u32* romLocation) {
/* bool romLocationAdjust(const tNDSHeader* ndsHeader, const bool laterSdk, const bool dsiBios, u32* romLocation) {
const bool ntrType = (ndsHeader->unitCode == 0);
const u32 romLocationOld = *romLocation;
if (*romLocation == 0x0C3FC000) {
Expand All @@ -1382,9 +1385,6 @@ bool romLocationAdjust(const tNDSHeader* ndsHeader, const bool laterSdk, const b
} else if (*romLocation == 0x0CFFC000 && dsiBios) {
*romLocation += 0x4000;
}
/* if (*romLocation == (consoleModel > 0 ? 0x0E000000 : 0x0D000000)) {
*romLocation = sharedWramEnabled ? 0x036F8000 : 0x03700000;
} */
return (*romLocation != romLocationOld);
}
Expand Down Expand Up @@ -1434,9 +1434,10 @@ static void loadROMPartIntoRAM(void) {
}
leaveCriticalSection(oldIME);
}
} */
#endif

#ifdef UNUSED
static inline void sdmmcHandler(void) { // Unused
if (sdReadOngoing) {
if (my_sdmmc_sdcard_check_command(0x33C12)) {
Expand Down Expand Up @@ -1498,6 +1499,7 @@ static inline void sdmmcHandler(void) { // Unused
break; */
}
}
#endif

void runCardEngineCheck(void) {
// if (!(valueBits & b_runCardEngineCheck)) return;
Expand All @@ -1519,12 +1521,13 @@ void runCardEngineCheck(void) {
}*/

if (!(valueBits & gameOnFlashcard)) {
#ifndef TWLSDK
/* #ifndef TWLSDK
loadROMPartIntoRAM();
#endif
if (/* sharedAddr[3] == (vu32)0x020FF808 || sharedAddr[3] == (vu32)0x020FF80A || */ sharedAddr[3] == (vu32)0x025FFB08 || sharedAddr[3] == (vu32)0x025FFB0A) { // ARM9 Card Read
//if (!readOngoing ? start_cardRead_arm9() : resume_cardRead_arm9()) {
bool useApFixOverlays = false;
#endif */
if (/* sharedAddr[3] == (vu32)0x020FF808 || sharedAddr[3] == (vu32)0x020FF80A || */ sharedAddr[3] >= (vu32)0x025FFB08 && sharedAddr[3] <= (vu32)0x025FFB0A) { // ARM9 Card Read
const bool isDma = (sharedAddr[3] == (vu32)0x025FFB0A);
if (!readOngoing ? start_cardRead_arm9() : resume_cardRead_arm9()) {
/* bool useApFixOverlays = false;
u32 src = sharedAddr[2];
u32 dst = sharedAddr[0];
u32 len = sharedAddr[1];
Expand All @@ -1533,21 +1536,19 @@ void runCardEngineCheck(void) {
useApFixOverlays = true;
}
const bool isDma = (sharedAddr[3] == (vu32)0x025FFB0A);

// readOngoing = true;
if (lockMutex(&saveMutex)) {
cardReadLED(true, isDma); // When a file is loading, turn on LED for card read indicator
fileRead((char*)dst, useApFixOverlays ? apFixOverlaysFile : romFile, src, len);
cardReadLED(false, isDma); // After loading is done, turn off LED for card read indicator
unlockMutex(&saveMutex);
}
// readOngoing = false;
// readOngoing = false; */
sharedAddr[3] = 0;
}
if (isDma) {
IPC_SendSync(0x3);
}
//}
} /*else if (sharedAddr[3] == (vu32)0x026FFB0A) { // Card read DMA (Card data cache)
ndmaCopyWords(0, (u8*)sharedAddr[2], (u8*)(sharedAddr[0] >= 0x03000000 ? 0 : sharedAddr[0]), sharedAddr[1]);
sharedAddr[3] = 0;
Expand Down Expand Up @@ -1604,13 +1605,13 @@ void myIrqHandlerFIFO(void) {
return;
}

// runCardEngineCheck();
/* if (!(valueBits & gameOnFlashcard)) {
sdmmcHandler();
if (sdReadOngoing) {
runCardEngineCheck();
if (!(valueBits & gameOnFlashcard)) {
// sdmmcHandler();
if (readOngoing) {
sharedAddr[5] = 0x474E4950; // 'PING'
}
} */
}
}


Expand Down
Loading

0 comments on commit 2fa79d9

Please sign in to comment.