diff --git a/Makefile b/Makefile index 51fcaad..f7dd653 100644 --- a/Makefile +++ b/Makefile @@ -124,8 +124,9 @@ SPLAT_YAML ?= $(TARGET)-$(VERSION).yaml -IINC := -Iinclude -I. -IINC += -Ilib/ultralib/include -Ilib/ultralib/include/PR -Ilib/ultralib/include/ido +IINC := -Iinclude -Iinclude/libc +IINC += -Ilib/ultralib/include -Ilib/ultralib/include/PR +IINC += -I. ifeq ($(KEEP_MDEBUG),0) RM_MDEBUG = $(OBJCOPY) --remove-section .mdebug $@ diff --git a/include/gfxprint.h b/include/gfxprint.h index 226951e..4d92ab4 100644 --- a/include/gfxprint.h +++ b/include/gfxprint.h @@ -2,9 +2,11 @@ #define LIBU64_GFX_PRINT_H #include "ultra64.h" -#include "color.h" + +#include "stdbool.h" + #include "aprintf.h" -#include "libc/stdbool.h" +#include "color.h" typedef struct gfxprint { /* 0x00 */ PrintCallback proutFunc; /* Current print out func */ diff --git a/include/global.h b/include/global.h index 92c04e0..d28c29f 100644 --- a/include/global.h +++ b/include/global.h @@ -3,9 +3,9 @@ #include "ultra64.h" -#include "libc/stdbool.h" -#include "libc/stddef.h" -#include "libc/stdint.h" +#include "stdbool.h" +#include "stddef.h" +#include "stdint.h" #include "attributes.h" #include "eepmgr.h" diff --git a/include/libc/math.h b/include/libc/math.h index 8501a66..4b27be5 100644 --- a/include/libc/math.h +++ b/include/libc/math.h @@ -1,12 +1,26 @@ -#ifndef MATH_H -#define MATH_H - -#include "PR/ultratypes.h" - -float fabsf(float f); -#pragma intrinsic(fabsf) - -double fabs(double f); -#pragma intrinsic(fabs) - -#endif +#ifndef LIBC_MATH_H +#define LIBC_MATH_H + +#define M_PI 3.14159265358979323846 +#define M_PIf 3.14159265358979323846f +#define M_SQRT2f 1.41421356237309504880f +#define M_SQRT1_2f 0.70710678118654752440f /* 1/sqrt(2) */ + +#define FLT_MAX 3.40282347e+38f +#define SHRT_MAX 32767.0f + +float fabsf(float f); +#pragma intrinsic(fabsf) +#ifdef __GNUC__ +#define fabsf(f) __builtin_fabsf((float)(f)) +#endif + +double fabs(double f); +#pragma intrinsic(fabs) + +double sqrt(double d); +#pragma intrinsic(sqrt) + +float fmodf(float dividend, float divisor); + +#endif diff --git a/include/libc/stdarg.h b/include/libc/stdarg.h new file mode 100644 index 0000000..55f6943 --- /dev/null +++ b/include/libc/stdarg.h @@ -0,0 +1,44 @@ +#ifndef LIBC_STDARG_H +#define LIBC_STDARG_H + +// When building with GCC, use the official vaarg macros to avoid warnings +// and possibly bad codegen. +#ifdef __GNUC__ +#define va_list __builtin_va_list +#define va_start __builtin_va_start +#define va_arg __builtin_va_arg +#define va_end __builtin_va_end +#else + +typedef char *va_list; +#define _FP 1 +#define _INT 0 +#define _STRUCT 2 + +#define _VA_FP_SAVE_AREA 0x10 +#define _VA_ALIGN(p, a) (((unsigned int)(((char *)p) + ((a) > 4 ? (a) : 4) - 1)) & -((a) > 4 ? (a) : 4)) +#define va_start(vp, parmN) (vp = ((va_list)&parmN + sizeof(parmN))) + +#define __va_stack_arg(list, mode) \ + ( \ + ((list) = (char *)_VA_ALIGN(list, __builtin_alignof(mode)) + \ + _VA_ALIGN(sizeof(mode), 4)), \ + (((char *)list) - (_VA_ALIGN(sizeof(mode), 4) - sizeof(mode)))) + +#define __va_double_arg(list, mode) \ + ( \ + (((long)list & 0x1) /* 1 byte aligned? */ \ + ? (list = (char *)((long)list + 7), (char *)((long)list - 6 - _VA_FP_SAVE_AREA)) \ + : (((long)list & 0x2) /* 2 byte aligned? */ \ + ? (list = (char *)((long)list + 10), (char *)((long)list - 24 - _VA_FP_SAVE_AREA)) \ + : __va_stack_arg(list, mode)))) + +#define va_arg(list, mode) ((mode *)(((__builtin_classof(mode) == _FP && \ + __builtin_alignof(mode) == sizeof(double)) \ + ? __va_double_arg(list, mode) \ + : __va_stack_arg(list, mode))))[-1] +#define va_end(__list) + +#endif /* __GNUC__ */ + +#endif /* STDARG_H */ diff --git a/include/libc/stdlib.h b/include/libc/stdlib.h new file mode 100644 index 0000000..08d313f --- /dev/null +++ b/include/libc/stdlib.h @@ -0,0 +1,28 @@ +#ifndef LIBC_STDLIB_H +#define LIBC_STDLIB_H + +#include "stddef.h" + +typedef struct { + /* 0x0 */ int quot; + /* 0x4 */ int rem; +} div_t; + +typedef struct { + /* 0x0 */ long quot; + /* 0x4 */ long rem; +} ldiv_t; + +typedef struct { + /* 0x0 */ long long quot; + /* 0x8 */ long long rem; +} lldiv_t; + +typedef int ssize_t; + +typedef long wchar_t; + +ldiv_t ldiv(long numer, long denom); +lldiv_t lldiv(long long numer, long long denom); + +#endif /* STDLIB_H */ diff --git a/include/segment_symbols.h b/include/segment_symbols.h index bb74f1e..d10aa0e 100644 --- a/include/segment_symbols.h +++ b/include/segment_symbols.h @@ -1,8 +1,7 @@ #ifndef SEGMENT_SYMBOLS_H #define SEGMENT_SYMBOLS_H -#include "ultra64.h" -#include "libc/stdint.h" +#include "stdint.h" #define DECLARE_VRAM_SEGMENT(name) \ extern u8 name ## _VRAM[]; \ diff --git a/src/main/O2/65430.c b/src/main/O2/65430.c index 7bc7227..5628e0b 100644 --- a/src/main/O2/65430.c +++ b/src/main/O2/65430.c @@ -231,20 +231,19 @@ void Idle_ThreadEntry(void* arg) { void Idle_ThreadEntry(void* arg); #pragma GLOBAL_ASM("asm/us/nonmatchings/main/O2/65430/Idle_ThreadEntry.s") #endif -#undef NON_MATCHING void Idle_InitVideo(void) { switch (osTvType) { case OS_TV_TYPE_PAL: - osViSetMode(&osViModeTable[16]); + osViSetMode(&osViModeTable[OS_VI_PAL_LAN1]); break; case OS_TV_TYPE_NTSC: - osViSetMode(&osViModeTable[2]); + osViSetMode(&osViModeTable[OS_VI_NTSC_LAN1]); break; case OS_TV_TYPE_MPAL: - osViSetMode(&osViModeTable[30]); + osViSetMode(&osViModeTable[OS_VI_MPAL_LAN1]); break; } } diff --git a/src/main/O2/math64.c b/src/main/O2/math64.c index db64233..89d88f5 100644 --- a/src/main/O2/math64.c +++ b/src/main/O2/math64.c @@ -1,7 +1,9 @@ -#include "global.h" #include "math64.h" -#include "libc/math.h" + +#include "math.h" + #include "fp.h" +#include "macros.h" f32 Math_FTanF(f32 x) { return sinf(x) / cosf(x); @@ -61,9 +63,9 @@ f32 Math_FAtanContFracF(f32 x) { } if (sector > 0) { - return ((f32)M_PI / 2) - (x / (1.0f + conv)); + return (M_PIf / 2) - (x / (1.0f + conv)); } else if (sector < 0) { - return -((f32)M_PI / 2) - (x / (1.0f + conv)); + return -(M_PIf / 2) - (x / (1.0f + conv)); } else { return x / (1.0f + conv); } @@ -74,15 +76,15 @@ f32 fatan2(f32 y, f32 x) { return 0.0f; } else if (x == 0.0f) { if (y < 0.0f) { - return -((f32)M_PI / 2); + return -(M_PIf / 2); } else { - return (f32)M_PI / 2; + return M_PIf / 2; } } else if (x < 0.0f) { if (y < 0.0f) { - return -((f32)M_PI - Math_FAtanContFracF(fabs((f64)(y / x)))); + return -(M_PIf - Math_FAtanContFracF(fabs((f64)(y / x)))); } else { - return (f32)M_PI - Math_FAtanContFracF(fabs((f64)(y / x))); + return M_PIf - Math_FAtanContFracF(fabs((f64)(y / x))); } } else { return Math_FAtanContFracF(y / x); @@ -94,5 +96,5 @@ f32 Math_FAsinF(f32 x) { } f32 Math_FAcosF(f32 x) { - return ((f32)M_PI / 2) - Math_FAsinF(x); + return (M_PIf / 2) - Math_FAsinF(x); } diff --git a/src/main/O2/stackcheck.c b/src/main/O2/stackcheck.c index 80f8a42..7e1d233 100644 --- a/src/main/O2/stackcheck.c +++ b/src/main/O2/stackcheck.c @@ -1,7 +1,9 @@ #include "stackcheck.h" + +#include "stdbool.h" +#include "stdint.h" + #include "terminal.h" -#include "libc/stdbool.h" -#include "libc/stdint.h" StackEntry* sStackInfoListStart = NULL; StackEntry* sStackInfoListEnd = NULL; diff --git a/src/main/dmamgr.c b/src/main/dmamgr.c index 8b5386a..facfcd5 100644 --- a/src/main/dmamgr.c +++ b/src/main/dmamgr.c @@ -151,6 +151,6 @@ void DmaMgr_Init(void) { osCreateMesgQueue(&D_8010ED10, D_8010ED28, ARRAY_COUNT(D_8010ED28)); osCreateMesgQueue(&D_8010ED50, D_8010ED68, ARRAY_COUNT(D_8010ED68)); StackCheck_Init(&sDmaStackInfo, sDmaStack, STACK_TOP(sDmaStack), 0, 0x100, "dmamgr"); - osCreateThread(&sDmaThread, Y_THREAD_ID_DMA, DmaMgr_ThreadEntry, NULL, &sDmaStackInfo, Y_PRIORITY_DMA); + osCreateThread(&sDmaThread, Y_THREAD_ID_DMA, DmaMgr_ThreadEntry, NULL, STACK_TOP(sDmaStack), Y_PRIORITY_DMA); osStartThread(&sDmaThread); } diff --git a/src/main/fault.c b/src/main/fault.c index 5df6f9c..525432a 100644 --- a/src/main/fault.c +++ b/src/main/fault.c @@ -1,10 +1,10 @@ #include "fault.h" -#include "lib/ultralib/src/libc/xstdio.h" -#include "libc/stdbool.h" -#include "libc/stddef.h" -#include "libc/stdint.h" +#include "stdbool.h" +#include "stddef.h" +#include "stdint.h" #include "PR/os_internal.h" +#include "lib/ultralib/src/libc/xstdio.h" #include "global.h" @@ -740,7 +740,8 @@ void Fault_Init(void) { debugger.depth = 16; osCreateMesgQueue(&debugger.queue, debugger.msg, ARRAY_COUNT(debugger.msg)); StackCheck_Init(&sFaultStackInfo, sFaultStack, STACK_TOP(sFaultStack), 0, 0x100, "fault"); - osCreateThread(&debugger.thread, Y_THREAD_ID_FAULT, Fault_ThreadEntry, NULL, &sFaultStackInfo, Y_PRIORITY_FAULT); + osCreateThread(&debugger.thread, Y_THREAD_ID_FAULT, Fault_ThreadEntry, NULL, STACK_TOP(sFaultStack), + Y_PRIORITY_FAULT); osStartThread(&debugger.thread); } diff --git a/src/main/vimode.c b/src/main/vimode.c index cabdecc..cf47f4d 100644 --- a/src/main/vimode.c +++ b/src/main/vimode.c @@ -1,8 +1,10 @@ #include "vimode.h" + +#include "stdbool.h" +#include "lib/ultralib/src/io/viint.h" + #include "attributes.h" #include "macros.h" -#include "libc/stdbool.h" -#include "lib/ultralib/src/io/viint.h" #include "pad.h" void ViMode_LogPrint(UNUSED OSViMode* osViMode) {