23 Commits

Author SHA1 Message Date
95372a66e6 Create pic.c
Implementation for pic.h
2026-01-28 10:47:35 -08:00
e376526426 Create pic.h 2026-01-28 10:40:41 -08:00
f572101d6b Merge pull request #102 from gbowne1/gbowne1-patch-5
Create ata.h
2026-01-27 08:53:21 -08:00
3cd2ff6e1e Merge pull request #97 from gbowne1/gbowne1-patch-2
Update gui.c
2026-01-27 07:45:58 -08:00
84705fd225 Merge pull request #99 from gbowne1/gbowne1-add-hid
Add HID support
2026-01-26 12:18:04 -08:00
6c69b5fd6a Update hid.c
Fixing print_hex error
2026-01-25 08:45:45 -08:00
1037ba4f54 Update keyboard.h
fixed typo in header include
2026-01-25 08:24:43 -08:00
745deeddde Update mouse.h
Fixes static
2026-01-24 22:49:07 -08:00
f9e281a7ae Update mouse.c
Make mouse_data non static
2026-01-24 22:46:03 -08:00
18801a742f Update keyboard.c
Fix missing definitions so theres nothing that would break the build
2026-01-24 22:31:50 -08:00
a08648eff5 Update keyboard.h
fixing missing includes and definition
2026-01-24 22:21:44 -08:00
5a664c6e31 Update keyboard.h
Add a extern const for the scancode map
2026-01-24 22:14:37 -08:00
4c7de228f9 Update keyboard.c
remove static
2026-01-24 22:13:32 -08:00
af3f20485f Merge pull request #71 from gbowne1/gbowne1-patch-1
Update display.h
2026-01-20 00:18:22 -08:00
cca6aafd65 Create hid.c
Add bass HID implementation
2026-01-18 16:21:45 -08:00
49c1bad935 Create hid.h
Adding base HID device support for early HID standards 1.0
2026-01-18 16:20:43 -08:00
7adb798c17 Update gui.h
Adds gui base header
2026-01-18 15:43:03 -08:00
43b5cacd09 Update gui.c
Add base gui implementation
2026-01-18 15:40:23 -08:00
vmttmv
19ef0a8627 Fix includes for string.h/string_utils.h 2026-01-16 20:49:06 +02:00
c5f7e4e563 Update vga.c
Change my_strlen to use our klibc strlen in klibc/include/string.h
2026-01-16 01:19:01 -08:00
f516334e0d Update vga.h
Add vga_init(); function prototype
2026-01-15 17:00:37 -08:00
4047bc3936 Update display.c
Added the 95% completely wired up display driver implementation file
2025-11-26 16:02:07 -08:00
7e54f0de66 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.
2025-11-26 15:53:58 -08:00
14 changed files with 459 additions and 60 deletions

View File

