📄 timer.c
字号:
** Insert the name of the function in which the error was discovered. **
** This should always be provided, but check for NULL to be safe. **
\*---------------------------------------------------------------------*/
strcat ( error_message, "FUNCTION: " );
if ( pCalling_function_name != (char *)NULL )
strcat ( error_message, pCalling_function_name );
else
strcat ( error_message, "<not specified - kill the programmer>" );
strcat ( error_message, "\n" );
/*---------------------------------------------------------------------*\
** Insert the canned error message text for the specified error **
** number. **
\*---------------------------------------------------------------------*/
strcat ( error_message, gpTM_error_text[error_number] );
strcat ( error_message, "\n" );
/*---------------------------------------------------------------------*\
** Insert the custom_message, if it is supplied. This custom message **
** should provide more detailed information than the generic error **
** strings. **
\*---------------------------------------------------------------------*/
if ( pCustom_message != (char *)NULL )
{
strcat ( error_message, pCustom_message );
} /* end if ( custom message was supplied ) */
strcat ( error_message, "\n" );
/*---------------------------------------------------------------------*\
** Send the message off to stderr. **
\*---------------------------------------------------------------------*/
fprintf ( stderr, "%s", error_message );
} /* end _TM_Error ( ) */
/***************************************************************************\
** tTIME TM_ReadTime( ) **
*****************************************************************************
** ARGUMENTS **
** void **
**-------------------------------------------------------------------------**
** RETURNS **
** tTIME - the current time measured in units of 838ns **
**-------------------------------------------------------------------------**
** EXAMPLE USAGE (NOT INTENDED FOR EXTERNAL USE) **
** InitializeApplication(); **
** TM_Init(1); **
** . . . **
** current_time = TM_ReadTime( ); <-----<< **
** . . . **
** TM_Close(); **
** ShutDownApplication(); **
**-------------------------------------------------------------------------**
** DETECTABLE ERROR CONDITIONS **
** * Module has not been initialized (TM_Init() not called) **
**-------------------------------------------------------------------------**
** DESCRIPTION **
** This function generates a 32-bit value in units of 838ns. This **
** value spans a time period of about one hour. The high 16 bits of **
** this value come from the large timer (the 32-bit value at **
** 0x40:0x6C, the number of timer cycles since midnight), and the **
** low 16 bits come from system timer 0's counter. **
** **
** NOTE: System timer 0's counter repeatedly cycles from 65535 down **
** to 0 (decremented), but time is steadily increasing. To **
** get a steadily increasing value from this timer, we subtract **
** the actual value of the counter from 65535. **
**-------------------------------------------------------------------------**
** LIMITATIONS **
** The value returned by this function is the current time MOD **
** (approximately) one hour, not the time of day. It is only useful **
** useful for timing events that finish within an hour. **
\***************************************************************************/
tTIME TM_ReadTime ( void )
{
register unsigned char LSB; /* least significant byte */
register unsigned char MSB; /* most significant byte */
char *pFunction_name = /* This function's name, used for */
"TM_ReadTime()"; /* error reporting */
/*---------------------------------------------------------------------*\
** Handle uninitialized module error. **
\*---------------------------------------------------------------------*/
if ( gTM_module_not_initialized )
{
_TM_Error ( pFunction_name, TM_ERR_UNINITIALIZED, NULL );
return( (tTIME) (-1) );
} /* end if */
/*---------------------------------------------------------------------*\
** By writing 0x00 to port 0x43, we are specifying the following **
** command: **
** bits 76 = 00 --> timer 0 **
** bits 54 = 00 --> reading 16-bit value: lsb followed by msb **
** bits 321 = 000 --> ignored **
** bit 0 = 0 --> ignored **
\*---------------------------------------------------------------------*/
outportb ( 0x43, 0x00 );
/*---------------------------------------------------------------------*\
** The following two statements reading from port 0x40 are reading **
** system timer 0's 16-bit counter: MSB after LSB. **
\*---------------------------------------------------------------------*/
LSB = inportb ( 0x40 ); /* get low order byte */
MSB = inportb ( 0x40 ); /* get high order byte */
/*---------------------------------------------------------------------*\
** Pack the time into the 32-bit tTIME return value. **
** (see the preamble at the top of this file for more details) **
\*---------------------------------------------------------------------*/
return( (unsigned long int) (TM_LARGE_TIMER << 16)
| (unsigned int) (0xFFFF-((((unsigned int)MSB)<<8)|LSB)));
} /* end TM_ReadTime() */
/***************************************************************************\
** void TM_StartTimer( tid ) **
*****************************************************************************
** ARGUMENTS **
** unsigned int tid **
** (I) integer label, which is used as an index into the array **
** of timers (gpTM_start_time[]) **
**-------------------------------------------------------------------------**
** RETURNS **
** void **
**-------------------------------------------------------------------------**
** EXAMPLE USAGE **
** #define THIRTYITH_OF_A_SECOND 39759 **
** #define DELAY_TIMER 0 **
** InitializeApplication(); **
** TM_Init(1); **
** . . . **
** TM_StartTimer(DELAY_TIMER); <-----<< **
** MoveMonsters(); **
** MovePlayers(); **
** UpdateDisplay(); **
** while (TM_ElapsedTime(DELAY_TIMER) < THIRTYITH_OF_A_SECOND) **
** ; **
** . . . **
** TM_Close(); **
** ShutDownApplication(); **
**-------------------------------------------------------------------------**
** DETECTABLE ERROR CONDITIONS **
** * Module has not been initialized (TM_Init() not called) **
** * tid is out of range (range = [0..(gTM_max_timers-1)]) **
**-------------------------------------------------------------------------**
** DESCRIPTION **
** This procedure starts a pseudo-timer by reading the current time **
** and storing it in the global array gpTM_start_time[]. **
**-------------------------------------------------------------------------**
** LIMITATIONS **
** None **
\***************************************************************************/
void TM_StartTimer ( tid )
unsigned int tid;
{
char custom_message[256]; /* Buffer for custom error message */
char *pFunction_name = /* This function's name, used for */
"TM_StartTimer()"; /* error reporting */
/*---------------------------------------------------------------------*\
** Handle uninitialized module error. **
\*---------------------------------------------------------------------*/
if (gTM_module_not_initialized)
{
_TM_Error ( pFunction_name, TM_ERR_UNINITIALIZED, NULL );
return;
} /* end if */
if ( tid < gTM_max_timers ) /* then tid is valid */
{
/*-----------------------------------------------------------------*\
** The timer ID is valid, so mark the starting time for this **
** timer (tid) **
\*-----------------------------------------------------------------*/
gpTM_start_time[tid] = TM_ReadTime ( );
}
else /* tid is out of range */
{
/*-----------------------------------------------------------------*\
** Handle the bad timer ID error. **
\*-----------------------------------------------------------------*/
sprintf ( custom_message, "Request received for timer %u, but the "
"last valid timer is timer %u.\n",
tid, gTM_max_timers-1 );
_TM_Error ( pFunction_name, TM_ERR_BAD_TIMER_ID, custom_message );
} /* end if */
} /* end TM_StartTimer() */
/***************************************************************************\
** tTIME TM_ElapsedTime( tid ) **
*****************************************************************************
** ARGUMENTS **
** unsigned int tid **
** (I) integer label, which is used as an index into the array of **
** timers (gpTM_start_time[]) **
**-------------------------------------------------------------------------**
** RETURNS **
** tTIME - the amount of time that has passed since the last call **
** to TM_StartTimer(tid), measured in units of 838ns **
**-------------------------------------------------------------------------**
** EXAMPLE USAGE: **
** #define THIRTYITH_OF_A_SECOND 39759 **
** #define DELAY_TIMER 0 **
** InitializeApplication(); **
** TM_Init(1); **
** . . . **
** TM_StartTimer(DELAY_TIMER); **
** MoveMonsters(); **
** MovePlayers(); **
** UpdateDisplay(); **
** while (TM_ElapsedTime(DELAY_TIMER) < THIRTYITH_OF_A_SECOND) <---<< **
** ; **
** . . . **
** TM_Close(); **
** ShutDownApplication(); **
**-------------------------------------------------------------------------**
** DETECTABLE ERROR CONDITIONS **
** * Module has not been initialized (TM_Init() not called) **
** * tid is out of range (range = [0..(gTM_max_timers-1)]) **
**-------------------------------------------------------------------------**
** DESCRIPTION **
** This function calculates the time that has elapsed for timer slot **
** tid since the last call to TM_StartTimer(tid) by getting the **
** current time and subtracting the starting time (stored in global **
** array gpTM_start_time[].) **
**-------------------------------------------------------------------------**
** LIMITATIONS **
** The largest elapsed time that can accurately be measured is **
** approximately one hour: **
** (tTIME = unsigned long int = 32-bits) **
** (65536(ticks/cycle)*18.2(cycles/sec)*3600 ~= 2^32) **
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -