Initial revision

This commit is contained in:
Sam Lantinga
2001-04-26 16:45:43 +00:00
commit 5d2015c741
892 changed files with 170313 additions and 0 deletions

7
src/timer/.cvsignore Normal file
View File

@@ -0,0 +1,7 @@
Makefile.in
SDL_timer.c
Makefile
.libs
*.o
*.lo
*.la

27
src/timer/Makefile.am Normal file
View File

@@ -0,0 +1,27 @@
## Makefile.am for the SDL timer library
noinst_LTLIBRARIES = libtimer.la
ARCH_SUBDIRS = amigaos beos linux macos win32
# Include the architecture-independent sources
COMMON_SRCS = SDL_timer.c SDL_timer_c.h SDL_systimer.h
# Include the architecture-specific sources
ARCH_SRCS = SDL_systimer.c
libtimer_la_SOURCES = $(COMMON_SRCS) $(ARCH_SRCS)
## Let automake know that it shouldn't distribute linked sources
BUILT_SOURCES = $(ARCH_SRCS)
## Let automake know that it should remove these for distribution
DISTCLEANFILES = $(ARCH_SRCS)
# The architecture specific directories need to be copied into place
# when building a distribution.
dist-hook:
(cd $(distdir) && rm -f $(BUILT_SOURCES))
cp -rp $(ARCH_SUBDIRS) $(distdir)
(cd $(distdir) && rm -rf `find . -name CVS`)

46
src/timer/SDL_systimer.h Normal file
View File

@@ -0,0 +1,46 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
5635-34 Springhouse Dr.
Pleasanton, CA 94588 (USA)
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
/* The system dependent timer handling functions */
#include "SDL_timer.h"
#include "SDL_timer_c.h"
/* Initialize the system dependent timer subsystem */
extern int SDL_SYS_TimerInit(void);
/* Quit the system dependent timer subsystem */
extern void SDL_SYS_TimerQuit(void);
/* Start a timer set up by SDL_SetTimer() */
extern int SDL_SYS_StartTimer(void);
/* Stop a previously started timer */
extern void SDL_SYS_StopTimer(void);

52
src/timer/SDL_timer_c.h Normal file
View File

@@ -0,0 +1,52 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
5635-34 Springhouse Dr.
Pleasanton, CA 94588 (USA)
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
/* Useful functions and variables from SDL_timer.c */
#include "SDL_timer.h"
#define ROUND_RESOLUTION(X) \
(((X+TIMER_RESOLUTION-1)/TIMER_RESOLUTION)*TIMER_RESOLUTION)
extern int SDL_timer_started;
extern int SDL_timer_running;
/* Data to handle a single periodic alarm */
extern Uint32 SDL_alarm_interval;
extern SDL_TimerCallback SDL_alarm_callback;
/* Set whether or not the timer should use a thread.
This should be called while the timer subsystem is running.
*/
extern int SDL_SetTimerThreaded(int value);
extern int SDL_TimerInit(void);
extern void SDL_TimerQuit(void);
/* This function is called from the SDL event thread if it is available */
extern void SDL_ThreadedTimerCheck(void);

View File

@@ -0,0 +1,145 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "SDL_error.h"
#include "SDL_timer.h"
#include "SDL_timer_c.h"
/* The first ticks value of the application */
static struct timeval start;
void SDL_StartTicks(void)
{
/* Set first ticks value */
gettimeofday(&start, NULL);
}
Uint32 SDL_GetTicks (void)
{
struct timeval now;
Uint32 ticks;
gettimeofday(&now, NULL);
ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
return(ticks);
}
void SDL_Delay (Uint32 ms)
{
int was_error;
#ifndef linux /* Non-Linux implementations need to calculate time left */
Uint32 then, now, elapsed;
#endif
struct timeval tv;
/* Set the timeout interval - Linux only needs to do this once */
#ifdef linux
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
#else
then = SDL_GetTicks();
#endif
do {
errno = 0;
#ifndef linux
/* Calculate the time interval left (in case of interrupt) */
now = SDL_GetTicks();
elapsed = (now-then);
then = now;
if ( elapsed >= ms ) {
break;
}
ms -= elapsed;
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
#endif
was_error = select(0, NULL, NULL, NULL, &tv);
} while ( was_error && (errno == EINTR) );
}
#include "SDL_thread.h"
/* Data to handle a single periodic alarm */
static int timer_alive = 0;
static SDL_Thread *timer = NULL;
static int RunTimer(void *unused)
{
while ( timer_alive ) {
if ( SDL_timer_running ) {
SDL_ThreadedTimerCheck();
}
SDL_Delay(1);
}
return(0);
}
/* This is only called if the event thread is not running */
int SDL_SYS_TimerInit(void)
{
#ifdef NO_AMIGADEBUG
fprintf(stderr,"Creo il thread per il timer (NOITMER)...\n");
#endif
timer_alive = 1;
timer = SDL_CreateThread(RunTimer, NULL);
if ( timer == NULL )
{
#ifdef NO_AMIGADEBUG
fprintf(stderr,"Creazione del thread fallita...\n");
#endif
return(-1);
}
return(SDL_SetTimerThreaded(1));
}
void SDL_SYS_TimerQuit(void)
{
timer_alive = 0;
if ( timer ) {
SDL_WaitThread(timer, NULL);
timer = NULL;
}
}
int SDL_SYS_StartTimer(void)
{
SDL_SetError("Internal logic error: Linux uses threaded timer");
return(-1);
}
void SDL_SYS_StopTimer(void)
{
return;
}

