Skip to content

Commit

Permalink
Small General Cleanup (#51)
Browse files Browse the repository at this point in the history
* osCreateThread Stack top

* Idle_InitVideo defines

* Remove undef

* libc stuff

* headers
  • Loading branch information
hensldm authored Jul 20, 2024
1 parent 0d88020 commit d7fd64f
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 44 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
Expand Down
6 changes: 4 additions & 2 deletions include/gfxprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 26 additions & 12 deletions include/libc/math.h
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions include/libc/stdarg.h
Original file line number Diff line number Diff line change
@@ -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 */
28 changes: 28 additions & 0 deletions include/libc/stdlib.h
Original file line number Diff line number Diff line change
@@ -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 */
3 changes: 1 addition & 2 deletions include/segment_symbols.h
Original file line number Diff line number Diff line change
@@ -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[]; \
Expand Down
7 changes: 3 additions & 4 deletions src/main/O2/65430.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/main/O2/math64.c
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
6 changes: 4 additions & 2 deletions src/main/O2/stackcheck.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/dmamgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
11 changes: 6 additions & 5 deletions src/main/fault.c
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/vimode.c
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit d7fd64f

Please sign in to comment.