diff options
Diffstat (limited to 'kernel/drivers')
-rw-r--r-- | kernel/drivers/drivers.c | 12 | ||||
-rw-r--r-- | kernel/drivers/gpu.c | 119 | ||||
-rw-r--r-- | kernel/drivers/gpu/bochs.c | 98 | ||||
-rw-r--r-- | kernel/drivers/term.c | 105 | ||||
-rw-r--r-- | kernel/drivers/vga.c | 25 |
5 files changed, 242 insertions, 117 deletions
diff --git a/kernel/drivers/drivers.c b/kernel/drivers/drivers.c deleted file mode 100644 index 4e8f175..0000000 --- a/kernel/drivers/drivers.c +++ /dev/null @@ -1,12 +0,0 @@ -#include <comus/drivers.h> -#include <comus/drivers/acpi.h> -#include <comus/drivers/uart.h> -#include <comus/drivers/pci.h> -#include <comus/mboot.h> - -void drivers_init(void) -{ - uart_init(); - pci_init(); - acpi_init(mboot_get_rsdp()); -} diff --git a/kernel/drivers/gpu.c b/kernel/drivers/gpu.c new file mode 100644 index 0000000..7ecdada --- /dev/null +++ b/kernel/drivers/gpu.c @@ -0,0 +1,119 @@ +#include <lib.h> +#include <comus/drivers/gpu.h> +#include <comus/drivers/gpu/bochs.h> +#include <comus/error.h> +#include <comus/term.h> +#include <comus/asm.h> + +enum gpu_type { + GPU_UNSET = 0, + GPU_BOCHS = 1, + N_GPU_TYPE, +}; + +static enum gpu_type type = GPU_UNSET; + +static const char *gpu_type_str[N_GPU_TYPE] = { + "Unknown", "Bochs" +}; + +struct psf2_font { + uint8_t magic[4]; + uint32_t version; + uint32_t header_size; + uint32_t flags; + uint32_t length; + uint32_t glyph_size; + uint32_t height; + uint32_t width; + uint8_t data[]; +}; + +extern struct psf2_font en_font; + +int gpu_init(void) +{ + if (bochs_init() == SUCCESS) + type = GPU_BOCHS; + + if (type != GPU_UNSET) { + uint16_t width = gpu_width() / en_font.width; + uint16_t height = gpu_height() / en_font.height; + term_switch_handler(width, height, gpu_draw_char); + } + + return type != GPU_UNSET; +} + +uint32_t gpu_width(void) +{ + switch (type) { + case GPU_BOCHS: + return bochs_width(); + default: + return 0; + } +} + +uint32_t gpu_height(void) +{ + switch (type) { + case GPU_BOCHS: + return bochs_height(); + default: + return 0; + } +} + +uint8_t gpu_bit_depth(void) +{ + switch (type) { + case GPU_BOCHS: + return bochs_bit_depth(); + default: + return 0; + } +} + +void gpu_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, uint32_t b) +{ + switch (type) { + case GPU_BOCHS: + bochs_set_pixel(x, y, r, g, b); + break; + default: + break; + } +} + +void gpu_draw_char(char c, uint16_t cx, uint16_t cy) +{ + uint32_t sx = en_font.width * cx; + uint32_t sy = en_font.height * cy; + + uint8_t *glyph = &en_font.data[en_font.glyph_size * c]; + + for (uint32_t i = 0; i < en_font.width; i++) { + for (uint32_t j = 0; j < en_font.height; j++) { + uint32_t x = sx + (en_font.width - i - 1); + uint32_t y = sy + j; + uint32_t bitoff = i + j * en_font.width; + uint32_t byteoff = bitoff / 8; + uint32_t bitshift = bitoff % 8; + + uint32_t color = ((glyph[byteoff] >> bitshift) & 0x1) * 255; + gpu_set_pixel(x, y, color, color, color); + } + } +} + +void gpu_report(void) +{ + if (type == GPU_UNSET) + return; + + kprintf("GPU (%s)\n", gpu_type_str[type]); + kprintf("Width: %d\n", gpu_width()); + kprintf("Height: %d\n", gpu_height()); + kprintf("Bit depth: %d\n\n", gpu_bit_depth()); +} diff --git a/kernel/drivers/gpu/bochs.c b/kernel/drivers/gpu/bochs.c new file mode 100644 index 0000000..5b0f1fc --- /dev/null +++ b/kernel/drivers/gpu/bochs.c @@ -0,0 +1,98 @@ +#include <lib.h> +#include <comus/asm.h> +#include <comus/memory.h> +#include <comus/drivers/pci.h> +#include <comus/drivers/gpu/bochs.h> + +#define INDEX 0x1CE +#define DATA 0x1CF + +#define INDEX_ID 0 +#define INDEX_XRES 1 +#define INDEX_YRES 2 +#define INDEX_BPP 3 +#define INDEX_ENABLE 4 +#define INDEX_BANK 5 +#define INDEX_VIRT_WIDTH 6 +#define INDEX_VIRT_HEIGHT 7 +#define INDEX_X_OFFSET 8 +#define INDEX_Y_OFFSET 9 + +#define DATA_DISP_DISABLE 0x00 +#define DATA_DISP_ENABLE 0x01 +#define DATA_LFB_ENABLE 0x40 +#define DATA_NO_CLEAR_MEM 0x80 + +#define BOCHS_PCI_VENDOR 0x1234 +#define BOCHS_PCI_DEVICE 0x1111 + +#define BOCHS_WIDTH 1024 +#define BOCHS_HEIGHT 768 +#define BOCHS_BIT_DEPTH 32 + +static volatile uint32_t *framebuffer; +static uint16_t width = BOCHS_WIDTH; +static uint16_t height = BOCHS_HEIGHT; +static uint8_t bit_depth = BOCHS_BIT_DEPTH; + +static void write(uint16_t index, uint16_t data) { + outw(INDEX, index); + outw(DATA, data); +} + +static uint16_t read(uint16_t value) { + outw(INDEX, value); + return inw(DATA); +} + +static int is_available(void) { + return (read(INDEX_ID) == 0xB0C5); +} + +static void set_mode(uint16_t width, uint16_t height, uint16_t bit_depth, int lfb, int clear) { + write(INDEX_ENABLE, DATA_DISP_DISABLE); + write(INDEX_XRES, width); + write(INDEX_YRES, height); + write(INDEX_BPP, bit_depth); + write(INDEX_ENABLE, DATA_DISP_ENABLE | + (lfb ? DATA_LFB_ENABLE : 0) | + (clear ? 0 : DATA_NO_CLEAR_MEM)); +} + +int bochs_init(void) { + struct pci_device bochs = {0}; + bool found = pci_findby_id(&bochs, BOCHS_PCI_DEVICE, BOCHS_PCI_VENDOR, NULL); + if (!found) + return 1; + + set_mode(width, height, bit_depth, true, true); + if (!is_available()) + return 1; + + uint32_t bar0 = pci_rcfg_d(bochs, PCI_BAR0_D); + uint32_t *addr = (uint32_t *) (uintptr_t) bar0; + framebuffer = kmapaddr(addr, NULL, width * height * bit_depth, F_WRITEABLE); + + return 0; +} + +uint32_t bochs_width(void) +{ + return width; +} + +uint32_t bochs_height(void) +{ + return height; +} + +uint8_t bochs_bit_depth(void) +{ + return bit_depth; +} + +void bochs_set_pixel(uint32_t x, uint32_t y, uint32_t r, uint32_t g, uint32_t b) +{ + uint32_t index = x + y * width; + framebuffer[index] = (b << 0) | (g << 8) | (r << 16); +} diff --git a/kernel/drivers/term.c b/kernel/drivers/term.c deleted file mode 100644 index d1cd553..0000000 --- a/kernel/drivers/term.c +++ /dev/null @@ -1,105 +0,0 @@ -#include <lib.h> -#include <comus/drivers/term.h> -#include <comus/asm.h> -#include <comus/memory.h> - -#define VGA_ADDR 0xB8000 - -static const uint8_t width = 80; -static const uint8_t height = 25; -static volatile uint16_t *buffer = (uint16_t *)VGA_ADDR; - -// position -static uint32_t x = 0, y = 0; - -// color -static uint8_t fg = 15, bg = 0; - -// blank color -const uint16_t blank = (uint16_t)0 | 0 << 12 | 15 << 8; - -void term_clear_line(int line) -{ - if (line < 0 || line >= height) - return; - for (uint8_t x = 0; x < width; x++) { - const size_t index = line * width + x; - buffer[index] = blank; - } -} - -void term_clear(void) -{ - for (uint8_t y = 0; y < height; y++) - term_clear_line(y); -} - -void term_scroll(int lines) -{ - cli(); - y -= lines; - if (!lines) - return; - if (lines >= height || lines <= -height) { - term_clear(); - } else if (lines > 0) { - memmovev(buffer, buffer + lines * width, 2 * (height - lines) * width); - term_clear_line(height - lines); - } else { - memmovev(buffer + lines * width, buffer + lines, - (height + lines) * width); - } - sti(); -} - -void term_out(char c) -{ - if (buffer == NULL) - return; - - // handle special chars - switch (c) { - case '\n': - x = 0; - y++; - break; - case '\t': - x += 4; - break; - case '\v': - case '\f': - y++; - break; - case '\r': - x = 0; - break; - default: { - const size_t index = y * width + x; - buffer[index] = c | bg << 12 | fg << 8; - x++; - } - } - - // wrap cursor if needed - if (x >= width) { - x = 0; - y++; - } - if (y >= height) { - term_scroll(y - (height - 1)); - y = height - 1; - } - - // set cursor position on screen - const uint16_t pos = y * width + x; - outb(0x3D4, 0x0F); - outb(0x3D5, (uint8_t)(pos & 0xFF)); - outb(0x3D4, 0x0E); - outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); -} - -void term_out_str(const char *str) -{ - while (*str) - term_out(*str++); -} diff --git a/kernel/drivers/vga.c b/kernel/drivers/vga.c new file mode 100644 index 0000000..d73fa9f --- /dev/null +++ b/kernel/drivers/vga.c @@ -0,0 +1,25 @@ +#include <lib.h> +#include <comus/term.h> +#include <comus/asm.h> +#include <comus/memory.h> +#include <comus/drivers/vga.h> + +#define VGA_ADDR 0xB8000 +static volatile uint16_t *buffer = (uint16_t *)VGA_ADDR; + +// color +static uint8_t fg = 15, bg = 0; + +void vga_draw_char(char c, uint16_t x, uint16_t y) +{ + // output character + const size_t index = y * VGA_WIDTH + x; + buffer[index] = c | bg << 12 | fg << 8; + + // set cursor position on screen + const uint16_t pos = y * VGA_HEIGHT + x; + outb(0x3D4, 0x0F); + outb(0x3D5, (uint8_t)(pos & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); +} |