myos/console/console.c
2025-05-02 16:20:25 -04:00

99 lines
2.4 KiB
C

#include "console.h"
#include "portmap.h"
#include <stdint.h>
const int VGA_WIDTH = 80;
const int VGA_HEIGHT = 25;
const int VGA_BYTES_PER_CHARACTER = 2;
char* const VGA_BUFFER = (char*) 0xb8000;
static int terminal_position = 0;
static VGA_Color terminal_font_color = LIGHT_GRAY; // Default font color will be light gray
static VGA_Color terminal_background_color = BLACK; // Default background color is black
void set_terminal_font_color(VGA_Color col) {
terminal_font_color = col;
}
void set_terminal_background_color(VGA_Color col) {
terminal_background_color = col;
}
void clear_terminal() {
for (int i=0; i<(VGA_WIDTH * VGA_HEIGHT); i++) {
// Text byte gets nulled
VGA_BUFFER[i*2] = '\0';
// Style byte gets 0x07 for white text on a black background?
VGA_BUFFER[i*2 + 1] = 0x07;
}
}
void print_character(char c) {
print_character_with_color(c, terminal_background_color, terminal_font_color);
}
void print_character_with_color(char c, VGA_Color bg, VGA_Color fg) {
if (c == '\n') {
terminal_position = (terminal_position + ((VGA_BYTES_PER_CHARACTER / 2 * VGA_WIDTH)) - (terminal_position % (VGA_BYTES_PER_CHARACTER / 2 * VGA_WIDTH)));
} else if (c == '\b') {
terminal_position -= 1;
VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER)] = ' ';
int full_color = (bg << 4) | fg;
VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER) + 1] = full_color;
} else {
VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER)] = c;
int full_color = (bg << 4) | fg;
VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER) + 1] = full_color;
terminal_position++;
}
update_cursor();
}
void print_string(char* string) {
print_string_with_color(string, terminal_background_color, terminal_font_color);
}
void print_string_with_color(char* string, VGA_Color bg, VGA_Color fg) {
for (int i=0; string[i] != '\0'; i++) {
print_character_with_color(string[i], bg, fg);
}
}
void print_line(char* string) {
print_string(string);
print_character('\n');
}
void print_line_with_color(char* string, VGA_Color bg, VGA_Color fg) {
print_string_with_color(string, bg, fg);
print_character('\n');
}
void update_cursor() {
uint16_t cursor_position = terminal_position >> 0;
outb(0x3D4, 0x0F);
outb(0x3D5, (uint8_t) (cursor_position));
outb(0x3D4, 0x0E);
outb(0x3D5, (uint8_t) (cursor_position >> 8));
}