@@ -1,36 +1,80 @@
#include <string.h>
#include "display.h"
#include "io.h" // Include your I/O header for port access
#include "io.h"
#include "vga.h"
// Initialize the display
void init_display(void) {
// Initialize VGA settings, if necessary
// This could involve setting up the VGA mode, etc.
set_display_mode(0x13); // Example: Set to 320x200 256-color mode
// Initialize the VGA driver. This typically sets up the 80x25 text mode,
// clears the screen, and sets the cursor.
vga_init();
}
// Enumerate connected displays
void enumerate_displays(void) {
// This is a simplified example. Actual enumeration may require
// reading from specific VGA registers or using BIOS interrupts.
// This function is often a complex operation in a real driver.
// In this simplified kernel/VGA text mode environment, we use printf
// to output a message and rely on the fact that VGA is present.
// For demonstration, we will just print a message
// In a real driver, you would check the VGA registers
// to determine connected displays.
clear_display();
// Here you would typically read from VGA registers to find connected displays
// For example, using inb() to read from VGA ports
// Clear the display before printing a message
vga_clear(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
// Output a simplified enumeration message
vga_printf("Display: Standard VGA Text Mode (80x25) Detected.\n");
// In a real driver, you would use inb() and outb() with specific VGA ports
// to read information (e.g., from the CRTC registers 0x3D4/0x3D5)
// to check for display presence or configuration.
}
// Set the display mode
// NOTE: Setting arbitrary VGA modes (like 0x13 for 320x200) is very complex
// and requires writing hundreds of register values, often done via BIOS in
// real mode. Since we are in protected mode and have a simple text driver,
// this function is kept simple or treated as a placeholder for full mode changes.
void set_display_mode(uint8_t mode) {
// Set the VGA mode by writing to the appropriate registers
// Check if the requested mode is a known mode (e.g., VGA Text Mode 3)
// For this example, we simply acknowledge the call.
// A true mode set would involve complex register sequencing.
// The provided vga.c is a Text Mode driver, so a graphical mode set
// like 0x13 (320x200 256-color) would break the existing vga_printf functionality.
// A simplified text-mode-specific response:
if (mode == 0x03) { // Mode 3 is standard 80x25 text mode
vga_printf("Display mode set to 80x25 Text Mode (Mode 0x03).\n");
vga_init(); // Re-initialize the text mode
} else {
// Simple I/O example based on the original structure (Caution: Incomplete for full mode set)
outb(VGA_PORT, mode); // Example function to write to a port
vga_printf("Attempting to set display mode to 0x%x. (Warning: May break current display)\n", mode);
}
}
// Clear the display
void clear_display(void) {
// Clear the display by filling it with a color
// This is a placeholder for actual clearing logic
// You would typically write to video memory here
// Use the VGA driver's clear function, typically clearing to black on light grey
// or black on black. We'll use the black on light grey from vga_init for consistency.
vga_clear(vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_GREY));
// Reset cursor to 0, 0
vga_set_cursor_position(0, 0);
}
// Helper function to write a string
void display_write_string(const char* str) {
// Use the VGA driver's string writing function
vga_write_string(str, strlen(str));
}
// Helper function to print a formatted string
void display_printf(const char* format, ...) {
// Use the VGA driver's printf function
va_list args;
va_start(args, format);
// The vga_printf function already handles the va_list internally,
// so we can just call it directly.
vga_printf(format, args);
va_end(args);
}

View File

@@ -2,13 +2,21 @@
#define DISPLAY_H
#include <stdint.h>
#include "vga.h" // Include VGA functions
#define VGA_PORT 0x3C0 // Base port for VGA
#define VGA_PORT 0x3C0 // Base port for VGA (Often used for general control, though 0x3D4/0x3D5 are used for cursor)
// Function prototypes
void init_display(void);
void enumerate_displays(void);
void set_display_mode(uint8_t mode);
void set_display_mode(uint8_t mode); // In this context, modes are typically BIOS or VESA modes, which are complex.
// We'll treat this as a placeholder/simple mode call.
void clear_display(void);
// New function to write a string using the VGA driver
void display_write_string(const char* str);
// New function to print a formatted string using the VGA driver
void display_printf(const char* format, ...);
#endif // DISPLAY_H

View File

@@ -0,0 +1,66 @@
#include "gui.h"
#include "vga.h" // VGA functions for drawing and clearing screen
#include "framebuffer.h" // For pixel manipulation if needed
// Initialize the GUI (could set up any global state or variables here)
void gui_init(void) {
// Clear the screen with black or any color
gui_clear(vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_WHITE));
}
// Draw a window (simple rectangle with a title)
void gui_draw_window(gui_window_t* window) {
// Draw the window's border
for (uint32_t y = 0; y < window->height; ++y) {
for (uint32_t x = 0; x < window->width; ++x) {
// Check if we are at the border
if (x == 0 || y == 0 || x == window->width - 1 || y == window->height - 1) {
vga_put_entry_at('#', vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK), window->x + x, window->y + y);
} else {
// Fill the inside of the window
vga_put_entry_at(' ', vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_BLACK), window->x + x, window->y + y);
}
}
}
// Draw the title at the top
if (window->title) {
size_t i = 0;
while (window->title[i] != '\0' && i < window->width - 2) {
vga_put_entry_at(window->title[i], vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK), window->x + i + 1, window->y);
i++;
}
}
}
// Draw a button (a simple rectangle with text in the middle)
void gui_draw_button(gui_button_t* button) {
for (uint32_t y = 0; y < button->height; ++y) {
for (uint32_t x = 0; x < button->width; ++x) {
// Check if we are at the border
if (x == 0 || y == 0 || x == button->width - 1 || y == button->height - 1) {
vga_put_entry_at('#', vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK), button->x + x, button->y + y);
} else {
// Fill the inside of the button
vga_put_entry_at(' ', vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_BLACK), button->x + x, button->y + y);
}
}
}
// Draw the label in the center of the button
size_t label_len = 0;
while (button->label[label_len] != '\0') {
label_len++;
}
size_t start_x = button->x + (button->width - label_len) / 2;
size_t start_y = button->y + (button->height - 1) / 2;
for (size_t i = 0; i < label_len; ++i) {
vga_put_entry_at(button->label[i], vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK), start_x + i, start_y);
}
}
// Clear the screen with a color
void gui_clear(uint32_t color) {
vga_clear(color); // Just clear the VGA screen with a solid color
}

