Skip to content

Commit

Permalink
B4DS: Add support for *Cosmos X2*
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Jan 12, 2024
1 parent 54a0bf3 commit 7e8900c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 4 deletions.
1 change: 1 addition & 0 deletions retail/arm9/include/dsiwaresSetForBootloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static const char dsiWareForBootloader1[][4] = {
"K56", // Coropata
"KCX", // Cosmo Fighters
"K5D", // G.G Series: Cosmo Rally!!
"KX2", // Cosmos X2
"KDC", // Crash-Course Domo
"KQZ", // Crazy Chicken: Director's Cut (bootloaderi-exclusive)
"KCV", // Crazy Chicken: Pirates (bootloaderi-exclusive)
Expand Down
87 changes: 85 additions & 2 deletions retail/bootloader/source/arm7/dsi2ds_patches.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void patchDSiModeToDSMode(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
return;
}

const u32* ndmaCopy = ce9->patches->ndmaCopy;
const u32* dsiSaveCheckExists = ce9->patches->dsiSaveCheckExists;
const u32* dsiSaveGetResultCode = ce9->patches->dsiSaveGetResultCode;
const u32* dsiSaveCreate = ce9->patches->dsiSaveCreate;
Expand Down Expand Up @@ -3334,7 +3335,7 @@ void patchDSiModeToDSMode(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
else if (strcmp(romTid, "KJHE") == 0) {
const u32 newCodeAddr = *(u32*)0x02004FE8;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, 0x02035858, 0x14+0x218);
codeCopy((u32*)newCodeAddr, (u32*)0x02035858, 0x14+0x218);
setBL(newCodeAddr+8, newCodeAddr2);

*(u32*)0x020050FC = 0xE1A00000; // nop
Expand Down Expand Up @@ -3372,7 +3373,7 @@ void patchDSiModeToDSMode(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
else if (strcmp(romTid, "KJHP") == 0) {
const u32 newCodeAddr = *(u32*)0x02004FE8;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, 0x02035A1C, 0x14+0x218);
codeCopy((u32*)newCodeAddr, (u32*)0x02035A1C, 0x14+0x218);
setBL(newCodeAddr+8, newCodeAddr2);

*(u32*)0x020050EC = 0xE1A00000; // nop
Expand Down Expand Up @@ -7618,6 +7619,88 @@ void patchDSiModeToDSMode(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
}
}

// Cosmos X2 (USA)
else if (strcmp(romTid, "KX2E") == 0) {
const u32 newCodeAddr = *(u32*)0x02004FD0;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, (u32*)0x020208E0, 0x14+0x110);
setBL(newCodeAddr+8, newCodeAddr2);

*(u32*)0x02005100 = 0xE1A00000; // nop
*(u32*)0x02005108 = 0xE1A00000; // nop
*(u32*)0x0200518C = 0xE3A0392F; // mov r3, #0xBC000 (Shrink sound heap from 0x100000)
*(u32*)0x02005314 = 0xE1A00000; // nop
setBL(0x02009BBC, (u32)ndmaCopy);
*(u32*)0x0201EB14 = 0xE1A00000; // nop
*(u32*)0x0201EB20 = 0xE1A00000; // nop
*(u32*)0x0201EB34 = 0xE1A00000; // nop
*(u32*)0x0201ECC8 = 0xE1A00000; // nop
*(u32*)0x0201ED0C = 0xE1A00000; // nop
setBL(0x0201FCB0, newCodeAddr);
setBL(0x0201FE48, (u32)dsiSaveCreate);
setBL(0x0201FE58, (u32)dsiSaveOpen);
setBL(0x0201FE78, (u32)dsiSaveSetLength);
setBL(0x0201FE8C, (u32)dsiSaveClose);
setBL(0x0201FEA0, (u32)dsiSaveWrite);
setBL(0x0201FEAC, (u32)dsiSaveClose);
*(u32*)0x0201FFD0 = 0xE3A00000; // mov r0, #0
*(u32*)0x0202086C = 0xE1A00000; // nop
setBL(newCodeAddr2+0x30, (u32)dsiSaveOpenR);
setBL(newCodeAddr2+0x40, (u32)dsiSaveGetLength);
setBL(newCodeAddr2+0x5C, (u32)dsiSaveRead);
setBL(newCodeAddr2+0x84, (u32)dsiSaveClose);
setBL(0x0202212C, (u32)ndmaCopy);
setBL(0x020223CC, (u32)ndmaCopy);
*(u32*)0x0203D2AC = 0xE1A00000; // nop
*(u32*)0x020406E4 = 0xE1A00000; // nop
*(u32*)0x02040B0C = 0xE1A00000; // nop
*(u32*)0x02040B10 = 0xE1A00000; // nop
patchInitDSiWare(0x02046710, heapEnd);
*(u32*)0x02046A9C = (*(u32*)0x02004FD0)+0x200;
patchUserSettingsReadDSiWare(0x02047BA0);
}

// Cosmos X2 (Europe)
else if (strcmp(romTid, "KX2P") == 0) {
const u32 newCodeAddr = *(u32*)0x02004FE8;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, (u32*)0x020206C0, 0x14+0x118);
setBL(newCodeAddr+8, newCodeAddr2);

*(u32*)0x02005108 = 0xE1A00000; // nop
*(u32*)0x02005110 = 0xE1A00000; // nop
*(u32*)0x02005194 = 0xE3A0392F; // mov r3, #0xBC000 (Shrink sound heap from 0x100000)
*(u32*)0x02005328 = 0xE1A00000; // nop
setBL(0x020098A0, (u32)ndmaCopy);
*(u32*)0x0201E888 = 0xE1A00000; // nop
*(u32*)0x0201E894 = 0xE1A00000; // nop
*(u32*)0x0201E8A8 = 0xE1A00000; // nop
*(u32*)0x0201EA3C = 0xE1A00000; // nop
*(u32*)0x0201EA80 = 0xE1A00000; // nop
setBL(0x0201FA7C, newCodeAddr);
setBL(0x0201FC14, (u32)dsiSaveCreate);
setBL(0x0201FC24, (u32)dsiSaveOpen);
setBL(0x0201FC44, (u32)dsiSaveSetLength);
setBL(0x0201FC58, (u32)dsiSaveClose);
setBL(0x0201FC6C, (u32)dsiSaveWrite);
setBL(0x0201FC78, (u32)dsiSaveClose);
*(u32*)0x0201FD9C = 0xE3A00000; // mov r0, #0
*(u32*)0x02020648 = 0xE1A00000; // nop
setBL(newCodeAddr2+0x30, (u32)dsiSaveOpenR);
setBL(newCodeAddr2+0x40, (u32)dsiSaveGetLength);
setBL(newCodeAddr2+0x64, (u32)dsiSaveRead);
setBL(newCodeAddr2+0x8C, (u32)dsiSaveClose);
setBL(0x02021EA0, (u32)ndmaCopy);
setBL(0x02022140, (u32)ndmaCopy);
*(u32*)0x0203D020 = 0xE1A00000; // nop
*(u32*)0x020404EC = 0xE1A00000; // nop
*(u32*)0x0204093C = 0xE1A00000; // nop
*(u32*)0x02040940 = 0xE1A00000; // nop
patchInitDSiWare(0x02046564, heapEnd);
*(u32*)0x020468F0 = (*(u32*)0x02004FE8)+0x200;
patchUserSettingsReadDSiWare(0x02047A04);
}