View File

@@ -0,0 +1,96 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <be/kernel/OS.h>
#include "SDL_thread.h"
#include "SDL_timer.h"
#include "SDL_error.h"
#include "SDL_timer_c.h"
static bigtime_t start;
void SDL_StartTicks(void)
{
/* Set first ticks value */
start = system_time();
}
Uint32 SDL_GetTicks(void)
{
return((system_time()-start)/1000);
}
void SDL_Delay(Uint32 ms)
{
snooze(ms*1000);
}
/* Data to handle a single periodic alarm */
static int timer_alive = 0;
static SDL_Thread *timer = NULL;
static int RunTimer(void *unused)
{
while ( timer_alive ) {
if ( SDL_timer_running ) {
SDL_ThreadedTimerCheck();
}
SDL_Delay(10);
}
return(0);
}
/* This is only called if the event thread is not running */
int SDL_SYS_TimerInit(void)
{
timer_alive = 1;
timer = SDL_CreateThread(RunTimer, NULL);
if ( timer == NULL )
return(-1);
return(SDL_SetTimerThreaded(1));
}
void SDL_SYS_TimerQuit(void)
{
timer_alive = 0;
if ( timer ) {
SDL_WaitThread(timer, NULL);
timer = NULL;
}
}
int SDL_SYS_StartTimer(void)
{
SDL_SetError("Internal logic error: BeOS uses threaded timer");
return(-1);
}
void SDL_SYS_StopTimer(void)
{
return;
}

View File

