From 03c8d46ed26a7a67c4f97efa22e5bc50542af493 Mon Sep 17 00:00:00 2001 From: Derek Hensley Date: Sun, 19 Mar 2023 06:48:49 -0700 Subject: [PATCH] Slowly Cleanup (#1213) * Add slowly.h * Init and destroy * File header --- include/functions.h | 5 +--- include/slowly.h | 31 ++++++++++++++++++++ include/z64.h | 19 ------------ src/code/PreRender.c | 9 +++--- src/code/sys_slowly.c | 53 ++++++++++++++++++++++------------ tools/disasm/functions.txt | 4 +-- tools/disasm/variables.txt | 2 +- tools/sizes/code_functions.csv | 4 +-- 8 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 include/slowly.h diff --git a/include/functions.h b/include/functions.h index e5f0ccca61..fca5af0368 100644 --- a/include/functions.h +++ b/include/functions.h @@ -2684,10 +2684,7 @@ void func_8018450C(PlayState* play, SkeletonInfo* skeleton, Mtx* mtx, OverrideKe // 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(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/slowly.h b/include/slowly.h new file mode 100644 index 0000000000..b91d652d99 --- /dev/null +++ b/include/slowly.h @@ -0,0 +1,31 @@ +#ifndef SLOWLY_H +#define SLOWLY_H + +#include "ultra64.h" + +#define SLOWLY_STATUS_DONE (1 << 0) +#define SLOWLY_STATUS_STARTED (1 << 1) + +typedef void (*SlowlyCallbackZero)(void); +typedef void (*SlowlyCallbackOne)(void*); +typedef void (*SlowlyCallbackTwo)(void*, void*); + +typedef union { + SlowlyCallbackZero zero; + SlowlyCallbackOne one; + SlowlyCallbackTwo two; +} SlowlyCallback; // size = 0x4 + +typedef struct { + /* 0x000 */ OSThread thread; + /* 0x1B0 */ u8 argCount; + /* 0x1B1 */ u8 status; + /* 0x1B4 */ SlowlyCallback callback; + /* 0x1B8 */ void* arg0; + /* 0x1BC */ void* arg1; +} SlowlyMgr; // size = 0x1C0 + +void Slowly_Init(SlowlyMgr* slowly, void* stack, SlowlyCallbackTwo callback, void* arg0, void* arg1); +void Slowly_Destroy(SlowlyMgr* slowly); + +#endif diff --git a/include/z64.h b/include/z64.h index 17271501ff..cd9dc71cf3 100644 --- a/include/z64.h +++ b/include/z64.h @@ -219,25 +219,6 @@ typedef struct { /* 0x10 */ OSTime resetTime; } NmiBuff; // size >= 0x18 -typedef enum { - SLOWLY_CALLBACK_NO_ARGS, - SLOWLY_CALLBACK_ONE_ARG, - SLOWLY_CALLBACK_TWO_ARGS -} SlowlyCallbackArgCount; - -typedef struct { - /* 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; /* 0x04 */ void* unk4; diff --git a/src/code/PreRender.c b/src/code/PreRender.c index 247325251c..16ae999c50 100644 --- a/src/code/PreRender.c +++ b/src/code/PreRender.c @@ -1,4 +1,5 @@ #include "global.h" +#include "slowly.h" #include "stack.h" /** @@ -411,7 +412,7 @@ void PreRender_ApplyFilters(PreRender* this) { } } -extern SlowlyTask D_801F6E00; +extern SlowlyMgr sSlowlyMgr; extern s32 D_801F6FC0; extern StackEntry sSlowlyStackInfo; extern STACK(sSlowlyStack, 0x1000); @@ -423,12 +424,12 @@ void PreRender_ApplyFiltersSlowlyInit(PreRender* this) { if ((this->cvgSave != NULL) && (this->fbufSave != NULL)) { if (D_801F6FC0) { StackCheck_Cleanup(&sSlowlyStackInfo); - Slowly_Stop(&D_801F6E00); + Slowly_Destroy(&sSlowlyMgr); } this->unk_4D = 1; StackCheck_Init(&sSlowlyStackInfo, sSlowlyStack, STACK_TOP(sSlowlyStack), 0, 0x100, "slowly"); - Slowly_Start(&D_801F6E00, STACK_TOP(sSlowlyStack), PreRender_ApplyFilters, this, NULL); + Slowly_Init(&sSlowlyMgr, STACK_TOP(sSlowlyStack), (void*)PreRender_ApplyFilters, this, NULL); D_801F6FC0 = true; } } @@ -439,7 +440,7 @@ void PreRender_ApplyFiltersSlowlyInit(PreRender* this) { void PreRender_ApplyFiltersSlowlyDestroy(PreRender* this) { if (D_801F6FC0) { StackCheck_Cleanup(&sSlowlyStackInfo); - Slowly_Stop(&D_801F6E00); + Slowly_Destroy(&sSlowlyMgr); D_801F6FC0 = false; } } diff --git a/src/code/sys_slowly.c b/src/code/sys_slowly.c index 415d5f297e..29ac0db786 100644 --- a/src/code/sys_slowly.c +++ b/src/code/sys_slowly.c @@ -1,43 +1,58 @@ +/** + * @file sys_slowly.c + * + * This file implements a manager for running an asynchronous task on a thread with the lowest priority. + * + * The task callback is expected to have up to 2 void* arguments and have a void return. Setting `argCount` will adjust + * how many args the callback gets called with, but defaults to 2 and using the 2 argument callback. + * + * @note: `argCount` must be set manually, as this file implements no way to configure it. + */ +#include "slowly.h" #include "global.h" -#define SLOWLY_STATUS_DONE (1 << 0) -#define SLOWLY_STATUS_STARTED (1 << 1) - -void Slowly_Main(SlowlyTask* slowly) { +void Slowly_Main(SlowlyMgr* slowly) { slowly->status |= SLOWLY_STATUS_STARTED; - switch (slowly->callbackArgCount) { - case SLOWLY_CALLBACK_NO_ARGS: - slowly->callback0(); + switch (slowly->argCount) { + case 0: + slowly->callback.zero(); break; - case SLOWLY_CALLBACK_ONE_ARG: - slowly->callback1(slowly->callbackArg0); + + case 1: + slowly->callback.one(slowly->arg0); break; - case SLOWLY_CALLBACK_TWO_ARGS: - slowly->callback2(slowly->callbackArg0, slowly->callbackArg1); + + case 2: + slowly->callback.two(slowly->arg0, slowly->arg1); + break; + + default: break; } slowly->status |= SLOWLY_STATUS_DONE; } -void Slowly_ThreadEntry(SlowlyTask* slowly) { +void Slowly_ThreadEntry(void* arg) { + SlowlyMgr* slowly = (SlowlyMgr*)arg; + Slowly_Main(slowly); } -void Slowly_Start(SlowlyTask* slowly, void* stack, void (*callback)(), void* callbackArg0, void* callbackArg1) { - bzero(slowly, sizeof(SlowlyTask)); +void Slowly_Init(SlowlyMgr* slowly, void* stack, SlowlyCallbackTwo callback, void* arg0, void* arg1) { + bzero(slowly, sizeof(SlowlyMgr)); - slowly->callbackArgCount = SLOWLY_CALLBACK_TWO_ARGS; + slowly->argCount = 2; slowly->status = 0; - slowly->callback0 = callback; - slowly->callbackArg0 = callbackArg0; - slowly->callbackArg1 = callbackArg1; + slowly->callback.two = callback; + slowly->arg0 = arg0; + slowly->arg1 = arg1; osCreateThread(&slowly->thread, Z_THREAD_ID_SLOWLY, Slowly_ThreadEntry, slowly, stack, Z_PRIORITY_SLOWLY); osStartThread(&slowly->thread); } -void Slowly_Stop(SlowlyTask* slowly) { +void Slowly_Destroy(SlowlyMgr* slowly) { osDestroyThread(&slowly->thread); } diff --git a/tools/disasm/functions.txt b/tools/disasm/functions.txt index 235debbb50..ae62d6f364 100644 --- a/tools/disasm/functions.txt +++ b/tools/disasm/functions.txt @@ -3445,8 +3445,8 @@ 0x80185460:("func_80185460",), 0x80185660:("Slowly_Main",), 0x801856FC:("Slowly_ThreadEntry",), - 0x8018571C:("Slowly_Start",), - 0x801857A0:("Slowly_Stop",), + 0x8018571C:("Slowly_Init",), + 0x801857A0:("Slowly_Destroy",), 0x801857C0:("func_801857C0",), 0x801857D0:("func_801857D0",), 0x80185864:("func_80185864",), diff --git a/tools/disasm/variables.txt b/tools/disasm/variables.txt index c78f0a3a84..de6991bd59 100644 --- a/tools/disasm/variables.txt +++ b/tools/disasm/variables.txt @@ -4061,7 +4061,7 @@ 0x801F6D50:("sBombersNotebook","UNK_TYPE1","",0x1), 0x801F6DFC:("sBombersNotebookOpen","u8","",0x1), 0x801F6DFD:("sMotionBlurStatus","UNK_TYPE1","",0x1), - 0x801F6E00:("D_801F6E00","SlowlyTask","",0x1c0), + 0x801F6E00:("sSlowlyMgr","SlowlyMgr","",0x1c0), 0x801F6FC0:("D_801F6FC0","UNK_TYPE1","",0x1), 0x801F6FC8:("sSlowlyStackInfo","StackEntry","",0x1c), 0x801F6FE8:("sSlowlyStack","u8","[4096]",0x1000), diff --git a/tools/sizes/code_functions.csv b/tools/sizes/code_functions.csv index 0f8f70b8ab..3dd0ba7085 100644 --- a/tools/sizes/code_functions.csv +++ b/tools/sizes/code_functions.csv @@ -2959,8 +2959,8 @@ asm/non_matchings/code/c_keyframe/func_801853C8.s,func_801853C8,0x801853C8,0x26 asm/non_matchings/code/c_keyframe/func_80185460.s,func_80185460,0x80185460,0x80 asm/non_matchings/code/sys_slowly/Slowly_Main.s,Slowly_Main,0x80185660,0x27 asm/non_matchings/code/sys_slowly/Slowly_ThreadEntry.s,Slowly_ThreadEntry,0x801856FC,0x8 -asm/non_matchings/code/sys_slowly/Slowly_Start.s,Slowly_Start,0x8018571C,0x21 -asm/non_matchings/code/sys_slowly/Slowly_Stop.s,Slowly_Stop,0x801857A0,0x8 +asm/non_matchings/code/sys_slowly/Slowly_Init.s,Slowly_Init,0x8018571C,0x21 +asm/non_matchings/code/sys_slowly/Slowly_Destroy.s,Slowly_Destroy,0x801857A0,0x8 asm/non_matchings/code/sys_flashrom/func_801857C0.s,func_801857C0,0x801857C0,0x4 asm/non_matchings/code/sys_flashrom/func_801857D0.s,func_801857D0,0x801857D0,0x25 asm/non_matchings/code/sys_flashrom/func_80185864.s,func_80185864,0x80185864,0x29