2019-08-25 00:46:40 -04:00
|
|
|
#include "libultra_internal.h"
|
|
|
|
|
|
|
|
#define OS_VI_MANAGER_MESSAGE_BUFF_SIZE 5
|
|
|
|
|
2020-12-03 14:26:38 -05:00
|
|
|
OSMgrArgs viMgrMainArgs = { 0 };
|
2020-02-03 00:51:26 -05:00
|
|
|
static OSThread viMgrThread;
|
|
|
|
static u32 viMgrStack[0x400]; // stack bottom
|
|
|
|
static OSMesgQueue __osViMesgQueue;
|
|
|
|
static OSMesg viMgrMesgBuff[OS_VI_MANAGER_MESSAGE_BUFF_SIZE];
|
2019-08-25 00:46:40 -04:00
|
|
|
|
2020-02-03 00:51:26 -05:00
|
|
|
static OSIoMesg viEventViMesg;
|
|
|
|
static OSIoMesg viEventCounterMesg;
|
2019-08-25 00:46:40 -04:00
|
|
|
|
|
|
|
extern void __osTimerServicesInit(void);
|
|
|
|
extern void __osTimerInterrupt(void);
|
|
|
|
extern OSTime _osCurrentTime;
|
|
|
|
extern u32 D_80365DA8;
|
2020-02-03 00:51:26 -05:00
|
|
|
extern u32 __osViIntrCount;
|
2019-08-25 00:46:40 -04:00
|
|
|
void viMgrMain(void *);
|
|
|
|
|
|
|
|
void osCreateViManager(OSPri pri) {
|
|
|
|
u32 int_disabled;
|
|
|
|
OSPri newPri;
|
|
|
|
OSPri currentPri;
|
|
|
|
if (!viMgrMainArgs.initialized) {
|
|
|
|
__osTimerServicesInit();
|
|
|
|
osCreateMesgQueue(&__osViMesgQueue, &viMgrMesgBuff[0], OS_VI_MANAGER_MESSAGE_BUFF_SIZE);
|
2020-02-03 00:51:26 -05:00
|
|
|
viEventViMesg.hdr.type = 13;
|
|
|
|
viEventViMesg.hdr.pri = 0;
|
|
|
|
viEventViMesg.hdr.retQueue = 0;
|
|
|
|
viEventCounterMesg.hdr.type = 14;
|
|
|
|
viEventCounterMesg.hdr.pri = 0;
|
|
|
|
viEventCounterMesg.hdr.retQueue = 0;
|
2019-08-25 00:46:40 -04:00
|
|
|
osSetEventMesg(OS_EVENT_VI, &__osViMesgQueue, &viEventViMesg);
|
|
|
|
osSetEventMesg(OS_EVENT_COUNTER, &__osViMesgQueue, &viEventCounterMesg);
|
|
|
|
newPri = -1;
|
|
|
|
currentPri = osGetThreadPri(NULL);
|
|
|
|
if (currentPri < pri) {
|
|
|
|
newPri = currentPri;
|
|
|
|
osSetThreadPri(NULL, pri);
|
|
|
|
}
|
|
|
|
int_disabled = __osDisableInt();
|
|
|
|
viMgrMainArgs.initialized = TRUE;
|
|
|
|
viMgrMainArgs.mgrThread = &viMgrThread;
|
2020-12-03 14:26:38 -05:00
|
|
|
viMgrMainArgs.cmdQueue = &__osViMesgQueue;
|
|
|
|
viMgrMainArgs.eventQueue = &__osViMesgQueue;
|
|
|
|
viMgrMainArgs.accessQueue = NULL;
|
2019-08-25 00:46:40 -04:00
|
|
|
viMgrMainArgs.dma_func = NULL;
|
2020-12-03 14:26:38 -05:00
|
|
|
#if defined(VERSION_EU) || defined(VERSION_SH)
|
2020-02-03 00:51:26 -05:00
|
|
|
viMgrMainArgs.edma_func = NULL;
|
|
|
|
#endif
|
|
|
|
|
2019-08-25 00:46:40 -04:00
|
|
|
osCreateThread(&viMgrThread, 0, viMgrMain, (void *) &viMgrMainArgs, &viMgrStack[0x400], pri);
|
|
|
|
__osViInit();
|
|
|
|
osStartThread(&viMgrThread);
|
|
|
|
__osRestoreInt(int_disabled);
|
|
|
|
if (newPri != -1) {
|
|
|
|
osSetThreadPri(NULL, newPri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-03 14:26:38 -05:00
|
|
|
|
2019-08-25 00:46:40 -04:00
|
|
|
void viMgrMain(void *vargs) {
|
2020-02-03 00:51:26 -05:00
|
|
|
static u16 retrace;
|
2019-08-25 00:46:40 -04:00
|
|
|
OSViContext *context;
|
|
|
|
OSMgrArgs *args;
|
|
|
|
OSMesg mesg;
|
|
|
|
u32 sp28; // always 0
|
|
|
|
u32 sp24; // time related
|
|
|
|
mesg = NULL;
|
|
|
|
sp28 = FALSE;
|
|
|
|
context = __osViGetCurrentContext();
|
|
|
|
|
2020-02-03 00:51:26 -05:00
|
|
|
if ((retrace = context->retraceCount) == 0) {
|
|
|
|
retrace = 1;
|
2019-08-25 00:46:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
args = (OSMgrArgs *) vargs;
|
|
|
|
|
2020-12-03 14:26:38 -05:00
|
|
|
while (TRUE) {
|
|
|
|
osRecvMesg(args->eventQueue, &mesg, OS_MESG_BLOCK);
|
2019-08-25 00:46:40 -04:00
|
|
|
switch (*(u16 *) mesg) {
|
|
|
|
case 13:
|
|
|
|
__osViSwapContext();
|
2020-02-03 00:51:26 -05:00
|
|
|
if (!--retrace) {
|
2019-08-25 00:46:40 -04:00
|
|
|
context = __osViGetCurrentContext();
|
|
|
|
if (context->mq != NULL) {
|
|
|
|
osSendMesg(context->mq, context->msg, OS_MESG_NOBLOCK);
|
|
|
|
}
|
2020-02-03 00:51:26 -05:00
|
|
|
retrace = context->retraceCount;
|
2019-08-25 00:46:40 -04:00
|
|
|
}
|
2020-02-03 00:51:26 -05:00
|
|
|
__osViIntrCount++;
|
2019-08-25 00:46:40 -04:00
|
|
|
if (sp28) {
|
|
|
|
sp24 = osGetCount();
|
|
|
|
_osCurrentTime = sp24;
|
|
|
|
sp28 = 0;
|
|
|
|
}
|
|
|
|
sp24 = D_80365DA8;
|
|
|
|
D_80365DA8 = osGetCount();
|
|
|
|
sp24 = D_80365DA8 - sp24;
|
|
|
|
_osCurrentTime = _osCurrentTime + sp24;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 14:
|
|
|
|
__osTimerInterrupt();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|