@@ -0,0 +1,222 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "SDL_error.h"
#include "SDL_timer.h"
#include "SDL_timer_c.h"
#if defined(DISABLE_THREADS) || defined(FORK_HACK)
#define USE_ITIMER
#endif
/* The following defines should really be determined at configure time */
#ifdef linux
/* Linux select() changes its timeout parameter upon return to contain
the remaining time. Most other unixen leave it unchanged or undefined. */
#define SELECT_SETS_REMAINING
#elif defined(__bsdi__) || defined(__FreeBSD__) || defined(__sun__)
#define USE_NANOSLEEP
#endif
/* The first ticks value of the application */
static struct timeval start;
void SDL_StartTicks(void)
{
/* Set first ticks value */
gettimeofday(&start, NULL);
}
Uint32 SDL_GetTicks (void)
{
struct timeval now;
Uint32 ticks;
gettimeofday(&now, NULL);
ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
return(ticks);
}
void SDL_Delay (Uint32 ms)
{
int was_error;
#ifdef USE_NANOSLEEP
struct timespec elapsed, tv;
#else
struct timeval tv;
#ifndef SELECT_SETS_REMAINING
Uint32 then, now, elapsed;
#endif
#endif
/* Set the timeout interval - Linux only needs to do this once */
#ifdef SELECT_SETS_REMAINING
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
#elif defined(USE_NANOSLEEP)
elapsed.tv_sec = ms/1000;
elapsed.tv_nsec = (ms%1000)*1000000;
#else
then = SDL_GetTicks();
#endif
do {
errno = 0;
#ifdef USE_NANOSLEEP
tv.tv_sec = elapsed.tv_sec;
tv.tv_nsec = elapsed.tv_nsec;
was_error = nanosleep(&tv, &elapsed);
#else
#ifndef SELECT_SETS_REMAINING
/* Calculate the time interval left (in case of interrupt) */
now = SDL_GetTicks();
elapsed = (now-then);
then = now;
if ( elapsed >= ms ) {
break;
}
ms -= elapsed;
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
#endif
was_error = select(0, NULL, NULL, NULL, &tv);
#endif /* USE_NANOSLEEP */
} while ( was_error && (errno == EINTR) );
}
#ifdef USE_ITIMER
static void HandleAlarm(int sig)
{
Uint32 ms;
if ( SDL_alarm_callback ) {
ms = (*SDL_alarm_callback)(SDL_alarm_interval);
if ( ms != SDL_alarm_interval ) {
SDL_SetTimer(ms, SDL_alarm_callback);
}
}
}
int SDL_SYS_TimerInit(void)
{
struct sigaction action;
/* Set the alarm handler (Linux specific) */
memset(&action, 0, sizeof(action));
action.sa_handler = HandleAlarm;
action.sa_flags = SA_RESTART;
sigemptyset(&action.sa_mask);
sigaction(SIGALRM, &action, NULL);
return(0);
}
void SDL_SYS_TimerQuit(void)
{
SDL_SetTimer(0, NULL);
}
int SDL_SYS_StartTimer(void)
{
struct itimerval timer;
timer.it_value.tv_sec = (SDL_alarm_interval/1000);
timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000;
timer.it_interval.tv_sec = (SDL_alarm_interval/1000);
timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000;
setitimer(ITIMER_REAL, &timer, NULL);
return(0);
}
void SDL_SYS_StopTimer(void)
{
struct itimerval timer;
memset(&timer, 0, (sizeof timer));
setitimer(ITIMER_REAL, &timer, NULL);
}
#else /* USE_ITIMER */
#include "SDL_thread.h"
/* Data to handle a single periodic alarm */
static int timer_alive = 0;
static SDL_Thread *timer = NULL;
static int RunTimer(void *unused)
{
while ( timer_alive ) {
if ( SDL_timer_running ) {
SDL_ThreadedTimerCheck();
}
SDL_Delay(1);
}
return(0);
}
/* This is only called if the event thread is not running */
int SDL_SYS_TimerInit(void)
{
timer_alive = 1;
timer = SDL_CreateThread(RunTimer, NULL);
if ( timer == NULL )
return(-1);
return(SDL_SetTimerThreaded(1));
}
void SDL_SYS_TimerQuit(void)
{
timer_alive = 0;
if ( timer ) {
SDL_WaitThread(timer, NULL);
timer = NULL;
}
}
int SDL_SYS_StartTimer(void)
{
SDL_SetError("Internal logic error: Linux uses threaded timer");
return(-1);
}
void SDL_SYS_StopTimer(void)
{
return;
}
#endif /* USE_ITIMER */

347
src/timer/macos/FastTimes.c Normal file
View File

