feat(kernel): implement Phase 2 - IDT64, 8259 PIC, 100Hz PIT timer & PS/2 keyboard driver

This commit is contained in:
Adam Eehan committed 2026-10-23 09:37:54 +05:30
1 parent eec88c14c0
commit 7251770d7f
16 files changed
+863 -19

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
#include <airlys/keyboard.h>
#include <airlys/idt.h>
#include <airlys/io.h>
#include <airlys/vga.h>
#include <airlys/pic.h>
#define KBD_DATA_PORT 0x60
static bool shift_pressed = false;
/* Scan Code Set 1 mapping (lower-case / normal) */
static const char kbd_scancode_normal[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
0, /* Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
0, /* Left Shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
0, /* Right Shift */
'*',
0, /* Alt */
' ', /* Space */
0, /* Caps Lock */
};
/* Scan Code Set 1 mapping (Shifted / upper-case) */
static const char kbd_scancode_shift[128] = {
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b',
'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
0, /* Control */
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '\"', '~',
0, /* Left Shift */
'|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
0, /* Right Shift */
'*',
0, /* Alt */
' ', /* Space */
0, /* Caps Lock */
};
static void keyboard_callback(registers_t* regs) {
(void)regs;
uint8_t scancode = inb(KBD_DATA_PORT);
/* Check for Shift key press (0x2A left, 0x36 right) */
if (scancode == 0x2A || scancode == 0x36) {
shift_pressed = true;
return;
}
/* Check for Shift key release (0xAA left, 0xB6 right) */
if (scancode == 0xAA || scancode == 0xB6) {
shift_pressed = false;
return;
}
/* Key release has bit 7 set (scancode >= 0x80) */
if (scancode & 0x80) {
return;
}
if (scancode < 128) {
char c = shift_pressed ? kbd_scancode_shift[scancode] : kbd_scancode_normal[scancode];
if (c != 0) {
vga_putchar(c);
}
}
}
void keyboard_init(void) {
/* Register keyboard on IRQ1 (Vector 33) */
register_interrupt_handler(33, keyboard_callback);
/* Unmask IRQ1 on Master PIC */
pic_unmask_irq(1);
}
+37 -12
View File
@@ -1,8 +1,12 @@
#include <airlys/kernel.h>
#include <airlys/vga.h>
#include <airlys/idt.h>
#include <airlys/pic.h>
#include <airlys/timer.h>
#include <airlys/keyboard.h>
void kernel_main(uint64_t magic, uint64_t addr) {
/* Initialize hardware VGA terminal console */
/* Step 1: Initialize hardware VGA terminal console */
vga_init();
/* Banner & Sovereign Branding */
@@ -12,14 +16,13 @@ void kernel_main(uint64_t magic, uint64_t addr) {
vga_puts(" / \\ |_ _| _ \\| | \\ \\ / / ___| \n");
vga_puts(" / _ \\ | || |_) | | \\ V /\\___ \\ \n");
vga_puts(" / ___ \\ | || _ <| |___| | ___) |\n");
vga_puts(" /_/ \\_\\___|_| \\_\\_____|_| |____/ SOVEREIGN OS KERNEL v0.1.0\n");
vga_puts(" /_/ \\_\\___|_| \\_\\_____|_| |____/ SOVEREIGN OS KERNEL v0.2.0\n");
vga_puts("================================================================================\n");
vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
vga_puts(" [OK] Airlys Kernel Genesis initialized successfully!\n");
vga_puts(" [OK] Architecture : x86_64 (64-Bit Long Mode)\n");
vga_puts(" [OK] Creator : Er. Adam Eehan (Founder & CEO, Aira Group)\n");
vga_puts(" [OK] Co-Pilot : Sovereign AI Aira 3 Ultra (Engine v3.6)\n");
vga_puts(" [OK] Airlys Kernel Architecture : x86_64 Long Mode (64-Bit)\n");
vga_puts(" [OK] Lead Systems Architect : Er. Adam Eehan (Founder & CEO, Aira Group)\n");
vga_puts(" [OK] Engineering Co-Pilot : Sovereign AI Aira 3 Ultra (Engine v3.6)\n");
vga_set_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
vga_puts("--------------------------------------------------------------------------------\n");
@@ -27,19 +30,41 @@ void kernel_main(uint64_t magic, uint64_t addr) {
vga_set_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK);
vga_puts(" [*] Bootloader Magic : ");
vga_puthex(magic);
vga_puts("\n");
vga_puts(" [*] Multiboot Info : ");
vga_puts(" | Multiboot Info: ");
vga_puthex(addr);
vga_puts("\n");
/* Step 2: Initialize 64-bit Interrupt Descriptor Table (IDT) */
idt_init();
vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK);
vga_puts(" [*] Physical VGA Memory Map: 0x000B8000 (80x25 Text Mode)\n");
vga_puts(" [*] Preemptive Subsystems: Ready for Stage 2 (GDT & IDT Interrupts)\n");
vga_puts(" [OK] CPU IDT Loaded : 256 Gates (Vectors 0..255 active)\n");
/* Step 3: Remap 8259 Dual Programmable Interrupt Controllers */
pic_init();
vga_puts(" [OK] Dual 8259 PIC Remapped : Master IRQ 32..39, Slave IRQ 40..47\n");
/* Step 4: Initialize Programmable Interval Timer (PIT) at 100 Hz */
timer_init(100);
vga_puts(" [OK] Programmable Interval Timer: 100 Hz (10ms preemptive tick interval)\n");
/* Step 5: Initialize PS/2 Keyboard Driver */
keyboard_init();
vga_puts(" [OK] PS/2 Hardware Keyboard : Scan Code Set 1 Driver active\n");
/* Step 6: Enable CPU Hardware Interrupts */
__asm__ volatile ("sti");
vga_set_color(VGA_COLOR_GREEN, VGA_COLOR_BLACK);
vga_puts(" [OK] Hardware Interrupts Active : STI executed (IF=1)\n");
vga_set_color(VGA_COLOR_LIGHT_MAGENTA, VGA_COLOR_BLACK);
vga_puts("\n >>> Airlys System Idle Loop Active. CPU Halted awaiting hardware ticks.\n");
vga_puts("\n ------------------------------------------------------------------------------\n");
vga_puts(" >>> Interactive Console Active. Type on your keyboard to test hardware input:\n");
vga_puts(" ------------------------------------------------------------------------------\n\n");
/* Infinite safe idle loop */
vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK);
vga_puts("airlys> ");
/* Infinite safe idle loop awaiting hardware interrupts */
for (;;) {
__asm__ volatile ("hlt");
}
+77
View File
@@ -0,0 +1,77 @@
#include <airlys/pic.h>
#include <airlys/io.h>
#define ICW1_INIT 0x11
#define ICW4_8086 0x01
void pic_init(void) {
uint8_t a1, a2;
/* Save masks */
a1 = inb(PIC1_DATA);
a2 = inb(PIC2_DATA);
/* Start initialization sequence in cascade mode (ICW1) */
outb(PIC1_COMMAND, ICW1_INIT);
io_wait();
outb(PIC2_COMMAND, ICW1_INIT);
io_wait();
/* ICW2: Master PIC vector offset (32) and Slave PIC vector offset (40) */
outb(PIC1_DATA, PIC1_OFFSET);
io_wait();
outb(PIC2_DATA, PIC2_OFFSET);
io_wait();
/* ICW3: Tell Master PIC there is a Slave PIC at IRQ2 (0000 0100) */
outb(PIC1_DATA, 4);
io_wait();
/* Tell Slave PIC its cascade identity (0000 0010) */
outb(PIC2_DATA, 2);
io_wait();
/* ICW4: Set 8086 mode */
outb(PIC1_DATA, ICW4_8086);
io_wait();
outb(PIC2_DATA, ICW4_8086);
io_wait();
/* Restore masks (or mask all except cascade) */
outb(PIC1_DATA, a1);
outb(PIC2_DATA, a2);
}
void pic_send_eoi(uint8_t irq) {
if (irq >= 8) {
outb(PIC2_COMMAND, PIC_EOI);
}
outb(PIC1_COMMAND, PIC_EOI);
}
void pic_mask_irq(uint8_t irq) {
uint16_t port;
uint8_t value;
if (irq < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
irq -= 8;
}
value = inb(port) | (uint8_t)(1 << irq);
outb(port, value);
}
void pic_unmask_irq(uint8_t irq) {
uint16_t port;
uint8_t value;
if (irq < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
irq -= 8;
}
value = inb(port) & (uint8_t)~(1 << irq);
outb(port, value);
}
+45
View File
@@ -0,0 +1,45 @@
#include <airlys/timer.h>
#include <airlys/idt.h>
#include <airlys/io.h>
#include <airlys/pic.h>
#define PIT_CHANNEL0 0x40
#define PIT_COMMAND 0x43
#define PIT_BASE_FREQ 1193182
static volatile uint64_t timer_ticks = 0;
static void timer_callback(registers_t* regs) {
(void)regs;
timer_ticks++;
}
void timer_init(uint32_t frequency_hz) {
if (frequency_hz == 0) frequency_hz = 100;
uint32_t divisor = PIT_BASE_FREQ / frequency_hz;
if (divisor == 0) divisor = 1;
if (divisor > 65535) divisor = 65535;
/* Register callback on IRQ0 (Vector 32) */
register_interrupt_handler(32, timer_callback);
/* 0x36 = Channel 0, lobyte/hibyte, Mode 3 (Square wave generator), binary 16-bit */
outb(PIT_COMMAND, 0x36);
outb(PIT_CHANNEL0, (uint8_t)(divisor & 0xFF));
outb(PIT_CHANNEL0, (uint8_t)((divisor >> 8) & 0xFF));
/* Unmask IRQ0 on Master PIC */
pic_unmask_irq(0);
}
uint64_t timer_get_ticks(void) {
return timer_ticks;
}
void timer_sleep(uint32_t ticks) {
uint64_t start = timer_ticks;
while ((timer_ticks - start) < ticks) {
__asm__ volatile ("hlt");
}
}