A poorly written OS for the x86 arch. (WIP)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eOS/drivers/vga.c

101 lines
1.9 KiB

3 years ago
// VGA Graphics Library
3 years ago
#include "vga.h"
#include "../lib/types.h"
#include "../kernel/io.h"
3 years ago
#include "../kernel/memory.h"
#include "../lib/str.h"
3 years ago
#include "../lib/conv.h"
3 years ago
static uint cursor_row = 0;
static uint cursor_col = 0;
3 years ago
3 years ago
void vga_init() {
// Disable cursor
3 years ago
outb(0x3d4, 0x0a);
outb(0x3d5, 0x20);
3 years ago
// Clear screen
// clear_row(0);
3 years ago
// clear_screen();
3 years ago
3 years ago
//set_cursor_pos(0, 0);
3 years ago
}
3 years ago
/*
VGA & Memory Functions
*/
char* get_memory_charpos(uint col, uint row) {
3 years ago
return (char*)(VGA_ADDRESS + 2*((row*80) + col));
3 years ago
}
3 years ago
void writechar(char c, uint col, uint row, int attribute_byte) {
3 years ago
3 years ago
if( !attribute_byte )
attribute_byte = DEFAULT_COLOR;
3 years ago
3 years ago
char* mem = get_memory_charpos(col, row);
3 years ago
*mem = c; // Write the character
3 years ago
*(mem+1) = attribute_byte; // Write the attribute_byte
3 years ago
3 years ago
}
void set_cursor_pos(uint col, uint row) {
3 years ago
cursor_col = col;
cursor_row = row;
3 years ago
}
3 years ago
3 years ago
/*
Graphics Functions
*/
void clear_row(uint row) {
for( int c = 0; c < MAX_COLS; c++ )
writechar(0x20, c, row, 0x0);
}
void clear_screen() {
for( int r = 0; r < MAX_ROWS; r++ )
clear_row(r);
3 years ago
}
/*
General Printing Functions
*/
3 years ago
void print(char* str, int attribute_byte) {
3 years ago
for( char* c = str; *c != '\0'; c++ )
writechar(*c, (uint)(c - str) + cursor_col, cursor_row, attribute_byte);
3 years ago
cursor_col += strlen(str);
3 years ago
}
3 years ago
void new_line() {
cursor_row++;
cursor_col = 0;
}
3 years ago
void println(char* str, int attribute_byte) {
3 years ago
if( cursor_row >= MAX_ROWS ) {
set_cursor_pos(0, 0);
3 years ago
println(str, attribute_byte);
return;
}
3 years ago
print(str, attribute_byte);
3 years ago
new_line();
3 years ago
}
void printalign(char* str, int attribute_byte, enum align alignment) {
uint strlenbuf = strlen(str);
if( !alignment || alignment == LEFT ) {
set_cursor_pos(0, cursor_row);
} else if ( alignment == RIGHT ) {
set_cursor_pos(MAX_COLS - strlenbuf, cursor_row);
} else if ( alignment == MIDDLE ) {
set_cursor_pos((MAX_COLS/2) - (strlenbuf/2), cursor_row);
}
print(str, attribute_byte);
set_cursor_pos(0, cursor_row+1);
}