@@ -0,0 +1,347 @@
/* File "FastTimes.c" - Original code by Matt Slot <fprefect@ambrosiasw.com> */
/* Created 4/24/99 - This file is hereby placed in the public domain */
/* Updated 5/21/99 - Calibrate to VIA, add TBR support, renamed functions */
/* Updated 10/4/99 - Use AbsoluteToNanoseconds() in case Absolute = double */
/* Updated 2/15/00 - Check for native Time Manager, no need to calibrate */
/* Updated 2/19/00 - Fixed default value for gScale under native Time Mgr */
/* Updated 3/21/00 - Fixed ns conversion, create 2 different scale factors */
/* Updated 5/03/00 - Added copyright and placed into PD. No code changes */
/* Updated 8/01/00 - Made "Carbon-compatible" by replacing LMGetTicks() */
/* This file is Copyright (C) Matt Slot, 1999-2000. It is hereby placed into
the public domain. The author makes no warranty as to fitness or stability */
#include <Gestalt.h>
#include <LowMem.h>
#include <CodeFragments.h>
#include <DriverServices.h>
#include <Timer.h>
#include "FastTimes.h"
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/*
On 680x0 machines, we just use Microseconds().
On PowerPC machines, we try several methods:
* DriverServicesLib is available on all PCI PowerMacs, and perhaps
some NuBus PowerMacs. If it is, we use UpTime() : Overhead = 2.1 <20>sec.
* The PowerPC 601 has a built-in "real time clock" RTC, and we fall
back to that, accessing it directly from asm. Overhead = 1.3 <20>sec.
* Later PowerPCs have an accurate "time base register" TBR, and we
fall back to that, access it from PowerPC asm. Overhead = 1.3 <20>sec.
* We can also try Microseconds() which is emulated : Overhead = 36 <20>sec.
On PowerPC machines, we avoid the following:
* OpenTransport is available on all PCI and some NuBus PowerMacs, but it
uses UpTime() if available and falls back to Microseconds() otherwise.
* InputSprocket is available on many PowerMacs, but again it uses
UpTime() if available and falls back to Microseconds() otherwise.
Another PowerPC note: certain configurations, especially 3rd party upgrade
cards, may return inaccurate timings for the CPU or memory bus -- causing
skew in various system routines (up to 20% drift!). The VIA chip is very
accurate, and it's the basis for the Time Manager and Microseconds().
Unfortunately, it's also very slow because the MacOS has to (a) switch to
68K and (b) poll for a VIA event.
We compensate for the drift by calibrating a floating point scale factor
between our fast method and the accurate timer at startup, then convert
each sample quickly on the fly. I'd rather not have the initialization
overhead -- but it's simply necessary for accurate timing. You can drop
it down to 30 ticks if you prefer, but that's as low as I'd recommend.
Under MacOS 9, "new world" Macs (iMacs, B+W G3s and G+W G4s) have a native
Time Manager implementation: UpTime(), Microseconds(), and TickCount() are
all based on the same underlying counter. This makes it silly to calibrate
UpTime() against TickCount(). We now check for this feature using Gestalt(),
and skip the whole calibration step if possible.
*/
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
#define RTCToNano(w) ((double) (w).hi * 1000000000.0 + (double) (w).lo)
#define WideTo64bit(w) (*(UInt64 *) &(w))
/* LMGetTicks() is not in Carbon and TickCount() has a fair bit of overhead,
so for speed we always read lowmem directly. This is a MacOS X no-no, but
it always work on those systems that don't have a native Time Manager (ie,
anything before MacOS 9) -- regardless whether we are in Carbon or not! */
#define MyLMGetTicks() (*(volatile UInt32 *) 0x16A)
#if GENERATINGPOWERPC
static asm UnsignedWide PollRTC(void);
static asm UnsignedWide PollTBR(void);
static Ptr FindFunctionInSharedLib(StringPtr libName, StringPtr funcName);
static Boolean gInited = false;
static Boolean gNative = false;
static Boolean gUseRTC = false;
static Boolean gUseTBR = false;
static double gScaleUSec = 1.0 / 1000.0; /* 1 / ( nsec / usec) */
static double gScaleMSec = 1.0 / 1000000.0; /* 1 / ( nsec / msec) */
/* Functions loaded from DriverServicesLib */
typedef AbsoluteTime (*UpTimeProcPtr)(void);
typedef Nanoseconds (*A2NSProcPtr)(AbsoluteTime);
static UpTimeProcPtr gUpTime = NULL;
static A2NSProcPtr gA2NS = NULL;
#endif /* GENERATINGPOWERPC */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
void FastInitialize() {
SInt32 result;
if (!gInited) {
#if GENERATINGPOWERPC
/* Initialize the feature flags */
gNative = gUseRTC = gUseTBR = false;
/* We use CFM to find and load needed symbols from shared libraries, so
the application doesn't have to weak-link them, for convenience. */
gUpTime = (UpTimeProcPtr) FindFunctionInSharedLib(
"\pDriverServicesLib", "\pUpTime");
if (gUpTime) gA2NS = (A2NSProcPtr) FindFunctionInSharedLib(
"\pDriverServicesLib", "\pAbsoluteToNanoseconds");
if (!gA2NS) gUpTime = nil; /* Pedantic but necessary */
if (gUpTime) {
/* If we loaded UpTime(), then we need to know if the system has
a native implementation of the Time Manager. If so, then it's
pointless to calculate a scale factor against the missing VIA */
/* gestaltNativeTimeMgr = 4 in some future version of the headers */
if (!Gestalt(gestaltTimeMgrVersion, &result) &&
(result > gestaltExtendedTimeMgr))
gNative = true;
}
else {
/* If no DriverServicesLib, use Gestalt() to get the processor type.
Only NuBus PowerMacs with old System Software won't have DSL, so
we know it should either be a 601 or 603. */
/* Use the processor gestalt to determine which register to use */
if (!Gestalt(gestaltNativeCPUtype, &result)) {
if (result == gestaltCPU601) gUseRTC = true;
else if (result > gestaltCPU601) gUseTBR = true;
}
}
/* Now calculate a scale factor to keep us accurate. */
if ((gUpTime && !gNative) || gUseRTC || gUseTBR) {
UInt64 tick, usec1, usec2;
UnsignedWide wide;
/* Wait for the beginning of the very next tick */
for(tick = MyLMGetTicks() + 1; tick > MyLMGetTicks(); );
/* Poll the selected timer and prepare it (since we have time) */
wide = (gUpTime) ? (*gA2NS)((*gUpTime)()) :
((gUseRTC) ? PollRTC() : PollTBR());
usec1 = (gUseRTC) ? RTCToNano(wide) : WideTo64bit(wide);
/* Wait for the exact 60th tick to roll over */
while(tick + 60 > MyLMGetTicks());
/* Poll the selected timer again and prepare it */
wide = (gUpTime) ? (*gA2NS)((*gUpTime)()) :
((gUseRTC) ? PollRTC() : PollTBR());
usec2 = (gUseRTC) ? RTCToNano(wide) : WideTo64bit(wide);
/* Calculate a scale value that will give microseconds per second.
Remember, there are actually 60.15 ticks in a second, not 60. */
gScaleUSec = (60.0 * 1000000.0) / ((usec2 - usec1) * 60.15);
gScaleMSec = gScaleUSec / 1000.0;
}
#endif /* GENERATINGPOWERPC */
/* We've initialized our globals */
gInited = true;
}
}
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
UInt64 FastMicroseconds() {
UnsignedWide wide;
UInt64 usec;
#if GENERATINGPOWERPC
/* Initialize globals the first time we are called */
if (!gInited) FastInitialize();
if (gNative) {
/* Use DriverServices if it's available -- it's fast and compatible */
wide = (*gA2NS)((*gUpTime)());
usec = (double) WideTo64bit(wide) * gScaleUSec + 0.5;
}
else if (gUpTime) {
/* Use DriverServices if it's available -- it's fast and compatible */
wide = (*gA2NS)((*gUpTime)());
usec = (double) WideTo64bit(wide) * gScaleUSec + 0.5;
}
else if (gUseTBR) {
/* On a recent PowerPC, we poll the TBR directly */
wide = PollTBR();
usec = (double) WideTo64bit(wide) * gScaleUSec + 0.5;
}
else if (gUseRTC) {
/* On a 601, we can poll the RTC instead */
wide = PollRTC();
usec = (double) RTCToNano(wide) * gScaleUSec + 0.5;
}
else
#endif /* GENERATINGPOWERPC */
{
/* If all else fails, suffer the mixed mode overhead */
Microseconds(&wide);
usec = WideTo64bit(wide);
}
return(usec);
}
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
UInt64 FastMilliseconds() {
UnsignedWide wide;
UInt64 msec;
#if GENERATINGPOWERPC
/* Initialize globals the first time we are called */
if (!gInited) FastInitialize();
if (gNative) {
/* Use DriverServices if it's available -- it's fast and compatible */
wide = (*gA2NS)((*gUpTime)());
msec = (double) WideTo64bit(wide) * gScaleMSec + 0.5;
}
else if (gUpTime) {
/* Use DriverServices if it's available -- it's fast and compatible */
wide = (*gA2NS)((*gUpTime)());
msec = (double) WideTo64bit(wide) * gScaleMSec + 0.5;
}
else if (gUseTBR) {
/* On a recent PowerPC, we poll the TBR directly */
wide = PollTBR();
msec = (double) WideTo64bit(wide) * gScaleMSec + 0.5;
}
else if (gUseRTC) {
/* On a 601, we can poll the RTC instead */
wide = PollRTC();
msec = (double) RTCToNano(wide) * gScaleMSec + 0.5;
}
else
#endif /* GENERATINGPOWERPC */
{
/* If all else fails, suffer the mixed mode overhead */
Microseconds(&wide);
msec = ((double) WideTo64bit(wide) + 500.0) / 1000.0;
}
return(msec);
}
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
StringPtr FastMethod() {
StringPtr method = "\p<Unknown>";
#if GENERATINGPOWERPC
/* Initialize globals the first time we are called */
if (!gInited) FastInitialize();
if (gNative) {
/* The Time Manager and UpTime() are entirely native on this machine */
method = "\pNative UpTime()";
}
else if (gUpTime) {
/* Use DriverServices if it's available -- it's fast and compatible */
method = "\pUpTime()";
}
else if (gUseTBR) {
/* On a recent PowerPC, we poll the TBR directly */
method = "\pPowerPC TBR";
}
else if (gUseRTC) {
/* On a 601, we can poll the RTC instead */
method = "\pPowerPC RTC";
}
else
#endif /* GENERATINGPOWERPC */
{
/* If all else fails, suffer the mixed mode overhead */
method = "\pMicroseconds()";
}
return(method);
}
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
#pragma mark -
#if GENERATINGPOWERPC
asm static UnsignedWide PollRTC_() {
entry PollRTC /* Avoid CodeWarrior glue */
machine 601
@AGAIN:
mfrtcu r4 /* RTCU = SPR 4 */
mfrtcl r5 /* RTCL = SPR 5 */
mfrtcu r6
cmpw r4,r6
bne @AGAIN
stw r4,0(r3)
stw r5,4(r3)
blr
}
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
asm static UnsignedWide PollTBR_() {
entry PollTBR /* Avoid CodeWarrior glue */
machine 604
@AGAIN:
mftbu r4 /* TBRU = SPR 268 */
mftb r5 /* TBRL = SPR 269 */
mftbu r6
cmpw r4,r6
bne @AGAIN
stw r4,0(r3)
stw r5,4(r3)
blr
}
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
static Ptr FindFunctionInSharedLib(StringPtr libName, StringPtr funcName) {
OSErr error = noErr;
Str255 errorStr;
Ptr func = NULL;
Ptr entry = NULL;
CFragSymbolClass symClass;
CFragConnectionID connID;
/* Find CFM containers for the current archecture -- CFM-PPC or CFM-68K */
if (/* error = */ GetSharedLibrary(libName, kCompiledCFragArch,
kLoadCFrag, &connID, &entry, errorStr)) return(NULL);
if (/* error = */ FindSymbol(connID, funcName, &func, &symClass))
return(NULL);
return(func);
}
#endif /* GENERATINGPOWERPC */

