summaryrefslogtreecommitdiff
path: root/src/JSystem/JAudio2/JASAudioThread.cpp
blob: e689a9607fd8c646f6ecbd16df8feb6b47d5a744 (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
#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JAudio2/JASAudioThread.h"
#include "JSystem/JAudio2/JASAiCtrl.h"
#include "JSystem/JAudio2/JASDriverIF.h"
#include "JSystem/JAudio2/JASDSPChannel.h"
#include "JSystem/JAudio2/JASDSPInterface.h"
#include "JSystem/JAudio2/JASHeapCtrl.h"
#include "JSystem/JAudio2/JASProbe.h"
#include "JSystem/JKernel/JKRSolidHeap.h"
#include "dolphin/dsp.h"

JASAudioThread::JASAudioThread(int stackSize, int msgCount, u32 threadPriority)
    : 
    JKRThread(JASDram, threadPriority, msgCount, stackSize),
    JASGlobalInstance<JASAudioThread>(true)
{
    sbPauseFlag = false;
    OSInitThreadQueue(&sThreadQueue);
}

void JASAudioThread::create(s32 threadPriority) {
	JASAudioThread* sAudioThread = new (JASDram, 0) JASAudioThread(threadPriority, 0x10, 0x1400);
    sAudioThread->setCurrentHeap(JKRGetSystemHeap());
	sAudioThread->resume();
}

void JASAudioThread::stop() {
    jamMessageBlock((void*)2);
}

volatile int JASAudioThread::snIntCount;

class Lock {
public:
    Lock() {
        mInterrupts = OSDisableInterrupts();
    }
    ~Lock() {
        OSRestoreInterrupts(mInterrupts);
    }
private:
    BOOL mInterrupts;
};

class JASChannel {
    u8 filler[0x108];
};

// NONMATCHING location of JASPoolAllocObject_MultiThreaded<JASChannel>
void* JASAudioThread::run() {
    OSInitFastCast();
    JASDriver::initAI(DMACallback);
    JASDsp::boot(DSPCallback);
    JASDsp::initBuffer();
    JASDSPChannel::initAll();

    JASPoolAllocObject_MultiThreaded<JASChannel>::newMemPool(0x48);
    JASDriver::startDMA();

    while (true) {
        OSMessage msg = waitMessageBlock();
        switch ((int)msg) {
        case AUDIOMSG_DMA:
            if (sbPauseFlag) {
                JASDriver::stopDMA();
                OSSleepThread(&sThreadQueue);
            }
            JASDriver::updateDac();
            JASDriver::updateDacCallback();
            break;

        case AUDIOMSG_DSP:
            snIntCount--;
            if (snIntCount == 0) {
                JASProbe::stop(7);
                JASDriver::finishDSPFrame();
            } else {
                JASProbe::start(2, "SFR_DSP");
                JASDriver::updateDSP();
                JASProbe::stop(2);
            }
            break;

        case AUDIOMSG_STOP:
            JASDriver::stopDMA();
            OSExitThread(NULL);
            break;
        }
    }
}

void JASAudioThread::DMACallback() {
    JASAudioThread* thread = getInstance();
	JASProbe::stop(4);
	JASProbe::start(4, "UPDATE-DAC");
	thread->sendMessage((void*)AUDIOMSG_DMA);
}

void JASAudioThread::DSPCallback(void*) {
    JASAudioThread* thread = getInstance();
	while (DSPCheckMailFromDSP() == 0) { }

	u32 mail = DSPReadMailFromDSP();
	if (mail >> 0x10 == 0xF355) {
		if ((mail & 0xFF00) == 0xFF00) {
            thread->sendMessage((void*)AUDIOMSG_DSP);
		} else {
			JASDsp::finishWork(mail);
		}
	}
}