1
0
mirror of https://github.com/zeldaret/oot.git synced 2024-09-22 21:35:27 +00:00
oot/src/libultra_boot_O2/vimgr.c

118 lines
3.2 KiB
C
Raw Normal View History

2020-03-17 04:31:30 +00:00
#include <global.h>
2020-03-22 21:19:43 +00:00
typedef struct {
2020-03-17 04:31:30 +00:00
u16 unk00;
u8 unk02;
u32 unk04;
u8 pad[0xc];
u16 unk14;
u16 unk16;
} viMesgStruct;
OSThread viThread;
u8 viThreadStack[0x1000];
OSMesgQueue viEventQueue;
OSMesg viEventBuf[6];
viMesgStruct viRetraceMsg;
viMesgStruct viCounterMsg;
2020-03-22 21:19:43 +00:00
OSMgrArgs __osViDevMgr = { 0 };
2020-03-17 04:31:30 +00:00
u32 __additional_scanline = 0;
void viMgrMain(void*);
2020-03-22 21:19:43 +00:00
void osCreateViManager(OSPri pri) {
2020-03-17 04:31:30 +00:00
u32 int_disabled;
OSPri newPri;
OSPri currentPri;
2020-03-22 21:19:43 +00:00
if (!__osViDevMgr.initialized) {
2020-03-17 04:31:30 +00:00
__osTimerServicesInit();
__additional_scanline = 0;
osCreateMesgQueue(&viEventQueue, viEventBuf, 5);
viRetraceMsg.unk00 = 13;
viRetraceMsg.unk02 = 0;
viRetraceMsg.unk04 = 0;
viCounterMsg.unk00 = 14;
viCounterMsg.unk02 = 0;
viCounterMsg.unk04 = 0;
osSetEventMesg(OS_EVENT_VI, &viEventQueue, &viRetraceMsg);
osSetEventMesg(OS_EVENT_COUNTER, &viEventQueue, &viCounterMsg);
newPri = -1;
currentPri = osGetThreadPri(NULL);
2020-03-22 21:19:43 +00:00
if (currentPri < pri) {
2020-03-17 04:31:30 +00:00
newPri = currentPri;
osSetThreadPri(NULL, pri);
}
int_disabled = __osDisableInt();
__osViDevMgr.initialized = true;
__osViDevMgr.mgrThread = &viThread;
__osViDevMgr.cmdQueue = &viEventQueue;
__osViDevMgr.eventQueue = &viEventQueue;
__osViDevMgr.acccessQueue = NULL;
__osViDevMgr.piDmaCallback = NULL;
__osViDevMgr.epiDmaCallback = NULL;
2020-03-22 21:19:43 +00:00
osCreateThread(&viThread, 0, &viMgrMain, &__osViDevMgr, viThreadStack + sizeof(viThreadStack), pri);
2020-03-17 04:31:30 +00:00
__osViInit();
osStartThread(&viThread);
__osRestoreInt(int_disabled);
2020-03-22 21:19:43 +00:00
if (newPri != -1) {
2020-03-17 04:31:30 +00:00
osSetThreadPri(NULL, newPri);
}
}
}
2020-03-22 21:19:43 +00:00
void viMgrMain(void* vargs) {
2020-03-17 04:31:30 +00:00
OSMgrArgs* args;
static u16 viRetrace;
u32 addTime;
viMesgStruct* mesg;
u32 temp; // always 0
temp = 0;
mesg = NULL;
2020-03-22 21:19:43 +00:00
viRetrace = __osViGetCurrentContext()->retraceCount;
if (viRetrace == 0) {
2020-03-17 04:31:30 +00:00
viRetrace = 1;
2020-03-22 21:19:43 +00:00
}
2020-03-17 04:31:30 +00:00
args = (OSMgrArgs*)vargs;
2020-03-22 21:19:43 +00:00
while (1) {
2020-03-17 04:31:30 +00:00
osRecvMesg(args->eventQueue, (OSMesg)&mesg, OS_MESG_BLOCK);
2020-03-22 21:19:43 +00:00
switch (mesg->unk00) {
case 13:
__osViSwapContext();
viRetrace--;
if (!viRetrace) {
OSViContext* ctx = __osViGetCurrentContext();
if (ctx->mq) {
osSendMesg(ctx->mq, ctx->msg, OS_MESG_NOBLOCK);
}
viRetrace = ctx->retraceCount;
}
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
__osViIntrCount++;
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
// block optimized out since temp is always 0,
// but it changes register allocation and ordering for __osCurrentTime
if (temp != 0) {
addTime = osGetCount();
__osCurrentTime = addTime;
temp = 0;
}
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
addTime = __osBaseCounter;
__osBaseCounter = osGetCount();
addTime = __osBaseCounter - addTime;
__osCurrentTime = __osCurrentTime + addTime;
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
break;
2020-03-17 04:31:30 +00:00
2020-03-22 21:19:43 +00:00
case 14:
__osTimerInterrupt();
break;
2020-03-17 04:31:30 +00:00
}
}
}