forked from lukaszdk/ps2doom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer_thread.c
100 lines (83 loc) · 2.23 KB
/
mixer_thread.c
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
/// Based on modplayer_thread.c
#include <tamtypes.h>
#include <ps2lib_err.h>
#include <kernel.h>
#include <sifrpc.h>
#include <mixer/mixer_thread.h>
#include <mixer/mixer.h>
#define THREAD_STACK_SIZE (8 * 1024)
static int thread_waitsema = -1;
static u8 thread_stack[THREAD_STACK_SIZE] ALIGNED(16);
static ee_thread_t thread_thread;
static ee_sema_t thread_playsema;
static int thread_threadid;
extern s32 main_thread_id;
s32 mixer_VRstartID = 0;
///
void Mixer_StartThread()
{
extern void *_gp;
/* Create a temporary semaphore that we'll use to wait for the RPC
thread to finish initializing. */
thread_playsema.init_count = 0;
thread_playsema.max_count = 1;
if ((thread_waitsema = CreateSema(&thread_playsema)) < 0)
{
printf("CREATE SEMA ERROR!!!\n");
return;
}
thread_thread.func = MixerThread_Play;
thread_thread.stack = thread_stack;
thread_thread.stack_size = THREAD_STACK_SIZE;
thread_thread.gp_reg = _gp;
thread_thread.initial_priority = 42;
if ((thread_threadid = CreateThread(&thread_thread)) < 0)
{
printf("CREATE THREAD ERROR!!!\n");
return;
}
StartThread(thread_threadid, NULL);
}
// DO NOT CALL THIS - this is the vblank handler function
int Mixer_Tick_IntHandler(int cause)
{
//iChangeThreadPriority(main_thread_id, 42);
iSignalSema(thread_waitsema);
//iRotateThreadReadyQueue(42);
// Must do at the end
asm ("sync.l ; ei");
return 0;
}
// This adds the vblank handler and inits it
void Mixer_AddVBlankHandler()
{
DI();
mixer_VRstartID = AddIntcHandler(2, Mixer_Tick_IntHandler, 0);
EnableIntc(2);
EI();
}
// DO NOT CALL THIS, this is for the thread for playing
// this is triggered by : Mixer_Tick_IntHandler
void MixerThread_Play(void *arg)
{
// DO THE MEAT IN HERE
while (1)
{
// Wait to be triggered from the Vblank
WaitSema(thread_waitsema);
/* Do Modplay tick */
Mixer_Tick();
}
}
// This stops and removes the player thread
void Mixer_StopThread()
{
TerminateThread(thread_threadid);
DeleteThread(thread_threadid);
}
// This disables and removes the vblank handler
void Mixer_RemoveVBlankHandler()
{
DisableIntc(2);
RemoveIntcHandler(2, mixer_VRstartID);
}