home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / DELAY.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  3.6 KB  |  83 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - delay.cas
  3.  *
  4.  * function(s)
  5.  *        delay - wait for specified period.
  6.  *--------------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #pragma inline
  19. #include <asmrules.h>
  20. #include <dos.h>
  21.  
  22. static unsigned long readtimer( void )
  23.   {
  24.   asm xor  ax,ax               /* es points to BIOS data area               */
  25.   asm mov  es,ax             
  26.   asm mov  al,0Ah              /* Ask to read irr                           */
  27.   asm cli                      /* Disable interrupts                        */
  28.   asm out  20h,al              /* Address PIC ocw3                          */
  29.   asm mov  al,0h               /* Latch timer 0                             */
  30.   asm out  43h,al
  31.   asm in   al,20h              /* Read irr                                  */
  32.   asm xchg di,ax               /* Save it in DI                             */
  33.   asm in   al,40h              /* Counter --> bx                            */
  34.   asm mov  bl,al               /* LSB in BL                                 */
  35.   asm in   al,40h              
  36.   asm mov  bh,al               /* MSB in BH                                 */
  37.   asm not  bx                  /* Need ascending counter                    */
  38.   asm in   al,21h              /* Read PIC imr                              */
  39.   asm xchg si,ax               /* Save it in SI                             */
  40.   asm mov  al,0FFh             /* Mask all interrupts                       */
  41.   asm out  21h,al
  42.   asm mov  dx,es:[46Ch]        /* read low word of time from BIOS data area */
  43.   asm xchg ax,si               /* Restore imr from SI                       */
  44.   asm out  21h,al
  45.   asm sti                      /* Enable interrupts                         */
  46.   asm xchg ax,di               /* Retrieve old irr                          */
  47.   asm test al,01h              /* Counter hit 0?                            */
  48.   asm jz   done                /* Jump if not                               */
  49.   asm cmp  bx,0FFh             /* Counter > 0x0FF?                          */
  50.   asm ja   done                /* Done if so                                */
  51.   asm inc  dx                  /* Else count int req.                       */
  52.  
  53.   done:
  54.   asm xchg ax,bx               /* set function result                       */
  55.  
  56.   return( MK_ULONG );
  57.   }
  58.  
  59.  
  60. /*--------------------------------------------------------------------------*
  61.  
  62. Name            delay - wait for specified period.
  63.  
  64. Usage           void delay(unsigned milliseconds);
  65.  
  66. Prototype in    dos.h
  67.  
  68. Description     The current thread of execution is suspended for the specified
  69.                 number of milliseconds.
  70.  
  71. Return value    None
  72. *---------------------------------------------------------------------------*/
  73. void delay( unsigned milliseconds )
  74.   {
  75.   unsigned long start, stop;
  76.  
  77.   start = readtimer();
  78.   stop = start + (milliseconds * 1193L);
  79.  
  80.   while( readtimer() < stop )
  81.     ;
  82.   }
  83.