Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine CPU speed with a VFP FLL

Status
Not open for further replies.

rob444

Programmer
Nov 5, 2002
633
NL
This is sourcecode for a VFP FLL (needs to be compiled with Visual C++) to determine the speed of a CPU, and the determination of the CPU speed is written in Assembler, faster isn't possible...

Code:
#include "pro_ext.h"

// Originally written by Roberto Ianni
// Modified by Rob444

void FAR SpeedTest(ParamBlk FAR *parm)
{
   __int64 BeginCycle = 0, EndCycle = 0;
   unsigned __int64 nCtr = 0, nFreq = 0, nCtrStop = 0;

    // Obtains the frequency per second
    if(QueryPerformanceFrequency((LARGE_INTEGER *) &nFreq))
   {
      // Obtains the value of the current counter
      QueryPerformanceCounter((LARGE_INTEGER *) &nCtrStop);

      // Add the counter to the frequency
      nCtrStop += nFreq;

      _asm
         {
            // Obtain the beginning of the cycle from the CPU clock
            _emit 0x0f
            _emit 0x31
            mov DWORD PTR BeginCycle, eax
            mov DWORD PTR [BeginCycle + 4], edx
         }

         do{
            // retrieve the value of the performance counter
            // until 1 sec has gone by:
            // Waits for one second
            QueryPerformanceCounter((LARGE_INTEGER *) &nCtr);
         }while (nCtr < nCtrStop);

      _asm
         {
            // Obtains the CPU clock cycles again, but after one second
            _emit 0x0f
            _emit 0x31
            mov DWORD PTR EndCycle, eax
            mov DWORD PTR [EndCycle + 4], edx
         }
   }

   // EndCycle-BeginCycle is the speed in Hz,
   // which, divided by 1,000,000, is the speed in MHz.
   _RetFloat( ((float)EndCycle-(float)BeginCycle) / 1000000, 20, 8 );
}

FoxInfo myFoxInfo[] =
{
    {"CPUSpeed", (FPFI) CPUSpeed, 0, ""}
};
extern "C" {
FoxTable _FoxTable =
{
    (FoxTable *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};
}

Call it from VFP as follows:
Code:
SET LIBRARY TO "MyFLL.fll"
? CPUSpeed()
SET LIBRARY TO

 
SpeedTest" in the FLL sourcefile should be "CPUSpeed"

Rob.
 
Here then the corrected sourcecode for a FLL that determines the CPU SPEED in MHZ
Rob.

Code:
#include <stdio.h>
#include "pro_ext.h"

// Originally written by Roberto Ianni
// Modified by Rob444

void FAR CPUSpeed(ParamBlk FAR *parm)
{
   __int64 BeginCycle = 0, EndCycle = 0;
   unsigned __int64 nCtr = 0, nFreq = 0, nCtrStop = 0;

    // Obtains the frequency per second
    if(QueryPerformanceFrequency((LARGE_INTEGER *) &nFreq))
   {
      // Obtains the value of the current counter
      QueryPerformanceCounter((LARGE_INTEGER *) &nCtrStop);

      // Add the counter to the frequency
      nCtrStop += nFreq;

      _asm
         {
            // Obtain the beginning of the cycle from the CPU clock
            _emit 0x0f
            _emit 0x31
            mov DWORD PTR BeginCycle, eax
            mov DWORD PTR [BeginCycle + 4], edx
         }

         do{
            // retrieve the value of the performance counter
            // until 1 sec has gone by:
            // Waits for one second
            QueryPerformanceCounter((LARGE_INTEGER *) &nCtr);
         }while (nCtr < nCtrStop);

      _asm
         {
            // Obtains the CPU clock cycles again, but after one second
            _emit 0x0f
            _emit 0x31
            mov DWORD PTR EndCycle, eax
            mov DWORD PTR [EndCycle + 4], edx
         }
   }

   // EndCycle-BeginCycle is the speed in Hz,
   // which, divided by 1,000,000, is the speed in MHz.
   _RetFloat( ((float)EndCycle-(float)BeginCycle) / 1000000, 20, 8 );
}

FoxInfo myFoxInfo[] =
{
    {"CPUSpeed", (FPFI) CPUSpeed, 0, ""}
};
extern "C" {
FoxTable _FoxTable =
{
    (FoxTable *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top