From 3c103367d02a05dc26ede475d5fd917ff511785b Mon Sep 17 00:00:00 2001 From: Greg Bowne Date: Sat, 15 Jul 2023 14:25:31 -0700 Subject: [PATCH] trying to fix the drivers and also the isr, gdt and isr header files. --- src/drivers/display/display.c | 0 src/drivers/display/display.h | 0 src/drivers/screen/screen.c | 74 ++++++++++++++++++++++++++--------- src/drivers/screen/screen.h | 44 ++++++++++++++++++--- src/drivers/tty/tty.c | 36 +++++++++++++++++ src/drivers/tty/tty.h | 12 ++++++ src/kernel/arch/x86/gdt.c | 22 ++++++++--- src/kernel/arch/x86/idt.c | 6 +++ src/kernel/arch/x86/isr.h | 2 +- 9 files changed, 166 insertions(+), 30 deletions(-) create mode 100644 src/drivers/display/display.c create mode 100644 src/drivers/display/display.h create mode 100644 src/drivers/tty/tty.c create mode 100644 src/drivers/tty/tty.h diff --git a/src/drivers/display/display.c b/src/drivers/display/display.c new file mode 100644 index 0000000..e69de29 diff --git a/src/drivers/display/display.h b/src/drivers/display/display.h new file mode 100644 index 0000000..e69de29 diff --git a/src/drivers/screen/screen.c b/src/drivers/screen/screen.c index d34b868..fa14790 100644 --- a/src/drivers/screen/screen.c +++ b/src/drivers/screen/screen.c @@ -1,27 +1,63 @@ #include "screen.h" -#include -void screen_init() { - // Initialize the screen driver - // Add any necessary initialization code here +#include +#include +#include +#include +#include + +// Video memory address +#define VIDEO_MEMORY_ADDRESS 0xB8000 + +// Screen attributes (color) +static uint8_t screen_color = + (SCREEN_COLOR_LIGHT_GRAY << 4) | SCREEN_COLOR_BLACK; + +// Helper function to calculate the screen buffer offset +static inline uint16_t screen_offset(uint8_t x, uint8_t y) +{ + return (y * SCREEN_WIDTH + x) * 2; } -void set_40_column_mode() { - // Set the screen mode to 40 columns - union REGS regs; - regs.h.ah = 0x00; - regs.h.al = 0x03; - int86(0x10, ®s, ®s); +// Initialize the screen driver +void screen_init() +{ + // No initialization required for this simple driver } -void set_80_column_mode() { - // Set the screen mode to 80 columns - union REGS regs; - regs.h.ah = 0x00; - regs.h.al = 0x03; - int86(0x10, ®s, ®s); +// Clear the screen +void screen_clear() +{ + uint16_t *video_memory = (uint16_t *)VIDEO_MEMORY_ADDRESS; + for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) + { + video_memory[i] = ' ' | (screen_color << 8); + } +} - regs.h.ah = 0x00; - regs.h.al = 0x07; - int86(0x10, ®s, ®s); +// Set the foreground and background colors +void screen_set_color(uint8_t foreground, uint8_t background) +{ + screen_color = (background << 4) | foreground; +} + +// Print a character at the specified position +void screen_put_char(char c, uint8_t x, uint8_t y) +{ + uint16_t *video_memory = (uint16_t *)VIDEO_MEMORY_ADDRESS; + uint16_t offset = screen_offset(x, y); + video_memory[offset] = c | (screen_color << 8); +} + +// Print a null-terminated string at the specified position +void screen_put_string(const char *str, uint8_t x, uint8_t y) +{ + uint16_t *video_memory = (uint16_t *)VIDEO_MEMORY_ADDRESS; + uint16_t offset = screen_offset(x, y); + while (*str) + { + video_memory[offset] = *str | (screen_color << 8); + str++; + offset += 2; + } } \ No newline at end of file diff --git a/src/drivers/screen/screen.h b/src/drivers/screen/screen.h index f943148..8ab9be9 100644 --- a/src/drivers/screen/screen.h +++ b/src/drivers/screen/screen.h @@ -1,13 +1,47 @@ #ifndef SCREEN_H #define SCREEN_H -// Function to initialize the screen driver +#include +#include +#include +#include +#include + +// Screen dimensions +#define SCREEN_WIDTH 80 +#define SCREEN_HEIGHT 25 + +// Screen colors +#define SCREEN_COLOR_BLACK 0 +#define SCREEN_COLOR_BLUE 1 +#define SCREEN_COLOR_GREEN 2 +#define SCREEN_COLOR_CYAN 3 +#define SCREEN_COLOR_RED 4 +#define SCREEN_COLOR_MAGENTA 5 +#define SCREEN_COLOR_BROWN 6 +#define SCREEN_COLOR_LIGHT_GRAY 7 +#define SCREEN_COLOR_DARK_GRAY 8 +#define SCREEN_COLOR_LIGHT_BLUE 9 +#define SCREEN_COLOR_LIGHT_GREEN 10 +#define SCREEN_COLOR_LIGHT_CYAN 11 +#define SCREEN_COLOR_LIGHT_RED 12 +#define SCREEN_COLOR_LIGHT_MAGENTA 13 +#define SCREEN_COLOR_YELLOW 14 +#define SCREEN_COLOR_WHITE 15 + +// Initialize the screen driver void screen_init(); -// Function to set the screen mode to 40 columns -void set_40_column_mode(); +// Clear the screen +void screen_clear(); -// Function to set the screen mode to 80 columns -void set_80_column_mode(); +// Set the foreground and background colors +void screen_set_color(uint8_t foreground, uint8_t background); + +// Print a character at the specified position +void screen_put_char(char c, uint8_t x, uint8_t y); + +// Print a null-terminated string at the specified position +void screen_put_string(const char *str, uint8_t x, uint8_t y); #endif \ No newline at end of file diff --git a/src/drivers/tty/tty.c b/src/drivers/tty/tty.c new file mode 100644 index 0000000..f1012fd --- /dev/null +++ b/src/drivers/tty/tty.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +#include "../display.h" +#include "../interrupts.h" +#include "../io/io.h" +#include "../keyboard.h" +#include "../string.h" +#include "./screen/screen.h" + +#include "./tty.h" + +// Function definitions + +// Example function to print a message to the console +void tty_print(const char *message) +{ + printf("%s", message); +} + +// Example function to read input from the console +void tty_read(char *buffer, size_t size) +{ + fgets(buffer, size, stdin); +} + +// Example function to clear the console +void tty_clear() +{ + printf("\033[2J\033[H"); +} + +// More function definitions... diff --git a/src/drivers/tty/tty.h b/src/drivers/tty/tty.h new file mode 100644 index 0000000..a57389f --- /dev/null +++ b/src/drivers/tty/tty.h @@ -0,0 +1,12 @@ +#ifndef TTY_H +#define TTY_H + +#include +#include +#include +#include +#include + +// Function declarations + +#endif // TTY_H \ No newline at end of file diff --git a/src/kernel/arch/x86/gdt.c b/src/kernel/arch/x86/gdt.c index fb79722..6c162cf 100644 --- a/src/kernel/arch/x86/gdt.c +++ b/src/kernel/arch/x86/gdt.c @@ -1,10 +1,21 @@ #include "gdt.h" +#include "isr.h" +#include +#include +#include +#include +#include #include +#include + // GDT table -struct gdt_entry gdt[3]; +struct gdt_entry *gdt; // GDT pointer -struct gdt_ptr gp; +struct gdt_ptr { + uint16_t limit; + void* base; +} gp; // Initialize a GDT entry void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, @@ -24,10 +35,11 @@ void gdt_init() { // Set up GDT pointer gp.limit = (sizeof(struct gdt_entry) * 3) - 1; - gp.base = (uint32_t)&gdt; + gdt = (struct gdt_entry*)malloc(sizeof(struct gdt_entry) * 3); + gp.base = gdt; // Clear GDT - memset(&gdt, 0, sizeof(gdt)); + memset(gdt, 0, sizeof(struct gdt_entry) * 3); // Set up code segment gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); @@ -36,7 +48,7 @@ void gdt_init() gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Load GDT - _asm volatile("lgdt %0" : : "m"(gp)); + asm volatile("lgdt %0" : : "m"(gp)); } // Exception handlers diff --git a/src/kernel/arch/x86/idt.c b/src/kernel/arch/x86/idt.c index d151950..2b0ea44 100644 --- a/src/kernel/arch/x86/idt.c +++ b/src/kernel/arch/x86/idt.c @@ -1,6 +1,12 @@ #include "idt.h" #include +#include +#include +#include +#include +#include + // IDT table struct idt_entry idt[256]; diff --git a/src/kernel/arch/x86/isr.h b/src/kernel/arch/x86/isr.h index e733a0d..a7f8852 100644 --- a/src/kernel/arch/x86/isr.h +++ b/src/kernel/arch/x86/isr.h @@ -16,7 +16,7 @@ struct isr_regs }; // Register an ISR -void isr_register(uint8_t num, void (*handler)(struct isr_regs *regs)); +void isr_register(uint8_t num, void (*handler)(void)); // ISR handler void isr_handler(struct isr_regs *regs);