View File

@@ -0,0 +1,26 @@
/* File "FastTimes.h" - Original code by Matt Slot <fprefect@ambrosiasw.com> */
/* Created 4/24/99 - This file is hereby placed in the public domain */
/* Updated 5/21/99 - Calibrate to VIA, add TBR support, renamed functions */
/* Updated 10/4/99 - Use AbsoluteToNanoseconds() in case Absolute = double */
/* Updated 2/15/00 - Check for native Time Manager, no need to calibrate */
/* Updated 3/21/00 - Fixed ns conversion, create 2 different scale factors */
/* Updated 5/03/00 - Added copyright and placed into PD. No code changes */
/* This file is Copyright (C) Matt Slot, 1999-2000. It is hereby placed into
the public domain. The author makes no warranty as to fitness or stability */
#ifndef __FAST_TIMES_HEADER__
#define __FAST_TIMES_HEADER__
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
extern void FastInitialize(void);
extern UInt64 FastMicroseconds(void);
extern UInt64 FastMilliseconds(void);
extern StringPtr FastMethod(void);
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
/* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
#endif /* __FAST_TIMES_HEADER__ */

View File

@@ -0,0 +1,152 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <Types.h>
#include <Timer.h>
#include <OSUtils.h>
#include <Gestalt.h>
#include <Processes.h>
#include <LowMem.h>
#include "SDL_timer.h"
#include "SDL_timer_c.h"
#define MS_PER_TICK (1000/60) /* MacOS tick = 1/60 second */
/* Note: This is only a step above the original 1/60s implementation.
* For a good implementation, see FastTimes.[ch], by Matt Slot.
*/
#define USE_MICROSECONDS
#define WideTo64bit(w) (*(UInt64 *) &(w))
UInt64 start;
void SDL_StartTicks(void)
{
#ifdef USE_MICROSECONDS
UnsignedWide now;
Microseconds(&now);
start = WideTo64bit(now);
#else
/* FIXME: Should we implement a wrapping algorithm, like Win32? */
#endif
}
Uint32 SDL_GetTicks(void)
{
#ifdef USE_MICROSECONDS
UnsignedWide now;
Microseconds(&now);
return (Uint32)((WideTo64bit(now)-start)/1000);
#else
return(LMGetTicks()*MS_PER_TICK);
#endif
}
void SDL_Delay(Uint32 ms)
{
#ifdef USE_MICROSECONDS
Uint32 end_ms;
end_ms = SDL_GetTicks() + ms;
do {
/* FIXME: Yield CPU? */ ;
} while ( SDL_GetTicks() < end_ms );
#else
UInt32 unused; /* MJS */
Delay(ms/MS_PER_TICK, &unused);
#endif
}
/* Data to handle a single periodic alarm */
typedef struct _ExtendedTimerRec
{
TMTask tmTask;
ProcessSerialNumber taskPSN;
} ExtendedTimerRec, *ExtendedTimerPtr;
static ExtendedTimerRec gExtendedTimerRec;
int SDL_SYS_TimerInit(void)
{
/* We don't need a setup? */
return(0);
}
void SDL_SYS_TimerQuit(void)
{
/* We don't need a cleanup? */
return;
}
/* Our Stub routine to set up and then call the real routine. */
pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr)
{
Uint32 ms;
WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN);
ms = SDL_alarm_callback(SDL_alarm_interval);
if ( ms ) {
SDL_alarm_interval = ROUND_RESOLUTION(ms);
PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask,
SDL_alarm_interval);
} else {
SDL_alarm_interval = 0;
}
}
int SDL_SYS_StartTimer(void)
{
/*
* Configure the global structure that stores the timing information.
*/
gExtendedTimerRec.tmTask.qLink = NULL;
gExtendedTimerRec.tmTask.qType = 0;
gExtendedTimerRec.tmTask.tmAddr = NewTimerUPP(TimerCallbackProc);
gExtendedTimerRec.tmTask.tmCount = 0;
gExtendedTimerRec.tmTask.tmWakeUp = 0;
gExtendedTimerRec.tmTask.tmReserved = 0;
GetCurrentProcess(&gExtendedTimerRec.taskPSN);
/* Install the task record */
InsXTime((QElemPtr)&gExtendedTimerRec.tmTask);
/* Go! */
PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval);
return(0);
}
void SDL_SYS_StopTimer(void)
{
RmvTime((QElemPtr)&gExtendedTimerRec.tmTask);
}

