os/vga_drv/vga.c

63 lines
1.6 KiB
C
Raw Normal View History

2019-08-31 11:04:30 -05:00
#include "vga.h"
2020-07-23 11:50:23 -05:00
#include <cpu/ports.h>
2019-08-31 11:04:30 -05:00
#include <string.h>
#include <stddef.h>
2019-09-06 15:36:36 -05:00
#include "vtconsole.h"
2019-08-31 11:04:30 -05:00
#define xy_to_indx(x,y) ((x+(y*width))*2)
2019-09-06 15:36:36 -05:00
static vga_color colors[]={VGA_BLACK,VGA_RED,VGA_GREEN,VGA_BROWN,
VGA_BLUE,VGA_PURPLE,VGA_CYAN,VGA_WHITE};
2019-08-31 11:04:30 -05:00
static char* screen;
static int width;
static int height;
2019-09-06 15:36:36 -05:00
static vtconsole_t* console;
2019-08-31 11:04:30 -05:00
2019-09-06 15:36:36 -05:00
static void set_char(int x,int y,char ch,char style) {
screen[xy_to_indx(x,y)]=ch;
screen[xy_to_indx(x,y)+1]=style;
}
static void vt_set_char(struct vtconsole* vtc, vtcell_t* cell, int x, int y) {
vga_color fg_color=colors[cell->attr.fg];
vga_color bg_color=colors[cell->attr.bg];
char style=(fg_color&0xF)|((bg_color&0xF)<<4);
style=style|((cell->attr.bright&0x1)<<7);
set_char(x,y,cell->c,style);
2019-08-31 11:04:30 -05:00
}
void vga_clear() {
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
2019-09-06 15:36:36 -05:00
set_char(x,y,' ',0);
2019-08-31 11:04:30 -05:00
}
}
}
2019-09-06 15:36:36 -05:00
static void set_cursor(struct vtconsole* vtc, vtcursor_t* cur) {
int pos=(cur->x+(cur->y*width));
2019-08-31 11:04:30 -05:00
port_byte_out(0x3D4,0xF);
port_byte_out(0x3D5,pos&0xFF);
port_byte_out(0x3D4,0xE);
port_byte_out(0x3D5,(pos&0xFF00)>>8);
}
2020-07-23 11:50:23 -05:00
void vga_init() {
screen=map_phys((void*)0xB8000,10);
width=80;
height=25;
2019-09-06 15:36:36 -05:00
console=vtconsole(width,height,vt_set_char,set_cursor);
2019-08-31 11:04:30 -05: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);
2019-09-06 15:36:36 -05:00
vtcursor_t cur;
cur.x=0;
cur.y=0;
set_cursor(NULL,&cur);
2019-08-31 11:04:30 -05:00
vga_clear();
}
void vga_write_string(const char* string) {
2019-09-06 15:36:36 -05:00
vtconsole_write(console,string,strlen(string));
2019-08-31 11:04:30 -05:00
}