From 4993b321ce21f2cf9547600804f835e5553fec39 Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 8 Feb 2022 14:44:53 +0100 Subject: [PATCH] clang-format --- src/cafe/coreinit.cpp | 242 +++---- src/cafe/coreinit.h | 517 +++++++------- src/cafe/nn_act.cpp | 24 +- src/cafe/nn_act.h | 10 +- src/cafe/nn_save.cpp | 14 +- src/cafe/nsysnet.cpp | 30 +- src/cafe/nsysnet.h | 20 +- src/cafe/sysapp.cpp | 16 +- src/cafe/vpad.cpp | 8 +- src/cafe/vpad.h | 118 ++-- src/color.h | 20 +- src/debugger.cpp | 1497 ++++++++++++++++++++--------------------- src/debugger.h | 376 +++++------ src/exceptions.cpp | 67 +- src/hbl.h | 8 +- src/input.cpp | 24 +- src/kernel.cpp | 70 +- src/main.cpp | 125 ++-- src/memory.cpp | 20 +- src/menu.cpp | 57 +- src/menu.h | 34 +- src/patches.cpp | 73 +- src/screen.cpp | 86 +-- src/screen.h | 52 +- src/socket.cpp | 143 ++-- src/socket.h | 28 +- 26 files changed, 1824 insertions(+), 1855 deletions(-) diff --git a/src/cafe/coreinit.cpp b/src/cafe/coreinit.cpp index 69bdcae..80898a9 100644 --- a/src/cafe/coreinit.cpp +++ b/src/cafe/coreinit.cpp @@ -1,9 +1,9 @@ #include "cafe/coreinit.h" #include "hbl.h" -#include #include - +#include + int (*OSDynLoad_Acquire)(const char *name, uint32_t *handle); int (*OSDynLoad_FindExport)(uint32_t handle, bool isData, const char *name, void *ptr); int (*OSDynLoad_GetModuleName)(uint32_t handle, char *name, int *size); @@ -117,132 +117,132 @@ uint32_t (*MEMGetAllocatableSizeForExpHeapEx)(void *handle, int alignment); void *(**pMEMAllocFromDefaultHeap)(uint32_t size); void *(**pMEMAllocFromDefaultHeapEx)(uint32_t size, int alignment); -void (**pMEMFreeToDefaultHeap)(void *ptr); +void (**pMEMFreeToDefaultHeap)(void *ptr); OSDynLoad_RPLInfo **pMainRPL; OSDynLoad_RPLInfo **pFirstRPL; OSThread **pThreadList; void coreinitInitialize() { - *(uint32_t *)&OSDynLoad_Acquire = OS_SPECIFICS->OSDynLoad_Acquire; - *(uint32_t *)&OSDynLoad_FindExport = OS_SPECIFICS->OSDynLoad_FindExport; - - uint32_t handle; - OSDynLoad_Acquire("coreinit.rpl", &handle); - - OSDynLoad_FindExport(handle, false, "OSDynLoad_GetModuleName", &OSDynLoad_GetModuleName); - OSDynLoad_FindExport(handle, true, "OSDynLoad_gLoaderLock", &OSDynLoad_gLoaderLock); - - OSDynLoad_FindExport(handle, false, "OSIsDebuggerInitialized", &OSIsDebuggerInitialized); - - OSDynLoad_FindExport(handle, false, "exit", &exit); - OSDynLoad_FindExport(handle, false, "_Exit", &_Exit); + *(uint32_t *) &OSDynLoad_Acquire = OS_SPECIFICS->OSDynLoad_Acquire; + *(uint32_t *) &OSDynLoad_FindExport = OS_SPECIFICS->OSDynLoad_FindExport; - OSDynLoad_FindExport(handle, false, "OSFatal", &OSFatal); - - OSDynLoad_FindExport(handle, false, "OSGetSymbolName", &OSGetSymbolName); - OSDynLoad_FindExport(handle, false, "DisassemblePPCRange", &DisassemblePPCRange); - OSDynLoad_FindExport(handle, false, "DisassemblePPCOpcode", &DisassemblePPCOpcode); - - OSDynLoad_FindExport(handle, false, "OSSetExceptionCallback", &OSSetExceptionCallback); - OSDynLoad_FindExport(handle, false, "OSSetExceptionCallbackEx", &OSSetExceptionCallbackEx); - OSDynLoad_FindExport(handle, false, "OSLoadContext", &OSLoadContext); - - OSDynLoad_FindExport(handle, false, "OSDisableInterrupts", &OSDisableInterrupts); - OSDynLoad_FindExport(handle, false, "OSRestoreInterrupts", &OSRestoreInterrupts); - - OSDynLoad_FindExport(handle, false, "__OSLockScheduler", &__OSLockScheduler); - OSDynLoad_FindExport(handle, false, "__OSUnlockScheduler", &__OSUnlockScheduler); - - OSDynLoad_FindExport(handle, false, "OSInitMutex", &OSInitMutex); - OSDynLoad_FindExport(handle, false, "OSLockMutex", &OSLockMutex); - OSDynLoad_FindExport(handle, false, "OSUnlockMutex", &OSUnlockMutex); - - OSDynLoad_FindExport(handle, false, "OSInitMessageQueue", &OSInitMessageQueue); - OSDynLoad_FindExport(handle, false, "OSSendMessage", &OSSendMessage); - OSDynLoad_FindExport(handle, false, "OSReceiveMessage", &OSReceiveMessage); + uint32_t handle; + OSDynLoad_Acquire("coreinit.rpl", &handle); - OSDynLoad_FindExport(handle, false, "OSCreateAlarm", &OSCreateAlarm); - OSDynLoad_FindExport(handle, false, "OSSetAlarm", &OSSetAlarm); - OSDynLoad_FindExport(handle, false, "OSCancelAlarm", &OSCancelAlarm); - OSDynLoad_FindExport(handle, false, "OSWaitAlarm", &OSWaitAlarm); + OSDynLoad_FindExport(handle, false, "OSDynLoad_GetModuleName", &OSDynLoad_GetModuleName); + OSDynLoad_FindExport(handle, true, "OSDynLoad_gLoaderLock", &OSDynLoad_gLoaderLock); - OSDynLoad_FindExport(handle, false, "OSGetCurrentThread", &OSGetCurrentThread); - OSDynLoad_FindExport(handle, false, "OSGetDefaultThread", &OSGetDefaultThread); - OSDynLoad_FindExport(handle, false, "OSCreateThread", &OSCreateThread); - OSDynLoad_FindExport(handle, false, "OSResumeThread", &OSResumeThread); - OSDynLoad_FindExport(handle, false, "OSJoinThread", &OSJoinThread); - OSDynLoad_FindExport(handle, false, "OSExitThread", &OSExitThread); - OSDynLoad_FindExport(handle, false, "OSGetThreadName", &OSGetThreadName); - OSDynLoad_FindExport(handle, false, "OSSetThreadName", &OSSetThreadName); - OSDynLoad_FindExport(handle, false, "OSGetThreadAffinity", &OSGetThreadAffinity); - OSDynLoad_FindExport(handle, false, "OSGetThreadPriority", &OSGetThreadPriority); - - OSDynLoad_FindExport(handle, false, "OSGetSystemInfo", &OSGetSystemInfo); - OSDynLoad_FindExport(handle, false, "OSSleepTicks", &OSSleepTicks); - - OSDynLoad_FindExport(handle, false, "OSGetTitleID", &OSGetTitleID); - - OSDynLoad_FindExport(handle, false, "OSGetMemBound", &OSGetMemBound); - OSDynLoad_FindExport(handle, false, "OSEffectiveToPhysical", &OSEffectiveToPhysical); - - OSDynLoad_FindExport(handle, false, "OSScreenInit", &OSScreenInit); - OSDynLoad_FindExport(handle, false, "OSScreenGetBufferSizeEx", &OSScreenGetBufferSizeEx); - OSDynLoad_FindExport(handle, false, "OSScreenSetBufferEx", &OSScreenSetBufferEx); - OSDynLoad_FindExport(handle, false, "OSScreenEnableEx", &OSScreenEnableEx); - OSDynLoad_FindExport(handle, false, "OSScreenClearBufferEx", &OSScreenClearBufferEx); - OSDynLoad_FindExport(handle, false, "OSScreenFlipBuffersEx", &OSScreenFlipBuffersEx); - OSDynLoad_FindExport(handle, false, "OSScreenPutPixelEx", &OSScreenPutPixelEx); - OSDynLoad_FindExport(handle, false, "OSScreenPutFontEx", &OSScreenPutFontEx); - - OSDynLoad_FindExport(handle, false, "DCFlushRange", &DCFlushRange); - OSDynLoad_FindExport(handle, false, "DCInvalidateRange", &DCInvalidateRange); - OSDynLoad_FindExport(handle, false, "ICInvalidateRange", &ICInvalidateRange); - OSDynLoad_FindExport(handle, false, "IOS_Open", &IOS_Open); - OSDynLoad_FindExport(handle, false, "IOS_Ioctl", &IOS_Ioctl); - OSDynLoad_FindExport(handle, false, "IOS_Close", &IOS_Close); - - OSDynLoad_FindExport(handle, false, "FSInit", &FSInit); - OSDynLoad_FindExport(handle, false, "FSAddClient", &FSAddClient); - OSDynLoad_FindExport(handle, false, "FSInitCmdBlock", &FSInitCmdBlock); - OSDynLoad_FindExport(handle, false, "FSGetMountSource", &FSGetMountSource); - OSDynLoad_FindExport(handle, false, "FSMount", &FSMount); - OSDynLoad_FindExport(handle, false, "FSMakeDir", &FSMakeDir); - OSDynLoad_FindExport(handle, false, "FSChangeDir", &FSChangeDir); - OSDynLoad_FindExport(handle, false, "FSGetCwd", &FSGetCwd); - OSDynLoad_FindExport(handle, false, "FSOpenDir", &FSOpenDir); - OSDynLoad_FindExport(handle, false, "FSReadDir", &FSReadDir); - OSDynLoad_FindExport(handle, false, "FSCloseDir", &FSCloseDir); - OSDynLoad_FindExport(handle, false, "FSOpenFile", &FSOpenFile); - OSDynLoad_FindExport(handle, false, "FSGetStatFile", &FSGetStatFile); - OSDynLoad_FindExport(handle, false, "FSReadFile", &FSReadFile); - OSDynLoad_FindExport(handle, false, "FSWriteFile", &FSWriteFile); - OSDynLoad_FindExport(handle, false, "FSCloseFile", &FSCloseFile); - OSDynLoad_FindExport(handle, false, "FSGetStat", &FSGetStat); - OSDynLoad_FindExport(handle, false, "FSRename", &FSRename); - OSDynLoad_FindExport(handle, false, "FSRemove", &FSRemove); - OSDynLoad_FindExport(handle, false, "FSDelClient", &FSDelClient); - OSDynLoad_FindExport(handle, false, "FSShutdown", &FSShutdown); - - OSDynLoad_FindExport(handle, false, "MCP_Open", &MCP_Open); - OSDynLoad_FindExport(handle, false, "MCP_GetDeviceId", &MCP_GetDeviceId); - OSDynLoad_FindExport(handle, false, "MCP_TitleList", &MCP_TitleList); - OSDynLoad_FindExport(handle, false, "MCP_TitleListByAppType", &MCP_TitleListByAppType); - OSDynLoad_FindExport(handle, false, "MCP_TitleListByDevice", &MCP_TitleListByDevice); - OSDynLoad_FindExport(handle, false, "MCP_Close", &MCP_Close); - - OSDynLoad_FindExport(handle, false, "__KernelGetInfo", &__KernelGetInfo); - - OSDynLoad_FindExport(handle, false, "__os_snprintf", &snprintf); - - OSDynLoad_FindExport(handle, false, "MEMGetBaseHeapHandle", &MEMGetBaseHeapHandle); - OSDynLoad_FindExport(handle, false, "MEMGetAllocatableSizeForExpHeapEx", &MEMGetAllocatableSizeForExpHeapEx); - - OSDynLoad_FindExport(handle, true, "MEMAllocFromDefaultHeap", &pMEMAllocFromDefaultHeap); - OSDynLoad_FindExport(handle, true, "MEMAllocFromDefaultHeapEx", &pMEMAllocFromDefaultHeapEx); - OSDynLoad_FindExport(handle, true, "MEMFreeToDefaultHeap", &pMEMFreeToDefaultHeap); - - pMainRPL = (OSDynLoad_RPLInfo **)0x10081014; - pFirstRPL = (OSDynLoad_RPLInfo **)0x10081018; - pThreadList = (OSThread **)0x100567F8; + OSDynLoad_FindExport(handle, false, "OSIsDebuggerInitialized", &OSIsDebuggerInitialized); + + OSDynLoad_FindExport(handle, false, "exit", &exit); + OSDynLoad_FindExport(handle, false, "_Exit", &_Exit); + + OSDynLoad_FindExport(handle, false, "OSFatal", &OSFatal); + + OSDynLoad_FindExport(handle, false, "OSGetSymbolName", &OSGetSymbolName); + OSDynLoad_FindExport(handle, false, "DisassemblePPCRange", &DisassemblePPCRange); + OSDynLoad_FindExport(handle, false, "DisassemblePPCOpcode", &DisassemblePPCOpcode); + + OSDynLoad_FindExport(handle, false, "OSSetExceptionCallback", &OSSetExceptionCallback); + OSDynLoad_FindExport(handle, false, "OSSetExceptionCallbackEx", &OSSetExceptionCallbackEx); + OSDynLoad_FindExport(handle, false, "OSLoadContext", &OSLoadContext); + + OSDynLoad_FindExport(handle, false, "OSDisableInterrupts", &OSDisableInterrupts); + OSDynLoad_FindExport(handle, false, "OSRestoreInterrupts", &OSRestoreInterrupts); + + OSDynLoad_FindExport(handle, false, "__OSLockScheduler", &__OSLockScheduler); + OSDynLoad_FindExport(handle, false, "__OSUnlockScheduler", &__OSUnlockScheduler); + + OSDynLoad_FindExport(handle, false, "OSInitMutex", &OSInitMutex); + OSDynLoad_FindExport(handle, false, "OSLockMutex", &OSLockMutex); + OSDynLoad_FindExport(handle, false, "OSUnlockMutex", &OSUnlockMutex); + + OSDynLoad_FindExport(handle, false, "OSInitMessageQueue", &OSInitMessageQueue); + OSDynLoad_FindExport(handle, false, "OSSendMessage", &OSSendMessage); + OSDynLoad_FindExport(handle, false, "OSReceiveMessage", &OSReceiveMessage); + + OSDynLoad_FindExport(handle, false, "OSCreateAlarm", &OSCreateAlarm); + OSDynLoad_FindExport(handle, false, "OSSetAlarm", &OSSetAlarm); + OSDynLoad_FindExport(handle, false, "OSCancelAlarm", &OSCancelAlarm); + OSDynLoad_FindExport(handle, false, "OSWaitAlarm", &OSWaitAlarm); + + OSDynLoad_FindExport(handle, false, "OSGetCurrentThread", &OSGetCurrentThread); + OSDynLoad_FindExport(handle, false, "OSGetDefaultThread", &OSGetDefaultThread); + OSDynLoad_FindExport(handle, false, "OSCreateThread", &OSCreateThread); + OSDynLoad_FindExport(handle, false, "OSResumeThread", &OSResumeThread); + OSDynLoad_FindExport(handle, false, "OSJoinThread", &OSJoinThread); + OSDynLoad_FindExport(handle, false, "OSExitThread", &OSExitThread); + OSDynLoad_FindExport(handle, false, "OSGetThreadName", &OSGetThreadName); + OSDynLoad_FindExport(handle, false, "OSSetThreadName", &OSSetThreadName); + OSDynLoad_FindExport(handle, false, "OSGetThreadAffinity", &OSGetThreadAffinity); + OSDynLoad_FindExport(handle, false, "OSGetThreadPriority", &OSGetThreadPriority); + + OSDynLoad_FindExport(handle, false, "OSGetSystemInfo", &OSGetSystemInfo); + OSDynLoad_FindExport(handle, false, "OSSleepTicks", &OSSleepTicks); + + OSDynLoad_FindExport(handle, false, "OSGetTitleID", &OSGetTitleID); + + OSDynLoad_FindExport(handle, false, "OSGetMemBound", &OSGetMemBound); + OSDynLoad_FindExport(handle, false, "OSEffectiveToPhysical", &OSEffectiveToPhysical); + + OSDynLoad_FindExport(handle, false, "OSScreenInit", &OSScreenInit); + OSDynLoad_FindExport(handle, false, "OSScreenGetBufferSizeEx", &OSScreenGetBufferSizeEx); + OSDynLoad_FindExport(handle, false, "OSScreenSetBufferEx", &OSScreenSetBufferEx); + OSDynLoad_FindExport(handle, false, "OSScreenEnableEx", &OSScreenEnableEx); + OSDynLoad_FindExport(handle, false, "OSScreenClearBufferEx", &OSScreenClearBufferEx); + OSDynLoad_FindExport(handle, false, "OSScreenFlipBuffersEx", &OSScreenFlipBuffersEx); + OSDynLoad_FindExport(handle, false, "OSScreenPutPixelEx", &OSScreenPutPixelEx); + OSDynLoad_FindExport(handle, false, "OSScreenPutFontEx", &OSScreenPutFontEx); + + OSDynLoad_FindExport(handle, false, "DCFlushRange", &DCFlushRange); + OSDynLoad_FindExport(handle, false, "DCInvalidateRange", &DCInvalidateRange); + OSDynLoad_FindExport(handle, false, "ICInvalidateRange", &ICInvalidateRange); + OSDynLoad_FindExport(handle, false, "IOS_Open", &IOS_Open); + OSDynLoad_FindExport(handle, false, "IOS_Ioctl", &IOS_Ioctl); + OSDynLoad_FindExport(handle, false, "IOS_Close", &IOS_Close); + + OSDynLoad_FindExport(handle, false, "FSInit", &FSInit); + OSDynLoad_FindExport(handle, false, "FSAddClient", &FSAddClient); + OSDynLoad_FindExport(handle, false, "FSInitCmdBlock", &FSInitCmdBlock); + OSDynLoad_FindExport(handle, false, "FSGetMountSource", &FSGetMountSource); + OSDynLoad_FindExport(handle, false, "FSMount", &FSMount); + OSDynLoad_FindExport(handle, false, "FSMakeDir", &FSMakeDir); + OSDynLoad_FindExport(handle, false, "FSChangeDir", &FSChangeDir); + OSDynLoad_FindExport(handle, false, "FSGetCwd", &FSGetCwd); + OSDynLoad_FindExport(handle, false, "FSOpenDir", &FSOpenDir); + OSDynLoad_FindExport(handle, false, "FSReadDir", &FSReadDir); + OSDynLoad_FindExport(handle, false, "FSCloseDir", &FSCloseDir); + OSDynLoad_FindExport(handle, false, "FSOpenFile", &FSOpenFile); + OSDynLoad_FindExport(handle, false, "FSGetStatFile", &FSGetStatFile); + OSDynLoad_FindExport(handle, false, "FSReadFile", &FSReadFile); + OSDynLoad_FindExport(handle, false, "FSWriteFile", &FSWriteFile); + OSDynLoad_FindExport(handle, false, "FSCloseFile", &FSCloseFile); + OSDynLoad_FindExport(handle, false, "FSGetStat", &FSGetStat); + OSDynLoad_FindExport(handle, false, "FSRename", &FSRename); + OSDynLoad_FindExport(handle, false, "FSRemove", &FSRemove); + OSDynLoad_FindExport(handle, false, "FSDelClient", &FSDelClient); + OSDynLoad_FindExport(handle, false, "FSShutdown", &FSShutdown); + + OSDynLoad_FindExport(handle, false, "MCP_Open", &MCP_Open); + OSDynLoad_FindExport(handle, false, "MCP_GetDeviceId", &MCP_GetDeviceId); + OSDynLoad_FindExport(handle, false, "MCP_TitleList", &MCP_TitleList); + OSDynLoad_FindExport(handle, false, "MCP_TitleListByAppType", &MCP_TitleListByAppType); + OSDynLoad_FindExport(handle, false, "MCP_TitleListByDevice", &MCP_TitleListByDevice); + OSDynLoad_FindExport(handle, false, "MCP_Close", &MCP_Close); + + OSDynLoad_FindExport(handle, false, "__KernelGetInfo", &__KernelGetInfo); + + OSDynLoad_FindExport(handle, false, "__os_snprintf", &snprintf); + + OSDynLoad_FindExport(handle, false, "MEMGetBaseHeapHandle", &MEMGetBaseHeapHandle); + OSDynLoad_FindExport(handle, false, "MEMGetAllocatableSizeForExpHeapEx", &MEMGetAllocatableSizeForExpHeapEx); + + OSDynLoad_FindExport(handle, true, "MEMAllocFromDefaultHeap", &pMEMAllocFromDefaultHeap); + OSDynLoad_FindExport(handle, true, "MEMAllocFromDefaultHeapEx", &pMEMAllocFromDefaultHeapEx); + OSDynLoad_FindExport(handle, true, "MEMFreeToDefaultHeap", &pMEMFreeToDefaultHeap); + + pMainRPL = (OSDynLoad_RPLInfo **) 0x10081014; + pFirstRPL = (OSDynLoad_RPLInfo **) 0x10081018; + pThreadList = (OSThread **) 0x100567F8; } diff --git a/src/cafe/coreinit.h b/src/cafe/coreinit.h index c63376b..c91d832 100644 --- a/src/cafe/coreinit.h +++ b/src/cafe/coreinit.h @@ -1,101 +1,101 @@ #pragma once -#include #include +#include // Timers -#define OSTimerClockSpeed ((OSGetSystemInfo()->busClockSpeed) / 4) +#define OSTimerClockSpeed ((OSGetSystemInfo()->busClockSpeed) / 4) -#define OSSecondsToTicks(val) ((uint64_t)(val) * (uint64_t)OSTimerClockSpeed) -#define OSMillisecondsToTicks(val) (((uint64_t)(val) * (uint64_t)OSTimerClockSpeed) / 1000ull) +#define OSSecondsToTicks(val) ((uint64_t) (val) * (uint64_t) OSTimerClockSpeed) +#define OSMillisecondsToTicks(val) (((uint64_t) (val) * (uint64_t) OSTimerClockSpeed) / 1000ull) -#define OSTicksToSeconds(val) ((uint64_t)(val) / (uint64_t)OSTimerClockSpeed) -#define OSTicksToMilliseconds(val) (((uint64_t)(val) * 1000ull) / (uint64_t)OSTimerClockSpeed) +#define OSTicksToSeconds(val) ((uint64_t) (val) / (uint64_t) OSTimerClockSpeed) +#define OSTicksToMilliseconds(val) (((uint64_t) (val) *1000ull) / (uint64_t) OSTimerClockSpeed) // Memory enum OSMemoryType { - MEM1 = 1, - MEM2 = 2 + MEM1 = 1, + MEM2 = 2 }; // System enum KernelInfoType { - TITLE_INFO = 0, - SYSTEM_INFO = 1, - PLATFORM_INFO = 2, - - KERNEL_STATISTICS = 4, - PERFORMANCE_NUMBERS = 5, - - PROCESS_INFO = 8, - - CRASH_INFO = 11, - APP_CRASH_CONTROL = 12, - COS_REPORT_MASKS = 13, - CRASH_RECOVERY = 14, - CRASH_DETAIL_LEVEL = 15, - CRASH_DUMP_TYPE = 16, - SHUTDOWN_REASON = 17, - WRITE_GATHER_REGS = 18, - PROC_DATA_BOUNDS = 19 + TITLE_INFO = 0, + SYSTEM_INFO = 1, + PLATFORM_INFO = 2, + + KERNEL_STATISTICS = 4, + PERFORMANCE_NUMBERS = 5, + + PROCESS_INFO = 8, + + CRASH_INFO = 11, + APP_CRASH_CONTROL = 12, + COS_REPORT_MASKS = 13, + CRASH_RECOVERY = 14, + CRASH_DETAIL_LEVEL = 15, + CRASH_DUMP_TYPE = 16, + SHUTDOWN_REASON = 17, + WRITE_GATHER_REGS = 18, + PROC_DATA_BOUNDS = 19 }; struct OSSystemInfo { - uint32_t busClockSpeed; - uint32_t coreClockSpeed; - int64_t baseTime; - char _10[0x10]; + uint32_t busClockSpeed; + uint32_t coreClockSpeed; + int64_t baseTime; + char _10[0x10]; }; struct OSTitleInfo { - char _0[0xC]; - uint32_t ramStart; - uint32_t ramEnd; - char _14[0x20]; - uint32_t systemHeapSize; - char _38[0x40]; - uint32_t textStart; - uint32_t _7C; - uint32_t textSize; - uint32_t dataStart; - uint32_t _88; - uint32_t dataSize; - uint32_t loadStart; - uint32_t _94; - uint32_t loadSize; - char _9C[0xC]; + char _0[0xC]; + uint32_t ramStart; + uint32_t ramEnd; + char _14[0x20]; + uint32_t systemHeapSize; + char _38[0x40]; + uint32_t textStart; + uint32_t _7C; + uint32_t textSize; + uint32_t dataStart; + uint32_t _88; + uint32_t dataSize; + uint32_t loadStart; + uint32_t _94; + uint32_t loadSize; + char _9C[0xC]; }; struct OSTitleInfoEx { - OSTitleInfo info; - uint64_t osVersionId; + OSTitleInfo info; + uint64_t osVersionId; }; // OSDynLoad struct OSDynLoad_NotifyData { - const char *path; - uint32_t textAddr; - uint32_t textOffset; - uint32_t textSize; - uint32_t dataAddr; - uint32_t dataOffset; - uint32_t dataSize; - uint32_t readAddr; - uint32_t readOffset; - uint32_t readSize; + const char *path; + uint32_t textAddr; + uint32_t textOffset; + uint32_t textSize; + uint32_t dataAddr; + uint32_t dataOffset; + uint32_t dataSize; + uint32_t readAddr; + uint32_t readOffset; + uint32_t readSize; }; struct OSDynLoad_RPLInfo { - uint32_t handle; - uint32_t _4; - const char *name; - char _C[0x1C]; - OSDynLoad_NotifyData *notifyData; - void *entryPoint; - char _30[0x24]; - OSDynLoad_RPLInfo *next; - char _58[0x3C]; + uint32_t handle; + uint32_t _4; + const char *name; + char _C[0x1C]; + OSDynLoad_NotifyData *notifyData; + void *entryPoint; + char _30[0x24]; + OSDynLoad_RPLInfo *next; + char _58[0x3C]; }; // Thread / mutex / context @@ -104,188 +104,187 @@ struct OSMutex; struct OSAlarm; struct OSContext { - uint64_t tag; - - uint32_t gpr[32]; + uint64_t tag; - uint32_t cr; - uint32_t lr; - uint32_t ctr; - uint32_t xer; - - uint32_t srr0; - uint32_t srr1; - - uint32_t dsisr; - uint32_t dar; - - char _A8[0xC]; - - uint32_t fpscr; - double fpr[32]; - - uint16_t spinLockCount; - uint16_t state; - - uint32_t gqr[8]; - - uint32_t _1DC; - - double psf[32]; - - uint64_t coretime[3]; - uint64_t starttime; - - uint32_t error; - - uint32_t _304; - - uint32_t pmc1; - uint32_t pmc2; - uint32_t pmc3; - uint32_t pmc4; - uint32_t mmcr0; - uint32_t mmcr1; + uint32_t gpr[32]; + + uint32_t cr; + uint32_t lr; + uint32_t ctr; + uint32_t xer; + + uint32_t srr0; + uint32_t srr1; + + uint32_t dsisr; + uint32_t dar; + + char _A8[0xC]; + + uint32_t fpscr; + double fpr[32]; + + uint16_t spinLockCount; + uint16_t state; + + uint32_t gqr[8]; + + uint32_t _1DC; + + double psf[32]; + + uint64_t coretime[3]; + uint64_t starttime; + + uint32_t error; + + uint32_t _304; + + uint32_t pmc1; + uint32_t pmc2; + uint32_t pmc3; + uint32_t pmc4; + uint32_t mmcr0; + uint32_t mmcr1; }; struct OSThreadQueue { - OSThread *head; - OSThread *tail; - void *parent; - uint32_t _C; + OSThread *head; + OSThread *tail; + void *parent; + uint32_t _C; }; struct OSThreadLink { - OSThread *next; - OSThread *prev; + OSThread *next; + OSThread *prev; }; struct OSMutexLink { - OSMutex *next; - OSMutex *prev; + OSMutex *next; + OSMutex *prev; }; struct OSMutexQueue { - OSMutex *head; - OSMutex *tail; - void *parent; - uint32_t _C; + OSMutex *head; + OSMutex *tail; + void *parent; + uint32_t _C; }; struct OSMutex { - uint32_t tag; - const char *name; - uint32_t _8; - - OSThreadQueue queue; - OSThread *thread; - int count; - OSMutexLink link; + uint32_t tag; + const char *name; + uint32_t _8; + + OSThreadQueue queue; + OSThread *thread; + int count; + OSMutexLink link; }; typedef int (*OSThreadFunc)(int argc, void *argv); struct OSThread { - OSContext context; - - uint32_t tag; + OSContext context; - uint8_t state; - uint8_t attr; - uint16_t id; - uint32_t suspendCounter; - - int priority; - int basePriority; + uint32_t tag; - int exitValue; - - char _338[0x24]; - - OSThreadQueue *queue; - OSThreadLink link; - - OSThreadQueue joinQueue; - - OSMutex *mutex; - OSMutexQueue mutexQueue; - - OSThreadLink activeLink; - - void *stackBase; - void *stackEnd; - - OSThreadFunc entryPoint; - - char _3A0[0x57C - 0x3A0]; - - void *specific[0x10]; - - int type; - - const char *name; - - char _5C4[0x6A0 - 0x5C4]; - + uint8_t state; + uint8_t attr; + uint16_t id; + uint32_t suspendCounter; + + int priority; + int basePriority; + + int exitValue; + + char _338[0x24]; + + OSThreadQueue *queue; + OSThreadLink link; + + OSThreadQueue joinQueue; + + OSMutex *mutex; + OSMutexQueue mutexQueue; + + OSThreadLink activeLink; + + void *stackBase; + void *stackEnd; + + OSThreadFunc entryPoint; + + char _3A0[0x57C - 0x3A0]; + + void *specific[0x10]; + + int type; + + const char *name; + + char _5C4[0x6A0 - 0x5C4]; }; // Messages enum OSMessageFlags { - OS_MESSAGE_FLAGS_NONE = 0, - OS_MESSAGE_FLAGS_BLOCKING = 1 + OS_MESSAGE_FLAGS_NONE = 0, + OS_MESSAGE_FLAGS_BLOCKING = 1 }; struct OSMessage { - uint32_t message; - uint32_t args[3]; + uint32_t message; + uint32_t args[3]; }; struct OSMessageQueue { - uint32_t tag; - const char *name; - uint32_t _8; - OSThreadQueue sendQueue; - OSThreadQueue recvQueue; - OSMessage *messages; - uint32_t size; - uint32_t first; - uint32_t used; + uint32_t tag; + const char *name; + uint32_t _8; + OSThreadQueue sendQueue; + OSThreadQueue recvQueue; + OSMessage *messages; + uint32_t size; + uint32_t first; + uint32_t used; }; // Alarms struct OSAlarmQueue { - uint32_t tag; - const char *name; - uint32_t _8; - - OSThreadQueue threadQueue; - OSAlarm *head; - OSAlarm *tail; + uint32_t tag; + const char *name; + uint32_t _8; + + OSThreadQueue threadQueue; + OSAlarm *head; + OSAlarm *tail; }; struct OSAlarmLink { - OSAlarm *prev; - OSAlarm *next; + OSAlarm *prev; + OSAlarm *next; }; typedef void (*OSAlarmCallback)(OSAlarm *alarm, OSContext *context); struct OSAlarm { - uint32_t tag; - const char *name; - uint32_t _8; - OSAlarmCallback callback; - uint32_t group; - uint32_t _14; - uint64_t nextFire; - OSAlarmLink link; - uint64_t period; - uint64_t start; - void *userData; - uint32_t state; - OSThreadQueue threadQueue; - OSAlarmQueue *alarmQueue; - OSContext *context; + uint32_t tag; + const char *name; + uint32_t _8; + OSAlarmCallback callback; + uint32_t group; + uint32_t _14; + uint64_t nextFire; + OSAlarmLink link; + uint64_t period; + uint64_t start; + void *userData; + uint32_t state; + OSThreadQueue threadQueue; + OSAlarmQueue *alarmQueue; + OSContext *context; }; // PPC disassembly @@ -293,94 +292,94 @@ typedef void (*DisassemblyPrintFn)(const char *fmt, ...); typedef uint32_t (*DisassemblyFindSymbolFn)(uint32_t addr, char *buffer, size_t bufsize); enum DisassemblyFlags { - DISASSEMBLY_FLAGS_NONE = 0, - DISASSEMBLY_FLAGS_SIMPLIFY = 1, - DISASSEMBLY_FLAGS_SPACE = 0x20, - DISASSEMBLY_FLAGS_PLAIN = 0x40, - DISASSEMBLY_FLAGS_NO_OPCODE = 0x80, - DISASSEMBLY_FLAGS_PRINT_SYMBOLS = 0x100 + DISASSEMBLY_FLAGS_NONE = 0, + DISASSEMBLY_FLAGS_SIMPLIFY = 1, + DISASSEMBLY_FLAGS_SPACE = 0x20, + DISASSEMBLY_FLAGS_PLAIN = 0x40, + DISASSEMBLY_FLAGS_NO_OPCODE = 0x80, + DISASSEMBLY_FLAGS_PRINT_SYMBOLS = 0x100 }; // Exceptions enum OSExceptionMode { - OS_EXCEPTION_MODE_THREAD = 1, - OS_EXCEPTION_MODE_GLOBAL = 2, - OS_EXCEPTION_MODE_THREAD_ALL_CORES = 3, - OS_EXCEPTION_MODE_GLOBAL_ALL_CORES = 4 + OS_EXCEPTION_MODE_THREAD = 1, + OS_EXCEPTION_MODE_GLOBAL = 2, + OS_EXCEPTION_MODE_THREAD_ALL_CORES = 3, + OS_EXCEPTION_MODE_GLOBAL_ALL_CORES = 4 }; enum OSExceptionType { - OS_EXCEPTION_TYPE_DSI = 2, - OS_EXCEPTION_TYPE_ISI = 3, - OS_EXCEPTION_TYPE_PROGRAM = 6 + OS_EXCEPTION_TYPE_DSI = 2, + OS_EXCEPTION_TYPE_ISI = 3, + OS_EXCEPTION_TYPE_PROGRAM = 6 }; typedef bool (*OSExceptionCallback)(OSContext *context); // File system enum FSMode { - FS_MODE_READ_OWNER = 0x400, - FS_MODE_WRITE_OWNER = 0x200, - FS_MODE_EXEC_OWNER = 0x100, - - FS_MODE_READ_GROUP = 0x040, - FS_MODE_WRITE_GROUP = 0x020, - FS_MODE_EXEC_GROUP = 0x010, - - FS_MODE_READ_OTHER = 0x004, - FS_MODE_WRITE_OTHER = 0x002, - FS_MODE_EXEC_OTHER = 0x001 + FS_MODE_READ_OWNER = 0x400, + FS_MODE_WRITE_OWNER = 0x200, + FS_MODE_EXEC_OWNER = 0x100, + + FS_MODE_READ_GROUP = 0x040, + FS_MODE_WRITE_GROUP = 0x020, + FS_MODE_EXEC_GROUP = 0x010, + + FS_MODE_READ_OTHER = 0x004, + FS_MODE_WRITE_OTHER = 0x002, + FS_MODE_EXEC_OTHER = 0x001 }; enum FSStatFlags { - FS_STAT_DIRECTORY = 0x80000000 + FS_STAT_DIRECTORY = 0x80000000 }; enum FSMountSourceType { - FS_MOUNT_SOURCE_SD = 0 + FS_MOUNT_SOURCE_SD = 0 }; struct FSClient { - char data[0x1700]; + char data[0x1700]; }; struct FSCmdBlock { - char data[0xA80]; + char data[0xA80]; }; struct FSMountSource { - char data[0x300]; + char data[0x300]; }; struct __attribute__((packed)) FSStat { - FSStatFlags flags; - FSMode mode; - uint32_t owner; - uint32_t group; - uint32_t size; - char _14[0xC]; - uint32_t entryId; - int64_t created; - int64_t modified; - char _34[0x30]; + FSStatFlags flags; + FSMode mode; + uint32_t owner; + uint32_t group; + uint32_t size; + char _14[0xC]; + uint32_t entryId; + int64_t created; + int64_t modified; + char _34[0x30]; }; struct FSDirectoryEntry { - FSStat info; - char name[256]; + FSStat info; + char name[256]; }; // MCP struct MCPTitleListType { - uint64_t titleId; - uint32_t _4; - char path[56]; - uint32_t appType; - char _48[0xC]; - uint8_t device; - char _55; - char indexedDevice[10]; - uint8_t _60; + uint64_t titleId; + uint32_t _4; + char path[56]; + uint32_t appType; + char _48[0xC]; + uint8_t device; + char _55; + char indexedDevice[10]; + uint8_t _60; }; // Function pointers @@ -497,17 +496,17 @@ extern uint32_t (*MEMGetAllocatableSizeForExpHeapEx)(void *handle, int alignment extern void *(**pMEMAllocFromDefaultHeap)(uint32_t size); extern void *(**pMEMAllocFromDefaultHeapEx)(uint32_t size, int alignment); -extern void (**pMEMFreeToDefaultHeap)(void *ptr); -#define MEMAllocFromDefaultHeap (*pMEMAllocFromDefaultHeap) +extern void (**pMEMFreeToDefaultHeap)(void *ptr); +#define MEMAllocFromDefaultHeap (*pMEMAllocFromDefaultHeap) #define MEMAllocFromDefaultHeapEx (*pMEMAllocFromDefaultHeapEx) -#define MEMFreeToDefaultHeap (*pMEMFreeToDefaultHeap) +#define MEMFreeToDefaultHeap (*pMEMFreeToDefaultHeap) // Internal extern OSDynLoad_RPLInfo **pMainRPL; extern OSDynLoad_RPLInfo **pFirstRPL; extern OSThread **pThreadList; -#define MainRPL (*pMainRPL) -#define FirstRPL (*pFirstRPL) +#define MainRPL (*pMainRPL) +#define FirstRPL (*pFirstRPL) #define ThreadList (*pThreadList) void coreinitInitialize(); diff --git a/src/cafe/nn_act.cpp b/src/cafe/nn_act.cpp index e71b22d..0a0456b 100644 --- a/src/cafe/nn_act.cpp +++ b/src/cafe/nn_act.cpp @@ -3,18 +3,18 @@ #include namespace nn::act { - uint32_t (*Initialize)(); - uint8_t (*GetSlotNo)(); - uint32_t (*GetPersistentIdEx)(uint8_t slot); - uint32_t (*Finalize)(); -} + uint32_t (*Initialize)(); + uint8_t (*GetSlotNo)(); + uint32_t (*GetPersistentIdEx)(uint8_t slot); + uint32_t (*Finalize)(); +} // namespace nn::act void nnactInitialize() { - uint32_t handle; - OSDynLoad_Acquire("nn_act.rpl", &handle); - - OSDynLoad_FindExport(handle, false, "Initialize__Q2_2nn3actFv", &nn::act::Initialize); - OSDynLoad_FindExport(handle, false, "GetSlotNo__Q2_2nn3actFv", &nn::act::GetSlotNo); - OSDynLoad_FindExport(handle, false, "GetPersistentIdEx__Q2_2nn3actFUc", &nn::act::GetPersistentIdEx); - OSDynLoad_FindExport(handle, false, "Finalize__Q2_2nn3actFv", &nn::act::Finalize); + uint32_t handle; + OSDynLoad_Acquire("nn_act.rpl", &handle); + + OSDynLoad_FindExport(handle, false, "Initialize__Q2_2nn3actFv", &nn::act::Initialize); + OSDynLoad_FindExport(handle, false, "GetSlotNo__Q2_2nn3actFv", &nn::act::GetSlotNo); + OSDynLoad_FindExport(handle, false, "GetPersistentIdEx__Q2_2nn3actFUc", &nn::act::GetPersistentIdEx); + OSDynLoad_FindExport(handle, false, "Finalize__Q2_2nn3actFv", &nn::act::Finalize); } diff --git a/src/cafe/nn_act.h b/src/cafe/nn_act.h index cdc77fd..43a9595 100644 --- a/src/cafe/nn_act.h +++ b/src/cafe/nn_act.h @@ -2,10 +2,10 @@ #include namespace nn::act { - extern uint32_t (*Initialize)(); - extern uint8_t (*GetSlotNo)(); - extern uint32_t (*GetPersistentIdEx)(uint8_t slot); - extern uint32_t (*Finalize)(); -} + extern uint32_t (*Initialize)(); + extern uint8_t (*GetSlotNo)(); + extern uint32_t (*GetPersistentIdEx)(uint8_t slot); + extern uint32_t (*Finalize)(); +} // namespace nn::act void nnactInitialize(); diff --git a/src/cafe/nn_save.cpp b/src/cafe/nn_save.cpp index 257de8f..fd0f4e6 100644 --- a/src/cafe/nn_save.cpp +++ b/src/cafe/nn_save.cpp @@ -8,11 +8,11 @@ int (*SAVEGetSharedDataTitlePath)(uint64_t titleId, const char *path, char *outp void (*SAVEShutdown)(); void nnsaveInitialize() { - uint32_t handle; - OSDynLoad_Acquire("nn_save.rpl", &handle); - - OSDynLoad_FindExport(handle, false, "SAVEInit", &SAVEInit); - OSDynLoad_FindExport(handle, false, "SAVEOpenFile", &SAVEOpenFile); - OSDynLoad_FindExport(handle, false, "SAVEGetSharedDataTitlePath", &SAVEGetSharedDataTitlePath); - OSDynLoad_FindExport(handle, false, "SAVEShutdown", &SAVEShutdown); + uint32_t handle; + OSDynLoad_Acquire("nn_save.rpl", &handle); + + OSDynLoad_FindExport(handle, false, "SAVEInit", &SAVEInit); + OSDynLoad_FindExport(handle, false, "SAVEOpenFile", &SAVEOpenFile); + OSDynLoad_FindExport(handle, false, "SAVEGetSharedDataTitlePath", &SAVEGetSharedDataTitlePath); + OSDynLoad_FindExport(handle, false, "SAVEShutdown", &SAVEShutdown); } diff --git a/src/cafe/nsysnet.cpp b/src/cafe/nsysnet.cpp index 33697c9..b566963 100644 --- a/src/cafe/nsysnet.cpp +++ b/src/cafe/nsysnet.cpp @@ -17,19 +17,19 @@ int (*socketclose)(int socket); int (*socket_lib_finish)(); void nsysnetInitialize() { - uint32_t handle; - OSDynLoad_Acquire("nsysnet.rpl", &handle); - - OSDynLoad_FindExport(handle, false, "socket_lib_init", &socket_lib_init); - OSDynLoad_FindExport(handle, false, "inet_aton", &inet_aton); - OSDynLoad_FindExport(handle, false, "socket", &socket); - OSDynLoad_FindExport(handle, false, "setsockopt", &setsockopt); - OSDynLoad_FindExport(handle, false, "connect", &connect); - OSDynLoad_FindExport(handle, false, "bind", &bind); - OSDynLoad_FindExport(handle, false, "listen", &listen); - OSDynLoad_FindExport(handle, false, "accept", &accept); - OSDynLoad_FindExport(handle, false, "send", &send); - OSDynLoad_FindExport(handle, false, "recv", &recv); - OSDynLoad_FindExport(handle, false, "socketclose", &socketclose); - OSDynLoad_FindExport(handle, false, "socket_lib_finish", &socket_lib_finish); + uint32_t handle; + OSDynLoad_Acquire("nsysnet.rpl", &handle); + + OSDynLoad_FindExport(handle, false, "socket_lib_init", &socket_lib_init); + OSDynLoad_FindExport(handle, false, "inet_aton", &inet_aton); + OSDynLoad_FindExport(handle, false, "socket", &socket); + OSDynLoad_FindExport(handle, false, "setsockopt", &setsockopt); + OSDynLoad_FindExport(handle, false, "connect", &connect); + OSDynLoad_FindExport(handle, false, "bind", &bind); + OSDynLoad_FindExport(handle, false, "listen", &listen); + OSDynLoad_FindExport(handle, false, "accept", &accept); + OSDynLoad_FindExport(handle, false, "send", &send); + OSDynLoad_FindExport(handle, false, "recv", &recv); + OSDynLoad_FindExport(handle, false, "socketclose", &socketclose); + OSDynLoad_FindExport(handle, false, "socket_lib_finish", &socket_lib_finish); } diff --git a/src/cafe/nsysnet.h b/src/cafe/nsysnet.h index b9f95f3..31e56cf 100644 --- a/src/cafe/nsysnet.h +++ b/src/cafe/nsysnet.h @@ -3,22 +3,22 @@ #include -#define AF_INET 2 +#define AF_INET 2 -#define SOCK_STREAM 1 -#define SOCK_DGRAM 2 +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 -#define IPPROTO_TCP 6 -#define IPPROTO_UDP 17 +#define IPPROTO_TCP 6 +#define IPPROTO_UDP 17 -#define SOL_SOCKET -1 +#define SOL_SOCKET -1 #define SO_REUSEADDR 4 struct sockaddr { - uint16_t family; - uint16_t port; - uint32_t addr; - char zero[8]; + uint16_t family; + uint16_t port; + uint32_t addr; + char zero[8]; }; extern int (*socket_lib_init)(); diff --git a/src/cafe/sysapp.cpp b/src/cafe/sysapp.cpp index fbe453e..4e0ae00 100644 --- a/src/cafe/sysapp.cpp +++ b/src/cafe/sysapp.cpp @@ -9,12 +9,12 @@ void (*SYSLaunchMenu)(); void (*SYSLaunchTitleByPathFromLauncher)(const char *path, int len); void sysappInitialize() { - uint32_t handle; - OSDynLoad_Acquire("sysapp.rpl", &handle); - - OSDynLoad_FindExport(handle, false, "SYSCheckTitleExists", &SYSCheckTitleExists); - OSDynLoad_FindExport(handle, false, "SYSLaunchTitle", &SYSLaunchTitle); - OSDynLoad_FindExport(handle, false, "SYSLaunchMenu", &SYSLaunchMenu); - - OSDynLoad_FindExport(handle, false, "_SYSLaunchTitleByPathFromLauncher", &SYSLaunchTitleByPathFromLauncher); + uint32_t handle; + OSDynLoad_Acquire("sysapp.rpl", &handle); + + OSDynLoad_FindExport(handle, false, "SYSCheckTitleExists", &SYSCheckTitleExists); + OSDynLoad_FindExport(handle, false, "SYSLaunchTitle", &SYSLaunchTitle); + OSDynLoad_FindExport(handle, false, "SYSLaunchMenu", &SYSLaunchMenu); + + OSDynLoad_FindExport(handle, false, "_SYSLaunchTitleByPathFromLauncher", &SYSLaunchTitleByPathFromLauncher); } diff --git a/src/cafe/vpad.cpp b/src/cafe/vpad.cpp index 0fc2b50..a530774 100644 --- a/src/cafe/vpad.cpp +++ b/src/cafe/vpad.cpp @@ -5,8 +5,8 @@ void (*VPADRead)(int chan, VPADStatus *buffers, uint32_t count, int *error); void vpadInitialize() { - uint32_t handle; - OSDynLoad_Acquire("vpad.rpl", &handle); - - OSDynLoad_FindExport(handle, false, "VPADRead", &VPADRead); + uint32_t handle; + OSDynLoad_Acquire("vpad.rpl", &handle); + + OSDynLoad_FindExport(handle, false, "VPADRead", &VPADRead); } diff --git a/src/cafe/vpad.h b/src/cafe/vpad.h index e8c8329..eef1665 100644 --- a/src/cafe/vpad.h +++ b/src/cafe/vpad.h @@ -4,84 +4,84 @@ #include enum VPADButtons { - VPAD_BUTTON_A = 0x8000, - VPAD_BUTTON_B = 0x4000, - VPAD_BUTTON_X = 0x2000, - VPAD_BUTTON_Y = 0x1000, - VPAD_BUTTON_LEFT = 0x0800, - VPAD_BUTTON_RIGHT = 0x0400, - VPAD_BUTTON_UP = 0x0200, - VPAD_BUTTON_DOWN = 0x0100, - VPAD_BUTTON_ZL = 0x0080, - VPAD_BUTTON_ZR = 0x0040, - VPAD_BUTTON_L = 0x0020, - VPAD_BUTTON_R = 0x0010, - VPAD_BUTTON_PLUS = 0x0008, - VPAD_BUTTON_MINUS = 0x0004, - VPAD_BUTTON_HOME = 0x0002, - VPAD_BUTTON_SYNC = 0x0001, - - VPAD_BUTTON_STICK_R = 0x00020000, - VPAD_BUTTON_STICK_L = 0x00040000, - VPAD_BUTTON_TV = 0x00010000 + VPAD_BUTTON_A = 0x8000, + VPAD_BUTTON_B = 0x4000, + VPAD_BUTTON_X = 0x2000, + VPAD_BUTTON_Y = 0x1000, + VPAD_BUTTON_LEFT = 0x0800, + VPAD_BUTTON_RIGHT = 0x0400, + VPAD_BUTTON_UP = 0x0200, + VPAD_BUTTON_DOWN = 0x0100, + VPAD_BUTTON_ZL = 0x0080, + VPAD_BUTTON_ZR = 0x0040, + VPAD_BUTTON_L = 0x0020, + VPAD_BUTTON_R = 0x0010, + VPAD_BUTTON_PLUS = 0x0008, + VPAD_BUTTON_MINUS = 0x0004, + VPAD_BUTTON_HOME = 0x0002, + VPAD_BUTTON_SYNC = 0x0001, + + VPAD_BUTTON_STICK_R = 0x00020000, + VPAD_BUTTON_STICK_L = 0x00040000, + VPAD_BUTTON_TV = 0x00010000 }; struct VPADVec2D { - float x; - float y; + float x; + float y; }; struct VPADVec3D { - float x; - float y; - float z; + float x; + float y; + float z; }; struct VPADDirection { - VPADVec3D x; - VPADVec3D y; - VPADVec3D z; + VPADVec3D x; + VPADVec3D y; + VPADVec3D z; }; struct VPADTouchData { - uint16_t x; - uint16_t y; - uint16_t touched; - uint16_t validity; + uint16_t x; + uint16_t y; + uint16_t touched; + uint16_t validity; }; struct VPADAccStatus { - VPADVec3D acc; - float magnitude; - float variation; - VPADVec2D vertical; + VPADVec3D acc; + float magnitude; + float variation; + VPADVec2D vertical; }; struct VPADStatus { - uint32_t hold; - uint32_t pressed; - uint32_t released; - VPADVec2D leftStick; - VPADVec2D rightStick; - VPADAccStatus accelerometer; - VPADVec3D gyro; - VPADVec3D angle; - uint8_t error; - uint8_t _51; + uint32_t hold; + uint32_t pressed; + uint32_t released; + VPADVec2D leftStick; + VPADVec2D rightStick; + VPADAccStatus accelerometer; + VPADVec3D gyro; + VPADVec3D angle; + uint8_t error; + uint8_t _51; - VPADTouchData tpNormal; - VPADTouchData tpFiltered1; - VPADTouchData tpFiltered2; - uint16_t _6A; - - VPADDirection direction; - bool headphones; - VPADVec3D mag; - uint8_t volume; - uint8_t battery; - uint8_t micStatus; - uint8_t volumeEx; - char _A4[8]; + VPADTouchData tpNormal; + VPADTouchData tpFiltered1; + VPADTouchData tpFiltered2; + uint16_t _6A; + + VPADDirection direction; + bool headphones; + VPADVec3D mag; + uint8_t volume; + uint8_t battery; + uint8_t micStatus; + uint8_t volumeEx; + char _A4[8]; }; extern void (*VPADRead)(int chan, VPADStatus *buffers, uint32_t count, int *error); diff --git a/src/color.h b/src/color.h index f0414c4..3f7041b 100644 --- a/src/color.h +++ b/src/color.h @@ -2,14 +2,14 @@ #pragma once enum Colors { - COLOR_WHITE = 0xFFFFFFFF, - COLOR_BLACK = 0x000000FF, - - COLOR_RED = 0xFF0000FF, - COLOR_GREEN = 0x00FF00FF, - COLOR_BLUE = 0x0000FFFF, - - COLOR_LIGHT_RED = 0xFF8080FF, - COLOR_LIGHT_GREEN = 0x80FF80FF, - COLOR_LIGHT_BLUE = 0x8080FFFF, + COLOR_WHITE = 0xFFFFFFFF, + COLOR_BLACK = 0x000000FF, + + COLOR_RED = 0xFF0000FF, + COLOR_GREEN = 0x00FF00FF, + COLOR_BLUE = 0x0000FFFF, + + COLOR_LIGHT_RED = 0xFF8080FF, + COLOR_LIGHT_GREEN = 0x80FF80FF, + COLOR_LIGHT_BLUE = 0x8080FFFF, }; diff --git a/src/debugger.cpp b/src/debugger.cpp index d124810..647a673 100644 --- a/src/debugger.cpp +++ b/src/debugger.cpp @@ -6,940 +6,919 @@ #include "debugger.h" #include "exceptions.h" -#include "screen.h" #include "input.h" +#include "screen.h" #include bool BreakPoint::isRange(uint32_t addr, uint32_t length) { - return address >= addr && address <= addr + length - 1; + return address >= addr && address <= addr + length - 1; } BreakPoint *BreakPointMgr::find(uint32_t addr, bool includeSpecial) { - BreakPoint *bp = breakpoints.find(addr); - if (!bp && includeSpecial) { - bp = special.find(addr); - } - return bp; + BreakPoint *bp = breakpoints.find(addr); + if (!bp && includeSpecial) { + bp = special.find(addr); + } + return bp; } BreakPoint *BreakPointMgr::findRange(uint32_t addr, uint32_t length, int *index, bool includeSpecial) { - BreakPoint *bp = breakpoints.findRange(addr, length, index); - if (bp) { - return bp; - } - - if (includeSpecial) { - int temp = *index - breakpoints.size(); - bp = special.findRange(addr, length, &temp); - *index = temp + breakpoints.size(); - return bp; - } - - return nullptr; + BreakPoint *bp = breakpoints.findRange(addr, length, index); + if (bp) { + return bp; + } + + if (includeSpecial) { + int temp = *index - breakpoints.size(); + bp = special.findRange(addr, length, &temp); + *index = temp + breakpoints.size(); + return bp; + } + + return nullptr; } SpecialBreakPoint *BreakPointMgr::findSpecial(uint32_t addr, OSThread *thread) { - int index = 0; - SpecialBreakPoint *bp = special.findRange(addr, 4, &index); - while (bp) { - if (bp->thread == thread) { - return bp; - } - bp = special.findRange(addr, 4, &index); - } - return nullptr; + int index = 0; + SpecialBreakPoint *bp = special.findRange(addr, 4, &index); + while (bp) { + if (bp->thread == thread) { + return bp; + } + bp = special.findRange(addr, 4, &index); + } + return nullptr; } bool BreakPointMgr::isCustom(uint32_t addr) { - return find(addr, false); + return find(addr, false); } bool BreakPointMgr::isSpecial(uint32_t addr) { - return find(addr, true) && !find(addr, false); + return find(addr, true) && !find(addr, false); } bool BreakPointMgr::isSoftware(uint32_t addr) { - uint32_t instr = getInstr(addr); - uint32_t opcode = instr >> 26; - if (opcode == 3) { //twi - return true; - } - else if (opcode == 31) { - return (instr & 0x7FF) == 8; //tw - } - return false; + uint32_t instr = getInstr(addr); + uint32_t opcode = instr >> 26; + if (opcode == 3) { //twi + return true; + } else if (opcode == 31) { + return (instr & 0x7FF) == 8; //tw + } + return false; } void BreakPointMgr::disable(BreakPoint *bp) { - uint32_t address = bp->address; - if (bp->address) { - bp->address = 0; - if (!find(address, true)) { - KernelWriteU32(address, bp->instruction); - } - bp->instruction = 0; - } + uint32_t address = bp->address; + if (bp->address) { + bp->address = 0; + if (!find(address, true)) { + KernelWriteU32(address, bp->instruction); + } + bp->instruction = 0; + } } void BreakPointMgr::enable(BreakPoint *bp, uint32_t addr) { - BreakPoint *other = find(addr, true); - if (other) { - bp->instruction = other->instruction; - } - else { - bp->instruction = *(uint32_t *)addr; - KernelWriteU32(addr, TRAP); - } - bp->address = addr; + BreakPoint *other = find(addr, true); + if (other) { + bp->instruction = other->instruction; + } else { + bp->instruction = *(uint32_t *) addr; + KernelWriteU32(addr, TRAP); + } + bp->address = addr; } void BreakPointMgr::init() { - OSInitMutex(&mutex); + OSInitMutex(&mutex); } void BreakPointMgr::lock() { - OSLockMutex(&mutex); + OSLockMutex(&mutex); } void BreakPointMgr::unlock() { - OSUnlockMutex(&mutex); + OSUnlockMutex(&mutex); } void BreakPointMgr::cleanup() { - lock(); - breakpoints.cleanup(); - special.cleanup(); - unlock(); + lock(); + breakpoints.cleanup(); + special.cleanup(); + unlock(); } void BreakPointMgr::read(void *buffer, uint32_t addr, uint32_t length) { - lock(); - - memcpy(buffer, (void *)addr, length); - - int index = 0; - BreakPoint *bp = findRange(addr, length, &index, true); - while (bp) { - uint32_t offset = bp->address - addr; - char *bufptr = (char *)buffer + offset; - if (bp->address > addr + length - 4) { - uint32_t value = bp->instruction; - for (int i = 0; i < length - offset; i++) { - bufptr[i] = value >> 24; - value <<= 8; - } - } - else { - *(uint32_t *)bufptr = bp->instruction; - } - bp = findRange(addr, length, &index, true); - } - unlock(); + lock(); + + memcpy(buffer, (void *) addr, length); + + int index = 0; + BreakPoint *bp = findRange(addr, length, &index, true); + while (bp) { + uint32_t offset = bp->address - addr; + char *bufptr = (char *) buffer + offset; + if (bp->address > addr + length - 4) { + uint32_t value = bp->instruction; + for (int i = 0; i < length - offset; i++) { + bufptr[i] = value >> 24; + value <<= 8; + } + } else { + *(uint32_t *) bufptr = bp->instruction; + } + bp = findRange(addr, length, &index, true); + } + unlock(); } void BreakPointMgr::write(const void *buffer, uint32_t addr, uint32_t length) { - lock(); - - length = length & ~3; - - int index = 0; - if (!findRange(addr, length, &index, true)) { - KernelWrite(addr, buffer, length); - } - else { - for (uint32_t i = 0; i < length; i += 4) { - uint32_t value = *(uint32_t *)((char *)buffer + i); + lock(); - int index = 0; - BreakPoint *bp = findRange(addr + i, 4, &index, true); - if (!bp) { - KernelWriteU32(addr + i, value); - } - else { - while (bp) { - bp->instruction = value; - bp = findRange(addr + i, 4, &index, true); - } - } - } - } - - unlock(); + length = length & ~3; + + int index = 0; + if (!findRange(addr, length, &index, true)) { + KernelWrite(addr, buffer, length); + } else { + for (uint32_t i = 0; i < length; i += 4) { + uint32_t value = *(uint32_t *) ((char *) buffer + i); + + int index = 0; + BreakPoint *bp = findRange(addr + i, 4, &index, true); + if (!bp) { + KernelWriteU32(addr + i, value); + } else { + while (bp) { + bp->instruction = value; + bp = findRange(addr + i, 4, &index, true); + } + } + } + } + + unlock(); } void BreakPointMgr::toggle(uint32_t addr) { - lock(); - - BreakPoint *bp = find(addr, false); - if (bp) { - disable(bp); - } - else { - BreakPoint *bp = breakpoints.alloc(); - bp->isSpecial = false; - enable(bp, addr); - } - - unlock(); + lock(); + + BreakPoint *bp = find(addr, false); + if (bp) { + disable(bp); + } else { + BreakPoint *bp = breakpoints.alloc(); + bp->isSpecial = false; + enable(bp, addr); + } + + unlock(); } uint32_t BreakPointMgr::getInstr(uint32_t addr) { - lock(); - uint32_t instruction; - BreakPoint *bp = find(addr, true); - if (bp) { - instruction = bp->instruction; - } - else { - instruction = *(uint32_t *)addr; - } - unlock(); - return instruction; + lock(); + uint32_t instruction; + BreakPoint *bp = find(addr, true); + if (bp) { + instruction = bp->instruction; + } else { + instruction = *(uint32_t *) addr; + } + unlock(); + return instruction; } void BreakPointMgr::clearSpecial(OSThread *thread) { - lock(); - for (int i = 0; i < special.size(); i++) { - SpecialBreakPoint *bp = special[i]; - if (bp->thread == thread) { - disable(bp); - } - } - unlock(); + lock(); + for (int i = 0; i < special.size(); i++) { + SpecialBreakPoint *bp = special[i]; + if (bp->thread == thread) { + disable(bp); + } + } + unlock(); } void BreakPointMgr::predictStep(ExceptionState *state, bool stepOver) { - lock(); - - uint32_t address = state->context.srr0; - uint32_t instruction = getInstr(address); - - uint32_t target1 = address + 4; - uint32_t target2 = 0; - - uint8_t opcode = instruction >> 26; - if (opcode == 18) { //Branch - bool AA = instruction & 2; - bool LK = instruction & 1; - - uint32_t LI = instruction & 0x3FFFFFC; - if (LI & 0x2000000) LI -= 0x4000000; - - if (!LK || !stepOver) { - if (AA) target1 = LI; - else { - target1 = address + LI; - } - } - } - - else if (opcode == 16) { //Conditional branch - bool AA = instruction & 2; - bool LK = instruction & 1; - - uint32_t BD = instruction & 0xFFFC; - if (BD & 0x8000) BD -= 0x10000; - - if (!LK || !stepOver) { - if (AA) target2 = BD; - else { - target2 = address + BD; - } - } - } - - else if (opcode == 19) { //Conditional branch to lr/ctr - uint16_t XO = (instruction >> 1) & 0x3FF; - bool LK = instruction & 1; - - if (!LK || !stepOver) { - if (XO == 16) target2 = state->context.lr; - else if (XO == 528) target2 = state->context.ctr; - } - } - - SpecialBreakPoint *bp = special.alloc(); - bp->isSpecial = true; - bp->thread = state->thread; - enable(bp, target1); - - if (target2) { - SpecialBreakPoint *bp = special.alloc(); - bp->isSpecial = true; - bp->thread = state->thread; - enable(bp, target2); - } - - unlock(); + lock(); + + uint32_t address = state->context.srr0; + uint32_t instruction = getInstr(address); + + uint32_t target1 = address + 4; + uint32_t target2 = 0; + + uint8_t opcode = instruction >> 26; + if (opcode == 18) { //Branch + bool AA = instruction & 2; + bool LK = instruction & 1; + + uint32_t LI = instruction & 0x3FFFFFC; + if (LI & 0x2000000) LI -= 0x4000000; + + if (!LK || !stepOver) { + if (AA) target1 = LI; + else { + target1 = address + LI; + } + } + } + + else if (opcode == 16) { //Conditional branch + bool AA = instruction & 2; + bool LK = instruction & 1; + + uint32_t BD = instruction & 0xFFFC; + if (BD & 0x8000) BD -= 0x10000; + + if (!LK || !stepOver) { + if (AA) target2 = BD; + else { + target2 = address + BD; + } + } + } + + else if (opcode == 19) { //Conditional branch to lr/ctr + uint16_t XO = (instruction >> 1) & 0x3FF; + bool LK = instruction & 1; + + if (!LK || !stepOver) { + if (XO == 16) target2 = state->context.lr; + else if (XO == 528) + target2 = state->context.ctr; + } + } + + SpecialBreakPoint *bp = special.alloc(); + bp->isSpecial = true; + bp->thread = state->thread; + enable(bp, target1); + + if (target2) { + SpecialBreakPoint *bp = special.alloc(); + bp->isSpecial = true; + bp->thread = state->thread; + enable(bp, target2); + } + + unlock(); } bool ExceptionState::isBreakpoint() { - return type == PROGRAM && context.srr1 & 0x20000; + return type == PROGRAM && context.srr1 & 0x20000; } void ExceptionState::resume() { - OSLoadContext(&context); + OSLoadContext(&context); } void ExceptionMgr::init() { - OSInitMutex(&mutex); + OSInitMutex(&mutex); } void ExceptionMgr::lock() { - OSLockMutex(&mutex); + OSLockMutex(&mutex); } void ExceptionMgr::unlock() { - OSUnlockMutex(&mutex); + OSUnlockMutex(&mutex); } void ExceptionMgr::cleanup() { - OSMessage message; - message.message = Debugger::STEP_CONTINUE; - - lock(); - - for (int i = 0; i < list.size(); i++) { - ExceptionState *state = list[i]; - if (state->isPaused) { - OSSendMessage(&state->queue, &message, OS_MESSAGE_FLAGS_NONE); - } - } - - unlock(); + OSMessage message; + message.message = Debugger::STEP_CONTINUE; + + lock(); + + for (int i = 0; i < list.size(); i++) { + ExceptionState *state = list[i]; + if (state->isPaused) { + OSSendMessage(&state->queue, &message, OS_MESSAGE_FLAGS_NONE); + } + } + + unlock(); } ExceptionState *ExceptionMgr::find(OSThread *thread) { - lock(); - for (int i = 0; i < list.size(); i++) { - ExceptionState *state = list[i]; - if (state->thread == thread) { - unlock(); - return state; - } - } - unlock(); - return nullptr; + lock(); + for (int i = 0; i < list.size(); i++) { + ExceptionState *state = list[i]; + if (state->thread == thread) { + unlock(); + return state; + } + } + unlock(); + return nullptr; } ExceptionState *ExceptionMgr::findOrCreate(OSThread *thread) { - lock(); - ExceptionState *state = find(thread); - if (!state) { - state = new ExceptionState(); - state->thread = thread; - state->isPaused = false; - OSInitMessageQueue(&state->queue, &state->message, 1); - list.push_back(state); - } - unlock(); - return state; + lock(); + ExceptionState *state = find(thread); + if (!state) { + state = new ExceptionState(); + state->thread = thread; + state->isPaused = false; + OSInitMessageQueue(&state->queue, &state->message, 1); + list.push_back(state); + } + unlock(); + return state; } uint32_t StepMgr::buffer[96]; void StepMgr::init() { - OSInitMutex(&mutex); - usedMask = 0; + OSInitMutex(&mutex); + usedMask = 0; } void StepMgr::lock() { - OSLockMutex(&mutex); + OSLockMutex(&mutex); } void StepMgr::unlock() { - OSUnlockMutex(&mutex); + OSUnlockMutex(&mutex); } uint32_t *StepMgr::alloc() { - lock(); - int index = 0; - uint32_t mask = usedMask; - while (mask & 1) { - mask >>= 1; - index++; - } - usedMask |= 1 << index; - unlock(); - - if (index == 32) { - OSFatal("Displaced step allocation failed"); - } - return &buffer[index * 3]; + lock(); + int index = 0; + uint32_t mask = usedMask; + while (mask & 1) { + mask >>= 1; + index++; + } + usedMask |= 1 << index; + unlock(); + + if (index == 32) { + OSFatal("Displaced step allocation failed"); + } + return &buffer[index * 3]; } void StepMgr::free(int index) { - lock(); - usedMask &= ~(1 << index); - unlock(); + lock(); + usedMask &= ~(1 << index); + unlock(); } void StepMgr::branchConditional(ExceptionState *state, uint32_t instruction, uint32_t target, bool checkCtr) { - Bits<5> BO = (instruction >> 21) & 0x1F; - Bits<32> CR = state->context.cr; - - uint32_t BI = (instruction >> 16) & 0x1F; - bool LK = instruction & 1; - - if (LK) { - state->context.lr = state->context.srr0 + 4; - } + Bits<5> BO = (instruction >> 21) & 0x1F; + Bits<32> CR = state->context.cr; - bool ctr_ok = true; - if (checkCtr) { - if (!BO[2]) { - state->context.ctr--; - } - ctr_ok = BO[2] || ((state->context.ctr != 0) ^ BO[3]); - } - - bool cond_ok = BO[0] || (CR[BI] == BO[1]); - if (ctr_ok && cond_ok) { - state->context.srr0 = target; - } + uint32_t BI = (instruction >> 16) & 0x1F; + bool LK = instruction & 1; + + if (LK) { + state->context.lr = state->context.srr0 + 4; + } + + bool ctr_ok = true; + if (checkCtr) { + if (!BO[2]) { + state->context.ctr--; + } + ctr_ok = BO[2] || ((state->context.ctr != 0) ^ BO[3]); + } + + bool cond_ok = BO[0] || (CR[BI] == BO[1]); + if (ctr_ok && cond_ok) { + state->context.srr0 = target; + } } void StepMgr::singleStep(ExceptionState *state, uint32_t instruction) { - uint8_t opcode = instruction >> 26; - - if (opcode == 18) { //Branch - bool AA = instruction & 2; - bool LK = instruction & 1; - - uint32_t LI = instruction & 0x3FFFFFC; - if (LI & 0x2000000) LI -= 0x4000000; - - if (LK) { - state->context.lr = state->context.srr0 + 4; - } - - if (AA) state->context.srr0 = LI; - else { - state->context.srr0 += LI; - } - return; - } - - if (opcode == 16) { //Conditional branch - bool AA = instruction & 2; - - uint32_t BD = instruction & 0xFFFC; - if (BD & 0x8000) BD -= 0x10000; - - uint32_t target; - if (AA) target = BD; - else { - target = state->context.srr0 + BD; - } - - branchConditional(state, instruction, target, true); - return; - } - - if (opcode == 19) { //Conditional branch to lr/ctr - uint16_t XO = (instruction >> 1) & 0x3FF; - - uint32_t target; - if (XO == 16) { - branchConditional(state, instruction, state->context.lr, true); - return; - } - else if (XO == 528) { - branchConditional(state, instruction, state->context.ctr, false); - return; - } - } - - uint32_t *ptr = alloc(); - ptr[0] = instruction; - ptr[1] = TRAP; - ptr[2] = state->context.srr0; - DCFlushRange(ptr, 12); - ICInvalidateRange(ptr, 12); - state->context.srr0 = (uint32_t)ptr; + uint8_t opcode = instruction >> 26; + + if (opcode == 18) { //Branch + bool AA = instruction & 2; + bool LK = instruction & 1; + + uint32_t LI = instruction & 0x3FFFFFC; + if (LI & 0x2000000) LI -= 0x4000000; + + if (LK) { + state->context.lr = state->context.srr0 + 4; + } + + if (AA) state->context.srr0 = LI; + else { + state->context.srr0 += LI; + } + return; + } + + if (opcode == 16) { //Conditional branch + bool AA = instruction & 2; + + uint32_t BD = instruction & 0xFFFC; + if (BD & 0x8000) BD -= 0x10000; + + uint32_t target; + if (AA) target = BD; + else { + target = state->context.srr0 + BD; + } + + branchConditional(state, instruction, target, true); + return; + } + + if (opcode == 19) { //Conditional branch to lr/ctr + uint16_t XO = (instruction >> 1) & 0x3FF; + + uint32_t target; + if (XO == 16) { + branchConditional(state, instruction, state->context.lr, true); + return; + } else if (XO == 528) { + branchConditional(state, instruction, state->context.ctr, false); + return; + } + } + + uint32_t *ptr = alloc(); + ptr[0] = instruction; + ptr[1] = TRAP; + ptr[2] = state->context.srr0; + DCFlushRange(ptr, 12); + ICInvalidateRange(ptr, 12); + state->context.srr0 = (uint32_t) ptr; } void StepMgr::handleBreakPoint(ExceptionState *state) { - uint32_t start = (uint32_t)buffer; - uint32_t end = (uint32_t)buffer + sizeof(buffer); - - uint32_t addr = state->context.srr0; - if (addr >= start && addr < end) { - int offset = addr - start; - if (offset % 12 != 4) { - char buffer[100]; - snprintf(buffer, 100, "Unexpected srr0 after displaced step: %08X", addr); - OSFatal(buffer); - } - - int index = offset / 12; - state->context.srr0 = buffer[index * 3 + 2] + 4; - free(index); + uint32_t start = (uint32_t) buffer; + uint32_t end = (uint32_t) buffer + sizeof(buffer); - state->resume(); - } + uint32_t addr = state->context.srr0; + if (addr >= start && addr < end) { + int offset = addr - start; + if (offset % 12 != 4) { + char buffer[100]; + snprintf(buffer, 100, "Unexpected srr0 after displaced step: %08X", addr); + OSFatal(buffer); + } + + int index = offset / 12; + state->context.srr0 = buffer[index * 3 + 2] + 4; + free(index); + + state->resume(); + } } void StepMgr::adjustAddress(ExceptionState *state) { - uint32_t start = (uint32_t)buffer; - uint32_t end = (uint32_t)buffer + sizeof(buffer); - - uint32_t addr = state->context.srr0; - if (addr >= start && addr < end) { - int index = (addr - start) / 12; - state->context.srr0 = buffer[index * 3 + 2]; - } + uint32_t start = (uint32_t) buffer; + uint32_t end = (uint32_t) buffer + sizeof(buffer); + + uint32_t addr = state->context.srr0; + if (addr >= start && addr < end) { + int index = (addr - start) / 12; + state->context.srr0 = buffer[index * 3 + 2]; + } } bool Debugger::checkDataRead(uint32_t addr, uint32_t length) { - uint32_t memStart, memSize; - OSGetMemBound(MEM2, &memStart, &memSize); - - uint32_t memEnd = memStart + memSize; - - return addr >= 0x10000000 && addr + length <= memEnd; + uint32_t memStart, memSize; + OSGetMemBound(MEM2, &memStart, &memSize); + + uint32_t memEnd = memStart + memSize; + + return addr >= 0x10000000 && addr + length <= memEnd; } Debugger::StepCommand Debugger::notifyBreak(ExceptionState *state) { - OSMessage message; - message.message = ExceptionState::PROGRAM; - message.args[0] = (uint32_t)&state->context; - message.args[1] = sizeof(OSContext); - message.args[2] = (uint32_t)state->thread; - OSSendMessage(&eventQueue, &message, OS_MESSAGE_FLAGS_BLOCKING); - - state->isPaused = true; - OSReceiveMessage(&state->queue, &message, OS_MESSAGE_FLAGS_BLOCKING); - state->isPaused = false; - return (StepCommand)message.message; + OSMessage message; + message.message = ExceptionState::PROGRAM; + message.args[0] = (uint32_t) &state->context; + message.args[1] = sizeof(OSContext); + message.args[2] = (uint32_t) state->thread; + OSSendMessage(&eventQueue, &message, OS_MESSAGE_FLAGS_BLOCKING); + + state->isPaused = true; + OSReceiveMessage(&state->queue, &message, OS_MESSAGE_FLAGS_BLOCKING); + state->isPaused = false; + return (StepCommand) message.message; } void Debugger::resumeBreakPoint(ExceptionState *state) { - uint32_t address = state->context.srr0; - if (breakpoints.isSoftware(address)) { - state->context.srr0 += 4; - state->resume(); - } - - uint32_t instruction = breakpoints.getInstr(state->context.srr0); - stepper.singleStep(state, instruction); - state->resume(); + uint32_t address = state->context.srr0; + if (breakpoints.isSoftware(address)) { + state->context.srr0 += 4; + state->resume(); + } + + uint32_t instruction = breakpoints.getInstr(state->context.srr0); + stepper.singleStep(state, instruction); + state->resume(); } void Debugger::processBreakPoint(ExceptionState *state) { - StepCommand command = notifyBreak(state); - if (command == STEP_INTO || command == STEP_OVER) { - breakpoints.predictStep(state, command == STEP_OVER); - } + StepCommand command = notifyBreak(state); + if (command == STEP_INTO || command == STEP_OVER) { + breakpoints.predictStep(state, command == STEP_OVER); + } } void Debugger::handleBreakPoint(ExceptionState *state) { - if (firstTrap) { - firstTrap = false; + if (firstTrap) { + firstTrap = false; - Screen screen; - screen.init(); - screen.drawText( - 0, 0, "Waiting for debugger connection.\n" - "Press the home button to continue without debugger.\n" - "You can still connect while the game is running." - ); - screen.flip(); - - while (!connected) { - uint32_t buttons = GetInput(VPAD_BUTTON_HOME); - if (buttons) { - state->context.srr0 += 4; - state->resume(); - } - } - } - - stepper.handleBreakPoint(state); - - if (!connected) { - handleFatalCrash(&state->context, state->type); - } - - uint32_t addr = state->context.srr0; - bool trap; - - breakpoints.lock(); - trap = breakpoints.isCustom(addr) || breakpoints.isSoftware(addr) || - breakpoints.findSpecial(addr, state->thread); - breakpoints.unlock(); - - breakpoints.clearSpecial(state->thread); - if (trap) { - processBreakPoint(state); - } - resumeBreakPoint(state); + Screen screen; + screen.init(); + screen.drawText( + 0, 0, "Waiting for debugger connection.\n" + "Press the home button to continue without debugger.\n" + "You can still connect while the game is running."); + screen.flip(); + + while (!connected) { + uint32_t buttons = GetInput(VPAD_BUTTON_HOME); + if (buttons) { + state->context.srr0 += 4; + state->resume(); + } + } + } + + stepper.handleBreakPoint(state); + + if (!connected) { + handleFatalCrash(&state->context, state->type); + } + + uint32_t addr = state->context.srr0; + bool trap; + + breakpoints.lock(); + trap = breakpoints.isCustom(addr) || breakpoints.isSoftware(addr) || + breakpoints.findSpecial(addr, state->thread); + breakpoints.unlock(); + + breakpoints.clearSpecial(state->thread); + if (trap) { + processBreakPoint(state); + } + resumeBreakPoint(state); } void Debugger::handleCrash(ExceptionState *state) { - stepper.adjustAddress(state); - if (connected) { - OSMessage message; - message.message = state->type; - message.args[0] = (uint32_t)&state->context; - message.args[1] = sizeof(OSContext); - message.args[2] = (uint32_t)state->thread; - OSSendMessage(&eventQueue, &message, OS_MESSAGE_FLAGS_BLOCKING); - - while (true) { - OSReceiveMessage(&state->queue, &message, OS_MESSAGE_FLAGS_BLOCKING); - } - } - else { - handleFatalCrash(&state->context, state->type); - } + stepper.adjustAddress(state); + if (connected) { + OSMessage message; + message.message = state->type; + message.args[0] = (uint32_t) &state->context; + message.args[1] = sizeof(OSContext); + message.args[2] = (uint32_t) state->thread; + OSSendMessage(&eventQueue, &message, OS_MESSAGE_FLAGS_BLOCKING); + + while (true) { + OSReceiveMessage(&state->queue, &message, OS_MESSAGE_FLAGS_BLOCKING); + } + } else { + handleFatalCrash(&state->context, state->type); + } } void Debugger::handleFatalCrash(OSContext *context, ExceptionState::Type type) { - const char *name; - if (type == ExceptionState::DSI) name = "A DSI"; - else if (type == ExceptionState::ISI) name = "An ISI"; - else { - name = "A program"; - } - DumpContext(context, name); + const char *name; + if (type == ExceptionState::DSI) name = "A DSI"; + else if (type == ExceptionState::ISI) + name = "An ISI"; + else { + name = "A program"; + } + DumpContext(context, name); } void Debugger::handleException(OSContext *context, ExceptionState::Type type) { - OSThread *thread = OSGetCurrentThread(); - - if (thread == serverThread) { - handleFatalCrash(context, type); - } - - ExceptionState *state = exceptions.findOrCreate(thread); - memcpy(&state->context, context, sizeof(OSContext)); - state->type = type; - - delete context; - - if (state->isBreakpoint()) { - handleBreakPoint(state); - } - else { - handleCrash(state); - } + OSThread *thread = OSGetCurrentThread(); + + if (thread == serverThread) { + handleFatalCrash(context, type); + } + + ExceptionState *state = exceptions.findOrCreate(thread); + memcpy(&state->context, context, sizeof(OSContext)); + state->type = type; + + delete context; + + if (state->isBreakpoint()) { + handleBreakPoint(state); + } else { + handleCrash(state); + } } void Debugger::exceptionHandler(OSContext *context, ExceptionState::Type type) { - debugger->handleException(context, type); + debugger->handleException(context, type); } bool Debugger::dsiHandler(OSContext *context) { - OSContext *info = new OSContext(); - memcpy(info, context, sizeof(OSContext)); - context->srr0 = (uint32_t)exceptionHandler; - context->gpr[3] = (uint32_t)info; - context->gpr[4] = ExceptionState::DSI; - return true; + OSContext *info = new OSContext(); + memcpy(info, context, sizeof(OSContext)); + context->srr0 = (uint32_t) exceptionHandler; + context->gpr[3] = (uint32_t) info; + context->gpr[4] = ExceptionState::DSI; + return true; } bool Debugger::isiHandler(OSContext *context) { - OSContext *info = new OSContext(); - memcpy(info, context, sizeof(OSContext)); - context->srr0 = (uint32_t)exceptionHandler; - context->gpr[3] = (uint32_t)info; - context->gpr[4] = ExceptionState::ISI; - return true; + OSContext *info = new OSContext(); + memcpy(info, context, sizeof(OSContext)); + context->srr0 = (uint32_t) exceptionHandler; + context->gpr[3] = (uint32_t) info; + context->gpr[4] = ExceptionState::ISI; + return true; } bool Debugger::programHandler(OSContext *context) { - OSContext *info = new OSContext(); - memcpy(info, context, sizeof(OSContext)); - context->srr0 = (uint32_t)exceptionHandler; - context->gpr[3] = (uint32_t)info; - context->gpr[4] = ExceptionState::PROGRAM; - return true; + OSContext *info = new OSContext(); + memcpy(info, context, sizeof(OSContext)); + context->srr0 = (uint32_t) exceptionHandler; + context->gpr[3] = (uint32_t) info; + context->gpr[4] = ExceptionState::PROGRAM; + return true; } void Debugger::cleanup() { - breakpoints.cleanup(); - exceptions.cleanup(); - - OSMessage message; - while (OSReceiveMessage(&eventQueue, &message, OS_MESSAGE_FLAGS_NONE)); + breakpoints.cleanup(); + exceptions.cleanup(); + + OSMessage message; + while (OSReceiveMessage(&eventQueue, &message, OS_MESSAGE_FLAGS_NONE)) + ; } void Debugger::mainLoop(Client *client) { - while (true) { - uint8_t cmd; - if (!client->recvall(&cmd, 1)) return; - - if (cmd == COMMAND_CLOSE) return; - else if (cmd == COMMAND_READ) { - uint32_t addr, length; - if (!client->recvall(&addr, 4)) return; - if (!client->recvall(&length, 4)) return; - - char *buffer = new char[length]; - breakpoints.read(buffer, addr, length); - if (!client->sendall(buffer, length)) { - delete buffer; - return; - } - delete buffer; - } - else if (cmd == COMMAND_WRITE) { - uint32_t addr, length; - if (!client->recvall(&addr, 4)) return; - if (!client->recvall(&length, 4)) return; - if (!client->recvall((void *)addr, length)) return; - } - else if (cmd == COMMAND_WRITE_CODE) { - uint32_t addr, length; - if (!client->recvall(&addr, 4)) return; - if (!client->recvall(&length, 4)) return; - - char *buffer = new char[length]; - if (!client->recvall(buffer, length)) { - delete buffer; - return; - } - breakpoints.write(buffer, addr, length); - delete buffer; - } - else if (cmd == COMMAND_GET_MODULE_NAME) { - char name[0x40]; - int length = 0x40; - OSDynLoad_GetModuleName(-1, name, &length); - - length = strlen(name); - if (!client->sendall(&length, 4)) return; - if (!client->sendall(name, length)) return; - } - else if (cmd == COMMAND_GET_MODULE_LIST) { - OSLockMutex(OSDynLoad_gLoaderLock); - - char buffer[0x1000]; //This should be enough - uint32_t offset = 0; - OSDynLoad_RPLInfo *current = FirstRPL; - while (current) { - OSDynLoad_NotifyData *info = current->notifyData; - - uint32_t namelen = strlen(current->name); - if (offset + 0x18 + namelen > 0x1000) { - break; - } - - uint32_t *infobuf = (uint32_t *)(buffer + offset); - infobuf[0] = info->textAddr; - infobuf[1] = info->textSize; - infobuf[2] = info->dataAddr; - infobuf[3] = info->dataSize; - infobuf[4] = (uint32_t)current->entryPoint; - infobuf[5] = namelen; - memcpy(&infobuf[6], current->name, namelen); - offset += 0x18 + namelen; - - current = current->next; - } - - OSUnlockMutex(OSDynLoad_gLoaderLock); - - if (!client->sendall(&offset, 4)) return; - if (!client->sendall(buffer, offset)) return; - } - else if (cmd == COMMAND_GET_THREAD_LIST) { - int state = OSDisableInterrupts(); - __OSLockScheduler(this); - - char buffer[0x1000]; //This should be enough - uint32_t offset = 0; - OSThread *current = ThreadList; - while (current) { - const char *name = OSGetThreadName(current); - - uint32_t namelen = 0; - if (name) { - namelen = strlen(name); - } + while (true) { + uint8_t cmd; + if (!client->recvall(&cmd, 1)) return; - if (offset + 0x1C + namelen > 0x1000) { - break; - } - - int priority = current->basePriority; - if (current->type == 1) { - priority -= 0x20; - } - else if (current->type == 2) { - priority -= 0x40; - } - - uint32_t *infobuf = (uint32_t *)(buffer + offset); - infobuf[0] = (uint32_t)current; - infobuf[1] = current->attr & 7; - infobuf[2] = priority; - infobuf[3] = (uint32_t)current->stackBase; - infobuf[4] = (uint32_t)current->stackEnd; - infobuf[5] = (uint32_t)current->entryPoint; - infobuf[6] = namelen; - memcpy(&infobuf[7], name, namelen); - offset += 0x1C + namelen; - - current = current->activeLink.next; - } - - __OSUnlockScheduler(this); - OSRestoreInterrupts(state); - - if (!client->sendall(&offset, 4)) return; - if (!client->sendall(buffer, offset)) return; - } - else if (cmd == COMMAND_GET_STACK_TRACE) { - OSThread *thread; - if (!client->recvall(&thread, 4)) return; - - ExceptionState *state = exceptions.find(thread); - if (state) { - uint32_t sp = state->context.gpr[1]; - uint32_t trace[100]; - int index = 0; - while (checkDataRead(sp, 4) && index < 100) { - sp = *(uint32_t *)sp; - if (!checkDataRead(sp, 4)) break; - - trace[index] = *(uint32_t *)(sp + 4); - index++; - } - - if (!client->sendall(&index, 4)) return; - if (!client->sendall(trace, index * 4)) return; - } - else { - int index = 0; - if (!client->sendall(&index, 4)) return; - } - } - else if (cmd == COMMAND_TOGGLE_BREAKPOINT) { - uint32_t address; - if (!client->recvall(&address, 4)) return; - - breakpoints.toggle(address); - } - else if (cmd == COMMAND_POKE_REGISTERS) { - OSThread *thread; - if (!client->recvall(&thread, 4)) return; - - uint32_t gpr[32]; - double fpr[32]; - if (!client->recvall(gpr, 4 * 32)) return; - if (!client->recvall(fpr, 8 * 32)) return; - - exceptions.lock(); - ExceptionState *state = exceptions.find(thread); - if (state) { - memcpy(state->context.gpr, gpr, 4 * 32); - memcpy(state->context.fpr, fpr, 8 * 32); - } - exceptions.unlock(); - } - else if (cmd == COMMAND_RECEIVE_MESSAGES) { - OSMessage messages[10]; - - int count = 0; - while (count < 10) { - if (!OSReceiveMessage(&eventQueue, &messages[count], OS_MESSAGE_FLAGS_NONE)) { - break; - } - count++; - } - - if (!client->sendall(&count, 4)) return; - for (int i = 0; i < count; i++) { - if (!client->sendall(&messages[i], sizeof(OSMessage))) return; - if (messages[i].args[0]) { - void *data = (void *)messages[i].args[0]; - size_t length = messages[i].args[1]; - if (!client->sendall(data, length)) return; - } - } - } - else if (cmd == COMMAND_SEND_MESSAGE) { - OSMessage message; - if (!client->recvall(&message, sizeof(OSMessage))) return; - - OSThread *thread = (OSThread *)message.args[0]; - - exceptions.lock(); - ExceptionState *state = exceptions.find(thread); - if (state) { - OSSendMessage(&state->queue, &message, OS_MESSAGE_FLAGS_NONE); - } - exceptions.unlock(); - } - } + if (cmd == COMMAND_CLOSE) return; + else if (cmd == COMMAND_READ) { + uint32_t addr, length; + if (!client->recvall(&addr, 4)) return; + if (!client->recvall(&length, 4)) return; + + char *buffer = new char[length]; + breakpoints.read(buffer, addr, length); + if (!client->sendall(buffer, length)) { + delete buffer; + return; + } + delete buffer; + } else if (cmd == COMMAND_WRITE) { + uint32_t addr, length; + if (!client->recvall(&addr, 4)) return; + if (!client->recvall(&length, 4)) return; + if (!client->recvall((void *) addr, length)) return; + } else if (cmd == COMMAND_WRITE_CODE) { + uint32_t addr, length; + if (!client->recvall(&addr, 4)) return; + if (!client->recvall(&length, 4)) return; + + char *buffer = new char[length]; + if (!client->recvall(buffer, length)) { + delete buffer; + return; + } + breakpoints.write(buffer, addr, length); + delete buffer; + } else if (cmd == COMMAND_GET_MODULE_NAME) { + char name[0x40]; + int length = 0x40; + OSDynLoad_GetModuleName(-1, name, &length); + + length = strlen(name); + if (!client->sendall(&length, 4)) return; + if (!client->sendall(name, length)) return; + } else if (cmd == COMMAND_GET_MODULE_LIST) { + OSLockMutex(OSDynLoad_gLoaderLock); + + char buffer[0x1000]; //This should be enough + uint32_t offset = 0; + OSDynLoad_RPLInfo *current = FirstRPL; + while (current) { + OSDynLoad_NotifyData *info = current->notifyData; + + uint32_t namelen = strlen(current->name); + if (offset + 0x18 + namelen > 0x1000) { + break; + } + + uint32_t *infobuf = (uint32_t *) (buffer + offset); + infobuf[0] = info->textAddr; + infobuf[1] = info->textSize; + infobuf[2] = info->dataAddr; + infobuf[3] = info->dataSize; + infobuf[4] = (uint32_t) current->entryPoint; + infobuf[5] = namelen; + memcpy(&infobuf[6], current->name, namelen); + offset += 0x18 + namelen; + + current = current->next; + } + + OSUnlockMutex(OSDynLoad_gLoaderLock); + + if (!client->sendall(&offset, 4)) return; + if (!client->sendall(buffer, offset)) return; + } else if (cmd == COMMAND_GET_THREAD_LIST) { + int state = OSDisableInterrupts(); + __OSLockScheduler(this); + + char buffer[0x1000]; //This should be enough + uint32_t offset = 0; + OSThread *current = ThreadList; + while (current) { + const char *name = OSGetThreadName(current); + + uint32_t namelen = 0; + if (name) { + namelen = strlen(name); + } + + if (offset + 0x1C + namelen > 0x1000) { + break; + } + + int priority = current->basePriority; + if (current->type == 1) { + priority -= 0x20; + } else if (current->type == 2) { + priority -= 0x40; + } + + uint32_t *infobuf = (uint32_t *) (buffer + offset); + infobuf[0] = (uint32_t) current; + infobuf[1] = current->attr & 7; + infobuf[2] = priority; + infobuf[3] = (uint32_t) current->stackBase; + infobuf[4] = (uint32_t) current->stackEnd; + infobuf[5] = (uint32_t) current->entryPoint; + infobuf[6] = namelen; + memcpy(&infobuf[7], name, namelen); + offset += 0x1C + namelen; + + current = current->activeLink.next; + } + + __OSUnlockScheduler(this); + OSRestoreInterrupts(state); + + if (!client->sendall(&offset, 4)) return; + if (!client->sendall(buffer, offset)) return; + } else if (cmd == COMMAND_GET_STACK_TRACE) { + OSThread *thread; + if (!client->recvall(&thread, 4)) return; + + ExceptionState *state = exceptions.find(thread); + if (state) { + uint32_t sp = state->context.gpr[1]; + uint32_t trace[100]; + int index = 0; + while (checkDataRead(sp, 4) && index < 100) { + sp = *(uint32_t *) sp; + if (!checkDataRead(sp, 4)) break; + + trace[index] = *(uint32_t *) (sp + 4); + index++; + } + + if (!client->sendall(&index, 4)) return; + if (!client->sendall(trace, index * 4)) return; + } else { + int index = 0; + if (!client->sendall(&index, 4)) return; + } + } else if (cmd == COMMAND_TOGGLE_BREAKPOINT) { + uint32_t address; + if (!client->recvall(&address, 4)) return; + + breakpoints.toggle(address); + } else if (cmd == COMMAND_POKE_REGISTERS) { + OSThread *thread; + if (!client->recvall(&thread, 4)) return; + + uint32_t gpr[32]; + double fpr[32]; + if (!client->recvall(gpr, 4 * 32)) return; + if (!client->recvall(fpr, 8 * 32)) return; + + exceptions.lock(); + ExceptionState *state = exceptions.find(thread); + if (state) { + memcpy(state->context.gpr, gpr, 4 * 32); + memcpy(state->context.fpr, fpr, 8 * 32); + } + exceptions.unlock(); + } else if (cmd == COMMAND_RECEIVE_MESSAGES) { + OSMessage messages[10]; + + int count = 0; + while (count < 10) { + if (!OSReceiveMessage(&eventQueue, &messages[count], OS_MESSAGE_FLAGS_NONE)) { + break; + } + count++; + } + + if (!client->sendall(&count, 4)) return; + for (int i = 0; i < count; i++) { + if (!client->sendall(&messages[i], sizeof(OSMessage))) return; + if (messages[i].args[0]) { + void *data = (void *) messages[i].args[0]; + size_t length = messages[i].args[1]; + if (!client->sendall(data, length)) return; + } + } + } else if (cmd == COMMAND_SEND_MESSAGE) { + OSMessage message; + if (!client->recvall(&message, sizeof(OSMessage))) return; + + OSThread *thread = (OSThread *) message.args[0]; + + exceptions.lock(); + ExceptionState *state = exceptions.find(thread); + if (state) { + OSSendMessage(&state->queue, &message, OS_MESSAGE_FLAGS_NONE); + } + exceptions.unlock(); + } + } } void Debugger::threadFunc() { - int result = socket_lib_init(); - if (result < 0) { - OSFatal("Failed to initialize socket library"); - } - - OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_DSI, dsiHandler); - OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_ISI, isiHandler); - OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_PROGRAM, programHandler); - - Server server; - Client client; - - initialized = true; - while (true) { - if (!server.init(Socket::TCP)) continue; - if (!server.bind(1560)) continue; - if (!server.accept(&client)) continue; - connected = true; - mainLoop(&client); - cleanup(); - connected = false; - client.close(); - server.close(); - } + int result = socket_lib_init(); + if (result < 0) { + OSFatal("Failed to initialize socket library"); + } + + OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_DSI, dsiHandler); + OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_ISI, isiHandler); + OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_PROGRAM, programHandler); + + Server server; + Client client; + + initialized = true; + while (true) { + if (!server.init(Socket::TCP)) continue; + if (!server.bind(1560)) continue; + if (!server.accept(&client)) continue; + connected = true; + mainLoop(&client); + cleanup(); + connected = false; + client.close(); + server.close(); + } } int Debugger::threadEntry(int argc, void *argv) { - debugger->threadFunc(); - return 0; + debugger->threadFunc(); + return 0; } void Debugger::start() { - initialized = false; - connected = false; - firstTrap = true; - - OSInitMessageQueue(&eventQueue, eventMessages, MESSAGE_COUNT); - - breakpoints.init(); - exceptions.init(); - stepper.init(); - - serverThread = new OSThread(); - char *stack = new char[0x8000]; + initialized = false; + connected = false; + firstTrap = true; - OSCreateThread( - serverThread, threadEntry, 0, 0, - stack + STACK_SIZE, STACK_SIZE, - 0, 12 - ); - OSSetThreadName(serverThread, "Debug Server"); - OSResumeThread(serverThread); - - while (!initialized) { - OSSleepTicks(OSMillisecondsToTicks(20)); - } + OSInitMessageQueue(&eventQueue, eventMessages, MESSAGE_COUNT); + + breakpoints.init(); + exceptions.init(); + stepper.init(); + + serverThread = new OSThread(); + char *stack = new char[0x8000]; + + OSCreateThread( + serverThread, threadEntry, 0, 0, + stack + STACK_SIZE, STACK_SIZE, + 0, 12); + OSSetThreadName(serverThread, "Debug Server"); + OSResumeThread(serverThread); + + while (!initialized) { + OSSleepTicks(OSMillisecondsToTicks(20)); + } } Debugger *debugger; diff --git a/src/debugger.h b/src/debugger.h index 7ba629d..39eccf1 100644 --- a/src/debugger.h +++ b/src/debugger.h @@ -9,256 +9,256 @@ #include #define MESSAGE_COUNT 10 -#define STACK_SIZE 0x8000 +#define STACK_SIZE 0x8000 -#define TRAP 0x7FE00008 +#define TRAP 0x7FE00008 -template +template class Bits { public: - Bits(uint32_t value) { - this->value = value; - } + Bits(uint32_t value) { + this->value = value; + } - bool operator [](int index) { - return (value >> (N - index - 1)) & 1; - } + bool operator[](int index) { + return (value >> (N - index - 1)) & 1; + } private: - uint32_t value; + uint32_t value; }; class ExceptionState { public: - enum Type { - DSI, - ISI, - PROGRAM - }; + enum Type { + DSI, + ISI, + PROGRAM + }; - bool isBreakpoint(); - void resume(); - - Type type; - OSContext context; - - OSThread *thread; - - OSMessageQueue queue; - OSMessage message; - - bool isPaused; + bool isBreakpoint(); + void resume(); + + Type type; + OSContext context; + + OSThread *thread; + + OSMessageQueue queue; + OSMessage message; + + bool isPaused; }; class ExceptionMgr { public: - void init(); - void lock(); - void unlock(); - void cleanup(); - ExceptionState *find(OSThread *thread); - ExceptionState *findOrCreate(OSThread *thread); + void init(); + void lock(); + void unlock(); + void cleanup(); + ExceptionState *find(OSThread *thread); + ExceptionState *findOrCreate(OSThread *thread); private: - OSMutex mutex; - std::vector list; + OSMutex mutex; + std::vector list; }; class BreakPoint { public: - bool isRange(uint32_t addr, uint32_t length); + bool isRange(uint32_t addr, uint32_t length); - uint32_t address; - uint32_t instruction; - bool isSpecial; + uint32_t address; + uint32_t instruction; + bool isSpecial; }; class SpecialBreakPoint : public BreakPoint { public: - OSThread *thread; + OSThread *thread; }; -template +template class BreakPointList { public: - size_t size() { - return list.size(); - } - - T *alloc() { - for (int i = 0; i < size(); i++) { - if (list[i].address == 0) { - return &list[i]; - } - } - - T newBp; - newBp.address = 0; - newBp.instruction = 0; - list.push_back(newBp); - return &list.back(); - } - - T *find(uint32_t addr) { - for (int i = 0; i < size(); i++) { - if (list[i].address == addr) { - return &list[i]; - } - } - return nullptr; - } - - T *findRange(uint32_t addr, uint32_t length, int *index) { - int i = *index; - while (i < size()) { - if (list[i].isRange(addr, length)) { - *index = i + 1; - return &list[i]; - } - i++; - } - - if (i > *index) { - *index = i; - } - return nullptr; - } - - T *operator [](int index) { - return &list[index]; - } - - void cleanup() { - for (int i = 0; i < size(); i++) { - if (list[i].address != 0) { - KernelWriteU32(list[i].address, list[i].instruction); - list[i].address = 0; - list[i].instruction = 0; - } - } - } + size_t size() { + return list.size(); + } + + T *alloc() { + for (int i = 0; i < size(); i++) { + if (list[i].address == 0) { + return &list[i]; + } + } + + T newBp; + newBp.address = 0; + newBp.instruction = 0; + list.push_back(newBp); + return &list.back(); + } + + T *find(uint32_t addr) { + for (int i = 0; i < size(); i++) { + if (list[i].address == addr) { + return &list[i]; + } + } + return nullptr; + } + + T *findRange(uint32_t addr, uint32_t length, int *index) { + int i = *index; + while (i < size()) { + if (list[i].isRange(addr, length)) { + *index = i + 1; + return &list[i]; + } + i++; + } + + if (i > *index) { + *index = i; + } + return nullptr; + } + + T *operator[](int index) { + return &list[index]; + } + + void cleanup() { + for (int i = 0; i < size(); i++) { + if (list[i].address != 0) { + KernelWriteU32(list[i].address, list[i].instruction); + list[i].address = 0; + list[i].instruction = 0; + } + } + } private: - std::vector list; + std::vector list; }; class BreakPointMgr { public: - void init(); - void lock(); - void unlock(); - void cleanup(); - bool isCustom(uint32_t addr); - bool isSoftware(uint32_t addr); - bool isSpecial(uint32_t addr); - void read(void *buffer, uint32_t addr, uint32_t length); - void write(const void *buffer, uint32_t addr, uint32_t length); - void toggle(uint32_t addr); - uint32_t getInstr(uint32_t addr); - BreakPoint *find(uint32_t addr, bool includeSpecial); - BreakPoint *findRange(uint32_t addr, uint32_t length, int *index, bool includeSpecial); - SpecialBreakPoint *findSpecial(uint32_t addr, OSThread *thread); - void clearSpecial(OSThread *thread); - void predictStep(ExceptionState *state, bool stepOver); - -private: - BreakPoint *alloc(); - SpecialBreakPoint *allocSpecial(); - void disable(BreakPoint *bp); - void enable(BreakPoint *bp, uint32_t addr); + void init(); + void lock(); + void unlock(); + void cleanup(); + bool isCustom(uint32_t addr); + bool isSoftware(uint32_t addr); + bool isSpecial(uint32_t addr); + void read(void *buffer, uint32_t addr, uint32_t length); + void write(const void *buffer, uint32_t addr, uint32_t length); + void toggle(uint32_t addr); + uint32_t getInstr(uint32_t addr); + BreakPoint *find(uint32_t addr, bool includeSpecial); + BreakPoint *findRange(uint32_t addr, uint32_t length, int *index, bool includeSpecial); + SpecialBreakPoint *findSpecial(uint32_t addr, OSThread *thread); + void clearSpecial(OSThread *thread); + void predictStep(ExceptionState *state, bool stepOver); - BreakPointList breakpoints; - BreakPointList special; - - OSMutex mutex; +private: + BreakPoint *alloc(); + SpecialBreakPoint *allocSpecial(); + void disable(BreakPoint *bp); + void enable(BreakPoint *bp, uint32_t addr); + + BreakPointList breakpoints; + BreakPointList special; + + OSMutex mutex; }; class StepMgr { public: - void init(); - void lock(); - void unlock(); - void singleStep(ExceptionState *state, uint32_t instruction); - void handleBreakPoint(ExceptionState *state); - void adjustAddress(ExceptionState *state); - + void init(); + void lock(); + void unlock(); + void singleStep(ExceptionState *state, uint32_t instruction); + void handleBreakPoint(ExceptionState *state); + void adjustAddress(ExceptionState *state); + private: - static uint32_t buffer[96]; + static uint32_t buffer[96]; - uint32_t *alloc(); - void free(int index); - void branchConditional(ExceptionState *state, uint32_t instruction, uint32_t target, bool checkCtr); + uint32_t *alloc(); + void free(int index); + void branchConditional(ExceptionState *state, uint32_t instruction, uint32_t target, bool checkCtr); - OSMutex mutex; + OSMutex mutex; - uint32_t usedMask; + uint32_t usedMask; }; class Debugger { public: - enum StepCommand { - STEP_CONTINUE, - STEP_INTO, - STEP_OVER - }; + enum StepCommand { + STEP_CONTINUE, + STEP_INTO, + STEP_OVER + }; - void start(); + void start(); private: - enum Command { - COMMAND_CLOSE, - COMMAND_READ, - COMMAND_WRITE, - COMMAND_WRITE_CODE, - COMMAND_GET_MODULE_NAME, - COMMAND_GET_MODULE_LIST, - COMMAND_GET_THREAD_LIST, - COMMAND_GET_STACK_TRACE, - COMMAND_TOGGLE_BREAKPOINT, - COMMAND_POKE_REGISTERS, - COMMAND_RECEIVE_MESSAGES, - COMMAND_SEND_MESSAGE - }; - - static int threadEntry(int argc, void *argv); - static bool dsiHandler(OSContext *context); - static bool isiHandler(OSContext *context); - static bool programHandler(OSContext *context); - static void exceptionHandler(OSContext *context, ExceptionState::Type type); - - void threadFunc(); - void mainLoop(Client *client); - void handleException(OSContext *context, ExceptionState::Type type); - void handleFatalCrash(OSContext *context, ExceptionState::Type type); - void handleCrash(ExceptionState *state); - void handleBreakPoint(ExceptionState *state); - void processBreakPoint(ExceptionState *state); - void resumeBreakPoint(ExceptionState *state); - StepCommand notifyBreak(ExceptionState *state); - void cleanup(); - - bool checkDataRead(uint32_t addr, uint32_t length); - - OSMessageQueue eventQueue; - OSMessage eventMessages[MESSAGE_COUNT]; - - OSThread *serverThread; - - BreakPointMgr breakpoints; - ExceptionMgr exceptions; - StepMgr stepper; + enum Command { + COMMAND_CLOSE, + COMMAND_READ, + COMMAND_WRITE, + COMMAND_WRITE_CODE, + COMMAND_GET_MODULE_NAME, + COMMAND_GET_MODULE_LIST, + COMMAND_GET_THREAD_LIST, + COMMAND_GET_STACK_TRACE, + COMMAND_TOGGLE_BREAKPOINT, + COMMAND_POKE_REGISTERS, + COMMAND_RECEIVE_MESSAGES, + COMMAND_SEND_MESSAGE + }; - bool initialized; - bool connected; - bool firstTrap; + static int threadEntry(int argc, void *argv); + static bool dsiHandler(OSContext *context); + static bool isiHandler(OSContext *context); + static bool programHandler(OSContext *context); + static void exceptionHandler(OSContext *context, ExceptionState::Type type); + + void threadFunc(); + void mainLoop(Client *client); + void handleException(OSContext *context, ExceptionState::Type type); + void handleFatalCrash(OSContext *context, ExceptionState::Type type); + void handleCrash(ExceptionState *state); + void handleBreakPoint(ExceptionState *state); + void processBreakPoint(ExceptionState *state); + void resumeBreakPoint(ExceptionState *state); + StepCommand notifyBreak(ExceptionState *state); + void cleanup(); + + bool checkDataRead(uint32_t addr, uint32_t length); + + OSMessageQueue eventQueue; + OSMessage eventMessages[MESSAGE_COUNT]; + + OSThread *serverThread; + + BreakPointMgr breakpoints; + ExceptionMgr exceptions; + StepMgr stepper; + + bool initialized; + bool connected; + bool firstTrap; }; extern Debugger *debugger; diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 79382e3..9fffc6d 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -2,50 +2,49 @@ #include "cafe/coreinit.h" void DumpContext(OSContext *context, const char *excType) { - char buffer[1000]; - snprintf(buffer, 1000, - "%s exception occurred!\n" - "r0 :%08X r1 :%08X r2 :%08X r3 :%08X r4 :%08X\n" - "r5 :%08X r6 :%08X r7 :%08X r8 :%08X r9 :%08X\n" - "r10:%08X r11:%08X r12:%08X r13:%08X r14:%08X\n" - "r15:%08X r16:%08X r17:%08X r18:%08X r19:%08X\n" - "r20:%08X r21:%08X r22:%08X r23:%08X r24:%08X\n" - "r25:%08X r26:%08X r27:%08X r28:%08X r29:%08X\n" - "r30:%08X r31:%08X LR :%08X CTR:%08X XER:%08X\n" - "\n" - "SRR0:%08X SRR1:%08X DSISR:%08X DAR:%08X", - excType, - context->gpr[0], context->gpr[1], context->gpr[2], context->gpr[3], - context->gpr[4], context->gpr[5], context->gpr[6], context->gpr[7], - context->gpr[8], context->gpr[9], context->gpr[10], context->gpr[11], - context->gpr[12], context->gpr[13], context->gpr[14], context->gpr[15], - context->gpr[16], context->gpr[17], context->gpr[18], context->gpr[19], - context->gpr[20], context->gpr[21], context->gpr[22], context->gpr[23], - context->gpr[24], context->gpr[25], context->gpr[26], context->gpr[27], - context->gpr[28], context->gpr[29], context->gpr[30], context->gpr[31], - context->lr, context->ctr, context->xer, context->srr0, context->srr1, - context->dsisr, context->dar - ); - OSFatal(buffer); + char buffer[1000]; + snprintf(buffer, 1000, + "%s exception occurred!\n" + "r0 :%08X r1 :%08X r2 :%08X r3 :%08X r4 :%08X\n" + "r5 :%08X r6 :%08X r7 :%08X r8 :%08X r9 :%08X\n" + "r10:%08X r11:%08X r12:%08X r13:%08X r14:%08X\n" + "r15:%08X r16:%08X r17:%08X r18:%08X r19:%08X\n" + "r20:%08X r21:%08X r22:%08X r23:%08X r24:%08X\n" + "r25:%08X r26:%08X r27:%08X r28:%08X r29:%08X\n" + "r30:%08X r31:%08X LR :%08X CTR:%08X XER:%08X\n" + "\n" + "SRR0:%08X SRR1:%08X DSISR:%08X DAR:%08X", + excType, + context->gpr[0], context->gpr[1], context->gpr[2], context->gpr[3], + context->gpr[4], context->gpr[5], context->gpr[6], context->gpr[7], + context->gpr[8], context->gpr[9], context->gpr[10], context->gpr[11], + context->gpr[12], context->gpr[13], context->gpr[14], context->gpr[15], + context->gpr[16], context->gpr[17], context->gpr[18], context->gpr[19], + context->gpr[20], context->gpr[21], context->gpr[22], context->gpr[23], + context->gpr[24], context->gpr[25], context->gpr[26], context->gpr[27], + context->gpr[28], context->gpr[29], context->gpr[30], context->gpr[31], + context->lr, context->ctr, context->xer, context->srr0, context->srr1, + context->dsisr, context->dar); + OSFatal(buffer); } bool DSIHandler(OSContext *context) { - DumpContext(context, "A DSI"); - return false; + DumpContext(context, "A DSI"); + return false; } bool ISIHandler(OSContext *context) { - DumpContext(context, "An ISI"); - return false; + DumpContext(context, "An ISI"); + return false; } bool ProgramHandler(OSContext *context) { - DumpContext(context, "A program"); - return false; + DumpContext(context, "A program"); + return false; } void InstallExceptionHandlers() { - OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_DSI, DSIHandler); - OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_ISI, ISIHandler); - OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_PROGRAM, ProgramHandler); + OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_DSI, DSIHandler); + OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_ISI, ISIHandler); + OSSetExceptionCallbackEx(OS_EXCEPTION_MODE_GLOBAL_ALL_CORES, OS_EXCEPTION_TYPE_PROGRAM, ProgramHandler); } diff --git a/src/hbl.h b/src/hbl.h index f6303b7..c37cf67 100644 --- a/src/hbl.h +++ b/src/hbl.h @@ -6,10 +6,10 @@ #define MEM_BASE 0x800000 struct OsSpecifics { - uint32_t OSDynLoad_Acquire; - uint32_t OSDynLoad_FindExport; + uint32_t OSDynLoad_Acquire; + uint32_t OSDynLoad_FindExport; }; -#define OS_SPECIFICS ((OsSpecifics *)(MEM_BASE + 0x1500)) +#define OS_SPECIFICS ((OsSpecifics *) (MEM_BASE + 0x1500)) -#define EXIT_SUCCESS 0 +#define EXIT_SUCCESS 0 #define EXIT_RELAUNCH_ON_LOAD -3 diff --git a/src/input.cpp b/src/input.cpp index faf034c..b6a50b0 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -3,19 +3,19 @@ #include uint32_t GetInput(uint32_t mask) { - VPADStatus input; - int error; - VPADRead(0, &input, 1, &error); - return input.pressed & mask; + VPADStatus input; + int error; + VPADRead(0, &input, 1, &error); + return input.pressed & mask; } uint32_t WaitInput(uint32_t mask) { - VPADStatus input; - int error; - while (true) { - VPADRead(0, &input, 1, &error); - if (input.pressed & mask) { - return input.pressed & mask; - } - } + VPADStatus input; + int error; + while (true) { + VPADRead(0, &input, 1, &error); + if (input.pressed & mask) { + return input.pressed & mask; + } + } } diff --git a/src/kernel.cpp b/src/kernel.cpp index d7037ee..591432b 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -13,52 +13,50 @@ extern "C" void SCKernelCopyData(uint32_t dst, uint32_t src, uint32_t len); extern "C" void KernelCopyData(uint32_t dst, uint32_t src, uint32_t len); void KernelWrite(uint32_t addr, const void *data, uint32_t length) { - uint32_t dst = OSEffectiveToPhysical(addr); - uint32_t src = OSEffectiveToPhysical((uint32_t)data); - KernelCopyData(dst, src, length); - DCFlushRange((void *)addr, length); - ICInvalidateRange((void *)addr, length); + uint32_t dst = OSEffectiveToPhysical(addr); + uint32_t src = OSEffectiveToPhysical((uint32_t) data); + KernelCopyData(dst, src, length); + DCFlushRange((void *) addr, length); + ICInvalidateRange((void *) addr, length); } void KernelWriteU32(uint32_t addr, uint32_t value) { - uint32_t dst = OSEffectiveToPhysical(addr); - uint32_t src = OSEffectiveToPhysical((uint32_t)&value); - KernelCopyData(dst, src, 4); - DCFlushRange((void *)addr, 4); - ICInvalidateRange((void *)addr, 4); + uint32_t dst = OSEffectiveToPhysical(addr); + uint32_t src = OSEffectiveToPhysical((uint32_t) &value); + KernelCopyData(dst, src, 4); + DCFlushRange((void *) addr, 4); + ICInvalidateRange((void *) addr, 4); } /* Write a 32-bit word with kernel permissions */ -void __attribute__ ((noinline)) kern_write(uint32_t addr, uint32_t value) -{ - asm volatile ( - "li 3,1\n" - "li 4,0\n" - "mr 5,%1\n" - "li 6,0\n" - "li 7,0\n" - "lis 8,1\n" - "mr 9,%0\n" - "mr %1,1\n" - "li 0,0x3500\n" - "sc\n" - "nop\n" - "mr 1,%1\n" - : - : "r"(addr), "r"(value) - : "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10", - "11", "12" - ); +void __attribute__((noinline)) kern_write(uint32_t addr, uint32_t value) { + asm volatile( + "li 3,1\n" + "li 4,0\n" + "mr 5,%1\n" + "li 6,0\n" + "li 7,0\n" + "lis 8,1\n" + "mr 9,%0\n" + "mr %1,1\n" + "li 0,0x3500\n" + "sc\n" + "nop\n" + "mr 1,%1\n" + : + : "r"(addr), "r"(value) + : "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10", + "11", "12"); } void PatchSyscall(int index, uint32_t addr) { - kern_write(KERN_SYSCALL_TBL1 + index * 4, addr); - kern_write(KERN_SYSCALL_TBL2 + index * 4, addr); - kern_write(KERN_SYSCALL_TBL3 + index * 4, addr); - kern_write(KERN_SYSCALL_TBL4 + index * 4, addr); - kern_write(KERN_SYSCALL_TBL5 + index * 4, addr); + kern_write(KERN_SYSCALL_TBL1 + index * 4, addr); + kern_write(KERN_SYSCALL_TBL2 + index * 4, addr); + kern_write(KERN_SYSCALL_TBL3 + index * 4, addr); + kern_write(KERN_SYSCALL_TBL4 + index * 4, addr); + kern_write(KERN_SYSCALL_TBL5 + index * 4, addr); } void kernelInitialize() { - PatchSyscall(0x25, (uint32_t)SCKernelCopyData); + PatchSyscall(0x25, (uint32_t) SCKernelCopyData); } diff --git a/src/main.cpp b/src/main.cpp index 754098b..ff842a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,90 +1,87 @@ #include "cafe/coreinit.h" -#include "cafe/sysapp.h" -#include "cafe/nsysnet.h" -#include "cafe/vpad.h" -#include "cafe/nn_save.h" #include "cafe/nn_act.h" -#include "kernel.h" +#include "cafe/nn_save.h" +#include "cafe/nsysnet.h" +#include "cafe/sysapp.h" +#include "cafe/vpad.h" #include "hbl.h" +#include "kernel.h" -#include "patches.h" #include "debugger.h" #include "exceptions.h" -#include "screen.h" #include "menu.h" +#include "patches.h" +#include "screen.h" bool GetTitleIdOnDisk(uint64_t *titleId) { - MCPTitleListType title; - uint32_t count = 0; - - int handle = MCP_Open(); - MCP_TitleListByDevice(handle, "odd", &count, &title, sizeof(title)); - MCP_Close(handle); - - if (count > 0) { - *titleId = title.titleId; - return true; - } - return false; + MCPTitleListType title; + uint32_t count = 0; + + int handle = MCP_Open(); + MCP_TitleListByDevice(handle, "odd", &count, &title, sizeof(title)); + MCP_Close(handle); + + if (count > 0) { + *titleId = title.titleId; + return true; + } + return false; } int MenuMain() { - Screen screen; - screen.init(); - - Menu menu(&screen); - - Menu::Option result = menu.show(); - if (result == Menu::Exit) return EXIT_SUCCESS; - else if (result == Menu::LaunchDisk) { - uint64_t titleId; - if (GetTitleIdOnDisk(&titleId)) { - SYSLaunchTitle(titleId); - } - else { - menu.setMessage("Please insert a valid disk"); - } - } - else if (result == Menu::ReturnToMenu) { - SYSLaunchMenu(); - } - return EXIT_RELAUNCH_ON_LOAD; + Screen screen; + screen.init(); + + Menu menu(&screen); + + Menu::Option result = menu.show(); + if (result == Menu::Exit) return EXIT_SUCCESS; + else if (result == Menu::LaunchDisk) { + uint64_t titleId; + if (GetTitleIdOnDisk(&titleId)) { + SYSLaunchTitle(titleId); + } else { + menu.setMessage("Please insert a valid disk"); + } + } else if (result == Menu::ReturnToMenu) { + SYSLaunchMenu(); + } + return EXIT_RELAUNCH_ON_LOAD; } int DebuggerMain() { - ApplyPatches(); - debugger = new Debugger(); - debugger->start(); - return EXIT_RELAUNCH_ON_LOAD; + ApplyPatches(); + debugger = new Debugger(); + debugger->start(); + return EXIT_RELAUNCH_ON_LOAD; } bool firstRun = true; int start() { - coreinitInitialize(); - kernelInitialize(); - vpadInitialize(); - sysappInitialize(); - nsysnetInitialize(); - nnsaveInitialize(); - nnactInitialize(); - - InstallExceptionHandlers(); - - int result; - if (firstRun) { - result = MenuMain(); - } - else { - result = DebuggerMain(); - } - firstRun = false; - - return result; + coreinitInitialize(); + kernelInitialize(); + vpadInitialize(); + sysappInitialize(); + nsysnetInitialize(); + nnsaveInitialize(); + nnactInitialize(); + + InstallExceptionHandlers(); + + int result; + if (firstRun) { + result = MenuMain(); + } else { + result = DebuggerMain(); + } + firstRun = false; + + return result; } extern "C" int entryPoint() { - return start(); + return start(); } diff --git a/src/memory.cpp b/src/memory.cpp index 3649531..0cb8da5 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -3,26 +3,26 @@ #include -void * operator new(size_t size) { - return MEMAllocFromDefaultHeap(size); +void *operator new(size_t size) { + return MEMAllocFromDefaultHeap(size); } -void * operator new[](size_t size) { - return MEMAllocFromDefaultHeap(size); +void *operator new[](size_t size) { + return MEMAllocFromDefaultHeap(size); } -void * operator new(size_t size, int alignment) { - return MEMAllocFromDefaultHeapEx(size, alignment); +void *operator new(size_t size, int alignment) { + return MEMAllocFromDefaultHeapEx(size, alignment); } -void * operator new[](size_t size, int alignment) { - return MEMAllocFromDefaultHeapEx(size, alignment); +void *operator new[](size_t size, int alignment) { + return MEMAllocFromDefaultHeapEx(size, alignment); } void operator delete(void *ptr) { - MEMFreeToDefaultHeap(ptr); + MEMFreeToDefaultHeap(ptr); } void operator delete(void *ptr, size_t size) { - MEMFreeToDefaultHeap(ptr); + MEMFreeToDefaultHeap(ptr); } diff --git a/src/menu.cpp b/src/menu.cpp index e4f70d9..330cdf3 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,44 +1,43 @@ -#include "cafe/vpad.h" #include "menu.h" -#include "screen.h" -#include "input.h" +#include "cafe/vpad.h" #include "color.h" +#include "input.h" +#include "screen.h" Menu::Menu(Screen *screen) : screen(screen) { - currentOption = LaunchDisk; - message = nullptr; + currentOption = LaunchDisk; + message = nullptr; } Menu::Option Menu::show() { - while (true) { - redraw(); - uint32_t buttons = WaitInput(VPAD_BUTTON_A | VPAD_BUTTON_DOWN | VPAD_BUTTON_UP); - if (buttons & VPAD_BUTTON_A) return (Option)currentOption; - else if (buttons & VPAD_BUTTON_DOWN) { - if (currentOption < 2) currentOption++; - } - else if (buttons & VPAD_BUTTON_UP) { - if (currentOption > 0) currentOption--; - } - } + while (true) { + redraw(); + uint32_t buttons = WaitInput(VPAD_BUTTON_A | VPAD_BUTTON_DOWN | VPAD_BUTTON_UP); + if (buttons & VPAD_BUTTON_A) return (Option) currentOption; + else if (buttons & VPAD_BUTTON_DOWN) { + if (currentOption < 2) currentOption++; + } else if (buttons & VPAD_BUTTON_UP) { + if (currentOption > 0) currentOption--; + } + } } void Menu::setMessage(const char *message) { - this->message = message; - redraw(); + this->message = message; + redraw(); } void Menu::redraw() { - screen->clear(COLOR_BLUE); - screen->drawText(5, 5, "Wii U Debugger"); - screen->drawText(5, 7, "Choose an option:"); - screen->drawText(8, 9, "Install and launch disc"); - screen->drawText(8, 10, "Install and return to system menu"); - screen->drawText(8, 11, "Exit without installing"); - screen->drawText(5, 9 + currentOption, ">"); - if (message) { - screen->drawText(5, 13, message); - } - screen->flip(); + screen->clear(COLOR_BLUE); + screen->drawText(5, 5, "Wii U Debugger"); + screen->drawText(5, 7, "Choose an option:"); + screen->drawText(8, 9, "Install and launch disc"); + screen->drawText(8, 10, "Install and return to system menu"); + screen->drawText(8, 11, "Exit without installing"); + screen->drawText(5, 9 + currentOption, ">"); + if (message) { + screen->drawText(5, 13, message); + } + screen->flip(); } diff --git a/src/menu.h b/src/menu.h index e49c428..3ef984f 100644 --- a/src/menu.h +++ b/src/menu.h @@ -4,21 +4,21 @@ #include "screen.h" class Menu { - public: - enum Option { - LaunchDisk, - ReturnToMenu, - Exit - }; - - Menu(Screen *screen); - Option show(); - void setMessage(const char *message); - void redraw(); - - private: - Screen *screen; - int currentOption; - - const char *message; +public: + enum Option { + LaunchDisk, + ReturnToMenu, + Exit + }; + + Menu(Screen *screen); + Option show(); + void setMessage(const char *message); + void redraw(); + +private: + Screen *screen; + int currentOption; + + const char *message; }; diff --git a/src/patches.cpp b/src/patches.cpp index c013a28..5a273f3 100644 --- a/src/patches.cpp +++ b/src/patches.cpp @@ -5,53 +5,52 @@ #include int OSSetExceptionCallback_Patch() { - return 0; + return 0; } int OSSetExceptionCallbackEx_Patch() { - return 0; + return 0; } bool OSIsDebuggerInitialized_Patch() { - return true; + return true; } void Patch(void *funcPtr, void *patchPtr) { - OSDynLoad_NotifyData *sectionInfo = MainRPL->notifyData; - - uint32_t func = (uint32_t)funcPtr; - uint32_t patch = (uint32_t)patchPtr; - if (func < 0x01800000) { //OS function (with trampoline) - for (uint32_t addr = sectionInfo->textAddr; addr < 0x10000000; addr += 4) { - uint32_t *instrs = (uint32_t *)addr; - if (instrs[0] == (0x3D600000 | (func >> 16)) && //lis r11, func@h - instrs[1] == (0x616B0000 | (func & 0xFFFF)) && //ori r11, r11, func@l - instrs[2] == 0x7D6903A6 && //mtctr r11 - instrs[3] == 0x4E800420) //bctr - { - KernelWriteU32(addr, 0x3D600000 | (patch >> 16)); //lis r11, patch@h - KernelWriteU32(addr + 4, 0x616B0000 | (patch & 0xFFFF)); //ori r11, r11, patch@l - } - } - } - else { //Dynamic function - for (uint32_t addr = sectionInfo->textAddr; addr < 0x10000000; addr += 4) { - uint32_t instr = *(uint32_t *)addr; - if ((instr & 0xFC000002) == 0x48000000) { //b or bl - if ((instr & 0x03FFFFFC) == func - addr) { - instr = instr & ~0x03FFFFFC; - instr |= patch; - instr |= 2; //Turn b/bl into ba/bla - KernelWriteU32(addr, instr); - } - } - } - } + OSDynLoad_NotifyData *sectionInfo = MainRPL->notifyData; + + uint32_t func = (uint32_t) funcPtr; + uint32_t patch = (uint32_t) patchPtr; + if (func < 0x01800000) { //OS function (with trampoline) + for (uint32_t addr = sectionInfo->textAddr; addr < 0x10000000; addr += 4) { + uint32_t *instrs = (uint32_t *) addr; + if (instrs[0] == (0x3D600000 | (func >> 16)) && //lis r11, func@h + instrs[1] == (0x616B0000 | (func & 0xFFFF)) && //ori r11, r11, func@l + instrs[2] == 0x7D6903A6 && //mtctr r11 + instrs[3] == 0x4E800420) //bctr + { + KernelWriteU32(addr, 0x3D600000 | (patch >> 16)); //lis r11, patch@h + KernelWriteU32(addr + 4, 0x616B0000 | (patch & 0xFFFF)); //ori r11, r11, patch@l + } + } + } else { //Dynamic function + for (uint32_t addr = sectionInfo->textAddr; addr < 0x10000000; addr += 4) { + uint32_t instr = *(uint32_t *) addr; + if ((instr & 0xFC000002) == 0x48000000) { //b or bl + if ((instr & 0x03FFFFFC) == func - addr) { + instr = instr & ~0x03FFFFFC; + instr |= patch; + instr |= 2; //Turn b/bl into ba/bla + KernelWriteU32(addr, instr); + } + } + } + } } void ApplyPatches() { - Patch((void *)OSSetExceptionCallback, (void *)OSSetExceptionCallback_Patch); - Patch((void *)OSSetExceptionCallbackEx, (void *)OSSetExceptionCallbackEx_Patch); - - Patch((void *)OSIsDebuggerInitialized, (void *)OSIsDebuggerInitialized_Patch); + Patch((void *) OSSetExceptionCallback, (void *) OSSetExceptionCallback_Patch); + Patch((void *) OSSetExceptionCallbackEx, (void *) OSSetExceptionCallbackEx_Patch); + + Patch((void *) OSIsDebuggerInitialized, (void *) OSIsDebuggerInitialized_Patch); } diff --git a/src/screen.cpp b/src/screen.cpp index ffbb1e7..929a3d7 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -1,87 +1,87 @@ -#include "cafe/coreinit.h" #include "screen.h" +#include "cafe/coreinit.h" #include "memory.h" Screen::Screen() : screenBuffer(0) {} Screen::~Screen() { - if (screenBuffer) { - operator delete(screenBuffer); - } + if (screenBuffer) { + operator delete(screenBuffer); + } } void Screen::init() { - OSScreenInit(); - - uint32_t bufferSize0 = OSScreenGetBufferSizeEx(0); - uint32_t bufferSize1 = OSScreenGetBufferSizeEx(1); - screenBuffer = operator new(bufferSize0 + bufferSize1, 0x40); - OSScreenSetBufferEx(0, screenBuffer); - OSScreenSetBufferEx(1, (char *)screenBuffer + bufferSize0); + OSScreenInit(); - OSScreenEnableEx(0, 1); - OSScreenEnableEx(1, 1); - OSScreenClearBufferEx(0, 0); - OSScreenClearBufferEx(1, 0); - OSScreenFlipBuffersEx(0); - OSScreenFlipBuffersEx(1); + uint32_t bufferSize0 = OSScreenGetBufferSizeEx(0); + uint32_t bufferSize1 = OSScreenGetBufferSizeEx(1); + screenBuffer = operator new(bufferSize0 + bufferSize1, 0x40); + OSScreenSetBufferEx(0, screenBuffer); + OSScreenSetBufferEx(1, (char *) screenBuffer + bufferSize0); + + OSScreenEnableEx(0, 1); + OSScreenEnableEx(1, 1); + OSScreenClearBufferEx(0, 0); + OSScreenClearBufferEx(1, 0); + OSScreenFlipBuffersEx(0); + OSScreenFlipBuffersEx(1); } void Screen::clear(Display screen, uint32_t color) { - OSScreenClearBufferEx(screen, color); + OSScreenClearBufferEx(screen, color); } void Screen::drawRect(Display screen, int x1, int y1, int x2, int y2, uint32_t color) { - for (int x = x1; x < x2; x++) { - OSScreenPutPixelEx(screen, x, y1, color); - OSScreenPutPixelEx(screen, x, y2, color); - } - for (int y = y1; y < y2; y++) { - OSScreenPutPixelEx(screen, x1, y, color); - OSScreenPutPixelEx(screen, x2, y, color); - } + for (int x = x1; x < x2; x++) { + OSScreenPutPixelEx(screen, x, y1, color); + OSScreenPutPixelEx(screen, x, y2, color); + } + for (int y = y1; y < y2; y++) { + OSScreenPutPixelEx(screen, x1, y, color); + OSScreenPutPixelEx(screen, x2, y, color); + } } void Screen::fillRect(Display screen, int x1, int y1, int x2, int y2, uint32_t color) { - for (int x = x1; x < x2; x++) { - for (int y = y1; y < y2; y++) { - OSScreenPutPixelEx(screen, x, y, color); - } - } + for (int x = x1; x < x2; x++) { + for (int y = y1; y < y2; y++) { + OSScreenPutPixelEx(screen, x, y, color); + } + } } void Screen::drawText(Display screen, int x, int y, const char *text) { - OSScreenPutFontEx(screen, x, y, text); + OSScreenPutFontEx(screen, x, y, text); } void Screen::flip(Display screen) { - OSScreenFlipBuffersEx(screen); + OSScreenFlipBuffersEx(screen); } int Screen::convx(int x) { return x * 854 / 1280; } int Screen::convy(int y) { return y * 480 / 720; } void Screen::clear(uint32_t color) { - clear(TV, color); - clear(DRC, color); + clear(TV, color); + clear(DRC, color); } void Screen::drawRect(int x1, int y1, int x2, int y2, uint32_t color) { - drawRect(TV, x1, y1, x2, y2, color); - drawRect(DRC, convx(x1), convy(y1), convx(x2), convy(y2), color); + drawRect(TV, x1, y1, x2, y2, color); + drawRect(DRC, convx(x1), convy(y1), convx(x2), convy(y2), color); } void Screen::fillRect(int x1, int y1, int x2, int y2, uint32_t color) { - fillRect(TV, x1, y1, x2, y2, color); - fillRect(DRC, convx(x1), convy(y1), convx(x2), convy(y2), color); + fillRect(TV, x1, y1, x2, y2, color); + fillRect(DRC, convx(x1), convy(y1), convx(x2), convy(y2), color); } void Screen::drawText(int x, int y, const char *text) { - drawText(TV, x, y, text); - drawText(DRC, x, y, text); + drawText(TV, x, y, text); + drawText(DRC, x, y, text); } void Screen::flip() { - flip(TV); - flip(DRC); + flip(TV); + flip(DRC); } diff --git a/src/screen.h b/src/screen.h index b4d722a..cf8d166 100644 --- a/src/screen.h +++ b/src/screen.h @@ -4,31 +4,31 @@ #include class Screen { - public: - enum Display { - TV, - DRC - }; - - Screen(); - ~Screen(); - - void init(); - void clear(uint32_t color); - void drawRect(int x1, int y1, int x2, int y2, uint32_t color); - void fillRect(int x1, int y1, int x2, int y2, uint32_t color); - void drawText(int x, int y, const char *text); - void flip(); - - void clear(Display screen, uint32_t color); - void drawRect(Display screen, int x1, int y1, int x2, int y2, uint32_t color); - void fillRect(Display screen, int x1, int y1, int x2, int y2, uint32_t color); - void drawText(Display screen, int x, int y, const char *text); - void flip(Display screen); +public: + enum Display { + TV, + DRC + }; - private: - void *screenBuffer; - - int convx(int x); - int convy(int y); + Screen(); + ~Screen(); + + void init(); + void clear(uint32_t color); + void drawRect(int x1, int y1, int x2, int y2, uint32_t color); + void fillRect(int x1, int y1, int x2, int y2, uint32_t color); + void drawText(int x, int y, const char *text); + void flip(); + + void clear(Display screen, uint32_t color); + void drawRect(Display screen, int x1, int y1, int x2, int y2, uint32_t color); + void fillRect(Display screen, int x1, int y1, int x2, int y2, uint32_t color); + void drawText(Display screen, int x, int y, const char *text); + void flip(Display screen); + +private: + void *screenBuffer; + + int convx(int x); + int convy(int y); }; diff --git a/src/socket.cpp b/src/socket.cpp index f57da73..acf227b 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -6,96 +6,95 @@ Socket::Socket() : sock(-1) {} Socket::~Socket() { - if (sock >= 0) { - socketclose(sock); - } + if (sock >= 0) { + socketclose(sock); + } } bool Socket::init(Type type) { - if (type == TCP) { - sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - } - else { - sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - } - return sock >= 0; + if (type == TCP) { + sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + } else { + sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + } + return sock >= 0; } bool Socket::close() { - if (sock >= 0) { - int result = socketclose(sock); - sock = -1; - return result == 0; - } - return true; + if (sock >= 0) { + int result = socketclose(sock); + sock = -1; + return result == 0; + } + return true; } bool Client::sendall(const void *data, size_t length) { - size_t sent = 0; - while (sent < length) { - int num = send(sock, data, length - sent, 0); - if (num < 0) { - close(); - return false; - } - - sent += num; - data = (const char *)data + num; - } - return true; + size_t sent = 0; + while (sent < length) { + int num = send(sock, data, length - sent, 0); + if (num < 0) { + close(); + return false; + } + + sent += num; + data = (const char *) data + num; + } + return true; } bool Client::recvall(void *data, size_t length) { - size_t received = 0; - while (received < length) { - int num = recv(sock, data, length - received, 0); - if (num <= 0) { - close(); - return false; - } - - received += num; - data = (char *)data + num; - } - return true; + size_t received = 0; + while (received < length) { + int num = recv(sock, data, length - received, 0); + if (num <= 0) { + close(); + return false; + } + + received += num; + data = (char *) data + num; + } + return true; } bool Server::bind(int port) { - uint32_t reuseaddr = 1; - int result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, 4); - if (result < 0) { - close(); - return false; - } - - sockaddr serverAddr = {0}; - serverAddr.family = AF_INET; - serverAddr.port = port; - serverAddr.addr = 0; - - result = ::bind(sock, &serverAddr, 16); - if (result < 0) { - close(); - return false; - } - return true; + uint32_t reuseaddr = 1; + int result = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, 4); + if (result < 0) { + close(); + return false; + } + + sockaddr serverAddr = {0}; + serverAddr.family = AF_INET; + serverAddr.port = port; + serverAddr.addr = 0; + + result = ::bind(sock, &serverAddr, 16); + if (result < 0) { + close(); + return false; + } + return true; } bool Server::accept(Client *client) { - int result = listen(sock, 1); - if (result < 0) { - close(); - return false; - } - - int fd = ::accept(sock, 0, 0); - if (fd < 0) { - close(); - return false; - } - - client->sock = fd; - return true; + int result = listen(sock, 1); + if (result < 0) { + close(); + return false; + } + + int fd = ::accept(sock, 0, 0); + if (fd < 0) { + close(); + return false; + } + + client->sock = fd; + return true; } diff --git a/src/socket.h b/src/socket.h index 88ee6ec..f8e5c43 100644 --- a/src/socket.h +++ b/src/socket.h @@ -5,27 +5,27 @@ class Socket { public: - enum Type { - TCP, - UDP - }; - - Socket(); - ~Socket(); - bool init(Type type); - bool close(); + enum Type { + TCP, + UDP + }; - int sock; + Socket(); + ~Socket(); + bool init(Type type); + bool close(); + + int sock; }; class Client : public Socket { public: - bool sendall(const void *data, size_t length); - bool recvall(void *data, size_t length); + bool sendall(const void *data, size_t length); + bool recvall(void *data, size_t length); }; class Server : public Socket { public: - bool bind(int port); - bool accept(Client *client); + bool bind(int port); + bool accept(Client *client); };