home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / SW.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  2KB  |  80 lines

  1. /* These routines, plus the assembler hooks in stopwatch.asm, implement a
  2.  * general purpose "stopwatch" facility useful for timing the execution of
  3.  * code segments. The PC's "timer 2" channel (the one ordinarily
  4.  * used to drive the speaker) is used. It is driven with a 838 ns
  5.  * clock. The timer is 16 bits wide, so it "wraps around" in only 55 ms.
  6.  *
  7.  * There is an array of "stopwatch" structures used for recording the number
  8.  * of uses and the min/max times for each. Since only one hardware timer is
  9.  * available, only one stopwatch can actually be running at any one time.
  10.  *
  11.  * This facility is useful mainly for timing routines that must execute with
  12.  * interrupts disabled. An interrupt that occurs while the timer is running
  13.  * will not stop it, so it would show an errneously large value.
  14.  *
  15.  * To start a timer, call swstart(). To stop it and record its value,
  16.  * call swstop(n), where n is the number of the stopwatch structure.
  17.  * The stopwatch structures can be displayed with the "watch" command.
  18.  *
  19.  * Copyright 1991 Phil Karn, KA9Q
  20.  */
  21.  
  22. /****************************************************************************
  23. *    $Id: sw.c 1.2 93/07/16 11:50:57 ROOT_DOS Exp $
  24. *    15 Jul 93    1.2        GT    Fix warnings.                                    *
  25. ****************************************************************************/
  26.  
  27. #include "global.h"
  28. #include "pc.h"
  29.  
  30. struct stopwatch Sw[NSW];
  31.  
  32. /* Stop a stopwatch and record its value.
  33.  * Uses stopval() routine in stopwatch.asm
  34.  */
  35. void
  36. swstop(n)
  37. int n;
  38. {
  39.     register int16 cnt;
  40.     register struct stopwatch *sw;
  41.  
  42.     cnt = stopval();
  43.     sw = &Sw[n];
  44.  
  45.     if(sw->calls++ == 0){
  46.         sw->maxval = cnt;
  47.         sw->minval = cnt;
  48.     } else if(cnt > sw->maxval){
  49.         sw->maxval = cnt;
  50.     } else if(cnt < sw->minval){
  51.         sw->minval = cnt;
  52.     }
  53. }
  54. int
  55. doswatch(argc,argv,p)
  56. int argc;
  57. char *argv[];
  58. void *p;
  59. {
  60.     register struct stopwatch *sw;
  61.     long maxval,minval;
  62.     int i;
  63.  
  64.     if(argc > 1){
  65.         /* Clear timers */
  66.         for(i=0,sw=Sw;i < NSW;i++,sw++){
  67.             sw->calls = 0;
  68.         }
  69.     }
  70.     for(i=0,sw=Sw;sw < &Sw[NSW];i++,sw++){
  71.         if(sw->calls != 0){
  72.             minval = (65536L - sw->maxval) * 838 / 1000;
  73.             maxval = (65536L - sw->minval) * 838 / 1000;
  74.             tprintf("%d: calls %ld min %ld max %ld\n",
  75.              i,sw->calls,minval,maxval);
  76.         }
  77.     }
  78.     return 0;
  79. }
  80.