diff options
Diffstat (limited to 'apps/plugins/sdl/src/timer/macos')
-rw-r--r-- | apps/plugins/sdl/src/timer/macos/FastTimes.c | 352 | ||||
-rw-r--r-- | apps/plugins/sdl/src/timer/macos/FastTimes.h | 27 | ||||
-rw-r--r-- | apps/plugins/sdl/src/timer/macos/SDL_MPWtimer.c | 152 | ||||
-rw-r--r-- | apps/plugins/sdl/src/timer/macos/SDL_systimer.c | 186 |
4 files changed, 717 insertions, 0 deletions
diff --git a/apps/plugins/sdl/src/timer/macos/FastTimes.c b/apps/plugins/sdl/src/timer/macos/FastTimes.c new file mode 100644 index 0000000000..2da74b7d18 --- /dev/null +++ b/apps/plugins/sdl/src/timer/macos/FastTimes.c | |||
@@ -0,0 +1,352 @@ | |||
1 | /* File "FastTimes.c" - Original code by Matt Slot <fprefect@ambrosiasw.com> */ | ||
2 | /* Created 4/24/99 - This file is hereby placed in the public domain */ | ||
3 | /* Updated 5/21/99 - Calibrate to VIA, add TBR support, renamed functions */ | ||
4 | /* Updated 10/4/99 - Use AbsoluteToNanoseconds() in case Absolute = double */ | ||
5 | /* Updated 2/15/00 - Check for native Time Manager, no need to calibrate */ | ||
6 | /* Updated 2/19/00 - Fixed default value for gScale under native Time Mgr */ | ||
7 | /* Updated 3/21/00 - Fixed ns conversion, create 2 different scale factors */ | ||
8 | /* Updated 5/03/00 - Added copyright and placed into PD. No code changes */ | ||
9 | /* Updated 8/01/00 - Made "Carbon-compatible" by replacing LMGetTicks() */ | ||
10 | |||
11 | /* This file is Copyright (C) Matt Slot, 1999-2012. It is hereby placed into | ||
12 | the public domain. The author makes no warranty as to fitness or stability */ | ||
13 | |||
14 | #include <Gestalt.h> | ||
15 | #include <LowMem.h> | ||
16 | #include <CodeFragments.h> | ||
17 | #include <DriverServices.h> | ||
18 | #include <Timer.h> | ||
19 | |||
20 | #include "FastTimes.h" | ||
21 | |||
22 | #ifdef TARGET_CPU_PPC | ||
23 | #undef GENERATINGPOWERPC /* stop whining */ | ||
24 | #define GENERATINGPOWERPC TARGET_CPU_PPC | ||
25 | #endif | ||
26 | |||
27 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
28 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
29 | /* | ||
30 | On 680x0 machines, we just use Microseconds(). | ||
31 | |||
32 | On PowerPC machines, we try several methods: | ||
33 | * DriverServicesLib is available on all PCI PowerMacs, and perhaps | ||
34 | some NuBus PowerMacs. If it is, we use UpTime() : Overhead = 2.1 µsec. | ||
35 | * The PowerPC 601 has a built-in "real time clock" RTC, and we fall | ||
36 | back to that, accessing it directly from asm. Overhead = 1.3 µsec. | ||
37 | * Later PowerPCs have an accurate "time base register" TBR, and we | ||
38 | fall back to that, access it from PowerPC asm. Overhead = 1.3 µsec. | ||
39 | * We can also try Microseconds() which is emulated : Overhead = 36 µsec. | ||
40 | |||
41 | On PowerPC machines, we avoid the following: | ||
42 | * OpenTransport is available on all PCI and some NuBus PowerMacs, but it | ||
43 | uses UpTime() if available and falls back to Microseconds() otherwise. | ||
44 | * InputSprocket is available on many PowerMacs, but again it uses | ||
45 | UpTime() if available and falls back to Microseconds() otherwise. | ||
46 | |||
47 | Another PowerPC note: certain configurations, especially 3rd party upgrade | ||
48 | cards, may return inaccurate timings for the CPU or memory bus -- causing | ||
49 | skew in various system routines (up to 20% drift!). The VIA chip is very | ||
50 | accurate, and it's the basis for the Time Manager and Microseconds(). | ||
51 | Unfortunately, it's also very slow because the MacOS has to (a) switch to | ||
52 | 68K and (b) poll for a VIA event. | ||
53 | |||
54 | We compensate for the drift by calibrating a floating point scale factor | ||
55 | between our fast method and the accurate timer at startup, then convert | ||
56 | each sample quickly on the fly. I'd rather not have the initialization | ||
57 | overhead -- but it's simply necessary for accurate timing. You can drop | ||
58 | it down to 30 ticks if you prefer, but that's as low as I'd recommend. | ||
59 | |||
60 | Under MacOS 9, "new world" Macs (iMacs, B+W G3s and G+W G4s) have a native | ||
61 | Time Manager implementation: UpTime(), Microseconds(), and TickCount() are | ||
62 | all based on the same underlying counter. This makes it silly to calibrate | ||
63 | UpTime() against TickCount(). We now check for this feature using Gestalt(), | ||
64 | and skip the whole calibration step if possible. | ||
65 | |||
66 | */ | ||
67 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
68 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
69 | |||
70 | #define RTCToNano(w) ((double) (w).hi * 1000000000.0 + (double) (w).lo) | ||
71 | #define WideTo64bit(w) (*(UInt64 *) &(w)) | ||
72 | |||
73 | /* LMGetTicks() is not in Carbon and TickCount() has a fair bit of overhead, | ||
74 | so for speed we always read lowmem directly. This is a Mac OS X no-no, but | ||
75 | it always work on those systems that don't have a native Time Manager (ie, | ||
76 | anything before MacOS 9) -- regardless whether we are in Carbon or not! */ | ||
77 | #define MyLMGetTicks() (*(volatile UInt32 *) 0x16A) | ||
78 | |||
79 | #if GENERATINGPOWERPC | ||
80 | |||
81 | static asm UnsignedWide PollRTC(void); | ||
82 | static asm UnsignedWide PollTBR(void); | ||
83 | static Ptr FindFunctionInSharedLib(StringPtr libName, StringPtr funcName); | ||
84 | |||
85 | static Boolean gInited = false; | ||
86 | static Boolean gNative = false; | ||
87 | static Boolean gUseRTC = false; | ||
88 | static Boolean gUseTBR = false; | ||
89 | static double gScaleUSec = 1.0 / 1000.0; /* 1 / ( nsec / usec) */ | ||
90 | static double gScaleMSec = 1.0 / 1000000.0; /* 1 / ( nsec / msec) */ | ||
91 | |||
92 | /* Functions loaded from DriverServicesLib */ | ||
93 | typedef AbsoluteTime (*UpTimeProcPtr)(void); | ||
94 | typedef Nanoseconds (*A2NSProcPtr)(AbsoluteTime); | ||
95 | static UpTimeProcPtr gUpTime = NULL; | ||
96 | static A2NSProcPtr gA2NS = NULL; | ||
97 | |||
98 | #endif /* GENERATINGPOWERPC */ | ||
99 | |||
100 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
101 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
102 | |||
103 | void FastInitialize() { | ||
104 | SInt32 result; | ||
105 | |||
106 | if (!gInited) { | ||
107 | |||
108 | #if GENERATINGPOWERPC | ||
109 | |||
110 | /* Initialize the feature flags */ | ||
111 | gNative = gUseRTC = gUseTBR = false; | ||
112 | |||
113 | /* We use CFM to find and load needed symbols from shared libraries, so | ||
114 | the application doesn't have to weak-link them, for convenience. */ | ||
115 | gUpTime = (UpTimeProcPtr) FindFunctionInSharedLib( | ||
116 | "\pDriverServicesLib", "\pUpTime"); | ||
117 | if (gUpTime) gA2NS = (A2NSProcPtr) FindFunctionInSharedLib( | ||
118 | "\pDriverServicesLib", "\pAbsoluteToNanoseconds"); | ||
119 | if (!gA2NS) gUpTime = nil; /* Pedantic but necessary */ | ||
120 | |||
121 | if (gUpTime) { | ||
122 | /* If we loaded UpTime(), then we need to know if the system has | ||
123 | a native implementation of the Time Manager. If so, then it's | ||
124 | pointless to calculate a scale factor against the missing VIA */ | ||
125 | |||
126 | /* gestaltNativeTimeMgr = 4 in some future version of the headers */ | ||
127 | if (!Gestalt(gestaltTimeMgrVersion, &result) && | ||
128 | (result > gestaltExtendedTimeMgr)) | ||
129 | gNative = true; | ||
130 | } | ||
131 | else { | ||
132 | /* If no DriverServicesLib, use Gestalt() to get the processor type. | ||
133 | Only NuBus PowerMacs with old System Software won't have DSL, so | ||
134 | we know it should either be a 601 or 603. */ | ||
135 | |||
136 | /* Use the processor gestalt to determine which register to use */ | ||
137 | if (!Gestalt(gestaltNativeCPUtype, &result)) { | ||
138 | if (result == gestaltCPU601) gUseRTC = true; | ||
139 | else if (result > gestaltCPU601) gUseTBR = true; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | /* Now calculate a scale factor to keep us accurate. */ | ||
144 | if ((gUpTime && !gNative) || gUseRTC || gUseTBR) { | ||
145 | UInt64 tick, usec1, usec2; | ||
146 | UnsignedWide wide; | ||
147 | |||
148 | /* Wait for the beginning of the very next tick */ | ||
149 | for(tick = MyLMGetTicks() + 1; tick > MyLMGetTicks(); ); | ||
150 | |||
151 | /* Poll the selected timer and prepare it (since we have time) */ | ||
152 | wide = (gUpTime) ? (*gA2NS)((*gUpTime)()) : | ||
153 | ((gUseRTC) ? PollRTC() : PollTBR()); | ||
154 | usec1 = (gUseRTC) ? RTCToNano(wide) : WideTo64bit(wide); | ||
155 | |||
156 | /* Wait for the exact 60th tick to roll over */ | ||
157 | while(tick + 60 > MyLMGetTicks()); | ||
158 | |||
159 | /* Poll the selected timer again and prepare it */ | ||
160 | wide = (gUpTime) ? (*gA2NS)((*gUpTime)()) : | ||
161 | ((gUseRTC) ? PollRTC() : PollTBR()); | ||
162 | usec2 = (gUseRTC) ? RTCToNano(wide) : WideTo64bit(wide); | ||
163 | |||
164 | /* Calculate a scale value that will give microseconds per second. | ||
165 | Remember, there are actually 60.15 ticks in a second, not 60. */ | ||
166 | gScaleUSec = (60.0 * 1000000.0) / ((usec2 - usec1) * 60.15); | ||
167 | gScaleMSec = gScaleUSec / 1000.0; | ||
168 | } | ||
169 | |||
170 | #endif /* GENERATINGPOWERPC */ | ||
171 | |||
172 | /* We've initialized our globals */ | ||
173 | gInited = true; | ||
174 | } | ||
175 | } | ||
176 | |||
177 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
178 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
179 | |||
180 | UInt64 FastMicroseconds() { | ||
181 | UnsignedWide wide; | ||
182 | UInt64 usec; | ||
183 | |||
184 | #if GENERATINGPOWERPC | ||
185 | /* Initialize globals the first time we are called */ | ||
186 | if (!gInited) FastInitialize(); | ||
187 | |||
188 | if (gNative) { | ||
189 | /* Use DriverServices if it's available -- it's fast and compatible */ | ||
190 | wide = (*gA2NS)((*gUpTime)()); | ||
191 | usec = (double) WideTo64bit(wide) * gScaleUSec + 0.5; | ||
192 | } | ||
193 | else if (gUpTime) { | ||
194 | /* Use DriverServices if it's available -- it's fast and compatible */ | ||
195 | wide = (*gA2NS)((*gUpTime)()); | ||
196 | usec = (double) WideTo64bit(wide) * gScaleUSec + 0.5; | ||
197 | } | ||
198 | else if (gUseTBR) { | ||
199 | /* On a recent PowerPC, we poll the TBR directly */ | ||
200 | wide = PollTBR(); | ||
201 | usec = (double) WideTo64bit(wide) * gScaleUSec + 0.5; | ||
202 | } | ||
203 | else if (gUseRTC) { | ||
204 | /* On a 601, we can poll the RTC instead */ | ||
205 | wide = PollRTC(); | ||
206 | usec = (double) RTCToNano(wide) * gScaleUSec + 0.5; | ||
207 | } | ||
208 | else | ||
209 | #endif /* GENERATINGPOWERPC */ | ||
210 | { | ||
211 | /* If all else fails, suffer the mixed mode overhead */ | ||
212 | Microseconds(&wide); | ||
213 | usec = WideTo64bit(wide); | ||
214 | } | ||
215 | |||
216 | return(usec); | ||
217 | } | ||
218 | |||
219 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
220 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
221 | |||
222 | UInt64 FastMilliseconds() { | ||
223 | UnsignedWide wide; | ||
224 | UInt64 msec; | ||
225 | |||
226 | #if GENERATINGPOWERPC | ||
227 | /* Initialize globals the first time we are called */ | ||
228 | if (!gInited) FastInitialize(); | ||
229 | |||
230 | if (gNative) { | ||
231 | /* Use DriverServices if it's available -- it's fast and compatible */ | ||
232 | wide = (*gA2NS)((*gUpTime)()); | ||
233 | msec = (double) WideTo64bit(wide) * gScaleMSec + 0.5; | ||
234 | } | ||
235 | else if (gUpTime) { | ||
236 | /* Use DriverServices if it's available -- it's fast and compatible */ | ||
237 | wide = (*gA2NS)((*gUpTime)()); | ||
238 | msec = (double) WideTo64bit(wide) * gScaleMSec + 0.5; | ||
239 | } | ||
240 | else if (gUseTBR) { | ||
241 | /* On a recent PowerPC, we poll the TBR directly */ | ||
242 | wide = PollTBR(); | ||
243 | msec = (double) WideTo64bit(wide) * gScaleMSec + 0.5; | ||
244 | } | ||
245 | else if (gUseRTC) { | ||
246 | /* On a 601, we can poll the RTC instead */ | ||
247 | wide = PollRTC(); | ||
248 | msec = (double) RTCToNano(wide) * gScaleMSec + 0.5; | ||
249 | } | ||
250 | else | ||
251 | #endif /* GENERATINGPOWERPC */ | ||
252 | { | ||
253 | /* If all else fails, suffer the mixed mode overhead */ | ||
254 | Microseconds(&wide); | ||
255 | msec = ((double) WideTo64bit(wide) + 500.0) / 1000.0; | ||
256 | } | ||
257 | |||
258 | return(msec); | ||
259 | } | ||
260 | |||
261 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
262 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
263 | |||
264 | StringPtr FastMethod() { | ||
265 | StringPtr method = "\p<Unknown>"; | ||
266 | |||
267 | #if GENERATINGPOWERPC | ||
268 | /* Initialize globals the first time we are called */ | ||
269 | if (!gInited) FastInitialize(); | ||
270 | |||
271 | if (gNative) { | ||
272 | /* The Time Manager and UpTime() are entirely native on this machine */ | ||
273 | method = "\pNative UpTime()"; | ||
274 | } | ||
275 | else if (gUpTime) { | ||
276 | /* Use DriverServices if it's available -- it's fast and compatible */ | ||
277 | method = "\pUpTime()"; | ||
278 | } | ||
279 | else if (gUseTBR) { | ||
280 | /* On a recent PowerPC, we poll the TBR directly */ | ||
281 | method = "\pPowerPC TBR"; | ||
282 | } | ||
283 | else if (gUseRTC) { | ||
284 | /* On a 601, we can poll the RTC instead */ | ||
285 | method = "\pPowerPC RTC"; | ||
286 | } | ||
287 | else | ||
288 | #endif /* GENERATINGPOWERPC */ | ||
289 | { | ||
290 | /* If all else fails, suffer the mixed mode overhead */ | ||
291 | method = "\pMicroseconds()"; | ||
292 | } | ||
293 | |||
294 | return(method); | ||
295 | } | ||
296 | |||
297 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
298 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
299 | #pragma mark - | ||
300 | |||
301 | #if GENERATINGPOWERPC | ||
302 | asm static UnsignedWide PollRTC_() { | ||
303 | entry PollRTC /* Avoid CodeWarrior glue */ | ||
304 | machine 601 | ||
305 | @AGAIN: | ||
306 | mfrtcu r4 /* RTCU = SPR 4 */ | ||
307 | mfrtcl r5 /* RTCL = SPR 5 */ | ||
308 | mfrtcu r6 | ||
309 | cmpw r4,r6 | ||
310 | bne @AGAIN | ||
311 | stw r4,0(r3) | ||
312 | stw r5,4(r3) | ||
313 | blr | ||
314 | } | ||
315 | |||
316 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
317 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
318 | |||
319 | asm static UnsignedWide PollTBR_() { | ||
320 | entry PollTBR /* Avoid CodeWarrior glue */ | ||
321 | machine 604 | ||
322 | @AGAIN: | ||
323 | mftbu r4 /* TBRU = SPR 268 */ | ||
324 | mftb r5 /* TBRL = SPR 269 */ | ||
325 | mftbu r6 | ||
326 | cmpw r4,r6 | ||
327 | bne @AGAIN | ||
328 | stw r4,0(r3) | ||
329 | stw r5,4(r3) | ||
330 | blr | ||
331 | } | ||
332 | |||
333 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
334 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
335 | |||
336 | static Ptr FindFunctionInSharedLib(StringPtr libName, StringPtr funcName) { | ||
337 | OSErr error = noErr; | ||
338 | Str255 errorStr; | ||
339 | Ptr func = NULL; | ||
340 | Ptr entry = NULL; | ||
341 | CFragSymbolClass symClass; | ||
342 | CFragConnectionID connID; | ||
343 | |||
344 | /* Find CFM containers for the current archecture -- CFM-PPC or CFM-68K */ | ||
345 | if (/* error = */ GetSharedLibrary(libName, kCompiledCFragArch, | ||
346 | kLoadCFrag, &connID, &entry, errorStr)) return(NULL); | ||
347 | if (/* error = */ FindSymbol(connID, funcName, &func, &symClass)) | ||
348 | return(NULL); | ||
349 | |||
350 | return(func); | ||
351 | } | ||
352 | #endif /* GENERATINGPOWERPC */ | ||
diff --git a/apps/plugins/sdl/src/timer/macos/FastTimes.h b/apps/plugins/sdl/src/timer/macos/FastTimes.h new file mode 100644 index 0000000000..d25744c330 --- /dev/null +++ b/apps/plugins/sdl/src/timer/macos/FastTimes.h | |||
@@ -0,0 +1,27 @@ | |||
1 | /* File "FastTimes.h" - Original code by Matt Slot <fprefect@ambrosiasw.com> */ | ||
2 | #include "SDL_config.h" | ||
3 | /* Created 4/24/99 - This file is hereby placed in the public domain */ | ||
4 | /* Updated 5/21/99 - Calibrate to VIA, add TBR support, renamed functions */ | ||
5 | /* Updated 10/4/99 - Use AbsoluteToNanoseconds() in case Absolute = double */ | ||
6 | /* Updated 2/15/00 - Check for native Time Manager, no need to calibrate */ | ||
7 | /* Updated 3/21/00 - Fixed ns conversion, create 2 different scale factors */ | ||
8 | /* Updated 5/03/00 - Added copyright and placed into PD. No code changes */ | ||
9 | |||
10 | /* This file is Copyright (C) Matt Slot, 1999-2012. It is hereby placed into | ||
11 | the public domain. The author makes no warranty as to fitness or stability */ | ||
12 | |||
13 | #ifndef __FAST_TIMES_HEADER__ | ||
14 | #define __FAST_TIMES_HEADER__ | ||
15 | |||
16 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
17 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
18 | |||
19 | extern void FastInitialize(void); | ||
20 | extern UInt64 FastMicroseconds(void); | ||
21 | extern UInt64 FastMilliseconds(void); | ||
22 | extern StringPtr FastMethod(void); | ||
23 | |||
24 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
25 | /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */ | ||
26 | |||
27 | #endif /* __FAST_TIMES_HEADER__ */ | ||
diff --git a/apps/plugins/sdl/src/timer/macos/SDL_MPWtimer.c b/apps/plugins/sdl/src/timer/macos/SDL_MPWtimer.c new file mode 100644 index 0000000000..114b6c7c0a --- /dev/null +++ b/apps/plugins/sdl/src/timer/macos/SDL_MPWtimer.c | |||
@@ -0,0 +1,152 @@ | |||
1 | /* | ||
2 | SDL - Simple DirectMedia Layer | ||
3 | Copyright (C) 1997-2012 Sam Lantinga | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | |||
19 | Sam Lantinga | ||
20 | slouken@libsdl.org | ||
21 | */ | ||
22 | #include "SDL_config.h" | ||
23 | |||
24 | #ifdef SDL_TIMER_MACOS | ||
25 | |||
26 | #include <Types.h> | ||
27 | #include <Timer.h> | ||
28 | #include <OSUtils.h> | ||
29 | #include <Gestalt.h> | ||
30 | #include <Processes.h> | ||
31 | |||
32 | #include <LowMem.h> | ||
33 | |||
34 | #include "SDL_timer.h" | ||
35 | #include "../SDL_timer_c.h" | ||
36 | |||
37 | #define MS_PER_TICK (1000/60) /* MacOS tick = 1/60 second */ | ||
38 | |||
39 | /* Note: This is only a step above the original 1/60s implementation. | ||
40 | * For a good implementation, see FastTimes.[ch], by Matt Slot. | ||
41 | */ | ||
42 | #define USE_MICROSECONDS | ||
43 | #define WideTo64bit(w) (*(UInt64 *) &(w)) | ||
44 | |||
45 | UInt64 start; | ||
46 | |||
47 | void SDL_StartTicks(void) | ||
48 | { | ||
49 | #ifdef USE_MICROSECONDS | ||
50 | UnsignedWide now; | ||
51 | |||
52 | Microseconds(&now); | ||
53 | start = WideTo64bit(now); | ||
54 | #else | ||
55 | /* FIXME: Should we implement a wrapping algorithm, like Win32? */ | ||
56 | #endif | ||
57 | } | ||
58 | |||
59 | Uint32 SDL_GetTicks(void) | ||
60 | { | ||
61 | #ifdef USE_MICROSECONDS | ||
62 | UnsignedWide now; | ||
63 | |||
64 | Microseconds(&now); | ||
65 | return (Uint32)((WideTo64bit(now)-start)/1000); | ||
66 | #else | ||
67 | return(LMGetTicks()*MS_PER_TICK); | ||
68 | #endif | ||
69 | } | ||
70 | |||
71 | void SDL_Delay(Uint32 ms) | ||
72 | { | ||
73 | #ifdef USE_MICROSECONDS | ||
74 | Uint32 end_ms; | ||
75 | |||
76 | end_ms = SDL_GetTicks() + ms; | ||
77 | do { | ||
78 | /* FIXME: Yield CPU? */ ; | ||
79 | } while ( SDL_GetTicks() < end_ms ); | ||
80 | #else | ||
81 | UInt32 unused; /* MJS */ | ||
82 | Delay(ms/MS_PER_TICK, &unused); | ||
83 | #endif | ||
84 | } | ||
85 | |||
86 | |||
87 | /* Data to handle a single periodic alarm */ | ||
88 | typedef struct _ExtendedTimerRec | ||
89 | { | ||
90 | TMTask tmTask; | ||
91 | ProcessSerialNumber taskPSN; | ||
92 | } ExtendedTimerRec, *ExtendedTimerPtr; | ||
93 | |||
94 | static ExtendedTimerRec gExtendedTimerRec; | ||
95 | |||
96 | |||
97 | int SDL_SYS_TimerInit(void) | ||
98 | { | ||
99 | /* We don't need a setup? */ | ||
100 | return(0); | ||
101 | } | ||
102 | |||
103 | void SDL_SYS_TimerQuit(void) | ||
104 | { | ||
105 | /* We don't need a cleanup? */ | ||
106 | return; | ||
107 | } | ||
108 | |||
109 | /* Our Stub routine to set up and then call the real routine. */ | ||
110 | pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr) | ||
111 | { | ||
112 | Uint32 ms; | ||
113 | |||
114 | WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN); | ||
115 | |||
116 | ms = SDL_alarm_callback(SDL_alarm_interval); | ||
117 | if ( ms ) { | ||
118 | SDL_alarm_interval = ROUND_RESOLUTION(ms); | ||
119 | PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, | ||
120 | SDL_alarm_interval); | ||
121 | } else { | ||
122 | SDL_alarm_interval = 0; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | int SDL_SYS_StartTimer(void) | ||
127 | { | ||
128 | /* | ||
129 | * Configure the global structure that stores the timing information. | ||
130 | */ | ||
131 | gExtendedTimerRec.tmTask.qLink = NULL; | ||
132 | gExtendedTimerRec.tmTask.qType = 0; | ||
133 | gExtendedTimerRec.tmTask.tmAddr = NewTimerUPP(TimerCallbackProc); | ||
134 | gExtendedTimerRec.tmTask.tmCount = 0; | ||
135 | gExtendedTimerRec.tmTask.tmWakeUp = 0; | ||
136 | gExtendedTimerRec.tmTask.tmReserved = 0; | ||
137 | GetCurrentProcess(&gExtendedTimerRec.taskPSN); | ||
138 | |||
139 | /* Install the task record */ | ||
140 | InsXTime((QElemPtr)&gExtendedTimerRec.tmTask); | ||
141 | |||
142 | /* Go! */ | ||
143 | PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval); | ||
144 | return(0); | ||
145 | } | ||
146 | |||
147 | void SDL_SYS_StopTimer(void) | ||
148 | { | ||
149 | RmvTime((QElemPtr)&gExtendedTimerRec.tmTask); | ||
150 | } | ||
151 | |||
152 | #endif /* SDL_TIMER_MACOS */ | ||
diff --git a/apps/plugins/sdl/src/timer/macos/SDL_systimer.c b/apps/plugins/sdl/src/timer/macos/SDL_systimer.c new file mode 100644 index 0000000000..7a8063e57d --- /dev/null +++ b/apps/plugins/sdl/src/timer/macos/SDL_systimer.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | SDL - Simple DirectMedia Layer | ||
3 | Copyright (C) 1997-2012 Sam Lantinga | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | |||
19 | Sam Lantinga | ||
20 | slouken@libsdl.org | ||
21 | */ | ||
22 | #include "SDL_config.h" | ||
23 | |||
24 | #ifdef SDL_TIMER_MACOS | ||
25 | |||
26 | #include <Types.h> | ||
27 | #include <Timer.h> | ||
28 | #include <OSUtils.h> | ||
29 | #include <Gestalt.h> | ||
30 | #include <Processes.h> | ||
31 | |||
32 | #include <LowMem.h> | ||
33 | |||
34 | #include "SDL_timer.h" | ||
35 | #include "../SDL_timer_c.h" | ||
36 | |||
37 | #include "FastTimes.h" | ||
38 | |||
39 | #if TARGET_API_MAC_CARBON | ||
40 | #define NewTimerProc NewTimerUPP | ||
41 | #endif | ||
42 | |||
43 | #define MS_PER_TICK (1000.0/60.0) /* MacOS tick = 1/60 second */ | ||
44 | |||
45 | |||
46 | #define kTwoPower32 (4294967296.0) /* 2^32 */ | ||
47 | |||
48 | static double start_tick; | ||
49 | static int is_fast_inited = 0; | ||
50 | |||
51 | void SDL_StartTicks(void) | ||
52 | { | ||
53 | if ( ! is_fast_inited ) // important to check or FastTime may hang machine! | ||
54 | SDL_SYS_TimerInit(); | ||
55 | |||
56 | start_tick = FastMicroseconds(); | ||
57 | } | ||
58 | |||
59 | Uint32 SDL_GetTicks(void) | ||
60 | { | ||
61 | |||
62 | if ( ! is_fast_inited ) | ||
63 | SDL_SYS_TimerInit(); | ||
64 | |||
65 | return FastMilliseconds(); | ||
66 | } | ||
67 | |||
68 | void SDL_Delay(Uint32 ms) | ||
69 | { | ||
70 | Uint32 stop, now; | ||
71 | |||
72 | stop = SDL_GetTicks() + ms; | ||
73 | do { | ||
74 | #if TARGET_API_MAC_CARBON | ||
75 | MPYield(); | ||
76 | #else | ||
77 | SystemTask(); | ||
78 | #endif | ||
79 | |||
80 | now = SDL_GetTicks(); | ||
81 | |||
82 | } while ( stop > now ); | ||
83 | } | ||
84 | |||
85 | /* | ||
86 | void SDL_StartTicks(void) | ||
87 | { | ||
88 | // FIXME: Should we implement a wrapping algorithm, like Win32? | ||
89 | } | ||
90 | |||
91 | Uint32 SDL_GetTicks(void) | ||
92 | { | ||
93 | UnsignedWide ms; | ||
94 | |||
95 | Microseconds (&ms); | ||
96 | |||
97 | return ( ms.lo / 1000 ); | ||
98 | } | ||
99 | |||
100 | void SDL_Delay(Uint32 ms) | ||
101 | { | ||
102 | |||
103 | UnsignedWide microsecs; | ||
104 | UInt32 stop; | ||
105 | |||
106 | Microseconds (µsecs); | ||
107 | |||
108 | stop = microsecs.lo + (ms * 1000); | ||
109 | |||
110 | while ( stop > microsecs.lo ) { | ||
111 | |||
112 | SystemTask (); | ||
113 | |||
114 | Microseconds (µsecs); | ||
115 | } | ||
116 | |||
117 | }*/ | ||
118 | |||
119 | /* Data to handle a single periodic alarm */ | ||
120 | typedef struct _ExtendedTimerRec | ||
121 | { | ||
122 | TMTask tmTask; | ||
123 | ProcessSerialNumber taskPSN; | ||
124 | } ExtendedTimerRec, *ExtendedTimerPtr; | ||
125 | |||
126 | static ExtendedTimerRec gExtendedTimerRec; | ||
127 | |||
128 | |||
129 | int SDL_SYS_TimerInit(void) | ||
130 | { | ||
131 | FastInitialize (); | ||
132 | is_fast_inited = 1; | ||
133 | |||
134 | return(0); | ||
135 | } | ||
136 | |||
137 | void SDL_SYS_TimerQuit(void) | ||
138 | { | ||
139 | /* We don't need a cleanup? */ | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | /* Our Stub routine to set up and then call the real routine. */ | ||
144 | pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr) | ||
145 | { | ||
146 | Uint32 ms; | ||
147 | |||
148 | WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN); | ||
149 | |||
150 | ms = SDL_alarm_callback(SDL_alarm_interval); | ||
151 | if ( ms ) { | ||
152 | SDL_alarm_interval = ROUND_RESOLUTION(ms); | ||
153 | PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, | ||
154 | SDL_alarm_interval); | ||
155 | } else { | ||
156 | SDL_alarm_interval = 0; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | int SDL_SYS_StartTimer(void) | ||
161 | { | ||
162 | /* | ||
163 | * Configure the global structure that stores the timing information. | ||
164 | */ | ||
165 | gExtendedTimerRec.tmTask.qLink = NULL; | ||
166 | gExtendedTimerRec.tmTask.qType = 0; | ||
167 | gExtendedTimerRec.tmTask.tmAddr = NewTimerProc(TimerCallbackProc); | ||
168 | gExtendedTimerRec.tmTask.tmCount = 0; | ||
169 | gExtendedTimerRec.tmTask.tmWakeUp = 0; | ||
170 | gExtendedTimerRec.tmTask.tmReserved = 0; | ||
171 | GetCurrentProcess(&gExtendedTimerRec.taskPSN); | ||
172 | |||
173 | /* Install the task record */ | ||
174 | InsXTime((QElemPtr)&gExtendedTimerRec.tmTask); | ||
175 | |||
176 | /* Go! */ | ||
177 | PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval); | ||
178 | return(0); | ||
179 | } | ||
180 | |||
181 | void SDL_SYS_StopTimer(void) | ||
182 | { | ||
183 | RmvTime((QElemPtr)&gExtendedTimerRec.tmTask); | ||
184 | } | ||
185 | |||
186 | #endif /* SDL_TIMER_MACOS */ | ||