From e97f10a6fa573e8b817ed03a4674d52b1818dafa Mon Sep 17 00:00:00 2001 From: Rozelette Date: Mon, 19 Apr 2021 16:44:12 -0500 Subject: [PATCH] sys_slowly OK (#101) * sys_slowly OK * Feedback * rename type member --- include/functions.h | 8 +++---- include/variables.h | 2 +- include/z64.h | 39 +++++++++++++++++++++++------- linker_scripts/code_script.txt | 1 + src/boot_O2_g3/boot_main.c | 2 +- src/boot_O2_g3/idle.c | 4 ++-- src/boot_O2_g3/irqmgr.c | 2 +- src/boot_O2_g3/z_std_dma.c | 10 ++++---- src/code/sys_slowly.c | 44 ++++++++++++++++++++++++++++++++++ tables/functions.txt | 2 +- tables/objects.txt | 1 + tables/variables.txt | 2 +- 12 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 src/code/sys_slowly.c diff --git a/include/functions.h b/include/functions.h index 1f91e1cf9..0ade16d9d 100644 --- a/include/functions.h +++ b/include/functions.h @@ -3456,10 +3456,10 @@ void func_80183070(void); // void func_801850A0(UNK_TYPE1 param_1, UNK_TYPE1 param_2, UNK_TYPE1 param_3, UNK_TYPE1 param_4, UNK_TYPE4 param_5, UNK_TYPE4 param_6, UNK_TYPE4 param_7); // void func_801853C8(UNK_TYPE1 param_1, UNK_TYPE1 param_2, UNK_TYPE1 param_3, UNK_TYPE1 param_4, UNK_TYPE4 param_5, UNK_TYPE4 param_6); // void func_80185460(void); -void Slowly_Main(s8018571C* iParm1); -void Slowly_ThreadEntry(s8018571C* param_1); -// void Slowly_Start(s8018571C* param_1, s32* stack, UNK_TYPE4 param_3, s32 param_4, s32 param_5); -// void func_801857A0(void); +//void Slowly_Main(SlowlyTask* slowly); +//void Slowly_ThreadEntry(SlowlyTask* slowly); +void Slowly_Start(SlowlyTask* slowly, void* stack, void (*callback)(), void* callbackArg0, void* callbackArg1); +void Slowly_Stop(SlowlyTask* slowly); // void func_801857C0(void); // char* func_801857D0(void); // void func_80185864(void); diff --git a/include/variables.h b/include/variables.h index 3c04b85c3..acd23dfa3 100644 --- a/include/variables.h +++ b/include/variables.h @@ -4096,7 +4096,7 @@ extern Color_RGBA8 D_801F6D30; // extern UNK_TYPE1 D_801F6D50; extern u8 D_801F6DFC; // extern UNK_TYPE1 D_801F6DFD; -extern s8018571C D_801F6E00; +extern SlowlyTask D_801F6E00; // extern UNK_TYPE1 D_801F6FC0; extern StackEntry slowlyStackEntry; extern u8 slowlyStack[4096]; diff --git a/include/z64.h b/include/z64.h index 80d182faa..67c05b115 100644 --- a/include/z64.h +++ b/include/z64.h @@ -38,6 +38,19 @@ #define SCREEN_WIDTH_HIGH_RES 576 #define SCREEN_HEIGHT_HIGH_RES 454 +#define Z_THREAD_ID_IDLE 1 +#define Z_THREAD_ID_SLOWLY 2 +#define Z_THREAD_ID_MAIN 3 +#define Z_THREAD_ID_DMAMGR 18 +#define Z_THREAD_ID_IRQMGR 19 + +#define Z_PRIORITY_SLOWLY 5 +#define Z_PRIORITY_GRAPH 9 +#define Z_PRIORITY_IDLE 12 +#define Z_PRIORITY_MAIN 12 +#define Z_PRIORITY_DMAMGR 17 +#define Z_PRIORITY_IRQMGR 18 + typedef struct { /* 0x0 */ s16 priority; // Lower means higher priority. -1 means it ignores priority /* 0x2 */ s16 length; @@ -456,16 +469,24 @@ typedef void(*osCreateThread_func)(void*); typedef void*(*printf_func)(void*, char*, size_t); +typedef enum { + SLOWLY_CALLBACK_NO_ARGS, + SLOWLY_CALLBACK_ONE_ARG, + SLOWLY_CALLBACK_TWO_ARGS +} SlowlyCallbackArgCount; + typedef struct { - /* 0x000 */ OSThread unk0; - /* 0x1B0 */ s8 argCount; - /* 0x1B1 */ s8 unk1B1; - /* 0x1B2 */ UNK_TYPE1 pad1B2[0x2]; - /* 0x1B4 */ UNK_TYPE1 func; - /* 0x1B5 */ UNK_TYPE1 pad1B5[0x3]; - /* 0x1B8 */ s32 arg0; - /* 0x1BC */ s32 arg1; -} s8018571C; // size = 0x1C0 + /* 0x000 */ OSThread thread; + /* 0x1B0 */ u8 callbackArgCount; + /* 0x1B1 */ u8 status; + /* 0x1B4 */ union { + void (*callback0)(void); + void (*callback1)(void*); + void (*callback2)(void*, void*); + }; + /* 0x1B8 */ void* callbackArg0; + /* 0x1BC */ void* callbackArg1; +} SlowlyTask; // size = 0x1C0 typedef struct { /* 0x00 */ int unk0; diff --git a/linker_scripts/code_script.txt b/linker_scripts/code_script.txt index 22839accb..6fca65822 100644 --- a/linker_scripts/code_script.txt +++ b/linker_scripts/code_script.txt @@ -438,6 +438,7 @@ SECTIONS build/asm/code/code_0x80182CE0.o(.text) build/src/code/code_0x80183070.o(.text) build/asm/code/code_0x801830A0.o(.text) + build/src/code/sys_slowly.o(.text) build/asm/code/sys_flashrom.o(.text) build/asm/code/code_0x80185F90.o(.text) build/asm/code/code_0x801877D0.o(.text) diff --git a/src/boot_O2_g3/boot_main.c b/src/boot_O2_g3/boot_main.c index 15f975124..2a7d69f98 100644 --- a/src/boot_O2_g3/boot_main.c +++ b/src/boot_O2_g3/boot_main.c @@ -9,6 +9,6 @@ void bootproc(void) { osUnmapTLBAll(); gCartHandle = osCartRomInit(); StackCheck_Init(&sIdleThreadInfo, sIdleThreadStack, sIdleThreadStack + sizeof(sIdleThreadStack), 0, 256, "idle"); - osCreateThread(&sIdleThread, 1, Idle_ThreadEntry, NULL, sIdleThreadStack + sizeof(sIdleThreadStack), 12); + osCreateThread(&sIdleThread, Z_THREAD_ID_IDLE, Idle_ThreadEntry, NULL, sIdleThreadStack + sizeof(sIdleThreadStack), Z_PRIORITY_IDLE); osStartThread(&sIdleThread); } diff --git a/src/boot_O2_g3/idle.c b/src/boot_O2_g3/idle.c index 3da1cb5f7..e4acb7b55 100644 --- a/src/boot_O2_g3/idle.c +++ b/src/boot_O2_g3/idle.c @@ -67,7 +67,7 @@ void Idle_InitCodeAndMemory(void) { void Main_ThreadEntry(void* arg) { StackCheck_Init(&sIrqMgrStackInfo, sIrqMgrStack, sIrqMgrStack + sizeof(sIrqMgrStack), 0, 256, "irqmgr"); - IrqMgr_Init(&gIrqMgr, &sIrqMgrStackInfo, 18, 1); + IrqMgr_Init(&gIrqMgr, &sIrqMgrStackInfo, Z_PRIORITY_IRQMGR, 1); DmaMgr_Start(); Idle_InitCodeAndMemory(); main(arg); @@ -104,7 +104,7 @@ void Idle_ThreadEntry(void* arg) { Idle_InitVideo(); osCreatePiManager(150, &gPiMgrCmdQ, sPiMgrCmdBuff, ARRAY_COUNT(sPiMgrCmdBuff)); StackCheck_Init(&sMainStackInfo, sMainStack, sMainStack + sizeof(sMainStack), 0, 1024, "main"); - osCreateThread(&gMainThread, 3, Main_ThreadEntry, arg, sMainStack + sizeof(sMainStack), 12); + osCreateThread(&gMainThread, Z_THREAD_ID_MAIN, Main_ThreadEntry, arg, sMainStack + sizeof(sMainStack), Z_PRIORITY_MAIN); osStartThread(&gMainThread); osSetThreadPri(NULL, 0); diff --git a/src/boot_O2_g3/irqmgr.c b/src/boot_O2_g3/irqmgr.c index cc3c917f2..143a8a0f2 100644 --- a/src/boot_O2_g3/irqmgr.c +++ b/src/boot_O2_g3/irqmgr.c @@ -167,6 +167,6 @@ void IrqMgr_Init(IrqMgr* irqmgr, void* stack, OSPri pri, u8 retraceCount) { osSetEventMesg(0xE, &irqmgr->irqQueue, (OSMesg)0x29D); osViSetEvent(&irqmgr->irqQueue, (OSMesg)0x29A, retraceCount); - osCreateThread(&irqmgr->thread, 0x13, (osCreateThread_func)IrqMgr_ThreadEntry, irqmgr, stack, pri); + osCreateThread(&irqmgr->thread, Z_THREAD_ID_IRQMGR, (osCreateThread_func)IrqMgr_ThreadEntry, irqmgr, stack, pri); osStartThread(&irqmgr->thread); } diff --git a/src/boot_O2_g3/z_std_dma.c b/src/boot_O2_g3/z_std_dma.c index e7c153a49..f293eaf69 100644 --- a/src/boot_O2_g3/z_std_dma.c +++ b/src/boot_O2_g3/z_std_dma.c @@ -108,7 +108,7 @@ void DmaMgr_ProcessMsg(DmaRequest* req) { size = req->size; index = DmaMgr_FindDmaIndex(vrom); - + if ((index >= 0) && (index < numDmaEntries)) { dmaEntry = &dmadata[index]; if (dmaEntry->romEnd == 0) { @@ -144,18 +144,18 @@ void DmaMgr_ThreadEntry(void* a0) { while (1) { osRecvMesg(&sDmaMgrMsgQueue, &msg, OS_MESG_BLOCK); - + if (msg == NULL) { break; } - + req = (DmaRequest *)msg; DmaMgr_ProcessMsg(req); if (req->notifyQueue) { osSendMesg(req->notifyQueue, req->notifyMsg, OS_MESG_NOBLOCK); } - + } } @@ -211,7 +211,7 @@ void DmaMgr_Start() { osCreateMesgQueue(&sDmaMgrMsgQueue, sDmaMgrMsgs, ARRAY_COUNT(sDmaMgrMsgs)); StackCheck_Init(&sDmaMgrStackInfo, sDmaMgrStack, sDmaMgrStack + sizeof(sDmaMgrStack), 0, 256, dmamgrThreadName); - osCreateThread(&sDmaMgrThread, 18, DmaMgr_ThreadEntry, NULL, sDmaMgrStack + sizeof(sDmaMgrStack), 17); + osCreateThread(&sDmaMgrThread, Z_THREAD_ID_DMAMGR, DmaMgr_ThreadEntry, NULL, sDmaMgrStack + sizeof(sDmaMgrStack), Z_PRIORITY_DMAMGR); osStartThread(&sDmaMgrThread); } #else diff --git a/src/code/sys_slowly.c b/src/code/sys_slowly.c new file mode 100644 index 000000000..60baad68c --- /dev/null +++ b/src/code/sys_slowly.c @@ -0,0 +1,44 @@ +#include +#include + +#define SLOWLY_STATUS_DONE (1 << 0) +#define SLOWLY_STATUS_STARTED (1 << 1) + +void Slowly_Main(SlowlyTask* slowly) { + slowly->status |= SLOWLY_STATUS_STARTED; + + switch (slowly->callbackArgCount) { + case SLOWLY_CALLBACK_NO_ARGS: + slowly->callback0(); + break; + case SLOWLY_CALLBACK_ONE_ARG: + slowly->callback1(slowly->callbackArg0); + break; + case SLOWLY_CALLBACK_TWO_ARGS: + slowly->callback2(slowly->callbackArg0, slowly->callbackArg1); + break; + } + + slowly->status |= SLOWLY_STATUS_DONE; +} + +void Slowly_ThreadEntry(SlowlyTask* slowly) { + Slowly_Main(slowly); +} + +void Slowly_Start(SlowlyTask* slowly, void* stack, void (*callback)(), void* callbackArg0, void* callbackArg1) { + bzero(slowly, sizeof(SlowlyTask)); + + slowly->callbackArgCount = SLOWLY_CALLBACK_TWO_ARGS; + slowly->status = 0; + slowly->callback0 = callback; + slowly->callbackArg0 = callbackArg0; + slowly->callbackArg1 = callbackArg1; + + osCreateThread(&slowly->thread, Z_THREAD_ID_SLOWLY, Slowly_ThreadEntry, slowly, stack, Z_PRIORITY_SLOWLY); + osStartThread(&slowly->thread); +} + +void Slowly_Stop(SlowlyTask* slowly) { + osDestroyThread(&slowly->thread); +} diff --git a/tables/functions.txt b/tables/functions.txt index 59f301146..3cdda5fe9 100644 --- a/tables/functions.txt +++ b/tables/functions.txt @@ -3455,7 +3455,7 @@ 0x80185660:("Slowly_Main",), 0x801856FC:("Slowly_ThreadEntry",), 0x8018571C:("Slowly_Start",), - 0x801857A0:("func_801857A0",), + 0x801857A0:("Slowly_Stop",), 0x801857C0:("func_801857C0",), 0x801857D0:("func_801857D0",), 0x80185864:("func_80185864",), diff --git a/tables/objects.txt b/tables/objects.txt index 708fa806c..9f4e7668b 100644 --- a/tables/objects.txt +++ b/tables/objects.txt @@ -369,6 +369,7 @@ 0x80182CE0:"", 0x80183070:"", 0x801830A0:"", + 0x80185660:"sys_slowly", 0x801857C0:"sys_flashrom", 0x80185F90:"", 0x801877D0:"", diff --git a/tables/variables.txt b/tables/variables.txt index c94cf8dab..2d993b479 100644 --- a/tables/variables.txt +++ b/tables/variables.txt @@ -4276,7 +4276,7 @@ 0x801F6D50:("D_801F6D50","UNK_TYPE1","",0x1), 0x801F6DFC:("D_801F6DFC","u8","",0x1), 0x801F6DFD:("D_801F6DFD","UNK_TYPE1","",0x1), - 0x801F6E00:("D_801F6E00","s8018571C","",0x1c0), + 0x801F6E00:("D_801F6E00","SlowlyTask","",0x1c0), 0x801F6FC0:("D_801F6FC0","UNK_TYPE1","",0x1), 0x801F6FC8:("slowlyStackEntry","StackEntry","",0x1c), 0x801F6FE8:("slowlyStack","u8","[4096]",0x1000),