View File

@@ -0,0 +1,34 @@
#ifndef GUI_H
#define GUI_H
#include <stdint.h>
#include <stddef.h>
#define GUI_WINDOW_WIDTH 80
#define GUI_WINDOW_HEIGHT 25
#define GUI_BUTTON_WIDTH 10
#define GUI_BUTTON_HEIGHT 3
// Window structure
typedef struct {
uint32_t x, y;
uint32_t width, height;
uint32_t color; // Background color
const char* title;
} gui_window_t;
// Button structure
typedef struct {
uint32_t x, y;
uint32_t width, height;
uint32_t color; // Background color
const char* label;
} gui_button_t;
// Function prototypes for GUI elements
void gui_init(void);
void gui_draw_window(gui_window_t* window);
void gui_draw_button(gui_button_t* button);
void gui_clear(uint32_t color);
#endif // GUI_H

65
kernel/hid.c Normal file
View File

@@ -0,0 +1,65 @@
#include "hid.h"
#include "usb.h"
#include "mouse.h"
#include "keyboard.h"
#include "print.h"
#include <stdint.h>
#include <stdbool.h>
// Global variables
static bool hid_initialized = false;
void hid_init(void) {
if (hid_initialized) return;
hid_initialized = true;
// Initialize keyboard and mouse HID handling
keyboard_init();
// Assume USB mouse has been initialized and is connected.
usb_hid_init(); // Initializes USB HID for both keyboard and mouse
}
void hid_process_report(uint8_t* report, uint8_t length) {
// Process the HID report based on its type
if (length == 8) { // Assuming a standard 8-byte report for HID keyboard
keyboard_hid_report_t* k_report = (keyboard_hid_report_t*) report;
hid_process_keyboard_report(k_report);
} else if (length == 3) { // Assuming a standard 3-byte report for HID mouse
mouse_hid_report_t* m_report = (mouse_hid_report_t*) report;
hid_process_mouse_report(m_report);
}
}
// Handle HID keyboard report
void hid_process_keyboard_report(const keyboard_hid_report_t* report) {
// Iterate over the keycodes and process key presses
for (int i = 0; i < 6; i++) {
uint8_t keycode = report->keycodes[i];
if (keycode != 0) {
char key = scancode_map[keycode];
if (key) {
keyboard_buffer_add(key);
}
}
}
}
// Handle HID mouse report
void hid_process_mouse_report(const mouse_hid_report_t* report) {
// Process mouse movement and button clicks
mouse_data.x += report->x;
mouse_data.y += report->y;
mouse_data.left_button = (report->buttons & 0x01) != 0;
mouse_data.right_button = (report->buttons & 0x02) != 0;
print_hex((uint32_t)mouse_data.x, 1, 1);
print_hex((uint32_t)mouse_data.y, 1, 1);
print_hex((uint32_t)report->buttons, 1, 1);
}
// Parse the HID descriptor (for parsing USB HID device descriptors)
bool hid_parse_descriptor(uint8_t* descriptor, uint32_t length) {
// HID descriptors are defined in the USB HID specification, we'll need to parse them here.
// For now, just return true assuming we have a valid descriptor.
return true;
}

46
kernel/hid.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef HID_H
#define HID_H
#include <stdint.h>
#include <stdbool.h>
// HID Report types
#define HID_REPORT_INPUT 0x01
#define HID_REPORT_OUTPUT 0x02
#define HID_REPORT_FEATURE 0x03
// HID usage page constants (USB HID)
#define HID_USAGE_PAGE_GENERIC 0x01
#define HID_USAGE_KEYBOARD 0x06
#define HID_USAGE_MOUSE 0x02
// HID keyboard and mouse data
typedef struct {
uint8_t modifier; // Modifier keys (shift, ctrl, alt, etc.)
uint8_t reserved; // Reserved byte
uint8_t keycodes[6]; // Keycodes for keys pressed
} keyboard_hid_report_t;
typedef struct {
uint8_t buttons; // Mouse buttons (bitwise: 0x01 = left, 0x02 = right, 0x04 = middle)
int8_t x; // X axis movement
int8_t y; // Y axis movement
int8_t wheel; // Mouse wheel
} mouse_hid_report_t;
// Initialize the HID subsystem
void hid_init(void);
// Process an incoming HID report
void hid_process_report(uint8_t* report, uint8_t length);
// Process HID keyboard report
void hid_process_keyboard_report(const keyboard_hid_report_t* report);
// Process HID mouse report
void hid_process_mouse_report(const mouse_hid_report_t* report);
// USB HID report descriptor parsing
bool hid_parse_descriptor(uint8_t* descriptor, uint32_t length);
#endif // HID_H

View File

@@ -2,64 +2,91 @@
#include "io.h"
#include "isr.h"
#include "terminal.h"
#include <stddef.h>
#define KEYBOARD_DATA_PORT 0x60
#define KEY_BUFFER_SIZE 256
static char key_buffer[KEY_BUFFER_SIZE];
static uint8_t buffer_head = 0; // Write position (interrupt)
static uint8_t buffer_tail = 0; // Read position (get_char)
static uint8_t buffer_count = 0;
static uint8_t buffer_index = 0;
// Use volatile so the compiler knows these change inside interrupts
static volatile char key_buffer[KEY_BUFFER_SIZE];
static volatile uint8_t buffer_head = 0;
static volatile uint8_t buffer_tail = 0;
static volatile uint8_t buffer_count = 0;
// Basic US QWERTY keymap (scancode to ASCII)
static const char scancode_map[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', // 0x00 - 0x09
'9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', // 0x0A - 0x13
't', 'y', 'z', 'u', 'i', 'o', 'p', '[', ']', '\n', // 0x14 - 0x1D
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', // 0x1E - 0x27
';', '\'', '`', 0, '\\', 'x', 'c', 'v', 'b', // 0x28 - 0x31
'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0, // 0x32 - 0x3B
// rest can be filled as needed
// Exported map: Removed 'static' so hid.c can reference it if needed
const char scancode_map[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,
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
'\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n',
'm', ',', '.', '/', 0, '*', 0, ' ', 0
};
// Interrupt handler for IRQ1
void keyboard_callback(void) {
uint8_t scancode = inb(KEYBOARD_DATA_PORT);
if (scancode & 0x80) return; // Ignore key release
char c = scancode_map[scancode];
/**
* Shared function used by both PS/2 (callback) and USB (hid.c)
* This fixes the "undefined reference to keyboard_buffer_add" error.
*/
void keyboard_buffer_add(char c) {
if (!c) return;
uint8_t next_head = (buffer_head + 1) % KEY_BUFFER_SIZE;
// Drop key if buffer full
if (next_head == buffer_tail) return;
// If buffer is full, we must drop the key
if (next_head == buffer_tail) {
return;
}
key_buffer[buffer_head] = c;
buffer_head = next_head;
buffer_count++;
// Echo to terminal
terminal_putchar(c);
}
void keyboard_init() {
register_interrupt_handler(33, keyboard_callback); // IRQ1 = int 33 (0x21)
/**
* Hardware Interrupt Handler for PS/2
*/
void keyboard_callback(void) {
uint8_t scancode = inb(KEYBOARD_DATA_PORT);
// Ignore break codes (key release)
if (scancode & 0x80) return;
char c = scancode_map[scancode];
keyboard_buffer_add(c);
}
// Blocking read (returns one char)
char keyboard_get_char(void) {
while (buffer_count == 0) {
__asm__ __volatile__("hlt"); // Better than busy loop
}
void keyboard_init(void) {
buffer_head = 0;
buffer_tail = 0;
buffer_count = 0;
// IRQ1 is usually mapped to IDT entry 33
register_interrupt_handler(33, keyboard_callback);
}
/**
* Blocking read with a safe HLT to prevent CPU 100% usage
*/
char keyboard_get_char(void) {
char c;
__asm__ __volatile__("cli");
while (1) {
__asm__ __volatile__("cli"); // Disable interrupts to check buffer_count safely
if (buffer_count > 0) {
c = key_buffer[buffer_tail];
buffer_tail = (buffer_tail + 1) % KEY_BUFFER_SIZE;
buffer_count--;
__asm__ __volatile__("sti");
__asm__ __volatile__("sti"); // Re-enable interrupts after reading
return c;
}
/* * IMPORTANT: 'sti' followed by 'hlt' is guaranteed by x86
* to execute 'hlt' BEFORE the next interrupt can trigger.
* This prevents the race condition hang.
*/
__asm__ __volatile__("sti; hlt");
}
}

View File

@@ -1,7 +1,12 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <stdint.h>
void keyboard_init(void);
char keyboard_get_char(void); // Blocking read from buffer
void keyboard_buffer_add(char c);
char keyboard_get_char(void);
extern const char scancode_map[128];
#endif

View File

@@ -5,7 +5,7 @@
#include <stdbool.h>
// Mouse buffer
static mouse_data_t mouse_data;
mouse_data_t mouse_data;
// Read USB mouse data
mouse_data_t usb_read_mouse(void) {

View File

@@ -12,6 +12,8 @@ typedef struct {
bool right_button;
} mouse_data_t;
extern mouse_data_t mouse_data;
// Function declarations for USB 1.x HID mouse support
bool usb_mouse_init(void);
bool usb_mouse_detected(void);

25
kernel/pic.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef PIC_H
#define PIC_H
#include <stdint.h>
/* I/O Ports for the PICs */
#define PIC1_COMMAND 0x20
#define PIC1_DATA 0x21
#define PIC2_COMMAND 0xA0
#define PIC2_DATA 0xA1
/* PIC Commands */
#define PIC_EOI 0x20 /* End of Interrupt */
/* Offset vectors for remapping */
#define PIC1_OFFSET 0x20
#define PIC2_OFFSET 0x28
void pic_init(void);
void pic_send_eoi(uint8_t irq);
void pic_mask(uint8_t irq);
void pic_unmask(uint8_t irq);
void pic_disable(void);
#endif

View File

@@ -1,9 +1,9 @@
#include "vga.h"
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include "string_utils.h"
#include "vga.h"
void outb(uint16_t port, uint8_t value) {
__asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port));
@@ -134,7 +134,7 @@ void vga_printf(const char* format, ...) {
va_end(args);
// Now you can use the buffer with vga_write_string
vga_write_string(buffer, my_strlen(buffer)); // Use my_strlen instead of strlen
vga_write_string(buffer, strlen(buffer)); // Use my_strlen instead of strlen
}
void vga_init(void) {

View File

@@ -35,6 +35,7 @@ typedef enum {
// Function prototypes
uint8_t vga_entry_color(vga_color fg, vga_color bg);
uint16_t vga_entry(unsigned char uc, uint8_t color);
void vga_init(void);
void vga_put_entry_at(char c, uint8_t color, size_t x, size_t y);
void vga_clear(uint8_t color);

76
pic.c Normal file
View File

@@ -0,0 +1,76 @@
#include "pic.h"
#include "io.h"
/* Small delay for older hardware bus timing */
static inline void io_wait(void) {
outb(0x80, 0);
}
void pic_init(void) {
uint8_t a1, a2;
// Save current masks
a1 = inb(PIC1_DATA);
a2 = inb(PIC2_DATA);
// ICW1: Start initialization in cascade mode
outb(PIC1_COMMAND, 0x11);
io_wait();
outb(PIC2_COMMAND, 0x11);
io_wait();
// ICW2: Master PIC vector offset
outb(PIC1_DATA, PIC1_OFFSET);
io_wait();
// ICW2: Slave PIC vector offset
outb(PIC2_DATA, PIC2_OFFSET);
io_wait();
// ICW3: Tell Master there is a slave at IRQ2 (0000 0100)
outb(PIC1_DATA, 4);
io_wait();
// ICW3: Tell Slave its cascade identity (0000 0010)
outb(PIC2_DATA, 2);
io_wait();
// ICW4: Set 8086/88 mode
outb(PIC1_DATA, 0x01);
io_wait();
outb(PIC2_DATA, 0x01);
io_wait();
// Restore masks (or disable all to start clean)
outb(PIC1_DATA, 0xFB); // Keep IRQ2 (cascade) open
outb(PIC2_DATA, 0xFF);
}
void pic_send_eoi(uint8_t irq) {
if (irq >= 8) {
outb(PIC2_COMMAND, PIC_EOI);
}
outb(PIC1_COMMAND, PIC_EOI);
}
void pic_unmask(uint8_t irq) {
uint16_t port;
if (irq < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
irq -= 8;
}
uint8_t value = inb(port) & ~(1 << irq);
outb(port, value);
}
void pic_mask(uint8_t irq) {
uint16_t port;
if (irq < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
irq -= 8;
}
uint8_t value = inb(port) | (1 << irq);
outb(port, value);
}