100 Commits

Author SHA1 Message Date
05f5576c9c Create cmos.c 2026-01-28 16:58:08 -08:00
e750049556 Create cmos.h
Add a thing so that we can print CMOS backed clock time to the screen rtc etc
2026-01-28 16:55:04 -08:00
9be6a4e64b Merge pull request #74 from gbowne1/gbowne1-addpci
Adds PCI bus driver implementation
2026-01-28 13:40:37 -08:00
255ac93cae Merge pull request #75 from gbowne1/gbowne1-addps2
Add a PS/2 mouse keyboard driver
2026-01-28 13:38:06 -08:00
Borna Šoštarić
0584f7d49d Merge branch 'main' into gbowne1-addpci 2026-01-28 21:41:59 +01:00
4700e3f11a Update ata.h
Updated header to match ata.c
2026-01-28 21:41:10 +01:00
769f96acbc Update ata.c
Updated ata.c to include fixed BSY status register
2026-01-28 21:41:10 +01:00
1575c0dd12 Create ata.c 2026-01-28 21:41:10 +01:00
37e672a1c2 Create ata.h
Add base ATA PIO mode driver so that filesystems like fat16 fat32 work.
2026-01-28 21:41:10 +01:00
375dd9afdf Update gui.h
Adds gui base header
2026-01-28 21:41:10 +01:00
a42822dd54 Update gui.c
Add base gui implementation
2026-01-28 21:41:10 +01:00
6f0fee4182 Update hid.c
Fixing print_hex error
2026-01-28 21:41:10 +01:00
01f130227b Update keyboard.h
fixed typo in header include
2026-01-28 21:41:10 +01:00
f473fd5c32 Update mouse.h
Fixes static
2026-01-28 21:41:10 +01:00
576f5be8a6 Update mouse.c
Make mouse_data non static
2026-01-28 21:41:10 +01:00
538b97b877 Update keyboard.c
Fix missing definitions so theres nothing that would break the build
2026-01-28 21:41:10 +01:00
4d2eec9e93 Update keyboard.h
fixing missing includes and definition
2026-01-28 21:41:10 +01:00
d30bc6a7f5 Update keyboard.h
Add a extern const for the scancode map
2026-01-28 21:41:10 +01:00
96050d6a99 Update keyboard.c
remove static
2026-01-28 21:41:10 +01:00
e9e3237ba9 Create hid.c
Add bass HID implementation
2026-01-28 21:41:10 +01:00
3c6b2a4e8c Create hid.h
Adding base HID device support for early HID standards 1.0
2026-01-28 21:41:10 +01:00
vmttmv
8db5173495 Fix includes for string.h/string_utils.h 2026-01-28 21:41:10 +01:00
c0220b3afc Update vga.c
Change my_strlen to use our klibc strlen in klibc/include/string.h
2026-01-28 21:41:10 +01:00
f9ef99d11b Update vga.h
Add vga_init(); function prototype
2026-01-28 21:41:10 +01:00
17ce531b62 Update display.c
Added the 95% completely wired up display driver implementation file
2026-01-28 21:41:10 +01:00
4aa6fa3b59 Update display.h
updated header for display driver display.c and display.h this will need to be finished wired up. Old display driver would have done nothing.
2026-01-28 21:41:10 +01:00
Borna Šoštarić
6d78074060 add rules for formatting consecutives 2026-01-28 21:41:10 +01:00
Borna Šoštarić
3fde819c49 add .clang-format 2026-01-28 21:41:10 +01:00
Borna Šoštarić
aac87e30e0 added editorconfig file 2026-01-28 21:41:10 +01:00
Borna Šoštarić
975e21f9be add clangd config file 2026-01-28 21:41:10 +01:00
Borna Šoštarić
db2c17d4b4 add compile_commands.json genereation 2026-01-28 21:41:10 +01:00
vmttmv
c805d24dd0 Fix PDE/PTE definitions, header cleanup
- Fixes PDE/PTE definitions in kernel/paging.h
- removes memset declaration from kernel/utils.h, uses klibc string.h as
needed
2026-01-28 21:41:10 +01:00
vmttmv
b3831b6c69 Remove types.c/.h, use klibc headers, amend stdbool.h, reimplement cpuid() 2026-01-28 21:40:39 +01:00
4e1197356b Fix k_memcmp return logic and add disk_read_sector
Refactor k_memcmp to return correct difference and add disk_read_sector function.
2026-01-28 21:40:39 +01:00
a765fe5238 Fix extern declaration for disk_read_sector function 2026-01-28 21:40:39 +01:00
175c2949be Enhance cpu.h with Intel model definitions and struct
Added Intel model definitions and CPU info structure.
2026-01-28 21:40:39 +01:00
2dd82e69a7 Implement CPUID support check and CPU info printing
Added functions to check CPUID support and print CPU details.
2026-01-28 21:40:39 +01:00
Borna Šoštarić
aeecf2b0b7 fix vga clear section in on_error 2026-01-28 21:40:39 +01:00
vmttmv
091391907f Establish well-defined read buffers for bl, implement error printing 2026-01-28 21:40:39 +01:00
vmttmv
5dcf5d24b2 Fix non-aligned disk reads in bootloader 2026-01-28 21:40:39 +01:00
Borna Šoštarić
a7e536a6d3 fix RWX perms warnings in link step 2026-01-28 21:40:39 +01:00
vmttmv
b99cb27092 fix stage2.asm: disk reads wait for BSY 2026-01-28 21:40:39 +01:00
Borna Šoštarić
a3267d2ac3 initial implementation of klibc
fix linker error about ctx_switch
2026-01-28 21:40:39 +01:00
Borna Šoštarić
bc08701e63 lessen indirection in the makefile 2026-01-28 21:40:39 +01:00
Borna Šoštarić
e8f6beef8a update readme 2026-01-28 21:40:39 +01:00
Borna Šoštarić
bd427dc7e9 generate .build.env as part of configure script 2026-01-28 21:40:39 +01:00
Borna Šoštarić
619430d4bf add configure script for setting up cross compilation tools 2026-01-28 21:40:39 +01:00
Borna Šoštarić
84e28d8871 Merge branch 'main' into gbowne1-addps2 2026-01-28 21:36:14 +01:00
dee91ca211 Update ata.h
Updated header to match ata.c
2026-01-28 21:34:50 +01:00
59dfca9ebd Update ata.c
Updated ata.c to include fixed BSY status register
2026-01-28 21:34:50 +01:00
42f0b62e50 Create ata.c 2026-01-28 21:34:50 +01:00
0a4f0e59e3 Create ata.h
Add base ATA PIO mode driver so that filesystems like fat16 fat32 work.
2026-01-28 21:34:50 +01:00
9d37b7a944 Update gui.h
Adds gui base header
2026-01-28 21:34:50 +01:00
eecc2561da Update gui.c
Add base gui implementation
2026-01-28 21:34:50 +01:00
cae3731f91 Update hid.c
Fixing print_hex error
2026-01-28 21:34:50 +01:00
df33351d60 Update keyboard.h
fixed typo in header include
2026-01-28 21:34:50 +01:00
d2b19ce376 Update mouse.h
Fixes static
2026-01-28 21:34:50 +01:00
8aea3c3c0e Update mouse.c
Make mouse_data non static
2026-01-28 21:34:50 +01:00
f64e40d460 Update keyboard.c
Fix missing definitions so theres nothing that would break the build
2026-01-28 21:34:50 +01:00
45da511bfa Update keyboard.h
fixing missing includes and definition
2026-01-28 21:34:50 +01:00
0f2528e07b Update keyboard.h
Add a extern const for the scancode map
2026-01-28 21:34:50 +01:00
b36356f10c Update keyboard.c
remove static
2026-01-28 21:34:50 +01:00
4b981a5d18 Create hid.c
Add bass HID implementation
2026-01-28 21:34:50 +01:00
fa66433179 Create hid.h
Adding base HID device support for early HID standards 1.0
2026-01-28 21:34:50 +01:00
vmttmv
f08204a2b3 Fix includes for string.h/string_utils.h 2026-01-28 21:34:50 +01:00
bb6f9d1a16 Update vga.c
Change my_strlen to use our klibc strlen in klibc/include/string.h
2026-01-28 21:34:50 +01:00
57ac458a4f Update vga.h
Add vga_init(); function prototype
2026-01-28 21:34:50 +01:00
6ad8644752 Update display.c
Added the 95% completely wired up display driver implementation file
2026-01-28 21:34:50 +01:00
40462e55e6 Update display.h
updated header for display driver display.c and display.h this will need to be finished wired up. Old display driver would have done nothing.
2026-01-28 21:34:50 +01:00
Borna Šoštarić
4d2197aa2d add rules for formatting consecutives 2026-01-28 21:34:50 +01:00
Borna Šoštarić
f7b6a78b59 add .clang-format 2026-01-28 21:34:50 +01:00
Borna Šoštarić
2b32a29890 added editorconfig file 2026-01-28 21:34:50 +01:00
Borna Šoštarić
4ee0ddb9ef add clangd config file 2026-01-28 21:34:50 +01:00
Borna Šoštarić
13b915a99d add compile_commands.json genereation 2026-01-28 21:34:50 +01:00
vmttmv
d480fbcc80 Fix PDE/PTE definitions, header cleanup
- Fixes PDE/PTE definitions in kernel/paging.h
- removes memset declaration from kernel/utils.h, uses klibc string.h as
needed
2026-01-28 21:34:50 +01:00
vmttmv
d0c9c9c4e0 Remove types.c/.h, use klibc headers, amend stdbool.h, reimplement cpuid() 2026-01-28 21:33:36 +01:00
8be984d565 Fix k_memcmp return logic and add disk_read_sector
Refactor k_memcmp to return correct difference and add disk_read_sector function.
2026-01-28 21:33:36 +01:00
4f992c8fc5 Fix extern declaration for disk_read_sector function 2026-01-28 21:33:36 +01:00
16057d41d6 Enhance cpu.h with Intel model definitions and struct
Added Intel model definitions and CPU info structure.
2026-01-28 21:33:36 +01:00
41281de743 Implement CPUID support check and CPU info printing
Added functions to check CPUID support and print CPU details.
2026-01-28 21:33:36 +01:00
Borna Šoštarić
dfb161a15a fix vga clear section in on_error 2026-01-28 21:33:36 +01:00
vmttmv
a450ac06f2 Establish well-defined read buffers for bl, implement error printing 2026-01-28 21:33:36 +01:00
vmttmv
235fd2636d Fix non-aligned disk reads in bootloader 2026-01-28 21:33:36 +01:00
Borna Šoštarić
8c529c6fe4 fix RWX perms warnings in link step 2026-01-28 21:33:36 +01:00
vmttmv
155563e434 fix stage2.asm: disk reads wait for BSY 2026-01-28 21:33:36 +01:00
Borna Šoštarić
8b1ea16c56 initial implementation of klibc
fix linker error about ctx_switch
2026-01-28 21:33:36 +01:00
Borna Šoštarić
7bda5c25b8 lessen indirection in the makefile 2026-01-28 21:33:36 +01:00
Borna Šoštarić
ac0fde28a0 update readme 2026-01-28 21:33:36 +01:00
Borna Šoštarić
a182bbca19 generate .build.env as part of configure script 2026-01-28 21:33:36 +01:00
Borna Šoštarić
c503709ff4 add configure script for setting up cross compilation tools 2026-01-28 21:33:36 +01:00
903061551c Update ps2.c
Remove the definition of the in/out assembly and add io.h include instead of inline
2026-01-27 12:41:17 -08:00
4393fbf6cb Update pci.c
Change this to use io.h to define the in/out assembly instead of doing directly
2026-01-27 12:37:38 -08:00
a37f94de44 Update ps2.c
Fixed the `__volatile__` into a volatile keyword
2026-01-27 08:16:44 -08:00
401a19143c Update pci.c
Fixed volatile to a keyword
2026-01-27 08:10:56 -08:00
cc92ade8fd Update ps2.c
Fixing asm to __asm__
2026-01-26 17:06:44 -08:00
a9f6d5fa05 Update pci.c
Fixed the inline assembly macro asm to __asm__
2026-01-26 16:55:35 -08:00
4e8b13ad77 Create ps2.c
add initial ps/2 driver code. This will need wired up to be used
2025-12-17 05:50:02 -08:00
9f8ca3a60c Create ps2.h 2025-12-17 05:47:59 -08:00
574980035e Create pci.c 2025-12-17 05:32:44 -08:00
a0bd0941d6 Create pci.h 2025-12-17 05:31:15 -08:00
7 changed files with 496 additions and 68 deletions

86
kernel/cmos.c Normal file
View File

@@ -0,0 +1,86 @@
#include "cmos.h"
#include "io.h"
#include "print.h"
#define CMOS_ADDR 0x70
#define CMOS_DATA 0x71
enum {
CMOS_SEC = 0x00,
CMOS_MIN = 0x02,
CMOS_HOUR = 0x04,
CMOS_DAY = 0x07,
CMOS_MONTH= 0x08,
CMOS_YEAR = 0x09,
CMOS_STAT_A = 0x0A,
CMOS_STAT_B = 0x0B
};
// Check if CMOS is currently updating its values
static int is_updating() {
outb(CMOS_ADDR, CMOS_STAT_A);
return (inb(CMOS_DATA) & 0x80);
}
static uint8_t get_register(int reg) {
outb(CMOS_ADDR, reg);
return inb(CMOS_DATA);
}
void cmos_read_time(cmos_time_t* time) {
// Wait for any current update to finish
while (is_updating());
uint8_t sec = get_register(CMOS_SEC);
uint8_t min = get_register(CMOS_MIN);
uint8_t hour = get_register(CMOS_HOUR);
uint8_t day = get_register(CMOS_DAY);
uint8_t month = get_register(CMOS_MONTH);
uint8_t year = get_register(CMOS_YEAR);
uint8_t statb = get_register(CMOS_STAT_B);
// If Bit 2 of Status Register B is 0, then values are BCD
if (!(statb & 0x04)) {
time->second = (sec & 0x0F) + ((sec / 16) * 10);
time->minute = (min & 0x0F) + ((min / 16) * 10);
time->hour = ((hour & 0x0F) + (((hour & 0x70) / 16) * 10)) | (hour & 0x80);
time->day = (day & 0x0F) + ((day / 16) * 10);
time->month = (month & 0x0F) + ((month / 16) * 10);
time->year = (year & 0x0F) + ((year / 16) * 10);
} else {
time->second = sec;
time->minute = min;
time->hour = hour;
time->day = day;
time->month = month;
time->year = year;
}
// Adjust for Century (assuming we are in the 2000s for ClassicOS)
time->year += 2000;
}
void cmos_print_time(cmos_time_t* time) {
// Using your print_string/itoa style logic
char buf[16];
print_string("System Time: ");
// Simple padding check for minutes
print_hex(time->hour, 0, 1);
print_string(":");
if (time->minute < 10) print_string("0");
print_hex(time->minute, 0, 1);
print_string(":");
if (time->second < 10) print_string("0");
print_hex(time->second, 0, 1);
print_string(" ");
print_hex(time->month, 0, 1);
print_string("/");
print_hex(time->day, 0, 1);
print_string("/");
print_hex(time->year, 0, 1);
print_string("\n");
}

18
kernel/cmos.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef CMOS_H
#define CMOS_H
#include <stdint.h>
typedef struct {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
uint32_t year;
} cmos_time_t;
void cmos_read_time(cmos_time_t* time);
void cmos_print_time(cmos_time_t* time);
#endif

109
kernel/pci.c Normal file
View File

@@ -0,0 +1,109 @@
#include "pci.h"
#include "io.h"
/* --- Configuration Access Functions --- */
uint32_t pci_config_read_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
uint32_t address = (uint32_t)((uint32_t)1 << 31) |
((uint32_t)bus << 16) |
((uint32_t)slot << 11) |
((uint32_t)func << 8) |
(offset & 0xFC);
outl(PCI_CONFIG_ADDRESS, address);
return inl(PCI_CONFIG_DATA);
}
void pci_config_write_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t data) {
uint32_t address = (uint32_t)((uint32_t)1 << 31) |
((uint32_t)bus << 16) |
((uint32_t)slot << 11) |
((uint32_t)func << 8) |
(offset & 0xFC);
outl(PCI_CONFIG_ADDRESS, address);
outl(PCI_CONFIG_DATA, data);
}
/* To read a word or byte, we read the Dword and shift/mask */
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
uint32_t dword = pci_config_read_dword(bus, slot, func, offset);
return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF);
}
uint8_t pci_config_read_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
uint32_t dword = pci_config_read_dword(bus, slot, func, offset);
return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF);
}
/* --- BAR Decoding Logic --- */
pci_bar_t pci_get_bar(uint8_t bus, uint8_t slot, uint8_t func, uint8_t bar_index) {
pci_bar_t bar = {0};
uint8_t offset = PCI_REG_BAR0 + (bar_index * 4);
uint32_t initial_val = pci_config_read_dword(bus, slot, func, offset);
// The Size Masking Trick
pci_config_write_dword(bus, slot, func, offset, 0xFFFFFFFF);
uint32_t mask = pci_config_read_dword(bus, slot, func, offset);
pci_config_write_dword(bus, slot, func, offset, initial_val); // Restore
if (initial_val & 0x1) {
// I/O Space BAR
bar.is_io = true;
bar.base_address = initial_val & 0xFFFFFFFC;
bar.size = ~(mask & 0xFFFFFFFC) + 1;
} else {
// Memory Space BAR
bar.is_io = false;
bar.base_address = initial_val & 0xFFFFFFF0;
bar.is_prefetchable = (initial_val & 0x8) != 0;
bar.size = ~(mask & 0xFFFFFFF0) + 1;
}
return bar;
}
/* --- Enumeration and Discovery --- */
void pci_check_function(uint8_t bus, uint8_t slot, uint8_t func) {
uint16_t vendor_id = pci_config_read_word(bus, slot, func, PCI_REG_VENDOR_ID);
if (vendor_id == 0xFFFF) return;
uint16_t device_id = pci_config_read_word(bus, slot, func, PCI_REG_DEVICE_ID);
uint8_t class_code = pci_config_read_byte(bus, slot, func, PCI_REG_CLASS);
/* Optional: Set Master Latency Timer if it is 0.
A value of 32 (0x20) or 64 (0x40) is typical.
*/
uint8_t latency = pci_config_read_byte(bus, slot, func, PCI_REG_LATENCY_TIMER);
if (latency == 0) {
// pci_config_write_byte would be needed here, or write a dword with the byte modified
uint32_t reg_0c = pci_config_read_dword(bus, slot, func, 0x0C);
reg_0c |= (0x20 << 8); // Set latency to 32
pci_config_write_dword(bus, slot, func, 0x0C, reg_0c);
}
// Replace with your kernel's print/logging function
// printf("Found PCI Device: %x:%x Class: %x at %d:%d:%d\n", vendor_id, device_id, class_code, bus, slot, func);
}
void pci_init(void) {
for (uint16_t bus = 0; bus < 256; bus++) {
for (uint8_t slot = 0; slot < 32; slot++) {
// Check Function 0 first
uint16_t vendor = pci_config_read_word(bus, slot, 0, PCI_REG_VENDOR_ID);
if (vendor == 0xFFFF) continue;
pci_check_function(bus, slot, 0);
// Check if this is a multi-function device
uint8_t header_type = pci_config_read_byte(bus, slot, 0, PCI_REG_HEADER_TYPE);
if (header_type & 0x80) {
// Check functions 1-7
for (uint8_t func = 1; func < 8; func++) {
pci_check_function(bus, slot, func);
}
}
}
}
}

60
kernel/pci.h Normal file
View File

@@ -0,0 +1,60 @@
#ifndef PCI_H
#define PCI_H
#include <stdint.h>
#include <stdbool.h>
/* I/O Ports for PCI Configuration Mechanism #1 */
#define PCI_CONFIG_ADDRESS 0xCF8
#define PCI_CONFIG_DATA 0xCFC
/* Common PCI Configuration Register Offsets */
#define PCI_REG_VENDOR_ID 0x00
#define PCI_REG_DEVICE_ID 0x02
#define PCI_REG_COMMAND 0x04
#define PCI_REG_STATUS 0x06
#define PCI_REG_REVISION_ID 0x08
#define PCI_REG_PROG_IF 0x09
#define PCI_REG_SUBCLASS 0x0A
#define PCI_REG_CLASS 0x0B
#define PCI_REG_CACHE_LINE_SIZE 0x0C
#define PCI_REG_LATENCY_TIMER 0x0D
#define PCI_REG_HEADER_TYPE 0x0E
#define PCI_REG_BIST 0x0F
#define PCI_REG_BAR0 0x10
#define PCI_REG_BAR1 0x14
#define PCI_REG_BAR2 0x18
#define PCI_REG_BAR3 0x1C
#define PCI_REG_BAR4 0x20
#define PCI_REG_BAR5 0x24
#define PCI_REG_INTERRUPT_LINE 0x3C
typedef struct {
uint32_t base_address;
uint32_t size;
bool is_io;
bool is_prefetchable; // Only for Memory BARs
} pci_bar_t;
typedef struct {
uint8_t bus;
uint8_t device;
uint8_t function;
uint16_t vendor_id;
uint16_t device_id;
uint8_t class_code;
uint8_t subclass;
uint8_t interrupt_line;
} pci_dev_t;
/* Function Prototypes */
uint32_t pci_config_read_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
void pci_config_write_dword(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t data);
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
uint8_t pci_config_read_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
pci_bar_t pci_get_bar(uint8_t bus, uint8_t slot, uint8_t func, uint8_t bar_index);
void pci_init(void);
#endif

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

@@ -4,8 +4,8 @@
#include "print.h"
#include "threading.h"
#define MAX_THREADS 16 // Maximum number of threads
#define THREAD_STACK_SIZE 8192 // Stack size for each thread
#define MAX_THREADS 16 // Maximum number of threads
#define THREAD_STACK_SIZE 8192 // Stack size for each thread
// The thread table stores information about all threads
static Thread thread_table[MAX_THREADS];
@@ -16,103 +16,106 @@ static uint32_t num_threads = 0; // Number of active threads
static volatile int mutex_locked = 0;
// Function declaration for context_switch
void context_switch(Thread *next);
void context_switch(Thread* next);
// Initialize the threading system
void thread_init(void) {
memset(thread_table, 0, sizeof(thread_table));
num_threads = 0;
memset(thread_table, 0, sizeof(thread_table));
num_threads = 0;
}
// Create a new thread
void thread_create(Thread *thread __attribute__((unused)), void (*start_routine)(void *), void *arg) {
if (num_threads >= MAX_THREADS) {
my_printf("Error: Maximum thread count reached.\n");
return;
}
void thread_create(Thread* thread __attribute__((unused)),
void (*start_routine)(void*), void* arg) {
if (num_threads >= MAX_THREADS) {
my_printf("Error: Maximum thread count reached.\n");
return;
}
// Find an empty slot for the new thread
int index = num_threads++;
thread_table[index] = (Thread){0};
// Set up the new thread
thread_table[index].start_routine = start_routine;
thread_table[index].arg = arg;
thread_table[index].stack_size = 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);
// Find an empty slot for the new thread
int index = num_threads++;
thread_table[index] = (Thread){0};
// Initialize the stack (simulate pushing the function's return address)
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)arg; // Argument to pass to the thread
// Set up the new thread
thread_table[index].start_routine = start_routine;
thread_table[index].arg = arg;
thread_table[index].stack_size = 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);
// Set the thread's state to ready
thread_table[index].state = THREAD_READY;
// Initialize the stack (simulate pushing the function's return address)
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)arg; // Argument to pass to the thread
// If this is the first thread, switch to it
if (index == 0) {
scheduler();
}
// Set the thread's state to ready
thread_table[index].state = THREAD_READY;
// If this is the first thread, switch to it
if (index == 0) {
scheduler();
}
}
// Yield the CPU to another thread
void thread_yield(void) {
// Find the next thread in a round-robin manner
uint32_t next_thread = (current_thread + 1) % num_threads;
while (next_thread != current_thread && thread_table[next_thread].state != THREAD_READY) {
next_thread = (next_thread + 1) % num_threads;
}
// Find the next thread in a round-robin manner
uint32_t next_thread = (current_thread + 1) % num_threads;
while (next_thread != current_thread &&
thread_table[next_thread].state != THREAD_READY) {
next_thread = (next_thread + 1) % num_threads;
}
if (next_thread != current_thread) {
current_thread = next_thread;
scheduler();
}
if (next_thread != current_thread) {
current_thread = next_thread;
scheduler();
}
}
// Exit the current thread
void thread_exit(void) {
thread_table[current_thread].state = THREAD_BLOCKED; // Mark the thread as blocked (finished)
free(thread_table[current_thread].stack); // Free the thread's stack
num_threads--; // Decrease thread count
thread_table[current_thread].state =
THREAD_BLOCKED; // Mark the thread as blocked (finished)
free(thread_table[current_thread].stack); // Free the thread's stack
num_threads--; // Decrease thread count
// Yield to the next thread
thread_yield();
// Yield to the next thread
thread_yield();
}
// Scheduler: This function selects the next thread to run
void scheduler(void) {
// Find the next ready thread
uint32_t next_thread = (current_thread + 1) % num_threads;
while (thread_table[next_thread].state != THREAD_READY) {
next_thread = (next_thread + 1) % num_threads;
}
// Find the next ready thread
uint32_t next_thread = (current_thread + 1) % num_threads;
while (thread_table[next_thread].state != THREAD_READY) {
next_thread = (next_thread + 1) % num_threads;
}
if (next_thread != current_thread) {
current_thread = next_thread;
context_switch(&thread_table[current_thread]);
}
if (next_thread != current_thread) {
current_thread = next_thread;
context_switch(&thread_table[current_thread]);
}
}
// Context switch to the next thread (assembly would go here to save/load registers)
void context_switch(Thread *next) {
// 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");
next->start_routine(next->arg); // Start running the next thread
// Context switch to the next thread (assembly would go here to save/load
// registers)
void context_switch(Thread* next) {
// 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");
next->start_routine(next->arg); // Start running the next thread
}
// Simple mutex functions (spinlock)
void mutex_init(void) {
mutex_locked = 0;
}
void mutex_init(void) { mutex_locked = 0; }
void mutex_lock(void) {
while (__sync_lock_test_and_set(&mutex_locked, 1)) {
// Busy wait (spinlock)
}
while (__sync_lock_test_and_set(&mutex_locked, 1)) {
// Busy wait (spinlock)
}
}
void mutex_unlock(void) {
__sync_lock_release(&mutex_locked);
}
void mutex_unlock(void) { __sync_lock_release(&mutex_locked); }