X-OS includes a comprehensive set of hardware drivers that provide abstraction between the kernel and physical devices.
The driver subsystem provides:
- Keyboard input: PS/2 keyboard with multiple layouts
- Timer services: System timing and delays
- Floppy disk I/O: Storage device access
- Video output: VGA text and VESA graphics
- Interrupt management: Hardware interrupt handling
- Modular design: Each driver is self-contained
- Interrupt-driven: Asynchronous I/O where possible
- Hardware abstraction: Hide device-specific details
- Error handling: Robust error detection and recovery
32b/drivers/
├── kbd.c # Keyboard driver
├── timer.c # System timer
├── floppystable.c # Floppy drive parameters
└── block/
└── floppy.c # Floppy disk controller
- Scancode processing: Raw keyboard input handling
- Multiple keymaps: French and English layouts
- Special keys: Caps Lock, Num Lock, Scroll Lock
- Key state tracking: Modifier key combinations
char getSC(void) // Get scancode from keyboard
char key(int index) // Convert scancode to ASCII
void set_keymap(char *map) // Change keyboard layout
short iscaps(void) // Check Caps Lock state
short isnum(void) // Check Num Lock stateThe keyboard driver uses IRQ1 for interrupt-driven input:
void irq_kbd(void)
{
// Read scancode from port 0x60
// Process special keys (Caps, Num, Scroll Lock)
// Wake up waiting processes
// Send EOI to interrupt controller
}Supports multiple keyboard layouts:
- AZERTY key arrangement
- French-specific characters (é, è, à, etc.)
- Proper accent key handling
- QWERTY key arrangement
- Standard ASCII character mapping
- US keyboard conventions
void switch_caps_lock(void) // Toggle Caps Lock
void switch_num_lock(void) // Toggle Num Lock
void switch_scroll_lock(void) // Toggle Scroll LockThe driver processes both press and release events:
- Press events: Scancodes 0x01-0x58
- Release events: Scancodes 0x81-0xD8 (press + 0x80)
- Extended keys: Prefixed with 0xE0 scancode
Provides system timing services and delays:
- System tick: Regular timer interrupts
- Delay functions: Millisecond-precision delays
- Scheduler support: Time-slice management
void set_timer(float freq) // Set timer frequency
void delay(unsigned int ms) // Millisecond delayUses the 8253/8254 Programmable Interval Timer:
void set_timer(float freq)
{
unsigned int rate = 1193182 / freq; // Calculate divisor
outb(Timer_cr, 0x36); // Configure timer mode
outb(Timer_port, (unsigned char) rate); // Low byte
outb(Timer_port, (unsigned char) (rate >> 8)); // High byte
}Timer IRQ0 handler:
- Increments global tick counter
- Calls scheduler for task switching
- Provides timing base for delays
Precise millisecond delays using tick counting:
void delay(unsigned int ms)
{
unsigned long target_ticks = (ms * FREQ_SYS) / 1000;
unsigned long start_tick = tick_count;
while ((tick_count - start_tick) < target_ticks);
}- Low-level FDC programming: Direct controller access
- DMA support: Direct Memory Access transfers
- Multiple formats: 1.44MB, 1.2MB, 720KB support
- Error recovery: Automatic retry on failures
- FAT12 integration: Filesystem support
Automatic floppy drive detection:
void detect_floppy(void)
{
// Read CMOS for drive types
// Detect 3.5" and 5.25" drives
// Configure drive parameters
}int fd_read_sector(xfloppy fd, int sector, char *buffer) // Read sector
int fd_write_sector(xfloppy fd, int sector, char *buffer) // Write sector
int fd_seek(xfloppy fd, int track) // Seek to track
int mountfloppy(xfloppy *fd) // Mount filesystemSets up DMA channel 2 for floppy transfers:
void setup_dma(void *buffer, int count, int mode)
{
// Configure DMA controller
// Set buffer address and count
// Enable DMA channel
}Comprehensive error recovery:
- Retry logic: Multiple attempts on failure
- Recalibration: Return to track 0 on errors
- Status checking: Verify operation completion
- Timeout handling: Prevent infinite waits
Support for multiple floppy formats:
typedef struct {
int sectors_per_track;
int heads;
int tracks;
int sector_size;
} floppy_params;Located in 32b/video/ directory:
- 80x25 text display: Standard VGA text mode
- Color support: 16 foreground, 8 background colors
- Cursor control: Hardware cursor positioning
- Scrolling: Automatic screen scrolling
VESA BIOS Extensions support:
- 640x480x256: Standard SVGA mode
- Linear framebuffer: Direct memory access
- Mode detection: Automatic capability detection
- Graphics primitives: Basic drawing functions
void printk(const char *format, ...) // Formatted output
void putcar(char c) // Single character output
void clrscr(void) // Clear screen
void gotoxy(int x, int y) // Position cursor
void attrb(int attr) // Set text attributesConfiguration and management of 8259A PICs:
void init_pic(void) // Initialize PIC
void enable_irq(int irq) // Enable specific IRQ
void disable_irq(int irq) // Disable specific IRQ
void send_eoi(int irq) // Send End of InterruptStandard PC IRQ allocation:
- IRQ0: System timer
- IRQ1: Keyboard
- IRQ6: Floppy disk controller
- IRQ14: Primary IDE (future use)
Each driver provides ISR functions:
void irq_timer(void) // Timer interrupt handler
void irq_kbd(void) // Keyboard interrupt handler
void irq_floppy(void) // Floppy interrupt handlerDrivers are initialized during kernel startup:
- PIC configuration: Set up interrupt controllers
- Timer setup: Configure system timer frequency
- Keyboard init: Initialize keyboard controller
- Floppy detection: Probe for floppy drives
- Video init: Set up display mode
Automatic hardware probing:
- CMOS reading: Get hardware configuration
- Port probing: Test for device presence
- BIOS queries: Use BIOS services for detection
- Timer: <5 microseconds
- Keyboard: <10 microseconds
- Floppy: <20 microseconds
- Floppy: ~500KB/s sustained transfer
- Keyboard: ~1000 characters/second max
- Timer: 1000Hz maximum frequency
- Driver code: ~8KB total
- Buffers: ~4KB for floppy I/O
- Data structures: ~1KB for state
Standardized error reporting:
- 0: Success
- -1: General failure
- -2: Timeout
- -3: Hardware error
- -4: Invalid parameter
Debug functions available in debug builds:
void print_floppy_status(void) // Display FDC status
void dump_keyboard_state(void) // Show keyboard state
void show_timer_info(void) // Timer statisticsReal-time hardware monitoring:
- Drive motor status: Floppy motor state
- Keyboard LEDs: Caps/Num/Scroll Lock indicators
- Timer frequency: Current timer settings
- IDE/ATA: Hard disk support
- Serial ports: RS-232 communication
- Parallel port: Printer support
- Sound: PC speaker and sound cards
- Network: Ethernet adapters
- Plug and Play: Automatic device detection
- Power management: ACPI support
- USB: Universal Serial Bus
- SCSI: Small Computer System Interface
#define FLOPPY_DEBUG // Enable floppy debugging
#define KBD_BUFFER_SIZE // Keyboard buffer size
#define TIMER_FREQ // Default timer frequency- Keymap selection: Switch between layouts
- Timer frequency: Adjustable timing
- Drive parameters: Custom floppy formats
This driver architecture provides a solid foundation for hardware abstraction while maintaining the simplicity appropriate for an educational operating system.