Ps/2 Compatible Mouse Driver 90%

int old_buttons = 0; while (1) if (mouse_buttons != old_buttons) printf("Buttons: %s %s %s\n", (mouse_buttons & 1) ? "Left" : " ", (mouse_buttons & 2) ? "Right" : " ", (mouse_buttons & 4) ? "Middle" : " "); old_buttons = mouse_buttons; if (mouse_x Enabling Scroll Wheel (IntelliMouse) Send this sequence to switch to 4-byte packets:

void install_mouse_handler() set_idt_gate(0x2C, (uint32_t)mouse_isr, 0x08, 0x8E); outb(0x21, inb(0x21) & ~0x20); // Unmask IRQ12 on slave PIC outb(0xA1, inb(0xA1) & ~0x20); ps/2 compatible mouse driver

// Set controller configuration byte outb(0x64, 0x20); // Read command byte uint8_t config = inb(0x60); config |= 0x02; // Enable mouse IRQ12 (bit 1) config &= ~0x10; // Enable standard translation (optional) outb(0x64, 0x60); // Write command byte outb(0x60, config); int old_buttons = 0; while (1) if (mouse_buttons

// Enable devices again outb(0x64, 0xAE); // Enable keyboard outb(0x64, 0xA8); // Enable mouse PS/2 commands are sent via port 0x64 , then data via 0x60 . The mouse acknowledges each command with 0xFA . Helper function: mouse_send_command int mouse_send_command(uint8_t cmd) // Wait until input buffer is empty while (inb(0x64) & 2); outb(0x64, 0xD4); // Tell controller next byte is for mouse while (inb(0x64) & 2); outb(0x60, cmd); // Wait for ACK uint8_t ack = 0; int timeout = 100000; while (timeout-- && !(inb(0x64) & 1)); if (timeout <= 0) return -1; ack = inb(0x60); return (ack == 0xFA) ? 0 : -1; Enabling the Mouse mouse_send_command(0xF4); // Enable data reporting Optional: Set sample rate, resolution, scaling. 5. Interrupt Handling The mouse fires IRQ12 every time a byte is ready. We must read 3 bytes, then assemble the packet. ISR (Interrupt Service Routine) skeleton: volatile uint8_t mouse_cycle = 0; volatile uint8_t mouse_packet[4]; // We use 3 for standard volatile int mouse_x = 0, mouse_y = 0; volatile uint8_t mouse_buttons = 0; void mouse_isr() uint8_t status = inb(0x64); if (!(status & 1)) return; // No data "Middle" : " "); old_buttons = mouse_buttons; if

if (mouse_cycle == 0) // Byte 0: verify bit 3 = 1 (always set in standard packet) if ((data & 0x08) == 0x08) mouse_packet[0] = data; mouse_cycle = 1; else if (mouse_cycle == 1) mouse_packet[1] = data; mouse_cycle = 2; else if (mouse_cycle == 2) = 0xFFFFFF00; if (mouse_packet[0] & 0x20) dy

Next steps: Add absolute positioning (tablet mode), dynamic rate switching, or hook the driver into a windowing system. Happy coding! Did this article help you? Share your PS/2 driver war stories in the comments below.

The PS/2 mouse might seem like a relic of the 1990s, but it remains the gold standard for low-level OS development. Unlike USB, which relies on complex host controllers and descriptor parsing, the PS/2 interface is simple, memory-mapped, and interrupt-driven. In this article, we’ll build a bare-bones PS/2 mouse driver from scratch, covering initialization, packet decoding, and integration with a simple GUI. 1. Understanding the PS/2 Interface The PS/2 port uses two bidirectional lines: Clock (usually IRQ 12 for the mouse) and Data . Communication is synchronous, with the device sending 11-bit packets (1 start bit, 8 data bits, 1 parity bit, 1 stop bit) when the host pulls the clock low.