Skip to content

Commit

Permalink
Change some vars into volatile.
Browse files Browse the repository at this point in the history
  • Loading branch information
ApacheThunder committed Dec 12, 2024
1 parent de31f9c commit 1cf2bb9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 64 deletions.
90 changes: 45 additions & 45 deletions BootLoader/source/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,88 +498,88 @@ static void setMemoryAddress(const tNDSHeader* ndsHeader, u32 ChipID) {
if (ndsHeader->unitCode & BIT(1)) {
copyLoop((u32*)0x02FFFA80, (u32*)ndsHeader, 0x160); // Make a duplicate of DS header

*(u32*)(0x02FFA680) = 0x02FD4D80;
*(u32*)(0x02FFA684) = 0x00000000;
*(u32*)(0x02FFA688) = 0x00001980;
*(vu32*)(0x02FFA680) = 0x02FD4D80;
*(vu32*)(0x02FFA684) = 0x00000000;
*(vu32*)(0x02FFA688) = 0x00001980;

*(u32*)(0x02FFF00C) = 0x0000007F;
*(u32*)(0x02FFF010) = 0x550E25B8;
*(u32*)(0x02FFF014) = 0x02FF4000;
*(vu32*)(0x02FFF00C) = 0x0000007F;
*(vu32*)(0x02FFF010) = 0x550E25B8;
*(vu32*)(0x02FFF014) = 0x02FF4000;

// Set region flag
if (strncmp(getRomTid(ndsHeader)+3, "J", 1) == 0) {
*(u8*)(0x02FFFD70) = 0;
*(vu8*)(0x02FFFD70) = 0;
} else if (strncmp(getRomTid(ndsHeader)+3, "E", 1) == 0) {
*(u8*)(0x02FFFD70) = 1;
*(vu8*)(0x02FFFD70) = 1;
} else if (strncmp(getRomTid(ndsHeader)+3, "P", 1) == 0) {
*(u8*)(0x02FFFD70) = 2;
*(vu8*)(0x02FFFD70) = 2;
} else if (strncmp(getRomTid(ndsHeader)+3, "U", 1) == 0) {
*(u8*)(0x02FFFD70) = 3;
*(vu8*)(0x02FFFD70) = 3;
} else if (strncmp(getRomTid(ndsHeader)+3, "C", 1) == 0) {
*(u8*)(0x02FFFD70) = 4;
*(vu8*)(0x02FFFD70) = 4;
} else if (strncmp(getRomTid(ndsHeader)+3, "K", 1) == 0) {
*(u8*)(0x02FFFD70) = 5;
*(vu8*)(0x02FFFD70) = 5;
}
}

// Set memory values expected by loaded NDS
// from NitroHax, thanks to Chism
*((u32*)0x02FFF800) = ChipID; // CurrentCardID
*((vu32*)0x02FFF800) = ChipID; // CurrentCardID
// *((u32*)0x02FFF804) = ChipID; // Command10CardID // This does not result in correct v
*((u16*)0x02FFF808) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((u16*)0x02FFF80A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((u16*)0x02FFF850) = 0x5835;
*((vu16*)0x02FFF808) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((vu16*)0x02FFF80A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((vu16*)0x02FFF850) = 0x5835;
// Copies of above
*((u32*)0x02FFFC00) = ChipID; // CurrentCardID
*((vu32*)0x02FFFC00) = ChipID; // CurrentCardID
// *((u32*)0x02FFFC04) = ChipID; // Command10CardID
*((u16*)0x02FFFC08) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((u16*)0x02FFFC0A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((u16*)0x02FFFC10) = 0x5835;
*((u16*)0x02FFFC40) = 0x01; // Boot Indicator -- EXTREMELY IMPORTANT!!! Thanks to cReDiAr
*((vu16*)0x02FFFC08) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((vu16*)0x02FFFC0A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((vu16*)0x02FFFC10) = 0x5835;
*((vu16*)0x02FFFC40) = 0x01; // Boot Indicator -- EXTREMELY IMPORTANT!!! Thanks to cReDiAr
}

static void setMemoryAddressTWL(const tNDSHeader* ndsHeader, u32 ChipID) {
if (ndsHeader->unitCode & BIT(1)) {
copyLoop((u32*)0x027FFA80, (u32*)ndsHeader, 0x160); // Make a duplicate of DS header

*(u32*)(0x027FA680) = 0x02FD4D80;
*(u32*)(0x027FA684) = 0x00000000;
*(u32*)(0x027FA688) = 0x00001980;
*(vu32*)(0x027FA680) = 0x02FD4D80;
*(vu32*)(0x027FA684) = 0x00000000;
*(vu32*)(0x027FA688) = 0x00001980;

*(u32*)(0x027FF00C) = 0x0000007F;
*(u32*)(0x027FF010) = 0x550E25B8;
*(u32*)(0x027FF014) = 0x02FF4000;
*(vu32*)(0x027FF00C) = 0x0000007F;
*(vu32*)(0x027FF010) = 0x550E25B8;
*(vu32*)(0x027FF014) = 0x02FF4000;

// Set region flag
if (strncmp(getRomTid(ndsHeader)+3, "J", 1) == 0) {
*(u8*)(0x027FFD70) = 0;
*(vu8*)(0x027FFD70) = 0;
} else if (strncmp(getRomTid(ndsHeader)+3, "E", 1) == 0) {
*(u8*)(0x027FFD70) = 1;
*(vu8*)(0x027FFD70) = 1;
} else if (strncmp(getRomTid(ndsHeader)+3, "P", 1) == 0) {
*(u8*)(0x027FFD70) = 2;
*(vu8*)(0x027FFD70) = 2;
} else if (strncmp(getRomTid(ndsHeader)+3, "U", 1) == 0) {
*(u8*)(0x027FFD70) = 3;
*(vu8*)(0x027FFD70) = 3;
} else if (strncmp(getRomTid(ndsHeader)+3, "C", 1) == 0) {
*(u8*)(0x027FFD70) = 4;
*(vu8*)(0x027FFD70) = 4;
} else if (strncmp(getRomTid(ndsHeader)+3, "K", 1) == 0) {
*(u8*)(0x027FFD70) = 5;
*(vu8*)(0x027FFD70) = 5;
}
}

// Set memory values expected by loaded NDS
// from NitroHax, thanks to Chism
*((u32*)0x027FF800) = ChipID; // CurrentCardID
*((vu32*)0x027FF800) = ChipID; // CurrentCardID
// *((u32*)0x027FF804) = ChipID; // Command10CardID
*((u16*)0x027FF808) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((u16*)0x027FF80A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((u16*)0x027FF850) = 0x5835;
*((vu16*)0x027FF808) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((vu16*)0x027FF80A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((vu16*)0x027FF850) = 0x5835;
// Copies of above
*((u32*)0x027FFC00) = ChipID; // CurrentCardID
*((vu32*)0x027FFC00) = ChipID; // CurrentCardID
// *((u32*)0x027FFC04) = ChipID; // Command10CardID
*((u16*)0x027FFC08) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((u16*)0x027FFC0A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((u16*)0x027FFC10) = 0x5835;
*((u16*)0x027FFC40) = 0x01; // Boot Indicator -- EXTREMELY IMPORTANT!!! Thanks to cReDiAr
*((vu16*)0x027FFC08) = ndsHeader->headerCRC16; // Header Checksum, CRC-16 of [000h-15Dh]
*((vu16*)0x027FFC0A) = ndsHeader->secureCRC16; // Secure Area Checksum, CRC-16 of [ [20h]..7FFFh]
*((vu16*)0x027FFC10) = 0x5835;
*((vu16*)0x027FFC40) = 0x01; // Boot Indicator -- EXTREMELY IMPORTANT!!! Thanks to cReDiAr

tonccpy((u32*)0x027FC000, (u32*)TMP_HEADER, 0x1000);
tonccpy((u32*)0x027FF000, (u32*)NDS_HEADER_POKEMON, 0x170);
Expand Down Expand Up @@ -662,7 +662,7 @@ static u16 arm7_loadBinary (void) {
errorCode = cardInit((sNDSHeaderExt*)twlHeaderTemp, &chipID);
}*/

errorCode = cardInit((sNDSHeaderExt*)twlHeaderTemp, (u32*)InitialCartChipID);
errorCode = cardInit((sNDSHeaderExt*)twlHeaderTemp, (vu32*)InitialCartChipID);

if (errorCode)return errorCode;

Expand Down Expand Up @@ -755,9 +755,9 @@ void arm7_main (void) {
// if (twlCLK > 0) { REG_SCFG_CLK = 0x187; } else { REG_SCFG_CLK = 0x107; }
if (scfgUnlock == 0)REG_SCFG_EXT &= ~(1UL << 31); /// REG_SCFG_EXT |= BIT(18);

setMemoryAddress(ndsHeader, *(u32*)InitialCartChipID);
setMemoryAddress(ndsHeader, *(vu32*)InitialCartChipID);

if ((twlRAM > 0) && ((twlMode == 0) || (isTWLSRL == 0)))setMemoryAddressTWL(ndsHeader, *(u32*)InitialCartChipID);
if ((twlRAM > 0) && ((twlMode == 0) || (isTWLSRL == 0) || ((isTWLSRL == 0) && (twlMode > 0))))setMemoryAddressTWL(ndsHeader, *(vu32*)InitialCartChipID);

ipcSendState(ARM7_BOOTBIN);

Expand Down
6 changes: 3 additions & 3 deletions BootLoader/source/read_card.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ static void switchToTwlBlowfish(sNDSHeaderExt* ndsHeader) {
}


u16 cardInit (sNDSHeaderExt* ndsHeader, u32 *chipID) {
u16 cardInit (sNDSHeaderExt* ndsHeader, vu32* chipID) {
u32 portFlagsKey1, portFlagsSecRead;
normalChip = 0; // As defined by GBAtek, normal chip secure area is accessed in blocks of 0x200, other chip in blocks of 0x1000
u32* secureArea;
Expand All @@ -306,10 +306,10 @@ u16 cardInit (sNDSHeaderExt* ndsHeader, u32 *chipID) {
if (ndsHeader->headerCRC16 != swiCRC16(0xFFFF, (void*)ndsHeader, 0x15E))return ERR_HEAD_CRC;

// 1st Get ROM Chip ID
cardParamCommand (CARD_CMD_HEADER_CHIPID, 0, (ndsHeader->cardControl13 & (CARD_WR|CARD_nRESET|CARD_CLK_SLOW)) | CARD_ACTIVATE | CARD_BLK_SIZE(7), chipID, sizeof(u32));
cardParamCommand (CARD_CMD_HEADER_CHIPID, 0, (ndsHeader->cardControl13 & (CARD_WR|CARD_nRESET|CARD_CLK_SLOW)) | CARD_ACTIVATE | CARD_BLK_SIZE(7), (u32*)chipID, sizeof(u32));
// iCardId = cardReadID((ndsHeader->cardControl13 & (CARD_WR|CARD_nRESET|CARD_CLK_SLOW)) | CARD_ACTIVATE | CARD_BLK_SIZE(7));
// iCardId = cardReadID(CARD_CLK_SLOW);
// chipID = cardReadID(CARD_CLK_SLOW);
// *chipID = cardReadID(CARD_CLK_SLOW);
while (REG_ROMCTRL & CARD_BUSY);

// *chipID = iCardId;
Expand Down
2 changes: 1 addition & 1 deletion BootLoader/source/read_card.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ typedef struct {
} sNDSHeaderExt;

// u32 cardGetId();
u16 cardInit (sNDSHeaderExt* ndsHeader, u32 *chipID);
u16 cardInit (sNDSHeaderExt* ndsHeader, vu32* chipID);
void cardRead (sNDSHeaderExt* ndsHeader, u32 src, u32* dest, size_t size);
// u32 cardInitShort (sNDSHeaderExt* ndsHeader, u32* chipID);

Expand Down
2 changes: 1 addition & 1 deletion arm9/common/read_card.twl.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ u32 cardInit (sNDSHeaderExt* ndsHeader) {

iCardId = cardReadID(CARD_CLK_SLOW);
while(REG_ROMCTRL & CARD_BUSY)swiWaitForVBlank();
*(u32*)InitialCartChipID = iCardId;
*(vu32*)InitialCartChipID = iCardId;

normalChip = (iCardId & BIT(31)) != 0; // ROM chip ID MSB
nandChip = (iCardId & BIT(27)) != 0; // Card has a NAND chip
Expand Down
4 changes: 2 additions & 2 deletions arm9/hbmenu/hbmenu.twl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static int BrowserUI(tLauncherSettings launchdata) {
}
}
// tonccpy((void*)0x02FFDFF0, (void*)LAUNCH_DATA, 0x10);
launchdata.cachedChipID = *(u32*)InitialCartChipID;
launchdata.cachedChipID = *(vu32*)InitialCartChipID;
int err = runNdsFile(c_args[0], c_args.size(), &c_args[0], launchdata);
iprintf("Start failed. Error %i\n", err);
break;
Expand All @@ -216,7 +216,7 @@ static int BrowserUI(tLauncherSettings launchdata) {
DoWait(29); // Give launch soundfx time to finish if card Init already occured.
}
if (ntrHeader.unitCode & BIT(1))launchdata.isTWLSRL = 0x01;
launchdata.cachedChipID = *(u32*)InitialCartChipID;
launchdata.cachedChipID = *(vu32*)InitialCartChipID;
runLaunchEngine(launchdata);
}
return stop();
Expand Down
3 changes: 2 additions & 1 deletion arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ int main() {
scanKeys();
swiWaitForVBlank();

// Enable for testing in No$GBA
/*scfgunlock = true;
twlmode = true;
twlclk = true;
Expand Down Expand Up @@ -349,7 +350,7 @@ int main() {
}
break;
} else {
LaunchData.cachedChipID = *(u32*)InitialCartChipID;
LaunchData.cachedChipID = *(vu32*)InitialCartChipID;
runLaunchEngine(LaunchData);
break;
}
Expand Down
21 changes: 10 additions & 11 deletions ndsbootloader/source/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,13 @@ void resetMemory_ARM7(void) {
REG_POWERCNT = 1; //turn off power to stuffs
}

static void XMenuFix() {
static void XMenuFix(const tNDSHeader* ndsHeader) {
*((vu8*)0x02FFFF70) = 0x91;
if (twlmode > 0)*((vu8*)0x027FFF70) = 0x91;

if ((twlram > 0) && !(ndsHeader->unitCode & BIT(1)))*((vu8*)0x027FFF70) = 0x91;
}

static void setMemoryAddressTWL(const tNDSHeader* ndsHeader) {
if (ndsHeader->unitCode > 0) {
if (ndsHeader->unitCode & BIT(1)) {
// copyLoop((u32*)0x027FFA80, (u32*)ndsHeader, 0x160); // Make a duplicate of DS header
tonccpy((u32*)0x027FFA80, (u32*)ndsHeader, 0x160); // Make a duplicate of DS header

Expand Down Expand Up @@ -300,7 +299,7 @@ static void setMemoryAddressTWL(const tNDSHeader* ndsHeader) {
}

static void setMemoryAddress(const tNDSHeader* ndsHeader) {
if (ndsHeader->unitCode > 0) {
if (ndsHeader->unitCode & BIT(1)) {
// copyLoop((u32*)0x02FFFA80, (u32*)ndsHeader, 0x160); // Make a duplicate of DS header
tonccpy((u32*)0x02FFFA80, (u32*)ndsHeader, 0x160); // Make a duplicate of DS header

Expand Down Expand Up @@ -347,14 +346,14 @@ static void setMemoryAddress(const tNDSHeader* ndsHeader) {
*((u16*)0x02FFFC10) = 0x5835;
*((u16*)0x02FFFC40) = 0x01; // Boot Indicator -- EXTREMELY IMPORTANT!!! Thanks to cReDiAr

if ((twlram > 0) && (ndsHeader->unitCode == 0))setMemoryAddressTWL(ndsHeader);

switch (*((vu16*)0x02FFFF5E)) {
case 0xF63D: XMenuFix(); break;
case 0x0695: XMenuFix(); break;
case 0xE4C4: XMenuFix(); break;
case 0x918C: XMenuFix(); break;
case 0xF63D: XMenuFix(ndsHeader); break;
case 0x0695: XMenuFix(ndsHeader); break;
case 0xE4C4: XMenuFix(ndsHeader); break;
case 0x918C: XMenuFix(ndsHeader); break;
}

if ((twlram > 0) && !(ndsHeader->unitCode & BIT(1)))setMemoryAddressTWL(ndsHeader);
}

static u8 readwriteSPI(u8 data) {
Expand Down

0 comments on commit 1cf2bb9

Please sign in to comment.