2026-06-10 18:28:33 -05:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include <lib/stream.h>
|
|
|
|
|
|
2026-07-12 11:42:49 -05:00
|
|
|
#include <drivers/vga/vga.h>
|
2026-07-15 08:07:06 -05:00
|
|
|
#include <drivers/serial/serial.h>
|
2026-07-12 11:42:49 -05:00
|
|
|
|
|
|
|
|
#include <common.h>
|
2026-07-14 17:40:30 -05:00
|
|
|
#include <memory/mm.h>
|
2026-07-10 07:46:46 -05:00
|
|
|
|
2026-07-14 00:07:50 -05:00
|
|
|
static uint32_t framebuffer_address;
|
|
|
|
|
static uint32_t framebuffer_width;
|
|
|
|
|
static uint32_t framebuffer_height;
|
|
|
|
|
static uint32_t framebuffer_total_pixels;
|
|
|
|
|
static uint32_t framebuffer_pitch;
|
|
|
|
|
static uint32_t framebuffer_bpp;
|
2026-06-10 18:28:33 -05:00
|
|
|
|
2026-07-15 08:07:06 -05:00
|
|
|
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};
|
|
|
|
|
|
2026-07-14 00:07:50 -05:00
|
|
|
void vga_init(
|
2026-07-14 17:40:30 -05:00
|
|
|
uint32_t fb_paddr,
|
2026-07-14 00:07:50 -05:00
|
|
|
uint32_t fb_width,
|
|
|
|
|
uint32_t fb_height,
|
|
|
|
|
uint32_t fb_pitch,
|
2026-07-15 08:07:06 -05:00
|
|
|
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
|
2026-07-14 00:07:50 -05:00
|
|
|
) {
|
2026-07-15 08:07:06 -05:00
|
|
|
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");
|
|
|
|
|
|
2026-07-14 17:40:30 -05:00
|
|
|
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++) {
|
|
|
|
|
uint32_t phys_addr = fb_paddr + (i * 4096);
|
|
|
|
|
uint32_t virt_addr = VGA_FRAMEBUFFER + (i * 4096);
|
|
|
|
|
map_page(virt_addr, phys_addr, PAGE_PRESENT | PAGE_WRITABLE);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 00:07:50 -05:00
|
|
|
framebuffer_width = fb_width;
|
|
|
|
|
framebuffer_height = fb_height;
|
2026-07-15 08:07:06 -05:00
|
|
|
framebuffer_total_pixels = framebuffer_width * framebuffer_height;
|
|
|
|
|
|
2026-07-14 00:07:50 -05:00
|
|
|
framebuffer_pitch = fb_pitch;
|
|
|
|
|
framebuffer_bpp = fb_bpp;
|
|
|
|
|
|
2026-07-15 08:07:06 -05:00
|
|
|
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;
|
2026-07-14 17:40:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t vga_get_pitch() {
|
|
|
|
|
return framebuffer_pitch;
|
2026-07-12 11:42:49 -05:00
|
|
|
}
|
|
|
|
|
|
2026-07-15 08:07:06 -05:00
|
|
|
uint8_t vga_get_bpp() {
|
|
|
|
|
return framebuffer_bpp;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 00:07:50 -05:00
|
|
|
void vga_clear_scrn(uint32_t color) {
|
|
|
|
|
if (framebuffer_address == 0) return;
|
|
|
|
|
|
2026-07-15 08:07:06 -05:00
|
|
|
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);
|
|
|
|
|
|
2026-07-14 17:40:30 -05:00
|
|
|
for (uint32_t x = 0; x < framebuffer_width; x++) {
|
2026-07-15 08:07:06 -05:00
|
|
|
uint8_t* pixel = row + (x * bytes_per_pixel);
|
|
|
|
|
|
|
|
|
|
switch(framebuffer_bpp) {
|
|
|
|
|
case 32: {
|
|
|
|
|
*(uint32_t*)pixel = color;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-14 17:40:30 -05:00
|
|
|
}
|
2026-07-14 00:07:50 -05:00
|
|
|
}
|
2026-06-10 18:28:33 -05:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 04:37:46 -05:00
|
|
|
uint32_t vga_write(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buf) {
|
2026-07-12 03:56:25 -05:00
|
|
|
return size;
|
2026-06-26 20:05:01 -05:00
|
|
|
}
|