home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PARADIS1 / DELAYP.PAS < prev    next >
Pascal/Delphi Source File  |  1992-04-27  |  3KB  |  103 lines

  1. (11146) Mon 20 Apr 92 10:53
  2. By: Wilbert Van.leijen
  3. To: Matt Schuttloffel
  4. Re: Delay Patch
  5. St:
  6. ---------------------------------------------------------------------------
  7. @MSGID: 2:500/12.10956 172E03D5
  8. @REPLY: 1:170/806 3491809e
  9. @PID: Dutched 2.91e 0013310373
  10. ** Quoting a message from Matt Schuttloffel to Dj Murdoch **
  11.  
  12.  > I once saw some code that would time and delay within a millionth of a
  13.  > second accuracy.  It somehow used a chip on the PC-Speaker to do it.  I
  14.  > think I got it off of TurboCity, but I seemed to have lost it.  :-(
  15.  
  16. Const
  17.   Timer0       = $40;                  { 8253 Timer 0 (Ticker timer) }
  18.   CtrlWord     = $43;                  { 8253 Control Word }
  19.   Mode2        = $34;                  { Free running 16-bit counter }
  20.   TickLength   = 55;                   { Tick length = 55 ms }
  21.   TickAddr     = $46C;                 { Lower word in BIOS data area }
  22.   MiliSec      = 1192;                 { Number of Timer 0 cycles in 1 ms }
  23.  
  24. { Read out the current value of the timer 0 }
  25.  
  26. Procedure ReadTimer; Assembler;
  27.  
  28. ASM
  29.         IN     AL, Timer0
  30.         XOR    AH, AH
  31.         MOV    DX, AX
  32.         IN     AL, Timer0
  33.         XCHG   AH, AL
  34.         ADD    AX, DX
  35. end;  { ReadTimer }
  36.  
  37. { Timed delay function }
  38.  
  39. Procedure Delay(time : Word); Assembler;
  40.  
  41. ASM
  42.     { Clear DX to prevent divide overflow.
  43.       Devide DelayTime in ticks (blocks of 55 ms).
  44.       Store the offset in ticks in OfsTicks.       }
  45.  
  46.         XOR     DX, DX
  47.         MOV     AX, time
  48.         MOV     CX, TickLength
  49.         DIV     CX
  50.         MOV     BX, AX
  51.  
  52.     { Reset timer for the sake of accuracy }
  53.  
  54.         MOV     AL, Mode2
  55.         OUT     CtrlWord, AL
  56.         XOR     AX, AX
  57.         OUT     Timer0, AL
  58.         OUT     Timer0, AL
  59.  
  60.     { Get the current value of the ticker timer at address 0000h:046Ch }
  61.  
  62.         MOV     ES, AX
  63.         MOV     DI, TickAddr
  64.         MOV     AX, ES:[DI]
  65.         ADD     AX, BX
  66.         MOV     SI, AX
  67.  
  68.     { Repeat
  69.       Until ticker timer value = SI; }
  70.  
  71. @1:     MOV     AX, ES:[DI]
  72.         CMP     AX, SI
  73.         JNZ     @1
  74.  
  75.     { Calculate remainder of delay time in cycles of Timer 0.
  76.       1192 cycles = 0.001 seconds
  77.       SI = (current value of Timer 0) - (1192*(DelayTime mod 55)); }
  78.  
  79.         XCHG   AX, DX
  80.         MOV    CX, MiliSec
  81.         MUL    CX
  82.         MOV    BX, AX
  83.         CALL   ReadTimer
  84.         SUB    AX, BX
  85.         MOV    SI, AX
  86.  
  87.     { Repeat }
  88.  
  89. @2:     CALL   ReadTimer
  90.         CMP    AX, SI
  91.         JNB    @2
  92.  
  93.     { Until Timer 0 value < SI }
  94.  
  95. end;  { Delay }
  96.  
  97. Cheerio, Wilbert
  98.  
  99. --- Dutchie V2.91e
  100.  * Origin: Programming Art's Computer (2:500/12.10956)
  101.  
  102. @PATH: 500/12 2 9 512/0 1007 
  103.