Code: Select all
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
enum PS2_CONTROLLER
{
PS2_DATA_PORT = 0x60,
PS2_STATUS_REGISTER = 0x64,
PS2_COMMAND_REGISTER = 0x64,
// status register
PS2_OUTPUT_BUFFER_STATUS = 0b1, // Must be set before reading 0x60 (0=empty, 1=full)
PS2_INPUT_BUFFER_STATUS = 0b10, // must be clear before writing 0x60 (0=empty, 1=full)
//READ_BYTE = 0x20; // response is Controller Configuration Byte
//WRITE_BYTE = 0x60;
// command port
PS2_READ_CONTROLLER_OUTPUT = 0xd0,
PS2_WRITE_CONTROLLER_OUTPUT = 0xd1, // check if output buffer is empty first
DISABLE_PS2_0 = 0xa7,
ENABLE_PS2_0 = 0xa8,
TEST_PS2_0 = 0xa9,
TEST_PS2_CONTROLLER = 0xaa,
TEST_PS2_1 = 0xaB,
DISABLE_PS2_1 = 0xad,
ENABLE_PS2_1 = 0xae,
// device commands ? keyboard ==> data port
PS2_IDENTIFY_DEVICE = 0xf2,
PS2_ENABLE_SCANNING = 0xf4, // 0xfa=ACK, 0xfe=RESEND
PS2_DISABLE_SCANNING = 0xf5,
PS2_RESET_SELF_TEST = 0xff // 0xaa=pass
};
void kmain()
{
print_char('C', 25, 79,0);
//disable interrupts
outb( PS2_COMMAND_REGISTER, 0x20);
wait();
uint8_t y = inb(0x60);
outb( PS2_COMMAND_REGISTER,0x60);
wait();
outb( PS2_DATA_PORT, (y & 0b10111100) ); // disable interrups
wait();
outb( PS2_COMMAND_REGISTER, ENABLE_PS2_0 );
wait();
outb( PS2_COMMAND_REGISTER, ENABLE_PS2_1 );
wait();
outb( PS2_DATA_PORT, 0xf6 ); // set default params
wait();
outb( PS2_DATA_PORT, PS2_ENABLE_SCANNING );
wait();
outb( PS2_DATA_PORT, PS2_IDENTIFY_DEVICE );
wait();
print_byte(inb(PS2_DATA_PORT),20,0,1);
wait();
print_byte(inb(PS2_DATA_PORT),20,0,2);
// see if key codes are being sent on port 0x60
uint8_t volatile value;
while (1)
{
wait();
value = inb(0x60);
print_byte(value, 25,0,0);
wait();
}
asm("cli");
while (1) { asm("hlt"); }
}