delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/09/05:01:54

From: sams AT nbisrv DOT nbi DOT dk
Newsgroups: comp.os.msdos.djgpp
Subject: 16bit timer problems
Message-ID: <1997May6.154232.3808@news.nbi.dk>
Date: 6 May 97 15:42:32 +0200
Organization: Niels Bohr Institute and Nordita, Copenhagen
Lines: 77
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

For a long time I've been using the timer function timer16 below to read 
the 1193180 Hz timer. It has worked flawlessly, and the test routine main 
below has always given uncertainty in timing (dtmax) less than 20 micro-
seconds. 
However, having bought a new Pentium Pro, I experience problems: 
I get dtmax of order 27000 microseconds. 
Does anybody have a clue? Could it be a parameter in the AMI bios 
that should be changed? Other suggestions. 
Ideas welcome!
Thomas Sams.




/* ------------------------------------------------------------------------ */
/* tim16tst.c, Thomas Sams, 970507.                                         */
/* Version for gnuCC (djgpp port to dos).                                   */
/* inportb and outportb should be changed for other C compilers,            */
/* and cli, sti should possibly be changed to disable(), enable().          */
/* Test program to test that interupts can be cleared and the 1193180Hz     */
/* can be read. The program should print out small values of dtmin and      */
/* dtmax, of order 9 microseconds respectively 18 microseconds for a 40MHz  */
/* i386 processor.                                                          */
/* ------------------------------------------------------------------------ */
#include <stdio.h>
#include <pc.h>
#include <stdlib.h>

#define CLOCK_FREQ 1193180

/* ------------------------------------------------------------------------ */
void cli()                                              /* disable interupt */
{
  __asm__ __volatile__ ("cli");
}
/* ------------------------------------------------------------------------ */
void sti()                                               /* enable interupt */
{
  __asm__ __volatile__ ("sti");
}
/* ------------------------------------------------------------------------ */
unsigned short timer16()                /* Usage: cli(); timer16(); sti();  */
{
  unsigned short sts, lsb, msb, result;
  do{
    outportb(0x43,0xc2);
    sts = inportb(0x40);
    lsb = inportb(0x40);
    msb = inportb(0x40);
    result = (msb<<7) | (lsb>>1);
  }while(!result);
  result |= ((sts<<8) & (1<<15)) ;
  return ~result;                                     /* return 16bit timer */
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
void main(int argc, char *argv[]){

  int i; 
  double t=0, t1, t2, dt, dtmin=1.e100, dtmax=-1.e100;

  cli();
  for(i=0;i<1000000;i++){
    t1 = timer16();
    t2 = timer16();
    dt = t2 - t1;
    if(dt<0) dt += 0x10000;
    if(dt<dtmin) dtmin = dt;
    if(dt>dtmax) dtmax = dt;
    t += dt;
  }
  sti();
  printf("t=%3.0f mus   dtmin=%3.0f mus   dtmax=%3.0f mus   \n",
    1000000*t/CLOCK_FREQ,1000000*dtmin/CLOCK_FREQ,1000000*dtmax/CLOCK_FREQ);
  return;
}
/* ------------------------------------------------------------------------ */

- Raw text -


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