home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / SW.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  3KB  |  91 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. /* Mods by PA0GRI */
  22.   
  23. #include "global.h"
  24. #ifdef SWATCH
  25. #include "cmdparse.h"
  26. #include "pc.h"
  27.   
  28. struct stopwatch Sw[NSW];
  29. char Swflag = 1;            /* default is allow stop watch functions */
  30.   
  31. /* Stop a stopwatch and record its value.
  32.  * Uses stopval() routine in stopwatch.asm
  33.  */
  34. void
  35. swstop(n)
  36. int n;
  37. {
  38.     register int16 cnt;
  39.     register struct stopwatch *sw;
  40.   
  41.     if((int)!Swflag)
  42.         return;         /* no stop watch functions */
  43.   
  44.     cnt = stopval();
  45.     sw = &Sw[n];
  46.   
  47.     if(sw->calls++ == 0){
  48.         sw->maxval = cnt;
  49.         sw->minval = cnt;
  50.     } else if(cnt > sw->maxval){
  51.         sw->maxval = cnt;
  52.     } else if(cnt < sw->minval){
  53.         sw->minval = cnt;
  54.     }
  55. }
  56. int
  57. doswatch(argc,argv,p)
  58. int argc;
  59. char *argv[];
  60. void *p;
  61. {
  62.     register struct stopwatch *sw;
  63.     long maxval,minval;
  64.     int i, ret, flag;
  65.   
  66.     if(argc > 1){
  67.         /* Clear timers */
  68.         for(i=0,sw=Sw;i < NSW;i++,sw++){
  69.             sw->calls = 0;
  70.         }
  71.     }
  72.     flag = (int)Swflag;
  73.     ret = setbool(&flag,"Stop Watch flag",argc,argv);
  74.     Swflag = (char)flag;
  75.   
  76.     if(ret != 0 || argc > 1)
  77.         return ret;
  78.   
  79.     for(i=0,sw=Sw;sw < &Sw[NSW];i++,sw++){
  80.         if(sw->calls != 0){
  81.             minval = (65536L - sw->maxval) * 838 / 1000;
  82.             maxval = (65536L - sw->minval) * 838 / 1000;
  83.             tprintf("%d: calls %ld min %ld max %ld\n",
  84.             i,sw->calls,minval,maxval);
  85.         }
  86.     }
  87.     return 0;
  88. }
  89. #endif /* SWATCH */
  90.   
  91.