@@ -320,7 +320,7 @@ static void patchSleepMode(const tNDSHeader* ndsHeader) {
320320}
321321
322322
323- static void patchSleepInputWrite (const tNDSHeader * ndsHeader , const module_params_t * moduleParams ) {
323+ static void patchSleepInputWrite (cardengineArm7 * ce7 , const tNDSHeader * ndsHeader , const module_params_t * moduleParams , const bool useRelocSrc ) {
324324 u32 * offset = patchOffsetCache .sleepInputWriteOffset ;
325325 if (!patchOffsetCache .sleepInputWriteOffset ) {
326326 offset = findSleepInputWriteOffset (ndsHeader , moduleParams );
@@ -332,15 +332,54 @@ static void patchSleepInputWrite(const tNDSHeader* ndsHeader, const module_param
332332 return ;
333333 }
334334
335+ const bool isArm = (* offset == 0x13A04902 || * offset == 0x11A05004 );
336+
335337 if (!sleepMode ) {
336- if (* offset == 0x13A04902 || * offset == 0x11A05004 ) {
338+ if (isArm ) {
337339 * offset = 0xE1A00000 ; // nop
338340 } else {
339341 u16 * offsetThumb = (u16 * )offset ;
340342 * offsetThumb = 0x46C0 ; // nop
341343 }
342344 }
343345
346+ if (useRelocSrc ) {
347+ bool found = false;
348+ u32 offsetAdjust = (u32 )offset ;
349+ if (isArm ) {
350+ for (int i = 0 ; i < 6 ; i ++ ) {
351+ offsetAdjust += 4 ;
352+ if (* (u32 * )offsetAdjust == 0xE1C010B0 ) {
353+ found = true;
354+ break ;
355+ }
356+ }
357+
358+ if (found ) {
359+ const u32 srcAddr = (u32 )offsetAdjust - vAddrOfRelocSrc + (* (u32 * )0x02FFE1A0 == 0x080037C0 ? 0x37C0000 : 0x37F8000 );
360+ * (u32 * )offsetAdjust = generateA7Instr (srcAddr , (u32 )ce7 -> patches -> patchKeyInputs );
361+ }
362+ } else {
363+ for (int i = 0 ; i < 10 ; i ++ ) {
364+ offsetAdjust += 2 ;
365+ if (* (u16 * )offsetAdjust == 0x8001 ) {
366+ found = true;
367+ break ;
368+ }
369+ }
370+
371+ if (found ) {
372+ * (u16 * )offsetAdjust = 0x46C0 ; // nop
373+ offsetAdjust += 2 ;
374+ ce7 -> patches -> patchKeyInputsThumb [3 ] = * (u16 * )offsetAdjust ;
375+ * (u16 * )offsetAdjust = 0x4B00 ; // ldr r3, =patchKeyInputsThumb
376+ * (u16 * )(offsetAdjust + 2 ) = 0x4718 ; // bx r3
377+ * (u32 * )(offsetAdjust + 4 ) = (u32 )ce7 -> patches -> patchKeyInputsThumb ;
378+ * (u32 * )(offsetAdjust + 4 ) += 1 ;
379+ }
380+ }
381+ }
382+
344383 dbg_printf ("Sleep input write location : " );
345384 dbg_hexa ((u32 )offset );
346385 dbg_printf ("\n\n" );
@@ -458,7 +497,8 @@ u32 patchCardNdsArm7(
458497 cardengineArm7 * ce7 ,
459498 tNDSHeader * ndsHeader ,
460499 const module_params_t * moduleParams ,
461- u32 saveFileCluster
500+ u32 saveFileCluster ,
501+ const bool buttonsRemapped
462502) {
463503 arm7newUnitCode = ndsHeader -> unitCode ;
464504 newArm7binarySize = ndsHeader -> arm7binarySize ;
@@ -524,7 +564,6 @@ u32 patchCardNdsArm7(
524564 patchPostBoot (ndsHeader );
525565
526566 patchSleepMode (ndsHeader );
527- patchSleepInputWrite (ndsHeader , moduleParams );
528567
529568 patchRamClear (ndsHeader , moduleParams );
530569
@@ -548,6 +587,7 @@ u32 patchCardNdsArm7(
548587 }
549588
550589 if (a7GetReloc (ndsHeader , moduleParams )) {
590+ patchSleepInputWrite (ce7 , ndsHeader , moduleParams , buttonsRemapped );
551591 u32 saveResult = 0 ;
552592 // Save Relocation Switch
553593 // Only when saveRelocation is turning off(FLASE) and GameCodeMatch is matching(TRUE),
@@ -603,6 +643,8 @@ u32 patchCardNdsArm7(
603643 if (!saveResult ) {
604644 patchOffsetCache .savePatchType = 0 ;
605645 }
646+ } else {
647+ patchSleepInputWrite (ce7 , ndsHeader , moduleParams , false);
606648 }
607649
608650 if (strcmp (romTid , "UBRP" ) == 0 ) {
0 commit comments