mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2025-12-18 10:15:19 -08:00
Compare commits
2 Commits
gbowne1-pa
...
gbowne1-ad
| Author | SHA1 | Date | |
|---|---|---|---|
| 574980035e | |||
| a0bd0941d6 |
@@ -1,79 +1,36 @@
|
||||
#include "display.h"
|
||||
#include "io.h"
|
||||
#include "io.h" // Include your I/O header for port access
|
||||
#include "vga.h"
|
||||
|
||||
// Initialize the display
|
||||
void init_display(void) {
|
||||
// Initialize the VGA driver. This typically sets up the 80x25 text mode,
|
||||
// clears the screen, and sets the cursor.
|
||||
vga_init();
|
||||
// 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
|
||||
}
|
||||
|
||||
// Enumerate connected displays
|
||||
void enumerate_displays(void) {
|
||||
// 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.
|
||||
// This is a simplified example. Actual enumeration may require
|
||||
// reading from specific VGA registers or using BIOS interrupts.
|
||||
|
||||
// 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.
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// 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);
|
||||
}
|
||||
// Set the VGA mode by writing to the appropriate registers
|
||||
outb(VGA_PORT, mode); // Example function to write to a port
|
||||
}
|
||||
|
||||
// Clear the display
|
||||
void clear_display(void) {
|
||||
// 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);
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -2,21 +2,13 @@
|
||||
#define DISPLAY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "vga.h" // Include VGA functions
|
||||
|
||||
#define VGA_PORT 0x3C0 // Base port for VGA (Often used for general control, though 0x3D4/0x3D5 are used for cursor)
|
||||
#define VGA_PORT 0x3C0 // Base port for VGA
|
||||
|
||||
// Function prototypes
|
||||
void init_display(void);
|
||||
void enumerate_displays(void);
|
||||
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 set_display_mode(uint8_t mode);
|
||||
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
|
||||
|
||||
119
kernel/pci.c
Normal file
119
kernel/pci.c
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "pci.h"
|
||||
|
||||
/* --- Internal I/O Helpers (If not defined in your kernel) --- */
|
||||
static inline void outl(uint16_t port, uint32_t val) {
|
||||
asm volatile ("outl %0, %1" : : "a"(val), "Nd"(port));
|
||||
}
|
||||
|
||||
static inline uint32_t inl(uint16_t port) {
|
||||
uint32_t ret;
|
||||
asm volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* --- 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
60
kernel/pci.h
Normal 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
|
||||
Reference in New Issue
Block a user