summaryrefslogtreecommitdiff
path: root/src/libultra_boot_O2/vimgr.c
blob: de54494f99b2d9d14b003b74f98cd7bb367ab30f (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <global.h>

typedef struct
{
    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;
OSMgrArgs __osViDevMgr = {0};
u32 __additional_scanline = 0;

void viMgrMain(void*);

void osCreateViManager(OSPri pri)
{
    u32 int_disabled;
    OSPri newPri;
    OSPri currentPri;
    if (!__osViDevMgr.initialized)
    {
        __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);
        if (currentPri < pri)
        {
            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;

        osCreateThread(&viThread, 0, &viMgrMain, &__osViDevMgr, viThreadStack+sizeof(viThreadStack), pri);
        __osViInit();
        osStartThread(&viThread);
        __osRestoreInt(int_disabled);
        if (newPri != -1)
        {
            osSetThreadPri(NULL, newPri);
        }
    }
}

void viMgrMain(void* vargs)
{
    OSMgrArgs* args;
    static u16 viRetrace;
    u32 addTime;
    viMesgStruct* mesg;
    u32 temp; // always 0

    temp = 0;
    mesg = NULL;
    viRetrace =  __osViGetCurrentContext()->retraceCount;
    if (viRetrace == 0)
        viRetrace = 1;

    args = (OSMgrArgs*)vargs;

    while (1)
    {
        osRecvMesg(args->eventQueue, (OSMesg)&mesg, OS_MESG_BLOCK);
        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;
            }

            __osViIntrCount++;

            // 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;
            }

            addTime = __osBaseCounter;
            __osBaseCounter = osGetCount();
            addTime = __osBaseCounter - addTime;
            __osCurrentTime = __osCurrentTime + addTime;

            break;

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