summaryrefslogtreecommitdiff
path: root/arm9/lib/NitroSDK/src/SND_command.c
blob: 044d960f5fccbc1d5dc871b6295c49af1e52a61d (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "SND_alarm.h"
#include "SND_command.h"
#include "SND_work.h"
#include "OS_emulator.h"
#include "OS_system.h"
#include "OS_cache.h"
#include "PXI_fifo.h"

#define SND_CMD_WAIT_QUEUE_COUNT 8

static struct SNDCommand sCommandArray[SND_CMD_COUNT];
static struct SNDSharedWork sSharedWork;
static struct SNDCommand *sWaitingCommandListQueue[SND_CMD_WAIT_QUEUE_COUNT + 1];   // not sure why this is one element to large
static struct SNDCommand *sReserveList;
static struct SNDCommand *sReserveListEnd;
static struct SNDCommand *sFreeListEnd;
static s32 sWaitingCommandListQueueRead;
static s32 sWaitingCommandListQueueWrite;
static s32 sWaitingCommandListCount;
static u32 sCurrentTag;
static u32 sFinishedTag;
static struct SNDCommand *sFreeList;

static void InitPXI(void);
static void RequestCommandProc(void);
static struct SNDCommand *AllocCommand(void);
static BOOL IsCommandAvailable(void);

ARM_FUNC void SND_CommandInit(void) {
    InitPXI();
    sFreeList = sCommandArray;
    for (int i = 0; i < SND_CMD_COUNT - 1; i++) {
        sCommandArray[i].llNext = &sCommandArray[i+1];
    }
    sCommandArray[SND_CMD_COUNT - 1].llNext = NULL;
    sFreeListEnd = &sCommandArray[SND_CMD_COUNT - 1];
    sReserveList = NULL;
    sReserveListEnd = NULL;
    sWaitingCommandListCount = 0;
    sWaitingCommandListQueueRead = 0;
    sWaitingCommandListQueueWrite = 0;
    sCurrentTag = 1;
    sFinishedTag = 0;
    SNDi_SharedWork = &sSharedWork;
    SNDi_InitSharedWork(SNDi_SharedWork);

    struct SNDCommand *cmd = SND_AllocCommand(SND_CMD_FLAG_BLOCK);
    if (cmd == NULL)
        return;

    cmd->id = SND_CMD_SET_SHARED_WORK;
    cmd->arg[0] = (u32)SNDi_SharedWork;
    SND_PushCommand(cmd);
    (void)SND_FlushCommand(SND_CMD_FLAG_BLOCK);
}

ARM_FUNC const struct SNDCommand *SND_RecvCommandReply(u32 flags) {
    OSIntrMode oldirq = OS_DisableInterrupts();

    if (flags & SND_CMD_FLAG_BLOCK) {
        u32 tag = SNDi_GetFinishedCommandTag();
        while (sFinishedTag == tag) {
            (void)OS_RestoreInterrupts(oldirq);
            OS_SpinWait(100);
            oldirq = OS_DisableInterrupts();
            tag = SNDi_GetFinishedCommandTag();
        }
    } else {
        u32 tag = SNDi_GetFinishedCommandTag();
        if (sFinishedTag == tag) {
            (void)OS_RestoreInterrupts(oldirq);
            return NULL;
        }
    }

    struct SNDCommand *queueRead = sWaitingCommandListQueue[sWaitingCommandListQueueRead];

    if (++sWaitingCommandListQueueRead > SND_CMD_WAIT_QUEUE_COUNT)
        sWaitingCommandListQueueRead = 0;

    struct SNDCommand *cur = queueRead;
    while (cur->llNext != NULL)
        cur = cur->llNext;

    if (sFreeListEnd != NULL) {
        sFreeListEnd->llNext = queueRead;
    } else {
        sFreeList = queueRead;
    }

    sFreeListEnd = cur;
    sWaitingCommandListCount--;
    sFinishedTag++;

    (void)OS_RestoreInterrupts(oldirq);
    return queueRead;
}

ARM_FUNC struct SNDCommand *SND_AllocCommand(u32 flags) {
    struct SNDCommand *cmd;
    if (!IsCommandAvailable())
        return NULL;

    cmd = AllocCommand();
    if (cmd != NULL)
        return cmd;

    if ((flags & SND_CMD_FLAG_BLOCK) == 0)
        return NULL;

    if (SND_CountWaitingCommand() > 0) {
        while (SND_RecvCommandReply(SND_CMD_FLAG_NOBLOCK) != NULL) { }

        cmd = AllocCommand();
        if (cmd != NULL)
            return cmd;
    } else {
        (void)SND_FlushCommand(SND_CMD_FLAG_BLOCK);
    }

    RequestCommandProc();
    
    do {
        (void)SND_RecvCommandReply(SND_CMD_FLAG_BLOCK);
        cmd = AllocCommand();
    } while (cmd == NULL);
    return cmd;
}

ARM_FUNC void SND_PushCommand(struct SNDCommand *cmd) {
    OSIntrMode oldirq = OS_DisableInterrupts();

    struct SNDCommand *newend = cmd;
    if (sReserveListEnd == NULL) {
        sReserveList = cmd;
        sReserveListEnd = cmd;
    } else {
        sReserveListEnd->llNext = cmd;
        sReserveListEnd = cmd;
    }

    cmd->llNext = NULL;

    (void)OS_RestoreInterrupts(oldirq);
}

ARM_FUNC BOOL SND_FlushCommand(u32 flags) {
    OSIntrMode oldirq = OS_DisableInterrupts();

    if (sReserveList == NULL) {
        (void)OS_RestoreInterrupts(oldirq);
        return TRUE;
    }

    if (sWaitingCommandListCount >= SND_CMD_WAIT_QUEUE_COUNT) {
        if ((flags & SND_CMD_FLAG_BLOCK) == 0) {
            (void)OS_RestoreInterrupts(oldirq);
            return FALSE;
        }

        do {
            (void)SND_RecvCommandReply(SND_CMD_FLAG_BLOCK);
        } while (sWaitingCommandListCount >= SND_CMD_WAIT_QUEUE_COUNT);
    }

    DC_FlushRange(sCommandArray, sizeof(sCommandArray));

    s32 result = PXI_SendWordByFifo(7, (u32)sReserveList, 0);
    if (result < 0) {
        if ((flags & SND_CMD_FLAG_BLOCK) == 0) {
            (void)OS_RestoreInterrupts(oldirq);
            return FALSE;
        }

        result = PXI_SendWordByFifo(7, (u32)sReserveList, 0);
        while (result < 0) {
            (void)OS_RestoreInterrupts(oldirq);
            OS_SpinWait(100);
            oldirq = OS_DisableInterrupts();
            result = PXI_SendWordByFifo(7, (u32)sReserveList, 0);
        }
    }

    if ((flags & SND_CMD_FLAG_IMMEDIATE) != 0) {
        RequestCommandProc();
    }

    sWaitingCommandListQueue[sWaitingCommandListQueueWrite] = sReserveList;

    if (++sWaitingCommandListQueueWrite > SND_CMD_WAIT_QUEUE_COUNT) {
        sWaitingCommandListQueueWrite = 0;
    }

    sReserveList = NULL;
    sReserveListEnd = NULL;
    sWaitingCommandListCount++;
    sCurrentTag++;

    (void)OS_RestoreInterrupts(oldirq);
    return TRUE;
}

ARM_FUNC void SND_WaitForCommandProc(u32 tag) {
    if (SND_IsFinishedCommandTag(tag))
        return;

    while (SND_RecvCommandReply(SND_CMD_FLAG_NOBLOCK) != NULL) { }

    if (SND_IsFinishedCommandTag(tag))
        return;

    RequestCommandProc();

    if (SND_IsFinishedCommandTag(tag))
        return;

    do {
        (void)SND_RecvCommandReply(SND_CMD_FLAG_BLOCK);
    } while (SND_IsFinishedCommandTag(tag) == 0);
}

ARM_FUNC u32 SND_GetCurrentCommandTag(void) {
    OSIntrMode oldirq = OS_DisableInterrupts();

    u32 retval;
    if (sReserveList == NULL)
        retval = sFinishedTag;
    else
        retval = sCurrentTag;

    (void)OS_RestoreInterrupts(oldirq);
    return retval;
}

ARM_FUNC BOOL SND_IsFinishedCommandTag(u32 tag) {
    OSIntrMode oldirq = OS_DisableInterrupts();

    BOOL result;
    if (tag > sFinishedTag) {
        if (tag - sFinishedTag < 0x80000000)
            result = FALSE;
        else
            result = TRUE;
    } else {
        if (sFinishedTag - tag < 0x80000000)
            result = TRUE;
        else
            result = FALSE;
    }

    (void)OS_RestoreInterrupts(oldirq);
    return result;
}

ARM_FUNC s32 SND_CountFreeCommand(void) {
    OSIntrMode oldirq = OS_DisableInterrupts();
    
    s32 count = 0;
    for (struct SNDCommand *cmd = sFreeList; cmd != NULL; cmd = cmd->llNext)
        count++;

    (void)OS_RestoreInterrupts(oldirq);
    return count;
}

ARM_FUNC s32 SND_CountReservedCommand(void) {
    OSIntrMode oldirq = OS_DisableInterrupts();
    
    s32 count = 0;
    for (struct SNDCommand *cmd = sReserveList; cmd != NULL; cmd = cmd->llNext)
        count++;

    (void)OS_RestoreInterrupts(oldirq);
    return count;
}

ARM_FUNC s32 SND_CountWaitingCommand(void) {
    return SND_CMD_COUNT - SND_CountFreeCommand() - SND_CountReservedCommand();
}

ARM_FUNC static void PxiFifoCallback(PXIFifoTag tag, u32 data, BOOL) {
#pragma unused (tag)
    OSIntrMode oldirq = OS_DisableInterrupts();
    SNDi_CallAlarmHandler((s32)data);
    (void)OS_RestoreInterrupts(oldirq);
}

ARM_FUNC static void InitPXI(void) {
    PXI_SetFifoRecvCallback(PXI_FIFO_TAG_SOUND, PxiFifoCallback);

    if (!IsCommandAvailable())
        return;

    if (PXI_IsCallbackReady(PXI_FIFO_TAG_SOUND, PXI_PROC_ARM7))
        return;

    do {
        OS_SpinWait(100);
    } while (!PXI_IsCallbackReady(PXI_FIFO_TAG_SOUND, PXI_PROC_ARM7));
}

ARM_FUNC static void RequestCommandProc(void) {
    while (PXI_SendWordByFifo(7, 0, 0) < 0) { }
}

ARM_FUNC static struct SNDCommand *AllocCommand(void) {
    OSIntrMode oldirq = OS_DisableInterrupts();
    if (sFreeList == NULL) {
        (void)OS_RestoreInterrupts(oldirq);
        return NULL;
    }

    struct SNDCommand *retval = sFreeList;

    sFreeList = sFreeList->llNext;
    if (sFreeList == NULL)
        sFreeListEnd = NULL;
    (void)OS_RestoreInterrupts(oldirq);
    return retval;
}

ARM_FUNC static BOOL IsCommandAvailable(void) {
    if (!OS_IsRunOnEmulator())
        return TRUE;

    OSIntrMode oldirq = OS_DisableInterrupts();
    // TODO use proper register names here
    // is this some kind of debug or ensata register?
    *(vu32 *)0x4FFF200 = 0x10;
    u32 resp = *(vu32 *)0x4FFF200;
    (void)OS_RestoreInterrupts(oldirq);
    return resp != 0;
}