From 942ec4af9b90a876afbddb0811b7aa61e7353721 Mon Sep 17 00:00:00 2001 From: fincs Date: Sun, 11 Oct 2015 23:44:10 +0200 Subject: [PATCH] Add code to manage New 3DS CPU speedup --- libctru/include/3ds/os.h | 6 ++++++ libctru/include/3ds/services/ptm.h | 12 ++++++++++++ libctru/source/os.c | 17 +++++++++++++++++ libctru/source/services/apt.c | 12 +++++++----- libctru/source/services/ptm.c | 27 +++++++++++++++++++++++++-- 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/libctru/include/3ds/os.h b/libctru/include/3ds/os.h index 9c433f8..edd5963 100644 --- a/libctru/include/3ds/os.h +++ b/libctru/include/3ds/os.h @@ -83,3 +83,9 @@ u64 osGetTime(void); * These values correspond with the number of wifi bars displayed by Home Menu. */ u8 osGetWifiStrength(void); + +/** + * @brief Configures the New 3DS speedup. + * @param enable Specifies whether to enable or disable the speedup. + */ +void osSetSpeedupEnable(bool enable); diff --git a/libctru/include/3ds/services/ptm.h b/libctru/include/3ds/services/ptm.h index 17f3ea8..1ee87af 100644 --- a/libctru/include/3ds/services/ptm.h +++ b/libctru/include/3ds/services/ptm.h @@ -10,6 +10,12 @@ Result ptmInit(void); /// Exits PTM. Result ptmExit(void); +/// Initializes ptm:sysm. +Result ptmSysmInit(void); + +/// Exits ptm:sysm. +Result ptmSysmExit(void); + /** * @brief Gets the system's current shell state. * @param servhandle Optional pointer to the handle to use. @@ -44,3 +50,9 @@ Result PTMU_GetPedometerState(Handle* servhandle, u8 *out); * @param steps Pointer to write the total step count to. */ Result PTMU_GetTotalStepCount(Handle* servhandle, u32 *steps); + +/** + * @brief Configures the New 3DS' CPU clock speed and L2 cache. + * @param value Bit0: enable higher clock, Bit1: enable L2 cache. + */ +Result PTMSYSM_ConfigureNew3DSCPU(u8 value); diff --git a/libctru/source/os.c b/libctru/source/os.c index edb19c7..338546d 100644 --- a/libctru/source/os.c +++ b/libctru/source/os.c @@ -1,6 +1,7 @@ #include <3ds/types.h> #include <3ds/os.h> #include <3ds/svc.h> +#include <3ds/services/ptm.h> #include #include @@ -26,6 +27,7 @@ static volatile datetime_t* __datetime0 = static volatile datetime_t* __datetime1 = (datetime_t*) 0x1FF81040; +__attribute__((weak)) bool __ctru_speedup = false; //--------------------------------------------------------------------------------- u32 osConvertVirtToPhys(u32 vaddr) { @@ -155,3 +157,18 @@ u8 osGetWifiStrength(void) { //--------------------------------------------------------------------------------- return *((u8*)0x1FF81066); } + +void __ctru_speedup_config(void) +{ + if (ptmSysmInit()==0) + { + PTMSYSM_ConfigureNew3DSCPU(__ctru_speedup ? 3 : 0); + ptmSysmExit(); + } +} + +void osSetSpeedupEnable(bool enable) +{ + __ctru_speedup = enable; + __ctru_speedup_config(); +} diff --git a/libctru/source/services/apt.c b/libctru/source/services/apt.c index 6e7e136..70686d7 100644 --- a/libctru/source/services/apt.c +++ b/libctru/source/services/apt.c @@ -63,6 +63,8 @@ __attribute__((weak)) void _aptDebug(int a, int b) { } +void __ctru_speedup_config(void); + static void aptAppStarted(void); static bool aptIsReinit(void) @@ -396,7 +398,7 @@ static bool __handle_incoming_parameter(void) { aptSetStatus(APP_APPLETCLOSED); return true; case 0xB: // Just returned from menu. - if (aptStatusMutex) + if (aptGetStatus() != APP_NOTINITIALIZED) { GSPGPU_AcquireRight(0x0); GSPGPU_RestoreVramSysArea(); @@ -463,6 +465,8 @@ Result aptInit(void) svcCreateEvent(&aptStatusEvent, 0); svcCreateEvent(&aptSleepSync, 0); + svcCreateMutex(&aptStatusMutex, false); + aptStatus=0; if(!aptIsCrippled()) { @@ -627,10 +631,6 @@ void aptAppStarted(void) { u8 buf1[4], buf2[4]; - svcCreateMutex(&aptStatusMutex, true); - aptStatus=0; - svcReleaseMutex(aptStatusMutex); - aptSetStatus(APP_RUNNING); if(!aptIsCrippled()) @@ -670,6 +670,8 @@ void aptSetStatus(APP_STATUS status) //if(prevstatus != APP_NOTINITIALIZED) //{ + if(status == APP_RUNNING) + __ctru_speedup_config(); if(status == APP_RUNNING || status == APP_EXITING || status == APP_APPLETSTARTED || status == APP_APPLETCLOSED) svcSignalEvent(aptStatusEvent); //} diff --git a/libctru/source/services/ptm.c b/libctru/source/services/ptm.c index 96de48a..c3851c1 100644 --- a/libctru/source/services/ptm.c +++ b/libctru/source/services/ptm.c @@ -6,11 +6,11 @@ #include <3ds/ipc.h> -static Handle ptmHandle; +static Handle ptmHandle, ptmSysmHandle; Result ptmInit(void) { - return srvGetServiceHandle(&ptmHandle, "ptm:u"); + return srvGetServiceHandle(&ptmHandle, "ptm:u"); } Result ptmExit(void) @@ -18,6 +18,16 @@ Result ptmExit(void) return svcCloseHandle(ptmHandle); } +Result ptmSysmInit(void) +{ + return srvGetServiceHandle(&ptmSysmHandle, "ptm:sysm"); +} + +Result ptmSysmExit(void) +{ + return svcCloseHandle(ptmSysmHandle); +} + Result PTMU_GetShellState(Handle* servhandle, u8 *out) { if(!servhandle)servhandle=&ptmHandle; @@ -92,3 +102,16 @@ Result PTMU_GetTotalStepCount(Handle* servhandle, u32 *steps) return (Result)cmdbuf[1]; } + +Result PTMSYSM_ConfigureNew3DSCPU(u8 value) +{ + Result ret; + u32 *cmdbuf = getThreadCommandBuffer(); + + cmdbuf[0] = IPC_MakeHeader(0x818,1,0); // 0x08180040 + cmdbuf[1] = value; + + if((ret = svcSendSyncRequest(ptmSysmHandle))!=0)return ret; + + return (Result)cmdbuf[1]; +}