diff --git a/Makefile b/Makefile index 7cdb1e35ec3..a3b65fb537e 100644 --- a/Makefile +++ b/Makefile @@ -98,7 +98,8 @@ OBJDUMP := $(MIPS_BINUTILS_PREFIX)objdump EMULATOR = mupen64plus EMU_FLAGS = --noosd -INC := -Iinclude -Isrc -Ibuild -I. +INC := -Iinclude -Iinclude/libc -Isrc -Ibuild -I. +build/src/libultra/%.o: INC := -Iinclude/libc -Iinclude/ultra64 -Isrc/libultra # Check code syntax with host compiler CHECK_WARNINGS := -Wall -Wextra -Wno-format-security -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-variable -Wno-missing-braces diff --git a/include/alloca.h b/include/alloca.h deleted file mode 100644 index 4e8720f7e8d..00000000000 --- a/include/alloca.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef ALLOCA_H -#define ALLOCA_H - -void* alloca(u32); -#define alloca __builtin_alloca - -#endif diff --git a/include/attributes.h b/include/attributes.h new file mode 100644 index 00000000000..86228368e04 --- /dev/null +++ b/include/attributes.h @@ -0,0 +1,12 @@ +#ifndef ATTRIBUTES_H +#define ATTRIBUTES_H + +#if !defined(__GNUC__) && !defined(__attribute__) +#define __attribute__(x) +#endif + +#define UNUSED __attribute__((unused)) +#define FALLTHROUGH __attribute__((fallthrough)) +#define NORETURN __attribute__((noreturn)) + +#endif diff --git a/include/fault.h b/include/fault.h index 511d3516fec..778f708e9b9 100644 --- a/include/fault.h +++ b/include/fault.h @@ -2,6 +2,7 @@ #define FAULT_H #include "ultra64.h" +#include "attributes.h" #include "padmgr.h" // These are the same as the 3-bit ansi color codes @@ -42,8 +43,8 @@ void Fault_Init(void); // Fatal Errors -void Fault_AddHungupAndCrashImpl(const char* exp1, const char* exp2); -void Fault_AddHungupAndCrash(const char* file, s32 line); +NORETURN void Fault_AddHungupAndCrashImpl(const char* exp1, const char* exp2); +NORETURN void Fault_AddHungupAndCrash(const char* file, s32 line); // Client Registration diff --git a/include/fp.h b/include/fp.h deleted file mode 100644 index c733be914c6..00000000000 --- a/include/fp.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef FP_H -#define FP_H -#include "ultra64.h" - -extern f32 qNaN0x3FFFFF; -extern f32 qNaN0x10000; -extern f32 sNaN0x3FFFFF; - -f32 floorf(f32 x); -f64 floor(f64 x); -s32 lfloorf(f32 x); -s32 lfloor(f64 x); - -f32 ceilf(f32 x); -f64 ceil(f64 x); -s32 lceilf(f32 x); -s32 lceil(f64 x); - -f32 truncf(f32 x); -f64 trunc(f64 x); -s32 ltruncf(f32 x); -s32 ltrunc(f64 x); - -f32 nearbyintf(f32 x); -f64 nearbyint(f64 x); -s32 lnearbyintf(f32 x); -s32 lnearbyint(f64 x); - -f32 roundf(f32 x); -f64 round(f64 x); -s32 lroundf(f32 x); -s32 lround(f64 x); - -#endif diff --git a/include/functions.h b/include/functions.h index e5ca798e7ae..dcfd5832300 100644 --- a/include/functions.h +++ b/include/functions.h @@ -1,20 +1,12 @@ #ifndef FUNCTIONS_H #define FUNCTIONS_H +// TODO this shouldn't be here but bss reordering currently prevents removing it +void osSyncPrintf(const char* fmt, ...); + #include "z64.h" #include "macros.h" -f32 fabsf(f32 f); -#ifndef __sgi -#define fabsf(f) __builtin_fabsf((f32)(f)) -#else -#pragma intrinsic(fabsf) -#endif -f32 sqrtf(f32 f); -#pragma intrinsic(sqrtf) -f64 sqrt(f64 f); -#pragma intrinsic(sqrt) - void cleararena(void); void bootproc(void); void Main_ThreadEntry(void* arg); @@ -30,14 +22,6 @@ void Locale_ResetRegion(void); u32 func_80001F48(void); u32 func_80001F8C(void); u32 Locale_IsRegionNative(void); -void __assert(const char* exp, const char* file, s32 line); -void isPrintfInit(void); -void osSyncPrintfUnused(const char* fmt, ...); -void osSyncPrintf(const char* fmt, ...); -void rmonPrintf(const char* fmt, ...); -void* is_proutSyncPrintf(void* arg, const char* str, u32 count); -void func_80002384(const char* exp, const char* file, u32 line); -OSPiHandle* osDriveRomInit(void); void Mio0_Decompress(Yaz0Header* hdr, u8* dst); void StackCheck_Init(StackEntry* entry, void* stackBottom, void* stackTop, u32 initValue, s32 minSpace, const char* name); @@ -57,92 +41,6 @@ void LogUtils_CheckValidPointer(const char* exp, void* ptr, const char* file, s3 void LogUtils_LogThreadId(const char* name, s32 line); void LogUtils_HungupThread(const char* name, s32 line); void LogUtils_ResetHungup(void); -s32 vsprintf(char* dst, const char* fmt, va_list args); -s32 sprintf(char* dst, const char* fmt, ...); -void __osPiCreateAccessQueue(void); -void __osPiGetAccess(void); -void __osPiRelAccess(void); -s32 osSendMesg(OSMesgQueue* mq, OSMesg msg, s32 flag); -void osStopThread(OSThread* thread); -void osViExtendVStart(u32 value); -s32 osRecvMesg(OSMesgQueue* mq, OSMesg* msg, s32 flag); -void __osInitialize_common(void); -void __osInitialize_autodetect(void); -void __osEnqueueAndYield(OSThread**); -void __osEnqueueThread(OSThread**, OSThread*); -OSThread* __osPopThread(OSThread**); -void __osDispatchThread(void); -void __osCleanupThread(void); -void __osDequeueThread(OSThread** queue, OSThread* thread); -void osDestroyThread(OSThread* thread); -void bzero(void* __s, s32 __n); -void osCreateThread(OSThread* thread, OSId id, void (*entry)(void*), void* arg, void* sp, OSPri pri); -void __osSetSR(u32); -u32 __osGetSR(void); -void osWritebackDCache(void* vaddr, s32 nbytes); -void* osViGetNextFramebuffer(void); -void osCreatePiManager(OSPri pri, OSMesgQueue* cmdQueue, OSMesg* cmdBuf, s32 cmdMsgCnt); -void __osDevMgrMain(void* arg); -s32 __osPiRawStartDma(s32 dir, u32 cartAddr, void* dramAddr, size_t size); -u32 osVirtualToPhysical(void* vaddr); -void osViBlack(u8 active); -s32 __osSiRawReadIo(void* devAddr, u32* dst); -OSId osGetThreadId(OSThread* thread); -void osViSetMode(OSViMode* mode); -u32 __osProbeTLB(void*); -u32 osGetMemSize(void); -void osSetEventMesg(OSEvent e, OSMesgQueue* mq, OSMesg msg); -s32 _Printf(PrintCallback, void* arg, const char* fmt, va_list ap); -void osUnmapTLBAll(void); -s32 osEPiStartDma(OSPiHandle* handle, OSIoMesg* mb, s32 direction); -const char* strchr(const char* str, s32 ch); -u32 strlen(const char* str); -void* memcpy(void* dst, const void* src, size_t size); -void osInvalICache(void* vaddr, s32 nbytes); -void osCreateMesgQueue(OSMesgQueue* mq, OSMesg* msg, s32 count); -void osInvalDCache(void* vaddr, s32 nbytes); -s32 __osSiDeviceBusy(void); -s32 osJamMesg(OSMesgQueue* mq, OSMesg msg, s32 flag); -void osSetThreadPri(OSThread* thread, OSPri pri); -OSPri osGetThreadPri(OSThread* thread); -s32 __osEPiRawReadIo(OSPiHandle* handle, u32 devAddr, u32* data); -void osViSwapBuffer(void* frameBufPtr); -s32 __osEPiRawStartDma(OSPiHandle* handle, s32 direction, u32 cartAddr, void* dramAddr, size_t size); -u32 bcmp(void* __sl, void* __s2, u32 __n); -OSTime osGetTime(void); -void __osTimerServicesInit(void); -void __osTimerInterrupt(void); -void __osSetTimerIntr(OSTime time); -OSTime __osInsertTimer(OSTimer* timer); -u32 osGetCount(void); -void __osSetCompare(u32); -void* bcopy(void* __src, void* __dest, u32 __n); -s32 __osDisableInt(void); -void __osRestoreInt(s32); -void __osViInit(void); -void __osViSwapContext(void); -OSMesgQueue* osPiGetCmdQueue(void); -s32 osEPiReadIo(OSPiHandle* handle, u32 devAddr, u32* data); -void osViSetSpecialFeatures(u32 func); -OSPiHandle* osCartRomInit(void); -void __osSetFpcCsr(u32); -u32 __osGetFpcCsr(void); -s32 osEPiWriteIo(OSPiHandle* handle, u32 devAddr, u32 data); -void osMapTLBRdb(void); -void osYieldThread(void); -u32 __osGetCause(void); -s32 __osEPiRawWriteIo(OSPiHandle* handle, u32 devAddr, u32 data); -void _Litob(_Pft* args, u8 type); -ldiv_t ldiv(s32 num, s32 denom); -lldiv_t lldiv(s64 num, s64 denom); -void _Ldtob(_Pft* args, u8 type); -s32 __osSiRawWriteIo(void* devAddr, u32 val); -void osCreateViManager(OSPri pri); -OSViContext* __osViGetCurrentContext(void); -void osStartThread(OSThread* thread); -void osViSetYScale(f32 scale); -void osViSetXScale(f32 value); -void __osSetWatchLo(u32); EnItem00* Item_DropCollectible(PlayState* play, Vec3f* spawnPos, s16 params); EnItem00* Item_DropCollectible2(PlayState* play, Vec3f* spawnPos, s16 params); @@ -1516,7 +1414,7 @@ u64* SysUcode_GetUCodeBoot(void); size_t SysUcode_GetUCodeBootSize(void); u64* SysUcode_GetUCode(void); u64* SysUcode_GetUCodeData(void); -void func_800D31A0(void); +NORETURN void func_800D31A0(void); void func_800D31F0(void); void func_800D3210(void); void DebugArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action); @@ -1896,111 +1794,6 @@ s32 JpegDecoder_Decode(JpegDecoder* decoder, u16* mcuBuff, s32 count, u8 isFollo s32 JpegDecoder_ProcessMcu(JpegHuffmanTable* hTable0, JpegHuffmanTable* hTable1, u16* mcu, s16* unk); s32 JpegDecoder_ParseNextSymbol(JpegHuffmanTable* hTable, s16* outCoeff, s8* outZeroCount); u16 JpegDecoder_ReadBits(u8 len); -s32 osPfsFreeBlocks(OSPfs* pfs, s32* leftoverBytes); -void guScale(Mtx* m, f32 x, f32 y, f32 z); -f32 sinf(f32 angle); -s16 sins(u16 angle); -OSTask* _VirtualToPhysicalTask(OSTask* intp); -void osSpTaskLoad(OSTask* intp); -void osSpTaskStartGo(OSTask* tp); -void __osSiCreateAccessQueue(void); -void __osSiGetAccess(void); -void __osSiRelAccess(void); -s32 osContInit(OSMesgQueue* mq, u8* ctlBitfield, OSContStatus* status); -void __osContGetInitData(u8* ctlBitfield, OSContStatus* data); -void __osPackRequestData(u8 poll); -s32 osContStartReadData(OSMesgQueue* mq); -void osContGetReadData(OSContPad* contData); -void __osPackReadData(void); -void guPerspectiveF(f32 mf[4][4], u16* perspNorm, f32 fovy, f32 aspect, f32 near, f32 far, f32 scale); -void guPerspective(Mtx* m, u16* perspNorm, f32 fovy, f32 aspect, f32 near, f32 far, f32 scale); -s32 __osSpRawStartDma(s32 direction, void* devAddr, void* dramAddr, u32 size); -s32 __osSiRawStartDma(s32 dir, void* addr); -void osSpTaskYield(void); -s32 __osPfsGetNextPage(OSPfs* pfs, u8* bank, __OSInode* inode, __OSInodeUnit* page); -s32 osPfsReadWriteFile(OSPfs* pfs, s32 fileNo, u8 flag, s32 offset, s32 size, u8* data); -s32 __osPfsGetStatus(OSMesgQueue* queue, s32 channel); -void __osPfsRequestOneChannel(s32 channel, u8 cmd); -void __osPfsGetOneChannelData(s32 channel, OSContStatus* contData); -void guMtxIdentF(f32 mf[4][4]); -void guLookAtF(f32 mf[4][4], f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f32 xUp, f32 yUp, f32 zUp); -void guLookAt(Mtx*, f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f32 xUp, f32 yUp, f32 zUp); -s32 osPfsAllocateFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName, s32 fileSize, s32* fileNo); -s32 __osPfsDeclearPage(OSPfs* pfs, __OSInode* inode, s32 fileSizeInPages, s32* startPage, u8 bank, s32* decleared, - s32* finalPage); -s32 osStopTimer(OSTimer* timer); -u16 __osSumcalc(u8* ptr, s32 length); -s32 __osIdCheckSum(u16* ptr, u16* checkSum, u16* idSum); -s32 __osRepairPackId(OSPfs* pfs, __OSPackId* badid, __OSPackId* newid); -s32 __osCheckPackId(OSPfs* pfs, __OSPackId* check); -s32 __osGetId(OSPfs* pfs); -s32 __osCheckId(OSPfs* pfs); -s32 __osPfsRWInode(OSPfs* pfs, __OSInode* inode, u8 flag, u8 bank); -void guMtxL2F(MtxF* m1, Mtx* m2); -s32 osPfsFindFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName, s32* fileNo); -s32 osAfterPreNMI(void); -s32 osContStartQuery(OSMesgQueue* mq); -void osContGetQuery(OSContStatus* data); -void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, - f32 xUp, f32 yUp, f32 zUp, f32 xl1, f32 yl1, f32 zl1, f32 xl2, f32 yl2, f32 zl2, s32 hiliteWidth, - s32 hiliteHeight); -void guLookAtHilite(Mtx* m, LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f32 xUp, - f32 yUp, f32 zUp, f32 xl1, f32 yl1, f32 zl1, f32 xl2, f32 yl2, f32 zl2, s32 hiliteWidth, - s32 hiliteHeight); -u32 __osSpDeviceBusy(void); -void guMtxIdent(Mtx*); -void guPositionF(f32 mf[4][4], f32 rot, f32 pitch, f32 yaw, f32 scale, f32 x, f32 y, f32 z); -void guPosition(Mtx*, f32, f32, f32, f32, f32, f32, f32); -OSYieldResult osSpTaskYielded(OSTask* task); -void guRotateF(f32 m[4][4], f32 a, f32 x, f32 y, f32 z); -void guRotate(Mtx*, f32 a, f32 x, f32 y, f32 z); -s32 osAiSetFrequency(u32 frequency); -OSThread* __osGetActiveQueue(void); -void guNormalize(f32* x, f32* y, f32* z); -u32 osDpGetStatus(void); -void osDpSetStatus(u32 status); -s32 osPfsDeleteFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName); -s32 __osPfsReleasePages(OSPfs* pfs, __OSInode* inode, u8 initialPage, u8 bank, __OSInodeUnit* finalPage); -void guOrthoF(f32[4][4], f32, f32, f32, f32, f32, f32, f32); -void guOrtho(Mtx*, f32, f32, f32, f32, f32, f32, f32); -f32 cosf(f32 angle); -s16 coss(u16 angle); -void osViSetEvent(OSMesgQueue* mq, OSMesg msg, u32 retraceCount); -s32 osPfsIsPlug(OSMesgQueue* mq, u8* pattern); -void __osPfsRequestData(u8 cmd); -void __osPfsGetInitData(u8* pattern, OSContStatus* contData); -void guS2DInitBg(uObjBg* bg); -s32 __osPfsSelectBank(OSPfs* pfs, u8 bank); -s32 osContSetCh(u8 ch); -s32 osPfsFileState(OSPfs* pfs, s32 fileNo, OSPfsState* state); -s32 osPfsInitPak(OSMesgQueue* queue, OSPfs* pfs, s32 channel); -s32 __osPfsCheckRamArea(OSPfs* pfs); -s32 osPfsChecker(OSPfs* pfs); -s32 func_80105788(OSPfs* pfs, __OSInodeCache* cache); -s32 func_80105A60(OSPfs* pfs, __OSInodeUnit fpage, __OSInodeCache* cache); -u32 osAiGetLength(void); -void guTranslate(Mtx* m, f32 x, f32 y, f32 z); -s32 __osContRamWrite(OSMesgQueue* mq, s32 channel, u16 address, u8* buffer, s32 force); -s32 __osContRamRead(OSMesgQueue* ctrlrqueue, s32 channel, u16 addr, u8* data); -u8 __osContAddressCrc(u16 addr); -u8 __osContDataCrc(u8* data); -s32 osSetTimer(OSTimer* timer, OSTime countdown, OSTime interval, OSMesgQueue* mq, OSMesg msg); -u32 __osSpGetStatus(void); -void __osSpSetStatus(u32 status); -void osWritebackDCacheAll(void); -OSThread* __osGetCurrFaultedThread(void); -void guMtxF2L(MtxF* m1, Mtx* m2); -// ? __d_to_ll(?); -// ? __f_to_ll(?); -// ? __d_to_ull(?); -// ? __f_to_ull(?); -// ? __ll_to_d(?); -// ? __ll_to_f(?); -// ? __ull_to_d(?); -// ? __ull_to_f(?); -void* osViGetCurrentFramebuffer(void); -s32 __osSpSetPc(void* pc); -f32 absf(f32); void* __osMemset(void* dest, s32 val, size_t len); void* __osMemmove(void* dest, const void* src, size_t len); void Message_UpdateOcarinaMemoryGame(PlayState* play); diff --git a/include/is_debug.h b/include/is_debug.h new file mode 100644 index 00000000000..a0bab99185f --- /dev/null +++ b/include/is_debug.h @@ -0,0 +1,12 @@ +#ifndef ISVIEWER_H +#define ISVIEWER_H + +#include "ultra64.h" +#include "attributes.h" + +void isPrintfInit(void); + +void isPrintf(const char* fmt, ...); +NORETURN void isAssertFail(const char* exp, const char* file, int line); + +#endif diff --git a/include/libc/alloca.h b/include/libc/alloca.h new file mode 100644 index 00000000000..abecea942e6 --- /dev/null +++ b/include/libc/alloca.h @@ -0,0 +1,9 @@ +#ifndef ALLOCA_H +#define ALLOCA_H + +#include "stddef.h" + +void* alloca(size_t); +#define alloca __builtin_alloca + +#endif diff --git a/include/libc/assert.h b/include/libc/assert.h new file mode 100644 index 00000000000..64d99d252a6 --- /dev/null +++ b/include/libc/assert.h @@ -0,0 +1,41 @@ +#ifndef ASSERT_H +#define ASSERT_H + +#if !defined(__GNUC__) && !defined(__attribute__) +#define __attribute__(x) +#endif + +// Runtime assertions + +__attribute__((noreturn)) void __assert(const char* assertion, const char* file, int line); + +#ifndef NDEBUG +# ifndef NON_MATCHING +# define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(msg, file, line)) +# else +// macro for matching +# define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(#cond, __FILE__, __LINE__)) +// standard assert macro +# define assert(cond) ASSERT(cond, ., ., .) +# endif +#else +# define ASSERT(cond, msg, file, line) ((void)0) +# define assert(cond) ((void)0) +#endif + +// Static/compile-time assertions + +#if defined(__GNUC__) || (__STDC_VERSION__ >= 201112L) +# define static_assert(cond, msg) _Static_assert(cond, msg) +#else +# ifndef GLUE +# define GLUE(a, b) a##b +# endif +# ifndef GLUE2 +# define GLUE2(a, b) GLUE(a, b) +# endif + +# define static_assert(cond, msg) typedef char GLUE2(static_assertion_failed, __LINE__)[(cond) ? 1 : -1] +#endif + +#endif diff --git a/include/libc/math.h b/include/libc/math.h index 2858084eba9..da1b169e4cd 100644 --- a/include/libc/math.h +++ b/include/libc/math.h @@ -1,28 +1,39 @@ #ifndef MATH_H #define MATH_H -#include "ultra64/ultratypes.h" - #define M_PI 3.14159265358979323846f #define M_SQRT2 1.41421356237309504880f -#define FLT_MAX 340282346638528859811704183484516925440.0f +#define MAXFLOAT 3.40282347e+38f #define SHT_MAX 32767.0f #define SHT_MINV (1.0f / SHT_MAX) -typedef union { - struct { - u32 hi; - u32 lo; - } word; +float floorf(float); +double floor(double); +long lfloorf(float); +long lfloor(double); + +float ceilf(float); +double ceil(double); +long lceilf(float); +long lceil(double); + +float truncf(float); +double trunc(double); +long ltruncf(float); +long ltrunc(double); - f64 d; -} du; +float nearbyintf(float); +double nearbyint(double); +long lnearbyintf(float); +long lnearbyint(double); -typedef union { - u32 i; - f32 f; -} fu; +float roundf(float); +double round(double); +long lroundf(float); +long lround(double); -extern f32 __libm_qnan_f; +extern float qNaN0x3FFFFF; +extern float qNaN0x10000; +extern float sNaN0x3FFFFF; #endif diff --git a/include/libc/stdarg.h b/include/libc/stdarg.h index 0744dfa609d..a9d8a8567d7 100644 --- a/include/libc/stdarg.h +++ b/include/libc/stdarg.h @@ -1,8 +1,7 @@ #ifndef STDARG_H #define STDARG_H -// When building with GCC, use the official vaarg macros to avoid warnings -// and possibly bad codegen. +// 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 @@ -10,33 +9,44 @@ #define va_end __builtin_va_end #else +#ifndef _VA_LIST_ +#define _VA_LIST_ typedef char* va_list; -#define _FP 1 +#endif + #define _INT 0 +#define _FP 1 #define _STRUCT 2 #define _VA_FP_SAVE_AREA 0x10 -#define _VA_ALIGN(p, a) (((u32)(((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) \ - ( \ - (((s32)list & 0x1) /* 1 byte aligned? */ \ - ? (list = (char*)((s32)list + 7), (char*)((s32)list - 6 - _VA_FP_SAVE_AREA)) \ - : (((s32)list & 0x2) /* 2 byte aligned? */ \ - ? (list = (char*)((s32)list + 10), (char*)((s32)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(f64)) \ - ? __va_double_arg(list, mode) \ - : __va_stack_arg(list, mode))))[-1] + +#define _VA_ALIGN(p, a) (((unsigned int)(((char*)p) + ((a) > 4 ? (a) : 4) - 1)) & -((a) > 4 ? (a) : 4)) + +#define va_start(list, parmN) (list = ((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] + +/* No cleanup processing is required for the end of a varargs list: */ #define va_end(__list) #endif diff --git a/include/libc/stddef.h b/include/libc/stddef.h index d277a260378..64986c034f2 100644 --- a/include/libc/stddef.h +++ b/include/libc/stddef.h @@ -3,7 +3,15 @@ #define NULL ((void*)0) -typedef unsigned long size_t; +#if !defined(_SIZE_T) && !defined(_SIZE_T_) +#define _SIZE_T +#if (_MIPS_SZLONG == 32) +typedef unsigned int size_t; +#endif +#if (_MIPS_SZLONG == 64) +typedef unsigned long size_t; +#endif +#endif #ifdef __GNUC__ #define offsetof(structure, member) __builtin_offsetof (structure, member) diff --git a/include/libc/stdio.h b/include/libc/stdio.h new file mode 100644 index 00000000000..ce918161451 --- /dev/null +++ b/include/libc/stdio.h @@ -0,0 +1,9 @@ +#ifndef STDIO_H +#define STDIO_H + +#include "stdarg.h" + +int sprintf(char* dst, const char* fmt, ...); +int vsprintf(char* dst, const char* fmt, va_list args); + +#endif diff --git a/include/libc/stdlib.h b/include/libc/stdlib.h index 75657d8dbd1..632fd25ae31 100644 --- a/include/libc/stdlib.h +++ b/include/libc/stdlib.h @@ -1,16 +1,17 @@ #ifndef STDLIB_H #define STDLIB_H -#include "ultra64.h" - typedef struct lldiv_t { - s64 quot; - s64 rem; + long long quot; + long long rem; } lldiv_t; typedef struct ldiv_t { - s32 quot; - s32 rem; + long quot; + long rem; } ldiv_t; +ldiv_t ldiv(long num, long denom); +lldiv_t lldiv(long long num, long long denom); + #endif diff --git a/include/libc/string.h b/include/libc/string.h new file mode 100644 index 00000000000..3f9a5cee71d --- /dev/null +++ b/include/libc/string.h @@ -0,0 +1,11 @@ +#ifndef STRING_H +#define STRING_H + +#include "stddef.h" + +char* strchr(const char*, int); +size_t strlen(const char*); + +void* memcpy(void*, const void*, size_t); + +#endif diff --git a/include/macros.h b/include/macros.h index fc0615266ac..b51ce3687e5 100644 --- a/include/macros.h +++ b/include/macros.h @@ -1,9 +1,7 @@ #ifndef MACROS_H #define MACROS_H -#ifndef __GNUC__ -#define __attribute__(x) -#endif +#include "attributes.h" #ifndef AVOID_UB #define BAD_RETURN(type) type @@ -11,9 +9,6 @@ #define BAD_RETURN(type) void #endif -#define UNUSED __attribute__((unused)) -#define FALLTHROUGH __attribute__((fallthrough)) - #define ARRAY_COUNT(arr) (s32)(sizeof(arr) / sizeof(arr[0])) #define ARRAY_COUNTU(arr) (u32)(sizeof(arr) / sizeof(arr[0])) @@ -139,8 +134,6 @@ } \ (void)0 -struct GraphicsContext; - extern struct GraphicsContext* __gfxCtx; #define WORK_DISP __gfxCtx->work.p @@ -150,12 +143,12 @@ extern struct GraphicsContext* __gfxCtx; // __gfxCtx shouldn't be used directly. // Use the DISP macros defined above when writing to display buffers. -#define OPEN_DISPS(gfxCtx, file, line) \ - { \ - GraphicsContext* __gfxCtx; \ - Gfx* dispRefs[4]; \ - __gfxCtx = gfxCtx; \ - (void)__gfxCtx; \ +#define OPEN_DISPS(gfxCtx, file, line) \ + { \ + struct GraphicsContext* __gfxCtx; \ + Gfx* dispRefs[4]; \ + __gfxCtx = gfxCtx; \ + (void)__gfxCtx; \ Graph_OpenDisps(dispRefs, gfxCtx, file, line) #define CLOSE_DISPS(gfxCtx, file, line) \ @@ -178,14 +171,6 @@ extern struct GraphicsContext* __gfxCtx; #define VTX_T(x,y,z,s,t,cr,cg,cb,a) { { x, y, z }, 0, { s, t }, { cr, cg, cb, a } } -#ifdef NDEBUG -#define ASSERT(cond, msg, file, line) ((void)0) -#elif defined(REAL_ASSERT_MACRO) -#define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(#cond, __FILE__, __LINE__)) -#else -#define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(msg, file, line)) -#endif - #define gDPSetTileCustom(pkt, fmt, siz, width, height, pal, cms, cmt, masks, maskt, shifts, shiftt) \ do { \ gDPPipeSync(pkt); \ diff --git a/include/message_data_fmt.h b/include/message_data_fmt.h index 7ec967b503a..447117d81fb 100644 --- a/include/message_data_fmt.h +++ b/include/message_data_fmt.h @@ -7,7 +7,9 @@ * The strings are used in the message_data_static files themselves, as you can only concat strings with other strings */ +#ifndef GLUE #define GLUE(a, b) a##b +#endif #define STRINGIFY(s) #s #define EXPAND_AND_STRINGIFY(s) STRINGIFY(s) diff --git a/include/stack.h b/include/stack.h new file mode 100644 index 00000000000..be61f9a86b8 --- /dev/null +++ b/include/stack.h @@ -0,0 +1,13 @@ +#ifndef STACK_H +#define STACK_H + +#include "ultra64/ultratypes.h" +#include "alignment.h" + +#define STACK(stack, size) \ + u64 stack[ALIGN8(size) / sizeof(u64)] + +#define STACK_TOP(stack) \ + ((u8*)(stack) + sizeof(stack)) + +#endif diff --git a/include/ultra64.h b/include/ultra64.h index 6eadfb6af49..23db82ce277 100644 --- a/include/ultra64.h +++ b/include/ultra64.h @@ -4,29 +4,40 @@ #include "ultra64/ultratypes.h" #include "unk.h" +#include "libc/assert.h" +#include "libc/math.h" #include "libc/stdarg.h" #include "libc/stdbool.h" #include "libc/stddef.h" #include "libc/stdint.h" +#include "libc/stdio.h" #include "libc/stdlib.h" -#include "libc/math.h" +#include "libc/string.h" +#include "ultra64/libc.h" +#include "ultra64/rmon.h" #include "ultra64/exception.h" +#include "ultra64/system.h" +#include "ultra64/internal_reg.h" #include "ultra64/rcp.h" +#include "ultra64/message.h" #include "ultra64/thread.h" +#include "ultra64/tlb.h" +#include "ultra64/cache.h" #include "ultra64/convert.h" #include "ultra64/time.h" -#include "ultra64/message.h" #include "ultra64/sptask.h" #include "ultra64/gu.h" #include "ultra64/vi.h" #include "ultra64/pi.h" #include "ultra64/controller.h" -#include "ultra64/printf.h" +#include "ultra64/xstdio.h" #include "ultra64/mbi.h" #include "ultra64/pfs.h" #include "ultra64/motor.h" #include "ultra64/R4300.h" #include "ultra64/ucode.h" +#include "ultra64/internal.h" + #endif diff --git a/include/ultra64/ai.h b/include/ultra64/ai.h new file mode 100644 index 00000000000..2b21f6bc871 --- /dev/null +++ b/include/ultra64/ai.h @@ -0,0 +1,10 @@ +#ifndef ULTRA64_AI_H +#define ULTRA64_AI_H + +#include "ultratypes.h" + +u32 osAiGetLength(void); +s32 osAiSetFrequency(u32); +s32 osAiSetNextBuffer(void*, u32); + +#endif diff --git a/include/ultra64/cache.h b/include/ultra64/cache.h new file mode 100644 index 00000000000..6f98521ec0d --- /dev/null +++ b/include/ultra64/cache.h @@ -0,0 +1,14 @@ +#ifndef ULTRA64_CACHE_H +#define ULTRA64_CACHE_H + +#include "ultratypes.h" + +#define OS_DCACHE_ROUNDUP_ADDR(x) (void*)(((((u32)(x) + 0xf) / 0x10) * 0x10)) +#define OS_DCACHE_ROUNDUP_SIZE(x) (u32)(((((u32)(x) + 0xf) / 0x10) * 0x10)) + +void osInvalDCache(void*, s32); +void osInvalICache(void*, s32); +void osWritebackDCache(void*, s32); +void osWritebackDCacheAll(void); + +#endif diff --git a/include/ultra64/cont.h b/include/ultra64/cont.h new file mode 100644 index 00000000000..7e5314420ff --- /dev/null +++ b/include/ultra64/cont.h @@ -0,0 +1,52 @@ +#ifndef ULTRA64_CONT_H +#define ULTRA64_CONT_H + +#include "message.h" + +/* Buttons */ +#define BTN_CRIGHT 0x0001 +#define BTN_CLEFT 0x0002 +#define BTN_CDOWN 0x0004 +#define BTN_CUP 0x0008 +#define BTN_R 0x0010 +#define BTN_L 0x0020 +#define BTN_DRIGHT 0x0100 +#define BTN_DLEFT 0x0200 +#define BTN_DDOWN 0x0400 +#define BTN_DUP 0x0800 +#define BTN_START 0x1000 +#define BTN_Z 0x2000 +#define BTN_B 0x4000 +#define BTN_A 0x8000 + +typedef struct { + /* 0x00 */ u16 type; + /* 0x02 */ u8 status; + /* 0x03 */ u8 errno; +} OSContStatus; // size = 0x04 + +typedef struct { + /* 0x00 */ u16 button; + /* 0x02 */ s8 stick_x; + /* 0x03 */ s8 stick_y; + /* 0x04 */ u8 errno; +} OSContPad; // size = 0x06 + +typedef struct { + /* 0x00 */ void* address; + /* 0x04 */ u8 databuffer[32]; + /* 0x24 */ u8 addressCrc; + /* 0x25 */ u8 dataCrc; + /* 0x26 */ u8 errno; +} OSContRamIo; // size = 0x28 + +s32 osContInit(OSMesgQueue*, u8*, OSContStatus*); +s32 osContStartQuery(OSMesgQueue*); +s32 osContStartReadData(OSMesgQueue*); +#ifndef _HW_VERSION_1 +s32 osContSetCh(u8); +#endif +void osContGetQuery(OSContStatus*); +void osContGetReadData(OSContPad*); + +#endif diff --git a/include/ultra64/controller.h b/include/ultra64/controller.h index 8b759db77fe..03230977f66 100644 --- a/include/ultra64/controller.h +++ b/include/ultra64/controller.h @@ -1,7 +1,8 @@ #ifndef ULTRA64_CONTROLLER_H #define ULTRA64_CONTROLLER_H -#include "message.h" +#include "internal.h" +#include "rcp.h" #define CHNL_ERR(readFormat) (((readFormat).rxsize & CHNL_ERR_MASK) >> 4) @@ -49,7 +50,6 @@ #define CONT_ERR_VOICE_WORD 14 #define CONT_ERR_VOICE_NO_RESPONSE 15 - #define DIR_STATUS_EMPTY 0 #define DIR_STATUS_UNKNOWN 1 #define DIR_STATUS_OCCUPIED 2 @@ -103,22 +103,6 @@ #define CONT_BLOCK_GB_BANK CONT_BLOCKS(CONT_ADDR_GB_BANK) #define CONT_BLOCK_GB_STATUS CONT_BLOCKS(CONT_ADDR_GB_STATUS) -/* Buttons */ -#define BTN_CRIGHT 0x0001 -#define BTN_CLEFT 0x0002 -#define BTN_CDOWN 0x0004 -#define BTN_CUP 0x0008 -#define BTN_R 0x0010 -#define BTN_L 0x0020 -#define BTN_DRIGHT 0x0100 -#define BTN_DLEFT 0x0200 -#define BTN_DDOWN 0x0400 -#define BTN_DUP 0x0800 -#define BTN_START 0x1000 -#define BTN_Z 0x2000 -#define BTN_B 0x4000 -#define BTN_A 0x8000 - typedef union { struct { /* 0x00 */ u32 ram[15]; @@ -127,27 +111,6 @@ typedef union { u64 force_structure_alignment; } OSPifRam; // size = 0x40 -typedef struct { - /* 0x00 */ u16 type; - /* 0x02 */ u8 status; - /* 0x03 */ u8 errno; -} OSContStatus; // size = 0x04 - -typedef struct { - /* 0x00 */ u16 button; - /* 0x02 */ s8 stick_x; - /* 0x03 */ s8 stick_y; - /* 0x04 */ u8 errno; -} OSContPad; // size = 0x06 - -typedef struct { - /* 0x00 */ void* address; - /* 0x04 */ u8 databuffer[32]; - /* 0x24 */ u8 addressCrc; - /* 0x25 */ u8 dataCrc; - /* 0x26 */ u8 errno; -} OSContRamIo; // size = 0x28 - typedef struct { /* 0x00 */ u8 align; /* 0x01 */ u8 txsize; @@ -191,4 +154,30 @@ typedef struct { /* 0x07 */ s8 joyY; } __OSContReadFormat; // size = 0x8 +s32 __osPfsDeclearPage(OSPfs* pfs, __OSInode* inode, s32 fileSizeInPages, s32* startPage, u8 bank, s32* decleared, + s32* finalPage); +s32 __osPfsGetStatus(OSMesgQueue* queue, s32 channel); +void __osPackRequestData(u8 poll); +void __osContGetInitData(u8* ctlBitfield, OSContStatus* data); +u16 __osSumcalc(u8* ptr, s32 length); +s32 __osIdCheckSum(u16* ptr, u16* checkSum, u16* idSum); +s32 __osRepairPackId(OSPfs* pfs, __OSPackId* badid, __OSPackId* newid); +s32 __osCheckPackId(OSPfs* pfs, __OSPackId* check); +s32 __osGetId(OSPfs* pfs); +s32 __osCheckId(OSPfs* pfs); +s32 __osPfsRWInode(OSPfs* pfs, __OSInode* inode, u8 flag, u8 bank); +s32 __osPfsReleasePages(OSPfs* pfs, __OSInode* inode, u8 initialPage, u8 bank, __OSInodeUnit* finalPage); +void __osPfsRequestData(u8 cmd); +void __osPfsGetInitData(u8* pattern, OSContStatus* contData); +s32 __osPfsSelectBank(OSPfs* pfs, u8 bank); +s32 __osContRamWrite(OSMesgQueue* mq, s32 channel, u16 address, u8* buffer, s32 force); +s32 __osContRamRead(OSMesgQueue* ctrlrqueue, s32 channel, u16 addr, u8* data); +u8 __osContAddressCrc(u16 addr); +u8 __osContDataCrc(u8* data); + +extern OSPifRam __osContPifRam; +extern u8 __osContLastCmd; +extern u8 __osMaxControllers; +extern OSPifRam __osPfsPifRam; + #endif diff --git a/include/ultra64/convert.h b/include/ultra64/convert.h index 02024d17fa7..55b70fc2374 100644 --- a/include/ultra64/convert.h +++ b/include/ultra64/convert.h @@ -1,6 +1,8 @@ #ifndef ULTRA64_CONVERT_H #define ULTRA64_CONVERT_H +#include "ultratypes.h" + #define OS_CLOCK_RATE 62500000LL #define OS_CPU_COUNTER (OS_CLOCK_RATE*3/4) @@ -18,4 +20,6 @@ #define OS_MSEC_TO_CYCLES(n) OS_USEC_TO_CYCLES((n) * 1000) #define OS_SEC_TO_CYCLES(n) OS_MSEC_TO_CYCLES((n) * 1000) +u32 osVirtualToPhysical(void* vaddr); + #endif diff --git a/include/ultra64/exception.h b/include/ultra64/exception.h index 7d4977f4f92..32a03fa8e7c 100644 --- a/include/ultra64/exception.h +++ b/include/ultra64/exception.h @@ -33,15 +33,15 @@ typedef u32 OSIntMask; typedef u32 OSHWIntr; -typedef struct { - /* 0x00 */ void* callback; - /* 0x04 */ void* sp; +typedef struct { + /* 0x00 */ s32 (*handler)(void); + /* 0x04 */ void* stackEnd; } __osHwInt; // size = 0x08 OSIntMask osSetIntMask(OSIntMask); -void __osSetHWIntrRoutine(OSHWIntr intr, s32 (*callback)(void), void* sp); -void __osGetHWIntrRoutine(OSHWIntr intr, s32 (**callbackOut)(void), void** spOut); +void __osSetHWIntrRoutine(OSHWIntr interrupt, s32 (*handler)(void), void* stackEnd); +void __osGetHWIntrRoutine(OSHWIntr interrupt, s32 (**handler)(void), void** stackEnd); void __osSetGlobalIntMask(OSHWIntr mask); void __osResetGlobalIntMask(OSHWIntr mask); @@ -51,11 +51,11 @@ extern __osHwInt __osHwIntTable[]; #else // __osHwInt struct member offsets -#define HWINT_CALLBACK 0x00 -#define HWINT_SP 0x04 +#define HWINT_HANDLER 0x00 +#define HWINT_STACK 0x04 // __osHwInt struct size -#define HWINT_SIZE 0x8 +#define HWINT_SIZE 0x8 #endif diff --git a/include/ultra64/gu.h b/include/ultra64/gu.h index 8fc9e0291ef..81c022ebfd3 100644 --- a/include/ultra64/gu.h +++ b/include/ultra64/gu.h @@ -1,8 +1,51 @@ #ifndef ULTRA64_GU_H #define ULTRA64_GU_H +#include "ultratypes.h" +#include "mbi.h" + #define GU_PI 3.1415926 #define ROUND(x) (s32)(((x) >= 0.0) ? ((x) + 0.5) : ((x) - 0.5)) +void guMtxIdent(Mtx*); +void guPositionF(f32 mf[4][4], f32 rot, f32 pitch, f32 yaw, f32 scale, f32 x, f32 y, f32 z); +void guPosition(Mtx*, f32, f32, f32, f32, f32, f32, f32); +void guRotateF(f32 m[4][4], f32 a, f32 x, f32 y, f32 z); +void guRotate(Mtx*, f32 a, f32 x, f32 y, f32 z); +void guNormalize(f32* x, f32* y, f32* z); +void guOrthoF(f32[4][4], f32, f32, f32, f32, f32, f32, f32); +void guOrtho(Mtx*, f32, f32, f32, f32, f32, f32, f32); +void guScale(Mtx* m, f32 x, f32 y, f32 z); +void guTranslate(Mtx* m, f32 x, f32 y, f32 z); +void guPerspectiveF(f32 mf[4][4], u16* perspNorm, f32 fovy, f32 aspect, f32 near, f32 far, f32 scale); +void guPerspective(Mtx* m, u16* perspNorm, f32 fovy, f32 aspect, f32 near, f32 far, f32 scale); +void guMtxIdentF(f32 mf[4][4]); +void guLookAtF(f32 mf[4][4], f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f32 xUp, f32 yUp, f32 zUp); +void guLookAt(Mtx*, f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f32 xUp, f32 yUp, f32 zUp); +void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, + f32 xUp, f32 yUp, f32 zUp, f32 xl1, f32 yl1, f32 zl1, f32 xl2, f32 yl2, f32 zl2, s32 hiliteWidth, + s32 hiliteHeight); +void guLookAtHilite(Mtx* m, LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f32 xUp, + f32 yUp, f32 zUp, f32 xl1, f32 yl1, f32 zl1, f32 xl2, f32 yl2, f32 zl2, s32 hiliteWidth, + s32 hiliteHeight); +void guMtxL2F(f32 mf[4][4], Mtx* m2); +void guMtxF2L(f32 mf[4][4], Mtx* m2); +f32 cosf(f32 angle); +s16 coss(u16 angle); +f32 sinf(f32 angle); +s16 sins(u16 angle); + +float absf(float); +float fabsf(float f); +#ifndef __sgi +#define fabsf(f) __builtin_fabsf((float)(f)) +#else +#pragma intrinsic(fabsf) +#endif +float sqrtf(float f); +#pragma intrinsic(sqrtf) +double sqrt(double f); +#pragma intrinsic(sqrt) + #endif diff --git a/include/ultra64/guint.h b/include/ultra64/guint.h new file mode 100644 index 00000000000..3e376415231 --- /dev/null +++ b/include/ultra64/guint.h @@ -0,0 +1,27 @@ +#ifndef ULTRA64_GUINT_H +#define ULTRA64_GUINT_H + +#include "ultratypes.h" +#include "gu.h" + +typedef union { + struct { + u32 hi; + u32 lo; + } word; + + f64 d; +} du; + +typedef union { + u32 i; + f32 f; +} fu; + +#ifndef SQ +#define SQ(x) ((x)*(x)) +#endif + +extern f32 __libm_qnan_f; + +#endif diff --git a/include/ultra64/internal.h b/include/ultra64/internal.h index 0851314a8b9..476975ad5f4 100644 --- a/include/ultra64/internal.h +++ b/include/ultra64/internal.h @@ -1,24 +1,20 @@ #ifndef ULTRA64_INTERNAL_H #define ULTRA64_INTERNAL_H -#include "pi.h" +#include "../libc/math.h" +#include "../libc/stdarg.h" +#include "../libc/stdbool.h" +#include "../libc/stddef.h" +#include "../libc/stdint.h" +#include "../libc/stdlib.h" +#include "../libc/string.h" -typedef struct { - /* 0x00 */ u32 active; - /* 0x04 */ OSThread* thread; - /* 0x08 */ OSMesgQueue* cmdQueue; - /* 0x0C */ OSMesgQueue* evtQueue; - /* 0x10 */ OSMesgQueue* acsQueue; - /* 0x14 */ s32 (*dma)(s32, u32, void*, size_t); - /* 0x18 */ s32 (*edma)(OSPiHandle*, s32, u32, void*, size_t); -} OSDevMgr; // size = 0x1C +#include "os.h" -typedef struct { - /* 0x00 */ OSMesgQueue* queue; - /* 0x04 */ OSMesg msg; -} __OSEventState; // size = 0x08 - -extern OSDevMgr __osPiDevMgr; -extern __OSEventState __osEventStateTab[]; +#include "internal_error.h" +#include "internal_reg.h" +#include "internal_thread.h" +#include "internal_si.h" +#include "internal_rsp.h" #endif diff --git a/include/ultra64/internal_error.h b/include/ultra64/internal_error.h new file mode 100644 index 00000000000..835cb6bd814 --- /dev/null +++ b/include/ultra64/internal_error.h @@ -0,0 +1,8 @@ +#ifndef ULTRA64_INTERNAL_ERROR +#define ULTRA64_INTERNAL_ERROR + +#include "thread.h" + +OSThread* __osGetCurrFaultedThread(void); + +#endif diff --git a/include/ultra64/internal_reg.h b/include/ultra64/internal_reg.h new file mode 100644 index 00000000000..c4dc97dc4ba --- /dev/null +++ b/include/ultra64/internal_reg.h @@ -0,0 +1,17 @@ +#ifndef ULTRA64_INTERNAL_REG +#define ULTRA64_INTERNAL_REG + +#include "ultratypes.h" + +void __osSetSR(u32); +u32 __osGetSR(void); +void __osSetWatchLo(u32); +void __osSetCompare(u32); +void __osSetFpcCsr(u32); +u32 __osGetFpcCsr(void); +u32 __osGetCause(void); +u32 osGetCount(void); +u32 __osDisableInt(void); +void __osRestoreInt(u32); + +#endif diff --git a/include/ultra64/internal_rsp.h b/include/ultra64/internal_rsp.h new file mode 100644 index 00000000000..205932e415e --- /dev/null +++ b/include/ultra64/internal_rsp.h @@ -0,0 +1,13 @@ +#ifndef ULTRA64_RSP_H +#define ULTRA64_RSP_H + +#include "ultratypes.h" + +u32 __osSpGetStatus(void); +void __osSpSetStatus(u32); +s32 __osSpSetPc(u32); +s32 __osSpRawWriteIo(u32, u32); +s32 __osSpRawReadIo(u32, u32*); +s32 __osSpRawStartDma(s32, u32, void*, u32); + +#endif diff --git a/include/ultra64/internal_si.h b/include/ultra64/internal_si.h new file mode 100644 index 00000000000..444f0c79737 --- /dev/null +++ b/include/ultra64/internal_si.h @@ -0,0 +1,10 @@ +#ifndef ULTRA64_INTERNAL_SI +#define ULTRA64_INTERNAL_SI + +#include "ultratypes.h" + +s32 __osSiRawWriteIo(u32, u32); +s32 __osSiRawReadIo(u32, u32*); +s32 __osSiRawStartDma(s32, void*); + +#endif diff --git a/include/ultra64/internal_thread.h b/include/ultra64/internal_thread.h new file mode 100644 index 00000000000..89f07b8a403 --- /dev/null +++ b/include/ultra64/internal_thread.h @@ -0,0 +1,8 @@ +#ifndef ULTRA64_INTERNAL_THREAD +#define ULTRA64_INTERNAL_THREAD + +#include "thread.h" + +OSThread* __osGetActiveQueue(void); + +#endif diff --git a/include/ultra64/libc.h b/include/ultra64/libc.h new file mode 100644 index 00000000000..31898a96714 --- /dev/null +++ b/include/ultra64/libc.h @@ -0,0 +1,12 @@ +#ifndef ULTRA64_LIBC_H +#define ULTRA64_LIBC_H + +#include "stddef.h" + +void osSyncPrintf(const char* fmt, ...); + +void bzero(void* __s, size_t __n); +int bcmp(const void* __sl, const void* __s2, size_t __n); +void bcopy(const void* __src, void* __dest, size_t __n); + +#endif diff --git a/include/ultra64/message.h b/include/ultra64/message.h index bf93c524c90..cba04cb3617 100644 --- a/include/ultra64/message.h +++ b/include/ultra64/message.h @@ -45,6 +45,15 @@ typedef struct OSMesgQueue { #define MQ_IS_EMPTY(mq) (MQ_GET_COUNT(mq) == 0) #define MQ_IS_FULL(mq) (MQ_GET_COUNT(mq) >= (mq)->msgCount) +/* Message operations */ +void osCreateMesgQueue(OSMesgQueue*, OSMesg*, s32); +s32 osSendMesg(OSMesgQueue*, OSMesg, s32); +s32 osJamMesg(OSMesgQueue*, OSMesg, s32); +s32 osRecvMesg(OSMesgQueue*, OSMesg*, s32); + +/* Event operations */ +void osSetEventMesg(OSEvent, OSMesgQueue*, OSMesg); + #else // OSMesgQueue struct member offsets diff --git a/include/ultra64/os.h b/include/ultra64/os.h new file mode 100644 index 00000000000..a3c3d9e33a0 --- /dev/null +++ b/include/ultra64/os.h @@ -0,0 +1,25 @@ +#ifndef ULTRA64_OS_H +#define ULTRA64_OS_H + +#include "ultratypes.h" +#include "libc.h" +#include "message.h" +#include "thread.h" +#include "time.h" +#include "exception.h" +#include "pi.h" +#include "ai.h" +#include "vi.h" +#include "tlb.h" +#include "rdp.h" +#include "cache.h" +#include "gu.h" +#include "cont.h" +#include "convert.h" +#include "system.h" +#include "motor.h" + +#define OS_PIM_STACKSIZE 0x1000 +#define OS_VIM_STACKSIZE 0x1000 + +#endif diff --git a/include/ultra64/osint.h b/include/ultra64/osint.h new file mode 100644 index 00000000000..a68213b2740 --- /dev/null +++ b/include/ultra64/osint.h @@ -0,0 +1,42 @@ +#ifndef ULTRA64_OSINT_H +#define ULTRA64_OSINT_H + +#include "internal.h" + +void __osTimerServicesInit(void); +void __osTimerInterrupt(void); +void __osSetTimerIntr(OSTime); +OSTime __osInsertTimer(OSTimer*); + +s32 __osSpDeviceBusy(void); + +u32 __osProbeTLB(void*); + +typedef struct { + OSThread* next; + OSPri priority; +} __OSThreadTail; // size = 0x8 + +typedef struct { + /* 0x00 */ OSMesgQueue* queue; + /* 0x04 */ OSMesg msg; +} __OSEventState; // size = 0x08 + +extern __OSEventState __osEventStateTab[OS_NUM_EVENTS]; + +extern u32 __osShutdown; +extern s32 osViClock; + +extern __OSThreadTail __osThreadTail; +extern OSThread* __osRunQueue; +extern OSThread* __osActiveQueue; +extern OSThread* __osRunningThread; +extern OSThread* __osFaultedThread; +extern OSTimer* __osTimerList; + +extern u32 __osBaseCounter; +extern u32 __osViIntrCount; +extern u32 __osTimerCounter; +extern OSTime __osCurrentTime; + +#endif diff --git a/include/ultra64/pfs.h b/include/ultra64/pfs.h index 019f29350c7..b791caac281 100644 --- a/include/ultra64/pfs.h +++ b/include/ultra64/pfs.h @@ -134,4 +134,14 @@ typedef struct { /* 0x101 */ u8 map[PFS_INODE_DIST_MAP]; } __OSInodeCache; // size = 0x202 +s32 osPfsFreeBlocks(OSPfs* pfs, s32* leftoverBytes); +s32 osPfsReadWriteFile(OSPfs* pfs, s32 fileNo, u8 flag, s32 offset, s32 size, u8* data); +s32 osPfsAllocateFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName, s32 fileSize, s32* fileNo); +s32 osPfsFindFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName, s32* fileNo); +s32 osPfsDeleteFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName); +s32 osPfsIsPlug(OSMesgQueue* mq, u8* pattern); +s32 osPfsFileState(OSPfs* pfs, s32 fileNo, OSPfsState* state); +s32 osPfsInitPak(OSMesgQueue* queue, OSPfs* pfs, s32 channel); +s32 osPfsChecker(OSPfs* pfs); + #endif diff --git a/include/ultra64/pi.h b/include/ultra64/pi.h index 6fb52cea70a..eb81e541120 100644 --- a/include/ultra64/pi.h +++ b/include/ultra64/pi.h @@ -56,22 +56,58 @@ typedef struct { /* 0x14 */ OSPiHandle* piHandle; } OSIoMesg; // size = 0x18 +typedef struct { + /* 0x00 */ u32 active; + /* 0x04 */ OSThread* thread; + /* 0x08 */ OSMesgQueue* cmdQueue; + /* 0x0C */ OSMesgQueue* evtQueue; + /* 0x10 */ OSMesgQueue* acsQueue; + /* 0x14 */ s32 (*dma)(s32, u32, void*, size_t); + /* 0x18 */ s32 (*edma)(OSPiHandle*, s32, u32, void*, size_t); +} OSDevMgr; // size = 0x1C + +extern OSPiHandle* __osPiTable; + +/* + * Flags to indicate direction of data transfer + */ #define OS_READ 0 // device -> RDRAM #define OS_WRITE 1 // device <- RDRAM #define OS_OTHERS 2 // for disk drive transfers +/* + * I/O message types + */ +#define OS_MESG_TYPE_BASE (10) +#define OS_MESG_TYPE_LOOPBACK (OS_MESG_TYPE_BASE + 0) +#define OS_MESG_TYPE_DMAREAD (OS_MESG_TYPE_BASE + 1) +#define OS_MESG_TYPE_DMAWRITE (OS_MESG_TYPE_BASE + 2) +#define OS_MESG_TYPE_VRETRACE (OS_MESG_TYPE_BASE + 3) +#define OS_MESG_TYPE_COUNTER (OS_MESG_TYPE_BASE + 4) +#define OS_MESG_TYPE_EDMAREAD (OS_MESG_TYPE_BASE + 5) +#define OS_MESG_TYPE_EDMAWRITE (OS_MESG_TYPE_BASE + 6) + +/* + * I/O message priority + */ +#define OS_MESG_PRI_NORMAL 0 +#define OS_MESG_PRI_HIGH 1 + +/* + * PI/EPI + */ #define PI_DOMAIN1 0 #define PI_DOMAIN2 1 -#define OS_MESG_TYPE_LOOPBACK 10 -#define OS_MESG_TYPE_DMAREAD 11 -#define OS_MESG_TYPE_DMAWRITE 12 -#define OS_MESG_TYPE_VRETRACE 13 -#define OS_MESG_TYPE_COUNTER 14 -#define OS_MESG_TYPE_EDMAREAD 15 -#define OS_MESG_TYPE_EDMAWRITE 16 +void osCreatePiManager(OSPri, OSMesgQueue*, OSMesg*, s32); -#define OS_MESG_PRI_NORMAL 0 -#define OS_MESG_PRI_HIGH 1 +/* Enhanced PI interface */ + +OSPiHandle *osCartRomInit(void); +OSPiHandle *osDriveRomInit(void); + +s32 osEPiWriteIo(OSPiHandle*, u32 , u32); +s32 osEPiReadIo(OSPiHandle*, u32 , u32*); +s32 osEPiStartDma(OSPiHandle*, OSIoMesg*, s32); #endif diff --git a/include/ultra64/piint.h b/include/ultra64/piint.h new file mode 100644 index 00000000000..f4500d1fbab --- /dev/null +++ b/include/ultra64/piint.h @@ -0,0 +1,28 @@ +#ifndef ULTRA64_PIINT_H +#define ULTRA64_PIINT_H + +#include "internal.h" +#include "rcp.h" + +void __osDevMgrMain(void*); + +OSMesgQueue* osPiGetCmdQueue(void); + +void __osPiCreateAccessQueue(void); +void __osPiGetAccess(void); +void __osPiRelAccess(void); + +s32 __osPiRawStartDma(s32, u32, void*, size_t); +s32 __osEPiRawReadIo(OSPiHandle*, u32, u32*); +s32 __osEPiRawWriteIo(OSPiHandle*, u32, u32); +s32 __osEPiRawStartDma(OSPiHandle*, s32, u32, void*, size_t); + +extern OSDevMgr __osPiDevMgr; +extern OSMesgQueue __osPiAccessQueue; +extern u32 __osPiAccessQueueEnabled; +extern OSPiHandle* __osCurrentHandle[2]; + +extern OSPiHandle __Dom1SpeedParam; +extern OSPiHandle __Dom2SpeedParam; + +#endif diff --git a/include/ultra64/printf.h b/include/ultra64/printf.h deleted file mode 100644 index 4f2e29730a1..00000000000 --- a/include/ultra64/printf.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef ULTRA64_PRINTF_H -#define ULTRA64_PRINTF_H - -#include "ultratypes.h" - -typedef struct { - /* 0x0 */ union { - /* 0x0 */ s64 ll; - /* 0x0 */ f64 ld; - } v; - /* 0x8 */ char* s; - /* 0xC */ s32 n0; - /* 0x10 */ s32 nz0; - /* 0x14 */ s32 n1; - /* 0x18 */ s32 nz1; - /* 0x1C */ s32 n2; - /* 0x20 */ s32 nz2; - /* 0x24 */ s32 prec; - /* 0x28 */ s32 width; - /* 0x2C */ u32 nchar; - /* 0x30 */ u32 flags; - /* 0x34 */ u8 qual; -} _Pft; // size = 0x38 - -typedef void* (*PrintCallback)(void*, const char*, u32); - -#define FLAGS_SPACE 1 -#define FLAGS_PLUS 2 -#define FLAGS_MINUS 4 -#define FLAGS_HASH 8 -#define FLAGS_ZERO 16 - -#endif diff --git a/include/ultra64/rcp.h b/include/ultra64/rcp.h index 86387ef5e83..1d3988d1d3f 100644 --- a/include/ultra64/rcp.h +++ b/include/ultra64/rcp.h @@ -1,8 +1,8 @@ #ifndef ULTRA64_RCP_H #define ULTRA64_RCP_H -#include "R4300.h" #include "ultratypes.h" +#include "R4300.h" /** * RCP memory map overview: diff --git a/include/ultra64/rdp.h b/include/ultra64/rdp.h new file mode 100644 index 00000000000..01b92a79ba6 --- /dev/null +++ b/include/ultra64/rdp.h @@ -0,0 +1,9 @@ +#ifndef ULTRA64_RDP_H +#define ULTRA64_RDP_H + +#include "ultratypes.h" + +u32 osDpGetStatus(void); +void osDpSetStatus(u32); + +#endif diff --git a/include/ultra64/rmon.h b/include/ultra64/rmon.h new file mode 100644 index 00000000000..0a232ab29dc --- /dev/null +++ b/include/ultra64/rmon.h @@ -0,0 +1,6 @@ +#ifndef ULTRA64_RMON_H +#define ULTRA64_RMON_H + +void rmonPrintf(const char* fmt, ...); + +#endif diff --git a/include/ultra64/siint.h b/include/ultra64/siint.h new file mode 100644 index 00000000000..861e4adbd7b --- /dev/null +++ b/include/ultra64/siint.h @@ -0,0 +1,16 @@ +#ifndef ULTRA64_SIINT_H +#define ULTRA64_SIINT_H + +#include "internal.h" +#include "rcp.h" + +extern u8 __osPfsInodeCacheBank; +extern s32 __osPfsLastChannel; + +void __osSiGetAccess(void); +void __osSiRelAccess(void); +void __osSiCreateAccessQueue(void); + +s32 __osSiDeviceBusy(void); + +#endif diff --git a/include/ultra64/sptask.h b/include/ultra64/sptask.h index ea00dbfdf53..c95b0f8a43e 100644 --- a/include/ultra64/sptask.h +++ b/include/ultra64/sptask.h @@ -61,8 +61,14 @@ typedef union { typedef u32 OSYieldResult; +void osSpTaskLoad(OSTask*); +void osSpTaskStartGo(OSTask*); + #define osSpTaskStart(p) \ osSpTaskLoad(p); \ osSpTaskStartGo(p); +void osSpTaskYield(void); +OSYieldResult osSpTaskYielded(OSTask*); + #endif diff --git a/include/ultra64/system.h b/include/ultra64/system.h new file mode 100644 index 00000000000..dcba40805f9 --- /dev/null +++ b/include/ultra64/system.h @@ -0,0 +1,48 @@ +#ifndef ULTRA64_SYSTEM_H +#define ULTRA64_SYSTEM_H + +#include "ultratypes.h" +#include "exception.h" + +#define OS_TV_PAL 0 +#define OS_TV_NTSC 1 +#define OS_TV_MPAL 2 + +#define OS_APP_NMI_BUFSIZE 64 + +#if defined(_LANGUAGE_C) || defined(_LANGUAGE_C_PLUS_PLUS) + +extern s32 osRomType; /* Bulk or cartridge ROM. 0=cartridge 1=bulk */ +extern void* osRomBase; /* Rom base address of the game image */ +extern s32 osTvType; /* 0 = PAL, 1 = NTSC, 2 = MPAL */ +extern s32 osResetType; /* 0 = cold reset, 1 = NMI */ +extern s32 osCicId; +extern s32 osVersion; +extern u32 osMemSize; /* Memory Size */ +extern s32 osAppNMIBuffer[OS_APP_NMI_BUFSIZE / sizeof(s32)]; + +/* + * CPU counter increments at 3/4 of bus clock rate: + * + * Bus Clock PClock Counter (1/2 PClock) + * --------- ---------- ------------------------ + * 62.5 Mhz 93.75 Mhz 46.875 Mhz + */ +extern u64 osClockRate; + +extern OSIntMask __OSGlobalIntMask; + +#define osInitialize() \ + __osInitialize_common(); \ + __osInitialize_autodetect() + +void __osInitialize_common(void); +void __osInitialize_autodetect(void); + +u32 osGetMemSize(void); + +s32 osAfterPreNMI(void); + +#endif + +#endif diff --git a/include/ultra64/thread.h b/include/ultra64/thread.h index 474a63641a2..18543233a8e 100644 --- a/include/ultra64/thread.h +++ b/include/ultra64/thread.h @@ -66,10 +66,20 @@ typedef struct OSThread { /* 0x20 */ __OSThreadContext context; } OSThread; // size = 0x1B0 -typedef struct { - OSThread* next; - OSPri priority; -} __OSThreadTail; // size = 0x8 +void osStopThread(OSThread* thread); +OSId osGetThreadId(OSThread* thread); +void osSetThreadPri(OSThread* thread, OSPri pri); +OSPri osGetThreadPri(OSThread* thread); +void osYieldThread(void); +void osStartThread(OSThread* thread); +void osCreateThread(OSThread* thread, OSId id, void (*entry)(void*), void* arg, void* sp, OSPri pri); +void __osEnqueueAndYield(OSThread**); +void __osEnqueueThread(OSThread**, OSThread*); +OSThread* __osPopThread(OSThread**); +void __osDispatchThread(void); +void __osCleanupThread(void); +void __osDequeueThread(OSThread** queue, OSThread* thread); +void osDestroyThread(OSThread* thread); #else diff --git a/include/ultra64/time.h b/include/ultra64/time.h index 5c6650b7d23..10238526085 100644 --- a/include/ultra64/time.h +++ b/include/ultra64/time.h @@ -1,6 +1,7 @@ #ifndef ULTRA64_TIME_H #define ULTRA64_TIME_H +#include "ultratypes.h" #include "message.h" typedef u64 OSTime; @@ -14,4 +15,9 @@ typedef struct OSTimer { /* 0x1C */ OSMesg msg; } OSTimer; // size = 0x20 +OSTime osGetTime(void); +void osSetTime(OSTime); +s32 osSetTimer(OSTimer*, OSTime, OSTime, OSMesgQueue*, OSMesg); +s32 osStopTimer(OSTimer*); + #endif diff --git a/include/ultra64/tlb.h b/include/ultra64/tlb.h new file mode 100644 index 00000000000..249b37cc58f --- /dev/null +++ b/include/ultra64/tlb.h @@ -0,0 +1,9 @@ +#ifndef ULTRA64_TLB_H +#define ULTRA64_TLB_H + +#include "ultratypes.h" + +void osMapTLBRdb(void); +void osUnmapTLBAll(void); + +#endif diff --git a/include/ultra64/ultratypes.h b/include/ultra64/ultratypes.h index e4a30537023..2f843ea4d68 100644 --- a/include/ultra64/ultratypes.h +++ b/include/ultra64/ultratypes.h @@ -3,6 +3,10 @@ #ifdef _LANGUAGE_C +#include "stdbool.h" +#include "stddef.h" +#include "stdint.h" + typedef signed char s8; typedef unsigned char u8; typedef signed short int s16; @@ -24,20 +28,6 @@ typedef volatile s64 vs64; typedef float f32; typedef double f64; - -typedef float MtxF_t[4][4]; -typedef union { - MtxF_t mf; - struct { - // Note: The order displayed here is the transpose of the order in which matrices are typically written. - // For example, [xw, yw, zw] is the translation part of the matrix, not [wx, wy, wz]. - float xx, yx, zx, wx, - xy, yy, zy, wy, - xz, yz, zz, wz, - xw, yw, zw, ww; - }; -} MtxF; - #endif #endif diff --git a/include/ultra64/vi.h b/include/ultra64/vi.h index 51254611237..f4096dfdb67 100644 --- a/include/ultra64/vi.h +++ b/include/ultra64/vi.h @@ -1,6 +1,7 @@ #ifndef ULTRA64_VI_H #define ULTRA64_VI_H +#include "ultratypes.h" #include "message.h" /* Special Features */ @@ -117,8 +118,21 @@ typedef struct { #define OS_VI_FPAL_HPN2 54 #define OS_VI_FPAL_HPF2 55 -#define OS_TV_PAL 0 -#define OS_TV_NTSC 1 -#define OS_TV_MPAL 2 +void* osViGetCurrentFramebuffer(void); +void* osViGetNextFramebuffer(void); +void osViSetXScale(f32); +void osViSetYScale(f32); +void osViExtendVStart(u32); +void osViSetSpecialFeatures(u32); +void osViSetMode(OSViMode*); +void osViSetEvent(OSMesgQueue*, OSMesg, u32); +void osViSwapBuffer(void*); +void osViBlack(u8); +void osCreateViManager(OSPri); + +extern OSViMode osViModeNtscLan1; +extern OSViMode osViModePalLan1; +extern OSViMode osViModeMpalLan1; +extern OSViMode osViModeFpalLan1; #endif diff --git a/include/ultra64/viint.h b/include/ultra64/viint.h index 4fea2259a2a..3709d153b79 100644 --- a/include/ultra64/viint.h +++ b/include/ultra64/viint.h @@ -1,6 +1,9 @@ #ifndef ULTRA64_VIINT_H #define ULTRA64_VIINT_H +#include "internal.h" +#include "rcp.h" + #define VI_STATE_MODE_SET (1 << 0) #define VI_STATE_XSCALE_SET (1 << 1) #define VI_STATE_YSCALE_FACTOR_SET (1 << 2) @@ -34,4 +37,12 @@ #define VINTR(v) v #define HSTART START +void __osViInit(void); +void __osViSwapContext(void); +OSViContext* __osViGetCurrentContext(void); + +extern OSViContext* __osViCurr; +extern OSViContext* __osViNext; +extern u32 __additional_scanline; + #endif diff --git a/include/ultra64/xstdio.h b/include/ultra64/xstdio.h new file mode 100644 index 00000000000..5dffdf69b65 --- /dev/null +++ b/include/ultra64/xstdio.h @@ -0,0 +1,44 @@ +#ifndef ULTRA64_PRINTF_H +#define ULTRA64_PRINTF_H + +#include "stdarg.h" + +// IDO doesn't support long double types, improve portability for compilers supporting them +#ifdef __sgi +#define LONG_DOUBLE_TYPE double +#else +#define LONG_DOUBLE_TYPE long double +#endif + +typedef struct { + /* 0x00 */ union { + long long ll; + LONG_DOUBLE_TYPE ld; + } v; + /* 0x08 */ char* s; + /* 0x0C */ int n0; + /* 0x10 */ int nz0; + /* 0x14 */ int n1; + /* 0x18 */ int nz1; + /* 0x1C */ int n2; + /* 0x20 */ int nz2; + /* 0x24 */ int prec; + /* 0x28 */ int width; + /* 0x2C */ size_t nchar; + /* 0x30 */ unsigned int flags; + /* 0x34 */ char qual; +} _Pft; // size = 0x38 + +typedef void* (*PrintCallback)(void*, const char*, size_t); + +int _Printf(PrintCallback, void* arg, const char* fmt, va_list ap); +void _Litob(_Pft* args, char code); +void _Ldtob(_Pft* args, char code); + +#define FLAGS_SPACE 1 +#define FLAGS_PLUS 2 +#define FLAGS_MINUS 4 +#define FLAGS_HASH 8 +#define FLAGS_ZERO 16 + +#endif diff --git a/include/variables.h b/include/variables.h index 03d9fa47c45..1790c1dfc12 100644 --- a/include/variables.h +++ b/include/variables.h @@ -6,51 +6,19 @@ extern Mtx D_01000000; -extern u32 osTvType; -extern u32 osRomBase; -extern u32 osResetType; -extern u32 osMemSize; -extern u8 osAppNMIBuffer[0x40]; - extern s8 D_80009430; +extern OSMesgQueue gPiMgrCmdQueue; +extern OSViMode gViConfigMode; +extern u8 gViConfigModeType; extern vu8 gViConfigBlack; extern u8 gViConfigAdditionalScanLines; extern u32 gViConfigFeatures; extern f32 gViConfigXScale; extern f32 gViConfigYScale; extern OSPiHandle* gCartHandle; -extern u32 __osPiAccessQueueEnabled; -extern OSViMode osViModePalLan1; -extern s32 osViClock; -extern u32 __osShutdown; -extern OSHWIntr __OSGlobalIntMask; -extern __OSThreadTail __osThreadTail; -extern OSThread* __osRunQueue; -extern OSThread* __osActiveQueue; -extern OSThread* __osRunningThread; -extern OSThread* __osFaultedThread; -extern OSPiHandle* __osPiTable; -extern OSPiHandle* __osCurrentHandle[]; -extern OSTimer* __osTimerList; -extern OSViMode osViModeNtscLan1; -extern OSViMode osViModeMpalLan1; -extern OSViContext* __osViCurr; -extern OSViContext* __osViNext; -extern OSViMode osViModeFpalLan1; -extern u32 __additional_scanline; extern u8 gBuildTeam[]; extern u8 gBuildDate[]; extern u8 gBuildMakeOption[]; -extern OSMesgQueue gPiMgrCmdQueue; -extern OSViMode gViConfigMode; -extern u8 gViConfigModeType; -extern OSMesgQueue __osPiAccessQueue; -extern OSPiHandle __Dom1SpeedParam; -extern OSPiHandle __Dom2SpeedParam; -extern OSTime __osCurrentTime; -extern u32 __osBaseCounter; -extern u32 __osViIntrCount; -extern u32 __osTimerCounter; extern EffectSsOverlay gEffectSsOverlayTable[EFFECT_SS_TYPE_MAX]; extern Gfx D_80116280[]; extern ActorOverlay gActorOverlayTable[ACTOR_ID_MAX]; // original name: "actor_dlftbls" 801162A0 @@ -154,8 +122,6 @@ extern u8 D_80133418; extern AudioSpec gAudioSpecs[18]; extern s32 gOverlayLogSeverity; extern s32 gSystemArenaLogSeverity; -extern u8 __osPfsInodeCacheBank; -extern s32 __osPfsLastChannel; extern const s16 D_8014A6C0[]; #define gTatumsPerBeat (D_8014A6C0[1]) @@ -217,11 +183,7 @@ extern void(*D_801755D0)(void); extern u32 __osMalloc_FreeBlockTest_Enable; extern Arena gSystemArena; -extern OSPifRam __osContPifRam; -extern u8 __osContLastCmd; -extern u8 __osMaxControllers; -extern __OSInode __osPfsInodeCache; -extern OSPifRam __osPfsPifRam; + extern u16 gZBuffer[SCREEN_HEIGHT][SCREEN_WIDTH]; // 0x25800 bytes extern u64 gGfxSPTaskOutputBuffer[0x3000]; // 0x18000 bytes extern u64 gGfxSPTaskYieldBuffer[OS_YIELD_DATA_SIZE / sizeof(u64)]; // 0xC00 bytes diff --git a/include/z64.h b/include/z64.h index 289641a522d..afebacf1072 100644 --- a/include/z64.h +++ b/include/z64.h @@ -3,6 +3,7 @@ #include "ultra64.h" #include "ultra64/gs2dex.h" +#include "stack.h" #include "z64save.h" #include "z64light.h" #include "z64bgcheck.h" @@ -75,12 +76,6 @@ #define THREAD_ID_DMAMGR 18 #define THREAD_ID_IRQMGR 19 -#define STACK(stack, size) \ - u64 stack[ALIGN8(size) / sizeof(u64)] - -#define STACK_TOP(stack) \ - ((u8*)(stack) + sizeof(stack)) - // Texture memory size, 4 KiB #define TMEM_SIZE 0x1000 @@ -1455,15 +1450,6 @@ typedef enum { STACK_STATUS_OVERFLOW = 2 } StackStatus; -typedef struct { - /* 0x00 */ u32 magic; // IS64 - /* 0x04 */ u32 get; - /* 0x08 */ u8 unk_08[0x14-0x08]; - /* 0x14 */ u32 put; - /* 0x18 */ u8 unk_18[0x20-0x18]; - /* 0x20 */ u8 data[0x10000-0x20]; -} ISVDbg; - typedef struct { /* 0x00 */ char name[0x18]; /* 0x18 */ u32 mediaFormat; diff --git a/include/z64math.h b/include/z64math.h index 6661a8dc226..5beb9771849 100644 --- a/include/z64math.h +++ b/include/z64math.h @@ -3,6 +3,19 @@ #include "ultra64.h" +typedef f32 MtxF_t[4][4]; +typedef union { + MtxF_t mf; + struct { + // Note: The order displayed here is the transpose of the order in which matrices are typically written. + // For example, [xw, yw, zw] is the translation part of the matrix, not [wx, wy, wz]. + f32 xx, yx, zx, wx, + xy, yy, zy, wy, + xz, yz, zz, wz, + xw, yw, zw, ww; + }; +} MtxF; + #define VEC_SET(V,X,Y,Z) (V).x=(X);(V).y=(Y);(V).z=(Z) typedef struct { diff --git a/spec b/spec index 5ab17d2ee17..4bce5e26303 100644 --- a/spec +++ b/spec @@ -26,7 +26,7 @@ beginseg include "build/src/boot/mio0.o" include "build/src/boot/stackcheck.o" include "build/src/boot/logutils.o" - include "build/src/libultra/rmon/sprintf.o" + include "build/src/libultra/libc/sprintf.o" include "build/src/libultra/io/piacs.o" include "build/src/libultra/os/sendmesg.o" include "build/src/libultra/os/stopthread.o" @@ -57,7 +57,7 @@ beginseg include "build/src/libultra/os/probetlb.o" include "build/src/libultra/os/getmemsize.o" include "build/src/libultra/os/seteventmesg.o" - include "build/src/libultra/rmon/xprintf.o" + include "build/src/libultra/libc/xprintf.o" include "build/src/libultra/os/unmaptlball.o" include "build/src/libultra/io/epidma.o" include "build/src/libultra/libc/string.o" @@ -96,9 +96,9 @@ beginseg include "build/src/libultra/os/yieldthread.o" include "build/src/libultra/os/getcause.o" include "build/src/libultra/io/epirawwrite.o" - include "build/src/libultra/rmon/xlitob.o" + include "build/src/libultra/libc/xlitob.o" include "build/src/libultra/libc/ldiv.o" - include "build/src/libultra/rmon/xldtob.o" + include "build/src/libultra/libc/xldtob.o" include "build/src/boot/build.o" include "build/src/libultra/io/sirawwrite.o" include "build/src/libultra/io/vimgr.o" diff --git a/src/boot/assert.c b/src/boot/assert.c index b7895ddddae..7097a6dc207 100644 --- a/src/boot/assert.c +++ b/src/boot/assert.c @@ -1,9 +1,9 @@ -#include "global.h" +#include "fault.h" -void __assert(const char* exp, const char* file, s32 line) { +NORETURN void __assert(const char* assertion, const char* file, int line) { char msg[256]; - osSyncPrintf("Assertion failed: %s, file %s, line %d, thread %d\n", exp, file, line, osGetThreadId(NULL)); + osSyncPrintf("Assertion failed: %s, file %s, line %d, thread %d\n", assertion, file, line, osGetThreadId(NULL)); sprintf(msg, "ASSERT: %s:%d(%d)", file, line, osGetThreadId(NULL)); - Fault_AddHungupAndCrashImpl(msg, exp); + Fault_AddHungupAndCrashImpl(msg, assertion); } diff --git a/src/boot/boot_main.c b/src/boot/boot_main.c index fdba964e80f..b0be12c07c2 100644 --- a/src/boot/boot_main.c +++ b/src/boot/boot_main.c @@ -1,4 +1,5 @@ #include "global.h" +#include "is_debug.h" #include "boot.h" StackEntry sBootThreadInfo; @@ -16,8 +17,7 @@ void bootproc(void) { osMemSize = osGetMemSize(); cleararena(); - __osInitialize_common(); - __osInitialize_autodetect(); + osInitialize(); gCartHandle = osCartRomInit(); osDriveRomInit(); diff --git a/src/boot/is_debug.c b/src/boot/is_debug.c index c064c43a300..0868d8ca47c 100644 --- a/src/boot/is_debug.c +++ b/src/boot/is_debug.c @@ -1,18 +1,27 @@ -#include "global.h" +#include "is_debug.h" -OSPiHandle* sISVHandle; // official name : is_Handle +#define ISV_BASE PHYS_TO_K1(PI_DOM1_ADDR2 | 0x03FF0000) +#define ISV_MAGIC_ADDR (ISV_BASE + 0x00) +#define ISV_GET_ADDR (ISV_BASE + 0x04) +#define ISV_PUT_ADDR (ISV_BASE + 0x14) +#define ISV_BUFFER (ISV_BASE + 0x20) + +#define ISV_BUFFER_LEN (0x10000 - 0x20) -#define gISVDbgPrnAdrs ((ISVDbg*)0xB3FF0000) -#define ASCII_TO_U32(a, b, c, d) ((u32)((a << 24) | (b << 16) | (c << 8) | (d << 0))) +#define ISV_MAGIC_VAUE (('I' << 24) | ('S' << 16) | ('6' << 8) | ('4' << 0)) + +OSPiHandle* sISVHandle; // official name : is_Handle void isPrintfInit(void) { sISVHandle = osCartRomInit(); - osEPiWriteIo(sISVHandle, (u32)&gISVDbgPrnAdrs->put, 0); - osEPiWriteIo(sISVHandle, (u32)&gISVDbgPrnAdrs->get, 0); - osEPiWriteIo(sISVHandle, (u32)&gISVDbgPrnAdrs->magic, ASCII_TO_U32('I', 'S', '6', '4')); + osEPiWriteIo(sISVHandle, ISV_PUT_ADDR, 0); + osEPiWriteIo(sISVHandle, ISV_GET_ADDR, 0); + osEPiWriteIo(sISVHandle, ISV_MAGIC_ADDR, ISV_MAGIC_VAUE); } -void osSyncPrintfUnused(const char* fmt, ...) { +void* is_proutSyncPrintf(void* arg, const char* str, size_t count); + +void isPrintf(const char* fmt, ...) { va_list args; va_start(args, fmt); @@ -30,7 +39,6 @@ void osSyncPrintf(const char* fmt, ...) { va_end(args); } -// assumption void rmonPrintf(const char* fmt, ...) { va_list args; va_start(args, fmt); @@ -40,23 +48,24 @@ void rmonPrintf(const char* fmt, ...) { va_end(args); } -void* is_proutSyncPrintf(void* arg, const char* str, u32 count) { +void* is_proutSyncPrintf(void* arg, const char* str, size_t count) { u32 data; s32 pos; s32 start; s32 end; - osEPiReadIo(sISVHandle, (u32)&gISVDbgPrnAdrs->magic, &data); - if (data != ASCII_TO_U32('I', 'S', '6', '4')) { + osEPiReadIo(sISVHandle, ISV_MAGIC_ADDR, &data); + if (data != ISV_MAGIC_VAUE) { return (void*)1; } - osEPiReadIo(sISVHandle, (u32)&gISVDbgPrnAdrs->get, &data); + osEPiReadIo(sISVHandle, ISV_GET_ADDR, &data); pos = data; - osEPiReadIo(sISVHandle, (u32)&gISVDbgPrnAdrs->put, &data); + osEPiReadIo(sISVHandle, ISV_PUT_ADDR, &data); start = data; + end = start + count; - if (end >= 0xFFE0) { - end -= 0xFFE0; + if (end >= ISV_BUFFER_LEN) { + end -= ISV_BUFFER_LEN; if (pos < end || start < pos) { return (void*)1; } @@ -65,28 +74,28 @@ void* is_proutSyncPrintf(void* arg, const char* str, u32 count) { return (void*)1; } } + while (count) { - u32 addr = (u32)&gISVDbgPrnAdrs->data + (start & 0xFFFFFFC); - s32 shift = ((3 - (start & 3)) * 8); + u32 addr = ISV_BUFFER + (start & 0xFFFFFFC); + u32 shift = (3 - (start & 3)) * 8; if (*str) { osEPiReadIo(sISVHandle, addr, &data); osEPiWriteIo(sISVHandle, addr, (*str << shift) | (data & ~(0xFF << shift))); start++; - if (start >= 0xFFE0) { - start -= 0xFFE0; + if (start >= ISV_BUFFER_LEN) { + start -= ISV_BUFFER_LEN; } } count--; str++; } - osEPiWriteIo(sISVHandle, (u32)&gISVDbgPrnAdrs->put, start); - + osEPiWriteIo(sISVHandle, ISV_PUT_ADDR, start); return (void*)1; } -void func_80002384(const char* exp, const char* file, u32 line) { +NORETURN void isAssertFail(const char* exp, const char* file, int line) { osSyncPrintf("File:%s Line:%d %s \n", file, line, exp); while (true) { ; diff --git a/src/boot/z_std_dma.c b/src/boot/z_std_dma.c index 3b6320b85ae..e68b03806a4 100644 --- a/src/boot/z_std_dma.c +++ b/src/boot/z_std_dma.c @@ -231,7 +231,7 @@ void DmaMgr_DmaFromDriveRom(void* ram, uintptr_t rom, size_t size) { * * This function does not return. */ -void DmaMgr_Error(DmaRequest* req, const char* file, const char* errorName, const char* errorDesc) { +NORETURN void DmaMgr_Error(DmaRequest* req, const char* file, const char* errorName, const char* errorDesc) { uintptr_t vrom = req->vromAddr; void* ram = req->dramAddr; size_t size = req->size; diff --git a/src/code/code_800D31A0.c b/src/code/code_800D31A0.c index 6bfa18d97c5..a6540c429bd 100644 --- a/src/code/code_800D31A0.c +++ b/src/code/code_800D31A0.c @@ -3,7 +3,7 @@ u32 gIsCtrlr2Valid = false; -void func_800D31A0(void) { +NORETURN void func_800D31A0(void) { osSyncPrintf(VT_FGCOL(RED) "\n**** Freeze!! ****\n" VT_RST); while (true) { Sleep_Msec(1000); diff --git a/src/code/code_800EC960.c b/src/code/code_800EC960.c index fb4fd25fe1a..3e8c9790494 100644 --- a/src/code/code_800EC960.c +++ b/src/code/code_800EC960.c @@ -1,4 +1,3 @@ -#include "ultra64.h" #include "global.h" #define Audio_DisableSeq(seqPlayerIndex, fadeOut) Audio_QueueCmdS32(0x83000000 | ((u8)seqPlayerIndex << 16), fadeOut) diff --git a/src/code/code_800FCE80.c b/src/code/code_800FCE80.c index 3d73123da07..17b2bc4e574 100644 --- a/src/code/code_800FCE80.c +++ b/src/code/code_800FCE80.c @@ -1,5 +1,4 @@ #include "global.h" -#include "fp.h" s32 gUseAtanContFrac; diff --git a/src/code/fault.c b/src/code/fault.c index 4a302d9f3be..51bce0921b4 100644 --- a/src/code/fault.c +++ b/src/code/fault.c @@ -1295,19 +1295,23 @@ void Fault_HungupFaultClient(const char* exp1, const char* exp2) { * error occurs. The parameters specify two messages detailing the error, one * or both may be NULL. */ -void Fault_AddHungupAndCrashImpl(const char* exp1, const char* exp2) { +NORETURN void Fault_AddHungupAndCrashImpl(const char* exp1, const char* exp2) { FaultClient client; s32 pad; Fault_AddClient(&client, Fault_HungupFaultClient, (void*)exp1, (void*)exp2); *(u32*)0x11111111 = 0; // trigger an exception via unaligned memory access +#ifdef __GNUC__ + // since the above code triggers an exception this function does not return + __builtin_unreachable(); +#endif } /** * Like `Fault_AddHungupAndCrashImpl`, however provides a fixed message containing * filename and line number */ -void Fault_AddHungupAndCrash(const char* file, s32 line) { +NORETURN void Fault_AddHungupAndCrash(const char* file, s32 line) { char msg[256]; sprintf(msg, "HungUp %s:%d", file, line); diff --git a/src/code/fault_drawer.c b/src/code/fault_drawer.c index 49bcdd68989..c6479687544 100644 --- a/src/code/fault_drawer.c +++ b/src/code/fault_drawer.c @@ -234,7 +234,7 @@ void FaultDrawer_FillScreen(void) { FaultDrawer_SetCursor(sFaultDrawer.xStart, sFaultDrawer.yStart); } -void* FaultDrawer_PrintCallback(void* arg, const char* str, u32 count) { +void* FaultDrawer_PrintCallback(void* arg, const char* str, size_t count) { for (; count != 0; count--, str++) { s32 curXStart; s32 curXEnd; diff --git a/src/code/game.c b/src/code/game.c index ae22e6ab36e..72bb20be3c3 100644 --- a/src/code/game.c +++ b/src/code/game.c @@ -265,22 +265,22 @@ void GameState_Update(GameState* gameState) { gfxCtx->xScale = gViConfigXScale; gfxCtx->yScale = gViConfigYScale; - if (SREG(63) == 6 || (SREG(63) == 2u && osTvType == OS_TV_NTSC)) { + if (SREG(63) == 6 || (SREG(63) == 2u && (u32)osTvType == OS_TV_NTSC)) { gfxCtx->viMode = &osViModeNtscLan1; gfxCtx->yScale = 1.0f; } - if (SREG(63) == 5 || (SREG(63) == 2u && osTvType == OS_TV_MPAL)) { + if (SREG(63) == 5 || (SREG(63) == 2u && (u32)osTvType == OS_TV_MPAL)) { gfxCtx->viMode = &osViModeMpalLan1; gfxCtx->yScale = 1.0f; } - if (SREG(63) == 4 || (SREG(63) == 2u && osTvType == OS_TV_PAL)) { + if (SREG(63) == 4 || (SREG(63) == 2u && (u32)osTvType == OS_TV_PAL)) { gfxCtx->viMode = &osViModePalLan1; gfxCtx->yScale = 1.0f; } - if (SREG(63) == 3 || (SREG(63) == 2u && osTvType == OS_TV_PAL)) { + if (SREG(63) == 3 || (SREG(63) == 2u && (u32)osTvType == OS_TV_PAL)) { gfxCtx->viMode = &osViModeFpalLan1; gfxCtx->yScale = 0.833f; } diff --git a/src/code/gfxprint.c b/src/code/gfxprint.c index 7cd7a2a243f..391e129a7cf 100644 --- a/src/code/gfxprint.c +++ b/src/code/gfxprint.c @@ -285,9 +285,9 @@ void GfxPrint_PrintChar(GfxPrint* this, u8 c) { } } -void GfxPrint_PrintStringWithSize(GfxPrint* this, const void* buffer, u32 charSize, u32 charCount) { +void GfxPrint_PrintStringWithSize(GfxPrint* this, const void* buffer, u32 charSize, size_t charCount) { const char* str = (const char*)buffer; - u32 count = charSize * charCount; + size_t count = charSize * charCount; while (count != 0) { GfxPrint_PrintChar(this, *(str++)); @@ -301,7 +301,7 @@ void GfxPrint_PrintString(GfxPrint* this, const char* str) { } } -void* GfxPrint_Callback(void* arg, const char* str, u32 size) { +void* GfxPrint_Callback(void* arg, const char* str, size_t size) { GfxPrint* this = arg; GfxPrint_PrintStringWithSize(this, str, sizeof(char), size); diff --git a/src/code/load.c b/src/code/load.c index 888f2bdc98a..a4f9eeecf57 100644 --- a/src/code/load.c +++ b/src/code/load.c @@ -41,7 +41,7 @@ s32 Overlay_Load(uintptr_t vRomStart, uintptr_t vRomEnd, void* vRamStart, void* // "Clear BSS area (% 08x-% 08x)" osSyncPrintf("BSS領域をクリアします(%08x-%08x)\n", end, end + ovl->bssSize); } - bzero((void*)end, ovl->bssSize); + bzero((void*)end, (s32)ovl->bssSize); } size = (uintptr_t)&ovl->relocations[ovl->nRelocations] - (uintptr_t)ovl; diff --git a/src/code/mtxuty-cvt.c b/src/code/mtxuty-cvt.c index 18c7a36f3ae..d9b1fbaded8 100644 --- a/src/code/mtxuty-cvt.c +++ b/src/code/mtxuty-cvt.c @@ -20,5 +20,5 @@ void MtxConv_F2L(Mtx* m1, MtxF* m2) { void MtxConv_L2F(MtxF* m1, Mtx* m2) { LogUtils_CheckNullPointer("m1", m1, "../mtxuty-cvt.c", 55); LogUtils_CheckNullPointer("m2", m2, "../mtxuty-cvt.c", 56); - guMtxL2F(m1, m2); + guMtxL2F(m1->mf, m2); } diff --git a/src/code/sys_matrix.c b/src/code/sys_matrix.c index f58872d07ba..bb1ec7d9907 100644 --- a/src/code/sys_matrix.c +++ b/src/code/sys_matrix.c @@ -999,7 +999,7 @@ void Matrix_SetTranslateUniformScaleMtx(Mtx* mtx, f32 scale, f32 translateX, f32 MtxF mf; Matrix_SetTranslateUniformScaleMtxF(&mf, scale, translateX, translateY, translateZ); - guMtxF2L(&mf, mtx); + guMtxF2L(mf.mf, mtx); } void Matrix_SetTranslateUniformScaleMtx2(Mtx* mtx, f32 scale, f32 translateX, f32 translateY, f32 translateZ) { diff --git a/src/code/z_actor.c b/src/code/z_actor.c index efc057dfd58..fd1f1cd96c7 100644 --- a/src/code/z_actor.c +++ b/src/code/z_actor.c @@ -810,7 +810,7 @@ void Actor_Init(Actor* actor, PlayState* play) { Actor_SetScale(actor, 0.01f); actor->targetMode = 3; actor->minVelocityY = -20.0f; - actor->xyzDistToPlayerSq = FLT_MAX; + actor->xyzDistToPlayerSq = MAXFLOAT; actor->naviEnemyId = NAVI_ENEMY_NONE; actor->uncullZoneForward = 1000.0f; actor->uncullZoneScale = 350.0f; @@ -1443,7 +1443,7 @@ f32 func_8002EFC0(Actor* actor, Player* player, s16 arg2) { if (player->unk_664 != NULL) { if ((yawTempAbs > 0x4000) || (actor->flags & ACTOR_FLAG_27)) { - return FLT_MAX; + return MAXFLOAT; } else { f32 ret = actor->xyzDistToPlayerSq - actor->xyzDistToPlayerSq * 0.8f * ((0x4000 - yawTempAbs) * (1.0f / 0x8000)); @@ -1453,7 +1453,7 @@ f32 func_8002EFC0(Actor* actor, Player* player, s16 arg2) { } if (yawTempAbs > 0x2AAA) { - return FLT_MAX; + return MAXFLOAT; } return actor->xyzDistToPlayerSq; @@ -1488,7 +1488,7 @@ s32 func_8002F0C8(Actor* actor, Player* player, s32 flag) { f32 dist; if ((player->unk_664 == NULL) && (abs_var > 0x2AAA)) { - dist = FLT_MAX; + dist = MAXFLOAT; } else { dist = actor->xyzDistToPlayerSq; } @@ -3028,7 +3028,7 @@ Actor* func_80032AF0(PlayState* play, ActorContext* actorCtx, Actor** actorPtr, u8* entry; D_8015BBE8 = D_8015BBEC = NULL; - D_8015BBF0 = sbgmEnemyDistSq = FLT_MAX; + D_8015BBF0 = sbgmEnemyDistSq = MAXFLOAT; D_8015BBF8 = 0x7FFFFFFF; if (!Player_InCsMode(play)) { diff --git a/src/code/z_collision_check.c b/src/code/z_collision_check.c index 7c4d4259a64..d11e8a448e9 100644 --- a/src/code/z_collision_check.c +++ b/src/code/z_collision_check.c @@ -3226,8 +3226,6 @@ void Collider_SetTrisDim(PlayState* play, ColliderTris* collider, s32 index, Col // by the compiler between structs like TriNorm and/or Vec3f, so they don't take space in bss. static s8 sBssDummy11; static s8 sBssDummy12; -static s8 sBssDummy13; -static s8 sBssDummy14; /** * Updates the world spheres for all of the collider's JntSph elements attached to the specified limb diff --git a/src/libultra/gu/cosf.c b/src/libultra/gu/cosf.c index 639084093f7..99a79eeaf33 100644 --- a/src/libultra/gu/cosf.c +++ b/src/libultra/gu/cosf.c @@ -1,5 +1,4 @@ -#include "ultra64.h" -#include "global.h" +#include "guint.h" static const du P[] = { { 0x3FF00000, 0x00000000 }, { 0xBFC55554, 0xBC83656D }, { 0x3F8110ED, 0x3804C2A0 }, diff --git a/src/libultra/gu/coss.c b/src/libultra/gu/coss.c index 0eb8b5e65de..b393fd928b2 100644 --- a/src/libultra/gu/coss.c +++ b/src/libultra/gu/coss.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "guint.h" /** * @param angle binang diff --git a/src/libultra/gu/lookat.c b/src/libultra/gu/lookat.c index 5313d3cc3e2..701e8b71dce 100644 --- a/src/libultra/gu/lookat.c +++ b/src/libultra/gu/lookat.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "guint.h" void guLookAtF(f32 mf[4][4], f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f32 xUp, f32 yUp, f32 zUp) { f32 length; @@ -61,5 +61,5 @@ void guLookAt(Mtx* m, f32 xEye, f32 yEye, f32 zEye, f32 xAt, f32 yAt, f32 zAt, f guLookAtF(mf, xEye, yEye, zEye, xAt, yAt, zAt, xUp, yUp, zUp); - guMtxF2L((MtxF*)mf, m); + guMtxF2L(mf, m); } diff --git a/src/libultra/gu/lookathil.c b/src/libultra/gu/lookathil.c index d1070b579c1..fd532d1000e 100644 --- a/src/libultra/gu/lookathil.c +++ b/src/libultra/gu/lookathil.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "guint.h" #define FTOFRAC8(x) ((s32)MIN(((x) * (128.0f)), 127.0f) & 0xFF) @@ -27,7 +27,7 @@ void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 xLook = xAt - xEye; yLook = yAt - yEye; zLook = zAt - zEye; - length = -1.0 / sqrtf(xLook * xLook + yLook * yLook + zLook * zLook); + length = -1.0 / sqrtf(SQ(xLook) + SQ(yLook) + SQ(zLook)); xLook *= length; yLook *= length; zLook *= length; @@ -35,7 +35,7 @@ void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 xRight = yUp * zLook - zUp * yLook; yRight = zUp * xLook - xUp * zLook; zRight = xUp * yLook - yUp * xLook; - length = 1.0 / sqrtf(xRight * xRight + yRight * yRight + zRight * zRight); + length = 1.0 / sqrtf(SQ(xRight) + SQ(yRight) + SQ(zRight)); xRight *= length; yRight *= length; zRight *= length; @@ -43,14 +43,14 @@ void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 xUp = yLook * zRight - zLook * yRight; yUp = zLook * xRight - xLook * zRight; zUp = xLook * yRight - yLook * xRight; - length = 1.0 / sqrtf(xUp * xUp + yUp * yUp + zUp * zUp); + length = 1.0 / sqrtf(SQ(xUp) + SQ(yUp) + SQ(zUp)); xUp *= length; yUp *= length; zUp *= length; /* hilite vectors */ - length = 1.0 / sqrtf(xl1 * xl1 + yl1 * yl1 + zl1 * zl1); + length = 1.0 / sqrtf(SQ(xl1) + SQ(yl1) + SQ(zl1)); xl1 *= length; yl1 *= length; zl1 *= length; @@ -59,7 +59,7 @@ void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 yHilite = yl1 + yLook; zHilite = zl1 + zLook; - length = sqrtf(xHilite * xHilite + yHilite * yHilite + zHilite * zHilite); + length = sqrtf(SQ(xHilite) + SQ(yHilite) + SQ(zHilite)); if (length > 0.1) { length = 1.0 / length; @@ -75,7 +75,7 @@ void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 h->h.y1 = hiliteHeight * 2; } - length = 1.0 / sqrtf(xl2 * xl2 + yl2 * yl2 + zl2 * zl2); + length = 1.0 / sqrtf(SQ(xl2) + SQ(yl2) + SQ(zl2)); xl2 *= length; yl2 *= length; zl2 *= length; @@ -83,7 +83,7 @@ void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 xHilite = xl2 + xLook; yHilite = yl2 + yLook; zHilite = zl2 + zLook; - length = sqrtf(xHilite * xHilite + yHilite * yHilite + zHilite * zHilite); + length = sqrtf(SQ(xHilite) + SQ(yHilite) + SQ(zHilite)); if (length > 0.1) { length = 1.0 / length; xHilite *= length; @@ -106,22 +106,22 @@ void guLookAtHiliteF(f32 mf[4][4], LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 l->l[1].l.dir[0] = FTOFRAC8(xUp); l->l[1].l.dir[1] = FTOFRAC8(yUp); l->l[1].l.dir[2] = FTOFRAC8(zUp); - l->l[0].l.col[0] = 0x00; - l->l[0].l.col[1] = 0x00; - l->l[0].l.col[2] = 0x00; - l->l[0].l.pad1 = 0x00; - l->l[0].l.colc[0] = 0x00; - l->l[0].l.colc[1] = 0x00; - l->l[0].l.colc[2] = 0x00; - l->l[0].l.pad2 = 0x00; - l->l[1].l.col[0] = 0x00; - l->l[1].l.col[1] = 0x80; - l->l[1].l.col[2] = 0x00; - l->l[1].l.pad1 = 0x00; - l->l[1].l.colc[0] = 0x00; - l->l[1].l.colc[1] = 0x80; - l->l[1].l.colc[2] = 0x00; - l->l[1].l.pad2 = 0x00; + l->l[0].l.col[0] = 0; + l->l[0].l.col[1] = 0; + l->l[0].l.col[2] = 0; + l->l[0].l.pad1 = 0; + l->l[0].l.colc[0] = 0; + l->l[0].l.colc[1] = 0; + l->l[0].l.colc[2] = 0; + l->l[0].l.pad2 = 0; + l->l[1].l.col[0] = 0; + l->l[1].l.col[1] = 128; + l->l[1].l.col[2] = 0; + l->l[1].l.pad1 = 0; + l->l[1].l.colc[0] = 0; + l->l[1].l.colc[1] = 128; + l->l[1].l.colc[2] = 0; + l->l[1].l.pad2 = 0; mf[0][0] = xRight; mf[1][0] = yRight; @@ -157,5 +157,5 @@ void guLookAtHilite(Mtx* m, LookAt* l, Hilite* h, f32 xEye, f32 yEye, f32 zEye, guLookAtHiliteF(mf, l, h, xEye, yEye, zEye, xAt, yAt, zAt, xUp, yUp, zUp, xl1, yl1, zl1, xl2, yl2, zl2, hiliteWidth, hiliteHeight); - guMtxF2L((MtxF*)mf, m); + guMtxF2L(mf, m); } diff --git a/src/libultra/gu/ortho.c b/src/libultra/gu/ortho.c index 517ba1dccca..6813eb9ee48 100644 --- a/src/libultra/gu/ortho.c +++ b/src/libultra/gu/ortho.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "guint.h" void guOrthoF(f32 mf[4][4], f32 left, f32 right, f32 bottom, f32 top, f32 near, f32 far, f32 scale) { s32 i, j; @@ -25,5 +25,5 @@ void guOrtho(Mtx* mtx, f32 left, f32 right, f32 bottom, f32 top, f32 near, f32 f guOrthoF(mf, left, right, bottom, top, near, far, scale); - guMtxF2L((MtxF*)mf, mtx); + guMtxF2L(mf, mtx); } diff --git a/src/libultra/gu/perspective.c b/src/libultra/gu/perspective.c index 21b0e1a0a62..b78a7a447ec 100644 --- a/src/libultra/gu/perspective.c +++ b/src/libultra/gu/perspective.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "guint.h" void guPerspectiveF(f32 mf[4][4], u16* perspNorm, f32 fovy, f32 aspect, f32 near, f32 far, f32 scale) { f32 yscale; @@ -38,5 +38,5 @@ void guPerspective(Mtx* m, u16* perspNorm, f32 fovy, f32 aspect, f32 near, f32 f f32 mf[4][4]; guPerspectiveF(mf, perspNorm, fovy, aspect, near, far, scale); - guMtxF2L((MtxF*)mf, m); + guMtxF2L(mf, m); } diff --git a/src/libultra/gu/position.c b/src/libultra/gu/position.c index 65517139d8a..4faa49d2d47 100644 --- a/src/libultra/gu/position.c +++ b/src/libultra/gu/position.c @@ -1,17 +1,18 @@ -#include "global.h" +#include "math.h" +#include "guint.h" /** * guPositionF * Creates a rotation/parallel translation modeling matrix (floating point) */ void guPositionF(f32 mf[4][4], f32 rot, f32 pitch, f32 yaw, f32 scale, f32 x, f32 y, f32 z) { - static f32 D_80134D00 = M_PI / 180.0; + static f32 dtor = M_PI / 180.0f; f32 sinr, sinp, sinh; f32 cosr, cosp, cosh; - rot *= D_80134D00; - pitch *= D_80134D00; - yaw *= D_80134D00; + rot *= dtor; + pitch *= dtor; + yaw *= dtor; sinr = sinf(rot); cosr = cosf(rot); @@ -50,5 +51,5 @@ void guPosition(Mtx* m, f32 rot, f32 pitch, f32 yaw, f32 scale, f32 x, f32 y, f3 guPositionF(mf, rot, pitch, yaw, scale, x, y, z); - guMtxF2L((MtxF*)mf, m); + guMtxF2L(mf, m); } diff --git a/src/libultra/gu/rotate.c b/src/libultra/gu/rotate.c index 64d4bd6ac78..af171897f4d 100644 --- a/src/libultra/gu/rotate.c +++ b/src/libultra/gu/rotate.c @@ -1,7 +1,8 @@ -#include "global.h" +#include "math.h" +#include "guint.h" void guRotateF(f32 m[4][4], f32 a, f32 x, f32 y, f32 z) { - static f32 D_80134D10 = M_PI / 180.0f; + static f32 dtor = M_PI / 180.0f; f32 sine; f32 cosine; f32 ab; @@ -14,7 +15,7 @@ void guRotateF(f32 m[4][4], f32 a, f32 x, f32 y, f32 z) { guNormalize(&x, &y, &z); - a = a * D_80134D10; + a = a * dtor; sine = sinf(a); cosine = cosf(a); @@ -47,5 +48,5 @@ void guRotate(Mtx* m, f32 a, f32 x, f32 y, f32 z) { f32 mf[4][4]; guRotateF(mf, a, x, y, z); - guMtxF2L((MtxF*)mf, m); + guMtxF2L(mf, m); } diff --git a/src/libultra/gu/sinf.c b/src/libultra/gu/sinf.c index 49929e84368..5be9cce43db 100644 --- a/src/libultra/gu/sinf.c +++ b/src/libultra/gu/sinf.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64.h" +#include "guint.h" static const du P[] = { { 0x3FF00000, 0x00000000 }, { 0xBFC55554, 0xBC83656D }, { 0x3F8110ED, 0x3804C2A0 }, diff --git a/src/libultra/gu/sins.c b/src/libultra/gu/sins.c index 636b76bd666..4fed319c59e 100644 --- a/src/libultra/gu/sins.c +++ b/src/libultra/gu/sins.c @@ -1,4 +1,4 @@ -#include "ultra64.h" +#include "guint.h" #include "sintable.inc.c" diff --git a/src/libultra/gu/sintable.inc.c b/src/libultra/gu/sintable.inc.c index 2753d55a989..21d5ffb4044 100644 --- a/src/libultra/gu/sintable.inc.c +++ b/src/libultra/gu/sintable.inc.c @@ -1,5 +1,3 @@ -#include "ultra64/ultratypes.h" - static s16 sintable[0x400] = { 0x0000, 0x0032, 0x0064, 0x0096, 0x00C9, 0x00FB, 0x012D, 0x0160, 0x0192, 0x01C4, 0x01F7, 0x0229, 0x025B, 0x028E, 0x02C0, 0x02F2, 0x0324, 0x0357, 0x0389, 0x03BB, 0x03EE, 0x0420, 0x0452, 0x0484, 0x04B7, 0x04E9, 0x051B, 0x054E, diff --git a/src/libultra/gu/sqrtf.c b/src/libultra/gu/sqrtf.c index 5e5fa233cc8..698c85feb60 100644 --- a/src/libultra/gu/sqrtf.c +++ b/src/libultra/gu/sqrtf.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "guint.h" #ifndef __GNUC__ #define __builtin_sqrtf sqrtf diff --git a/src/libultra/gu/us2dex.c b/src/libultra/gu/us2dex.c index d2e5a2b043e..8df0acbc6d3 100644 --- a/src/libultra/gu/us2dex.c +++ b/src/libultra/gu/us2dex.c @@ -1,4 +1,6 @@ -#include "global.h" +#include "ultratypes.h" +#include "gbi.h" +#include "gs2dex.h" void guS2DInitBg(uObjBg* bg) { u32 size; diff --git a/src/libultra/io/aigetlen.c b/src/libultra/io/aigetlen.c index 45be4425163..9279477f6d0 100644 --- a/src/libultra/io/aigetlen.c +++ b/src/libultra/io/aigetlen.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "rcp.h" /** * Returns the number of bytes remaining in a currently ongoing audio DMA. diff --git a/src/libultra/io/aisetfreq.c b/src/libultra/io/aisetfreq.c index 9e1b8c44e22..cd5da9c2433 100644 --- a/src/libultra/io/aisetfreq.c +++ b/src/libultra/io/aisetfreq.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "rcp.h" +#include "osint.h" /** * Programs the operating frequency of the Audio DAC. diff --git a/src/libultra/io/aisetnextbuf.c b/src/libultra/io/aisetnextbuf.c index 4cf5d1f7d9f..6af5f73e611 100644 --- a/src/libultra/io/aisetnextbuf.c +++ b/src/libultra/io/aisetnextbuf.c @@ -1,4 +1,6 @@ -#include "global.h" +#include "internal.h" +#include "rcp.h" +#include "convert.h" /** * Submits an audio buffer to be consumed by the Audio DAC. The audio interface can queue a second DMA while another diff --git a/src/libultra/io/cartrominit.c b/src/libultra/io/cartrominit.c index b2cf1d1e177..25c4bf492f5 100644 --- a/src/libultra/io/cartrominit.c +++ b/src/libultra/io/cartrominit.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" OSPiHandle __CartRomHandle; diff --git a/src/libultra/io/contpfs.c b/src/libultra/io/contpfs.c index 026237a8028..3532376f338 100644 --- a/src/libultra/io/contpfs.c +++ b/src/libultra/io/contpfs.c @@ -1,9 +1,11 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" +#include "../macros.h" // TODO s32 __osPfsInodeCacheChannel = -1; u8 __osPfsInodeCacheBank = 250; +__OSInode __osPfsInodeCache; + u16 __osSumcalc(u8* ptr, s32 length) { s32 i; u32 sum = 0; diff --git a/src/libultra/io/contquery.c b/src/libultra/io/contquery.c index 660e2dcd427..ba6f7c92320 100644 --- a/src/libultra/io/contquery.c +++ b/src/libultra/io/contquery.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "controller.h" +#include "siint.h" /** * osContStartQuery: diff --git a/src/libultra/io/contramread.c b/src/libultra/io/contramread.c index 058f58e80fb..98dfff102dd 100644 --- a/src/libultra/io/contramread.c +++ b/src/libultra/io/contramread.c @@ -1,6 +1,5 @@ -#include "global.h" - -#define BLOCKSIZE 32 +#include "controller.h" +#include "siint.h" s32 __osPfsLastChannel = -1; @@ -46,14 +45,14 @@ s32 __osContRamRead(OSMesgQueue* ctrlrqueue, s32 channel, u16 addr, u8* data) { if (ret) { break; } - ret = 4; // Retry + ret = PFS_ERR_CONTRFAIL; } else { bcopy(ptr + 6, data, BLOCKSIZE); } } else { - ret = 1; // Error + ret = PFS_ERR_NOPACK; } - if (ret != 4) { + if (ret != PFS_ERR_CONTRFAIL) { break; } } while (0 <= retryCount--); diff --git a/src/libultra/io/contramwrite.c b/src/libultra/io/contramwrite.c index 6a776a13b8c..9e7be884692 100644 --- a/src/libultra/io/contramwrite.c +++ b/src/libultra/io/contramwrite.c @@ -1,5 +1,5 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" +#include "siint.h" s32 __osContRamWrite(OSMesgQueue* mq, s32 channel, u16 address, u8* buffer, s32 force) { s32 ret = 0; diff --git a/src/libultra/io/contreaddata.c b/src/libultra/io/contreaddata.c index ff44bc15b89..2974212aa57 100644 --- a/src/libultra/io/contreaddata.c +++ b/src/libultra/io/contreaddata.c @@ -1,4 +1,8 @@ -#include "global.h" +#include "controller.h" +#include "siint.h" +#include "../macros.h" // TODO + +void __osPackReadData(void); s32 osContStartReadData(OSMesgQueue* mq) { s32 ret; diff --git a/src/libultra/io/controller.c b/src/libultra/io/controller.c index 290f1b5854b..014a9ae6f3b 100644 --- a/src/libultra/io/controller.c +++ b/src/libultra/io/controller.c @@ -1,4 +1,6 @@ -#include "global.h" +#include "controller.h" +#include "siint.h" +#include "../macros.h" // TODO OSPifRam __osContPifRam; u8 __osContLastCmd; diff --git a/src/libultra/io/contsetch.c b/src/libultra/io/contsetch.c index feab63cdb43..bea5f2d1aa9 100644 --- a/src/libultra/io/contsetch.c +++ b/src/libultra/io/contsetch.c @@ -1,5 +1,5 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" +#include "siint.h" /* * s32 osContSetCh(u8 ch) diff --git a/src/libultra/io/crc.c b/src/libultra/io/crc.c index c1af0b12c5f..741f4758dc5 100644 --- a/src/libultra/io/crc.c +++ b/src/libultra/io/crc.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "controller.h" // Valid addr up to 0x7FF // It's the address of a block of 0x20 bytes in the mempak diff --git a/src/libultra/io/devmgr.c b/src/libultra/io/devmgr.c index d0e7d5289a0..0e8f9d97042 100644 --- a/src/libultra/io/devmgr.c +++ b/src/libultra/io/devmgr.c @@ -1,6 +1,5 @@ -#include "global.h" -#include "ultra64/internal.h" -#include "ultra64/leodrive.h" +#include "piint.h" +#include "leodrive.h" // os.h #define LEO_BLOCK_MODE 1 diff --git a/src/libultra/io/dpgetstat.c b/src/libultra/io/dpgetstat.c index 1a944b2c7ab..d6cfee9bf70 100644 --- a/src/libultra/io/dpgetstat.c +++ b/src/libultra/io/dpgetstat.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "rcp.h" +#include "rdp.h" u32 osDpGetStatus(void) { return IO_READ(DPC_STATUS_REG); diff --git a/src/libultra/io/dpsetstat.c b/src/libultra/io/dpsetstat.c index 4275e1fde61..e14e0dec720 100644 --- a/src/libultra/io/dpsetstat.c +++ b/src/libultra/io/dpsetstat.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "rcp.h" +#include "rdp.h" void osDpSetStatus(u32 status) { IO_WRITE(DPC_STATUS_REG, status); diff --git a/src/libultra/io/driverominit.c b/src/libultra/io/driverominit.c index 7d1441cdd99..d9b1a1c8b1d 100644 --- a/src/libultra/io/driverominit.c +++ b/src/libultra/io/driverominit.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" OSPiHandle __DriveRomHandle; diff --git a/src/libultra/io/epidma.c b/src/libultra/io/epidma.c index 28565e72c05..b9cad53e934 100644 --- a/src/libultra/io/epidma.c +++ b/src/libultra/io/epidma.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/internal.h" +#include "piint.h" s32 osEPiStartDma(OSPiHandle* handle, OSIoMesg* mb, s32 direction) { s32 ret; @@ -10,9 +9,9 @@ s32 osEPiStartDma(OSPiHandle* handle, OSIoMesg* mb, s32 direction) { mb->piHandle = handle; if (direction == OS_READ) { - mb->hdr.type = 0xF; + mb->hdr.type = OS_MESG_TYPE_EDMAREAD; } else { - mb->hdr.type = 0x10; + mb->hdr.type = OS_MESG_TYPE_EDMAWRITE; } if (mb->hdr.pri == 1) { diff --git a/src/libultra/io/epirawdma.c b/src/libultra/io/epirawdma.c index ff6bb9a6533..ae468ab2818 100644 --- a/src/libultra/io/epirawdma.c +++ b/src/libultra/io/epirawdma.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" s32 __osEPiRawStartDma(OSPiHandle* handle, s32 direction, u32 cartAddr, void* dramAddr, size_t size) { s32 status; diff --git a/src/libultra/io/epirawread.c b/src/libultra/io/epirawread.c index 41997df66df..d33cda6b1a9 100644 --- a/src/libultra/io/epirawread.c +++ b/src/libultra/io/epirawread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" s32 __osEPiRawReadIo(OSPiHandle* handle, u32 devAddr, u32* data) { s32 status; diff --git a/src/libultra/io/epirawwrite.c b/src/libultra/io/epirawwrite.c index 4d704a1923e..50d29ceb550 100644 --- a/src/libultra/io/epirawwrite.c +++ b/src/libultra/io/epirawwrite.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" s32 __osEPiRawWriteIo(OSPiHandle* handle, u32 devAddr, u32 data) { s32 status; diff --git a/src/libultra/io/epiread.c b/src/libultra/io/epiread.c index d05604a589b..55a80f38f41 100644 --- a/src/libultra/io/epiread.c +++ b/src/libultra/io/epiread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" s32 osEPiReadIo(OSPiHandle* handle, u32 devAddr, u32* data) { register s32 ret; diff --git a/src/libultra/io/epiwrite.c b/src/libultra/io/epiwrite.c index 0547e699913..5f0b842afe3 100644 --- a/src/libultra/io/epiwrite.c +++ b/src/libultra/io/epiwrite.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" s32 osEPiWriteIo(OSPiHandle* handle, u32 devAddr, u32 data) { register s32 ret; diff --git a/src/libultra/io/motor.c b/src/libultra/io/motor.c index b63dd38f519..64b97684b56 100644 --- a/src/libultra/io/motor.c +++ b/src/libultra/io/motor.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "controller.h" +#include "siint.h" #define MOTOR_ID 0x80 diff --git a/src/libultra/io/pfsallocatefile.c b/src/libultra/io/pfsallocatefile.c index c20531224ea..ec844dab3c3 100644 --- a/src/libultra/io/pfsallocatefile.c +++ b/src/libultra/io/pfsallocatefile.c @@ -1,6 +1,4 @@ -#include "ultra64.h" -#include "global.h" -#include "ultra64/pfs.h" +#include "controller.h" s32 osPfsAllocateFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName, s32 fileSize, s32* fileNo) { s32 startPage; diff --git a/src/libultra/io/pfschecker.c b/src/libultra/io/pfschecker.c index b0a94f1ad47..b9b9750b2fe 100644 --- a/src/libultra/io/pfschecker.c +++ b/src/libultra/io/pfschecker.c @@ -1,11 +1,12 @@ -#include "ultra64.h" -#include "global.h" -#include "ultra64/pfs.h" +#include "controller.h" #define CHECK_IPAGE(p) \ (((p).ipage >= pfs->inodeStartPage) && ((p).inode_t.bank < pfs->banks) && ((p).inode_t.page >= 0x01) && \ ((p).inode_t.page < 0x80)) +s32 corrupted_init(OSPfs* pfs, __OSInodeCache* cache); +s32 corrupted(OSPfs* pfs, __OSInodeUnit fpage, __OSInodeCache* cache); + s32 osPfsChecker(OSPfs* pfs) { s32 j; s32 ret; @@ -27,7 +28,7 @@ s32 osPfsChecker(OSPfs* pfs) { if (ret) { return ret; } - if ((ret = func_80105788(pfs, &cache)) != 0) { + if ((ret = corrupted_init(pfs, &cache)) != 0) { return ret; } @@ -54,7 +55,7 @@ s32 osPfsChecker(OSPfs* pfs) { return ret; } } - if ((cc = func_80105A60(pfs, next, &cache) - cl) != 0) { + if ((cc = corrupted(pfs, next, &cache) - cl) != 0) { break; } cl = 1; @@ -83,7 +84,7 @@ s32 osPfsChecker(OSPfs* pfs) { } if ((tempDir.company_code != 0) && (tempDir.game_code != 0) && - (tempDir.start_page.ipage >= (u16)pfs->inodeStartPage)) { // cast required + (tempDir.start_page.ipage >= (u16)pfs->inodeStartPage)) { nextNodeInFile[j].ipage = tempDir.start_page.ipage; } else { nextNodeInFile[j].ipage = 0; @@ -105,7 +106,7 @@ s32 osPfsChecker(OSPfs* pfs) { for (j = 0; j < pfs->dir_size; j++) { while (nextNodeInFile[j].inode_t.bank == bank && - nextNodeInFile[j].ipage >= (u16)pfs->inodeStartPage) { // cast required + nextNodeInFile[j].ipage >= (u16)pfs->inodeStartPage) { u8 val; val = nextNodeInFile[j].inode_t.page; nextNodeInFile[j] = checkedInode.inodePage[val] = tempInode.inodePage[val]; @@ -124,8 +125,7 @@ s32 osPfsChecker(OSPfs* pfs) { return 0; } -// Original name: corrupted_init (probably needs better name) -s32 func_80105788(OSPfs* pfs, __OSInodeCache* cache) { +s32 corrupted_init(OSPfs* pfs, __OSInodeCache* cache) { s32 i; s32 n; s32 offset; @@ -158,8 +158,7 @@ s32 func_80105788(OSPfs* pfs, __OSInodeCache* cache) { return 0; } -// original name: corrupted (probably needs a better name) -s32 func_80105A60(OSPfs* pfs, __OSInodeUnit fpage, __OSInodeCache* cache) { +s32 corrupted(OSPfs* pfs, __OSInodeUnit fpage, __OSInodeCache* cache) { s32 j; s32 n; s32 hit = 0; diff --git a/src/libultra/io/pfsdeletefile.c b/src/libultra/io/pfsdeletefile.c index 573af6a7240..e18ea5825b4 100644 --- a/src/libultra/io/pfsdeletefile.c +++ b/src/libultra/io/pfsdeletefile.c @@ -1,5 +1,4 @@ -#include "ultra64/pfs.h" -#include "global.h" +#include "controller.h" s32 osPfsDeleteFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName) { s32 file_no; diff --git a/src/libultra/io/pfsfilestate.c b/src/libultra/io/pfsfilestate.c index 367a7f27d67..812c825df12 100644 --- a/src/libultra/io/pfsfilestate.c +++ b/src/libultra/io/pfsfilestate.c @@ -1,5 +1,4 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" s32 osPfsFileState(OSPfs* pfs, s32 fileNo, OSPfsState* state) { s32 ret; diff --git a/src/libultra/io/pfsfindfile.c b/src/libultra/io/pfsfindfile.c index ad0bde885a3..9db78a1a4a9 100644 --- a/src/libultra/io/pfsfindfile.c +++ b/src/libultra/io/pfsfindfile.c @@ -1,5 +1,4 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" s32 osPfsFindFile(OSPfs* pfs, u16 companyCode, u32 gameCode, u8* gameName, u8* extName, s32* fileNo) { s32 j; diff --git a/src/libultra/io/pfsfreeblocks.c b/src/libultra/io/pfsfreeblocks.c index 7210c65815f..a7895b46fe9 100644 --- a/src/libultra/io/pfsfreeblocks.c +++ b/src/libultra/io/pfsfreeblocks.c @@ -1,6 +1,4 @@ -#include "ultra64.h" -#include "global.h" -#include "ultra64/pfs.h" +#include "controller.h" s32 osPfsFreeBlocks(OSPfs* pfs, s32* leftoverBytes) { s32 j; diff --git a/src/libultra/io/pfsgetstatus.c b/src/libultra/io/pfsgetstatus.c index c0b05e45e40..e63ff20381b 100644 --- a/src/libultra/io/pfsgetstatus.c +++ b/src/libultra/io/pfsgetstatus.c @@ -1,7 +1,8 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" +#include "siint.h" -OSPifRam __osPfsPifRam; +void __osPfsRequestOneChannel(s32 channel, u8 cmd); +void __osPfsGetOneChannelData(s32 channel, OSContStatus* contData); s32 __osPfsGetStatus(OSMesgQueue* queue, s32 channel) { s32 ret = 0; diff --git a/src/libultra/io/pfsinitpak.c b/src/libultra/io/pfsinitpak.c index 59fb8eda9bf..a5ced9f4913 100644 --- a/src/libultra/io/pfsinitpak.c +++ b/src/libultra/io/pfsinitpak.c @@ -1,5 +1,7 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" +#include "siint.h" + +s32 __osPfsCheckRamArea(OSPfs* pfs); s32 osPfsInitPak(OSMesgQueue* queue, OSPfs* pfs, s32 channel) { s32 ret; diff --git a/src/libultra/io/pfsisplug.c b/src/libultra/io/pfsisplug.c index dd51933752d..efcff88282e 100644 --- a/src/libultra/io/pfsisplug.c +++ b/src/libultra/io/pfsisplug.c @@ -1,5 +1,7 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" +#include "siint.h" + +OSPifRam __osPfsPifRam; s32 osPfsIsPlug(OSMesgQueue* mq, u8* pattern) { s32 ret = 0; diff --git a/src/libultra/io/pfsreadwritefile.c b/src/libultra/io/pfsreadwritefile.c index 9b99c08d2c2..3e256334175 100644 --- a/src/libultra/io/pfsreadwritefile.c +++ b/src/libultra/io/pfsreadwritefile.c @@ -1,12 +1,9 @@ -#include "ultra64.h" -#include "global.h" +#include "controller.h" #define CHECK_IPAGE(p, pfs) \ (((p).ipage >= (pfs).inodeStartPage) && ((p).inode_t.bank < (pfs).banks) && ((p).inode_t.page >= 0x01) && \ ((p).inode_t.page < 0x80)) -__OSInode __osPfsInodeCache; - s32 __osPfsGetNextPage(OSPfs* pfs, u8* bank, __OSInode* inode, __OSInodeUnit* page) { s32 ret; diff --git a/src/libultra/io/pfsselectbank.c b/src/libultra/io/pfsselectbank.c index d257e049113..6a003091acb 100644 --- a/src/libultra/io/pfsselectbank.c +++ b/src/libultra/io/pfsselectbank.c @@ -1,5 +1,4 @@ -#include "ultra64/pfs.h" -#include "global.h" +#include "controller.h" s32 __osPfsSelectBank(OSPfs* pfs, u8 bank) { u8 temp[BLOCKSIZE]; diff --git a/src/libultra/io/piacs.c b/src/libultra/io/piacs.c index 0ffa3448c4d..36e0b203128 100644 --- a/src/libultra/io/piacs.c +++ b/src/libultra/io/piacs.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "controller.h" +#include "../macros.h" // TODO u32 __osPiAccessQueueEnabled = false; OSMesg piAccessBuf[1]; diff --git a/src/libultra/io/pigetcmdq.c b/src/libultra/io/pigetcmdq.c index 3f6f173580a..84d56fc2eb3 100644 --- a/src/libultra/io/pigetcmdq.c +++ b/src/libultra/io/pigetcmdq.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/internal.h" +#include "piint.h" OSMesgQueue* osPiGetCmdQueue(void) { if (!__osPiDevMgr.active) { diff --git a/src/libultra/io/pimgr.c b/src/libultra/io/pimgr.c index 514fe26e8a0..7ac0812d4c9 100644 --- a/src/libultra/io/pimgr.c +++ b/src/libultra/io/pimgr.c @@ -1,5 +1,5 @@ -#include "global.h" -#include "ultra64/internal.h" +#include "piint.h" +#include "../stack.h" // TODO OSDevMgr __osPiDevMgr = { 0 }; diff --git a/src/libultra/io/pirawdma.c b/src/libultra/io/pirawdma.c index 84f809c9c90..4312821f73e 100644 --- a/src/libultra/io/pirawdma.c +++ b/src/libultra/io/pirawdma.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "piint.h" s32 __osPiRawStartDma(s32 dir, u32 cartAddr, void* dramAddr, size_t size) { s32 status; diff --git a/src/libultra/io/si.c b/src/libultra/io/si.c index 4afee034051..c013f33b2ff 100644 --- a/src/libultra/io/si.c +++ b/src/libultra/io/si.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "stdbool.h" +#include "siint.h" s32 __osSiDeviceBusy(void) { register u32 status = IO_READ(SI_STATUS_REG); diff --git a/src/libultra/io/siacs.c b/src/libultra/io/siacs.c index c814867bd80..c4869d35e25 100644 --- a/src/libultra/io/siacs.c +++ b/src/libultra/io/siacs.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "siint.h" +#include "../macros.h" // TODO OSMesg siAccessBuf[1]; OSMesgQueue __osSiAccessQueue; diff --git a/src/libultra/io/sirawdma.c b/src/libultra/io/sirawdma.c index 9bf499cc6ab..bd5ab54ad56 100644 --- a/src/libultra/io/sirawdma.c +++ b/src/libultra/io/sirawdma.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "siint.h" #define PIF_RAM_SIZE (PIF_RAM_END + 1 - PIF_RAM_START) diff --git a/src/libultra/io/sirawread.c b/src/libultra/io/sirawread.c index 8efa071efb7..8b65caa72cc 100644 --- a/src/libultra/io/sirawread.c +++ b/src/libultra/io/sirawread.c @@ -1,6 +1,6 @@ -#include "global.h" +#include "siint.h" -s32 __osSiRawReadIo(void* devAddr, u32* dst) { +s32 __osSiRawReadIo(u32 devAddr, u32* dst) { if (__osSiDeviceBusy()) { return -1; } diff --git a/src/libultra/io/sirawwrite.c b/src/libultra/io/sirawwrite.c index f0e4514d778..db7c3721f72 100644 --- a/src/libultra/io/sirawwrite.c +++ b/src/libultra/io/sirawwrite.c @@ -1,6 +1,6 @@ -#include "global.h" +#include "siint.h" -s32 __osSiRawWriteIo(void* devAddr, u32 val) { +s32 __osSiRawWriteIo(u32 devAddr, u32 val) { if (__osSiDeviceBusy()) { return -1; } diff --git a/src/libultra/io/sp.c b/src/libultra/io/sp.c index 1796dc8f6e8..eb4481cc7f0 100644 --- a/src/libultra/io/sp.c +++ b/src/libultra/io/sp.c @@ -1,6 +1,7 @@ -#include "global.h" +#include "rcp.h" +#include "osint.h" -u32 __osSpDeviceBusy(void) { +s32 __osSpDeviceBusy(void) { register u32 status = IO_READ(SP_STATUS_REG); if (status & (SP_STATUS_DMA_BUSY | SP_STATUS_DMA_FULL | SP_STATUS_IO_FULL)) { diff --git a/src/libultra/io/spgetstat.c b/src/libultra/io/spgetstat.c index e1545bdb7cb..2545b58a98e 100644 --- a/src/libultra/io/spgetstat.c +++ b/src/libultra/io/spgetstat.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "internal.h" +#include "rcp.h" u32 __osSpGetStatus(void) { return IO_READ(SP_STATUS_REG); diff --git a/src/libultra/io/sprawdma.c b/src/libultra/io/sprawdma.c index 795282c4a01..30940eb022f 100644 --- a/src/libultra/io/sprawdma.c +++ b/src/libultra/io/sprawdma.c @@ -1,6 +1,7 @@ -#include "global.h" +#include "osint.h" +#include "rcp.h" -s32 __osSpRawStartDma(s32 direction, void* devAddr, void* dramAddr, u32 size) { +s32 __osSpRawStartDma(s32 direction, u32 devAddr, void* dramAddr, u32 size) { if (__osSpDeviceBusy()) { return -1; } diff --git a/src/libultra/io/spsetpc.c b/src/libultra/io/spsetpc.c index 944fc986318..0c854eea55f 100644 --- a/src/libultra/io/spsetpc.c +++ b/src/libultra/io/spsetpc.c @@ -1,6 +1,7 @@ -#include "global.h" +#include "internal.h" +#include "rcp.h" -s32 __osSpSetPc(void* pc) { +s32 __osSpSetPc(u32 pc) { register u32 spStatus = IO_READ(SP_STATUS_REG); if (!(spStatus & SP_STATUS_HALT)) { diff --git a/src/libultra/io/spsetstat.c b/src/libultra/io/spsetstat.c index 3fa7a49188d..bcf25e123a5 100644 --- a/src/libultra/io/spsetstat.c +++ b/src/libultra/io/spsetstat.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "internal_rsp.h" +#include "rcp.h" void __osSpSetStatus(u32 status) { IO_WRITE(SP_STATUS_REG, status); diff --git a/src/libultra/io/sptask.c b/src/libultra/io/sptask.c index 8b3286a4530..809d4c8aa16 100644 --- a/src/libultra/io/sptask.c +++ b/src/libultra/io/sptask.c @@ -1,4 +1,6 @@ -#include "global.h" +#include "osint.h" +#include "sptask.h" +#include "rcp.h" #define _osVirtualToPhysical(ptr) \ if (ptr != NULL) { \ @@ -38,16 +40,16 @@ void osSpTaskLoad(OSTask* intp) { osWritebackDCache(tp, sizeof(OSTask)); __osSpSetStatus(SP_CLR_SIG0 | SP_CLR_SIG1 | SP_CLR_SIG2 | SP_SET_INTR_BREAK); - while (__osSpSetPc((void*)SP_IMEM_START) == -1) { + while (__osSpSetPc(SP_IMEM_START) == -1) { ; } - while (__osSpRawStartDma(OS_WRITE, (void*)(SP_IMEM_START - sizeof(*tp)), tp, sizeof(OSTask)) == -1) { + while (__osSpRawStartDma(OS_WRITE, SP_IMEM_START - sizeof(*tp), tp, sizeof(OSTask)) == -1) { ; } while (__osSpDeviceBusy()) { ; } - while (__osSpRawStartDma(OS_WRITE, (void*)SP_IMEM_START, tp->t.ucode_boot, tp->t.ucode_boot_size) == -1) { + while (__osSpRawStartDma(OS_WRITE, SP_IMEM_START, tp->t.ucode_boot, tp->t.ucode_boot_size) == -1) { ; } } diff --git a/src/libultra/io/sptaskyield.c b/src/libultra/io/sptaskyield.c index ec7a248bbba..62ba4046303 100644 --- a/src/libultra/io/sptaskyield.c +++ b/src/libultra/io/sptaskyield.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "internal.h" +#include "rcp.h" void osSpTaskYield(void) { __osSpSetStatus(SP_SET_SIG0); diff --git a/src/libultra/io/sptaskyielded.c b/src/libultra/io/sptaskyielded.c index 4fec2a6d517..04709f240d8 100644 --- a/src/libultra/io/sptaskyielded.c +++ b/src/libultra/io/sptaskyielded.c @@ -1,4 +1,6 @@ -#include "global.h" +#include "internal.h" +#include "sptask.h" +#include "rcp.h" u32 osSpTaskYielded(OSTask* task) { u32 ret; diff --git a/src/libultra/io/vi.c b/src/libultra/io/vi.c index dac56ed8121..63cfd5fdc75 100644 --- a/src/libultra/io/vi.c +++ b/src/libultra/io/vi.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" OSViContext vi[2] = { 0 }; OSViContext* __osViCurr = &vi[0]; diff --git a/src/libultra/io/viblack.c b/src/libultra/io/viblack.c index 491522fb1f5..e4edb8234cb 100644 --- a/src/libultra/io/viblack.c +++ b/src/libultra/io/viblack.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" void osViBlack(u8 active) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/io/viextendvstart.c b/src/libultra/io/viextendvstart.c index 9efb9dbc3a1..3b5ed30cd53 100644 --- a/src/libultra/io/viextendvstart.c +++ b/src/libultra/io/viextendvstart.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "viint.h" void osViExtendVStart(u32 value) { __additional_scanline = value; diff --git a/src/libultra/io/vigetcurrcontext.c b/src/libultra/io/vigetcurrcontext.c index 1e3c6ffa3c4..7169592afa9 100644 --- a/src/libultra/io/vigetcurrcontext.c +++ b/src/libultra/io/vigetcurrcontext.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "viint.h" OSViContext* __osViGetCurrentContext(void) { return __osViCurr; diff --git a/src/libultra/io/vigetcurrframebuf.c b/src/libultra/io/vigetcurrframebuf.c index 442a1afa9d8..5451b3132e9 100644 --- a/src/libultra/io/vigetcurrframebuf.c +++ b/src/libultra/io/vigetcurrframebuf.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "viint.h" void* osViGetCurrentFramebuffer(void) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/io/vigetnextframebuf.c b/src/libultra/io/vigetnextframebuf.c index 5e6bd2ac74f..dea50bcdef5 100644 --- a/src/libultra/io/vigetnextframebuf.c +++ b/src/libultra/io/vigetnextframebuf.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "viint.h" void* osViGetNextFramebuffer(void) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/io/vimgr.c b/src/libultra/io/vimgr.c index 2d541672b7e..f2b3e23a40f 100644 --- a/src/libultra/io/vimgr.c +++ b/src/libultra/io/vimgr.c @@ -1,8 +1,10 @@ -#include "global.h" -#include "ultra64/internal.h" +#include "viint.h" +#include "osint.h" +#include "../stack.h" // TODO +#include "../macros.h" // TODO OSThread viThread; -STACK(viThreadStack, 0x1000); +STACK(viThreadStack, OS_VIM_STACKSIZE); OSMesgQueue viEventQueue; OSMesg viEventBuf[5]; OSIoMesg viRetraceMsg; @@ -45,7 +47,7 @@ void osCreateViManager(OSPri pri) { __osViDevMgr.dma = NULL; __osViDevMgr.edma = NULL; - osCreateThread(&viThread, 0, &viMgrMain, &__osViDevMgr, STACK_TOP(viThreadStack), pri); + osCreateThread(&viThread, 0, viMgrMain, &__osViDevMgr, STACK_TOP(viThreadStack), pri); __osViInit(); osStartThread(&viThread); __osRestoreInt(prevInt); diff --git a/src/libultra/io/vimodefpallan1.c b/src/libultra/io/vimodefpallan1.c index 5fb019cdfbb..b8f45eb6057 100644 --- a/src/libultra/io/vimodefpallan1.c +++ b/src/libultra/io/vimodefpallan1.c @@ -8,8 +8,7 @@ * N = Deinterlaced * 1 = 16-bit Framebuffer */ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" OSViMode osViModeFpalLan1 = { OS_VI_FPAL_LAN1, // type diff --git a/src/libultra/io/vimodempallan1.c b/src/libultra/io/vimodempallan1.c index 23d53987b0e..a346cd9c8a6 100644 --- a/src/libultra/io/vimodempallan1.c +++ b/src/libultra/io/vimodempallan1.c @@ -8,8 +8,7 @@ * N = Deinterlaced * 1 = 16-bit Framebuffer */ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" OSViMode osViModeMpalLan1 = { OS_VI_MPAL_LAN1, // type diff --git a/src/libultra/io/vimodentsclan1.c b/src/libultra/io/vimodentsclan1.c index 19a7160ac81..78fe5cf586e 100644 --- a/src/libultra/io/vimodentsclan1.c +++ b/src/libultra/io/vimodentsclan1.c @@ -8,8 +8,7 @@ * N = Deinterlaced * 1 = 16-bit Framebuffer */ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" OSViMode osViModeNtscLan1 = { OS_VI_NTSC_LAN1, // type diff --git a/src/libultra/io/vimodepallan1.c b/src/libultra/io/vimodepallan1.c index 7a8db603f41..a99d3d040c9 100644 --- a/src/libultra/io/vimodepallan1.c +++ b/src/libultra/io/vimodepallan1.c @@ -8,8 +8,7 @@ * N = Deinterlaced * 1 = 16-bit Framebuffer */ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" OSViMode osViModePalLan1 = { OS_VI_PAL_LAN1, // type diff --git a/src/libultra/io/visetevent.c b/src/libultra/io/visetevent.c index 89e482cf848..51d389ad15a 100644 --- a/src/libultra/io/visetevent.c +++ b/src/libultra/io/visetevent.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "viint.h" void osViSetEvent(OSMesgQueue* mq, OSMesg msg, u32 retraceCount) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/io/visetmode.c b/src/libultra/io/visetmode.c index ab5b3b0e189..9a45d218888 100644 --- a/src/libultra/io/visetmode.c +++ b/src/libultra/io/visetmode.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" void osViSetMode(OSViMode* mode) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/io/visetspecial.c b/src/libultra/io/visetspecial.c index d51e3479d32..3d7c67b66d6 100644 --- a/src/libultra/io/visetspecial.c +++ b/src/libultra/io/visetspecial.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" /** * Configures VI "special features" to be applied on the next context swap. diff --git a/src/libultra/io/visetxscale.c b/src/libultra/io/visetxscale.c index 5e5e04ee478..14d4c0e02b9 100644 --- a/src/libultra/io/visetxscale.c +++ b/src/libultra/io/visetxscale.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" void osViSetXScale(f32 value) { register u32 nomValue; diff --git a/src/libultra/io/visetyscale.c b/src/libultra/io/visetyscale.c index adea96fd231..e3bf57fe8cd 100644 --- a/src/libultra/io/visetyscale.c +++ b/src/libultra/io/visetyscale.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" void osViSetYScale(f32 scale) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/io/viswapbuf.c b/src/libultra/io/viswapbuf.c index 99259c03c32..9a403b82bc8 100644 --- a/src/libultra/io/viswapbuf.c +++ b/src/libultra/io/viswapbuf.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" void osViSwapBuffer(void* frameBufPtr) { u32 prevInt = __osDisableInt(); diff --git a/src/libultra/io/viswapcontext.c b/src/libultra/io/viswapcontext.c index d1a7d0c0e62..d2622e617a4 100644 --- a/src/libultra/io/viswapcontext.c +++ b/src/libultra/io/viswapcontext.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/viint.h" +#include "viint.h" void __osViSwapContext(void) { register OSViMode* viMode; diff --git a/src/libultra/libc/absf.c b/src/libultra/libc/absf.c index ebdbb72c08d..8a9c18c774d 100644 --- a/src/libultra/libc/absf.c +++ b/src/libultra/libc/absf.c @@ -1,5 +1,5 @@ -#include "global.h" +#include "guint.h" -f32 absf(f32 a) { - return fabsf(a); +float absf(float n) { + return fabsf(n); } diff --git a/src/libultra/libc/ldiv.c b/src/libultra/libc/ldiv.c index 8c53d3d4cce..10edd00a0c2 100644 --- a/src/libultra/libc/ldiv.c +++ b/src/libultra/libc/ldiv.c @@ -1,6 +1,6 @@ -#include "global.h" +#include "stdlib.h" -ldiv_t ldiv(s32 num, s32 denom) { +ldiv_t ldiv(long num, long denom) { ldiv_t ret; ret.quot = num / denom; @@ -13,7 +13,7 @@ ldiv_t ldiv(s32 num, s32 denom) { return ret; } -lldiv_t lldiv(s64 num, s64 denom) { +lldiv_t lldiv(long long num, long long denom) { lldiv_t ret; ret.quot = num / denom; diff --git a/src/libultra/libc/ll.c b/src/libultra/libc/ll.c index 9d87204d1ed..926fa60649b 100644 --- a/src/libultra/libc/ll.c +++ b/src/libultra/libc/ll.c @@ -1,47 +1,48 @@ -#include "global.h" +// IDO Compiler Intrinsics for 64-bit arithmetic -s64 __ull_rshift(u64 l, s64 r) { +unsigned long long __ull_rshift(unsigned long long l, unsigned long long r) { return l >> r; } -u64 __ull_rem(u64 l, u64 r) { +unsigned long long __ull_rem(unsigned long long l, unsigned long long r) { return l % r; } -u64 __ull_div(u64 l, u64 r) { +unsigned long long __ull_div(unsigned long long l, unsigned long long r) { return l / r; } -s64 __ll_lshift(s64 l, s64 r) { +unsigned long long __ll_lshift(unsigned long long l, unsigned long long r) { return l << r; } -s64 __ll_rem(s64 l, u64 r) { +long long __ll_rem(unsigned long long l, long long r) { return l % r; } -s64 __ll_div(s64 l, s64 r) { +long long __ll_div(long long l, long long r) { return l / r; } -s64 __ll_mul(s64 l, s64 r) { +unsigned long long __ll_mul(unsigned long long l, unsigned long long r) { return l * r; } -void __ull_divremi(u64* quotient, u64* remainder, u64 dividend, u16 divisor) { +void __ull_divremi(unsigned long long int* quotient, unsigned long long int* remainder, + unsigned long long dividend, unsigned short divisor) { *quotient = dividend / divisor; *remainder = dividend % divisor; } -s64 __ll_mod(s64 l, s64 r) { - s64 remainder = l % r; +long long __ll_mod(long long l, long long r) { + long long remainder = l % r; - if (((remainder < 0) && (r > 0)) || ((remainder > 0) && (r < 0))) { + if ((remainder < 0 && r > 0) || (remainder > 0 && r < 0)) { remainder += r; } return remainder; } -s64 __ll_rshift(s64 l, s64 r) { +long long __ll_rshift(long long l, long long r) { return l >> r; } diff --git a/src/libultra/libc/llcvt.c b/src/libultra/libc/llcvt.c index c88d6f04fdc..02a01074d2d 100644 --- a/src/libultra/libc/llcvt.c +++ b/src/libultra/libc/llcvt.c @@ -1,33 +1,33 @@ -#include "global.h" +// IDO Compiler Intrinsics for 64-bit conversion -s64 __d_to_ll(f64 d) { +long long __d_to_ll(double d) { return d; } -s64 __f_to_ll(f32 f) { +long long __f_to_ll(float f) { return f; } -u64 __d_to_ull(f64 d) { +unsigned long long __d_to_ull(double d) { return d; } -u64 __f_to_ull(f32 f) { +unsigned long long __f_to_ull(float f) { return f; } -f64 __ll_to_d(s64 l) { +double __ll_to_d(long long l) { return l; } -f32 __ll_to_f(s64 l) { +float __ll_to_f(long long l) { return l; } -f64 __ull_to_d(u64 l) { +double __ull_to_d(unsigned long long l) { return l; } -f32 __ull_to_f(u64 l) { +float __ull_to_f(unsigned long long l) { return l; } diff --git a/src/libultra/libc/sprintf.c b/src/libultra/libc/sprintf.c new file mode 100644 index 00000000000..2bd5760b6c2 --- /dev/null +++ b/src/libultra/libc/sprintf.c @@ -0,0 +1,31 @@ +#include "stdarg.h" +#include "stdio.h" +#include "string.h" +#include "xstdio.h" + +void* proutSprintf(void* dst, const char* fmt, size_t size) { + return (char*)memcpy(dst, fmt, size) + size; +} + +int vsprintf(char* dst, const char* fmt, va_list args) { + int ret = _Printf(proutSprintf, dst, fmt, args); + if (ret > -1) { + dst[ret] = '\0'; + } + return ret; +} + +int sprintf(char* dst, const char* fmt, ...) { + int ret; + va_list args; + va_start(args, fmt); + + ret = _Printf(proutSprintf, dst, fmt, args); + if (ret > -1) { + dst[ret] = '\0'; + } + + va_end(args); + + return ret; +} diff --git a/src/libultra/libc/sqrt.c b/src/libultra/libc/sqrt.c index 0e8cc95d510..fcb7ab2d90a 100644 --- a/src/libultra/libc/sqrt.c +++ b/src/libultra/libc/sqrt.c @@ -1,9 +1,9 @@ -#include "global.h" +#include "guint.h" #ifndef __GNUC__ #define __builtin_sqrt sqrt #endif -f64 sqrt(f64 f) { +double sqrt(double f) { return __builtin_sqrt(f); } diff --git a/src/libultra/libc/string.c b/src/libultra/libc/string.c index 37091ab83aa..87e7c4c7371 100644 --- a/src/libultra/libc/string.c +++ b/src/libultra/libc/string.c @@ -1,18 +1,18 @@ -#include "global.h" +#include "string.h" -const char* strchr(const char* str, s32 ch) { - u8 c = ch; +char* strchr(const char* str, int c) { + char ch = c; - while (*str != c) { + while (*str != ch) { if (*str == 0) { return NULL; } str++; } - return str; + return (char*)str; } -u32 strlen(const char* str) { +size_t strlen(const char* str) { const char* ptr = str; while (*ptr) { @@ -21,13 +21,13 @@ u32 strlen(const char* str) { return ptr - str; } -void* memcpy(void* dst, const void* src, size_t size) { - u8* _dst = dst; - const u8* _src = src; +void* memcpy(void* dst, const void* src, size_t n) { + char* _dst = dst; + const char* _src = src; - while (size > 0) { + while (n > 0) { *_dst++ = *_src++; - size--; + n--; } return dst; } diff --git a/src/libultra/rmon/xldtob.c b/src/libultra/libc/xldtob.c similarity index 79% rename from src/libultra/rmon/xldtob.c rename to src/libultra/libc/xldtob.c index cc518a44b28..99695b54cbb 100644 --- a/src/libultra/rmon/xldtob.c +++ b/src/libultra/libc/xldtob.c @@ -1,13 +1,15 @@ -#include "global.h" +#include "stdlib.h" +#include "string.h" +#include "xstdio.h" #define BUFF_LEN 0x20 -s16 _Ldunscale(s16*, _Pft*); -void _Genld(_Pft*, u8, u8*, s16, s16); +short _Ldunscale(short*, _Pft*); +void _Genld(_Pft*, char, char*, short, short); -const f64 D_800122E0[] = { 10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L, 10e127L, 10e255L }; +static const double pows[] = { 10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L, 10e127L, 10e255L }; -/* float properties */ +// float properties #define _D0 0 #define _DBIAS 0x3FF #define _DLONG 1 @@ -17,7 +19,8 @@ const f64 D_800122E0[] = { 10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L, 1 #define _FRND 1 #define _LBIAS 0x3FFE #define _LOFF 15 -/* integer properties */ + +// integer properties #define _C2 1 #define _CSIGN 1 #define _ILONG 0 @@ -31,38 +34,26 @@ const f64 D_800122E0[] = { 10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L, 1 #define _DNAN (0x8000 | _DMAX << _DOFF | 1 << (_DOFF - 1)) #define _DSIGN 0x8000 #if _D0 == 3 -#define _D1 2 /* little-endian order */ +#define _D1 2 // little-endian order #define _D2 1 #define _D3 0 #else -#define _D1 1 /* big-endian order */ +#define _D1 1 // big-endian order #define _D2 2 #define _D3 3 #endif -void _Ldtob(_Pft* args, u8 type) { - u8 buff[BUFF_LEN]; - u8* ptr = buff; - u32 sp70; - f64 val = args->v.ld; - /* maybe struct? */ - s16 err; - s16 nsig; - s16 exp; - - s32 i; - s32 n; - f64 factor; - s32 gen; - s32 j; - s32 lo; - ldiv_t qr; - u8 drop; - s32 n2; +void _Ldtob(_Pft* args, char code) { + char buff[BUFF_LEN]; + char* ptr = buff; + LONG_DOUBLE_TYPE val = args->v.ld; + short err; + short nsig; + short exp; if (args->prec < 0) { args->prec = 6; - } else if (args->prec == 0 && (type == 'g' || type == 'G')) { + } else if (args->prec == 0 && (code == 'g' || code == 'G')) { args->prec = 1; } err = _Ldunscale(&exp, (_Pft*)args); @@ -74,16 +65,22 @@ void _Ldtob(_Pft* args, u8 type) { nsig = 0; exp = 0; } else { + int i; + int n; + double factor; + int gen; + if (val < 0) { val = -val; } - exp = exp * 30103 / 0x000186A0 - 4; + + exp = exp * 30103 / 100000 - 4; if (exp < 0) { n = (3 - exp) & ~3; exp = -n; for (i = 0; n > 0; n >>= 1, i++) { if ((n & 1) != 0) { - val *= D_800122E0[i]; + val *= pows[i]; } } } else if (exp > 0) { @@ -92,27 +89,33 @@ void _Ldtob(_Pft* args, u8 type) { for (n = exp, i = 0; n > 0; n >>= 1, i++) { if ((n & 1) != 0) { - factor *= D_800122E0[i]; + factor *= pows[i]; } } val /= factor; } - gen = ((type == 'f') ? exp + 10 : 6) + args->prec; + + gen = ((code == 'f') ? exp + 10 : 6) + args->prec; if (gen > 0x13) { gen = 0x13; } + *ptr++ = '0'; while (gen > 0 && 0 < val) { - lo = val; + int j; + int lo = val; + if ((gen -= 8) > 0) { val = (val - lo) * 1.0e8; } - ptr = ptr + 8; + ptr += 8; + for (j = 8; lo > 0 && --j >= 0;) { - qr = ldiv(lo, 10); + ldiv_t qr = ldiv(lo, 10); *--ptr = qr.rem + '0'; lo = qr.quot; } + while (--j >= 0) { ptr--; *ptr = '0'; @@ -125,11 +128,14 @@ void _Ldtob(_Pft* args, u8 type) { --gen, --exp; } - nsig = ((type == 'f') ? exp + 1 : ((type == 'e' || type == 'E') ? 1 : 0)) + args->prec; + nsig = ((code == 'f') ? exp + 1 : ((code == 'e' || code == 'E') ? 1 : 0)) + args->prec; if (gen < nsig) { nsig = gen; } if (nsig > 0) { + char drop; + int n2; + if (nsig < gen && ptr[nsig] > '4') { drop = '9'; } else { @@ -147,16 +153,16 @@ void _Ldtob(_Pft* args, u8 type) { } } } - _Genld((_Pft*)args, type, ptr, nsig, exp); + _Genld((_Pft*)args, code, ptr, nsig, exp); } -s16 _Ldunscale(s16* pex, _Pft* px) { - u16* ps = (u16*)px; - s16 xchar = (ps[_D0] & _DMASK) >> _DOFF; +short _Ldunscale(short* pex, _Pft* px) { + unsigned short* ps = (unsigned short*)px; + short xchar = (ps[_D0] & _DMASK) >> _DOFF; if (xchar == _DMAX) { /* NaN or INF */ *pex = 0; - return (s16)(ps[_D0] & _DFRAC || ps[_D1] || ps[_D2] || ps[_D3] ? NAN : INF); + return (short)(ps[_D0] & _DFRAC || ps[_D1] || ps[_D2] || ps[_D3] ? NAN : INF); } else if (0 < xchar) { ps[_D0] = (ps[_D0] & ~_DMASK) | (_DBIAS << _DOFF); *pex = xchar - (_DBIAS - 1); @@ -170,13 +176,13 @@ s16 _Ldunscale(s16* pex, _Pft* px) { } } -void _Genld(_Pft* px, u8 code, u8* p, s16 nsig, s16 xexp) { - u8 point = '.'; +void _Genld(_Pft* px, char code, char* p, short nsig, short xexp) { + const char point = '.'; if (nsig <= 0) { nsig = 1, - p = (u8*)"0"; + p = (char*)"0"; } if (code == 'f' || ((code == 'g' || code == 'G') && (-4 <= xexp) && (xexp < px->prec))) { /* 'f' format */ @@ -248,7 +254,7 @@ void _Genld(_Pft* px, u8 code, u8* p, s16 nsig, s16 xexp) { px->n1 += nsig; px->nz1 = px->prec - nsig; } - p = (u8*)&px->s[px->n1]; /* put exponent */ + p = &px->s[px->n1]; /* put exponent */ *p++ = code; if (0 <= xexp) { *p++ = '+'; @@ -264,10 +270,10 @@ void _Genld(_Pft* px, u8 code, u8* p, s16 nsig, s16 xexp) { } *p++ = xexp / 10 + '0', xexp %= 10; *p++ = xexp + '0'; - px->n2 = p - (u8*)&px->s[px->n1]; + px->n2 = p - &px->s[px->n1]; } if ((px->flags & (FLAGS_ZERO | FLAGS_MINUS)) == FLAGS_ZERO) { /* pad with leading zeros */ - s32 n = px->n0 + px->n1 + px->nz1 + px->n2 + px->nz2; + int n = px->n0 + px->n1 + px->nz1 + px->n2 + px->nz2; if (n < px->width) { px->nz0 = px->width - n; diff --git a/src/libultra/libc/xlitob.c b/src/libultra/libc/xlitob.c new file mode 100644 index 00000000000..3e967474c9b --- /dev/null +++ b/src/libultra/libc/xlitob.c @@ -0,0 +1,57 @@ +#include "stdlib.h" +#include "string.h" +#include "xstdio.h" + +#define BUFF_LEN 0x18 + +static char ldigs[] = "0123456789abcdef"; +static char udigs[] = "0123456789ABCDEF"; + +void _Litob(_Pft* args, char type) { + char buff[BUFF_LEN]; + const char* digs; + int base; + int i; + unsigned long long num; + + if (type == 'X') { + digs = udigs; + } else { + digs = ldigs; + } + + base = (type == 'o') ? 8 : ((type != 'x' && type != 'X') ? 10 : 16); + i = BUFF_LEN; + num = args->v.ll; + + if ((type == 'd' || type == 'i') && args->v.ll < 0) { + num = -num; + } + + if (num != 0 || args->prec != 0) { + buff[--i] = digs[num % base]; + } + + args->v.ll = num / base; + + while (args->v.ll > 0 && i > 0) { + lldiv_t quotrem = lldiv(args->v.ll, base); + args->v.ll = quotrem.quot; + buff[--i] = digs[quotrem.rem]; + } + + args->n1 = BUFF_LEN - i; + + memcpy(args->s, buff + i, args->n1); + + if (args->n1 < args->prec) { + args->nz0 = args->prec - args->n1; + } + + if (args->prec < 0 && (args->flags & (FLAGS_ZERO | FLAGS_MINUS)) == FLAGS_ZERO) { + i = args->width - args->n0 - args->nz0 - args->n1; + if (i > 0) { + args->nz0 += i; + } + } +} diff --git a/src/libultra/rmon/xprintf.c b/src/libultra/libc/xprintf.c similarity index 52% rename from src/libultra/rmon/xprintf.c rename to src/libultra/libc/xprintf.c index 36fc00ad518..2a68cf11f99 100644 --- a/src/libultra/rmon/xprintf.c +++ b/src/libultra/libc/xprintf.c @@ -1,11 +1,18 @@ -#include "global.h" - -#define ATOI(i, a) \ - for (i = 0; *a >= '0' && *a <= '9'; a++) \ - if (i < 999) \ +#include "stdarg.h" +#include "stdbool.h" +#include "stdlib.h" +#include "string.h" +#include "xstdio.h" + +#define isdigit(x) (((x) >= '0' && (x) <= '9')) +#define LDSIGN(x) (((unsigned short*)&(x))[0] & 0x8000) + +#define ATOI(i, a) \ + for (i = 0; isdigit(*a); a++) \ + if (i < 999) \ i = *a + i * 10 - '0'; -#define _PROUT(fmt, _size) \ +#define PUT(fmt, _size) \ if (_size > 0) { \ arg = pfn(arg, fmt, _size); \ if (arg != NULL) \ @@ -13,51 +20,57 @@ else \ return x.nchar; \ } -#define _PAD(m, src, extracond) \ - if (extracond && m > 0) { \ - s32 i; \ - s32 j; \ - for (j = m; j > 0; j -= i) { \ - if ((u32)j > 32) \ - i = 32; \ - else \ - i = j; \ - _PROUT(src, i); \ - } \ + +#define MAX_PAD ((int)sizeof(spaces) - 1) + +#define PAD(src, m) \ + if (m > 0) { \ + int i; \ + int j; \ + for (j = m; j > 0; j -= i) { \ + if ((unsigned)j > MAX_PAD) \ + i = MAX_PAD; \ + else \ + i = j; \ + PUT(src, i); \ + } \ } char spaces[] = " "; char zeroes[] = "00000000000000000000000000000000"; -void _Putfld(_Pft*, va_list*, u8, u8*); +void _Putfld(_Pft*, va_list*, char, char*); -s32 _Printf(PrintCallback pfn, void* arg, const char* fmt, va_list ap) { +int _Printf(PrintCallback pfn, void* arg, const char* fmt, va_list ap) { _Pft x; x.nchar = 0; while (true) { static const char fchar[] = " +-#0"; - static const u32 fbit[] = { FLAGS_SPACE, FLAGS_PLUS, FLAGS_MINUS, FLAGS_HASH, FLAGS_ZERO, 0 }; + static const unsigned int fbit[] = { FLAGS_SPACE, FLAGS_PLUS, FLAGS_MINUS, FLAGS_HASH, FLAGS_ZERO, 0 }; - const u8* s = (u8*)fmt; - u8 c; + const char* s = fmt; + char c; const char* t; - u8 ac[0x20]; + char ac[0x20]; while ((c = *s) != 0 && c != '%') { s++; } - _PROUT(fmt, s - (u8*)fmt); + + PUT(fmt, s - fmt); if (c == 0) { return x.nchar; } - fmt = (char*)++s; - x.flags = 0; - for (; (t = strchr(fchar, *s)) != NULL; s++) { + + fmt = ++s; + + for (x.flags = 0; (t = strchr(fchar, *s)) != NULL; s++) { x.flags |= fbit[t - fchar]; } + if (*s == '*') { - x.width = va_arg(ap, s32); + x.width = va_arg(ap, int); if (x.width < 0) { x.width = -x.width; x.flags |= FLAGS_MINUS; @@ -66,21 +79,23 @@ s32 _Printf(PrintCallback pfn, void* arg, const char* fmt, va_list ap) { } else { ATOI(x.width, s); } + if (*s != '.') { x.prec = -1; } else { s++; if (*s == '*') { - x.prec = va_arg(ap, s32); + x.prec = va_arg(ap, int); s++; } else { ATOI(x.prec, s); } } + if (strchr("hlL", *s) != NULL) { x.qual = *s++; } else { - x.qual = 0; + x.qual = '\0'; } if (x.qual == 'l' && *s == 'l') { @@ -89,38 +104,42 @@ s32 _Printf(PrintCallback pfn, void* arg, const char* fmt, va_list ap) { } _Putfld(&x, &ap, *s, ac); x.width -= x.n0 + x.nz0 + x.n1 + x.nz1 + x.n2 + x.nz2; - _PAD(x.width, spaces, !(x.flags & FLAGS_MINUS)); - _PROUT((char*)ac, x.n0); - _PAD(x.nz0, zeroes, 1); - _PROUT(x.s, x.n1); - _PAD(x.nz1, zeroes, 1); - _PROUT((char*)(&x.s[x.n1]), x.n2) - _PAD(x.nz2, zeroes, 1); - _PAD(x.width, spaces, x.flags & FLAGS_MINUS); - fmt = (char*)s + 1; + if (!(x.flags & FLAGS_MINUS)) { + PAD(spaces, x.width); + } + PUT(ac, x.n0); + PAD(zeroes, x.nz0); + PUT(x.s, x.n1); + PAD(zeroes, x.nz1); + PUT(&x.s[x.n1], x.n2) + PAD(zeroes, x.nz2); + if (x.flags & FLAGS_MINUS) { + PAD(spaces, x.width); + } + fmt = s + 1; } } -void _Putfld(_Pft* px, va_list* pap, u8 code, u8* ac) { +void _Putfld(_Pft* px, va_list* pap, char code, char* ac) { px->n0 = px->nz0 = px->n1 = px->nz1 = px->n2 = px->nz2 = 0; switch (code) { case 'c': - ac[px->n0++] = va_arg(*pap, u32); + ac[px->n0++] = va_arg(*pap, unsigned int); break; case 'd': case 'i': if (px->qual == 'l') { - px->v.ll = va_arg(*pap, s32); + px->v.ll = va_arg(*pap, long); } else if (px->qual == 'L') { - px->v.ll = va_arg(*pap, s64); + px->v.ll = va_arg(*pap, long long); } else { - px->v.ll = va_arg(*pap, s32); + px->v.ll = va_arg(*pap, int); } if (px->qual == 'h') { - px->v.ll = (s16)px->v.ll; + px->v.ll = (short)px->v.ll; } if (px->v.ll < 0) { @@ -131,75 +150,76 @@ void _Putfld(_Pft* px, va_list* pap, u8 code, u8* ac) { ac[px->n0++] = ' '; } - px->s = (char*)&ac[px->n0]; + px->s = &ac[px->n0]; _Litob(px, code); break; + case 'x': case 'X': case 'u': case 'o': if (px->qual == 'l') { - px->v.ll = va_arg(*pap, s32); + px->v.ll = va_arg(*pap, long); } else if (px->qual == 'L') { - px->v.ll = va_arg(*pap, s64); + px->v.ll = va_arg(*pap, long long); } else { - px->v.ll = va_arg(*pap, s32); + px->v.ll = va_arg(*pap, int); } if (px->qual == 'h') { - px->v.ll = (u16)px->v.ll; - } else if (px->qual == 0) { - px->v.ll = (u32)px->v.ll; + px->v.ll = (unsigned short)px->v.ll; + } else if (px->qual == '\0') { + px->v.ll = (unsigned int)px->v.ll; } if (px->flags & FLAGS_HASH) { ac[px->n0++] = '0'; if (code == 'x' || code == 'X') { - ac[px->n0++] = code; } } - px->s = (char*)&ac[px->n0]; + px->s = &ac[px->n0]; _Litob(px, code); break; + case 'e': case 'f': case 'g': case 'E': case 'G': - px->v.ld = px->qual == 'L' ? va_arg(*pap, f64) : va_arg(*pap, f64); + px->v.ld = px->qual == 'L' ? va_arg(*pap, LONG_DOUBLE_TYPE) : va_arg(*pap, double); - if (*(u16*)&px->v.ll & 0x8000) { + if (LDSIGN(px->v.ld)) { ac[px->n0++] = '-'; - } else { - if (px->flags & FLAGS_PLUS) { - ac[px->n0++] = '+'; - } else if (px->flags & FLAGS_SPACE) { - ac[px->n0++] = ' '; - } + } else if (px->flags & FLAGS_PLUS) { + ac[px->n0++] = '+'; + } else if (px->flags & FLAGS_SPACE) { + ac[px->n0++] = ' '; } - px->s = (char*)&ac[px->n0]; + px->s = &ac[px->n0]; _Ldtob(px, code); break; + case 'n': if (px->qual == 'h') { - *(va_arg(*pap, u16*)) = px->nchar; + *(va_arg(*pap, unsigned short*)) = px->nchar; } else if (px->qual == 'l') { - *va_arg(*pap, u32*) = px->nchar; + *va_arg(*pap, unsigned long*) = px->nchar; } else if (px->qual == 'L') { - *va_arg(*pap, u64*) = px->nchar; + *va_arg(*pap, unsigned long long*) = px->nchar; } else { - *va_arg(*pap, u32*) = px->nchar; + *va_arg(*pap, unsigned int*) = px->nchar; } break; case 'p': - px->v.ll = (s32)va_arg(*pap, void*); - px->s = (char*)&ac[px->n0]; + px->v.ll = (long)va_arg(*pap, void*); + px->s = &ac[px->n0]; _Litob(px, 'x'); break; + case 's': px->s = va_arg(*pap, char*); px->n1 = strlen(px->s); @@ -207,9 +227,11 @@ void _Putfld(_Pft* px, va_list* pap, u8 code, u8* ac) { px->n1 = px->prec; } break; + case '%': ac[px->n0++] = '%'; break; + default: ac[px->n0++] = code; break; diff --git a/src/libultra/os/afterprenmi.c b/src/libultra/os/afterprenmi.c index b59814763fc..2500d27bd81 100644 --- a/src/libultra/os/afterprenmi.c +++ b/src/libultra/os/afterprenmi.c @@ -1,5 +1,4 @@ -#include "ultra64.h" -#include "global.h" +#include "internal.h" s32 osAfterPreNMI(void) { return __osSpSetPc(0); diff --git a/src/libultra/os/createmesgqueue.c b/src/libultra/os/createmesgqueue.c index a85eadb0d70..3de8c897923 100644 --- a/src/libultra/os/createmesgqueue.c +++ b/src/libultra/os/createmesgqueue.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" void osCreateMesgQueue(OSMesgQueue* mq, OSMesg* msg, s32 count) { mq->mtqueue = (OSThread*)&__osThreadTail; diff --git a/src/libultra/os/createthread.c b/src/libultra/os/createthread.c index 8d54fb98719..02878e9e1d3 100644 --- a/src/libultra/os/createthread.c +++ b/src/libultra/os/createthread.c @@ -1,5 +1,6 @@ -#include "global.h" -#include "ultra64/asm.h" +#include "osint.h" +#include "R4300.h" +#include "asm.h" void osCreateThread(OSThread* thread, OSId id, void (*entry)(void*), void* arg, void* sp, OSPri pri) { register u32 prevInt; diff --git a/src/libultra/os/destroythread.c b/src/libultra/os/destroythread.c index a3273cadeed..7ee4f3b6b3c 100644 --- a/src/libultra/os/destroythread.c +++ b/src/libultra/os/destroythread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" void osDestroyThread(OSThread* thread) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/exceptasm.s b/src/libultra/os/exceptasm.s index 16ff4f57925..109c2e8c754 100644 --- a/src/libultra/os/exceptasm.s +++ b/src/libultra/os/exceptasm.s @@ -308,7 +308,7 @@ cart: // Load cart callback set by __osSetHWIntrRoutine lui $t1, %hi(__osHwIntTable) addiu $t1, %lo(__osHwIntTable) - lw $t2, (OS_INTR_CART*HWINT_SIZE+HWINT_CALLBACK)($t1) + lw $t2, (OS_INTR_CART*HWINT_SIZE+HWINT_HANDLER)($t1) // Mask out interrupt li $at, ~CAUSE_IP4 and $s0, $s0, $at @@ -317,7 +317,7 @@ cart: addi $t1, $t1, (OS_INTR_CART*HWINT_SIZE) // Set up a stack and run the callback jalr $t2 - lw $sp, HWINT_SP($t1) + lw $sp, HWINT_STACK($t1) beqz $v0, send_cart_mesg nop // Redispatch immediately if the callback returned nonzero @@ -455,14 +455,14 @@ pi: // Load pi callback lui $t1, %hi(__osPiIntTable) addiu $t1, %lo(__osPiIntTable) - lw $t2, HWINT_CALLBACK($t1) + lw $t2, HWINT_HANDLER($t1) // Mask out pi interrupt andi $s1, $s1, (MI_INTR_SP | MI_INTR_SI | MI_INTR_AI | MI_INTR_VI | MI_INTR_DP) // Skip callback if NULL beqz $t2, no_pi_callback nop // Set up a stack and run the callback - lw $sp, HWINT_SP($t1) + lw $sp, HWINT_STACK($t1) jalr $t2 move $a0, $v0 // If the callback returns non-zero, don't post a pi event message diff --git a/src/libultra/os/getactivequeue.c b/src/libultra/os/getactivequeue.c index de55d7f0416..9599c45c19e 100644 --- a/src/libultra/os/getactivequeue.c +++ b/src/libultra/os/getactivequeue.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" OSThread* __osGetActiveQueue(void) { return __osActiveQueue; diff --git a/src/libultra/os/getcurrfaultedthread.c b/src/libultra/os/getcurrfaultedthread.c index f36799b26fa..5c074dd2338 100644 --- a/src/libultra/os/getcurrfaultedthread.c +++ b/src/libultra/os/getcurrfaultedthread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" OSThread* __osGetCurrFaultedThread(void) { return __osFaultedThread; diff --git a/src/libultra/os/gethwintrroutine.c b/src/libultra/os/gethwintrroutine.c index d7d88978032..e8bfe3a0a77 100644 --- a/src/libultra/os/gethwintrroutine.c +++ b/src/libultra/os/gethwintrroutine.c @@ -1,7 +1,6 @@ -#include "global.h" -#include "ultra64/internal.h" +#include "internal.h" -void __osGetHWIntrRoutine(OSHWIntr intr, s32 (**callbackOut)(void), void** spOut) { - *callbackOut = __osHwIntTable[intr].callback; - *spOut = __osHwIntTable[intr].sp; +void __osGetHWIntrRoutine(OSHWIntr intr, s32 (**handler)(void), void** stackEnd) { + *handler = __osHwIntTable[intr].handler; + *stackEnd = __osHwIntTable[intr].stackEnd; } diff --git a/src/libultra/os/getmemsize.c b/src/libultra/os/getmemsize.c index 787346de636..180af02f8fd 100644 --- a/src/libultra/os/getmemsize.c +++ b/src/libultra/os/getmemsize.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "system.h" +#include "R4300.h" #define STEP 0x100000 diff --git a/src/libultra/os/getthreadid.c b/src/libultra/os/getthreadid.c index 792229c9bf9..28702868f32 100644 --- a/src/libultra/os/getthreadid.c +++ b/src/libultra/os/getthreadid.c @@ -1,9 +1,8 @@ -#include "global.h" +#include "osint.h" OSId osGetThreadId(OSThread* thread) { if (thread == NULL) { thread = __osRunningThread; } - return thread->id; } diff --git a/src/libultra/os/getthreadpri.c b/src/libultra/os/getthreadpri.c index 6c36a7c3b1a..05e9dd440e3 100644 --- a/src/libultra/os/getthreadpri.c +++ b/src/libultra/os/getthreadpri.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" OSPri osGetThreadPri(OSThread* thread) { if (thread == NULL) { diff --git a/src/libultra/os/gettime.c b/src/libultra/os/gettime.c index 2ca90b0df99..43f06f3d063 100644 --- a/src/libultra/os/gettime.c +++ b/src/libultra/os/gettime.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" OSTime osGetTime(void) { u32 count; diff --git a/src/libultra/os/initialize.c b/src/libultra/os/initialize.c index 3266d1e4ddc..d94e922cda5 100644 --- a/src/libultra/os/initialize.c +++ b/src/libultra/os/initialize.c @@ -1,4 +1,6 @@ -#include "global.h" +#include "internal.h" +#include "rcp.h" +#include "piint.h" typedef struct { u32 inst1; // lui $k0, %hi(__osException) @@ -38,10 +40,10 @@ void __osInitialize_common(void) { __osSetFpcCsr(FPCSR_FS | FPCSR_EV); __osSetWatchLo(0x04900000); - while (__osSiRawReadIo((void*)(PIF_RAM_END - 3), &pifdata)) { + while (__osSiRawReadIo(PIF_RAM_END - 3, &pifdata)) { ; } - while (__osSiRawWriteIo((void*)(PIF_RAM_END - 3), pifdata | 8)) { + while (__osSiRawWriteIo(PIF_RAM_END - 3, pifdata | 8)) { ; } diff --git a/src/libultra/os/jammesg.c b/src/libultra/os/jammesg.c index e0ca40fc68e..dfb4ccbc722 100644 --- a/src/libultra/os/jammesg.c +++ b/src/libultra/os/jammesg.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" s32 osJamMesg(OSMesgQueue* mq, OSMesg msg, s32 flag) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/parameters.s b/src/libultra/os/parameters.s index c0020ddbede..367bbf445a4 100644 --- a/src/libultra/os/parameters.s +++ b/src/libultra/os/parameters.s @@ -1,4 +1,5 @@ #include "ultra64/asm.h" +#include "ultra64/system.h" .section .text @@ -17,6 +18,6 @@ IPL_SYMBOL osResetType, 0x8000030C, 4 IPL_SYMBOL osCicId, 0x80000310, 4 IPL_SYMBOL osVersion, 0x80000314, 4 IPL_SYMBOL osMemSize, 0x80000318, 4 -IPL_SYMBOL osAppNMIBuffer, 0x8000031C, 0x40 +IPL_SYMBOL osAppNMIBuffer, 0x8000031C, OS_APP_NMI_BUFSIZE .fill 0x60 diff --git a/src/libultra/os/recvmesg.c b/src/libultra/os/recvmesg.c index f065188cd03..dcdd069f9fb 100644 --- a/src/libultra/os/recvmesg.c +++ b/src/libultra/os/recvmesg.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" s32 osRecvMesg(OSMesgQueue* mq, OSMesg* msg, s32 flag) { register u32 prevInt = __osDisableInt(); @@ -24,6 +24,5 @@ s32 osRecvMesg(OSMesgQueue* mq, OSMesg* msg, s32 flag) { } __osRestoreInt(prevInt); - return 0; } diff --git a/src/libultra/os/resetglobalintmask.c b/src/libultra/os/resetglobalintmask.c index c4187318388..04ce66b3c46 100644 --- a/src/libultra/os/resetglobalintmask.c +++ b/src/libultra/os/resetglobalintmask.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "internal.h" void __osResetGlobalIntMask(OSHWIntr mask) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/sendmesg.c b/src/libultra/os/sendmesg.c index df0bc2bee47..ab0d5ebf659 100644 --- a/src/libultra/os/sendmesg.c +++ b/src/libultra/os/sendmesg.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" s32 osSendMesg(OSMesgQueue* mq, OSMesg msg, s32 flag) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/seteventmesg.c b/src/libultra/os/seteventmesg.c index d105850ee1a..d0e9235f748 100644 --- a/src/libultra/os/seteventmesg.c +++ b/src/libultra/os/seteventmesg.c @@ -1,5 +1,4 @@ -#include "global.h" -#include "ultra64/internal.h" +#include "osint.h" __OSEventState __osEventStateTab[OS_NUM_EVENTS]; diff --git a/src/libultra/os/setglobalintmask.c b/src/libultra/os/setglobalintmask.c index 8c4aebb9264..7b7cc3f4e99 100644 --- a/src/libultra/os/setglobalintmask.c +++ b/src/libultra/os/setglobalintmask.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "internal.h" void __osSetGlobalIntMask(OSHWIntr mask) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/sethwintrroutine.c b/src/libultra/os/sethwintrroutine.c index e6bb74f0f3e..9f8e4fea0f2 100644 --- a/src/libultra/os/sethwintrroutine.c +++ b/src/libultra/os/sethwintrroutine.c @@ -1,11 +1,10 @@ -#include "global.h" -#include "ultra64/internal.h" +#include "osint.h" -void __osSetHWIntrRoutine(OSHWIntr intr, s32 (*callback)(void), void* sp) { +void __osSetHWIntrRoutine(OSHWIntr intr, s32 (*handler)(void), void* stackEnd) { register u32 prevInt = __osDisableInt(); - __osHwIntTable[intr].callback = callback; - __osHwIntTable[intr].sp = sp; + __osHwIntTable[intr].handler = handler; + __osHwIntTable[intr].stackEnd = stackEnd; __osRestoreInt(prevInt); } diff --git a/src/libultra/os/setthreadpri.c b/src/libultra/os/setthreadpri.c index a9a1b0eb6e8..11c560b7790 100644 --- a/src/libultra/os/setthreadpri.c +++ b/src/libultra/os/setthreadpri.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" void osSetThreadPri(OSThread* thread, OSPri pri) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/settimer.c b/src/libultra/os/settimer.c index 7775b987bfb..9c6317cfde9 100644 --- a/src/libultra/os/settimer.c +++ b/src/libultra/os/settimer.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "osint.h" +#include "../macros.h" // TODO s32 osSetTimer(OSTimer* timer, OSTime countdown, OSTime interval, OSMesgQueue* mq, OSMesg msg) { UNUSED OSTime time; diff --git a/src/libultra/os/startthread.c b/src/libultra/os/startthread.c index 6594fd5ae57..57024284de6 100644 --- a/src/libultra/os/startthread.c +++ b/src/libultra/os/startthread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" void osStartThread(OSThread* thread) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/stopthread.c b/src/libultra/os/stopthread.c index 54a236b49bb..a7927416f53 100644 --- a/src/libultra/os/stopthread.c +++ b/src/libultra/os/stopthread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" void osStopThread(OSThread* thread) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/os/stoptimer.c b/src/libultra/os/stoptimer.c index d6d20536a12..c4afc2c2953 100644 --- a/src/libultra/os/stoptimer.c +++ b/src/libultra/os/stoptimer.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" s32 osStopTimer(OSTimer* timer) { register u32 prevInt; diff --git a/src/libultra/os/thread.c b/src/libultra/os/thread.c index c78a34e4eca..f85fe909a98 100644 --- a/src/libultra/os/thread.c +++ b/src/libultra/os/thread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" __OSThreadTail __osThreadTail = { NULL, OS_PRIORITY_THREADTAIL }; OSThread* __osRunQueue = (OSThread*)&__osThreadTail; diff --git a/src/libultra/os/timerintr.c b/src/libultra/os/timerintr.c index 7e2e3613884..ff65c7aca39 100644 --- a/src/libultra/os/timerintr.c +++ b/src/libultra/os/timerintr.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" OSTimer __osBaseTimer; OSTime __osCurrentTime; diff --git a/src/libultra/os/virtualtophysical.c b/src/libultra/os/virtualtophysical.c index 5f8f79d83c3..3bb9ebb73eb 100644 --- a/src/libultra/os/virtualtophysical.c +++ b/src/libultra/os/virtualtophysical.c @@ -1,4 +1,5 @@ -#include "global.h" +#include "osint.h" +#include "R4300.h" u32 osVirtualToPhysical(void* vaddr) { if (IS_KSEG0(vaddr)) { diff --git a/src/libultra/os/yieldthread.c b/src/libultra/os/yieldthread.c index af9e15a2c1c..0d8b8103142 100644 --- a/src/libultra/os/yieldthread.c +++ b/src/libultra/os/yieldthread.c @@ -1,4 +1,4 @@ -#include "global.h" +#include "osint.h" void osYieldThread(void) { register u32 prevInt = __osDisableInt(); diff --git a/src/libultra/rmon/sprintf.c b/src/libultra/rmon/sprintf.c deleted file mode 100644 index a149a1eafaf..00000000000 --- a/src/libultra/rmon/sprintf.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "global.h" - -void* proutSprintf(void* dst, const char* fmt, u32 size) { - return (void*)((u32)memcpy(dst, fmt, size) + size); -} - -s32 vsprintf(char* dst, const char* fmt, va_list args) { - s32 ret = _Printf(proutSprintf, dst, fmt, args); - if (ret > -1) { - dst[ret] = '\0'; - } - return ret; -} - -s32 sprintf(char* dst, const char* fmt, ...) { - s32 ret; - va_list args; - va_start(args, fmt); - - ret = _Printf(proutSprintf, dst, fmt, args); - if (ret > -1) { - dst[ret] = '\0'; - } - - va_end(args); - - return ret; -} diff --git a/src/libultra/rmon/xlitob.c b/src/libultra/rmon/xlitob.c deleted file mode 100644 index 2f8152a1095..00000000000 --- a/src/libultra/rmon/xlitob.c +++ /dev/null @@ -1,56 +0,0 @@ -#include "global.h" - -#define BUFF_LEN 0x18 - -u8 D_8000AF70[] = "0123456789abcdef"; -u8 D_8000AF84[] = "0123456789ABCDEF"; - -void _Litob(_Pft* args, u8 type) { - u8 buff[BUFF_LEN]; - const u8* numMap; - s32 base; - s32 idx; - u64 num; - lldiv_t quotrem; - - if (type == 'X') { - numMap = D_8000AF84; - } else { - numMap = D_8000AF70; - } - - base = (type == 'o') ? 8 : ((type != 'x' && type != 'X') ? 10 : 16); - idx = BUFF_LEN; - num = args->v.ll; - - if ((type == 'd' || type == 'i') && args->v.ll < 0) { - num = -num; - } - - if (num != 0 || args->prec != 0) { - buff[--idx] = numMap[num % base]; - } - - args->v.ll = num / base; - - while (args->v.ll > 0 && idx > 0) { - quotrem = lldiv(args->v.ll, base); - args->v.ll = quotrem.quot; - buff[--idx] = numMap[quotrem.rem]; - } - - args->n1 = BUFF_LEN - idx; - - memcpy(args->s, buff + idx, args->n1); - - if (args->n1 < args->prec) { - args->nz0 = args->prec - args->n1; - } - - if (args->prec < 0 && (args->flags & (FLAGS_ZERO | FLAGS_MINUS)) == FLAGS_ZERO) { - idx = args->width - args->n0 - args->nz0 - args->n1; - if (idx > 0) { - args->nz0 += idx; - } - } -} diff --git a/src/overlays/actors/ovl_Door_Gerudo/z_door_gerudo.c b/src/overlays/actors/ovl_Door_Gerudo/z_door_gerudo.c index 432045a500f..55b4045a1fb 100644 --- a/src/overlays/actors/ovl_Door_Gerudo/z_door_gerudo.c +++ b/src/overlays/actors/ovl_Door_Gerudo/z_door_gerudo.c @@ -70,7 +70,7 @@ f32 func_809946BC(PlayState* play, DoorGerudo* this, f32 arg2, f32 arg3, f32 arg func_8002DBD0(&this->dyna.actor, &sp1C, &playerPos); if ((arg3 < fabsf(sp1C.x)) || (arg4 < fabsf(sp1C.y))) { - return FLT_MAX; + return MAXFLOAT; } else { return sp1C.z; } diff --git a/src/overlays/actors/ovl_Door_Shutter/z_door_shutter.c b/src/overlays/actors/ovl_Door_Shutter/z_door_shutter.c index 152f92f38ac..85dd58c5f5b 100644 --- a/src/overlays/actors/ovl_Door_Shutter/z_door_shutter.c +++ b/src/overlays/actors/ovl_Door_Shutter/z_door_shutter.c @@ -500,7 +500,7 @@ f32 DoorShutter_GetPlayerDistance(PlayState* play, DoorShutter* this, f32 offset func_8002DBD0(&this->dyna.actor, &relPlayerPos, &playerPos); if (fabsf(relPlayerPos.x) > maxDistSides || fabsf(relPlayerPos.y) > maxDistY) { - return FLT_MAX; + return MAXFLOAT; } else { return relPlayerPos.z; } diff --git a/src/overlays/actors/ovl_player_actor/z_player.c b/src/overlays/actors/ovl_player_actor/z_player.c index e631135f03c..001ca37bbc4 100644 --- a/src/overlays/actors/ovl_player_actor/z_player.c +++ b/src/overlays/actors/ovl_player_actor/z_player.c @@ -10790,7 +10790,7 @@ void Player_UpdateCommon(Player* this, PlayState* play, Input* input) { this->targetActorDistance = 0.0f; } else { this->targetActor = NULL; - this->targetActorDistance = FLT_MAX; + this->targetActorDistance = MAXFLOAT; this->exchangeItemId = EXCH_ITEM_NONE; } @@ -10810,7 +10810,7 @@ void Player_UpdateCommon(Player* this, PlayState* play, Input* input) { } this->stateFlags2 &= ~PLAYER_STATE2_23; - this->closestSecretDistSq = FLT_MAX; + this->closestSecretDistSq = MAXFLOAT; temp_f0 = this->actor.world.pos.y - this->actor.prevPos.y;