www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/22/15:21:05

Message-Id: <3.0.5.32.19990822133535.007ab400@200.252.238.1>
X-Sender: thiagofga AT 200 DOT 252 DOT 238 DOT 1
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.5 (32)
Date: Sun, 22 Aug 1999 13:35:35 -0300
To: djgpp AT delorie DOT com
From: "Thiago F.G. Albuquerque" <thiagofga AT ambr DOT com DOT br>
Subject: Detect clock speed
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com

Hi,

How can I detect the clock speed?

Thomas Harte <T DOT Harte AT btinternet DOT com> sent me this piece of code, but this
method is quite slow (you have to wait for a whole second) and not very
accurate. Any suggestions?

Thiago
-------------------------------------------

#include <allegro.h>

uint64 tsc();
typedef unsigned long long uint64;

// allegro timer callback ///////////////////////////////////////////////////

volatile int timervar;

void timerfunc()
{
   timervar++;
}
END_OF_FUNCTION(timerfunc);

// get_clockspeed() /////////////////////////////////////////////////////////

uint64 get_clockspeed()
{
   uint64 ticks[3];
   int count = 3;

   //install an Allegro timer
   LOCK_VARIABLE(timervar);
   LOCK_FUNCTION(timerfunc);
   install_int_ex(timerfunc, BPS_TO_TIMER(50));

   //now to wait until the timer has finished one cycle so that our first
   //reading is not 'short' . . . also the reason that our timer is beating
   //at 50 bps and not 1 - to cut down the length of time waiting for an
   //entry point

   timervar = 0;
   while(!timervar)
      ;

   //reads three RDTSC values - each a second apart. Theoretically it could
   //be checked that the Hz values generated between reads 1&2 and 2&3 do not
   //vary greatly from half the difference between 1&3 to check that no great
   //error has occured (e.g. our program was interrupted in a multitasking
   //environment, and missed one timer beat by a great distance, etc etc

   do
   {
      timervar = 0;
      count--;
      ticks[count] = tsc();

      while(timervar < 25)
         ;
   }
   while(count);
   remove_int(timerfunc);

   return (ticks[0] - ticks[2]);
}

uint64 tsc()
{
     uint64 ticks, temp;
     uint top, bottom;

     __asm__ __volatile__("rdtsc":"=d"(top), "=a"(bottom));

     temp = top;
     ticks = (temp << 32) | bottom;
     return ticks;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019