⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kb.c

📁 用C语言及汇编用的,用于测试键盘的程序,有兴趣地可以
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
   File Name  : KB.C
   Date       : 2005.06.13
   Revsion    : 1.00
   Function   : KB Test and KBC8042 Command
   Compile    : TC3.0
   Author     : Travis Zhao
   Copyright  : Quanta Computer Inc.

   Remark: 1.You can modify the "KB.H" file to different Keyboard and modify the key position in Array for 
             special test1 & special test2. 
           2.Special test is test the KB digital cable
	   3.Special test 2 can cover the unpress issue
	   4.You also need to modify the "KB.H" path when include
*/

#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <graphics.h>
#include "c:\code\KB.h"


//=======================================================
// Test bit
//=======================================================
#define  bit0 0x01
#define	 bit1 0x02
#define  bit7 0x80  //Released key =Pressed key+80H
#define  E0   0xe0  //Keyboard Scan Code = E0 xx example:E0 1c
#define  E1   0xe1  //Keyboard Scan Code = E1 xx xx
//=======================================================
// 8042 access port
//=======================================================
#define Port_60 0x60
#define Port_64 0x64
#define Port_20 0x20 /* End interrupt */
//=======================================================
// 8042 Command
//=======================================================
#define Disable_KB  0x0ad
#define Enable_KB   0x0ae
#define End_INT     0x20
//=======================================================
//Control Flag
//=======================================================
static int QuitFlag=0;
int leadingFlag=0,ext_Num=0;
int leading_ext_Num=0; //E1 Scan Code =E1 1d 45 E1 9d c5, two leading code.
int ESC=0; //init ESC count
int FAIL=0,FailFlag=0; //init FAIL count
			 //if user press 'F' 5 times, set  FailFlag=1.
int curkey=0;    //for special test, it's current key number
int keystu=0;    //for special test, it the sign of E0 key of Stand Key
clock_t t_Start,t_End;
int PressKeyFlag=0;
int RetestFlag=0,RETEST=0;
int s=0; //key switch flag for special test
unsigned int openflag=100; //for special test 2 key open
int sStand[StandMax],sExtE0[ExtE0Max],sExtE1[ExtE1Max]; // Save the Status at 1st.
float var;
int theResult=0; // Pass/Fail(0/1) Flag,  Return a value to DOS
//char far *offsetAddr = (char far*)0x0417; /* address of offsetAddr flags     */

//=======================================================
//   8042 control
//=======================================================

void CLR_8042OBF(void); //Clear 8042 output buffer.
void Wait_8042_InBuf(void); //Wait for 8042 input buffer ready.
void Wait_8042_OutBuf(void); // Wait for 8042 output data to output buffer.
int Read_8042Data(void);  // Read data from 8042 output buffer.
void Write_8042Data(int data);
void Send8042Cmd(int cmd); // Send command to 8042.

//=======================================================
//  Hook KB Interrupt control(INT 09)
//=======================================================
void interrupt (*oldint9)(void);          /* storage for old INT 9 handler */
void Restore_OLD_INT9H(void);
void interrupt Code_Int9H(void);
void interrupt Special_Hook(void);
void interrupt Hook_Int9H(void);
void interrupt Special_Hook2(void);
void TurnOff_Interrupt(void);

//=======================================================
//  Main
//=======================================================
int allkey(void);
void Main_Window(void);
void Draw_Window(void);//int x1,int y1,int x2,int y2);
void SetColor(int front,int back);
int CheckPressed(void);
void Draw_Frame(int left,int top,int right,int down,int front,int back);
void CheckTiming(void);
void help(int cmd);
int getcode(void);    //get kb scan code
int special(void);    //special kb test for kb digital cable
int special2(void);
void KBCreset(void);  //use KBC controller to reset
void KBOFF(int cmd);     //disable KB function
void KBON(int cmd);      //enable KB function
void CMdelay(unsigned long delaytime);
void jumpkey(int key,int stu);
unsigned char ScanCode(void);
int emessage(int code,char *error);

char buffer[20];
char *Read_BIOSVer(int segAddr,int offAddr);

