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

74 lines
1.5 KiB

3 years ago
// VGA Graphics Library
#include "../kernel/io.h"
3 years ago
// VGA base address: 0xb8000
// Charpos = 0xb8000 + 2(row*80 + col)
3 years ago
// Memory
3 years ago
#define VGA_ADDRESS (char*)0xb8000
#define VGA_ADDRESS_MAX (char*)0xb8fa0
#define MAX_ROWS 25
#define MAX_COLS 80
3 years ago
3 years ago
// Global
3 years ago
static unsigned int cursor_row = 0;
static unsigned int cursor_col = 0;
3 years ago
/*
VGA & Memory Functions
*/
3 years ago
char* get_vga_charpos_pointer(unsigned int col, unsigned int row) {
3 years ago
return (char*)(VGA_ADDRESS + 2*((row*80) + col));
3 years ago
}
3 years ago
3 years ago
void writechar(char c, unsigned int col, unsigned int row, int attribute_byte) {
3 years ago
if( !attribute_byte )
attribute_byte = 0x0f;
3 years ago
char* mem = get_vga_charpos_pointer(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
}
/*
Graphics Functions
*/
void clear_screen() {
3 years ago
for( int c = 0; c < MAX_COLS; c++ )
for( int r = 0; r < MAX_ROWS; r++ )
writechar(0x20, c, r, 0x0);
3 years ago
}
void disable_vga_cursor() {
port_outb(0x0a, 0x3d4);
port_outb(0x20, 0x3d5);
}
3 years ago
/*
General Printing Functions
*/
3 years ago
void set_cursor_pos(unsigned int x, unsigned int y) {
cursor_col = x;
cursor_row = y;
3 years ago
}
3 years ago
void print(char* str, int attribute_byte) {
3 years ago
for( char* c = str; *c != '\0'; c++ )
3 years ago
writechar(*c, (unsigned int)(c - str) + cursor_col, cursor_row, attribute_byte);
3 years ago
}
3 years ago
void println(char* str, int attribute_byte) {
print(str, attribute_byte);
3 years ago
cursor_row++; // Increment to next y-pos (newline)
3 years ago
}
// VGA Initialization Function
void vga_init() {
disable_vga_cursor();
clear_screen();
}