Files
nub-os/src/vga.c
nub31 1bc122e29a ...
2025-09-01 19:18:38 +02:00

150 lines
2.7 KiB
C

#include "vga.h"
#include "string.h"
#define ROWS 25
#define COLUMNS 80
typedef struct
{
u8 character;
u8 color;
} vga_char;
vga_char* vga_buffer = (vga_char*)0xb8000;
u8 cursor_row = 0;
u8 cursor_col = 0;
void vga_set_char(u8 row, u8 col, vga_char character)
{
vga_buffer[COLUMNS * row + col] = character;
}
vga_char vga_char_at(u8 row, u8 col)
{
return vga_buffer[COLUMNS * row + col];
}
void vga_clear()
{
for (u8 row = 0; row < ROWS; row++)
{
for (u8 col = 0; col < COLUMNS; col++)
{
vga_char character = {
.character = ' ',
.color = vga_default_color(),
};
vga_set_char(row, col, character);
}
}
cursor_row = 0;
cursor_col = 0;
}
void vga_set_cursor_position(u8 row, u8 col)
{
if (row < ROWS && col < COLUMNS)
{
cursor_row = row;
cursor_col = col;
}
}
void vga_print_char_colored(char character, vga_color_t color)
{
switch (character)
{
case '\n':
{
cursor_row += 1;
cursor_col = 0;
break;
}
case '\r':
{
cursor_col = 0;
break;
}
case '\t':
{
u8 remainter = cursor_col % 4;
cursor_col += remainter == 0 ? 4 : remainter;
break;
}
default:
{
vga_char c = {
.character = character,
.color = color,
};
vga_set_char(cursor_row, cursor_col, c);
cursor_col += 1;
break;
}
}
if (cursor_col >= COLUMNS)
{
cursor_col = 0;
cursor_row += 1;
}
if (cursor_row >= ROWS)
{
for (u8 row = 1; row < ROWS; row++)
{
for (u8 col = 0; col < COLUMNS; col++)
{
vga_set_char(row - 1, col, vga_char_at(row, col));
}
}
for (u8 col = 0; col < COLUMNS; col++)
{
vga_char c = {
.character = ' ',
.color = vga_default_color(),
};
vga_set_char(ROWS - 1, col, c);
};
cursor_row = ROWS - 1;
}
}
void vga_print_colored(const char* string, vga_color_t color)
{
for (u8 i = 0; string[i] != '\0'; i++)
{
vga_print_char_colored(string[i], color);
}
}
void vga_print_success()
{
vga_print("[ ");
vga_print_colored("success", VGA_GREEN);
vga_print(" ]");
}
void vga_print_error()
{
vga_print("[ ");
vga_print_colored("error", VGA_RED);
vga_print(" ]");
}
void vga_print_uint(u32 value)
{
char buffer[11];
uitoa(value, buffer);
vga_print(buffer);
}
void vga_print_int(i32 value)
{
char buffer[12];
itoa(value, buffer);
vga_print(buffer);
}