Add VGA driver

This commit is contained in:
pjht 2019-04-26 10:03:16 -05:00
parent fbef5710ed
commit 5384ec3af0
2 changed files with 92 additions and 7 deletions

84
drivers/x86_64/vga.c Normal file
View File

@ -0,0 +1,84 @@
#include <grub/text_fb_info.h>
#include "../vga.h"
#include "../../cpu/x86_64/ports.h"
#include <string.h>
#include <stddef.h>
#define xy_to_indx(x,y) ((x+(y*width))*2)
static char* screen;
static int width;
static int height;
static int x;
static int y;
static vga_colors fg_color;
static vga_colors bg_color;
static void set_char(int x,int y,char c) {
screen[xy_to_indx(x,y)]=c;
screen[xy_to_indx(x,y)+1]=(bg_color<<4)|fg_color;
}
void vga_clear() {
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
set_char(x,y,' ');
}
}
}
static void set_cursor(int x,int y) {
int pos=(x+(y*width));
port_byte_out(0x3D4,0xF);
port_byte_out(0x3D5,pos&0xFF);
port_byte_out(0x3D4,0xE);
port_byte_out(0x3D5,(pos&0xFF00)>>8);
}
void vga_init(text_fb_info framebuffer_info) {
x=0;
y=0;
fg_color=VGA_WHITE;
bg_color=VGA_BLACK;
screen=framebuffer_info.address;
width=framebuffer_info.width;
height=framebuffer_info.height;
port_byte_out(0x3D4,0xA);
port_byte_out(0x3D5,(port_byte_in(0x3D5)&0xC0)|14);
port_byte_out(0x3D4,0xB);
port_byte_out(0x3D5,(port_byte_in(0x3D5)&0xE0)|15);
set_cursor(0,0);
vga_clear();
}
void vga_write_string(const char* string) {
for (size_t i=0;i<strlen(string);i++) {
char c=string[i];
if (c=='\n') {
x=0;
y++;
} else {
set_char(x,y,c);
x++;
}
if (x==width) {
x=0;
y++;
}
if (y==height) {
x=0;
y=0;
// char* pg1=(char*)((uint32_t)screen+0xfa0);
// memcpy(pg1,&screen[xy_to_indx(0,1)],xy_to_indx(0,24));
// vga_clear();
// memcpy(&screen,pg1,xy_to_indx(0,25));
}
}
set_cursor(x,y);
}
void vga_backspace() {
if (x!=0) {
x--;
set_char(x,y,' ');
set_cursor(x,y);
}
}

View File

@ -1,12 +1,12 @@
// #include "../cpu/cpu_init.h" // #include "../cpu/cpu_init.h"
// #include "../drivers/vga.h" #include "../drivers/vga.h"
// #include "../drivers/pci.h" // #include "../drivers/pci.h"
// #include "../drivers/serial.h" // #include "../drivers/serial.h"
// #include "../cpu/i386/ports.h" // #include "../cpu/i386/ports.h"
// #include "vfs.h" // #include "vfs.h"
// #include "../fs/devfs.h" // #include "../fs/devfs.h"
// #include "../fs/initrd.h" // #include "../fs/initrd.h"
// #include <grub/text_fb_info.h> #include <grub/text_fb_info.h>
// #include <stdlib.h> // #include <stdlib.h>
// #include <tasking.h> // #include <tasking.h>
// #include <string.h> // #include <string.h>
@ -138,17 +138,18 @@
void kmain(multiboot_info_t* header) { void kmain(multiboot_info_t* header) {
// mbd=header; // mbd=header;
// cpu_init(mbd); // cpu_init(mbd);
// text_fb_info info; text_fb_info info;
// if (header->flags&MULTIBOOT_INFO_FRAMEBUFFER_INFO&&header->framebuffer_type==2) { // if (header->flags&MULTIBOOT_INFO_FRAMEBUFFER_INFO&&header->framebuffer_type==2) {
// info.address=(char*)(((uint32_t)header->framebuffer_addr&0xFFFFFFFF)+0xC0000000); // info.address=(char*)(((uint32_t)header->framebuffer_addr&0xFFFFFFFF)+0xC0000000);
// info.width=header->framebuffer_width; // info.width=header->framebuffer_width;
// info.height=header->framebuffer_height; // info.height=header->framebuffer_height;
// } else { // } else {
// info.address=(char*)0xB8000; info.address=(char*)0xffff8000000B8000;
// info.width=80; info.width=80;
// info.height=25; info.height=25;
// } // }
// vga_init(info); vga_init(info);
vga_write_string("Hello long mode world!");
// createTask(init); // createTask(init);
// for (;;) { // for (;;) {
// yield(); // yield();