📄 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.调用PC_GetDateTime()函数,可得到PC中的当前日期
和时间,并且以ASCII码的形式返回,格式是"YYYY-MM-DD HH:MM:SS"。至少需要21个字符(包括NULL字符)存放这些数据。注意:日期数据
和时间数据有2个空格字符,因此需要21个字符(包括NULL字符)存放这些数据。该函数使用了Borland C的库函数gettime()和getdate(),
其他DOS环境下的C编译器也应该有类似的函数。
*
* 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()函数检查键盘是否有键被按下。如果有,就得到按键值并返回。该函数调用了Borland C的库函数kbhit()和getch(),其他DOS
环境下的C编译器也同样有类似函数。
*
* 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()允许通过指定所需要的频率,改变时钟节拍的频率值。在DOS下,时钟节拍产生18.206 48次/秒,或每隔54.925ms产生一
次。这是因为82C54芯片的计数器没有被初始化,而是使用默认值65 535.如果芯片被初始化为59 659,那么时钟节拍就会是精确的20.000Hz
笔者决定将时钟节拍设置的更快一些,用的是大约200Hz(实际为199.9966HZ)。OS_CPU_A.ASM文件中的代码将会每11个时钟节拍调用1次
DOS的时钟节拍处理程序,这是为了保证DOS以前,须调用PC_SetTickRate()函数,并将频率设为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个函数应该是与编译器无关的,是需要编译器支持一下3个宏即可
MK_FP()定义长指针
FP_OFF()得到一个长指针的偏移量
FP_SEG()得到一个长指针的段地址。
*
* Description: This function reads the pointer stored at the specified vector.
这个函数用于获取由中断向量值vect指定的中断服务子程序的地址。80x86处理器可支持多达256个中断/异常处理程序
*
* Arguments : vect is the desired interrupt vector number, a number between 0 and 255.
vect 中断向量值,是一个介于0~255的数值
*
* Returns : The address of the Interrupt handler stored at the desired vector location.
由中断向量值指定的中断/异常处理程序的地址
注意:中断向量值指定的中断/异常处理程序
假定80x86的代码在编译时选择大模式,即所有的返回指针都是长指针。
假定80x86运行在实时模式下
*********************************************************************************************************
*/
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.
用于设定中断向量表的内容。80x86处理器可支持多达256个中断/异常处理程序
*
* Arguments : vect is the desired interrupt vector number, a number between 0 and 255.
中断向量值,是一个介于0~255的数值
* isr is a pointer to a function to execute when the interrupt or exception occurs.
中断/异常处理程序的地址
*
* Returns : none
注意:设定中断向量时必须小心,因为有写中断向量是被操作系统(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();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -