-
Notifications
You must be signed in to change notification settings - Fork 72
/
OSAlarm.c
205 lines (178 loc) · 4.72 KB
/
OSAlarm.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
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
#include <placeholder.h>
#include <dolphin/base/PPCArch.h>
#include <dolphin/os/OSAlarm.h>
#include <dolphin/os/OSContext.h>
#include <dolphin/os/OSException.h>
#include <dolphin/os/OSInit.h>
#include <dolphin/os/OSInterrupt.h>
#include <dolphin/os/OSThread.h>
#include <dolphin/os/OSTime.h>
static struct OSAlarmQueue {
OSAlarm* head;
OSAlarm* tail;
} AlarmQueue;
static void DecrementerExceptionHandler(__OSException exception,
OSContext* context);
void OSInitAlarm(void)
{
if (__OSGetExceptionHandler(8) != DecrementerExceptionHandler) {
AlarmQueue.tail = 0;
AlarmQueue.head = 0;
__OSSetExceptionHandler(8, DecrementerExceptionHandler);
}
}
void OSCreateAlarm(OSAlarm* alarm)
{
alarm->handler = NULL;
}
static void SetTimer(OSAlarm* alarm)
{
OSTime delta = alarm->fire - __OSGetSystemTime();
if (delta < 0) {
PPCMtdec(0);
} else if (delta < 0x80000000) {
PPCMtdec((u32) delta);
} else {
PPCMtdec(0x7fffffff);
}
}
static void InsertAlarm(OSAlarm* alarm, OSTime fire, OSAlarmHandler handler)
{
OSAlarm* next;
OSAlarm* prev;
if (0 < alarm->period) {
OSTime time = __OSGetSystemTime();
fire = alarm->start;
if (alarm->start < time) {
fire +=
alarm->period * ((time - alarm->start) / alarm->period + 1);
}
}
alarm->handler = handler;
alarm->fire = fire;
for (next = AlarmQueue.head; next; next = next->next) {
if (next->fire <= fire) {
continue;
}
alarm->prev = next->prev;
next->prev = alarm;
alarm->next = next;
prev = alarm->prev;
if (prev) {
prev->next = alarm;
} else {
AlarmQueue.head = alarm;
SetTimer(alarm);
}
return;
}
alarm->next = 0;
prev = AlarmQueue.tail;
AlarmQueue.tail = alarm;
alarm->prev = prev;
if (prev) {
prev->next = alarm;
} else {
AlarmQueue.head = AlarmQueue.tail = alarm;
SetTimer(alarm);
}
}
void OSSetAlarm(OSAlarm* alarm, OSTime tick, OSAlarmHandler handler)
{
u32 oldInt = OSDisableInterrupts();
alarm->period = 0;
InsertAlarm(alarm, __OSGetSystemTime() + tick, handler);
OSRestoreInterrupts(oldInt);
}
void OSSetPeriodicAlarm(OSAlarm* alarm, OSTime start, OSTime period,
OSAlarmHandler handler)
{
bool enabled = OSDisableInterrupts();
alarm->period = period;
alarm->start = __OSTimeToSystemTime(start);
InsertAlarm(alarm, 0, handler);
OSRestoreInterrupts(enabled);
}
void OSCancelAlarm(OSAlarm* alarm)
{
OSAlarm* next;
bool enabled;
enabled = OSDisableInterrupts();
if (alarm->handler == 0) {
OSRestoreInterrupts(enabled);
return;
}
next = alarm->next;
if (next == 0) {
AlarmQueue.tail = alarm->prev;
} else {
next->prev = alarm->prev;
}
if (alarm->prev) {
alarm->prev->next = next;
} else {
AlarmQueue.head = next;
if (next) {
SetTimer(next);
}
}
alarm->handler = 0;
OSRestoreInterrupts(enabled);
}
static void DecrementerExceptionCallback(register OSException exception,
register OSContext* context)
{
OSAlarm* alarm;
OSAlarm* next;
OSAlarmHandler handler;
OSTime time;
OSContext exceptionContext;
time = __OSGetSystemTime();
alarm = AlarmQueue.head;
if (alarm == 0) {
OSLoadContext(context);
}
if (time < alarm->fire) {
SetTimer(alarm);
OSLoadContext(context);
}
next = alarm->next;
AlarmQueue.head = next;
if (next == 0) {
AlarmQueue.tail = 0;
} else {
next->prev = 0;
}
handler = alarm->handler;
alarm->handler = 0;
if (0 < alarm->period) {
InsertAlarm(alarm, 0, handler);
}
if (AlarmQueue.head) {
SetTimer(AlarmQueue.head);
}
OSDisableScheduler();
OSClearContext(&exceptionContext);
OSSetCurrentContext(&exceptionContext);
handler(alarm, context);
OSClearContext(&exceptionContext);
OSSetCurrentContext(context);
OSEnableScheduler();
__OSReschedule();
OSLoadContext(context);
}
#ifdef MWERKS_GEKKO
static asm void DecrementerExceptionHandler(register __OSException exception,
register OSContext* context)
{ // clang-format off
nofralloc
OS_EXCEPTION_SAVE_GPRS(context)
b DecrementerExceptionCallback
} // clang-format on
#else
static void DecrementerExceptionHandler(register __OSException exception,
register OSContext* context)
{
NOT_IMPLEMENTED;
}
#endif