sm64

A Super Mario 64 decompilation
Log | Files | Refs | README | LICENSE

osCreateViManager.c (3571B)


      1 #include "libultra_internal.h"
      2 
      3 #define OS_VI_MANAGER_MESSAGE_BUFF_SIZE 5
      4 
      5 OSMgrArgs viMgrMainArgs = { 0 };
      6 static OSThread viMgrThread;
      7 static u32 viMgrStack[0x400]; // stack bottom
      8 static OSMesgQueue __osViMesgQueue;
      9 static OSMesg viMgrMesgBuff[OS_VI_MANAGER_MESSAGE_BUFF_SIZE];
     10 
     11 static OSIoMesg viEventViMesg;
     12 static OSIoMesg viEventCounterMesg;
     13 
     14 extern void __osTimerServicesInit(void);
     15 extern void __osTimerInterrupt(void);
     16 extern OSTime __osCurrentTime;
     17 extern u32 __osBaseCounter;
     18 extern u32 __osViIntrCount;
     19 void viMgrMain(void *);
     20 
     21 #ifdef VERSION_CN
     22 u32 __additional_scanline = 0;
     23 #endif
     24 
     25 void osCreateViManager(OSPri pri) {
     26     u32 int_disabled;
     27     OSPri newPri;
     28     OSPri currentPri;
     29     if (!viMgrMainArgs.initialized) {
     30         __osTimerServicesInit();
     31 #ifdef VERSION_CN
     32         __additional_scanline = 0;
     33 #endif
     34         osCreateMesgQueue(&__osViMesgQueue, &viMgrMesgBuff[0], OS_VI_MANAGER_MESSAGE_BUFF_SIZE);
     35         viEventViMesg.hdr.type = 13;
     36         viEventViMesg.hdr.pri = 0;
     37         viEventViMesg.hdr.retQueue = 0;
     38         viEventCounterMesg.hdr.type = 14;
     39         viEventCounterMesg.hdr.pri = 0;
     40         viEventCounterMesg.hdr.retQueue = 0;
     41         osSetEventMesg(OS_EVENT_VI, &__osViMesgQueue, &viEventViMesg);
     42         osSetEventMesg(OS_EVENT_COUNTER, &__osViMesgQueue, &viEventCounterMesg);
     43         newPri = -1;
     44         currentPri = osGetThreadPri(NULL);
     45         if (currentPri < pri) {
     46             newPri = currentPri;
     47             osSetThreadPri(NULL, pri);
     48         }
     49         int_disabled = __osDisableInt();
     50         viMgrMainArgs.initialized = TRUE;
     51         viMgrMainArgs.mgrThread = &viMgrThread;
     52         viMgrMainArgs.cmdQueue = &__osViMesgQueue;
     53         viMgrMainArgs.eventQueue = &__osViMesgQueue;
     54         viMgrMainArgs.accessQueue = NULL;
     55         viMgrMainArgs.dma_func = NULL;
     56 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN)
     57         viMgrMainArgs.edma_func = NULL;
     58 #endif
     59 
     60         osCreateThread(&viMgrThread, 0, viMgrMain, (void *) &viMgrMainArgs, &viMgrStack[0x400], pri);
     61         __osViInit();
     62         osStartThread(&viMgrThread);
     63         __osRestoreInt(int_disabled);
     64         if (newPri != -1) {
     65             osSetThreadPri(NULL, newPri);
     66         }
     67     }
     68 }
     69 
     70 void viMgrMain(void *vargs) {
     71     static u16 retrace;
     72     OSViContext *context;
     73     OSMgrArgs *args;
     74     OSMesg mesg;
     75     u32 sp28; // always 0
     76     u32 sp24; // time related
     77     mesg = NULL;
     78     sp28 = FALSE;
     79     context = __osViGetCurrentContext();
     80 
     81     if ((retrace = context->retraceCount) == 0) {
     82         retrace = 1;
     83     }
     84 
     85     args = (OSMgrArgs *) vargs;
     86 
     87     while (TRUE) {
     88         osRecvMesg(args->eventQueue, &mesg, OS_MESG_BLOCK);
     89         switch (*(u16 *) mesg) {
     90             case 13:
     91                 __osViSwapContext();
     92                 if (!--retrace) {
     93                     context = __osViGetCurrentContext();
     94                     if (context->mq != NULL) {
     95                         osSendMesg(context->mq, context->msg, OS_MESG_NOBLOCK);
     96                     }
     97                     retrace = context->retraceCount;
     98                 }
     99                 __osViIntrCount++;
    100                 if (sp28) {
    101                     sp24 = osGetCount();
    102                     __osCurrentTime = sp24;
    103                     sp28 = 0;
    104                 }
    105                 sp24 = __osBaseCounter;
    106                 __osBaseCounter = osGetCount();
    107                 sp24 = __osBaseCounter - sp24;
    108                 __osCurrentTime = __osCurrentTime + sp24;
    109                 break;
    110 
    111             case 14:
    112                 __osTimerInterrupt();
    113                 break;
    114         }
    115     }
    116 }