voodoo programming on PS/2 mouse and keyboard

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
KoizumiChineko
Posts: 4
Joined: Mon Sep 07, 2020 8:32 pm
Libera.chat IRC: Chineko

voodoo programming on PS/2 mouse and keyboard

Post by KoizumiChineko »

At the very beginning I point that my code is working after I swapped mouse initialization with keyboard initialization, and I have no idea why it did not work in first place, I read wiki and couple of related topic and found no answer, but I suspect that might be a problem with mouse and keyboard initialization. Second question is: why mouse is sending second data packet as first?

Currently mouse starts first, then keyboard.
My code:

Code: Select all

void InitMouse()
{	
	MouseStruct.mouse_data[0] = 0;
	MouseStruct.mouse_data[1] = 0;
	MouseStruct.mouse_data[2] = 0;
	MouseStruct.mouse_X = 0;
	MouseStruct.mouse_Y = 0;
	MouseStruct.mouse_data_itterator = 1;
	
	WaitFor0x60_0x64Write();
	
	MS_CP = 0xA8;
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&MS_CP);
	
	WaitFor0x60_0x64Write();
	
	MS_CP = 0x20;
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&MS_CP);
		
	WaitFor0x60Read();
	
	readPort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_DP, (void*)&MouseStruct.status);
	MouseStruct.status |=2;
		
		WaitFor0x60_0x64Write();
	
	MS_CP = 0x60;
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&MS_CP);
	
		WaitFor0x60_0x64Write();
	
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_DP, (void*)&MouseStruct.status);
	
		WaitFor0x60_0x64Write();
	
	MS_CP = 0xD4;
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&MS_CP);
	
		WaitFor0x60_0x64Write();
		
	MS_DP = 0xF6;
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_DP, (void*)&MS_DP);
	
		WaitFor0x60_0x64Write();

	MS_CP = 0xD4;
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&MS_CP);
	
		WaitFor0x60_0x64Write();
		
	MS_DP = 0xF4;
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_DP, (void*)&MS_DP);
	
		WaitFor0x60Read();
		
		printf("Mouse ready\n");
}

Code: Select all

void InitKeyboard()
{	
		readPort(PORT_MODE_8_SLOW, HEX_PORT_NUMBER_KBMS_CP, (void*)&KeyboardStruct.status);
		
			WaitFor0x60_0x64Write();
		
		KB_CP = 0xAE;
		writePort(PORT_MODE_8_SLOW, HEX_PORT_NUMBER_KBMS_CP, (void*)&KB_CP); 
			
			WaitFor0x60_0x64Write();
		
		KB_CP = 0x20;
		writePort(PORT_MODE_8_SLOW, HEX_PORT_NUMBER_KBMS_CP, (void*)&KB_CP);  
	
			WaitFor0x60Read();
		
		readPort(PORT_MODE_8_SLOW, HEX_PORT_NUMBER_KBMS_DP, (void*)&KeyboardStruct.status);
		KeyboardStruct.status = (KeyboardStruct.status | 1) & ~0x10;
		
			WaitFor0x60_0x64Write();
			
		KB_CP = 0x60;
		writePort(PORT_MODE_8_SLOW, HEX_PORT_NUMBER_KBMS_CP, (void*)&KB_CP);  
		
			WaitFor0x60_0x64Write();
		
		writePort(PORT_MODE_8_SLOW, HEX_PORT_NUMBER_KBMS_DP, (void*)&KeyboardStruct.status);
		
			WaitFor0x60_0x64Write();
		
		KB_DP = 0xF4;
		writePort(PORT_MODE_8_SLOW, HEX_PORT_NUMBER_KBMS_DP, (void*)&KB_DP);
		printf("Keyboard ready.\n");
}
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: voodoo programming on PS/2 mouse and keyboard

Post by Octocontrabass »

When your mouse driver accesses the PS/2 controller, it affects the keyboard driver. When your keyboard driver accesses the PS/2 controller, it affects the mouse.

What you need is a PS/2 controller driver. Your PS/2 controller driver should be responsible for initializing the PS/2 controller and providing functions the keyboard and mouse drivers can use to send and receive data.
User avatar
KoizumiChineko
Posts: 4
Joined: Mon Sep 07, 2020 8:32 pm
Libera.chat IRC: Chineko

Re: voodoo programming on PS/2 mouse and keyboard

Post by KoizumiChineko »

I wrote this, and it seems to work.

Code: Select all

void InitPS2()
{
	PS2Struct.status = 0b01000111; //Controller Configuration Byte;
	
		WaitFor0x60_0x64Write();
	PS2_CP = 0xAE; //Enable first PS2 port
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&PS2_CP);
	
		WaitFor0x60_0x64Write();
	PS2_CP = 0xA8; //Enable second PS2 port
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&PS2_CP);
		
		WaitFor0x60_0x64Write();
	PS2_CP = 0x60; //Write next byte as CCB
	writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_CP, (void*)&PS2_CP);
	
		WaitFor0x60_0x64Write(); //Send CCB to controller
	 writePort(PORT_MODE_8, HEX_PORT_NUMBER_KBMS_DP, (void*)&PS2Struct.status);
}
My previous approach did something weird with CCB and that's why it sometimes worked sometimes not, but now i have one more question. What's port translation (6th bit of CCB)?
Post Reply