From 6dd16009361b2561f77b7933981611a9b975fa21 Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Sun, 26 Nov 2023 22:01:42 -0300 Subject: [PATCH] Organize `libc64` files (#1492) * Move qrand to libc64 * use an union to avoid type punning * __osMalloc * math64.c * fixed_point.h * sleep * aprintf.h * sprintf * malloc * use original names on aprintf.c and malloc.c * qrand cleanup pass * use original names of sleep.c * og names for sprintf * more cleanup * format * fixes * whoops * use ARRAY_COUNT again * comment * Use `fu` * forgot this one * review * fix * sneak a tiny cleanup --- Makefile | 2 + include/PR/os_libc.h | 2 - include/functions.h | 8 --- include/libc/math.h | 2 - include/libc64/aprintf.h | 9 +++ include/{ => libc64}/fixed_point.h | 1 + include/libc64/malloc.h | 20 +++++++ include/libc64/math64.h | 20 +++++++ include/{ => libc64}/os_malloc.h | 9 ++- include/{rand.h => libc64/qrand.h} | 8 +-- include/libc64/sleep.h | 12 ++++ include/libc64/sprintf.h | 9 +++ include/system_malloc.h | 20 ------- include/variables.h | 1 - include/z64.h | 2 +- include/z64math.h | 16 +---- spec | 16 ++--- src/boot/O2/gfxprint.c | 3 +- src/boot/O2/loadfragment.c | 6 +- src/boot/O2/loadfragment2.c | 4 +- src/boot/O2/printutils.c | 17 ------ src/boot/O2/sleep.c | 27 --------- src/boot/O2/system_heap.c | 10 ++-- src/boot/O2/system_malloc.c | 51 ---------------- src/boot/fault.c | 9 ++- src/boot/{O2 => libc64}/__osMalloc.c | 8 ++- src/boot/libc64/aprintf.c | 17 ++++++ src/boot/libc64/malloc.c | 50 ++++++++++++++++ src/boot/{O2 => libc64}/math64.c | 18 +++--- src/boot/{O2/rand.c => libc64/qrand.c} | 60 ++++++++++++++----- src/boot/libc64/sleep.c | 28 +++++++++ src/boot/{O2 => libc64}/sprintf.c | 13 ++-- src/boot/yaz0.c | 4 +- src/code/code_80183070.c | 3 +- src/code/game.c | 4 +- src/code/gamealloc.c | 8 +-- src/code/graph.c | 11 ++-- src/code/listalloc.c | 6 +- src/code/padmgr.c | 1 + src/code/sched.c | 5 +- src/code/speed_meter.c | 4 +- src/code/sys_cfb.c | 2 +- src/code/sys_cmpdma.c | 6 +- src/code/sys_flashrom.c | 6 +- src/code/z_DLF.c | 4 +- src/code/z_bgcheck.c | 3 +- src/code/z_debug.c | 4 +- src/code/z_fbdemo.c | 31 +++++----- src/code/z_kankyo.c | 14 ++--- src/code/z_malloc.c | 2 +- src/code/z_vismono.c | 8 +-- src/libultra/gu/sins.c | 1 + src/overlays/actors/ovl_Eff_Dust/z_eff_dust.c | 4 +- tools/disasm/files.txt | 10 ++-- tools/disasm/functions.txt | 36 +++++------ tools/disasm/variables.txt | 2 +- tools/namefixer.py | 22 +++++++ tools/sizes/boot_functions.csv | 38 ++++++------ 58 files changed, 406 insertions(+), 311 deletions(-) create mode 100644 include/libc64/aprintf.h rename include/{ => libc64}/fixed_point.h (99%) create mode 100644 include/libc64/malloc.h create mode 100644 include/libc64/math64.h rename include/{ => libc64}/os_malloc.h (89%) rename include/{rand.h => libc64/qrand.h} (87%) create mode 100644 include/libc64/sleep.h create mode 100644 include/libc64/sprintf.h delete mode 100644 include/system_malloc.h delete mode 100644 src/boot/O2/printutils.c delete mode 100644 src/boot/O2/sleep.c delete mode 100644 src/boot/O2/system_malloc.c rename src/boot/{O2 => libc64}/__osMalloc.c (99%) create mode 100644 src/boot/libc64/aprintf.c create mode 100644 src/boot/libc64/malloc.c rename src/boot/{O2 => libc64}/math64.c (93%) rename src/boot/{O2/rand.c => libc64/qrand.c} (62%) create mode 100644 src/boot/libc64/sleep.c rename src/boot/{O2 => libc64}/sprintf.c (57%) diff --git a/Makefile b/Makefile index a3d325a38e..8b9d461d28 100644 --- a/Makefile +++ b/Makefile @@ -207,6 +207,8 @@ $(shell mkdir -p build/baserom $(foreach dir,$(SRC_DIRS) $(ASM_DIRS) $(ASSET_BIN # directory flags build/src/boot/O2/%.o: OPTFLAGS := -O2 +build/src/boot/libc64/%.o: OPTFLAGS := -O2 + build/src/libultra/os/%.o: OPTFLAGS := -O1 build/src/libultra/voice/%.o: OPTFLAGS := -O2 build/src/libultra/io/%.o: OPTFLAGS := -O2 diff --git a/include/PR/os_libc.h b/include/PR/os_libc.h index ffbf879936..d53d871bbb 100644 --- a/include/PR/os_libc.h +++ b/include/PR/os_libc.h @@ -8,8 +8,6 @@ void bcopy(void* __src, void* __dest, int __n); int bcmp(void* __s1, void* __s2, int __n); void bzero(void* begin, int length); -int vsprintf(char* dst, char* fmt, va_list args); -int sprintf(char* dst, const char* fmt, ...); void osSyncPrintf(const char* fmt, ...); #endif diff --git a/include/functions.h b/include/functions.h index 8a63226edb..44fb1b6153 100644 --- a/include/functions.h +++ b/include/functions.h @@ -65,14 +65,6 @@ void MtxConv_L2F(MtxF* mtx, Mtx* mf); s32 func_80086620(OSMesgQueue* param_1, PadMgr* param_2, OSContStatus* param_3); -s32 PrintUtils_VPrintf(PrintCallback* pfn, const char* fmt, va_list args); -s32 PrintUtils_Printf(PrintCallback* pfn, const char* fmt, ...); -void Sleep_Cycles(OSTime time); -void Sleep_Nsec(u32 nsec); -void Sleep_Usec(u32 usec); -void Sleep_Msec(u32 ms); -void Sleep_Sec(u32 sec); - f32 fmodf(f32 dividend, f32 divisor); void* __osMemset(void* ptr, s32 val, size_t size); s32 __osStrcmp(const char* str1, const char* str2); diff --git a/include/libc/math.h b/include/libc/math.h index fd32cf1995..6e904dca83 100644 --- a/include/libc/math.h +++ b/include/libc/math.h @@ -1,8 +1,6 @@ #ifndef LIBC_MATH_H #define LIBC_MATH_H -#include "PR/ultratypes.h" - #define M_PI 3.14159265358979323846f #define M_SQRT2 1.41421356237309504880f #define M_SQRT1_2 0.70710678118654752440f /* 1/sqrt(2) */ diff --git a/include/libc64/aprintf.h b/include/libc64/aprintf.h new file mode 100644 index 0000000000..f7b9dcdee8 --- /dev/null +++ b/include/libc64/aprintf.h @@ -0,0 +1,9 @@ +#ifndef LIBC64_APRINTF_H +#define LIBC64_APRINTF_H + +#include "ultra64.h" + +int vaprintf(PrintCallback* pfn, const char* fmt, va_list args); +int aprintf(PrintCallback* pfn, const char* fmt, ...); + +#endif diff --git a/include/fixed_point.h b/include/libc64/fixed_point.h similarity index 99% rename from include/fixed_point.h rename to include/libc64/fixed_point.h index bb7e964946..41362abbf2 100644 --- a/include/fixed_point.h +++ b/include/libc64/fixed_point.h @@ -1,5 +1,6 @@ #ifndef FIXED_POINT_H #define FIXED_POINT_H + #include "ultra64.h" extern f32 qNaN0x3FFFFF; diff --git a/include/libc64/malloc.h b/include/libc64/malloc.h new file mode 100644 index 0000000000..6ad3dd993c --- /dev/null +++ b/include/libc64/malloc.h @@ -0,0 +1,20 @@ +#ifndef LIBC64_MALLOC_H +#define LIBC64_MALLOC_H + +#include "ultra64.h" +#include "libc64/os_malloc.h" + +void* malloc(size_t size); +void* malloc_r(size_t size); +void* realloc(void* oldPtr, size_t newSize); +void free(void* ptr); +void* calloc(size_t num, size_t size); +void GetFreeArena(size_t* maxFreeBlock, size_t* bytesFree, size_t* bytesAllocated); +s32 CheckArena(void); +void MallocInit(void* start, size_t size); +void MallocCleanup(void); +s32 MallocIsInitialized(void); + +extern Arena malloc_arena; + +#endif diff --git a/include/libc64/math64.h b/include/libc64/math64.h new file mode 100644 index 0000000000..16795a0008 --- /dev/null +++ b/include/libc64/math64.h @@ -0,0 +1,20 @@ +#ifndef LIBC64_MATH64_H +#define LIBC64_MATH64_H + +#include "ultra64.h" + +f32 Math_FTanF(f32 x); +f32 Math_FFloorF(f32 x); +f32 Math_FCeilF(f32 x); +f32 Math_FRoundF(f32 x); +f32 Math_FTruncF(f32 x); +f32 Math_FNearbyIntF(f32 x); +f32 Math_FAtanTaylorQF(f32 x); +f32 Math_FAtanTaylorF(f32 x); +f32 Math_FAtanContFracF(f32 x); +f32 Math_FAtanF(f32 x); +f32 Math_FAtan2F(f32 y, f32 x); +f32 Math_FAsinF(f32 x); +f32 Math_FAcosF(f32 x); + +#endif diff --git a/include/os_malloc.h b/include/libc64/os_malloc.h similarity index 89% rename from include/os_malloc.h rename to include/libc64/os_malloc.h index 4106d42e80..01711bd7fe 100644 --- a/include/os_malloc.h +++ b/include/libc64/os_malloc.h @@ -1,8 +1,7 @@ -#ifndef OS_MALLOC_H -#define OS_MALLOC_H +#ifndef LIBC64_OS_MALLOC_H +#define LIBC64_OS_MALLOC_H -#include "PR/ultratypes.h" -#include "PR/os_message.h" +#include "ultra64.h" #include "libc/stddef.h" typedef struct ArenaNode { @@ -13,7 +12,7 @@ typedef struct ArenaNode { /* 0xC */ struct ArenaNode* prev; } ArenaNode; // size = 0x10 -typedef struct { +typedef struct Arena { /* 0x00 */ ArenaNode* head; /* 0x04 */ void* start; /* 0x08 */ OSMesgQueue lock; diff --git a/include/rand.h b/include/libc64/qrand.h similarity index 87% rename from include/rand.h rename to include/libc64/qrand.h index ad13e22a44..0af96c144c 100644 --- a/include/rand.h +++ b/include/libc64/qrand.h @@ -1,7 +1,7 @@ -#ifndef RAND_H -#define RAND_H +#ifndef LIBC64_QRAND_H +#define LIBC64_QRAND_H -#include "PR/ultratypes.h" +#include "ultra64.h" //! These values are recommended by the algorithms book *Numerical Recipes in C. The Art of Scientific Computing*, 2nd //! Edition, 1992, ISBN 0-521-43108-5. (p. 284): @@ -18,6 +18,6 @@ u32 Rand_Next_Variable(u32* rndNum); f32 Rand_ZeroOne_Variable(u32* rndNum); f32 Rand_Centered_Variable(u32* rndNum); -extern u32 gRandFloat; +extern fu gRandFloat; #endif diff --git a/include/libc64/sleep.h b/include/libc64/sleep.h new file mode 100644 index 0000000000..0dca485be2 --- /dev/null +++ b/include/libc64/sleep.h @@ -0,0 +1,12 @@ +#ifndef LIBC64_SLEEP_H +#define LIBC64_SLEEP_H + +#include "ultra64.h" + +void csleep(OSTime time); +void nsleep(u32 nsec); +void usleep(u32 usec); +void msleep(u32 msec); +void sleep(u32 sec); + +#endif diff --git a/include/libc64/sprintf.h b/include/libc64/sprintf.h new file mode 100644 index 0000000000..c6d7f03d0d --- /dev/null +++ b/include/libc64/sprintf.h @@ -0,0 +1,9 @@ +#ifndef LIBC64_SPRINTF_H +#define LIBC64_SPRINTF_H + +#include "ultra64.h" + +int vsprintf(char* dst, const char* fmt, va_list args); +int sprintf(char* dst, const char* fmt, ...); + +#endif diff --git a/include/system_malloc.h b/include/system_malloc.h deleted file mode 100644 index 6c6f3456ad..0000000000 --- a/include/system_malloc.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef SYSTEM_MALLOC_H -#define SYSTEM_MALLOC_H - -#include "PR/ultratypes.h" -#include "os_malloc.h" - -void* SystemArena_Malloc(size_t size); -void* SystemArena_MallocR(size_t size); -void* SystemArena_Realloc(void* oldPtr, size_t newSize); -void SystemArena_Free(void* ptr); -void* SystemArena_Calloc(size_t num, size_t size); -void SystemArena_GetSizes(size_t* maxFreeBlock, size_t* bytesFree, size_t* bytesAllocated); -u32 SystemArena_CheckArena(void); -void SystemArena_Init(void* start, size_t size); -void SystemArena_Cleanup(void); -u8 SystemArena_IsInitialized(void); - -extern Arena gSystemArena; - -#endif diff --git a/include/variables.h b/include/variables.h index dce2df6632..51cabcc966 100644 --- a/include/variables.h +++ b/include/variables.h @@ -17,7 +17,6 @@ extern s32 sIrqMgrRetraceCount; // extern UNK_TYPE1 sGfxPrintRainbowData; // extern UNK_TYPE1 sGfxPrintFontData; // extern UNK_TYPE4 D_80097524; -// extern u32 sRandInt; extern u8 sYaz0DataBuffer[0x400]; extern u8* sYaz0CurDataEnd; diff --git a/include/z64.h b/include/z64.h index 5ef67b9e86..8eb6e9beb0 100644 --- a/include/z64.h +++ b/include/z64.h @@ -26,7 +26,7 @@ #include "gfx.h" #include "gfxprint.h" #include "padutils.h" -#include "rand.h" +#include "libc64/qrand.h" #include "sys_matrix.h" #include "tha.h" #include "thga.h" diff --git a/include/z64math.h b/include/z64math.h index 27ab62dca4..725450397e 100644 --- a/include/z64math.h +++ b/include/z64math.h @@ -4,6 +4,8 @@ #include "ultra64.h" #include "libc/math.h" +#include "libc64/math64.h" + #define VEC_SET(V,X,Y,Z) V.x=X;V.y=Y;V.z=Z typedef struct { @@ -240,20 +242,6 @@ f32 Math_CosF(f32 rad); f32 Rand_ZeroFloat(f32 scale); f32 Rand_CenteredFloat(f32 scale); -f32 Math_FTanF(f32 x); -f32 Math_FFloorF(f32 x); -f32 Math_FCeilF(f32 x); -f32 Math_FRoundF(f32 x); -f32 Math_FTruncF(f32 x); -f32 Math_FNearbyIntF(f32 x); -f32 Math_FAtanTaylorQF(f32 x); -f32 Math_FAtanTaylorF(f32 x); -f32 Math_FAtanContFracF(f32 x); -f32 Math_FAtanF(f32 x); -f32 Math_FAtan2F(f32 y, f32 x); -f32 Math_FAsinF(f32 x); -f32 Math_FAcosF(f32 x); - s16 Math_Atan2S(f32 y, f32 x); f32 Math_Atan2F(f32 y, f32 x); s16 Math_Atan2S_XY(f32 x, f32 y); diff --git a/spec b/spec index c36ef9edf2..dbe71ed3bf 100644 --- a/spec +++ b/spec @@ -41,15 +41,15 @@ beginseg include "build/src/boot/O2/debug.o" include "build/src/boot/O2/system_heap.o" include "build/src/boot/O2/padsetup.o" - include "build/src/boot/O2/math64.o" - include "build/asm/boot/fp.text.o" + include "build/src/boot/libc64/math64.o" + include "build/asm/boot/fp.text.o" // Part of libc64 include "build/data/boot/fp.data.o" - include "build/src/boot/O2/system_malloc.o" - include "build/src/boot/O2/rand.o" - include "build/src/boot/O2/__osMalloc.o" - include "build/src/boot/O2/sprintf.o" - include "build/src/boot/O2/printutils.o" - include "build/src/boot/O2/sleep.o" + include "build/src/boot/libc64/malloc.o" + include "build/src/boot/libc64/qrand.o" + include "build/src/boot/libc64/__osMalloc.o" + include "build/src/boot/libc64/sprintf.o" + include "build/src/boot/libc64/aprintf.o" + include "build/src/boot/libc64/sleep.o" include "build/asm/boot/setcause.text.o" include "build/src/libultra/os/sendmesg.o" include "build/src/libultra/io/pfsfreeblocks.o" diff --git a/src/boot/O2/gfxprint.c b/src/boot/O2/gfxprint.c index b526758294..368ef7d68c 100644 --- a/src/boot/O2/gfxprint.c +++ b/src/boot/O2/gfxprint.c @@ -1,4 +1,5 @@ #include "global.h" +#include "libc64/aprintf.h" #define GFXP_FLAG_HIRAGANA (1 << 0) #define GFXP_FLAG_RAINBOW (1 << 1) @@ -220,7 +221,7 @@ Gfx* GfxPrint_Close(GfxPrint* this) { } s32 GfxPrint_VPrintf(GfxPrint* this, const char* fmt, va_list args) { - return PrintUtils_VPrintf(&this->callback, fmt, args); + return vaprintf(&this->callback, fmt, args); } s32 GfxPrint_Printf(GfxPrint* this, const char* fmt, ...) { diff --git a/src/boot/O2/loadfragment.c b/src/boot/O2/loadfragment.c index 69fa9959c9..4da7a78b1e 100644 --- a/src/boot/O2/loadfragment.c +++ b/src/boot/O2/loadfragment.c @@ -11,7 +11,7 @@ */ #include "global.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "loadfragment.h" s32 gLoadLogSeverity = 2; @@ -186,7 +186,7 @@ void* Fragment_AllocateAndLoad(uintptr_t vromStart, uintptr_t vromEnd, void* vra if (gLoadLogSeverity >= 3) {} - allocatedRamAddr = SystemArena_MallocR(size); + allocatedRamAddr = malloc_r(size); end = (uintptr_t)allocatedRamAddr + size; if (gLoadLogSeverity >= 3) {} @@ -202,7 +202,7 @@ void* Fragment_AllocateAndLoad(uintptr_t vromStart, uintptr_t vromEnd, void* vra allocatedBytes = ovlRelocs->bssSize + size; - allocatedRamAddr = SystemArena_Realloc(allocatedRamAddr, allocatedBytes); + allocatedRamAddr = realloc(allocatedRamAddr, allocatedBytes); if (gLoadLogSeverity >= 3) {} diff --git a/src/boot/O2/loadfragment2.c b/src/boot/O2/loadfragment2.c index 02b3ab2189..44b64f532e 100644 --- a/src/boot/O2/loadfragment2.c +++ b/src/boot/O2/loadfragment2.c @@ -7,7 +7,7 @@ * These are for specific fragment overlays with the .ovl file extension */ #include "global.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "loadfragment.h" s32 gOverlayLogSeverity = 2; @@ -167,7 +167,7 @@ size_t Overlay_Load(uintptr_t vromStart, uintptr_t vromEnd, void* ramStart, void } void* Overlay_AllocateAndLoad(uintptr_t vromStart, uintptr_t vromEnd, void* vramStart, void* vramEnd) { - void* allocatedRamAddr = SystemArena_MallocR((uintptr_t)vramEnd - (uintptr_t)vramStart); + void* allocatedRamAddr = malloc_r((uintptr_t)vramEnd - (uintptr_t)vramStart); if (allocatedRamAddr != NULL) { Overlay_Load(vromStart, vromEnd, vramStart, vramEnd, allocatedRamAddr); diff --git a/src/boot/O2/printutils.c b/src/boot/O2/printutils.c deleted file mode 100644 index 3fb8cf3672..0000000000 --- a/src/boot/O2/printutils.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "global.h" - -s32 PrintUtils_VPrintf(PrintCallback* pfn, const char* fmt, va_list args) { - return _Printf(*pfn, pfn, fmt, args); -} - -s32 PrintUtils_Printf(PrintCallback* pfn, const char* fmt, ...) { - s32 ret; - va_list args; - va_start(args, fmt); - - ret = PrintUtils_VPrintf(pfn, fmt, args); - - va_end(args); - - return ret; -} diff --git a/src/boot/O2/sleep.c b/src/boot/O2/sleep.c deleted file mode 100644 index a35abc5b25..0000000000 --- a/src/boot/O2/sleep.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "global.h" - -void Sleep_Cycles(u64 time) { - OSMesgQueue mq; - OSMesg msg[1]; - OSTimer timer; - - osCreateMesgQueue(&mq, msg, ARRAY_COUNT(msg)); - osSetTimer(&timer, time, 0, &mq, NULL); - osRecvMesg(&mq, NULL, OS_MESG_BLOCK); -} - -void Sleep_Nsec(u32 nsec) { - Sleep_Cycles(OS_NSEC_TO_CYCLES(nsec)); -} - -void Sleep_Usec(u32 usec) { - Sleep_Cycles(OS_USEC_TO_CYCLES(usec)); -} - -void Sleep_Msec(u32 ms) { - Sleep_Cycles((ms * OS_CPU_COUNTER) / 1000ULL); -} - -void Sleep_Sec(u32 sec) { - Sleep_Cycles(sec * OS_CPU_COUNTER); -} diff --git a/src/boot/O2/system_heap.c b/src/boot/O2/system_heap.c index c34129f121..7ad21b6ae3 100644 --- a/src/boot/O2/system_heap.c +++ b/src/boot/O2/system_heap.c @@ -2,11 +2,11 @@ * @file system_heap.c * * @note: - * Only SystemHeap_Init() is used, and is essentially just a wrapper for SystemArena_Init(). + * Only SystemHeap_Init() is used, and is essentially just a wrapper for MallocInit(). * */ #include "global.h" -#include "system_malloc.h" +#include "libc64/malloc.h" typedef void (*BlockFunc)(uintptr_t); typedef void (*BlockFunc1)(uintptr_t, u32); @@ -31,12 +31,12 @@ void* SystemHeap_Malloc(size_t size) { size = 1; } - return __osMalloc(&gSystemArena, size); + return __osMalloc(&malloc_arena, size); } void SystemHeap_Free(void* ptr) { if (ptr != NULL) { - __osFree(&gSystemArena, ptr); + __osFree(&malloc_arena, ptr); } } @@ -118,6 +118,6 @@ void SystemHeap_RunInits(void) { } void SystemHeap_Init(void* start, size_t size) { - SystemArena_Init(start, size); + MallocInit(start, size); SystemHeap_RunInits(); } diff --git a/src/boot/O2/system_malloc.c b/src/boot/O2/system_malloc.c deleted file mode 100644 index e7dc064fc2..0000000000 --- a/src/boot/O2/system_malloc.c +++ /dev/null @@ -1,51 +0,0 @@ -#include "global.h" -#include "os_malloc.h" - -Arena gSystemArena; - -void* SystemArena_Malloc(size_t size) { - return __osMalloc(&gSystemArena, size); -} - -void* SystemArena_MallocR(size_t size) { - return __osMallocR(&gSystemArena, size); -} - -void* SystemArena_Realloc(void* oldPtr, size_t newSize) { - return __osRealloc(&gSystemArena, oldPtr, newSize); -} - -void SystemArena_Free(void* ptr) { - __osFree(&gSystemArena, ptr); -} - -void* SystemArena_Calloc(size_t num, size_t size) { - void* ptr; - size_t totalSize = num * size; - - ptr = __osMalloc(&gSystemArena, totalSize); - if (ptr != NULL) { - bzero(ptr, totalSize); - } - return ptr; -} - -void SystemArena_GetSizes(size_t* maxFreeBlock, size_t* bytesFree, size_t* bytesAllocated) { - __osGetSizes(&gSystemArena, maxFreeBlock, bytesFree, bytesAllocated); -} - -u32 SystemArena_CheckArena(void) { - return __osCheckArena(&gSystemArena); -} - -void SystemArena_Init(void* start, size_t size) { - __osMallocInit(&gSystemArena, start, size); -} - -void SystemArena_Cleanup(void) { - __osMallocCleanup(&gSystemArena); -} - -u8 SystemArena_IsInitialized(void) { - return __osMallocIsInitalized(&gSystemArena); -} diff --git a/src/boot/fault.c b/src/boot/fault.c index 49734191bc..dba4af49ad 100644 --- a/src/boot/fault.c +++ b/src/boot/fault.c @@ -42,8 +42,11 @@ #include "fault_internal.h" #include "fault.h" + #include "prevent_bss_reordering.h" #include "prevent_bss_reordering2.h" +#include "libc64/sprintf.h" +#include "libc64/sleep.h" #include "vt.h" #include "PR/osint.h" #include "stackcheck.h" @@ -81,10 +84,10 @@ const char* sFpuExceptions[] = { "Unimplemented operation", "Invalid operation", "Division by zero", "Overflow", "Underflow", "Inexact operation", }; -void Fault_SleepImpl(u32 duration) { - OSTime value = (duration * OS_CPU_COUNTER) / 1000ULL; +void Fault_SleepImpl(u32 msec) { + OSTime value = (msec * OS_CPU_COUNTER) / 1000ULL; - Sleep_Cycles(value); + csleep(value); } /** diff --git a/src/boot/O2/__osMalloc.c b/src/boot/libc64/__osMalloc.c similarity index 99% rename from src/boot/O2/__osMalloc.c rename to src/boot/libc64/__osMalloc.c index 71a5a5ba3c..2c736740bf 100644 --- a/src/boot/O2/__osMalloc.c +++ b/src/boot/libc64/__osMalloc.c @@ -1,8 +1,10 @@ -#include "os_malloc.h" +#include "libc64/os_malloc.h" + +#include "alignment.h" +#include "functions.h" // for __osMemcpy #include "libc/stdbool.h" #include "libc/stdint.h" -#include "macros.h" -#include "functions.h" +#include "macros.h" // for ARRAY_COUNT #define FILL_ALLOCBLOCK (1 << 0) #define FILL_FREEBLOCK (1 << 1) diff --git a/src/boot/libc64/aprintf.c b/src/boot/libc64/aprintf.c new file mode 100644 index 0000000000..5e69e97f87 --- /dev/null +++ b/src/boot/libc64/aprintf.c @@ -0,0 +1,17 @@ +#include "libc64/aprintf.h" + +int vaprintf(PrintCallback* pfn, const char* fmt, va_list args) { + return _Printf(*pfn, pfn, fmt, args); +} + +int aprintf(PrintCallback* pfn, const char* fmt, ...) { + int ret; + va_list args; + va_start(args, fmt); + + ret = vaprintf(pfn, fmt, args); + + va_end(args); + + return ret; +} diff --git a/src/boot/libc64/malloc.c b/src/boot/libc64/malloc.c new file mode 100644 index 0000000000..510a3d05b2 --- /dev/null +++ b/src/boot/libc64/malloc.c @@ -0,0 +1,50 @@ +#include "libc64/malloc.h" + +Arena malloc_arena; + +void* malloc(size_t size) { + return __osMalloc(&malloc_arena, size); +} + +void* malloc_r(size_t size) { + return __osMallocR(&malloc_arena, size); +} + +void* realloc(void* oldPtr, size_t newSize) { + return __osRealloc(&malloc_arena, oldPtr, newSize); +} + +void free(void* ptr) { + __osFree(&malloc_arena, ptr); +} + +void* calloc(size_t num, size_t size) { + void* ptr; + size_t totalSize = num * size; + + ptr = __osMalloc(&malloc_arena, totalSize); + if (ptr != NULL) { + bzero(ptr, totalSize); + } + return ptr; +} + +void GetFreeArena(size_t* maxFreeBlock, size_t* bytesFree, size_t* bytesAllocated) { + __osGetSizes(&malloc_arena, maxFreeBlock, bytesFree, bytesAllocated); +} + +s32 CheckArena(void) { + return __osCheckArena(&malloc_arena); +} + +void MallocInit(void* start, size_t size) { + __osMallocInit(&malloc_arena, start, size); +} + +void MallocCleanup(void) { + __osMallocCleanup(&malloc_arena); +} + +s32 MallocIsInitialized(void) { + return __osMallocIsInitalized(&malloc_arena); +} diff --git a/src/boot/O2/math64.c b/src/boot/libc64/math64.c similarity index 93% rename from src/boot/O2/math64.c rename to src/boot/libc64/math64.c index cb77bed507..48cc5e025d 100644 --- a/src/boot/O2/math64.c +++ b/src/boot/libc64/math64.c @@ -2,8 +2,12 @@ * MathF library * Contains tangent function, wrappers for a number of the handwritten functions in fp, and a suite of arctangents */ -#include "global.h" -#include "fixed_point.h" + +#include "libc64/math64.h" + +#include "libc64/fixed_point.h" +#include "libc/stdbool.h" +#include "libc/math.h" s32 gUseAtanContFrac; @@ -49,7 +53,7 @@ f32 Math_FAtanTaylorQF(f32 x) { }; f32 poly = x; - f32 sq = SQ(x); + f32 sq = x * x; f32 exp = x * sq; const f32* c = coeffs; f32 term; @@ -129,11 +133,11 @@ f32 Math_FAtanContFracF(f32 x) { } // Builds the continued fraction from the innermost fraction out - sq = SQ(x); + sq = x * x; conv = 0.0f; z = 8.0f; - for (i = 8; i != 0; i--) { - conv = SQ(z) * sq / (2.0f * z + 1.0f + conv); + for (i = 8; i > 0; i--) { + conv = (z * z) * sq / (2.0f * z + 1.0f + conv); z -= 1.0f; } conv = x / (1.0f + conv); @@ -183,7 +187,7 @@ f32 Math_FAtan2F(f32 y, f32 x) { } f32 Math_FAsinF(f32 x) { - return Math_FAtan2F(x, sqrtf(1.0f - SQ(x))); + return Math_FAtan2F(x, sqrtf(1.0f - (x * x))); } f32 Math_FAcosF(f32 x) { diff --git a/src/boot/O2/rand.c b/src/boot/libc64/qrand.c similarity index 62% rename from src/boot/O2/rand.c rename to src/boot/libc64/qrand.c index 152c8a0e7b..a8be652f3f 100644 --- a/src/boot/O2/rand.c +++ b/src/boot/libc64/qrand.c @@ -1,21 +1,34 @@ -#include "rand.h" +#include "libc64/qrand.h" -//! The latest generated random number, used to generate the next number in the sequence. -static u32 sRandInt = 1; +/** + * The latest generated random number, used to generate the next number in the sequence. + * + * Original name: __qrand_idum + */ +u32 sRandInt = 1; -//! Space to store a value to be re-interpreted as a float. -//! This can't be static because it is used in z_kankyo. -u32 gRandFloat; +/** + * Space to store a value to be re-interpreted as a float. + * + * Orignal name: __qrand_itemp + */ +fu gRandFloat; /** * Generates the next pseudo-random integer. + * + * Original name: qrand */ u32 Rand_Next(void) { - return sRandInt = (sRandInt * RAND_MULTIPLIER) + RAND_INCREMENT; + sRandInt = (sRandInt * RAND_MULTIPLIER) + RAND_INCREMENT; + + return sRandInt; } /** * Seeds the internal pseudo-random number generator with a provided starting value. + * + * Original name: sqrand */ void Rand_Seed(u32 seed) { sRandInt = seed; @@ -28,20 +41,24 @@ void Rand_Seed(u32 seed) { * subtracting 1.0f. * * @remark This is also recommended by Numerical Recipes, pp. 284-5. + * + * Original name: fqrand */ f32 Rand_ZeroOne(void) { sRandInt = (sRandInt * RAND_MULTIPLIER) + RAND_INCREMENT; - gRandFloat = ((sRandInt >> 9) | 0x3F800000); - return *((f32*)&gRandFloat) - 1.0f; + gRandFloat.i = ((sRandInt >> 9) | 0x3F800000); + return gRandFloat.f - 1.0f; } /** * Returns a pseudo-random float between -0.5f and 0.5f in the same way as Rand_ZeroOne(). + * + * Original name: fqrand2 */ f32 Rand_Centered(void) { sRandInt = (sRandInt * RAND_MULTIPLIER) + RAND_INCREMENT; - gRandFloat = ((sRandInt >> 9) | 0x3F800000); - return *((f32*)&gRandFloat) - 1.5f; + gRandFloat.i = ((sRandInt >> 9) | 0x3F800000); + return gRandFloat.f - 1.5f; } //! All functions below are unused variants of the above four, that use a provided random number variable instead of the @@ -51,6 +68,8 @@ f32 Rand_Centered(void) { * Seeds a provided pseudo-random number with a provided starting value. * * @see Rand_Seed + * + * Original name: sqrand_r */ void Rand_Seed_Variable(u32* rndNum, u32 seed) { *rndNum = seed; @@ -60,31 +79,40 @@ void Rand_Seed_Variable(u32* rndNum, u32 seed) { * Generates the next pseudo-random number from the provided rndNum. * * @see Rand_Next + * + * Original name: qrand_r */ u32 Rand_Next_Variable(u32* rndNum) { - return *rndNum = (*rndNum * RAND_MULTIPLIER) + RAND_INCREMENT; + u32 t = (*rndNum * RAND_MULTIPLIER) + RAND_INCREMENT; + + *rndNum = t; + return t; } /** * Generates the next pseudo-random float between 0.0f and 1.0f from the provided rndNum. * * @see Rand_ZeroOne + * + * Original name: fqrand_r */ f32 Rand_ZeroOne_Variable(u32* rndNum) { u32 next = (*rndNum * RAND_MULTIPLIER) + RAND_INCREMENT; - gRandFloat = ((*rndNum = next) >> 9) | 0x3F800000; - return *((f32*)&gRandFloat) - 1.0f; + gRandFloat.i = ((*rndNum = next) >> 9) | 0x3F800000; + return gRandFloat.f - 1.0f; } /** * Generates the next pseudo-random float between -0.5f and 0.5f from the provided rndNum. * * @see Rand_ZeroOne, Rand_Centered + * + * Original name: fqrand2_r */ f32 Rand_Centered_Variable(u32* rndNum) { u32 next = (*rndNum * RAND_MULTIPLIER) + RAND_INCREMENT; - gRandFloat = ((*rndNum = next) >> 9) | 0x3F800000; - return *((f32*)&gRandFloat) - 1.5f; + gRandFloat.i = ((*rndNum = next) >> 9) | 0x3F800000; + return gRandFloat.f - 1.5f; } diff --git a/src/boot/libc64/sleep.c b/src/boot/libc64/sleep.c new file mode 100644 index 0000000000..2451a9b792 --- /dev/null +++ b/src/boot/libc64/sleep.c @@ -0,0 +1,28 @@ +#include "libc64/sleep.h" +#include "macros.h" // for ARRAY_COUNT + +void csleep(OSTime time) { + OSMesgQueue mq; + OSMesg msg[1]; + OSTimer timer; + + osCreateMesgQueue(&mq, msg, ARRAY_COUNT(msg)); + osSetTimer(&timer, time, 0, &mq, NULL); + osRecvMesg(&mq, NULL, OS_MESG_BLOCK); +} + +void nsleep(u32 nsec) { + csleep(OS_NSEC_TO_CYCLES(nsec)); +} + +void usleep(u32 usec) { + csleep(OS_USEC_TO_CYCLES(usec)); +} + +void msleep(u32 msec) { + csleep((msec * OS_CPU_COUNTER) / 1000ULL); +} + +void sleep(u32 sec) { + csleep(sec * OS_CPU_COUNTER); +} diff --git a/src/boot/O2/sprintf.c b/src/boot/libc64/sprintf.c similarity index 57% rename from src/boot/O2/sprintf.c rename to src/boot/libc64/sprintf.c index 773b982fff..07bee8064d 100644 --- a/src/boot/O2/sprintf.c +++ b/src/boot/libc64/sprintf.c @@ -1,13 +1,14 @@ -#include "ultra64.h" -#include "libc/stdlib.h" +#include "libc64/sprintf.h" + #include "libc/string.h" -void* proutSprintf(void* dst, const char* fmt, size_t size) { +void* proutPrintf(void* dst, const char* fmt, size_t size) { return (void*)((uintptr_t)memcpy(dst, fmt, size) + size); } -int vsprintf(char* dst, char* fmt, va_list args) { - int ans = _Printf(proutSprintf, dst, fmt, args); +int vsprintf(char* dst, const char* fmt, va_list args) { + int ans = _Printf(proutPrintf, dst, fmt, args); + if (ans > -1) { dst[ans] = 0; } @@ -19,7 +20,7 @@ int sprintf(char* dst, const char* fmt, ...) { va_list args; va_start(args, fmt); - ans = _Printf(&proutSprintf, dst, fmt, args); + ans = _Printf(&proutPrintf, dst, fmt, args); if (ans > -1) { dst[ans] = 0; } diff --git a/src/boot/yaz0.c b/src/boot/yaz0.c index 54a3481f19..7d33f8309b 100644 --- a/src/boot/yaz0.c +++ b/src/boot/yaz0.c @@ -1,5 +1,7 @@ #include "global.h" #include "fault.h" +#include "libc64/sprintf.h" +#include "libc64/sleep.h" u8 sYaz0DataBuffer[0x400] ALIGNED(16); u8* sYaz0CurDataEnd; @@ -131,7 +133,7 @@ void Yaz0_Decompress(uintptr_t romStart, void* dst, size_t size) { if (sYaz0CurDataEnd != NULL) { while (sYaz0CurDataEnd != NULL) { - Sleep_Usec(10); + usleep(10); } } diff --git a/src/code/code_80183070.c b/src/code/code_80183070.c index 7b84aa2809..ce84e3b2ba 100644 --- a/src/code/code_80183070.c +++ b/src/code/code_80183070.c @@ -1,7 +1,8 @@ #include "global.h" +#include "libc64/sleep.h" void func_80183070(void) { for (;;) { - Sleep_Msec(1000); + msleep(1000); } } diff --git a/src/code/game.c b/src/code/game.c index 237783319c..0179062f45 100644 --- a/src/code/game.c +++ b/src/code/game.c @@ -2,7 +2,7 @@ #include "audiomgr.h" #include "idle.h" #include "sys_cfb.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "z64debug_text.h" #include "z64rumble.h" #include "z64speed_meter.h" @@ -182,7 +182,7 @@ void GameState_Realloc(GameState* gameState, size_t size) { THA_Destroy(&gameState->tha); GameAlloc_Free(alloc, heapStart); - SystemArena_GetSizes(&systemMaxFree, &bytesFree, &bytesAllocated); + GetFreeArena(&systemMaxFree, &bytesFree, &bytesAllocated); size = ((systemMaxFree - sizeof(ArenaNode)) < size) ? 0 : size; if (size == 0) { size = systemMaxFree - sizeof(ArenaNode); diff --git a/src/code/gamealloc.c b/src/code/gamealloc.c index a0d6f741f8..58894324b0 100644 --- a/src/code/gamealloc.c +++ b/src/code/gamealloc.c @@ -1,6 +1,6 @@ #include "gamealloc.h" -#include "system_malloc.h" +#include "libc64/malloc.h" void GameAlloc_Log(GameAlloc* this) { GameAllocEntry* iter = this->base.next; @@ -11,7 +11,7 @@ void GameAlloc_Log(GameAlloc* this) { } void* GameAlloc_Malloc(GameAlloc* this, size_t size) { - GameAllocEntry* ptr = SystemArena_Malloc(size + sizeof(GameAllocEntry)); + GameAllocEntry* ptr = malloc(size + sizeof(GameAllocEntry)); if (ptr != NULL) { ptr->size = size; @@ -34,7 +34,7 @@ void GameAlloc_Free(GameAlloc* this, void* data) { ptr->prev->next = ptr->next; ptr->next->prev = ptr->prev; this->head = this->base.prev; - SystemArena_Free(ptr); + free(ptr); } } @@ -45,7 +45,7 @@ void GameAlloc_Cleanup(GameAlloc* this) { while (&this->base != next) { cur = next; next = next->next; - SystemArena_Free(cur); + free(cur); } this->head = &this->base; diff --git a/src/code/graph.c b/src/code/graph.c index 004659f34d..d9264d9b4a 100644 --- a/src/code/graph.c +++ b/src/code/graph.c @@ -16,7 +16,7 @@ OSTime sGraphPrevUpdateEndTime; #include "buffers.h" #include "idle.h" #include "sys_cfb.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "z64speed_meter.h" #include "overlays/gamestates/ovl_daytelop/z_daytelop.h" #include "overlays/gamestates/ovl_file_choose/z_file_select.h" @@ -341,13 +341,12 @@ void Graph_ThreadEntry(void* arg) { u32 size; s32 pad[2]; - gZBufferLoRes = SystemArena_Malloc(sizeof(*gZBufferLoRes) + sizeof(*gWorkBufferLoRes) + 64 - 1); + gZBufferLoRes = malloc(sizeof(*gZBufferLoRes) + sizeof(*gWorkBufferLoRes) + 64 - 1); gZBufferLoRes = (void*)ALIGN64((u32)gZBufferLoRes); gWorkBufferLoRes = (void*)((u8*)gZBufferLoRes + sizeof(*gZBufferLoRes)); - gGfxSPTaskOutputBufferHiRes = gGfxSPTaskOutputBufferLoRes = - SystemArena_Malloc(sizeof(*gGfxSPTaskOutputBufferLoRes)); + gGfxSPTaskOutputBufferHiRes = gGfxSPTaskOutputBufferLoRes = malloc(sizeof(*gGfxSPTaskOutputBufferLoRes)); gGfxSPTaskOutputBufferEndLoRes = (u8*)gGfxSPTaskOutputBufferLoRes + sizeof(*gGfxSPTaskOutputBufferLoRes); gGfxSPTaskOutputBufferEndHiRes = (u8*)gGfxSPTaskOutputBufferHiRes + sizeof(*gGfxSPTaskOutputBufferHiRes); @@ -365,7 +364,7 @@ void Graph_ThreadEntry(void* arg) { func_800809F4(ovl->vromStart); - gameState = SystemArena_Malloc(size); + gameState = malloc(size); bzero(gameState, size); GameState_Init(gameState, ovl->init, &gfxCtx); @@ -379,7 +378,7 @@ void Graph_ThreadEntry(void* arg) { if (size) {} GameState_Destroy(gameState); - SystemArena_Free(gameState); + free(gameState); Overlay_FreeGameState(ovl); } diff --git a/src/code/listalloc.c b/src/code/listalloc.c index ba346d954d..e6b62eef51 100644 --- a/src/code/listalloc.c +++ b/src/code/listalloc.c @@ -1,5 +1,5 @@ #include "listalloc.h" -#include "system_malloc.h" +#include "libc64/malloc.h" ListAlloc* ListAlloc_Init(ListAlloc* this) { this->prev = NULL; @@ -8,7 +8,7 @@ ListAlloc* ListAlloc_Init(ListAlloc* this) { } void* ListAlloc_Alloc(ListAlloc* this, size_t size) { - ListAlloc* ptr = SystemArena_Malloc(size + sizeof(ListAlloc)); + ListAlloc* ptr = malloc(size + sizeof(ListAlloc)); ListAlloc* next; if (ptr == NULL) { @@ -50,7 +50,7 @@ void ListAlloc_Free(ListAlloc* this, void* data) { this->next = ptr->prev; } - SystemArena_Free(ptr); + free(ptr); } void ListAlloc_FreeAll(ListAlloc* this) { diff --git a/src/code/padmgr.c b/src/code/padmgr.c index 2301024597..9faf232fce 100644 --- a/src/code/padmgr.c +++ b/src/code/padmgr.c @@ -34,6 +34,7 @@ #include "global.h" #include "PR/controller.h" #include "PR/os_motor.h" +#include "libc64/sprintf.h" #include "fault.h" #include "z64voice.h" diff --git a/src/code/sched.c b/src/code/sched.c index c7fb5a4953..7b4a0b920c 100644 --- a/src/code/sched.c +++ b/src/code/sched.c @@ -1,5 +1,6 @@ #include "fault.h" #include "idle.h" +#include "libc64/sleep.h" #include "z64.h" // Variables are put before most headers as a hacky way to bypass bss reordering @@ -87,7 +88,7 @@ void Sched_HandleAudioCancel(SchedContext* sched) { osSyncPrintf("AUDIO SP止まりませんでした(10msタイムアウト)\n"); goto send_mesg; } - Sleep_Usec(100); + usleep(100); } // AUDIO SP stopped (% d * 100us) osSyncPrintf("AUDIO SP止まりました(%d * 100us)\n", i); @@ -150,7 +151,7 @@ void Sched_HandleGfxCancel(SchedContext* sched) { osSyncPrintf("GRAPH SP止まりませんでした(10msタイムアウト)\n"); goto send_mesg; } - Sleep_Usec(100); + usleep(100); } // GRAPH SP stopped (%d * 100us) osSyncPrintf("GRAPH SP止まりました(%d * 100us)\n", i); diff --git a/src/code/speed_meter.c b/src/code/speed_meter.c index 894127f2c2..e82fb29753 100644 --- a/src/code/speed_meter.c +++ b/src/code/speed_meter.c @@ -4,7 +4,7 @@ #include "gfx.h" #include "regs.h" #include "sys_cfb.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "z64game.h" #include "z64malloc.h" #include "z64view.h" @@ -240,7 +240,7 @@ void SpeedMeter_DrawAllocEntries(SpeedMeter* meter, GraphicsContext* gfxCtx, Gam } if (R_ENABLE_ARENA_DBG > 1) { - SystemArena_GetSizes((size_t*)&sysFreeMax, (size_t*)&sysFree, (size_t*)&sysAlloc); + GetFreeArena((size_t*)&sysFreeMax, (size_t*)&sysFree, (size_t*)&sysAlloc); SpeedMeter_InitAllocEntry(&entry, sysFree + sysAlloc - state->tha.size, sysAlloc - state->tha.size, GPACK_RGBA5551(0, 0, 255, 1), GPACK_RGBA5551(255, 128, 128, 1), ulx, lrx, y, y); SpeedMeter_DrawAllocEntry(&entry, gfxCtx); diff --git a/src/code/sys_cfb.c b/src/code/sys_cfb.c index b559a44ee4..71da5e73d5 100644 --- a/src/code/sys_cfb.c +++ b/src/code/sys_cfb.c @@ -38,7 +38,7 @@ u8 gSysCfbHiResEnabled; #include "sys_cfb.h" #include "libc/stdbool.h" #include "buffers.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "z64vimode.h" void SysCfb_SetLoResMode(void) { diff --git a/src/code/sys_cmpdma.c b/src/code/sys_cmpdma.c index 9e1a66d2e0..71d92c1873 100644 --- a/src/code/sys_cmpdma.c +++ b/src/code/sys_cmpdma.c @@ -1,5 +1,5 @@ #include "global.h" -#include "system_malloc.h" +#include "libc64/malloc.h" typedef struct { /* 0x0 */ union { @@ -83,11 +83,11 @@ void CmpDma_LoadFileImpl(uintptr_t segmentRom, s32 id, void* dst, size_t size) { CmpDma_GetFileInfo(segmentRom, id, &romStart, &compressedSize, &flag); if (flag & 1) { - void* tempBuf = SystemArena_Malloc(0x1000); + void* tempBuf = malloc(0x1000); CmpDma_Decompress(romStart, compressedSize, tempBuf); func_80178AC0(tempBuf, dst, size); - SystemArena_Free(tempBuf); + free(tempBuf); } else { CmpDma_Decompress(romStart, compressedSize, dst); } diff --git a/src/code/sys_flashrom.c b/src/code/sys_flashrom.c index 5ebc5dcba7..7ba8f219c5 100644 --- a/src/code/sys_flashrom.c +++ b/src/code/sys_flashrom.c @@ -3,7 +3,7 @@ #include "fault.h" #include "stack.h" #include "stackcheck.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "z64thread.h" #include "sys_flashrom.h" #include "PR/os_internal_flash.h" @@ -172,7 +172,7 @@ s32 SysFlashrom_WriteData(void* addr, u32 pageNum, u32 pageCount) { return -1; } size = pageCount * FLASH_BLOCK_SIZE; - data = SystemArena_Malloc(size); + data = malloc(size); if (data == NULL) { ret = SysFlashrom_AttemptWrite(addr, pageNum, pageCount); } else { @@ -195,7 +195,7 @@ s32 SysFlashrom_WriteData(void* addr, u32 pageNum, u32 pageCount) { } } } - SystemArena_Free(data); + free(data); } return ret; } diff --git a/src/code/z_DLF.c b/src/code/z_DLF.c index e71339bbb1..1e03cead74 100644 --- a/src/code/z_DLF.c +++ b/src/code/z_DLF.c @@ -1,5 +1,5 @@ #include "global.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "loadfragment.h" void Overlay_LoadGameState(GameStateOverlay* overlayEntry) { @@ -89,7 +89,7 @@ void Overlay_FreeGameState(GameStateOverlay* overlayEntry) { (uintptr_t)overlayEntry->loadedRamAddr)) : NULL); - SystemArena_Free(overlayEntry->loadedRamAddr); + free(overlayEntry->loadedRamAddr); overlayEntry->loadedRamAddr = NULL; } } diff --git a/src/code/z_bgcheck.c b/src/code/z_bgcheck.c index d16102567a..9316984820 100644 --- a/src/code/z_bgcheck.c +++ b/src/code/z_bgcheck.c @@ -1,7 +1,8 @@ #include "prevent_bss_reordering.h" #include "global.h" #include "fault.h" -#include "fixed_point.h" +#include "libc64/fixed_point.h" +#include "libc64/sprintf.h" #include "vt.h" #include "overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope.h" diff --git a/src/code/z_debug.c b/src/code/z_debug.c index 058d896a45..b22709fa75 100644 --- a/src/code/z_debug.c +++ b/src/code/z_debug.c @@ -1,5 +1,5 @@ #include "regs.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #include "macros.h" RegEditor* gRegEditor; @@ -7,7 +7,7 @@ RegEditor* gRegEditor; void Regs_Init(void) { s32 i; - gRegEditor = SystemArena_Malloc(sizeof(RegEditor)); + gRegEditor = malloc(sizeof(RegEditor)); if (1) {} gRegEditor->regPage = 0; gRegEditor->regGroup = 0; diff --git a/src/code/z_fbdemo.c b/src/code/z_fbdemo.c index 6ad5f7a6c7..b0edfb0b7d 100644 --- a/src/code/z_fbdemo.c +++ b/src/code/z_fbdemo.c @@ -12,8 +12,9 @@ #include "z64transition.h" -#include "global.h" -#include "system_malloc.h" +#include "libc64/sleep.h" +#include "libc64/malloc.h" +#include "macros.h" Gfx sTransTileSetupDL[] = { gsDPPipeSync(), @@ -103,22 +104,22 @@ void TransitionTile_InitVtxData(TransitionTile* this) { } void TransitionTile_Destroy(TransitionTile* this) { - Sleep_Msec(100); + msleep(100); if (this->vtxData != NULL) { - SystemArena_Free(this->vtxData); + free(this->vtxData); this->vtxData = NULL; } if (this->vtxFrame1 != NULL) { - SystemArena_Free(this->vtxFrame1); + free(this->vtxFrame1); this->vtxFrame1 = NULL; } if (this->vtxFrame2 != NULL) { - SystemArena_Free(this->vtxFrame2); + free(this->vtxFrame2); this->vtxFrame2 = NULL; } if (this->gfx != NULL) { - SystemArena_Free(this->gfx); + free(this->gfx); this->gfx = NULL; } } @@ -131,26 +132,26 @@ TransitionTile* TransitionTile_Init(TransitionTile* this, s32 cols, s32 rows) { this->cols = cols; this->rows = rows; gridSize = (cols + 1) * (rows + 1); - this->vtxData = SystemArena_Malloc(gridSize * sizeof(TransitionTileVtxData)); - this->vtxFrame1 = SystemArena_Malloc(gridSize * sizeof(Vtx)); - this->vtxFrame2 = SystemArena_Malloc(gridSize * sizeof(Vtx)); - this->gfx = SystemArena_Malloc(((cols * 9 + 1) * rows + 2) * sizeof(Gfx)); + this->vtxData = malloc(gridSize * sizeof(TransitionTileVtxData)); + this->vtxFrame1 = malloc(gridSize * sizeof(Vtx)); + this->vtxFrame2 = malloc(gridSize * sizeof(Vtx)); + this->gfx = malloc(((cols * 9 + 1) * rows + 2) * sizeof(Gfx)); if ((this->vtxData == NULL) || (this->vtxFrame1 == NULL) || (this->vtxFrame2 == NULL) || (this->gfx == NULL)) { if (this->vtxData != NULL) { - SystemArena_Free(this->vtxData); + free(this->vtxData); this->vtxData = NULL; } if (this->vtxFrame1 != NULL) { - SystemArena_Free(this->vtxFrame1); + free(this->vtxFrame1); this->vtxFrame1 = NULL; } if (this->vtxFrame2 != NULL) { - SystemArena_Free(this->vtxFrame2); + free(this->vtxFrame2); this->vtxFrame2 = NULL; } if (this->gfx != NULL) { - SystemArena_Free(this->gfx); + free(this->gfx); this->gfx = NULL; } return NULL; diff --git a/src/code/z_kankyo.c b/src/code/z_kankyo.c index 84acb9c646..894daa37f4 100644 --- a/src/code/z_kankyo.c +++ b/src/code/z_kankyo.c @@ -3226,16 +3226,16 @@ void Environment_DrawSkyboxStarsImpl(PlayState* play, Gfx** gfxP) { // temp_f4 = Rand_ZeroOne_Variable(&randInt); randInt = (randInt * RAND_MULTIPLIER) + RAND_INCREMENT; - gRandFloat = (randInt >> 9) | 0x3F800000; - temp = *((f32*)&gRandFloat); + gRandFloat.i = (randInt >> 9) | 0x3F800000; + temp = gRandFloat.f; temp_f4 = temp - 1.0f; // temp_f20 = Rand_ZeroOne_Variable(&randInt); randInt = (randInt * RAND_MULTIPLIER) + RAND_INCREMENT; - gRandFloat = (randInt >> 9) | 0x3F800000; - temp_f20 = ((*((f32*)&gRandFloat) - 1.0f) + temp_f4) * 0.5f; + gRandFloat.i = (randInt >> 9) | 0x3F800000; + temp_f20 = ((gRandFloat.f - 1.0f) + temp_f4) * 0.5f; - // randInt = Rand_Next_Variable(&randInt); + // Rand_Next_Variable(&randInt); randInt = (randInt * RAND_MULTIPLIER) + RAND_INCREMENT; // Set random position @@ -3245,8 +3245,8 @@ void Environment_DrawSkyboxStarsImpl(PlayState* play, Gfx** gfxP) { // temp_f2 = Rand_ZeroOne_Variable(&randInt); randInt = (randInt * RAND_MULTIPLIER) + RAND_INCREMENT; - gRandFloat = ((randInt >> 9) | 0x3F800000); - temp_f2 = *((f32*)&gRandFloat) - 1.0f; + gRandFloat.i = ((randInt >> 9) | 0x3F800000); + temp_f2 = gRandFloat.f - 1.0f; // Set random width imgWidth = (u32)((SQ(temp_f2) * 8.0f) + 2.0f); diff --git a/src/code/z_malloc.c b/src/code/z_malloc.c index 80d7fc7afc..f54d8f5166 100644 --- a/src/code/z_malloc.c +++ b/src/code/z_malloc.c @@ -1,6 +1,6 @@ #include "z64malloc.h" -#include "os_malloc.h" +#include "libc64/os_malloc.h" Arena sZeldaArena; diff --git a/src/code/z_vismono.c b/src/code/z_vismono.c index 86937c3158..d4c462fc47 100644 --- a/src/code/z_vismono.c +++ b/src/code/z_vismono.c @@ -5,7 +5,7 @@ #include "global.h" #include "z64vismono.h" -#include "system_malloc.h" +#include "libc64/malloc.h" // Height of the fragments the color frame buffer (CFB) is split into. // It is the maximum amount of lines such that all rgba16 SCREEN_WIDTH-long lines fit into @@ -39,7 +39,7 @@ void VisMono_Init(VisMono* this) { } void VisMono_Destroy(VisMono* this) { - SystemArena_Free(this->dList); + free(this->dList); } void VisMono_DesaturateTLUT(u16* tlut) { @@ -169,12 +169,12 @@ void VisMono_Draw(VisMono* this, Gfx** gfxp) { void VisMono_DrawOld(VisMono* this) { if (this->tlut == NULL) { - this->tlut = SystemArena_Malloc(256 * G_IM_SIZ_16b_BYTES); + this->tlut = malloc(256 * G_IM_SIZ_16b_BYTES); VisMono_DesaturateTLUT(this->tlut); } if (this->dList == NULL) { - this->dList = SystemArena_Malloc(VISMONO_DLSIZE * sizeof(Gfx)); + this->dList = malloc(VISMONO_DLSIZE * sizeof(Gfx)); VisMono_DesaturateDList(this->dList); } } diff --git a/src/libultra/gu/sins.c b/src/libultra/gu/sins.c index 39bc7ce0c1..247bb36890 100644 --- a/src/libultra/gu/sins.c +++ b/src/libultra/gu/sins.c @@ -1,3 +1,4 @@ +#include "ultra64.h" #include "libc/math.h" static s16 sintable[0x400] = { diff --git a/src/overlays/actors/ovl_Eff_Dust/z_eff_dust.c b/src/overlays/actors/ovl_Eff_Dust/z_eff_dust.c index d7c9f6a296..aa274f83cb 100644 --- a/src/overlays/actors/ovl_Eff_Dust/z_eff_dust.c +++ b/src/overlays/actors/ovl_Eff_Dust/z_eff_dust.c @@ -6,7 +6,7 @@ #include "z_eff_dust.h" #include "objects/gameplay_keep/gameplay_keep.h" -#include "system_malloc.h" +#include "libc64/malloc.h" #define FLAGS (ACTOR_FLAG_10 | ACTOR_FLAG_20) @@ -99,7 +99,7 @@ void EffDust_Init(Actor* thisx, PlayState* play) { break; default: - SystemArena_Free(this); + free(this); break; } this->life = 10; diff --git a/tools/disasm/files.txt b/tools/disasm/files.txt index 01a43df765..a4fbcd21be 100644 --- a/tools/disasm/files.txt +++ b/tools/disasm/files.txt @@ -61,8 +61,8 @@ 0x80086620 : "padsetup", 0x80086760 : "math64", 0x80086C70 : "fp", - 0x80086DD0 : "system_malloc", - 0x80086FA0 : "rand", + 0x80086DD0 : "malloc", + 0x80086FA0 : "qrand", 0x80087160 : "__osMalloc", 0x80087830 : "sprintf", 0x80087900 : "printutils", @@ -258,7 +258,7 @@ 0x80096C50 : "gfxprint", 0x80097500 : "system_heap", 0x80097520 : "fp", - 0x80097530 : "rand", + 0x80097530 : "qrand", 0x80097540 : "vimodentschpf1", 0x80097590 : "vimodepallan1", 0x800975E0 : "sins", @@ -322,8 +322,8 @@ 0x8009BE50 : "fault", 0x8009CCD0 : "fault_drawer", 0x8009CD10 : "math64", - 0x8009CD20 : "system_malloc", - 0x8009CD50 : "rand", + 0x8009CD20 : "malloc", + 0x8009CD50 : "qrand", 0x8009CD60 : "__osMalloc", 0x8009CD70 : "sptask", 0x8009CDB0 : "motor", diff --git a/tools/disasm/functions.txt b/tools/disasm/functions.txt index 36f6b0a598..a114497382 100644 --- a/tools/disasm/functions.txt +++ b/tools/disasm/functions.txt @@ -199,16 +199,16 @@ 0x80086D6C:("round",), 0x80086D8C:("lroundf",), 0x80086DAC:("lround",), - 0x80086DD0:("SystemArena_Malloc",), - 0x80086DF8:("SystemArena_MallocR",), - 0x80086E20:("SystemArena_Realloc",), - 0x80086E50:("SystemArena_Free",), - 0x80086E78:("SystemArena_Calloc",), - 0x80086ECC:("SystemArena_GetSizes",), - 0x80086F04:("SystemArena_CheckArena",), - 0x80086F28:("SystemArena_Init",), - 0x80086F58:("SystemArena_Cleanup",), - 0x80086F7C:("SystemArena_IsInitialized",), + 0x80086DD0:("malloc",), + 0x80086DF8:("malloc_r",), + 0x80086E20:("realloc",), + 0x80086E50:("free",), + 0x80086E78:("calloc",), + 0x80086ECC:("GetFreeArena",), + 0x80086F04:("CheckArena",), + 0x80086F28:("MallocInit",), + 0x80086F58:("MallocCleanup",), + 0x80086F7C:("MallocIsInitialized",), 0x80086FA0:("Rand_Next",), 0x80086FD0:("Rand_Seed",), 0x80086FDC:("Rand_ZeroOne",), @@ -231,16 +231,16 @@ 0x800875E4:("__osRealloc",), 0x80087714:("__osGetSizes",), 0x800877C4:("__osCheckArena",), - 0x80087830:("proutSprintf",), + 0x80087830:("proutPrintf",), 0x80087854:("vsprintf",), 0x800878A4:("sprintf",), - 0x80087900:("PrintUtils_VPrintf",), - 0x80087934:("PrintUtils_Printf",), - 0x80087960:("Sleep_Cycles",), - 0x800879CC:("Sleep_Nsec",), - 0x80087A1C:("Sleep_Usec",), - 0x80087A6C:("Sleep_Msec",), - 0x80087AC0:("Sleep_Sec",), + 0x80087900:("vaprintf",), + 0x80087934:("aprintf",), + 0x80087960:("csleep",), + 0x800879CC:("nsleep",), + 0x80087A1C:("usleep",), + 0x80087A6C:("msleep",), + 0x80087AC0:("sleep",), 0x80087B00:("__osSetCause",), 0x80087B10:("osSendMesg",), 0x80087C60:("osPfsFreeBlocks",), diff --git a/tools/disasm/variables.txt b/tools/disasm/variables.txt index 9952b1ef36..5fa224d616 100644 --- a/tools/disasm/variables.txt +++ b/tools/disasm/variables.txt @@ -321,7 +321,7 @@ 0x8009C480:("gFaultMgr","FaultMgr","",0x848), 0x8009CCD0:("sFaultDrawer","FaultDrawer","",0x3c), 0x8009CD10:("D_8009CD10","UNK_TYPE4","",0x4), - 0x8009CD20:("gSystemArena","Arena","",0x24), + 0x8009CD20:("malloc_arena","Arena","",0x24), 0x8009CD50:("gRandFloat","f32","",0x4), 0x8009CD60:("sArenaLockMsg","OSMesg","[1]",0x4), 0x8009CD70:("tmp_task","OSTask","",0x40), diff --git a/tools/namefixer.py b/tools/namefixer.py index 8384a8af38..e28f714c94 100755 --- a/tools/namefixer.py +++ b/tools/namefixer.py @@ -18,6 +18,28 @@ simpleReplace = { # explanation in replace_single below) wordReplace = { # Functions + "SystemArena_Malloc": "malloc", + "SystemArena_MallocR": "malloc_r", + "SystemArena_Realloc": "realloc", + "SystemArena_Free": "free", + "SystemArena_Calloc": "calloc", + "SystemArena_GetSizes": "GetFreeArena", + "SystemArena_CheckArena": "CheckArena", + "SystemArena_Init": "MallocInit", + "SystemArena_Cleanup": "MallocCleanup", + "SystemArena_IsInitialized": "MallocIsInitialized", + + "proutSprintf": "proutPrintf", + + "PrintUtils_VPrintf": "vaprintf", + "PrintUtils_Printf": "aprintf", + + "Sleep_Cycles": "csleep", + "Sleep_Nsec": "nsleep", + "Sleep_Usec": "usleep", + "Sleep_Msec": "msleep", + "Sleep_Sec": "sleep", + "Actor_GetSwitchFlag": "Flags_GetSwitch", "Math_Acot2F": "Math_Atan2F_XY", "atan_flip": "Math_Atan2F_XY", diff --git a/tools/sizes/boot_functions.csv b/tools/sizes/boot_functions.csv index 651eb9f088..ef0ac1e974 100644 --- a/tools/sizes/boot_functions.csv +++ b/tools/sizes/boot_functions.csv @@ -155,7 +155,7 @@ asm/non_matchings/boot/mtxuty-cvt/MtxConv_L2F.s,MtxConv_L2F,0x80086258,0xA asm/non_matchings/boot/assert/_dbg_hungup.s,_dbg_hungup,0x80086280,0xD asm/non_matchings/boot/assert/Reset.s,Reset,0x800862B4,0xB asm/non_matchings/boot/boot_800862E0/SystemHeap_Malloc.s,SystemHeap_Malloc,0x800862E0,0xC -asm/non_matchings/boot/boot_800862E0/SystemArena_Free.s,SystemArena_Free,0x80086310,0xB +asm/non_matchings/boot/boot_800862E0/free.s,free,0x80086310,0xB asm/non_matchings/boot/boot_800862E0/SystemHeap_RunBlockFunc.s,SystemHeap_RunBlockFunc,0x8008633C,0x1C asm/non_matchings/boot/boot_800862E0/SystemHeap_RunBlockFunc1.s,SystemHeap_RunBlockFunc1,0x800863AC,0x1C asm/non_matchings/boot/boot_800862E0/SystemHeap_RunBlockFunc8.s,SystemHeap_RunBlockFunc8,0x8008641C,0x34 @@ -196,16 +196,16 @@ asm/non_matchings/boot/fp/roundf.s,roundf,0x80086D50,0x7 asm/non_matchings/boot/fp/round.s,round,0x80086D6C,0x8 asm/non_matchings/boot/fp/lroundf.s,lroundf,0x80086D8C,0x8 asm/non_matchings/boot/fp/lround.s,lround,0x80086DAC,0x9 -asm/non_matchings/boot/system_malloc/SystemArena_Malloc.s,SystemArena_Malloc,0x80086DD0,0xA -asm/non_matchings/boot/system_malloc/SystemArena_MallocR.s,SystemArena_MallocR,0x80086DF8,0xA -asm/non_matchings/boot/system_malloc/SystemArena_Realloc.s,SystemArena_Realloc,0x80086E20,0xC -asm/non_matchings/boot/system_malloc/SystemArena_Free.s,SystemArena_Free,0x80086E50,0xA -asm/non_matchings/boot/system_malloc/SystemArena_Calloc.s,SystemArena_Calloc,0x80086E78,0x15 -asm/non_matchings/boot/system_malloc/SystemArena_GetSizes.s,SystemArena_GetSizes,0x80086ECC,0xE -asm/non_matchings/boot/system_malloc/SystemArena_CheckArena.s,SystemArena_CheckArena,0x80086F04,0x9 -asm/non_matchings/boot/system_malloc/SystemArena_Init.s,SystemArena_Init,0x80086F28,0xC -asm/non_matchings/boot/system_malloc/SystemArena_Cleanup.s,SystemArena_Cleanup,0x80086F58,0x9 -asm/non_matchings/boot/system_malloc/SystemArena_IsInitialized.s,SystemArena_IsInitialized,0x80086F7C,0x9 +asm/non_matchings/boot/system_malloc/malloc.s,malloc,0x80086DD0,0xA +asm/non_matchings/boot/system_malloc/malloc_r.s,malloc_r,0x80086DF8,0xA +asm/non_matchings/boot/system_malloc/realloc.s,realloc,0x80086E20,0xC +asm/non_matchings/boot/system_malloc/free.s,free,0x80086E50,0xA +asm/non_matchings/boot/system_malloc/calloc.s,calloc,0x80086E78,0x15 +asm/non_matchings/boot/system_malloc/GetFreeArena.s,GetFreeArena,0x80086ECC,0xE +asm/non_matchings/boot/system_malloc/CheckArena.s,CheckArena,0x80086F04,0x9 +asm/non_matchings/boot/system_malloc/MallocInit.s,MallocInit,0x80086F28,0xC +asm/non_matchings/boot/system_malloc/MallocCleanup.s,MallocCleanup,0x80086F58,0x9 +asm/non_matchings/boot/system_malloc/MallocIsInitialized.s,MallocIsInitialized,0x80086F7C,0x9 asm/non_matchings/boot/rand/Rand_Next.s,Rand_Next,0x80086FA0,0xC asm/non_matchings/boot/rand/Rand_Seed.s,Rand_Seed,0x80086FD0,0x3 asm/non_matchings/boot/rand/Rand_ZeroOne.s,Rand_ZeroOne,0x80086FDC,0x15 @@ -228,16 +228,16 @@ asm/non_matchings/boot/__osMalloc/__osFree.s,__osFree,0x800874EC,0x3E asm/non_matchings/boot/__osMalloc/__osRealloc.s,__osRealloc,0x800875E4,0x4C asm/non_matchings/boot/__osMalloc/__osGetSizes.s,__osGetSizes,0x80087714,0x2C asm/non_matchings/boot/__osMalloc/__osCheckArena.s,__osCheckArena,0x800877C4,0x1B -asm/non_matchings/boot/sprintf/proutSprintf.s,proutSprintf,0x80087830,0x9 +asm/non_matchings/boot/sprintf/proutPrintf.s,proutPrintf,0x80087830,0x9 asm/non_matchings/boot/sprintf/vsprintf.s,vsprintf,0x80087854,0x14 asm/non_matchings/boot/sprintf/sprintf.s,sprintf,0x800878A4,0x17 -asm/non_matchings/boot/printutils/PrintUtils_VPrintf.s,PrintUtils_VPrintf,0x80087900,0xD -asm/non_matchings/boot/printutils/PrintUtils_Printf.s,PrintUtils_Printf,0x80087934,0xB -asm/non_matchings/boot/printutils/Sleep_Cycles.s,Sleep_Cycles,0x80087960,0x1B -asm/non_matchings/boot/printutils/Sleep_Nsec.s,Sleep_Nsec,0x800879CC,0x14 -asm/non_matchings/boot/printutils/Sleep_Usec.s,Sleep_Usec,0x80087A1C,0x14 -asm/non_matchings/boot/printutils/Sleep_Msec.s,Sleep_Msec,0x80087A6C,0x15 -asm/non_matchings/boot/printutils/Sleep_Sec.s,Sleep_Sec,0x80087AC0,0x10 +asm/non_matchings/boot/printutils/vaprintf.s,vaprintf,0x80087900,0xD +asm/non_matchings/boot/printutils/aprintf.s,aprintf,0x80087934,0xB +asm/non_matchings/boot/printutils/csleep.s,csleep,0x80087960,0x1B +asm/non_matchings/boot/printutils/nsleep.s,nsleep,0x800879CC,0x14 +asm/non_matchings/boot/printutils/usleep.s,usleep,0x80087A1C,0x14 +asm/non_matchings/boot/printutils/msleep.s,msleep,0x80087A6C,0x15 +asm/non_matchings/boot/printutils/sleep.s,sleep,0x80087AC0,0x10 asm/non_matchings/boot/setcause/__osSetCause.s,__osSetCause,0x80087B00,0x4 asm/non_matchings/boot/sendmesg/osSendMesg.s,osSendMesg,0x80087B10,0x54 asm/non_matchings/boot/pfsfreeblocks/osPfsFreeBlocks.s,osPfsFreeBlocks,0x80087C60,0x68