View File

@@ -0,0 +1,178 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <Types.h>
#include <Timer.h>
#include <OSUtils.h>
#include <Gestalt.h>
#include <Processes.h>
#include <LowMem.h>
#include "SDL_timer.h"
#include "SDL_timer_c.h"
#include "FastTimes.h"
#define MS_PER_TICK (1000.0/60.0) /* MacOS tick = 1/60 second */
#define kTwoPower32 (4294967296.0) /* 2^32 */
static double start_tick;
static int is_fast_inited = 0;
void SDL_StartTicks(void)
{
if ( ! is_fast_inited ) // important to check or FastTime may hang machine!
SDL_SYS_TimerInit();
start_tick = FastMicroseconds();
}
Uint32 SDL_GetTicks(void)
{
if ( ! is_fast_inited )
SDL_SYS_TimerInit();
return FastMilliseconds();
}
void SDL_Delay(Uint32 ms)
{
Uint32 stop, now;
stop = SDL_GetTicks() + ms;
do {
SystemTask();
now = SDL_GetTicks();
} while ( stop > now );
}
/*
void SDL_StartTicks(void)
{
// FIXME: Should we implement a wrapping algorithm, like Win32?
}
Uint32 SDL_GetTicks(void)
{
UnsignedWide ms;
Microseconds (&ms);
return ( ms.lo / 1000 );
}
void SDL_Delay(Uint32 ms)
{
UnsignedWide microsecs;
UInt32 stop;
Microseconds (&microsecs);
stop = microsecs.lo + (ms * 1000);
while ( stop > microsecs.lo ) {
SystemTask ();
Microseconds (&microsecs);
}
}*/
/* Data to handle a single periodic alarm */
typedef struct _ExtendedTimerRec
{
TMTask tmTask;
ProcessSerialNumber taskPSN;
} ExtendedTimerRec, *ExtendedTimerPtr;
static ExtendedTimerRec gExtendedTimerRec;
int SDL_SYS_TimerInit(void)
{
FastInitialize ();
is_fast_inited = 1;
return(0);
}
void SDL_SYS_TimerQuit(void)
{
/* We don't need a cleanup? */
return;
}
/* Our Stub routine to set up and then call the real routine. */
pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr)
{
Uint32 ms;
WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN);
ms = SDL_alarm_callback(SDL_alarm_interval);
if ( ms ) {
SDL_alarm_interval = ROUND_RESOLUTION(ms);
PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask,
SDL_alarm_interval);
} else {
SDL_alarm_interval = 0;
}
}
int SDL_SYS_StartTimer(void)
{
/*
* Configure the global structure that stores the timing information.
*/
gExtendedTimerRec.tmTask.qLink = NULL;
gExtendedTimerRec.tmTask.qType = 0;
gExtendedTimerRec.tmTask.tmAddr = NewTimerProc(TimerCallbackProc);
gExtendedTimerRec.tmTask.tmCount = 0;
gExtendedTimerRec.tmTask.tmWakeUp = 0;
gExtendedTimerRec.tmTask.tmReserved = 0;
GetCurrentProcess(&gExtendedTimerRec.taskPSN);
/* Install the task record */
InsXTime((QElemPtr)&gExtendedTimerRec.tmTask);
/* Go! */
PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval);
return(0);
}
void SDL_SYS_StopTimer(void)
{
RmvTime((QElemPtr)&gExtendedTimerRec.tmTask);
}

