Merge pull request #75 from gbowne1/gbowne1-addps2

Add a PS/2 mouse keyboard driver
This commit is contained in:
2026-01-28 13:38:06 -08:00
committed by GitHub
3 changed files with 223 additions and 68 deletions

107
kernel/ps2.c Normal file
View File

@@ -0,0 +1,107 @@
#include "ps2.h"
#include "io.h"
/* --- Controller Synchronization --- */
// Wait until the controller is ready to receive a byte
static void ps2_wait_write() {
while (inb(PS2_STATUS_REG) & PS2_STATUS_INPUT);
}
// Wait until the controller has a byte for us to read
static void ps2_wait_read() {
while (!(inb(PS2_STATUS_REG) & PS2_STATUS_OUTPUT));
}
/* --- Initialization --- */
void ps2_write_device(uint8_t command) {
ps2_wait_write();
outb(PS2_DATA_PORT, command);
}
void ps2_write_mouse(uint8_t data) {
ps2_wait_write();
outb(PS2_COMMAND_REG, PS2_CMD_WRITE_MOUSE); // "Next byte goes to mouse"
ps2_wait_write();
outb(PS2_DATA_PORT, data);
}
void ps2_init(void) {
// 1. Disable Devices
ps2_wait_write();
outb(PS2_COMMAND_REG, PS2_CMD_DISABLE_KB);
ps2_wait_write();
outb(PS2_COMMAND_REG, PS2_CMD_DISABLE_MS);
// 2. Flush Output Buffer
while (inb(PS2_STATUS_REG) & PS2_STATUS_OUTPUT) {
inb(PS2_DATA_PORT);
}
// 3. Set Controller Configuration Byte
// Bit 0: KB Interrupt, Bit 1: Mouse Interrupt, Bit 6: Translation
ps2_wait_write();
outb(PS2_COMMAND_REG, PS2_CMD_READ_CONFIG);
ps2_wait_read();
uint8_t status = inb(PS2_DATA_PORT);
status |= (1 << 0) | (1 << 1); // Enable IRQ 1 and IRQ 12
ps2_wait_write();
outb(PS2_COMMAND_REG, PS2_CMD_WRITE_CONFIG);
ps2_wait_write();
outb(PS2_DATA_PORT, status);
// 4. Enable Devices
ps2_wait_write();
outb(PS2_COMMAND_REG, PS2_CMD_ENABLE_KB);
ps2_wait_write();
outb(PS2_COMMAND_REG, PS2_CMD_ENABLE_MS);
// 5. Initialize Mouse (The mouse won't send IRQs until you tell it to)
ps2_write_mouse(MOUSE_CMD_SET_DEFAULTS);
ps2_wait_read(); inb(PS2_DATA_PORT); // Read ACK (0xFA)
ps2_write_mouse(MOUSE_CMD_ENABLE_SCAN);
ps2_wait_read(); inb(PS2_DATA_PORT); // Read ACK (0xFA)
}
/* --- IRQ Handlers --- */
// Called from IRQ 1 (Keyboard)
void ps2_keyboard_handler(void) {
uint8_t scancode = inb(PS2_DATA_PORT);
// Process scancode (e.g., put it into a circular buffer)
}
// Called from IRQ 12 (Mouse)
static uint8_t mouse_cycle = 0;
static uint8_t mouse_bytes[3];
void ps2_mouse_handler(void) {
uint8_t status = inb(PS2_STATUS_REG);
// Ensure this is actually mouse data
if (!(status & PS2_STATUS_MOUSE)) return;
mouse_bytes[mouse_cycle++] = inb(PS2_DATA_PORT);
if (mouse_cycle == 3) {
mouse_cycle = 0;
// Byte 0: Flags (Buttons, Signs)
// Byte 1: X Delta
// Byte 2: Y Delta
mouse_state_t state;
state.left_button = (mouse_bytes[0] & 0x01);
state.right_button = (mouse_bytes[0] & 0x02);
state.middle_button = (mouse_bytes[0] & 0x04);
// Handle negative deltas (signed 9-bit logic)
state.x_delta = (int8_t)mouse_bytes[1];
state.y_delta = (int8_t)mouse_bytes[2];
// Update your kernel's internal mouse position here
}
}

45
kernel/ps2.h Normal file
View File

@@ -0,0 +1,45 @@
#ifndef PS2_H
#define PS2_H
#include <stdint.h>
#include <stdbool.h>
/* I/O Ports */
#define PS2_DATA_PORT 0x60
#define PS2_STATUS_REG 0x64
#define PS2_COMMAND_REG 0x64
/* Status Register Bits */
#define PS2_STATUS_OUTPUT 0x01 // 1 = Data ready to be read
#define PS2_STATUS_INPUT 0x02 // 1 = Controller busy, don't write yet
#define PS2_STATUS_SYS 0x04 // System flag
#define PS2_STATUS_CMD_DATA 0x08 // 0 = Data written to 0x60, 1 = Cmd to 0x64
#define PS2_STATUS_MOUSE 0x20 // 1 = Mouse data, 0 = Keyboard data
/* Controller Commands */
#define PS2_CMD_READ_CONFIG 0x20
#define PS2_CMD_WRITE_CONFIG 0x60
#define PS2_CMD_DISABLE_MS 0xA7
#define PS2_CMD_ENABLE_MS 0xA8
#define PS2_CMD_DISABLE_KB 0xAD
#define PS2_CMD_ENABLE_KB 0xAE
#define PS2_CMD_WRITE_MOUSE 0xD4
/* Mouse Commands */
#define MOUSE_CMD_SET_DEFAULTS 0xF6
#define MOUSE_CMD_ENABLE_SCAN 0xF4
typedef struct {
int8_t x_delta;
int8_t y_delta;
bool left_button;
bool right_button;
bool middle_button;
} mouse_state_t;
/* Public API */
void ps2_init(void);
void ps2_keyboard_handler(void);
void ps2_mouse_handler(void);
#endif

