From 44a0c9aebafe4af43c0741a5e85e5865dcce67ba Mon Sep 17 00:00:00 2001 From: slinky55 Date: Wed, 15 Jul 2026 08:07:06 -0500 Subject: [PATCH] blue! --- clean.sh | 1 - kernel/drivers/vga/vga.c | 91 +++++++++++++++++++---- kernel/drivers/vga/vga.h | 44 +++++------ kernel/kmain.c | 16 +++- kernel/{drivers/console => lib}/console.c | 13 ++-- kernel/{drivers/console => lib}/console.h | 2 +- kernel/lib/print.c | 2 +- make_img.sh | 6 +- makefile | 2 +- 9 files changed, 126 insertions(+), 51 deletions(-) rename kernel/{drivers/console => lib}/console.c (84%) rename kernel/{drivers/console => lib}/console.h (80%) diff --git a/clean.sh b/clean.sh index a3fc7ab..f9b4f97 100755 --- a/clean.sh +++ b/clean.sh @@ -18,6 +18,5 @@ cd .. make clean cd initrd -rm -rf output rm -f bin/*.elf cd .. \ No newline at end of file diff --git a/kernel/drivers/vga/vga.c b/kernel/drivers/vga/vga.c index 641446f..8623557 100644 --- a/kernel/drivers/vga/vga.c +++ b/kernel/drivers/vga/vga.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -15,13 +16,55 @@ static uint32_t framebuffer_total_pixels; static uint32_t framebuffer_pitch; static uint32_t framebuffer_bpp; +static uint8_t red_mask_sz; +static uint8_t red_field_pos; +static uint8_t green_mask_sz; +static uint8_t green_field_pos; +static uint8_t blue_mask_sz; +static uint8_t blue_field_pos; + +static uint32_t create_pixel(uint8_t r, uint8_t g, uint8_t b) { + return (r >> (8 - red_mask_sz)) << red_field_pos + | (g >> (8 - green_mask_sz)) << green_field_pos + | (b >> (8 - blue_mask_sz)) << blue_field_pos; +} + +vga_color_t VGA_COLOR_BLACK = {0, 0, 0, 255}; +vga_color_t VGA_COLOR_RED = {0, 0, 255, 255}; +vga_color_t VGA_COLOR_GREEN = {0, 255, 0, 255}; +vga_color_t VGA_COLOR_BLUE = {255, 0, 0, 255}; + void vga_init( uint32_t fb_paddr, uint32_t fb_width, uint32_t fb_height, uint32_t fb_pitch, - uint8_t fb_bpp + uint8_t fb_bpp, + uint8_t _red_mask_sz, + uint8_t _red_field_pos, + uint8_t _green_mask_sz, + uint8_t _green_field_pos, + uint8_t _blue_mask_sz, + uint8_t _blue_field_pos ) { + framebuffer_address = VGA_FRAMEBUFFER; + + red_mask_sz = _red_mask_sz; + red_field_pos = _red_field_pos; + green_mask_sz = _green_mask_sz; + green_field_pos = _green_field_pos; + blue_mask_sz = _blue_mask_sz; + blue_field_pos = _blue_field_pos; + + print_serial("initializing vga.\r\n"); + print_serial_hex(fb_paddr); + print_serial("\r\n"); + print_serial_hex(framebuffer_address); + print_serial("\r\n"); + print_serial("BPP: "); + print_serial_hex(fb_bpp); + print_serial("\r\n"); + uint32_t fb_size_bytes = fb_height * fb_pitch; uint32_t num_pages = (fb_size_bytes + 4095) / 4096; for (uint32_t i = 0; i < num_pages; i++) { @@ -30,36 +73,58 @@ void vga_init( map_page(virt_addr, phys_addr, PAGE_PRESENT | PAGE_WRITABLE); } - framebuffer_address = VGA_FRAMEBUFFER; framebuffer_width = fb_width; framebuffer_height = fb_height; - framebuffer_pitch = fb_pitch; - framebuffer_bpp = fb_bpp; framebuffer_total_pixels = framebuffer_width * framebuffer_height; - vga_clear_scrn(0x000000FF); + framebuffer_pitch = fb_pitch; + framebuffer_bpp = fb_bpp; + + vga_clear_scrn(0x00000000); +} + +uint32_t vga_get_framebuffer() { + return framebuffer_address; +} + +uint32_t vga_get_width() { + return framebuffer_width; +} + +uint32_t vga_get_height() { + return framebuffer_height; } uint32_t vga_get_pitch() { return framebuffer_pitch; } +uint8_t vga_get_bpp() { + return framebuffer_bpp; +} + void vga_clear_scrn(uint32_t color) { if (framebuffer_address == 0) return; - for (uint32_t y = 0; y < framebuffer_height; y++) { - uint32_t* row = (uint32_t*)((uintptr_t)framebuffer_address + (y * framebuffer_pitch)); + uint8_t* fb_bytes = (uint8_t*)framebuffer_address; + uint8_t bytes_per_pixel = framebuffer_bpp / 8; + + for (uint32_t y = 0; y < framebuffer_height; y++) { + uint8_t* row = fb_bytes + (y * framebuffer_pitch); + for (uint32_t x = 0; x < framebuffer_width; x++) { - row[x] = color; + uint8_t* pixel = row + (x * bytes_per_pixel); + + switch(framebuffer_bpp) { + case 32: { + *(uint32_t*)pixel = color; + break; + } + } } } } -void vga_drawchar(char c, int x, int y, psf_font_t* font) { - -} - uint32_t vga_write(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buf) { - // probably just memcpy here return size; } \ No newline at end of file diff --git a/kernel/drivers/vga/vga.h b/kernel/drivers/vga/vga.h index 234fab1..4dec120 100644 --- a/kernel/drivers/vga/vga.h +++ b/kernel/drivers/vga/vga.h @@ -4,39 +4,41 @@ #include #include -#define VGA_FRAMEBUFFER 0xFF700000 +#define VGA_FRAMEBUFFER 0xF0000000 -enum vga_color { - VGA_COLOR_BLACK = 0, - VGA_COLOR_BLUE = 1, - VGA_COLOR_GREEN = 2, - VGA_COLOR_CYAN = 3, - VGA_COLOR_RED = 4, - VGA_COLOR_MAGENTA = 5, - VGA_COLOR_BROWN = 6, - VGA_COLOR_LIGHT_GREY = 7, - VGA_COLOR_DARK_GREY = 8, - VGA_COLOR_LIGHT_BLUE = 9, - VGA_COLOR_LIGHT_GREEN = 10, - VGA_COLOR_LIGHT_CYAN = 11, - VGA_COLOR_LIGHT_RED = 12, - VGA_COLOR_LIGHT_MAGENTA = 13, - VGA_COLOR_LIGHT_BROWN = 14, - VGA_COLOR_WHITE = 15, -}; +typedef struct { + uint8_t b; + uint8_t g; + uint8_t r; + uint8_t a; +} vga_color_t; + +extern vga_color_t VGA_COLOR_BLACK; +extern vga_color_t VGA_COLOR_RED; +extern vga_color_t VGA_COLOR_GREEN; +extern vga_color_t VGA_COLOR_BLUE; void vga_init( uint32_t fb_paddr, uint32_t fb_width, uint32_t fb_height, uint32_t fb_pitch, - uint8_t fb_bpp + uint8_t fb_bpp, + uint8_t red_mask_sz, + uint8_t red_field_pos, + uint8_t green_mask_sz, + uint8_t green_field_pos, + uint8_t blue_mask_sz, + uint8_t blue_field_pos ); +uint32_t vga_get_framebuffer(); +uint32_t vga_get_width(); +uint32_t vga_get_height(); uint32_t vga_get_pitch(); +uint8_t vga_get_bpp(); void vga_clear_scrn(uint32_t color); -void vga_drawchar(char c, int x, int y, psf_font_t* font); uint32_t vga_write(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buf); diff --git a/kernel/kmain.c b/kernel/kmain.c index cf421c5..27d87cd 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -24,7 +25,6 @@ #include #include #include -#include #include #include @@ -129,10 +129,20 @@ void kmain(multiboot_info* mbi) { vga_fb_width, vga_fb_height, vga_fb_pitch, - vga_fb_bpp); + vga_fb_bpp, + 0, 0, 0, 0, 0, 0); console_init(); - set_console_font("/fonts/cp850-8x16.psf"); + + int font_loaded = set_console_font("/fonts/cp850-8x16.psf"); + if (!font_loaded) { + #ifdef SERIAL_DBG + print_serial("failed to load console font"); + #endif + } + + console_putchar('c'); + return; kprintf("basic initializations complete\n"); diff --git a/kernel/drivers/console/console.c b/kernel/lib/console.c similarity index 84% rename from kernel/drivers/console/console.c rename to kernel/lib/console.c index 70d8546..a469dac 100644 --- a/kernel/drivers/console/console.c +++ b/kernel/lib/console.c @@ -1,6 +1,6 @@ -#include - +#include #include + #include static psf_font_t* font; @@ -10,7 +10,7 @@ static uint32_t console_col; static uint32_t console_text_color; static uint32_t console_backgrnd_color; -static void write_vga_char(int row, int col, char c) { +static void write_vga_char(char c) { uint32_t vga_pitch = vga_get_pitch(); uint8_t* glyph = (uint8_t*)font->glyphs + (c * font->hdr->charsz); @@ -25,8 +25,9 @@ static void write_vga_char(int row, int col, char c) { } } -void set_console_font(const char* path) { +int set_console_font(const char* path) { font = load_font(path); + return font != NULL; } void set_console_fg(uint32_t color) { @@ -41,9 +42,9 @@ void console_init() { console_row = 0; console_col = 0; set_console_fg(0xFFFFFFFF); - set_console_bg(0x00000000); + vga_clear_scrn(0x00000000); } void console_putchar(char c) { - + write_vga_char(c); } \ No newline at end of file diff --git a/kernel/drivers/console/console.h b/kernel/lib/console.h similarity index 80% rename from kernel/drivers/console/console.h rename to kernel/lib/console.h index 7587531..fd3bedc 100644 --- a/kernel/drivers/console/console.h +++ b/kernel/lib/console.h @@ -5,7 +5,7 @@ void console_putchar(char c); -void set_console_font(const char* path); +int set_console_font(const char* path); void set_console_fg(uint32_t color); void set_console_bg(uint32_t color); void console_init(); diff --git a/kernel/lib/print.c b/kernel/lib/print.c index db02de7..f7ded6b 100644 --- a/kernel/lib/print.c +++ b/kernel/lib/print.c @@ -5,7 +5,7 @@ #include #include -#include +#include spinlock_t kwrite_lock = {0}; diff --git a/make_img.sh b/make_img.sh index 8a211b2..16c9e63 100755 --- a/make_img.sh +++ b/make_img.sh @@ -8,7 +8,5 @@ mkdir -p initrd/bin cp programs/bin/* initrd/bin cd initrd -find . -depth -print | cpio -o -H newc > ../initrd.img -cd .. - -cp initrd.img isodir/boot \ No newline at end of file +find . -depth -print | cpio -o -H newc > ../isodir/boot/initrd.img +cd .. \ No newline at end of file diff --git a/makefile b/makefile index 9906a6b..a5279a7 100644 --- a/makefile +++ b/makefile @@ -33,7 +33,7 @@ obj/%.S.o: %.S $(CC) -c $< -o $@ $(CFLAGS) run: $(OSNAME).iso - qemu-system-i386 -cdrom $(OSNAME).iso + qemu-system-i386 -device VGA,edid=on -cdrom $(OSNAME).iso clean: @echo "Cleaning up build artifacts..."