diff --git a/arm7/Makefile b/arm7/Makefile index 5fc971fcc..0628dbf76 100644 --- a/arm7/Makefile +++ b/arm7/Makefile @@ -41,7 +41,7 @@ LDFLAGS = -specs=../ds_arm7_ram.specs -g $(ARCH) -Wl,--nmagic -Wl,-Map,$(notdir #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lmm7 -lnds7 +LIBS := -lnds7 #--------------------------------------------------------------------------------- diff --git a/arm7/source/main.c b/arm7/source/main.c index 4c6c7ce03..4c6103f11 100644 --- a/arm7/source/main.c +++ b/arm7/source/main.c @@ -29,7 +29,7 @@ redistribute it freely, subject to the following restrictions: ---------------------------------------------------------------------------------*/ #include -#include +// #include #include #include "fifocheck.h" @@ -127,11 +127,11 @@ int main(void) { // Start the RTC tracking IRQ initClockIRQ(); - mmInstall(FIFO_MAXMOD); + // mmInstall(FIFO_MAXMOD); SetYtrigger(80); - installSoundFIFO(); + // installSoundFIFO(); installSystemFIFO(); irqSet(IRQ_VCOUNT, VcountHandler); diff --git a/arm9/Makefile b/arm9/Makefile index 9d75a895d..e20d45797 100644 --- a/arm9/Makefile +++ b/arm9/Makefile @@ -43,7 +43,7 @@ LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project (order is important) #--------------------------------------------------------------------------------- -LIBS := -lfat -lmm9 -lnds9 +LIBS := -lfat -lnds9 #--------------------------------------------------------------------------------- diff --git a/arm9/source/main.cpp b/arm9/source/main.cpp index 07946c930..6d6825506 100644 --- a/arm9/source/main.cpp +++ b/arm9/source/main.cpp @@ -65,7 +65,7 @@ void dopause() { scanKeys(); } -void runFile(string filename, string savPath) { +void runFile(string filename, string savPath, string arm7DonorPath, u32 patchMpuRegion, u32 patchMpuSize) { vector argarray; if(debug) dopause(); @@ -98,7 +98,7 @@ void runFile(string filename, string savPath) { dbg_printf("no nds file specified\n"); } else { dbg_printf("Running %s with %d parameters\n", argarray[0], argarray.size()); - int err = runNdsFile (argarray[0], strdup(savPath.c_str()), argarray.size(), (const char **)&argarray[0]); + int err = runNdsFile (argarray[0], strdup(savPath.c_str()), strdup(arm7DonorPath.c_str()), patchMpuRegion, patchMpuSize, argarray.size(), (const char **)&argarray[0]); dbg_printf("Start failed. Error %i\n", err); } @@ -248,6 +248,12 @@ int main( int argc, char **argv) { std::string ndsPath = bootstrapini.GetString( "NDS-BOOTSTRAP", "NDS_PATH", ""); std::string savPath = bootstrapini.GetString( "NDS-BOOTSTRAP", "SAV_PATH", ""); + + std::string arm7DonorPath = bootstrapini.GetString( "NDS-BOOTSTRAP", "ARM7_DONOR_PATH", ""); + + u32 patchMpuRegion = bootstrapini.GetInt( "NDS-BOOTSTRAP", "PATCH_MPU_REGION", 0); + + u32 patchMpuSize = bootstrapini.GetInt( "NDS-BOOTSTRAP", "PATCH__MPU_SIZE", 0); if(bootstrapini.GetInt("NDS-BOOTSTRAP","BOOST_CPU",0) == 1) { dbg_printf("CPU boosted\n"); @@ -273,17 +279,17 @@ int main( int argc, char **argv) { dbg_printf("RERUN BOOTSTRAP in NTR mode via argv\n"); dbg_printf("Running %s\n", bootstrapPath.c_str()); - runFile(bootstrapPath.c_str(), savPath); + runFile(bootstrapPath.c_str(), savPath, arm7DonorPath.c_str(), patchMpuRegion, patchMpuSize); } else { dbg_printf("Running %s\n", ndsPath.c_str()); - runFile(ndsPath.c_str(), savPath.c_str()); + runFile(ndsPath.c_str(), savPath.c_str(), arm7DonorPath.c_str(), patchMpuRegion, patchMpuSize); } } else { dbg_printf("TWL MODE enabled\n"); dbg_printf("Running %s\n", ndsPath.c_str()); - runFile(ndsPath.c_str(), savPath.c_str()); + runFile(ndsPath.c_str(), savPath.c_str(), arm7DonorPath.c_str(), patchMpuRegion, patchMpuSize); } } else { consoleDemoInit(); diff --git a/arm9/source/nds_loader_arm9.c b/arm9/source/nds_loader_arm9.c index d886f2b67..b3a5acac3 100644 --- a/arm9/source/nds_loader_arm9.c +++ b/arm9/source/nds_loader_arm9.c @@ -67,6 +67,9 @@ #define ARG_SIZE_OFFSET 20 #define HAVE_DSISD_OFFSET 28 #define SAV_OFFSET 32 +#define DONOR_OFFSET 36 +#define PUR_OFFSET 40 +#define PUS_OFFSET 44 typedef signed int addr_t; typedef unsigned char data_t; @@ -266,7 +269,7 @@ static bool dldiPatchLoader (data_t *binData, u32 binSize, bool clearBSS) return true; } -int runNds (const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, bool initDisc, bool dldiPatchNds, int argc, const char** argv) +int runNds (const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 donorCluster, u32 patchMpuRegion, u32 patchMpuSize, bool initDisc, bool dldiPatchNds, int argc, const char** argv) { char* argStart; u16* argData; @@ -332,6 +335,9 @@ int runNds (const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, bo writeAddr ((data_t*) LCDC_BANK_C, ARG_SIZE_OFFSET, argSize); writeAddr ((data_t*) LCDC_BANK_C, SAV_OFFSET, saveCluster); + writeAddr ((data_t*) LCDC_BANK_C, DONOR_OFFSET, donorCluster); + writeAddr ((data_t*) LCDC_BANK_C, PUR_OFFSET, patchMpuRegion); + writeAddr ((data_t*) LCDC_BANK_C, PUS_OFFSET, patchMpuSize); if(dldiPatchNds) { // Patch the loader with a DLDI for the card @@ -369,10 +375,12 @@ int runNds (const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, bo return true; } -int runNdsFile (const char* filename, const char* savename, int argc, const char** argv) { +int runNdsFile (const char* filename, const char* savename, const char* arm7DonorPath, int patchMpuRegion, int patchMpuSize, int argc, const char** argv) { struct stat st; struct stat stSav; + struct stat stDonor; u32 clusterSav = 0; + u32 clusterDonor = 0; char filePath[PATH_MAX]; int pathLen; const char* args[1]; @@ -385,6 +393,10 @@ int runNdsFile (const char* filename, const char* savename, int argc, const char if (stat (savename, &stSav) >= 0) { clusterSav = stSav.st_ino; } + + if (stat (arm7DonorPath, &stDonor) >= 0) { + clusterDonor = stDonor.st_ino; + } if (argc <= 0 || !argv) { // Construct a command line if we weren't supplied with one @@ -403,7 +415,7 @@ int runNdsFile (const char* filename, const char* savename, int argc, const char //installBootStub(havedsiSD); - return runNds (load_bin, load_bin_size, st.st_ino, clusterSav, true, true, argc, argv); + return runNds (load_bin, load_bin_size, st.st_ino, clusterSav, clusterDonor, patchMpuRegion, patchMpuSize, true, true, argc, argv); } /* diff --git a/arm9/source/nds_loader_arm9.h b/arm9/source/nds_loader_arm9.h index 6957b6d3d..2bee485e7 100644 --- a/arm9/source/nds_loader_arm9.h +++ b/arm9/source/nds_loader_arm9.h @@ -29,9 +29,9 @@ extern "C" { #define LOAD_DEFAULT_NDS 0 -int runNds (const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, bool initDisc, bool dldiPatchNds, int argc, const char** argv); +int runNds (const void* loader, u32 loaderSize, u32 cluster, u32 saveCluster, u32 donorCluster, u32 patchMpuRegion, u32 patchMpuSize, bool initDisc, bool dldiPatchNds, int argc, const char** argv); -int runNdsFile (const char* filename, const char* savename, int argc, const char** argv); +int runNdsFile (const char* filename, const char* savename, const char* arm7DonorPath, int patchMpuRegion, int patchMpuSize, int argc, const char** argv); bool installBootStub(bool havedsiSD); diff --git a/bootloader/source/boot.c b/bootloader/source/boot.c index 737baa44a..bdf60e044 100644 --- a/bootloader/source/boot.c +++ b/bootloader/source/boot.c @@ -72,8 +72,6 @@ void sdmmc_controller_init(); #define CHEAT_DATA_LOCATION 0x06010000 #define ENGINE_LOCATION_ARM7 0x03780000 #define ENGINE_LOCATION_ARM9 0x03700000 -#define MARKER_ADDRESS_1 0x03740000 -#define MARKER_ADDRESS_2 0x03746004 const char* bootName = "BOOT.NDS"; @@ -85,6 +83,9 @@ extern unsigned long argStart; extern unsigned long argSize; extern unsigned long dsiSD; extern unsigned long saveFileCluster; +extern unsigned long donorFileCluster; +extern unsigned long patchMpuRegion; +extern unsigned long patchMpuSize; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Firmware stuff @@ -294,14 +295,17 @@ int main (void) { _io_dldi.fn_isInserted = sdmmc_inserted; _io_dldi.fn_startup = sdmmc_startup; } - - aFile file = getFileFromCluster (storedFileCluster); + // Init card if(!FAT_InitFiles(initDisc)) { nocashMessage("!FAT_InitFiles"); return -1; } + + aFile file = getFileFromCluster (storedFileCluster); + aFile donorFile = getFileFromCluster (donorFileCluster); + if ((file.firstCluster < CLUSTER_FIRST) || (file.firstCluster >= CLUSTER_EOF)) /* Invalid file cluster specified */ { file = getBootFileCluster(bootName); @@ -354,19 +358,15 @@ int main (void) { } else { nocashMessage("dldi Patch Unsuccessful try to patch card"); copyLoop (ENGINE_LOCATION_ARM7, (u32*)cardengine_arm7_bin, cardengine_arm7_bin_size); - copyLoop (ENGINE_LOCATION_ARM9, (u32*)cardengine_arm9_bin, cardengine_arm9_bin_size); - - // set a synchronisation marker on the WRAM block - *((vu32*)MARKER_ADDRESS_1) = (vu32)0xDEADBABE; - *((vu32*)MARKER_ADDRESS_2) = (vu32)0xDEADBABE; + copyLoop (ENGINE_LOCATION_ARM9, (u32*)cardengine_arm9_bin, cardengine_arm9_bin_size); module_params_t* params = findModuleParams(NDS_HEAD); if(params) { ensureArm9Decompressed(NDS_HEAD, params); } - - patchCardNds(NDS_HEAD,ENGINE_LOCATION_ARM7,ENGINE_LOCATION_ARM9,params,saveFileCluster); + + patchCardNds(NDS_HEAD,ENGINE_LOCATION_ARM7,ENGINE_LOCATION_ARM9,params,saveFileCluster, patchMpuRegion, patchMpuSize, donorFile); int error = hookNdsRetail(NDS_HEAD, file, (const u32*)CHEAT_DATA_LOCATION, (u32*)CHEAT_ENGINE_LOCATION, (u32*)ENGINE_LOCATION_ARM7); if(error == ERR_NONE) { diff --git a/bootloader/source/card_patcher.c b/bootloader/source/card_patcher.c index 6a57afa59..b3e1bc63e 100644 --- a/bootloader/source/card_patcher.c +++ b/bootloader/source/card_patcher.c @@ -63,6 +63,9 @@ u32 irqEnableStartSignature4[4] = {0xE92D4010, 0xE1A04000, 0xEBFFFFF6, 0xE59FC02 u32 arenaLowSignature[4] = {0xE1A00100,0xE2800627,0xE2800AFF,0xE5801DA0}; +u32 mpuInitRegion0Signature[1] = {0xEE060F10}; +u32 mpuInitRegion0Data[1] = {0x4000033}; + u32 mpuInitRegion1Signature[1] = {0xEE060F11}; u32 mpuInitRegion1Data1[1] = {0x200002D}; // sdk >= 4 version @@ -176,7 +179,7 @@ void ensureArm9Decompressed(const tNDSHeader* ndsHeader, module_params_t* module moduleParams->compressed_static_end = 0; } -u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams) { +u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams, u32 patchMpuRegion, u32 patchMpuSize) { u32* debug = (u32*)0x03784000; debug[4] = ndsHeader->arm9destination; @@ -200,6 +203,41 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu cardReadCachedEndSignature = cardReadCachedEndSignature4; mpuInitRegion1Data = mpuInitRegion1Data4; } + + u32* mpuInitRegionSignature = mpuInitRegion1Signature; + u32* mpuInitRegionData = mpuInitRegion1Data; + u32 mpuInitRegionNewData = PAGE_32M | 0x02000000 | 1; + u32 needFlushCache = 0; + int mpuAccessOffset = 0; + u32 mpuNewDataAccess = 0; + u32 mpuNewInstrAccess = 0; + + switch(patchMpuRegion) { + case 0 : + mpuInitRegionSignature = mpuInitRegion0Signature; + mpuInitRegionData = mpuInitRegion0Data; + mpuInitRegionNewData = PAGE_128M | 0x00000000 | 1; + break; + case 1 : + mpuInitRegionSignature = mpuInitRegion1Signature; + mpuInitRegionData = mpuInitRegion1Data; + needFlushCache = 1; + break; + case 2 : + mpuInitRegionSignature = mpuInitRegion2Signature; + mpuInitRegionData = mpuInitRegion2Data; + mpuNewDataAccess = 0x15111111; + mpuNewInstrAccess = 0x5111111; + mpuAccessOffset = 6; + break; + case 3 : + mpuInitRegionSignature = mpuInitRegion3Signature; + mpuInitRegionData = mpuInitRegion3Data; + mpuInitRegionNewData = PAGE_8M | 0x03000000 | 1; + mpuNewInstrAccess = 0x5111111; + mpuAccessOffset = 5; + break; + } // Find the card read u32 cardReadEndOffset = @@ -278,13 +316,13 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu u32* mpuDataOffset = 0; u32 mpuStartOffset = getOffset((u32*)ndsHeader->arm9destination, ndsHeader->arm9binarySize, - (u32*)mpuInitRegion3Signature, 1, 1); + (u32*)mpuInitRegionSignature, 1, 1); if (!mpuStartOffset) { dbg_printf("Mpu init not found\n"); } else { mpuDataOffset = getOffset((u32*)mpuStartOffset, 0x100, - (u32*)mpuInitRegion1Data, 1, 1); + (u32*)mpuInitRegionData, 1, 1); if (!mpuDataOffset) { dbg_printf("Mpu data not found\n"); } else { @@ -304,23 +342,16 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu if(mpuDataOffset) { // change the region 1 configuration - *mpuDataOffset = PAGE_32M | 0x02000000 | 1; - - - // change the region 3 configuration - //*mpuDataOffset = PAGE_8M | 0x03000000 | 1; - - /*// Region 2 settings - // change intruction access - mpuDataOffset[6] = 0x5111111; - // change data access - mpuDataOffset[7] = 0x15111111; */ + *mpuDataOffset = mpuInitRegionNewData; - // Region 3 settings - // change intruction access - //mpuDataOffset[5] = 0x5103011; - // change data access - //mpuDataOffset[6] = 0x15113011; + if(mpuAccessOffset) { + if(mpuNewInstrAccess) { + mpuDataOffset[mpuAccessOffset] = mpuNewInstrAccess; + } + if(mpuNewDataAccess) { + mpuDataOffset[mpuAccessOffset] = mpuNewDataAccess; + } + } } // Find the mpu cache init @@ -331,20 +362,35 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu dbg_printf("Mpu init cache not found\n"); } else { *mpuCacheOffset = 0xE3A00046; - } */ - - + } */ // patch out all further mpu reconfiguration - while(mpuStartOffset) { - mpuStartOffset = getOffset(mpuStartOffset+4, ndsHeader->arm9binarySize, + while(mpuStartOffset && patchMpuSize) { + u32 patchSize = ndsHeader->arm9binarySize; + if(patchMpuSize>1) { + patchSize = patchMpuSize; + } + mpuStartOffset = getOffset(mpuStartOffset+4, patchSize, (u32*)mpuInitRegion1Signature, 1, 1); if(mpuStartOffset) { dbg_printf("Mpu init :\t"); dbg_hexa(mpuStartOffset); dbg_printf("\n"); - *((u32*)mpuStartOffset) = 0xE1A00000 ; + + *((u32*)mpuStartOffset) = 0xE1A00000 ; // nop + + /*// try to found it + for (int i = 0; i<0x100; i++) { + mpuDataOffset = (u32*)(mpuStartOffset+i); + if(((*mpuDataOffset) & 0xFFFFFF00) == 0x02000000) { + *mpuDataOffset = PAGE_32M | 0x02000000 | 1; + break; + } + if(i == 100) { + *((u32*)mpuStartOffset) = 0xE1A00000 ; + } + }*/ } } @@ -408,6 +454,7 @@ u32 patchCardNdsArm9 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu *((u32*)patches[7]) = cardPullOutOffset+4; *((u32*)patches[8]) = cardReadCachedOffset; + *((u32*)patches[10]) = needFlushCache; //copyLoop (oldArenaLow, cardReadPatch, 0xF0); @@ -660,7 +707,7 @@ u32 savePatchV2 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_pa dbg_printf("[Error] CardRead not found:\n"); dbg_hexa((u32)cardRead); dbg_printf("\n"); - return 1; + return 0; } u32* cardId = (u32*) (JumpTableFunc + 0xE0); @@ -729,7 +776,7 @@ u32 savePatchV2 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_pa arm7Function[8] = saveFileCluster; } - return 0; + return 1; } @@ -924,7 +971,27 @@ u32 savePatchV1 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_pa return 1; } -u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams, u32 saveFileCluster ) { +void swapBinary_ARM7(aFile donorfile) +{ + u32 ndsHeader[0x170>>2]; + + nocashMessage("loadBinary_ARM7"); + + // read NDS header + fileRead ((char*)ndsHeader, donorfile, 0, 0x170); + // read ARM7 info from NDS header + u32 ARM7_SRC = ndsHeader[0x030>>2]; + char* ARM7_DST = (char*)ndsHeader[0x038>>2]; + u32 ARM7_LEN = ndsHeader[0x03C>>2]; + + fileRead(ARM7_DST, donorfile, ARM7_SRC, ARM7_LEN); + + NDS_HEAD[0x030>>2] = ARM7_SRC; + NDS_HEAD[0x038>>2] = ARM7_DST; + NDS_HEAD[0x03C>>2] = ARM7_LEN; +} + +u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, module_params_t* moduleParams, u32 saveFileCluster, aFile donorFile) { u32* debug = (u32*)0x03784000; u32* irqEnableStartSignature = irqEnableStartSignature1; @@ -967,16 +1034,27 @@ u32 patchCardNdsArm7 (const tNDSHeader* ndsHeader, u32* cardEngineLocation, modu u32 saveResult = savePatchV1(ndsHeader, cardEngineLocation, moduleParams, saveFileCluster); if(!saveResult) saveResult = savePatchV2(ndsHeader, cardEngineLocation, moduleParams, saveFileCluster); + if(!saveResult) { + if ((donorFile.firstCluster >= CLUSTER_FIRST) && (donorFile.firstCluster < CLUSTER_EOF)) { + dbg_printf("swap the arm7 binary"); + swapBinary_ARM7(donorFile); + // apply the arm7 binary swap and the save patch again, assume save v2 nds file + saveResult = savePatchV2(ndsHeader, cardEngineLocation, moduleParams, saveFileCluster); + } else { + dbg_printf("no arm7 binary specified for swapping"); + } + } dbg_printf("ERR_NONE"); return 0; } -u32 patchCardNds (const tNDSHeader* ndsHeader, u32* cardEngineLocationArm7, u32* cardEngineLocationArm9, module_params_t* moduleParams, u32 saveFileCluster ) { +u32 patchCardNds (const tNDSHeader* ndsHeader, u32* cardEngineLocationArm7, u32* cardEngineLocationArm9, module_params_t* moduleParams, + u32 saveFileCluster, u32 patchMpuRegion, u32 patchMpuSize, aFile donorFile) { dbg_printf("patchCardNds"); - patchCardNdsArm9(ndsHeader, cardEngineLocationArm9, moduleParams); - patchCardNdsArm7(ndsHeader, cardEngineLocationArm7, moduleParams, saveFileCluster); + patchCardNdsArm9(ndsHeader, cardEngineLocationArm9, moduleParams, patchMpuRegion, patchMpuSize); + patchCardNdsArm7(ndsHeader, cardEngineLocationArm7, moduleParams, saveFileCluster, donorFile); dbg_printf("ERR_NONE"); return 0; diff --git a/bootloader/source/card_patcher.h b/bootloader/source/card_patcher.h index e7106ca41..c173e587a 100644 --- a/bootloader/source/card_patcher.h +++ b/bootloader/source/card_patcher.h @@ -21,6 +21,7 @@ #include #include +#include "fat.h" #define PAGE_4K (0b01011 << 1) #define PAGE_8K (0b01100 << 1) @@ -44,6 +45,8 @@ #define PAGE_2G (0b11110 << 1) #define PAGE_4G (0b11111 << 1) +#define NDS_HEAD ((u32*)0x02FFFE00) + typedef struct { u32 auto_load_list_offset; @@ -64,6 +67,6 @@ void ensureArm9Decompressed(const tNDSHeader* ndsHeader, module_params_t* module arm7_hookGame Adds a hook in the game's ARM7 binary to our own code -------------------------------------------------------------------------*/ -u32 patchCardNds (const tNDSHeader* ndsHeader, u32* cardEngineLocationArm7, u32* cardEngineLocationArm9, module_params_t* moduleParams, u32 saveFileCluster); +u32 patchCardNds (const tNDSHeader* ndsHeader, u32* cardEngineLocationArm7, u32* cardEngineLocationArm9, module_params_t* moduleParams, u32 saveFileCluster, u32 patchMpuRegion, u32 patchMpuSize, aFile donorFile); #endif // CARD_PATCHER_H \ No newline at end of file diff --git a/bootloader/source/load_crt0.s b/bootloader/source/load_crt0.s index 6f7256981..752bb5183 100644 --- a/bootloader/source/load_crt0.s +++ b/bootloader/source/load_crt0.s @@ -30,6 +30,9 @@ .global argSize .global dsiSD .global saveFileCluster + .global donorFileCluster + .global patchMpuRegion + .global patchMpuSize @--------------------------------------------------------------------------------- .align 4 .arm @@ -55,6 +58,12 @@ dsiSD: .word 0 saveFileCluster: .word 0x00000000 @ .sav file +donorFileCluster: + .word 0x00000000 @ donor .nds file +patchMpuRegion: + .word 0x00000000 +patchMpuSize: + .word 0x00000000 startUp: mov r0, #0x04000000 diff --git a/cardengine_arm7/source/cardEngine.c b/cardengine_arm7/source/cardEngine.c index 7eb3ca471..46a73503e 100644 --- a/cardengine_arm7/source/cardEngine.c +++ b/cardengine_arm7/source/cardEngine.c @@ -66,121 +66,124 @@ void initLogging() { } void runCardEngineCheck (void) { - //dbg_printf("runCardEngineCheck\n"); - int oldIME = enterCriticalSection(); + //dbg_printf("runCardEngineCheck\n"); + //nocashMessage("runCardEngineCheck"); - initLogging(); - - if(*(vu32*)(0x027FFB14) == (vu32)0x026ff800) - { - dbg_printf("\ncard read received\n"); - - if(calledViaIPC) { - dbg_printf("\ntriggered via IPC\n"); - } - - u32 src = *(vu32*)(sharedAddr+2); - u32 dst = *(vu32*)(sharedAddr); - u32 len = *(vu32*)(sharedAddr+1); - u32 marker = *(vu32*)(sharedAddr+3); - - dbg_printf("\nstr : \n"); - dbg_hexa(cardStruct); - dbg_printf("\nsrc : \n"); - dbg_hexa(src); - dbg_printf("\ndst : \n"); - dbg_hexa(dst); - dbg_printf("\nlen : \n"); - dbg_hexa(len); - dbg_printf("\nmarker : \n"); - dbg_hexa(marker); + if(tryLockMutex()) { + initLogging(); - dbg_printf("\nlog only \n"); + //nocashMessage("runCardEngineCheck mutex ok"); - *(vu32*)(0x027FFB14) = 0; - } + if(*(vu32*)(0x027FFB14) == (vu32)0x026ff800) + { + dbg_printf("\ncard read received\n"); + + if(calledViaIPC) { + dbg_printf("\ntriggered via IPC\n"); + } + + u32 src = *(vu32*)(sharedAddr+2); + u32 dst = *(vu32*)(sharedAddr); + u32 len = *(vu32*)(sharedAddr+1); + u32 marker = *(vu32*)(sharedAddr+3); + + dbg_printf("\nstr : \n"); + dbg_hexa(cardStruct); + dbg_printf("\nsrc : \n"); + dbg_hexa(src); + dbg_printf("\ndst : \n"); + dbg_hexa(dst); + dbg_printf("\nlen : \n"); + dbg_hexa(len); + dbg_printf("\nmarker : \n"); + dbg_hexa(marker); + + dbg_printf("\nlog only \n"); + + *(vu32*)(0x027FFB14) = 0; + } - if(*(vu32*)(0x027FFB14) == (vu32)0x027ff800) - { - dbg_printf("\ncard read received\n"); + if(*(vu32*)(0x027FFB14) == (vu32)0x027ff800) + { + dbg_printf("\ncard read received\n"); + + if(calledViaIPC) { + dbg_printf("\ntriggered via IPC\n"); + } + + u32 src = *(vu32*)(sharedAddr+2); + u32 dst = *(vu32*)(sharedAddr); + u32 len = *(vu32*)(sharedAddr+1); + u32 marker = *(vu32*)(sharedAddr+3); + + dbg_printf("\nstr : \n"); + dbg_hexa(cardStruct); + dbg_printf("\nsrc : \n"); + dbg_hexa(src); + dbg_printf("\ndst : \n"); + dbg_hexa(dst); + dbg_printf("\nlen : \n"); + dbg_hexa(len); + dbg_printf("\nmarker : \n"); + dbg_hexa(marker); + + fileRead(0x027ff800 ,romFile,src,len); + + dbg_printf("\nread \n"); - if(calledViaIPC) { - dbg_printf("\ntriggered via IPC\n"); + + if(is_aligned(dst,4) || is_aligned(len,4)) { + dbg_printf("\n aligned read : \n"); + //*(vu32*)(0x027FFB0C) = (vu32)2; + } else { + dbg_printf("\n misaligned read : \n"); + //*(vu32*)(0x027FFB0C) = (vu32)0; + } + *(vu32*)(0x027FFB14) = 0; } - - u32 src = *(vu32*)(sharedAddr+2); - u32 dst = *(vu32*)(sharedAddr); - u32 len = *(vu32*)(sharedAddr+1); - u32 marker = *(vu32*)(sharedAddr+3); - - dbg_printf("\nstr : \n"); - dbg_hexa(cardStruct); - dbg_printf("\nsrc : \n"); - dbg_hexa(src); - dbg_printf("\ndst : \n"); - dbg_hexa(dst); - dbg_printf("\nlen : \n"); - dbg_hexa(len); - dbg_printf("\nmarker : \n"); - dbg_hexa(marker); - - fileRead(0x027ff800 ,romFile,src,len); - dbg_printf("\nread \n"); - - - if(is_aligned(dst,4) || is_aligned(len,4)) { - dbg_printf("\n aligned read : \n"); - //*(vu32*)(0x027FFB0C) = (vu32)2; - } else { - dbg_printf("\n misaligned read : \n"); - //*(vu32*)(0x027FFB0C) = (vu32)0; - } - *(vu32*)(0x027FFB14) = 0; - } - - if(*(vu32*)(0x027FFB14) == (vu32)0x025FFB08) - { - //dbg_printf("\ncard read received v2\n"); - - if(calledViaIPC) { - //dbg_printf("\ntriggered via IPC\n"); + if(*(vu32*)(0x027FFB14) == (vu32)0x025FFB08) + { + //dbg_printf("\ncard read received v2\n"); + + if(calledViaIPC) { + //dbg_printf("\ntriggered via IPC\n"); + } + + // old sdk version + u32 src = *(vu32*)(sharedAddr+2); + u32 dst = *(vu32*)(sharedAddr); + u32 len = *(vu32*)(sharedAddr+1); + u32 marker = *(vu32*)(sharedAddr+3); + + /*dbg_printf("\nstr : \n"); + dbg_hexa(cardStruct); + dbg_printf("\nsrc : \n"); + dbg_hexa(src); + dbg_printf("\ndst : \n"); + dbg_hexa(dst); + dbg_printf("\nlen : \n"); + dbg_hexa(len); + dbg_printf("\nmarker : \n"); + dbg_hexa(marker); + //*/ + + fileRead(dst,romFile,src,len); + + //dbg_printf("\nread \n"); + + if(is_aligned(dst,4) || is_aligned(len,4)) { + //dbg_printf("\n aligned read : \n"); + //*(vu32*)(0x027FFB0C) = (vu32)2; + } else { + //dbg_printf("\n misaligned read : \n"); + //*(vu32*)(0x027FFB0C) = (vu32)0; + } + *(vu32*)(0x027FFB14) = 0; } - - // old sdk version - u32 src = *(vu32*)(sharedAddr+2); - u32 dst = *(vu32*)(sharedAddr); - u32 len = *(vu32*)(sharedAddr+1); - u32 marker = *(vu32*)(sharedAddr+3); - - /*dbg_printf("\nstr : \n"); - dbg_hexa(cardStruct); - dbg_printf("\nsrc : \n"); - dbg_hexa(src); - dbg_printf("\ndst : \n"); - dbg_hexa(dst); - dbg_printf("\nlen : \n"); - dbg_hexa(len); - dbg_printf("\nmarker : \n"); - dbg_hexa(marker); - //*/ - - fileRead(dst,romFile,src,len); - - //dbg_printf("\nread \n"); - - if(is_aligned(dst,4) || is_aligned(len,4)) { - //dbg_printf("\n aligned read : \n"); - //*(vu32*)(0x027FFB0C) = (vu32)2; - } else { - //dbg_printf("\n misaligned read : \n"); - //*(vu32*)(0x027FFB0C) = (vu32)0; - } - *(vu32*)(0x027FFB14) = 0; + unlockMutex(); } - - leaveCriticalSection(oldIME); } //--------------------------------------------------------------------------------- diff --git a/cardengine_arm7/source/card_engine_header.s b/cardengine_arm7/source/card_engine_header.s index 342bf3d3e..671113a66 100644 --- a/cardengine_arm7/source/card_engine_header.s +++ b/cardengine_arm7/source/card_engine_header.s @@ -298,4 +298,38 @@ arm7Functions : .word cardRead .word cardId saveCluster: -.word 0x00000000 \ No newline at end of file +.word 0x00000000 + +.global tryLockMutex +.type tryLockMutex STT_FUNC +tryLockMutex: +adr r1, mutex +mov r2, #1 +mutex_loop: + swp r0,r2, [r1] + cmp r0, #1 + beq mutex_fail + +mutex_success: + mov r2, #1 + str r2, [r1] + mov r0, #1 + b mutex_exit + +mutex_fail: + mov r0, #0 + +mutex_exit: + bx lr + + +.global unlockMutex +.type unlockMutex STT_FUNC +unlockMutex: + adr r1, mutex + mov r2, #0 + str r2, [r1] + bx lr + +mutex: +.word 0x00000000 \ No newline at end of file diff --git a/cardengine_arm9/source/cardEngine.c b/cardengine_arm9/source/cardEngine.c index b57b19a01..1426e2094 100644 --- a/cardengine_arm9/source/cardEngine.c +++ b/cardengine_arm9/source/cardEngine.c @@ -29,6 +29,7 @@ extern vu32* volatile cardStruct; //extern vu32* volatile cacheStruct; extern u32 sdk_version; +extern u32 needFlushDCCache; vu32* volatile sharedAddr = (vu32*)0x027FFB08; extern volatile int (*readCachedRef)(u32*); // this pointer is not at the end of the table but at the handler pointer corresponding to the current irq static u32 currentSector = 0; @@ -92,20 +93,11 @@ void cardRead (u32* cacheStruct) { // send a command to the arm7 to fill the WRAM cache commandRead = 0x025FFB08; - DC_FlushRange((vu32*)BUFFER_ADDRESS, READ_SIZE_ARM7); - //cacheFlush(); + if(needFlushDCCache) DC_FlushRange((vu32*)BUFFER_ADDRESS, READ_SIZE_ARM7); // transfer the WRAM-B cache to the arm7 REG_MBK_B=(vu8)0x81; - /*while(*((vu32*)MARKER_ADDRESS_1) == (vu32)0xDEADBABE) { - DC_FlushRange((vu32*)MARKER_ADDRESS_1, 4); - } - - while(*((vu32*)MARKER_ADDRESS_2) == (vu32)0xDEADBABE) { - DC_FlushRange((vu32*)MARKER_ADDRESS_2, 4); - }*/ - // write the command sharedAddr[0] = BUFFER_ADDRESS; sharedAddr[1] = READ_SIZE_ARM7; @@ -119,16 +111,6 @@ void cardRead (u32* cacheStruct) { // transfer back the WRAM-B cache to the arm9 REG_MBK_B=(vu8)0x80; - /*DC_FlushRange((vu32*)MARKER_ADDRESS_1, 4); - while(*((vu32*)MARKER_ADDRESS_1) != (vu32)0xDEADBABE) { - DC_FlushRange((vu32*)MARKER_ADDRESS_1, 4); - } - - DC_FlushRange((vu32*)MARKER_ADDRESS_2, 4); - while(*((vu32*)MARKER_ADDRESS_2) != (vu32)0xDEADBABE) { - DC_FlushRange((vu32*)MARKER_ADDRESS_2, 4); - }*/ - currentSector = sector; } diff --git a/cardengine_arm9/source/card_engine_header.s b/cardengine_arm9/source/card_engine_header.s index ce22ff339..cedc4519d 100644 --- a/cardengine_arm9/source/card_engine_header.s +++ b/cardengine_arm9/source/card_engine_header.s @@ -115,6 +115,9 @@ patches: .word cacheFlushRef .word readCachedRef .word 0x0 +.global needFlushDCCache +needFlushDCCache: +.word 0x0 @--------------------------------------------------------------------------------- card_read_arm9: @@ -239,4 +242,38 @@ loop_flush_range : ADD R0, R0, #0x20 CMP R0, R1 BLT loop_flush_range - BX LR \ No newline at end of file + BX LR + +.global tryLockMutex +.type tryLockMutex STT_FUNC +tryLockMutex: +adr r1, mutex +mov r2, #1 +mutex_loop: + swp r0,r2, [r1] + cmp r0, #1 + beq mutex_fail + +mutex_success: + mov r2, #1 + str r2, [r1] + mov r0, #1 + b mutex_exit + +mutex_fail: + mov r0, #0 + +mutex_exit: + bx lr + + +.global unLockMutex +.type unLockMutex STT_FUNC +unLockMutex: + adr r1, mutex + mov r2, #0 + str r2, [r1] + bx lr + +mutex: +.word 0x00000000 \ No newline at end of file