?? pc.c
字號:
OS_ENTER_CRITICAL();
data = (INT8U)inp(0x61); /* Disable the timer */
data &= 0xFE;
outp(0x61, data);
outp(TICK_T0_8254_CWR, TICK_T0_8254_CTR2_LATCH); /* Latch the timer value */
low = inp(TICK_T0_8254_CTR2);
high = inp(TICK_T0_8254_CTR2);
cnts = (INT16U)0xFFFF - (((INT16U)high << 8) + (INT16U)low); /* Compute time it took for operation */
OS_EXIT_CRITICAL();
return ((INT16U)((INT32U)cnts * 54926L >> 16) - PC_ElapsedOverhead);
}
/*$PAGE*/
/*
*********************************************************************************************************
* GET THE CURRENT DATE AND TIME
*
* Description: This function obtains the current date and time from the PC.調(diào)用PC_GetDateTime()函數(shù),可得到PC中的當(dāng)前日期
和時間,并且以ASCII碼的形式返回,格式是"YYYY-MM-DD HH:MM:SS"。至少需要21個字符(包括NULL字符)存放這些數(shù)據(jù)。注意:日期數(shù)據(jù)
和時間數(shù)據(jù)有2個空格字符,因此需要21個字符(包括NULL字符)存放這些數(shù)據(jù)。該函數(shù)使用了Borland C的庫函數(shù)gettime()和getdate(),
其他DOS環(huán)境下的C編譯器也應(yīng)該有類似的函數(shù)。
*
* Arguments : s is a pointer to where the ASCII string of the current date and time will be stored.
* You must allocate at least 21 bytes (includes the NUL) of storage in the return
* string. The date and time will be formatted as follows:
*
* "YYYY-MM-DD HH:MM:SS"
*
* Returns : none
*********************************************************************************************************
*/
void PC_GetDateTime (char *s)
{
struct time now;
struct date today;
gettime(&now);
getdate(&today);
sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
today.da_year,
today.da_mon,
today.da_day,
now.ti_hour,
now.ti_min,
now.ti_sec);
}
/*$PAGE*/
/*
*********************************************************************************************************
* CHECK AND GET KEYBOARD KEY
*
* Description: This function checks to see if a key has been pressed at the keyboard and returns TRUE if
* so. Also, if a key is pressed, the key is read and copied where the argument is pointing
* to.
PC_GetKey()函數(shù)檢查鍵盤是否有鍵被按下。如果有,就得到按鍵值并返回。該函數(shù)調(diào)用了Borland C的庫函數(shù)kbhit()和getch(),其他DOS
環(huán)境下的C編譯器也同樣有類似函數(shù)。
*
* Arguments : c is a pointer to where the read key will be stored.
*
* Returns : TRUE if a key was pressed
* FALSE otherwise
key 指向按鍵值存放地址的指針。如果沒有按鍵被放下,則該指針值為0x0000.
如果按鍵被按下,則返回TRUE;否則返回FALSE
*********************************************************************************************************
*/
BOOLEAN PC_GetKey (INT16S *c)
{
if (kbhit()) { /* See if a key has been pressed */
*c = (INT16S)getch(); /* Get key pressed */
return (TRUE);
} else {
*c = 0x00; /* No key pressed */
return (FALSE);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* SET THE PC'S TICK FREQUENCY
*
* Description: This function is called to change the tick rate of a PC.
PC_SetTickRate()允許通過指定所需要的頻率,改變時鐘節(jié)拍的頻率值。在DOS下,時鐘節(jié)拍產(chǎn)生18.206 48次/秒,或每隔54.925ms產(chǎn)生一
次。這是因?yàn)?2C54芯片的計(jì)數(shù)器沒有被初始化,而是使用默認(rèn)值65 535.如果芯片被初始化為59 659,那么時鐘節(jié)拍就會是精確的20.000Hz
筆者決定將時鐘節(jié)拍設(shè)置的更快一些,用的是大約200Hz(實(shí)際為199.9966HZ)。OS_CPU_A.ASM文件中的代碼將會每11個時鐘節(jié)拍調(diào)用1次
DOS的時鐘節(jié)拍處理程序,這是為了保證DOS以前,須調(diào)用PC_SetTickRate()函數(shù),并將頻率設(shè)為18Hz。
*
* Arguments : freq is the desired frequency of the ticker (in Hz)
*
* Returns : none
*
* Notes : 1) The magic number 2386360 is actually twice the input frequency of the 8254 chip which
* is always 1.193180 MHz.
* 2) The equation computes the counts needed to load into the 8254. The strange equation
* is actually used to round the number using integer arithmetic. This is equivalent to
* the floating point equation:
*
* 1193180.0 Hz
* count = ------------ + 0.5
* freq
*********************************************************************************************************
*/
void PC_SetTickRate (INT16U freq)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT16U count;
if (freq == 18) { /* See if we need to restore the DOS frequency */
count = 0;
} else if (freq > 0) {
/* Compute 8254 counts for desired frequency and ... */
/* ... round to nearest count */
count = (INT16U)(((INT32U)2386360L / freq + 1) >> 1);
} else {
count = 0;
}
OS_ENTER_CRITICAL();
outp(TICK_T0_8254_CWR, TICK_T0_8254_CTR0_MODE3); /* Load the 8254 with desired frequency */
outp(TICK_T0_8254_CTR0, count & 0xFF); /* Low byte */
outp(TICK_T0_8254_CTR0, (count >> 8) & 0xFF); /* High byte */
OS_EXIT_CRITICAL();
}
/*$PAGE*/
/*
*********************************************************************************************************
* OBTAIN INTERRUPT VECTOR
PC_VectGet()和PC_VectSet()這2個函數(shù)應(yīng)該是與編譯器無關(guān)的,是需要編譯器支持一下3個宏即可
MK_FP()定義長指針
FP_OFF()得到一個長指針的偏移量
FP_SEG()得到一個長指針的段地址。
*
* Description: This function reads the pointer stored at the specified vector.
這個函數(shù)用于獲取由中斷向量值vect指定的中斷服務(wù)子程序的地址。80x86處理器可支持多達(dá)256個中斷/異常處理程序
*
* Arguments : vect is the desired interrupt vector number, a number between 0 and 255.
vect 中斷向量值,是一個介于0~255的數(shù)值
*
* Returns : The address of the Interrupt handler stored at the desired vector location.
由中斷向量值指定的中斷/異常處理程序的地址
注意:中斷向量值指定的中斷/異常處理程序
假定80x86的代碼在編譯時選擇大模式,即所有的返回指針都是長指針。
假定80x86運(yùn)行在實(shí)時模式下
*********************************************************************************************************
*/
void *PC_VectGet (INT8U vect)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT16U *pvect;
INT16U off;
INT16U seg;
pvect = (INT16U *)MK_FP(0x0000, vect * 4); /* Point into IVT at desired vector location */
OS_ENTER_CRITICAL();
off = *pvect++; /* Obtain the vector's OFFSET */
seg = *pvect; /* Obtain the vector's SEGMENT */
OS_EXIT_CRITICAL();
return (MK_FP(seg, off));
}
/*
*********************************************************************************************************
* INSTALL INTERRUPT VECTOR
*
* Description: This function sets an interrupt vector in the interrupt vector table.
用于設(shè)定中斷向量表的內(nèi)容。80x86處理器可支持多達(dá)256個中斷/異常處理程序
*
* Arguments : vect is the desired interrupt vector number, a number between 0 and 255.
中斷向量值,是一個介于0~255的數(shù)值
* isr is a pointer to a function to execute when the interrupt or exception occurs.
中斷/異常處理程序的地址
*
* Returns : none
注意:設(shè)定中斷向量時必須小心,因?yàn)橛袑懼袛嘞蛄渴潜徊僮飨到y(tǒng)(DOS和/或UCOS_II)使用的
假定80x86的代碼在編譯時選擇大模式,即所有的返回指針都是長指針。
*********************************************************************************************************
*/
void PC_VectSet (INT8U vect, void (*isr)(void))
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT16U *pvect;
pvect = (INT16U *)MK_FP(0x0000, vect * 4); /* Point into IVT at desired vector location */
OS_ENTER_CRITICAL();
*pvect++ = (INT16U)FP_OFF(isr); /* Store ISR offset */
*pvect = (INT16U)FP_SEG(isr); /* Store ISR segment */
OS_EXIT_CRITICAL();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -