Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Dec 23, 2024
1 parent 41a68fb commit efd2727
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 32 deletions.
2 changes: 1 addition & 1 deletion retail/bootloaderi/source/arm7/main.arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,7 @@ int arm7_main(void) {

const u16 ce9size = (ROMsupportsDsiMode(&dsiHeaderTemp.ndshdr) && dsiModeConfirmed) ? 0x3800 : 0x2FA0;
ce7Location = *(u32*)CARDENGINEI_ARM7_BUFFERED_LOCATION;
u32 ce7Size = 0xB400;
u32 ce7Size = 0xB800;

const bool useSdk5ce7 = (isSdk5(moduleParams) && ROMsupportsDsiMode(&dsiHeaderTemp.ndshdr) && dsiModeConfirmed);
if (useSdk5ce7) {
Expand Down
2 changes: 1 addition & 1 deletion retail/cardenginei/arm7/cardengine.ld.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ OUTPUT_ARCH(arm)

MEMORY {

vram : ORIGIN = CARDENGINEI_ARM7_LOCATION, LENGTH = 44K /* WRAM A */
vram : ORIGIN = CARDENGINEI_ARM7_LOCATION, LENGTH = 45K /* WRAM A */
}

__vram_start = ORIGIN(vram);
Expand Down
29 changes: 17 additions & 12 deletions retail/cardenginei/arm7/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ extern u32 romLocation;

extern u32 romMapLines;
// 0: ROM part start, 1: ROM part start in RAM, 2: ROM part end in RAM
extern u32 romMap[5][3];
extern u32 romMap[7][3];

u32 currentSrlAddr = 0;

Expand All @@ -224,7 +224,7 @@ static void unlaunchSetFilename(bool boot) {
#ifdef TWLSDK
*(u8*)(0x02000838+i2) = *(u8*)(ce7+0x8400+i); // Unlaunch Device:/Path/Filename.ext (16bit Unicode,end by 0000h)
#else
*(u8*)(0x02000838+i2) = *(u8*)(ce7+0xB000+i); // Unlaunch Device:/Path/Filename.ext (16bit Unicode,end by 0000h)
*(u8*)(0x02000838+i2) = *(u8*)(ce7+0xB400+i); // Unlaunch Device:/Path/Filename.ext (16bit Unicode,end by 0000h)
#endif
i2 += 2;
}
Expand Down Expand Up @@ -262,9 +262,9 @@ static void readSrBackendId(void) {
*(u32*)(0x02000314) = *(u32*)(ce7+0x8504);
*(u32*)(0x02000318) = /* *(u32*)(ce7+0x8504) == 0x00030000 ? 0x13 : */ 0x17;
#else
*(u32*)(0x02000310) = *(u32*)(ce7+0xB100);
*(u32*)(0x02000314) = *(u32*)(ce7+0xB104);
*(u32*)(0x02000318) = /* *(u32*)(ce7+0xB104) == 0x00030000 ? 0x13 : */ 0x17;
*(u32*)(0x02000310) = *(u32*)(ce7+0xB500);
*(u32*)(0x02000314) = *(u32*)(ce7+0xB504);
*(u32*)(0x02000318) = /* *(u32*)(ce7+0xB504) == 0x00030000 ? 0x13 : */ 0x17;
#endif
*(u32*)(0x0200031C) = 0;
*(u16*)(0x02000306) = swiCRC16(0xFFFF, (void*)0x02000308, 0x18);
Expand Down Expand Up @@ -470,9 +470,9 @@ void reset(const bool downloadedSrl) {
*(u32*)0x02000000 = BIT(3);
*(u32*)0x02000004 = 0x54455352; // 'RSET'
if (consoleModel < 2) {
(*(u32*)(ce7+0xB100) == 0 && (valueBits & b_dsiSD)) ? unlaunchSetFilename(false) : unlaunchSetHiyaFilename();
(*(u32*)(ce7+0xB500) == 0 && (valueBits & b_dsiSD)) ? unlaunchSetFilename(false) : unlaunchSetHiyaFilename();
}
if (*(u32*)(ce7+0xB100) == 0 && (valueBits & b_dsiSD)) {
if (*(u32*)(ce7+0xB500) == 0 && (valueBits & b_dsiSD)) {
tonccpy((u32*)0x02000300, sr_data_srloader, 0x20);
} else {
// Use different SR backend ID
Expand Down Expand Up @@ -560,6 +560,11 @@ void reset(const bool downloadedSrl) {
} else {
valueBits &= ~isSdk5;
}
/* if ((moduleParams->sdk_version >= 0x2008000 && moduleParams->sdk_version != 0x2012774) || moduleParams->sdk_version == 0x20029A8) {
valueBits &= ~eSdk2;
} else {
valueBits |= eSdk2;
} */

ensureBinaryDecompressed(ndsHeader, moduleParams);

Expand Down Expand Up @@ -762,7 +767,7 @@ void forceGameReboot(void) {
#ifdef TWLSDK
(*(u32*)(ce7+0x8500) == 0) ? unlaunchSetFilename(false) : unlaunchSetHiyaFilename();
#else
(*(u32*)(ce7+0xB100) == 0) ? unlaunchSetFilename(false) : unlaunchSetHiyaFilename();
(*(u32*)(ce7+0xB500) == 0) ? unlaunchSetFilename(false) : unlaunchSetHiyaFilename();
#endif
}
waitFrames(5); // Wait for DSi screens to stabilize
Expand All @@ -778,7 +783,7 @@ void forceGameReboot(void) {
//if (doBak) restoreSdBakData();
if (*(u32*)(ce7+0x8500) == 0 && (valueBits & b_dsiSD))
#else
if (*(u32*)(ce7+0xB100) == 0 && (valueBits & b_dsiSD))
if (*(u32*)(ce7+0xB500) == 0 && (valueBits & b_dsiSD))
#endif
{
tonccpy((u32*)0x02000300, sr_data_srloader, 0x20);
Expand Down Expand Up @@ -927,18 +932,18 @@ void returnToLoader(bool reboot) {
#else
IPC_SendSync(0x8);
if (consoleModel >= 2) {
if (*(u32*)(ce7+0xB100) == 0 && (valueBits & b_dsiSD))
if (*(u32*)(ce7+0xB500) == 0 && (valueBits & b_dsiSD))
{
tonccpy((u32*)0x02000300, sr_data_srloader, 0x020);
}
else if (*(char*)(ce7+0xB103) == 'H' || *(char*)(ce7+0xB103) == 'K')
else if (*(char*)(ce7+0xB503) == 'H' || *(char*)(ce7+0xB503) == 'K')
{
// Use different SR backend ID
readSrBackendId();
}
waitFrames(1);
} else {
if (*(u32*)(ce7+0xB100) == 0 && (valueBits & b_dsiSD))
if (*(u32*)(ce7+0xB500) == 0 && (valueBits & b_dsiSD))
{
unlaunchSetFilename(true);
} else {
Expand Down
38 changes: 24 additions & 14 deletions retail/cardenginei/arm7/source/patcher/hook_arm9.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static const u32 handlerStartSig[5] = {
// alt gsun
static const u32 handlerStartSigAlt[5] = {
0xe3a0c301, // mov ip, #0x4000000
0xe5bc2208, //
0xe5bc2208, //
0xe1ec00d8, //
0xe3520000 // cmp r2, #0
};
Expand All @@ -47,10 +47,10 @@ static const u32 handlerEndSig[4] = {

// alt gsun
static const u32 handlerEndSigAlt[4] = {
0xe59f100C, //
0xe5813000, //
0xe5813004, //
0xeaffffb8 //
0xe59f100C, //
0xe5813000, //
0xe5813004, //
0xeaffffb8 //
};

static u32* hookInterruptHandler(const u32* start, size_t size) {
Expand All @@ -69,7 +69,7 @@ static u32* hookInterruptHandler(const u32* start, size_t size) {
return NULL;
}
}

/*dbg_printf("handlerStartSig\n");
dbg_hexa((u32)addr);
dbg_printf("\n");*/
Expand Down Expand Up @@ -103,12 +103,12 @@ static u32* hookInterruptHandler(const u32* start, size_t size) {
/*dbg_printf("tableAddr\n");
dbg_hexa(tableAddr);
dbg_printf("\n");*/

// u32 returnAddr = addr[1];
/*dbg_printf("returnAddr\n");
dbg_hexa(returnAddr);
dbg_printf("\n");*/

//u32* actualReturnAddr = addr + 2;
//u32* actualTableAddr = actualReturnAddr + (tableAddr - returnAddr)/sizeof(u32);

Expand All @@ -132,18 +132,28 @@ int hookNdsRetailArm9(
}
ce9->valueBits &= ~b_isDlp;

u32* tableAddr = hookInterruptHandler((u32*)ndsHeader->arm9destination, iUncompressedSize);

if (!tableAddr) {
extern u32 romMapLines;
extern u32 romMap[7][3];

ce9->romMapLines = romMapLines;
for (int i = 0; i < 7; i++) {
for (int i2 = 0; i2 < 3; i2++) {
ce9->romMap[i][i2] = romMap[i][i2];
}
}

u32* tableAddr = hookInterruptHandler((u32*)ndsHeader->arm9destination, iUncompressedSize);

if (!tableAddr) {
//dbg_printf("ERR_HOOK_9\n");
return ERR_HOOK;
}

/*dbg_printf("hookLocation arm9: ");
dbg_hexa((u32)tableAddr);
dbg_printf("\n\n");*/
ce9->irqTable = tableAddr;

ce9->irqTable = tableAddr;

//nocashMessage("ERR_NONE");
return ERR_NONE;
Expand Down
83 changes: 81 additions & 2 deletions retail/cardenginei/arm7/source/patcher/patch_arm7.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
//#include "debug_file.h"

#define gameOnFlashcard BIT(0)
#define eSdk2 BIT(1)
#define ROMinRAM BIT(3)
#define dsiMode BIT(4)
#define hasVramWifiBinary BIT(14)
#define sleepMode BIT(17)

extern u32 valueBits;
Expand Down Expand Up @@ -160,8 +162,13 @@ static void patchMirrorCheck(const tNDSHeader* ndsHeader, const module_params_t*
dbg_printf("\n\n"); */
}

extern u32 romMapLines;
extern u32 romMap[7][3];

static void patchVramWifiBinaryLoad(const tNDSHeader* ndsHeader, const module_params_t* moduleParams) {
if (ndsHeader->unitCode > 0 || moduleParams->sdk_version < 0x2008000) return;
if (ndsHeader->unitCode > 0 || moduleParams->sdk_version < 0x2008000) {
goto clearBit;
}

// Relocate VRAM WiFi binary from Main RAM to DSi WRAM
extern u32 relocationStart;
Expand All @@ -183,7 +190,7 @@ static void patchVramWifiBinaryLoad(const tNDSHeader* ndsHeader, const module_pa
offset++;
}
if (!found) {
return;
goto clearBit;
}

/* dbg_printf("VRAM WiFi binary load location end : ");
Expand All @@ -198,6 +205,78 @@ static void patchVramWifiBinaryLoad(const tNDSHeader* ndsHeader, const module_pa
break;
}
}

if (!(valueBits & hasVramWifiBinary)) {
bool mapFound = false;
bool mapFoundInCache = false;
for (int i = 0; i < romMapLines; i++) {
if (romMap[i][1] == 0x0C3EC000) {
romMap[i][1] = 0x0C7E8000;
romMap[i][2] = 0x0C7F8000;
mapFound = true;
break;
}
}

if (!(valueBits & ROMinRAM) && !(valueBits & gameOnFlashcard)) {
u32* cacheAddressTable = (u32*)((valueBits & eSdk2) ? CACHE_ADDRESS_TABLE_LOCATION2 : CACHE_ADDRESS_TABLE_LOCATION);
for (int i = 0; i < 0x2000/sizeof(u32); i++) {
if (cacheAddressTable[i] == 0 || cacheAddressTable[i] >= 0x0C7FC000) {
break;
} else if (cacheAddressTable[i] == 0x0C3EC000) {
cacheAddressTable[i] += 0x3FC000;
cacheAddressTable[i+1] += 0x3FC000;
cacheAddressTable[i+2] += 0x3FC000;
cacheAddressTable[i+3] += 0x3FC000;
mapFoundInCache = true;
break;
}
}
}

if (mapFound || mapFoundInCache) {
tonccpy((u32*)0x0C7E8000, (u32*)0x0C3EC000, 0x10000);
toncset((u32*)0x0C3EC000, 0, 0x10000);
}
valueBits |= hasVramWifiBinary;
}
return;

clearBit:
if (valueBits & hasVramWifiBinary) {
bool mapFound = false;
bool mapFoundInCache = false;
for (int i = 0; i < romMapLines; i++) {
if (romMap[i][1] == 0x0C7E8000) {
romMap[i][1] = 0x0C3EC000;
romMap[i][2] = 0x0C3FC000;
mapFound = true;
break;
}
}

if (!(valueBits & ROMinRAM) && !(valueBits & gameOnFlashcard)) {
u32* cacheAddressTable = (u32*)((valueBits & eSdk2) ? CACHE_ADDRESS_TABLE_LOCATION2 : CACHE_ADDRESS_TABLE_LOCATION);
for (int i = 0; i < 0x2000/sizeof(u32); i++) {
if (cacheAddressTable[i] == 0 || cacheAddressTable[i] >= 0x0C7FC000) {
break;
} else if (cacheAddressTable[i] == 0x0C7E8000) {
cacheAddressTable[i] -= 0x3FC000;
cacheAddressTable[i+1] -= 0x3FC000;
cacheAddressTable[i+2] -= 0x3FC000;
cacheAddressTable[i+3] -= 0x3FC000;
mapFoundInCache = true;
break;
}
}
}

if (mapFound || mapFoundInCache) {
tonccpy((u32*)0x0C3EC000, (u32*)0x0C7E8000, 0x10000);
toncset((u32*)0x0C7E8000, 0, 0x10000);
}
valueBits &= ~hasVramWifiBinary;
}
}

static void patchSleepMode(const tNDSHeader* ndsHeader) {
Expand Down
2 changes: 1 addition & 1 deletion retail/cardenginei/arm7_alt/cardengine.ld.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ OUTPUT_ARCH(arm)

MEMORY {

vram : ORIGIN = CARDENGINEI_ARM7_LOCATION_ALT, LENGTH = 44K /* WRAM A */
vram : ORIGIN = CARDENGINEI_ARM7_LOCATION_ALT, LENGTH = 45K /* WRAM A */
}

__vram_start = ORIGIN(vram);
Expand Down
2 changes: 1 addition & 1 deletion retail/common/include/locations.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
#define CARDENGINE_SHARED_ADDRESS_SDK1 0x027FFA0C
#define CARDENGINE_SHARED_ADDRESS_SDK5 0x02FFFA0C

#define LOADER_RETURN_LOCATION (u32)CARDENGINEI_ARM7_BUFFERED_LOCATION+0xB000
#define LOADER_RETURN_LOCATION (u32)CARDENGINEI_ARM7_BUFFERED_LOCATION+0xB400
#define LOADER_RETURN_SDK5_LOCATION (u32)CARDENGINEI_ARM7_BUFFERED_LOCATION+0x8400
#define LOADER_RETURN_DSIWARE_LOCATION (u32)CARDENGINEI_ARM7_BUFFERED_LOCATION+0x8000

Expand Down

0 comments on commit efd2727

Please sign in to comment.