2 Commits

Author SHA1 Message Date
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
4 changed files with 70 additions and 181 deletions

View File

@@ -1,36 +1,79 @@
#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
outb(VGA_PORT, mode); // Example function to write to a port
// 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, my_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

@@ -1,117 +0,0 @@
#include "ps2.h"
/* --- Low Level I/O Helpers --- */
static inline void outb(uint16_t port, uint8_t val) {
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint8_t inb(uint16_t port) {
uint8_t ret;
asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
/* --- 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
}
}

View File

@@ -1,45 +0,0 @@
#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