122 lines
2.9 KiB
C
122 lines
2.9 KiB
C
#include "vfs.h"
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#include <lib/print.h>
|
|
|
|
#include <drivers/vga/vga.h>
|
|
#include <drivers/serial/serial.h>
|
|
|
|
#include <common.h>
|
|
#include <memory/mm.h>
|
|
|
|
#include <sys/video.h>
|
|
|
|
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;
|
|
|
|
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};
|
|
|
|
uint32_t vga_mmap(vfs_node_t* node, uint32_t address) {
|
|
uint32_t fb_size_bytes = framebuffer_height * framebuffer_pitch;
|
|
uint32_t num_pages = (fb_size_bytes + 4095) / 4096;
|
|
for (uint32_t i = 0; i < num_pages; i++) {
|
|
uint32_t phys_addr = framebuffer_address + (i * 4096);
|
|
uint32_t virt_addr = address + (i * 4096);
|
|
map_page(virt_addr, phys_addr, PAGE_PRESENT | PAGE_WRITABLE);
|
|
}
|
|
return address;
|
|
}
|
|
|
|
uint32_t vga_ioctl(vfs_node_t* node, uint32_t request, void* arg) {
|
|
if (request == FBIOGET_INFO) {
|
|
fb_info_t* info = (fb_info_t*)arg;
|
|
info->width = framebuffer_width;
|
|
info->height = framebuffer_height;
|
|
info->pitch = framebuffer_pitch;
|
|
info->fb_size = framebuffer_total_pixels;
|
|
info->bpp = framebuffer_bpp;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void vga_init(
|
|
vfs_node_t* dev,
|
|
uint32_t fb_paddr,
|
|
uint32_t fb_width,
|
|
uint32_t fb_height,
|
|
uint32_t fb_pitch,
|
|
uint8_t fb_bpp
|
|
) {
|
|
framebuffer_address = fb_paddr;
|
|
framebuffer_width = fb_width;
|
|
framebuffer_height = fb_height;
|
|
framebuffer_total_pixels = framebuffer_width * framebuffer_height;
|
|
framebuffer_pitch = fb_pitch;
|
|
framebuffer_bpp = fb_bpp;
|
|
|
|
kprintf("mapping vga pages\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++) {
|
|
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);
|
|
}
|
|
|
|
vga_clear_scrn(0x00000000);
|
|
|
|
dev->flags = VFS_CHARDEVICE;
|
|
dev->mmap = vga_mmap;
|
|
dev->ioctl = vga_ioctl;
|
|
}
|
|
|
|
uint32_t vga_get_framebuffer() {
|
|
return VGA_FRAMEBUFFER;
|
|
}
|
|
|
|
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) {
|
|
uint8_t* fb_bytes = (uint8_t*)VGA_FRAMEBUFFER;
|
|
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++) {
|
|
uint8_t* pixel = row + (x * bytes_per_pixel);
|
|
|
|
switch(framebuffer_bpp) {
|
|
case 32: {
|
|
*(uint32_t*)pixel = color;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|