src/timer/mint/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@281
     1
/*
slouken@281
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@281
     4
slouken@281
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@281
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@281
     9
slouken@281
    10
    This library is distributed in the hope that it will be useful,
slouken@281
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@281
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@281
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@281
    18
slouken@281
    19
    Sam Lantinga
slouken@281
    20
    slouken@libsdl.org
slouken@281
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@281
    23
slouken@1635
    24
#ifdef SDL_TIMER_MINT
slouken@1635
    25
slouken@281
    26
/*
slouken@281
    27
 *	TOS/MiNT timer driver
slouken@281
    28
 *	based on vbl vector
slouken@281
    29
 *
slouken@281
    30
 *	Patrice Mandin
slouken@281
    31
 */
slouken@281
    32
slouken@281
    33
#include <stdio.h>
slouken@281
    34
#include <sys/time.h>
slouken@281
    35
#include <signal.h>
slouken@281
    36
#include <unistd.h>
slouken@281
    37
#include <string.h>
slouken@281
    38
#include <errno.h>
slouken@281
    39
patmandin@1045
    40
#include <mint/cookie.h>
patmandin@1045
    41
#include <mint/sysvars.h>
slouken@281
    42
#include <mint/osbind.h>
patmandin@1045
    43
#include <mint/mintbind.h>
slouken@281
    44
slouken@281
    45
#include "SDL_timer.h"
slouken@1361
    46
#include "../SDL_timer_c.h"
slouken@281
    47
#include "SDL_thread.h"
slouken@281
    48
patmandin@1396
    49
#include "SDL_vbltimer_s.h"
slouken@281
    50
slouken@281
    51
/* The first ticks value of the application */
slouken@281
    52
static Uint32 start;
slouken@281
    53
static SDL_bool supervisor;
slouken@1662
    54
static int mint_present;        /* can we use Syield() ? */
slouken@281
    55
slouken@1662
    56
void
slouken@1668
    57
SDL_StartTicks(void)
slouken@281
    58
{
slouken@1662
    59
    void *oldpile;
slouken@1662
    60
    unsigned long dummy;
slouken@281
    61
slouken@1662
    62
    /* Set first ticks value */
slouken@1668
    63
    oldpile = (void *) Super(0);
slouken@1662
    64
    start = *((volatile long *) _hz_200);
slouken@1668
    65
    Super(oldpile);
slouken@281
    66
slouken@1662
    67
    start *= 5;                 /* One _hz_200 tic is 5ms */
patmandin@1045
    68
slouken@1668
    69
    mint_present = (Getcookie(C_MiNT, &dummy) == C_FOUND);
slouken@281
    70
}
slouken@281
    71
slouken@1662
    72
Uint32
slouken@1668
    73
SDL_GetTicks(void)
slouken@281
    74
{
slouken@1662
    75
    Uint32 now;
slouken@1662
    76
    void *oldpile = NULL;
slouken@281
    77
slouken@1662
    78
    /* Check if we are in supervisor mode 
slouken@1662
    79
       (this is the case when called from SDL_ThreadedTimerCheck,
slouken@1662
    80
       which is called from RunTimer, running in the vbl vector)
slouken@1662
    81
     */
slouken@1662
    82
    if (!supervisor) {
slouken@1668
    83
        oldpile = (void *) Super(0);
slouken@1662
    84
    }
slouken@281
    85
slouken@1662
    86
    now = *((volatile long *) _hz_200);
slouken@281
    87
slouken@1662
    88
    if (!supervisor) {
slouken@1668
    89
        Super(oldpile);
slouken@1662
    90
    }
slouken@281
    91
slouken@1662
    92
    return ((now * 5) - start);
slouken@281
    93
}
slouken@281
    94
slouken@1662
    95
void
slouken@1668
    96
SDL_Delay(Uint32 ms)
slouken@281
    97
{
slouken@1662
    98
    Uint32 now;
slouken@281
    99
slouken@1668
   100
    now = SDL_GetTicks();
slouken@1668
   101
    while ((SDL_GetTicks() - now) < ms) {
slouken@1662
   102
        if (mint_present) {
slouken@1668
   103
            Syield();
slouken@1662
   104
        }
slouken@1662
   105
    }
slouken@281
   106
}
slouken@281
   107
slouken@281
   108
/* Data to handle a single periodic alarm */
slouken@1662
   109
static SDL_bool timer_installed = SDL_FALSE;
slouken@281
   110
slouken@1662
   111
static void
slouken@1668
   112
RunTimer(void)
slouken@281
   113
{
slouken@1662
   114
    supervisor = SDL_TRUE;
slouken@1668
   115
    SDL_ThreadedTimerCheck();
slouken@1662
   116
    supervisor = SDL_FALSE;
slouken@281
   117
}
slouken@281
   118
slouken@281
   119
/* This is only called if the event thread is not running */
slouken@1662
   120
int
slouken@1668
   121
SDL_SYS_TimerInit(void)
slouken@281
   122
{
slouken@1662
   123
    void *oldpile;
slouken@281
   124
slouken@1662
   125
    supervisor = SDL_FALSE;
slouken@281
   126
slouken@1662
   127
    /* Install RunTimer in vbl vector */
slouken@1668
   128
    oldpile = (void *) Super(0);
slouken@1668
   129
    timer_installed = !SDL_AtariVblInstall(RunTimer);
slouken@1668
   130
    Super(oldpile);
slouken@281
   131
slouken@1662
   132
    if (!timer_installed) {
slouken@1662
   133
        return (-1);
slouken@1662
   134
    }
slouken@1668
   135
    return (SDL_SetTimerThreaded(0));
slouken@281
   136
}
slouken@281
   137
slouken@1662
   138
void
slouken@1668
   139
SDL_SYS_TimerQuit(void)
slouken@281
   140
{
slouken@1662
   141
    void *oldpile;
slouken@281
   142
slouken@1662
   143
    if (timer_installed) {
slouken@1662
   144
        /* Uninstall RunTimer vbl vector */
slouken@1668
   145
        oldpile = (void *) Super(0);
slouken@1668
   146
        SDL_AtariVblUninstall(RunTimer);
slouken@1668
   147
        Super(oldpile);
slouken@1662
   148
        timer_installed = SDL_FALSE;
slouken@1662
   149
    }
slouken@281
   150
}
slouken@281
   151
slouken@1662
   152
int
slouken@1668
   153
SDL_SYS_StartTimer(void)
slouken@281
   154
{
slouken@1668
   155
    SDL_SetError("Internal logic error: MiNT uses vbl timer");
slouken@1662
   156
    return (-1);
slouken@281
   157
}
slouken@281
   158
slouken@1662
   159
void
slouken@1668
   160
SDL_SYS_StopTimer(void)
slouken@281
   161
{
slouken@1662
   162
    return;
slouken@281
   163
}
slouken@1635
   164
slouken@1635
   165
#endif /* SDL_TIMER_MINT */
slouken@1662
   166
/* vi: set ts=4 sw=4 expandtab: */