int main(int argc,char *argv[])
{
	int l,use=10,use2=99;
	if(!strcmp(argv[1],"1"))
	{
		l=allkey();
		if(l!=0)
			emessage(l,"All Key Test Error");
		return 1;
	}
	if(!strcmp(argv[1],"2"))
	{
		l=special();
		if(l!=0)
			emessage(l,"Keyboard Special Test 1 Error");
		return l;
	}
	if(!strcmp(argv[1],"3"))
	{
		l=getcode();
		return l;
	}
	if(!strcmp(argv[1],"4"))
	{
		KBCreset();
		return 0;
	}
	if(!strcmp(argv[1],"5") && argc==3)
	{
		if(!strcmp(argv[2],"21"))
		{	KBOFF(21);return 0;}
		if(!strcmp(argv[2],"64"))
		{	KBOFF(64);return 0;}
		return 2;
	}
	if(!strcmp(argv[1],"6") && argc==3)
	{
		if(!strcmp(argv[2],"21"))
		{	KBON(21);return 0;}
		if(!strcmp(argv[2],"64"))
		{	KBON(21);return 0;}
		return 3;
	}
	if(!strcmp(argv[1],"7"))
	{
		l=special2();
		if(l!=0)
			emessage(l,"Keyboard Special Test 2 Error");
		return l;
	}
	help(0);

	while(use!=0)
	{
		scanf("%d",&use);
		switch(use)
		{
			case 1: {textbackground(BLACK);
					 /*for(i=0;i<StandMax;i++)
						StandKey[i].Status=N;
					 for(i=0;i<ExtE0Max;i++)
						ExtE0Key[i].Status=sExtE0[i];
					 for(i=0;i<ExtE1Max;i++)
						ExtE1Key[i].Status=sExtE1[i];*/
					 l=allkey();
					 if(l!=0)
						emessage(l,"All Key Test Error");
					 help(0);return l;}
			case 2: {textbackground(BLACK);l=special();
					 if(l!=0)
						emessage(l,"Keyboard Special 1 Test Error");
					 help(0);return l;}
			case 3: {textbackground(BLACK);getcode();help(0);return l;}
			case 4: {KBCreset();help(0);break;}
			case 5: {printf("Disable Keboard Function\n");help(1);scanf("%d",&use2);
					 while(use2!=9)
					 {
						switch(use2)
						{
							case 21: {KBOFF(21);break;}
							case 64: {KBOFF(64);break;}
							case 9: {use2=9;break;}
							default: break;
						}
					 }help(0);
					break;}
			case 6: {printf("Enable Keboard Function\n");help(1);scanf("%d",&use2);
					 while(use2!=9)
					 {
						switch(use2)
						{
							case 21: {KBON(21);break;}
							case 64: {KBON(64);break;}
							case 9: {use2=9;break;}
							default: break;
						}
					 }help(0);
					break;}
			case 7: {textbackground(BLACK);l=special2();
					 if(l!=0)
						emessage(l,"Keyboard Special Test 2 Error");
					 help(0);return l;}
			case 0: {use=0;break;}
			default: break;
		}
	}
	return 1;
}
//all key function test
int allkey(void)
{
	int i;
	char *ptr,*str[2];
	QuitFlag=0;
	RetestFlag=0;
	//  ptr=Read_BIOSVer(0xf000,0xffc0);
	//  if(strstr(ptr,"TP2")==NULL)  //Check BIOS string "GU1" at addr f000:ffc0
	//   {
	//     printf("This program can't be use on this model\n");
	//     exit(1);
	//   }
	// Save the status from <.h> file
	for(i=0;i<StandMax;i++)
		sStand[i]=StandKey[i].Status;
	for(i=0;i<ExtE0Max;i++)
		sExtE0[i]=ExtE0Key[i].Status;
	for(i=0;i<ExtE1Max;i++)
		sExtE1[i]=ExtE1Key[i].Status;

	Main_Window();


	oldint9 = getvect(9);      /* store old interrupt vector */
	setvect(0x09,Hook_Int9H);   /* set up new interrupt handler */
	while(QuitFlag==0)
	{
		if(RetestFlag==1)
		{
			RetestFlag=0;
			RETEST=0;
			Main_Window();
		}
	}
	if(FailFlag==1)
	   theResult = 1;  // Fail
	CLR_8042OBF();
	Restore_OLD_INT9H();    /* set up exit handler to restore INT 9 */
	SetColor(WHITE,BLACK);
	//delay(1);
	//CLR_8042OBF();
	clrscr();
	return theResult;    //0=Pass , 1=Fail
}
//Keyborad special test for KB digital cable.
int special(void)
{
	int i;
	char *ptr,*str[2];
	QuitFlag=0;
	RetestFlag=0;
	for(i=0;i<StandMax;i++)
		sStand[i]=StandKey[i].Status;
	for(i=0;i<ExtE0Max;i++)
		sExtE0[i]=ExtE0Key[i].Status;
	for(i=0;i<ExtE1Max;i++)
		sExtE1[i]=ExtE1Key[i].Status;

	Main_Window();

  //Change order to fix KB abnormal display 09/18/2003 by Harrison
	oldint9 = getvect(9);      /* store old interrupt vector */
	setvect(0x09,Special_Hook);   /* set up new interrupt handler */
	while(QuitFlag==0)
	{
		if(RetestFlag==1)
		{
			RetestFlag=0;
			RETEST=0;
			Main_Window();
		}
		else
		{
			//keystr=0 is standkey keystr=1 is E0 key
			switch(s)
			{
				case 0: {jumpkey(15,0);break;}
				case 1: break;
				case 2: {jumpkey(0,0);break;}
				case 3: break;
				case 4: {jumpkey(1,0);break;}
				case 5: break;
				case 6: {jumpkey(2,0);break;}
				case 7: break;
				case 8: {jumpkey(3,0);break;}
				case 9: break;
				case 10: {jumpkey(4,0);break;}
				case 11: break;
				case 12: {jumpkey(5,0);break;}
				case 13: break;
				case 14: {jumpkey(6,0);break;}
				case 15: break;
				case 16: {jumpkey(7,0);break;}
				case 17: break;
				case 18: {jumpkey(8,0);break;}
				case 19: break;
				case 20: {jumpkey(9,0);break;}
				case 21: break;
				case 22: {jumpkey(10,0);break;}
				case 23: break;
				case 24: {jumpkey(11,0);break;}
				case 25: break;
				case 26: {jumpkey(12,0);break;}
				case 27: break;
				case 28: {jumpkey(13,0);break;}
				case 29: break;
				case 30: {jumpkey(14,0);break;}
				case 31: break;
				case 32: {jumpkey(42,0);break;}
				case 33: break;
				case 34: {jumpkey(55,0);break;}
				case 35: break;
				case 36: {jumpkey(0,1);break;}
				case 37: break;
				case 38: {jumpkey(56,0);break;}
				case 39: break;
				case 40: {jumpkey(57,0);break;}
				case 41: break;
				case 42: {jumpkey(60,0);break;}
				case 43: break;
				case 44: {jumpkey(58,0);break;}
				case 45: break;
				case 46: {jumpkey(59,0);break;}
				case 47: {break;}
				case 48: {jumpkey(1,1);break;}
				case 49: break;
				case 50: {jumpkey(2,1);break;}
				case 51: break;
				case 52: {jumpkey(3,1);break;}
				case 53: break;
				case 54: {jumpkey(4,1);break;}
				case 55: break;
				case 56: {jumpkey(5,1);break;}
				case 57: break;
				case 58: {jumpkey(6,1);break;}
				case 59: break;
				case 60: {jumpkey(7,1);break;}
				case 61: break;
				case 62: {QuitFlag=1;break;}
				default: break;
			}
		}
	}
	if(FailFlag==1)
	   theResult = 2;  // Fail
	CLR_8042OBF();
	Restore_OLD_INT9H();    /* set up exit handler to restore INT 9 */
	SetColor(WHITE,BLACK);
	//delay(1);
	//CLR_8042OBF();
	clrscr();
	return theResult;    //0=Pass , 1=Fail
}
//For keyboard special 2 test. Cover the key can not jumper error
int special2(void)
{
	int i;
	char *ptr,*str[2];
	QuitFlag=0;
	RetestFlag=0;

	for(i=0;i<StandMax;i++)
		sStand[i]=StandKey[i].Status;
	for(i=0;i<ExtE0Max;i++)
		sExtE0[i]=ExtE0Key[i].Status;
	for(i=0;i<ExtE1Max;i++)
		sExtE1[i]=ExtE1Key[i].Status;

	Main_Window();


	oldint9 = getvect(9);      /* store old interrupt vector */
	setvect(0x09,Special_Hook2);   /* set up new interrupt handler */
	while(QuitFlag==0)
	{
		if(RetestFlag==1)
		{
			RetestFlag=0;
			RETEST=0;
			Main_Window();
		}
	}
	if(FailFlag==1)
	   theResult = 3;  // Fail
	CLR_8042OBF();
	Restore_OLD_INT9H();    /* set up exit handler to restore INT 9 */
	SetColor(WHITE,BLACK);
	//delay(1);
	//CLR_8042OBF();
	clrscr();
	return theResult;    //0=Pass , 1=Fail
}
void Main_Window(void)
{
	int i;
	clrscr();
	//Restore the status for retest .
	for(i=0;i<StandMax;i++)
		StandKey[i].Status=sStand[i];
	for(i=0;i<ExtE0Max;i++)
		ExtE0Key[i].Status=sExtE0[i];
	for(i=0;i<ExtE1Max;i++)
		ExtE1Key[i].Status=sExtE1[i];
	// Main_Window
	Draw_Window();//X1+2,Y1,X1+74,Y1+17); // Main Window
	//Scan Window
	//Draw_Frame(X1+32,Y1+18,X1+64,Y1+20,LIGHTGREEN,BLACK);
	gotoxy(X1+34,Y1+19);printf("[Scan Code: ");
	gotoxy(X1+65,Y1+19);printf("]");
	//Timing Window
	//Draw_Frame(X1+2,Y1+18,X1+31,Y1+20,LIGHTGREEN,BLACK);//Clean up window
	//gotoxy(X1+4,Y1+19);printf("Timing : ");
	SetColor(LIGHTGREEN,BLACK);

	// Copyright window
	SetColor(WHITE,BLACK);
	for(i=0;i<2;i++)
	{
		gotoxy(4,i+2);cprintf("%s",Copyright[i]);
	}
}
void interrupt Special_Hook(void)
{
	//int i=0,f=0;
	unsigned short int keyCode;
	//Draw_Frame(X1+32,Y1+18,X1+64,Y1+20,LIGHTGREEN,BLACK);
	//Re-draw
	gotoxy(X1+34,Y1+19);printf("[Scan Code: ");

	Send8042Cmd(Disable_KB);// ensure keyboard is disable
	disable();
	keyCode=Read_8042Data();
	SetColor(WHITE,BLACK);  //Normal color
	QuitFlag=CheckPressed();
	//For Quit
	if(keyCode==E0)         //Is E0 ?
	{
		leadingFlag=E0;        //Yes, set Flag
		ext_Num=0;             //E0 1c , there is 1 extened code.
		//Draw_Frame(X1+32,Y1+18,X1+64,Y1+20,LIGHTGREEN,BLACK);
		//gotoxy(X1+34,Y1+18);printf("[Scan Code: ");
		//gotoxy(X1+51,Y1+19);printf("             ");
		//gotoxy(X1+46,Y1+19);printf("%x ",keyCode);
		//gotoxy(X1+65,Y1+19);printf("]");
	}
	else if(leadingFlag==E0)
	{
		//gotoxy(X1+34,Y1+18);printf("[Scan Code:                    ");
		//gotoxy(X1+46+(ext_Num+1)*3,Y1+19);
		//printf("%x ",keyCode); //Show scan code
		//gotoxy(X1+65,Y1+19);printf("]");
		if(ext_Num++ != 2)              //E1 1d 45 , there are 2 extened code.
		{
			if((keyCode & bit7) != bit7 ) //Hold down key
			{
				if(keyCode==0x4f) //Press ESC 5 times(0,1,2,3,4) to quit
				{
					QuitFlag=1;
					FailFlag=1;
				}
			}
		}
	}

	// Special KB test for KB digital cable
	//=======================
	//Is leading code = E0 ?
	//=======================
	if(keystu==1)
	{
		if(keyCode==E0)         //Is E0 ?
		{
			leadingFlag=E0;        //Yes, set Flag
			ext_Num=0;             //E0 1c , there is 1 extened code.
		//Draw_Frame(X1+32,Y1+18,X1+64,Y1+20,LIGHTGREEN,BLACK);
		//gotoxy(X1+34,Y1+18);printf("[Scan Code: ");
			gotoxy(X1+51,Y1+19);printf("             ");
			gotoxy(X1+46,Y1+19);printf("%x ",keyCode);
			gotoxy(X1+65,Y1+19);printf("]");
		}
   //=======================
   //Processing E0 code
   //=======================
		else if(leadingFlag==E0)
		{
		//gotoxy(X1+34,Y1+18);printf("[Scan Code:                    ");
			gotoxy(X1+46+(ext_Num+1)*3,Y1+19);
			printf("%x ",keyCode); //Show scan code
			gotoxy(X1+65,Y1+19);printf("]");
			if(ext_Num++ != 2)              //E1 1d 45 , there are 2 extened code.
			{
				if((keyCode & bit7) != bit7 ) //Hold down key
				{
					if(ExtE0Key[curkey].keyCode==keyCode && ExtE0Key[curkey].Status!=PRESSED && ExtE0Key[curkey].Status!=MID)
					{
					// If i=ExtE0Max , can't found the key in struct.
					// If Status=PRESSED, don't show it.
						ExtE0Key[curkey].Status=MID; //Set key status
						SetColor(BLACK,LIGHTGREEN);
						gotoxy(ExtE0Key[curkey].x,ExtE0Key[curkey].y); //Get position
						cprintf("%s",ExtE0Key[curkey].str); // Show key

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -