home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / pascal / 7405 < prev    next >
Encoding:
Internet Message Format  |  1992-12-12  |  3.7 KB

  1. Path: sparky!uunet!olivea!spool.mu.edu!agate!usenet.ins.cwru.edu!po.CWRU.Edu!wct
  2. From: wct@po.CWRU.Edu (William C. Thompson)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: High speed clock
  5. Message-ID: <1gc63eINNjie@usenet.INS.CWRU.Edu>
  6. Date: 12 Dec 92 07:58:37 GMT
  7. References: <1992Dec12.070535.26355@uwasa.fi> <1992Nov20.091212.24871@lth.se> <peter.422.723796479@psychnet.psychol.utas.edu.au> <Bz47zL.5sH@news.cso.uiuc.edu>
  8. Reply-To: wct@po.CWRU.Edu (William C. Thompson)
  9. Organization: Case Western Reserve University, Cleveland, OH (USA)
  10. Lines: 135
  11. NNTP-Posting-Host: slc5.ins.cwru.edu
  12.  
  13.  
  14. I don't normally do this, but here is a unit I have for timing.
  15.  
  16. Unit Timer;
  17.  
  18. {
  19.  *********************************************************************
  20.  **                                                                 **
  21.  ** Author....: Chris Wood                                          **
  22.  ** Date......: 7-11-1992                                           **
  23.  ** Internet..: woodc@jacobs.cs.orst.edu                            **
  24.  ** BBS.......: A Separate Reality (503)926-6991                    **
  25.  **             (Leave mail to the sysop)                           **
  26.  **                                                                 **
  27.  **   This source code is being released as Free-Ware.  You may use **
  28.  ** this code in your programs and modify it to fit your needs. The **
  29.  ** only  restrictions are that you  may not distribute the  source **
  30.  ** code in modified form or charge for the source code itself.  If **
  31.  ** you  discover any  errors  in the  source code  or find a  more **
  32.  ** efficient way to handle any of the following code, please leave **
  33.  ** me e-mail at the internet address or the BBS listed above.  All **
  34.  ** comments are welcome.                                           **
  35.  **                                                                 **
  36.  *********************************************************************
  37. }
  38.  
  39. {$O+}
  40. {$N+}
  41. {$E+}
  42.  
  43. Interface
  44.  
  45. Var ClockTicks : LongInt;  { Number of ticks elapsed }
  46.  
  47. Procedure TimerOn ( Freq : LongInt );
  48. Procedure TimerOff;
  49. Procedure ResetTimer;
  50. Function TimeElapsed : Real;
  51.  
  52. Implementation
  53.  
  54. Uses CRT,DOS;
  55.  
  56. Const MaxRate = 1193180;
  57.  
  58. Var OldInt08        : Procedure;
  59.     OldInt1C        : Procedure;
  60.     IntCount08      : Word;
  61.     Trigger         : Word;
  62.     TimerAlreadySet : Boolean;
  63.     Frequency       : Word;
  64.  
  65. Procedure IrqOn; Inline($FB);
  66.  
  67. Procedure IrqOff; Inline($FA);
  68.  
  69. {$F+}
  70. Procedure NewInt1C; Interrupt;
  71. Begin
  72.   Inc(ClockTicks);
  73. End;
  74. {$F-}
  75.  
  76. {$F+}
  77. Procedure NewInt08; Interrupt;
  78. Begin
  79.   IrqOff;
  80.   Inline($CD/$1C); {Generate INT 1Ch instruction to call interrupt 1Ch}
  81.   If IntCount08=Trigger then
  82.   Begin
  83.     IntCount08:=0;
  84.     Inline($9C);
  85.     OldInt08;
  86.   End
  87.     Else
  88.   Begin
  89.     Inc(IntCount08);
  90.   End;
  91.   Port[$20]:=$20; {Sends non-specific EOI to the PIC}
  92.   IrqOn;
  93. End;
  94. {$F-}
  95.  
  96. Procedure TimerOn ( Freq : LongInt );
  97. Var Temp  : LongInt;
  98.     Count : Word;
  99. Begin
  100.   If Not(TimerAlreadySet) then
  101.   Begin
  102.     ClockTicks:=0;
  103.     IntCount08:=0;
  104.     Frequency:=Freq;
  105.     Trigger:=Trunc(Freq/18.2);
  106.     Temp:=MaxRate;
  107.     Temp:=Trunc(Temp/Freq);
  108.     Count:=Temp;
  109.     GetIntVec($08,@OldInt08);
  110.     SetIntVec($08,Addr(NewInt08));
  111.     GetIntVec($1C,@OldInt1C);
  112.     SetIntVec($1C,Addr(NewInt1C));
  113.     Port[$43]:=$B6;
  114.     Port[$40]:=Lo(Count);
  115.     Port[$40]:=Hi(Count);
  116.     TimerAlreadySet:=True;
  117.   End;
  118. End;
  119.  
  120. Procedure TimerOff;
  121. Begin
  122.   If TimerAlreadySet then
  123.   Begin
  124.     Port[$43]:=$B6;
  125.     Port[$40]:=$FF;
  126.     Port[$40]:=$FF;
  127.     SetIntVec($08,@OldInt08);
  128.     SetIntvec($1C,@OldInt1C);
  129.     TimerAlreadySet:=False;
  130.   End;
  131. End;
  132.  
  133. Procedure ResetTimer;
  134. Begin
  135.   ClockTicks:=0;
  136. End;
  137.  
  138. Function TimeElapsed : Real;
  139. Begin
  140.   TimeElapsed:=ClockTicks/Frequency;
  141. End;
  142.  
  143. Begin
  144.   TimerAlreadySet:=False;
  145. End.
  146. -- 
  147. "What do you mean Star Trek isn't real!?" - anonymous
  148.