os/drivers/i386/vga.c

85 lines
1.7 KiB
C
Raw Normal View History

2019-02-09 14:35:29 -06:00
#include <grub/text_fb_info.h>
#include "../vga.h"
2019-02-25 15:42:23 -06:00
#include "../../cpu/i386/ports.h"
2019-02-09 17:55:06 -06:00
#include <string.h>
2019-02-25 15:42:23 -06:00
#include <stddef.h>
2019-02-10 14:13:41 -06:00
#define xy_to_indx(x,y) ((x+(y*width))*2)
2019-03-11 09:32:55 -05:00
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;
2019-02-09 14:35:29 -06:00
2019-03-11 09:32:55 -05:00
static void set_char(int x,int y,char c) {
2019-02-09 17:22:51 -06:00
screen[xy_to_indx(x,y)]=c;
screen[xy_to_indx(x,y)+1]=(bg_color<<4)|fg_color;
}
2019-02-11 09:28:25 -06:00
void vga_clear() {
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
2019-03-11 09:32:55 -05:00
set_char(x,y,' ');
2019-02-11 09:28:25 -06:00
}
}
}
2019-03-11 09:32:55 -05:00
static void set_cursor(int x,int y) {
2019-02-11 09:28:25 -06:00
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);
}
2019-02-09 14:35:29 -06:00
void vga_init(text_fb_info framebuffer_info) {
2019-02-09 17:55:06 -06:00
x=0;
y=0;
2019-02-09 17:15:38 -06:00
fg_color=VGA_WHITE;
bg_color=VGA_BLACK;
2019-02-09 14:35:29 -06:00
screen=framebuffer_info.address;
width=framebuffer_info.width;
height=framebuffer_info.height;
2019-02-11 09:28:25 -06:00
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();
}
2019-02-09 17:55:06 -06:00
void vga_write_string(const char* string) {
2019-02-25 15:42:23 -06:00
for (size_t i=0;i<strlen(string);i++) {
2019-02-09 17:55:06 -06:00
char c=string[i];
if (c=='\n') {
x=0;
y++;
} else {
2019-03-11 09:32:55 -05:00
set_char(x,y,c);
2019-02-09 17:55:06 -06:00
x++;
}
if (x==width) {
x=0;
y++;
}
if (y==height) {
x=0;
y=0;
2019-02-25 15:42:23 -06:00
// 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));
2019-02-09 17:55:06 -06:00
}
}
2019-02-11 09:28:25 -06:00
set_cursor(x,y);
2019-02-09 17:55:06 -06:00
}
2019-03-11 09:32:55 -05:00
void vga_backspace() {
if (x!=0) {
x--;
set_char(x,y,' ');
set_cursor(x,y);
}
}