src/timer/amigaos/SDL_systimer.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    58 
    58 
    59 #if !defined(__PPC__) || defined(STORMC4_WOS) || defined(MORPHOS)
    59 #if !defined(__PPC__) || defined(STORMC4_WOS) || defined(MORPHOS)
    60 static clock_t start;
    60 static clock_t start;
    61 
    61 
    62 void
    62 void
    63 SDL_StartTicks (void)
    63 SDL_StartTicks(void)
    64 {
    64 {
    65     /* Set first ticks value */
    65     /* Set first ticks value */
    66     start = clock ();
    66     start = clock();
    67 }
    67 }
    68 
    68 
    69 Uint32
    69 Uint32
    70 SDL_GetTicks (void)
    70 SDL_GetTicks(void)
    71 {
    71 {
    72     clock_t ticks;
    72     clock_t ticks;
    73 
    73 
    74     ticks = clock () - start;
    74     ticks = clock() - start;
    75 
    75 
    76 #ifdef __SASC
    76 #ifdef __SASC
    77 // CLOCKS_PER_SEC == 1000 !
    77 // CLOCKS_PER_SEC == 1000 !
    78 
    78 
    79     return (ticks);
    79     return (ticks);
    83     return ticks * (1000 / CLOCKS_PER_SEC);
    83     return ticks * (1000 / CLOCKS_PER_SEC);
    84 #endif
    84 #endif
    85 }
    85 }
    86 
    86 
    87 void
    87 void
    88 SDL_Delay (Uint32 ms)
    88 SDL_Delay(Uint32 ms)
    89 {
    89 {
    90 // Do a busy wait if time is less than 50ms
    90 // Do a busy wait if time is less than 50ms
    91 
    91 
    92     if (ms < 50) {
    92     if (ms < 50) {
    93         clock_t to_wait = clock ();
    93         clock_t to_wait = clock();
    94 
    94 
    95 #ifndef __SASC
    95 #ifndef __SASC
    96         ms *= (CLOCKS_PER_SEC / 1000);
    96         ms *= (CLOCKS_PER_SEC / 1000);
    97 #endif
    97 #endif
    98         to_wait += ms;
    98         to_wait += ms;
    99 
    99 
   100         while (clock () < to_wait);
   100         while (clock() < to_wait);
   101     } else {
   101     } else {
   102         Delay (ms / 20);
   102         Delay(ms / 20);
   103     }
   103     }
   104 }
   104 }
   105 
   105 
   106 #else
   106 #else
   107 
   107 
   108 ULONG MY_CLOCKS_PER_SEC;
   108 ULONG MY_CLOCKS_PER_SEC;
   109 
   109 
   110 void PPC_TimerInit (void);
   110 void PPC_TimerInit(void);
   111 APTR MyTimer;
   111 APTR MyTimer;
   112 
   112 
   113 ULONG start[2];
   113 ULONG start[2];
   114 
   114 
   115 void
   115 void
   116 SDL_StartTicks (void)
   116 SDL_StartTicks(void)
   117 {
   117 {
   118     /* Set first ticks value */
   118     /* Set first ticks value */
   119     if (!MyTimer)
   119     if (!MyTimer)
   120         PPC_TimerInit ();
   120         PPC_TimerInit();
   121 
   121 
   122     PPCGetTimerObject (MyTimer, PPCTIMERTAG_CURRENTTICKS, start);
   122     PPCGetTimerObject(MyTimer, PPCTIMERTAG_CURRENTTICKS, start);
   123     start[1] >>= 10;
   123     start[1] >>= 10;
   124     start[1] |= ((result[0] & 0x3ff) << 22);
   124     start[1] |= ((result[0] & 0x3ff) << 22);
   125     start[0] >>= 10;
   125     start[0] >>= 10;
   126 }
   126 }
   127 
   127 
   128 Uint32
   128 Uint32
   129 SDL_GetTicks (void)
   129 SDL_GetTicks(void)
   130 {
   130 {
   131     ULONG result[2];
   131     ULONG result[2];
   132     PPCGetTimerObject (MyTimer, PPCTIMERTAG_CURRENTTICKS, result);
   132     PPCGetTimerObject(MyTimer, PPCTIMERTAG_CURRENTTICKS, result);
   133 
   133 
   134 //      PPCAsr64p(result,10);
   134 //      PPCAsr64p(result,10);
   135 // Non va, la emulo:
   135 // Non va, la emulo:
   136 
   136 
   137     result[1] >>= 10;
   137     result[1] >>= 10;
   141 
   141 
   142     return result[1] * 1000 / MY_CLOCKS_PER_SEC;
   142     return result[1] * 1000 / MY_CLOCKS_PER_SEC;
   143 }
   143 }
   144 
   144 
   145 void
   145 void
   146 SDL_Delay (Uint32 ms)
   146 SDL_Delay(Uint32 ms)
   147 {
   147 {
   148 // Do a busy wait if time is less than 50ms
   148 // Do a busy wait if time is less than 50ms
   149 
   149 
   150     if (ms < 50) {
   150     if (ms < 50) {
   151         ULONG to_wait[2], actual[2];
   151         ULONG to_wait[2], actual[2];
   152         PPCGetTimerObject (MyTimer, PPCTIMERTAG_CURRENTTICKS, result);
   152         PPCGetTimerObject(MyTimer, PPCTIMERTAG_CURRENTTICKS, result);
   153         actual[1] = 0;
   153         actual[1] = 0;
   154         to_wait[1] += ms * 1000 / MY_CLOCKS_PER_SEC;
   154         to_wait[1] += ms * 1000 / MY_CLOCKS_PER_SEC;
   155 
   155 
   156         while (actual[1] < to_wait[1]) {
   156         while (actual[1] < to_wait[1]) {
   157             PPCGetTimerObject (MyTimer, PPCTIMERTAG_CURRENTTICKS, actual);
   157             PPCGetTimerObject(MyTimer, PPCTIMERTAG_CURRENTTICKS, actual);
   158         }
   158         }
   159     } else {
   159     } else {
   160         Delay (ms / 50);
   160         Delay(ms / 50);
   161     }
   161     }
   162 }
   162 }
   163 
   163 
   164 void
   164 void
   165 PPC_TimerInit (void)
   165 PPC_TimerInit(void)
   166 {
   166 {
   167     struct TagItem tags[] = {
   167     struct TagItem tags[] = {
   168         PPCTIMERTAG_CPU, TRUE,
   168         PPCTIMERTAG_CPU, TRUE,
   169         TAG_DONE, 0
   169         TAG_DONE, 0
   170     };
   170     };
   171 
   171 
   172 
   172 
   173     if (MyTimer = PPCCreateTimerObject (tags)) {
   173     if (MyTimer = PPCCreateTimerObject(tags)) {
   174         ULONG result[2];
   174         ULONG result[2];
   175 
   175 
   176         PPCGetTimerObject (MyTimer, PPCTIMERTAG_TICKSPERSEC, result);
   176         PPCGetTimerObject(MyTimer, PPCTIMERTAG_TICKSPERSEC, result);
   177         D (bug
   177         D(bug("Timer inizializzato, TPS: %lu - %lu\n", result[0], result[1]));
   178            ("Timer inizializzato, TPS: %lu - %lu\n", result[0], result[1]));
       
   179 //              PPCAsr64p(result,10);
   178 //              PPCAsr64p(result,10);
   180         result[1] >>= 10;
   179         result[1] >>= 10;
   181         result[1] |= ((result[0] & 0x3ff) << 22);
   180         result[1] |= ((result[0] & 0x3ff) << 22);
   182         result[0] >>= 10;
   181         result[0] >>= 10;
   183 
   182 
   184         D (bug ("Shiftato TPS: %lu - %lu\n", result[0], result[1]));
   183         D(bug("Shiftato TPS: %lu - %lu\n", result[0], result[1]));
   185         MY_CLOCKS_PER_SEC = result[1];
   184         MY_CLOCKS_PER_SEC = result[1];
   186 
   185 
   187         PPCGetTimerObject (MyTimer, PPCTIMERTAG_CURRENTTICKS, result);
   186         PPCGetTimerObject(MyTimer, PPCTIMERTAG_CURRENTTICKS, result);
   188 
   187 
   189         D (bug ("Current ticks: %lu - %lu\n", result[0], result[1]));
   188         D(bug("Current ticks: %lu - %lu\n", result[0], result[1]));
   190         result[1] >>= 10;
   189         result[1] >>= 10;
   191         result[1] |= ((result[0] & 0x3ff) << 22);
   190         result[1] |= ((result[0] & 0x3ff) << 22);
   192         result[0] >>= 10;
   191         result[0] >>= 10;
   193 //              PPCAsr64p(result,10);
   192 //              PPCAsr64p(result,10);
   194         D (bug ("Shiftato: %lu - %lu\n", result[0], result[1]));
   193         D(bug("Shiftato: %lu - %lu\n", result[0], result[1]));
   195     } else {
   194     } else {
   196         D (bug ("Errore nell'inizializzazione del timer!\n"));
   195         D(bug("Errore nell'inizializzazione del timer!\n"));
   197     }
   196     }
   198 }
   197 }
   199 
   198 
   200 #endif
   199 #endif
   201 
   200 
   204 /* Data to handle a single periodic alarm */
   203 /* Data to handle a single periodic alarm */
   205 static int timer_alive = 0;
   204 static int timer_alive = 0;
   206 static SDL_Thread *timer_thread = NULL;
   205 static SDL_Thread *timer_thread = NULL;
   207 
   206 
   208 static int
   207 static int
   209 RunTimer (void *unused)
   208 RunTimer(void *unused)
   210 {
   209 {
   211     D (bug ("SYSTimer: Entering RunTimer loop..."));
   210     D(bug("SYSTimer: Entering RunTimer loop..."));
   212 
   211 
   213     if (GfxBase == NULL)
   212     if (GfxBase == NULL)
   214         GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 37);
   213         GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 37);
   215 
   214 
   216     while (timer_alive) {
   215     while (timer_alive) {
   217         if (SDL_timer_running) {
   216         if (SDL_timer_running) {
   218             SDL_ThreadedTimerCheck ();
   217             SDL_ThreadedTimerCheck();
   219         }
   218         }
   220         if (GfxBase)
   219         if (GfxBase)
   221             WaitTOF ();         // Check the timer every fifth of seconds. Was SDL_Delay(1)->BusyWait!
   220             WaitTOF();          // Check the timer every fifth of seconds. Was SDL_Delay(1)->BusyWait!
   222         else
   221         else
   223             Delay (1);
   222             Delay(1);
   224     }
   223     }
   225     D (bug ("SYSTimer: EXITING RunTimer loop..."));
   224     D(bug("SYSTimer: EXITING RunTimer loop..."));
   226     return (0);
   225     return (0);
   227 }
   226 }
   228 
   227 
   229 /* This is only called if the event thread is not running */
   228 /* This is only called if the event thread is not running */
   230 int
   229 int
   231 SDL_SYS_TimerInit (void)
   230 SDL_SYS_TimerInit(void)
   232 {
   231 {
   233     D (bug ("Creating thread for the timer (NOITIMER)...\n"));
   232     D(bug("Creating thread for the timer (NOITIMER)...\n"));
   234 
   233 
   235     timer_alive = 1;
   234     timer_alive = 1;
   236     timer_thread = SDL_CreateThread (RunTimer, NULL);
   235     timer_thread = SDL_CreateThread(RunTimer, NULL);
   237     if (timer_thread == NULL) {
   236     if (timer_thread == NULL) {
   238         D (bug ("Creazione del thread fallita...\n"));
   237         D(bug("Creazione del thread fallita...\n"));
   239 
   238 
   240         return (-1);
   239         return (-1);
   241     }
   240     }
   242     return (SDL_SetTimerThreaded (1));
   241     return (SDL_SetTimerThreaded(1));
   243 }
   242 }
   244 
   243 
   245 void
   244 void
   246 SDL_SYS_TimerQuit (void)
   245 SDL_SYS_TimerQuit(void)
   247 {
   246 {
   248     timer_alive = 0;
   247     timer_alive = 0;
   249     if (timer_thread) {
   248     if (timer_thread) {
   250         SDL_WaitThread (timer_thread, NULL);
   249         SDL_WaitThread(timer_thread, NULL);
   251         timer_thread = NULL;
   250         timer_thread = NULL;
   252     }
   251     }
   253 }
   252 }
   254 
   253 
   255 int
   254 int
   256 SDL_SYS_StartTimer (void)
   255 SDL_SYS_StartTimer(void)
   257 {
   256 {
   258     SDL_SetError ("Internal logic error: AmigaOS uses threaded timer");
   257     SDL_SetError("Internal logic error: AmigaOS uses threaded timer");
   259     return (-1);
   258     return (-1);
   260 }
   259 }
   261 
   260 
   262 void
   261 void
   263 SDL_SYS_StopTimer (void)
   262 SDL_SYS_StopTimer(void)
   264 {
   263 {
   265     return;
   264     return;
   266 }
   265 }
   267 
   266 
   268 #endif /* SDL_TIMER_AMIGA */
   267 #endif /* SDL_TIMER_AMIGA */