Skip to content

Commit

Permalink
Fix ROM mirroring for if ROM size is larger than device size
Browse files Browse the repository at this point in the history
Fixes #1645
  • Loading branch information
RocketRobz committed Oct 21, 2023
1 parent 880ffff commit 7b5a709
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 19 deletions.
4 changes: 3 additions & 1 deletion retail/bootloader/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ int hookNdsRetailArm9(
nocashMessage("hookNdsRetailArm9");

const char* romTid = getRomTid(ndsHeader);
extern u32 romPaddingSize;
extern u32 romLocation;
extern u32 romSizeLimit;
extern u16 s2FlashcardId;
Expand Down Expand Up @@ -249,7 +250,8 @@ int hookNdsRetailArm9(
ce9->s2FlashcardId = s2FlashcardId;
ce9->overlaysSize = overlaysSize;
ce9->ioverlaysSize = ioverlaysSize;
ce9->romLocation = romLocation;
ce9->romPaddingSize = romPaddingSize;
ce9->romLocation = romLocation;

u32 romOffset = 0;
if (ROMinRAM) {
Expand Down
21 changes: 21 additions & 0 deletions retail/bootloader/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ u8 baseUnitCode = 0;
u16 baseHeaderCRC = 0;
u16 baseSecureCRC = 0;
u32 baseRomSize = 0;
u32 romPaddingSize = 0;
u32 arm9ibinarySize = 0;
u32 baseChipID = 0;
bool pkmnHeader = false;
Expand Down Expand Up @@ -540,6 +541,13 @@ static void loadBinary_ARM7(const tDSiHeader* dsiHeaderTemp, aFile* file) {
}
}

dbg_printf("Header CRC is ");
u16 currentHeaderCRC = swiCRC16(0xFFFF, (void*)&dsiHeaderTemp->ndshdr, 0x15E);
if (currentHeaderCRC != dsiHeaderTemp->ndshdr.headerCRC16) {
dbg_printf("in");
}
dbg_printf("valid!\n");

if (
strncmp(baseTid, "ADA", 3) == 0 // Diamond
|| strncmp(baseTid, "APA", 3) == 0 // Pearl
Expand All @@ -556,6 +564,19 @@ static void loadBinary_ARM7(const tDSiHeader* dsiHeaderTemp, aFile* file) {
fileRead((char*)&baseHeaderCRC, file, 0x15E, sizeof(u16));
fileRead((char*)&baseSecureCRC, file, 0x6C, sizeof(u16));
fileRead((char*)&baseRomSize, file, 0x80, sizeof(u32));

u8 baseDeviceSize = 0;
fileRead((char*)&baseDeviceSize, file, 0x14, sizeof(u8));

romPaddingSize = 0x20000 << baseDeviceSize;
if (baseRomSize == 0 ? (romSize > romPaddingSize) : (baseRomSize > romPaddingSize)) {
dbg_printf("ROM size is larger than device size!\n");
while (baseRomSize == 0 ? (romSize > romPaddingSize) : (baseRomSize > romPaddingSize)) {
baseDeviceSize++;
romPaddingSize = 0x20000 << baseDeviceSize;
}
}

arm9ibinarySize = dsiHeaderTemp->arm9ibinarySize;
}

Expand Down
2 changes: 2 additions & 0 deletions retail/bootloaderi/source/arm7/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ int hookNdsRetailArm9(
extern u32 iUncompressedSize;
extern u32 overlaysSize;
extern bool overlayPatch;
extern u32 romPaddingSize;
extern u32 dataToPreloadAddr[2];
extern u32 dataToPreloadSize[2];
extern bool dataToPreloadFound(const tNDSHeader* ndsHeader);
Expand Down Expand Up @@ -330,6 +331,7 @@ int hookNdsRetailArm9(
}
ce9->mainScreen = mainScreen;
ce9->overlaysSize = overlaysSize;
ce9->romPaddingSize = romPaddingSize;
ce9->consoleModel = consoleModel;

if (!ROMinRAM) {
Expand Down
22 changes: 19 additions & 3 deletions retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ u16 baseHeaderCRC = 0;
u16 baseSecureCRC = 0;
u32 baseRomSize = 0;
u32 baseChipID = 0;
u32 romPaddingSize = 0;
bool pkmnHeader = false;
bool ndmaDisabled = false;

Expand Down Expand Up @@ -712,6 +713,13 @@ static void loadBinary_ARM7(const tDSiHeader* dsiHeaderTemp, aFile* file) {
}
}

dbg_printf("Header CRC is ");
u16 currentHeaderCRC = swiCRC16(0xFFFF, (void*)&dsiHeaderTemp->ndshdr, 0x15E);
if (currentHeaderCRC != dsiHeaderTemp->ndshdr.headerCRC16) {
dbg_printf("in");
}
dbg_printf("valid!\n");

char baseTid[5] = {0};
fileRead((char*)&baseTid, file, 0xC, 4);
if (
Expand All @@ -731,9 +739,17 @@ static void loadBinary_ARM7(const tDSiHeader* dsiHeaderTemp, aFile* file) {
fileRead((char*)&baseSecureCRC, file, 0x6C, sizeof(u16));
fileRead((char*)&baseRomSize, file, 0x80, sizeof(u32));

/*isGSDD = (strncmp(romTid, "BO5", 3) == 0) // Golden Sun: Dark Dawn
|| (strncmp(romTid, "TBR", 3) == 0) // Disney Pixar Brave
;*/
u8 baseDeviceSize = 0;
fileRead((char*)&baseDeviceSize, file, 0x14, sizeof(u8));

romPaddingSize = 0x20000 << baseDeviceSize;
if (baseRomSize == 0 ? (romSize > romPaddingSize) : (baseRomSize > romPaddingSize)) {
dbg_printf("ROM size is larger than device size!\n");
while (baseRomSize == 0 ? (romSize > romPaddingSize) : (baseRomSize > romPaddingSize)) {
baseDeviceSize++;
romPaddingSize = 0x20000 << baseDeviceSize;
}
}

sdmmc_set_ndma_slot(4);
}
Expand Down
2 changes: 2 additions & 0 deletions retail/cardengine/arm9/source/card_engine_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ overlaysSize:
.word 0x00000000
ioverlaysSize:
.word 0x00000000
romPaddingSize:
.word 0x00000000
irqTable:
.word 0x00000000
romLocation:
Expand Down
5 changes: 2 additions & 3 deletions retail/cardengine/arm9/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,8 @@ void cardRead(u32* cacheStruct, u8* dst0, u32 src0, u32 len0) {
u32 len = ((ce9->valueBits & isSdk5) ? len0 : cardStruct[2]);

// Simulate ROM mirroring
const u32 romPaddingSize = 0x20000 << ndsHeader->deviceSize;
while (src >= romPaddingSize) {
src -= romPaddingSize;
while (src >= ce9->romPaddingSize) {
src -= ce9->romPaddingSize;
}

if ((ce9->valueBits & cardReadFix) && src < 0x8000) {
Expand Down
15 changes: 6 additions & 9 deletions retail/cardenginei/arm9/source/cardDma.thumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,8 @@ void cardSetDma(u32 * params) {
u32 len = params[5];

// Simulate ROM mirroring
const u32 romPaddingSize = 0x20000 << ndsHeader->deviceSize;
while (src >= romPaddingSize) {
src -= romPaddingSize;
while (src >= ce9->romPaddingSize) {
src -= ce9->romPaddingSize;
}
params[3] = src;

Expand Down Expand Up @@ -467,9 +466,8 @@ void cardSetDma(u32 * params) {
u32 len = ((ce9->valueBits & isSdk5) ? dmaParams[5] : cardStruct[2]);

// Simulate ROM mirroring
const u32 romPaddingSize = 0x20000 << ndsHeader->deviceSize;
while (src >= romPaddingSize) {
src -= romPaddingSize;
while (src >= ce9->romPaddingSize) {
src -= ce9->romPaddingSize;
}
if (ce9->valueBits & isSdk5) {
dmaParams[3] = src;
Expand Down Expand Up @@ -675,9 +673,8 @@ void cardSetDma(u32 * params) {
#endif

// Simulate ROM mirroring
const u32 romPaddingSize = 0x20000 << ndsHeader->deviceSize;
while (src >= romPaddingSize) {
src -= romPaddingSize;
while (src >= ce9->romPaddingSize) {
src -= ce9->romPaddingSize;
}

disableIrqMask(IRQ_CARD);
Expand Down
2 changes: 2 additions & 0 deletions retail/cardenginei/arm9/source/card_engine_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ mainScreen:
.word 0x00000000
overlaysSize:
.word 0x00000000
romPaddingSize:
.word 0x00000000
consoleModel:
.word 0x00000000
irqTable:
Expand Down
5 changes: 2 additions & 3 deletions retail/cardenginei/arm9/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,8 @@ void cardRead(u32* cacheStruct, u8* dst0, u32 src0, u32 len0) {
#endif

// Simulate ROM mirroring
const u32 romPaddingSize = 0x20000 << ndsHeader->deviceSize;
while (src >= romPaddingSize) {
src -= romPaddingSize;
while (src >= ce9->romPaddingSize) {
src -= ce9->romPaddingSize;
}

#ifdef DEBUG
Expand Down
2 changes: 2 additions & 0 deletions retail/cardenginei/arm9_dsiware/source/card_engine_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ mainScreen:
.word 0x00000000
overlaysSize:
.word 0x00000000
romPaddingSize:
.word 0x00000000
consoleModel:
.word 0x00000000
irqTable:
Expand Down
2 changes: 2 additions & 0 deletions retail/common/include/cardengine_header_arm9.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ typedef struct cardengineArm9 {
*/
s32 mainScreen;
u32 overlaysSize;
u32 romPaddingSize;
u32 consoleModel;
u32* irqTable;
// Below not used for DSiWare ce9 binary
Expand Down Expand Up @@ -221,6 +222,7 @@ typedef struct cardengineArm9 {
u16 padding;
u32 overlaysSize;
u32 ioverlaysSize;
u32 romPaddingSize;
u32* irqTable;
u32 romLocation;
u32 rumbleFrames[2];
Expand Down

0 comments on commit 7b5a709

Please sign in to comment.