diff --git a/lib/asm/exception/__osDispatchThread.s b/lib/asm/exception/__osDispatchThread.s index 9a00113f..fe45b6bb 100644 --- a/lib/asm/exception/__osDispatchThread.s +++ b/lib/asm/exception/__osDispatchThread.s @@ -1,7 +1,7 @@ glabel __osDispatchThread -/* 0D4024 800D3424 3C04800E */ lui $a0, %hi(__RunQueue) # $a0, 0x800e +/* 0D4024 800D3424 3C04800E */ lui $a0, %hi(__osRunQueue) # $a0, 0x800e /* 0D4028 800D3428 0C034D05 */ jal __osPopThread -/* 0D402C 800D342C 24844888 */ addiu $a0, %lo(__RunQueue) # addiu $a0, $a0, 0x4888 +/* 0D402C 800D342C 24844888 */ addiu $a0, %lo(__osRunQueue) # addiu $a0, $a0, 0x4888 /* 0D4030 800D3430 3C01800E */ lui $at, %hi(__osRunningThread) # $at, 0x800e /* 0D4034 800D3434 AC224890 */ sw $v0, %lo(__osRunningThread)($at) /* 0D4038 800D3438 24080004 */ li $t0, 4 diff --git a/lib/asm/exception/__osException.s b/lib/asm/exception/__osException.s index d227e567..12038ec8 100644 --- a/lib/asm/exception/__osException.s +++ b/lib/asm/exception/__osException.s @@ -312,8 +312,8 @@ glabel __osException /* 0D3CE4 800D30E4 AD2A0000 */ sw $t2, ($t1) /* 0D3CE8 800D30E8 0C034C79 */ jal send_mesg /* 0D3CEC 800D30EC 24040070 */ li $a0, 112 -/* 0D3CF0 800D30F0 3C0A800E */ lui $t2, %hi(__RunQueue) # $t2, 0x800e -/* 0D3CF4 800D30F4 8D4A4888 */ lw $t2, %lo(__RunQueue)($t2) +/* 0D3CF0 800D30F0 3C0A800E */ lui $t2, %hi(__osRunQueue) # $t2, 0x800e +/* 0D3CF4 800D30F4 8D4A4888 */ lw $t2, %lo(__osRunQueue)($t2) /* 0D3CF8 800D30F8 2401EFFF */ li $at, -4097 /* 0D3CFC 800D30FC 02018024 */ and $s0, $s0, $at /* 0D3D00 800D3100 8D5B0118 */ lw $k1, 0x118($t2) @@ -346,23 +346,23 @@ glabel __osException /* 0D3D60 800D3160 10000001 */ b .L800D3168 /* 0D3D64 800D3164 00000000 */ nop .L800D3168: -/* 0D3D68 800D3168 3C0A800E */ lui $t2, %hi(__RunQueue) # $t2, 0x800e -/* 0D3D6C 800D316C 8D4A4888 */ lw $t2, %lo(__RunQueue)($t2) +/* 0D3D68 800D3168 3C0A800E */ lui $t2, %hi(__osRunQueue) # $t2, 0x800e +/* 0D3D6C 800D316C 8D4A4888 */ lw $t2, %lo(__osRunQueue)($t2) /* 0D3D70 800D3170 8F490004 */ lw $t1, 4($k0) /* 0D3D74 800D3174 8D4B0004 */ lw $t3, 4($t2) /* 0D3D78 800D3178 012B082A */ slt $at, $t1, $t3 /* 0D3D7C 800D317C 10200007 */ beqz $at, .L800D319C /* 0D3D80 800D3180 00000000 */ nop -/* 0D3D84 800D3184 3C04800E */ lui $a0, %hi(__RunQueue) # $a0, 0x800e +/* 0D3D84 800D3184 3C04800E */ lui $a0, %hi(__osRunQueue) # $a0, 0x800e /* 0D3D88 800D3188 03402825 */ move $a1, $k0 /* 0D3D8C 800D318C 0C034CF3 */ jal __osEnqueueThread -/* 0D3D90 800D3190 24844888 */ addiu $a0, %lo(__RunQueue) # addiu $a0, $a0, 0x4888 +/* 0D3D90 800D3190 24844888 */ addiu $a0, %lo(__osRunQueue) # addiu $a0, $a0, 0x4888 /* 0D3D94 800D3194 08034D09 */ j __osDispatchThread /* 0D3D98 800D3198 00000000 */ nop .L800D319C: -/* 0D3D9C 800D319C 3C09800E */ lui $t1, %hi(__RunQueue) # $t1, 0x800e -/* 0D3DA0 800D31A0 25294888 */ addiu $t1, %lo(__RunQueue) # addiu $t1, $t1, 0x4888 +/* 0D3D9C 800D319C 3C09800E */ lui $t1, %hi(__osRunQueue) # $t1, 0x800e +/* 0D3DA0 800D31A0 25294888 */ addiu $t1, %lo(__osRunQueue) # addiu $t1, $t1, 0x4888 /* 0D3DA4 800D31A4 8D2A0000 */ lw $t2, ($t1) /* 0D3DA8 800D31A8 AF4A0000 */ sw $t2, ($k0) /* 0D3DAC 800D31AC 08034D09 */ j __osDispatchThread diff --git a/lib/asm/exception/send_mesg.s b/lib/asm/exception/send_mesg.s index 24f4904f..b2ce7a64 100644 --- a/lib/asm/exception/send_mesg.s +++ b/lib/asm/exception/send_mesg.s @@ -40,10 +40,10 @@ glabel send_mesg /* 0D3E74 800D3274 0C034D05 */ jal __osPopThread /* 0D3E78 800D3278 01202025 */ move $a0, $t1 /* 0D3E7C 800D327C 00405025 */ move $t2, $v0 -/* 0D3E80 800D3280 3C04800E */ lui $a0, %hi(__RunQueue) # $a0, 0x800e +/* 0D3E80 800D3280 3C04800E */ lui $a0, %hi(__osRunQueue) # $a0, 0x800e /* 0D3E84 800D3284 01402825 */ move $a1, $t2 /* 0D3E88 800D3288 0C034CF3 */ jal __osEnqueueThread -/* 0D3E8C 800D328C 24844888 */ addiu $a0, %lo(__RunQueue) # addiu $a0, $a0, 0x4888 +/* 0D3E8C 800D328C 24844888 */ addiu $a0, %lo(__osRunQueue) # addiu $a0, $a0, 0x4888 .L800D3290: /* 0D3E90 800D3290 02400008 */ jr $s2 /* 0D3E94 800D3294 00000000 */ nop diff --git a/lib/asm/non_matchings/unknown_0CCF90/osSetThreadPri.s b/lib/asm/non_matchings/unknown_0CCF90/osSetThreadPri.s index 37d1296e..fbfda341 100644 --- a/lib/asm/non_matchings/unknown_0CCF90/osSetThreadPri.s +++ b/lib/asm/non_matchings/unknown_0CCF90/osSetThreadPri.s @@ -37,8 +37,8 @@ glabel osSetThreadPri /* 0CD4C8 800CC8C8 01802825 */ move $a1, $t4 .L800CC8CC: /* 0CD4CC 800CC8CC 3C0D800E */ lui $t5, %hi(__osRunningThread) # $t5, 0x800e -/* 0CD4D0 800CC8D0 3C0F800E */ lui $t7, %hi(__RunQueue) # $t7, 0x800e -/* 0CD4D4 800CC8D4 8DEF4888 */ lw $t7, %lo(__RunQueue)($t7) +/* 0CD4D0 800CC8D0 3C0F800E */ lui $t7, %hi(__osRunQueue) # $t7, 0x800e +/* 0CD4D4 800CC8D4 8DEF4888 */ lw $t7, %lo(__osRunQueue)($t7) /* 0CD4D8 800CC8D8 8DAD4890 */ lw $t5, %lo(__osRunningThread)($t5) /* 0CD4DC 800CC8DC 8DF90004 */ lw $t9, 4($t7) /* 0CD4E0 800CC8E0 8DAE0004 */ lw $t6, 4($t5) @@ -46,10 +46,10 @@ glabel osSetThreadPri /* 0CD4E8 800CC8E8 10200006 */ beqz $at, .L800CC904 /* 0CD4EC 800CC8EC 00000000 */ nop /* 0CD4F0 800CC8F0 24080002 */ li $t0, 2 -/* 0CD4F4 800CC8F4 3C04800E */ lui $a0, %hi(__RunQueue) # $a0, 0x800e +/* 0CD4F4 800CC8F4 3C04800E */ lui $a0, %hi(__osRunQueue) # $a0, 0x800e /* 0CD4F8 800CC8F8 A5A80010 */ sh $t0, 0x10($t5) /* 0CD4FC 800CC8FC 0C034CB3 */ jal __osEnqueueAndYield -/* 0CD500 800CC900 24844888 */ addiu $a0, %lo(__RunQueue) # addiu $a0, $a0, 0x4888 +/* 0CD500 800CC900 24844888 */ addiu $a0, %lo(__osRunQueue) # addiu $a0, $a0, 0x4888 .L800CC904: /* 0CD504 800CC904 0C034960 */ jal __osRestoreInt /* 0CD508 800CC908 02002025 */ move $a0, $s0 diff --git a/lib/asm/non_matchings/unknown_0D3020/osYieldThread.s b/lib/asm/non_matchings/unknown_0D3020/osYieldThread.s index fb7f2db7..c3469326 100644 --- a/lib/asm/non_matchings/unknown_0D3020/osYieldThread.s +++ b/lib/asm/non_matchings/unknown_0D3020/osYieldThread.s @@ -6,9 +6,9 @@ glabel osYieldThread /* 0D71C0 800D65C0 3C0F800E */ lui $t7, %hi(__osRunningThread) # $t7, 0x800e /* 0D71C4 800D65C4 8DEF4890 */ lw $t7, %lo(__osRunningThread)($t7) /* 0D71C8 800D65C8 240E0002 */ li $t6, 2 -/* 0D71CC 800D65CC 3C04800E */ lui $a0, %hi(__RunQueue) # $a0, 0x800e +/* 0D71CC 800D65CC 3C04800E */ lui $a0, %hi(__osRunQueue) # $a0, 0x800e /* 0D71D0 800D65D0 00408025 */ move $s0, $v0 -/* 0D71D4 800D65D4 24844888 */ addiu $a0, %lo(__RunQueue) # addiu $a0, $a0, 0x4888 +/* 0D71D4 800D65D4 24844888 */ addiu $a0, %lo(__osRunQueue) # addiu $a0, $a0, 0x4888 /* 0D71D8 800D65D8 0C034CB3 */ jal __osEnqueueAndYield /* 0D71DC 800D65DC A5EE0010 */ sh $t6, 0x10($t7) /* 0D71E0 800D65E0 0C034960 */ jal __osRestoreInt diff --git a/lib/src/os/osInitialize.c b/lib/src/os/osInitialize.c index 803e299f..dc6353db 100644 --- a/lib/src/os/osInitialize.c +++ b/lib/src/os/osInitialize.c @@ -4,10 +4,72 @@ #include "types.h" #include "macros.h" #include "ultra64.h" +#include "viint.h" + +typedef struct +{ + /* 0x0 */ unsigned int inst1; + /* 0x4 */ unsigned int inst2; + /* 0x8 */ unsigned int inst3; + /* 0xC */ unsigned int inst4; +} __osExceptionVector; +extern __osExceptionVector __osExceptionPreamble; u64 osClockRate = OS_CLOCK_RATE; u32 __osShutdown = 0; u32 __OSGlobalIntMask = OS_IM_ALL; s32 D_800E38B0 = 0; // 64DD flag? (Hints from SM64 code) +#if 0 +//Declaring this variable breaks things right away +extern s32 osViClock; //This is defined in another file somehow +u32 __osFinalrom; +void osInitialize() +{ + u32 pifdata; + u32 clock = 0; + __osFinalrom = TRUE; + __osSetSR(__osGetSR() | SR_CU1); //enable fpu + __osSetFpcCsr(FPCSR_FS | FPCSR_EV); //flush denorm to zero, enable invalid operation + + while (__osSiRawReadIo(PIF_RAM_END - 3, &pifdata)) //last byte of joychannel ram + { + ; + } + while (__osSiRawWriteIo(PIF_RAM_END - 3, pifdata | 8)) + { + ; //todo: magic contant + } + *(__osExceptionVector *)UT_VEC = __osExceptionPreamble; + *(__osExceptionVector *)XUT_VEC = __osExceptionPreamble; + *(__osExceptionVector *)ECC_VEC = __osExceptionPreamble; + *(__osExceptionVector *)E_VEC = __osExceptionPreamble; + osWritebackDCache((void *)UT_VEC, E_VEC - UT_VEC + sizeof(__osExceptionVector)); + osInvalICache((void *)UT_VEC, E_VEC - UT_VEC + sizeof(__osExceptionVector)); + osMapTLBRdb(); + osPiRawReadIo(4, &clock); //TODO: remove magic constant; + clock &= ~0xf; //clear lower 4 bits + if (clock != 0) + { + osClockRate = clock; + } + osClockRate = osClockRate * 3 / 4; + if (osResetType == 0 /*cold reset */) + { + bzero(osAppNmiBuffer, sizeof(osAppNmiBuffer)); + } + if (osTvType == OS_TV_TYPE_PAL) { + osViClock = VI_PAL_CLOCK; + } + else if (osTvType == OS_TV_TYPE_MPAL) + { + osViClock = VI_MPAL_CLOCK; + } + else + { + osViClock = VI_NTSC_CLOCK; + } +} +#else GLOBAL_ASM("lib/asm/non_matchings/unknown_0CCF90/osInitialize.s") +#endif diff --git a/lib/src/os/osSetThreadPri.c b/lib/src/os/osSetThreadPri.c index d4d52792..7c9a704a 100644 --- a/lib/src/os/osSetThreadPri.c +++ b/lib/src/os/osSetThreadPri.c @@ -1,7 +1,25 @@ /* The comment below is needed for this file to be picked up by generate_ld */ /* RAM_POS: 0x800CC840 */ -#include "types.h" -#include "macros.h" +#include "libultra_internal.h" -GLOBAL_ASM("lib/asm/non_matchings/unknown_0CCF90/osSetThreadPri.s") +extern OSThread *__osRunningThread; +extern OSThread *__osRunQueue; + +void osSetThreadPri(OSThread *t, OSPri pri) { + register u32 saveMask = __osDisableInt(); + if (t == NULL) + t = __osRunningThread; + if (t->priority != pri) { + t->priority = pri; + if (t != __osRunningThread && t->state != OS_STATE_STOPPED) { + __osDequeueThread(t->queue, t); + __osEnqueueThread(t->queue, t); + } + if (__osRunningThread->priority < __osRunQueue->priority) { + __osRunningThread->state = OS_STATE_RUNNABLE; + __osEnqueueAndYield(&__osRunQueue); + } + } + __osRestoreInt(saveMask); +} diff --git a/lib/src/os/osStartThread.c b/lib/src/os/osStartThread.c index f1325574..753e304b 100644 --- a/lib/src/os/osStartThread.c +++ b/lib/src/os/osStartThread.c @@ -4,25 +4,24 @@ #include "libultra_internal.h" extern OSThread *__osRunningThread; -extern OSThread *__RunQueue; //__osRunQueue +extern OSThread *__osRunQueue; -void osStartThread(OSThread *t) -{ +void osStartThread(OSThread *t) { register u32 saveMask = __osDisableInt(); switch (t->state) { case OS_STATE_WAITING: t->state = OS_STATE_RUNNABLE; - __osEnqueueThread(&__RunQueue, t); + __osEnqueueThread(&__osRunQueue, t); break; case OS_STATE_STOPPED: - if (t->queue == NULL || t->queue == &__RunQueue) { + if (t->queue == NULL || t->queue == &__osRunQueue) { t->state = OS_STATE_RUNNABLE; - __osEnqueueThread(&__RunQueue, t); + __osEnqueueThread(&__osRunQueue, t); } else { t->state = OS_STATE_WAITING; __osEnqueueThread(t->queue, t); - __osEnqueueThread(&__RunQueue, __osPopThread(t->queue)); + __osEnqueueThread(&__osRunQueue, __osPopThread(t->queue)); } break; } @@ -30,9 +29,9 @@ void osStartThread(OSThread *t) __osDispatchThread(); } else { - if (__osRunningThread->priority < __RunQueue->priority) { + if (__osRunningThread->priority < __osRunQueue->priority) { __osRunningThread->state = OS_STATE_RUNNABLE; - __osEnqueueAndYield(&__RunQueue); + __osEnqueueAndYield(&__osRunQueue); } } __osRestoreInt(saveMask); diff --git a/lib/src/unknown_0D29F0.c b/lib/src/unknown_0D29F0.c index 8d32f1e6..3beba182 100644 --- a/lib/src/unknown_0D29F0.c +++ b/lib/src/unknown_0D29F0.c @@ -31,6 +31,7 @@ __OSViContext vi[2] = {0}; __OSViContext *__osViCurr = &vi[0]; __OSViContext *__osViNext = &vi[1]; +//TODO: This should be defined above osInitialize #define VI_NTSC_CLOCK 48681812 s32 osViClock = VI_NTSC_CLOCK; diff --git a/lib/src/unknown_0D6C90.c b/lib/src/unknown_0D6C90.c index 0ec12769..ed9511c9 100644 --- a/lib/src/unknown_0D6C90.c +++ b/lib/src/unknown_0D6C90.c @@ -12,7 +12,7 @@ struct __osThreadTail } __osThreadTail; struct __osThreadTail __osThreadTail = {0, -1}; -OSThread *__RunQueue = &__osThreadTail.next; +OSThread *__osRunQueue = &__osThreadTail.next; OSThread *__osActiveQueue = &__osThreadTail.next; extern OSThread *__osRunningThread = NULL; @@ -31,7 +31,7 @@ extern void __osRestoreInt(u32); void osYieldThread(void){ register u32 saveMask = __osDisableInt(); __osRunningThread->state = OS_STATE_RUNNABLE; - __osEnqueueAndYield(&__RunQueue); + __osEnqueueAndYield(&__osRunQueue); __osRestoreInt(saveMask); }*/