View File

@@ -16,7 +16,7 @@ static uint32_t num_threads = 0; // Number of active threads
static volatile int mutex_locked = 0; static volatile int mutex_locked = 0;
// Function declaration for context_switch // Function declaration for context_switch
void context_switch(Thread *next); void context_switch(Thread* next);
// Initialize the threading system // Initialize the threading system
void thread_init(void) { void thread_init(void) {
@@ -25,7 +25,8 @@ void thread_init(void) {
} }
// Create a new thread // Create a new thread
void thread_create(Thread *thread __attribute__((unused)), void (*start_routine)(void *), void *arg) { void thread_create(Thread* thread __attribute__((unused)),
void (*start_routine)(void*), void* arg) {
if (num_threads >= MAX_THREADS) { if (num_threads >= MAX_THREADS) {
my_printf("Error: Maximum thread count reached.\n"); my_printf("Error: Maximum thread count reached.\n");
return; return;
@@ -40,11 +41,13 @@ void thread_create(Thread *thread __attribute__((unused)), void (*start_routine)
thread_table[index].arg = arg; thread_table[index].arg = arg;
thread_table[index].stack_size = THREAD_STACK_SIZE; thread_table[index].stack_size = THREAD_STACK_SIZE;
thread_table[index].stack = (uint32_t*)malloc(THREAD_STACK_SIZE); thread_table[index].stack = (uint32_t*)malloc(THREAD_STACK_SIZE);
thread_table[index].stack_top = thread_table[index].stack + THREAD_STACK_SIZE / sizeof(uint32_t); thread_table[index].stack_top =
thread_table[index].stack + THREAD_STACK_SIZE / sizeof(uint32_t);
// Initialize the stack (simulate pushing the function's return address) // Initialize the stack (simulate pushing the function's return address)
uint32_t *stack_top = thread_table[index].stack_top; uint32_t* stack_top = thread_table[index].stack_top;
*(--stack_top) = (uint32_t)start_routine; // Return address (the thread's entry point) *(--stack_top) =
(uint32_t)start_routine; // Return address (the thread's entry point)
*(--stack_top) = (uint32_t)arg; // Argument to pass to the thread *(--stack_top) = (uint32_t)arg; // Argument to pass to the thread
// Set the thread's state to ready // Set the thread's state to ready
@@ -60,7 +63,8 @@ void thread_create(Thread *thread __attribute__((unused)), void (*start_routine)
void thread_yield(void) { void thread_yield(void) {
// Find the next thread in a round-robin manner // Find the next thread in a round-robin manner
uint32_t next_thread = (current_thread + 1) % num_threads; uint32_t next_thread = (current_thread + 1) % num_threads;
while (next_thread != current_thread && thread_table[next_thread].state != THREAD_READY) { while (next_thread != current_thread &&
thread_table[next_thread].state != THREAD_READY) {
next_thread = (next_thread + 1) % num_threads; next_thread = (next_thread + 1) % num_threads;
} }
@@ -72,7 +76,8 @@ void thread_yield(void) {
// Exit the current thread // Exit the current thread
void thread_exit(void) { void thread_exit(void) {
thread_table[current_thread].state = THREAD_BLOCKED; // Mark the thread as blocked (finished) thread_table[current_thread].state =
THREAD_BLOCKED; // Mark the thread as blocked (finished)
free(thread_table[current_thread].stack); // Free the thread's stack free(thread_table[current_thread].stack); // Free the thread's stack
num_threads--; // Decrease thread count num_threads--; // Decrease thread count
@@ -94,18 +99,18 @@ void scheduler(void) {
} }
} }
// Context switch to the next thread (assembly would go here to save/load registers) // Context switch to the next thread (assembly would go here to save/load
void context_switch(Thread *next) { // registers)
// For simplicity, context switching in this example would involve saving/restoring registers. void context_switch(Thread* next) {
// In a real system, you would need to save the CPU state (registers) and restore the next thread's state. // For simplicity, context switching in this example would involve
// saving/restoring registers. In a real system, you would need to save the
// CPU state (registers) and restore the next thread's state.
my_printf("Switching to thread...\n"); my_printf("Switching to thread...\n");
next->start_routine(next->arg); // Start running the next thread next->start_routine(next->arg); // Start running the next thread
} }
// Simple mutex functions (spinlock) // Simple mutex functions (spinlock)
void mutex_init(void) { void mutex_init(void) { mutex_locked = 0; }
mutex_locked = 0;
}
void mutex_lock(void) { void mutex_lock(void) {
while (__sync_lock_test_and_set(&mutex_locked, 1)) { while (__sync_lock_test_and_set(&mutex_locked, 1)) {
@@ -113,6 +118,4 @@ void mutex_lock(void) {
} }
} }
void mutex_unlock(void) { void mutex_unlock(void) { __sync_lock_release(&mutex_locked); }
__sync_lock_release(&mutex_locked);
}