summaryrefslogtreecommitdiff
path: root/src/os/osCreateViManager.c
blob: fc82ba87c7488f9f6b58aa26ef143998d3959f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "libultra_internal.h"
#include <macros.h>

#define OS_VI_MANAGER_MESSAGE_BUFF_SIZE 5

OSMgrArgs viMgrMainArgs = { 0 };
static OSThread viMgrThread;
static u32 viMgrStack[0x400]; // stack bottom
static OSMesgQueue __osViMesgQueue;
static OSMesg viMgrMesgBuff[OS_VI_MANAGER_MESSAGE_BUFF_SIZE];

static OSIoMesg viEventViMesg;
static OSIoMesg viEventCounterMesg;

extern void __osTimerServicesInit(void);
extern void __osTimerInterrupt(void);
extern OSTime __osCurrentTime;
extern u32 __osBaseCounter;
extern u32 __osViIntrCount;
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);
        viEventViMesg.hdr.type = 13;
        viEventViMesg.hdr.pri = 0;
        viEventViMesg.hdr.retQueue = 0;
        viEventCounterMesg.hdr.type = 14;
        viEventCounterMesg.hdr.pri = 0;
        viEventCounterMesg.hdr.retQueue = 0;
        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;
        viMgrMainArgs.cmdQueue = &__osViMesgQueue;
        viMgrMainArgs.eventQueue = &__osViMesgQueue;
        viMgrMainArgs.accessQueue = NULL;
        viMgrMainArgs.piDmaCallback = NULL;
        viMgrMainArgs.epiDmaCallback = NULL;

        osCreateThread(&viMgrThread, 0, viMgrMain, (void*) &viMgrMainArgs, &viMgrStack[0x400], pri);
        __osViInit();
        osStartThread(&viMgrThread);
        __osRestoreInt(int_disabled);
        if (newPri != -1) {
            osSetThreadPri(NULL, newPri);
        }
    }
}

void viMgrMain(void* vargs) {
    static u16 retrace;
    OSViContext* context;
    OSMgrArgs* args;
    OSMesg mesg;
    u32 sp28; // always 0
    u32 sp24; // time related
    mesg = NULL;
    sp28 = false;
    context = __osViGetCurrentContext();

    if ((retrace = context->retraceCount) == 0) {
        retrace = 1;
    }

    args = (OSMgrArgs*) vargs;

    while (true) {
        osRecvMesg(args->eventQueue, &mesg, OS_MESG_BLOCK);
        switch (*(u16*) mesg) {
            case 13:
                __osViSwapContext();
                if (!--retrace) {
                    context = __osViGetCurrentContext();
                    if (context->mq != NULL) {
                        osSendMesg(context->mq, context->msg, OS_MESG_NOBLOCK);
                    }
                    retrace = context->retraceCount;
                }
                __osViIntrCount++;
                if (sp28) {
                    sp24 = osGetCount();
                    __osCurrentTime = sp24;
                    sp28 = 0;
                }
                sp24 = __osBaseCounter;
                __osBaseCounter = osGetCount();
                sp24 = __osBaseCounter - sp24;
                __osCurrentTime = __osCurrentTime + sp24;
                break;

            case 14:
                __osTimerInterrupt();
                break;
        }
    }
}