View File

@@ -0,0 +1,181 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@devolution.com
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <windows.h>
#include <mmsystem.h>
#include "SDL_timer.h"
#include "SDL_timer_c.h"
#include "SDL_error.h"
#ifdef _WIN32_WCE
#define USE_GETTICKCOUNT
#define USE_SETTIMER
#endif
#define TIME_WRAP_VALUE (~(DWORD)0)
/* The first ticks value of the application */
static DWORD start;
void SDL_StartTicks(void)
{
/* Set first ticks value */
#ifdef USE_GETTICKCOUNT
start = GetTickCount();
#else
start = timeGetTime();
#endif
}
Uint32 SDL_GetTicks(void)
{
DWORD now, ticks;
#ifdef USE_GETTICKCOUNT
now = GetTickCount();
#else
now = timeGetTime();
#endif
if ( now < start ) {
ticks = (TIME_WRAP_VALUE-start) + now;
} else {
ticks = (now - start);
}
return(ticks);
}
void SDL_Delay(Uint32 ms)
{
Sleep(ms);
}
#ifdef USE_SETTIMER
static UINT WIN_timer;
int SDL_SYS_TimerInit(void)
{
return(0);
}
void SDL_SYS_TimerQuit(void)
{
return;
}
/* Forward declaration because this is called by the timer callback */
int SDL_SYS_StartTimer(void);
static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
Uint32 ms;
ms = SDL_alarm_callback(SDL_alarm_interval);
if ( ms != SDL_alarm_interval ) {
KillTimer(NULL, idEvent);
if ( ms ) {
SDL_alarm_interval = ROUND_RESOLUTION(ms);
SDL_SYS_StartTimer();
} else {
SDL_alarm_interval = 0;
}
}
}
int SDL_SYS_StartTimer(void)
{
int retval;
WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc);
if ( WIN_timer ) {
retval = 0;
} else {
retval = -1;
}
return retval;
}
void SDL_SYS_StopTimer(void)
{
if ( WIN_timer ) {
KillTimer(NULL, WIN_timer);
WIN_timer = 0;
}
}
#else /* !USE_SETTIMER */
/* Data to handle a single periodic alarm */
static UINT timerID = 0;
static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser,
DWORD dw1, DWORD dw2)
{
SDL_ThreadedTimerCheck();
}
int SDL_SYS_TimerInit(void)
{
MMRESULT result;
/* Set timer resolution */
result = timeBeginPeriod(TIMER_RESOLUTION);
if ( result != TIMERR_NOERROR ) {
SDL_SetError("Warning: Can't set %d ms timer resolution",
TIMER_RESOLUTION);
}
/* Allow 10 ms of drift so we don't chew on CPU */
timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC);
if ( ! timerID ) {
SDL_SetError("timeSetEvent() failed");
return(-1);
}
return(SDL_SetTimerThreaded(1));
}
void SDL_SYS_TimerQuit(void)
{
if ( timerID ) {
timeKillEvent(timerID);
}
timeEndPeriod(TIMER_RESOLUTION);
}
int SDL_SYS_StartTimer(void)
{
SDL_SetError("Internal logic error: Win32 uses threaded timer");
return(-1);
}
void SDL_SYS_StopTimer(void)
{
return;
}
#endif /* USE_SETTIMER */