GitFOSS
c1ad0fc5/1/2026, 8:41:11 PM
~kernel/ps2.c
.c
C
(text/x-csrc)
#include <stdint.h>
#include "module.h"
#include "pmm.h"

#define KBD_DATA 0x60
#define KBD_STATUS 0x64

static inline uint8_t inb(uint16_t port) {
    uint8_t ret;
    asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
    return ret;
}

/* Existing function to read scancode (used by IRQ handler) */
int kbd_read_scancode(void) {
    if (inb(KBD_STATUS) & 1) {
        return inb(KBD_DATA);
    }
    return 0;
}

/* IRQ handler for keyboard (IRQ 1) */
void keyboard_irq_handler(void) {
    int sc = kbd_read_scancode();
    if (sc) {
        extern void vga_write(const char*);
        char buf[32];
        int n = 0;
        if (sc == 0x1C) {
            vga_write("Enter\n");
        } else {
            buf[n++] = '0' + ((sc/10)%10);
            buf[n++] = '0' + (sc%10);
            buf[n++] = '\n';
            buf[n] = 0;
            vga_write(buf);
        }
    }
}

/* Initialization function to register the keyboard IRQ handler */
void ps2_init(struct kernel_api *api) {
    if (api && api->register_irq) {
        api->register_irq(1, keyboard_irq_handler); /* IRQ 1 = keyboard */
    }
}