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