Date: Wed, 6 May 92 13:11 +1000 From: Bill Metzenthen Subject: Re: delay() To: djgpp AT sun DOT soe DOT clarkson DOT edu Status: O Ted Cox (jtcox AT gumby DOT syr DOT edu) writes: >Has anyone implemented a version of Borland's delay() for djgpp? I >would like to use such a function, and have the source for Borland's >routine, but am not eager (or competent) to convert the ASM from tasm >style to as style. I had a need for this and wrote a kludge to perform the function. I can't say that I am proud of it, but it works for me ;-) The file "delay.c" is attached... --Bill --------------- snip,snip ---- file "delay.c" ---- snip,snip ----------- /* A delay() function for DJGPP */ #define DJGPP_BASE 0xe0000000 #define TICK_COUNT ( /* (volatile unsigned short) */ \ *((volatile unsigned short *)(DJGPP_BASE + 0x46c)) \ ) #define DIDDLE ( /* (volatile unsigned short) */ \ *((volatile unsigned short *)(DJGPP_BASE + 0x00)) \ ) static initialize_delay(void); static unsigned short calibration = 0; void delay(unsigned int milliseconds) { unsigned short j; unsigned short diddle; int i; if ( calibration == 0 ) { unsigned short first = TICK_COUNT; unsigned int taken; initialize_delay(); if ( (taken = (TICK_COUNT - first)*55) >= milliseconds ) return; milliseconds -= taken; } diddle = DIDDLE; for ( i = 0; i < milliseconds; i++ ) { for ( j = 0; j < calibration; j++ ) if ( DIDDLE != diddle ) /* diddle = DIDDLE */ ; } } static initialize_delay(void) { unsigned short first; unsigned short count; count = 0; /* wait for a change of tick count */ first = TICK_COUNT; while ( first == TICK_COUNT ) ; first = TICK_COUNT; while ( first == TICK_COUNT ) count++; #ifdef DEBUG printf("count = %d\n", count); #endif /* DEBUG */ /* This should over-estimate the required value of calibration */ calibration = count/55; /* determine FUDGE by experiments with the compiler */ #define FUDGE 42 calibration -= (calibration/100)*FUDGE; #ifdef DEBUG count = 0; #endif /* DEBUG */ while ( 1 ) { first = TICK_COUNT; while ( first == TICK_COUNT ) ; first = TICK_COUNT; delay(55); if ( first == TICK_COUNT ) { #ifdef DEBUG printf("Repeated %d times\n", count); #endif /* DEBUG */ return; } /* adjust in 2% steps */ calibration -= calibration/50; #ifdef DEBUG count++; #endif /* DEBUG */ } } ------------------------------- snip,snip -----------------------------------