// Crash-Course Domo (USA)
else if (strcmp(romTid, "KDCE") == 0) {
const u32 dsiSaveCreateT = 0x02024B0C;
Expand Down
46 changes: 44 additions & 2 deletions retail/bootloaderi/source/arm7/patch_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2167,7 +2167,7 @@ void dsiWarePatch(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
else if (strcmp(romTid, "KJHE") == 0 && saveOnFlashcard) {
const u32 newCodeAddr = 0x02F00000;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, 0x02035858, 0x14+0x218);
codeCopy((u32*)newCodeAddr, (u32*)0x02035858, 0x14+0x218);
setBL(newCodeAddr+8, newCodeAddr2);

setBL(0x02034C64, newCodeAddr);
Expand All @@ -2188,7 +2188,7 @@ void dsiWarePatch(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
else if (strcmp(romTid, "KJHP") == 0) {
const u32 newCodeAddr = 0x02F00000;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, 0x02035A1C, 0x14+0x218);
codeCopy((u32*)newCodeAddr, (u32*)0x02035A1C, 0x14+0x218);
setBL(newCodeAddr+8, newCodeAddr2);

setBL(0x02034E0C, newCodeAddr);
Expand Down Expand Up @@ -4548,6 +4548,48 @@ void dsiWarePatch(cardengineArm9* ce9, const tNDSHeader* ndsHeader) {
}
}

// Cosmos X2 (USA)
else if (strcmp(romTid, "KX2E") == 0 && saveOnFlashcard) {
const u32 newCodeAddr = 0x02003000;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, (u32*)0x020208E0, 0x14+0x110);
setBL(newCodeAddr+8, newCodeAddr2);

setBL(0x0201FCB0, newCodeAddr);
setBL(0x0201FE48, (u32)dsiSaveCreate);
setBL(0x0201FE58, (u32)dsiSaveOpen);
setBL(0x0201FE78, (u32)dsiSaveSetLength);
setBL(0x0201FE8C, (u32)dsiSaveClose);
setBL(0x0201FEA0, (u32)dsiSaveWrite);
setBL(0x0201FEAC, (u32)dsiSaveClose);
*(u32*)0x0201FFD0 = 0xE3A00000; // mov r0, #0
setBL(newCodeAddr2+0x30, (u32)dsiSaveOpenR);
setBL(newCodeAddr2+0x40, (u32)dsiSaveGetLength);
setBL(newCodeAddr2+0x5C, (u32)dsiSaveRead);
setBL(newCodeAddr2+0x84, (u32)dsiSaveClose);
}

// Cosmos X2 (Europe)
else if (strcmp(romTid, "KX2P") == 0 && saveOnFlashcard) {
const u32 newCodeAddr = 0x02F00000;
const u32 newCodeAddr2 = newCodeAddr+0x14;
codeCopy((u32*)newCodeAddr, (u32*)0x020206C0, 0x14+0x118);
setBL(newCodeAddr+8, newCodeAddr2);

setBL(0x0201FA7C, newCodeAddr);
setBL(0x0201FC14, (u32)dsiSaveCreate);
setBL(0x0201FC24, (u32)dsiSaveOpen);
setBL(0x0201FC44, (u32)dsiSaveSetLength);
setBL(0x0201FC58, (u32)dsiSaveClose);
setBL(0x0201FC6C, (u32)dsiSaveWrite);
setBL(0x0201FC78, (u32)dsiSaveClose);
*(u32*)0x0201FD9C = 0xE3A00000; // mov r0, #0
setBL(newCodeAddr2+0x30, (u32)dsiSaveOpenR);
setBL(newCodeAddr2+0x40, (u32)dsiSaveGetLength);
setBL(newCodeAddr2+0x64, (u32)dsiSaveRead);
setBL(newCodeAddr2+0x8C, (u32)dsiSaveClose);
}

// Crash-Course Domo (USA)
else if (strcmp(romTid, "KDCE") == 0) {
if (saveOnFlashcard) {
Expand Down
9 changes: 9 additions & 0 deletions retail/cardengine/arm9/source/card_engine_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ patches:
.word 0x0
.word 0x0
.word 0x0
.word 0x0
#else
.word ndmaCopy_arm
.word dsiSaveCheckExists_arm
.word dsiSaveGetResultCode_arm
.word dsiSaveCreate_arm
Expand Down Expand Up @@ -296,6 +298,13 @@ nand_write_arm9:
@---------------------------------------------------------------------------------

#ifndef NODSIWARE
@---------------------------------------------------------------------------------
ndmaCopy_arm:
@---------------------------------------------------------------------------------
ldr pc, =ndmaCopy
.pool
@---------------------------------------------------------------------------------

@---------------------------------------------------------------------------------
dsiSaveCheckExists_arm:
@---------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions retail/cardengine/arm9/source/cardengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,10 @@ bool nandWrite(void* memory,void* flash,u32 len,u32 dma) {
}

#ifndef NODSIWARE
void ndmaCopy(int ndmaSlot, const void* src, void* dst, u32 len) {
tonccpy(dst, src, len);
}

static bool sharedFontOpened = false;
static bool dsiSaveInited = false;
static bool dsiSaveExists = false;
Expand Down
1 change: 1 addition & 0 deletions retail/common/include/cardengine_header_arm9.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ typedef struct cardengineArm9Patches {
u32* card_dma_arm9;
u32* nand_read_arm9;
u32* nand_write_arm9;
u32* ndmaCopy;
u32* dsiSaveCheckExists;
u32* dsiSaveGetResultCode;
u32* dsiSaveCreate;
Expand Down

0 comments on commit 7e8900c

Please sign in to comment.