simplified print

This commit is contained in:
nub31
2025-08-22 23:59:03 +02:00
parent 6db69608bb
commit e2dcdbeb53
3 changed files with 34 additions and 80 deletions

View File

@@ -3,5 +3,5 @@
void init(void)
{
print_init();
print("Starting nub-os\n", VGA_FG_WHITE, VGA_BG_BLACK);
println("Starting nub-os");
}

View File

@@ -10,85 +10,32 @@ void print_init(void)
vga_text_pos = (char*)0xb8000;
}
void print(char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color)
void print_newline(void)
{
vga_text_pos += 160 - ((vga_text_pos - (char*)0xb8000) % 160);
}
void print_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color)
{
for (int i = 0; string[i]; i++)
{
char c = string[i];
switch (c)
{
case '\n':
{
vga_text_pos += 160 - ((vga_text_pos - (char*)0xb8000) % 160);
break;
}
case '\r':
{
vga_text_pos -= ((vga_text_pos - (char*)0xb8000) % 160);
break;
}
case '\t':
{
int offset = (vga_text_pos - (char*)0xb8000) % 160;
int next_tab = ((offset / 16) + 1) * 16;
vga_text_pos += next_tab - offset;
break;
}
case '\b':
{
if (vga_text_pos > (char*)0xb8000)
{
vga_text_pos -= 2;
*vga_text_pos = ' ';
*(vga_text_pos + 1) = (bg_color << 4) | fg_color;
}
break;
}
case '\f':
{
char* start = (char*)0xb8000;
for (int j = 0; j < 80 * 25 * 2; j += 2)
{
start[j] = ' ';
start[j + 1] = (bg_color << 4) | fg_color;
}
vga_text_pos = (char*)0xb8000;
break;
}
case '\a':
{
break;
}
default:
{
*vga_text_pos++ = c;
*vga_text_pos++ = (bg_color << 4) | fg_color;
break;
}
}
if (vga_text_pos >= (char*)0xb8000 + VGA_WIDTH * VGA_HEIGHT * 2)
{
char* start = (char*)0xb8000;
for (int row = 1; row < VGA_HEIGHT; row++)
{
for (int col = 0; col < VGA_WIDTH * 2; col++)
{
start[(row - 1) * VGA_WIDTH * 2 + col] = start[row * VGA_WIDTH * 2 + col];
}
}
char* last_row = start + (VGA_HEIGHT - 1) * VGA_WIDTH * 2;
for (int col = 0; col < VGA_WIDTH * 2; col += 2)
{
last_row[col] = ' ';
last_row[col + 1] = (VGA_BG_BLACK << 4) | VGA_FG_WHITE;
}
vga_text_pos -= VGA_WIDTH * 2;
}
*vga_text_pos++ = string[i];
*vga_text_pos++ = (bg_color << 4) | fg_color;
}
}
void println_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color)
{
print_clr(string, fg_color, bg_color);
print_newline();
}
void print(const char* string)
{
print_clr(string, VGA_FG_WHITE, VGA_BG_BLACK);
}
void println(const char* string)
{
println_clr(string, VGA_FG_WHITE, VGA_BG_BLACK);
}

View File

@@ -30,5 +30,12 @@ enum VGA_BG_COLOR
VGA_BG_LIGHT_GRAY = 0x7,
};
void print(char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color);
void print_init(void);
void print_newline(void);
void print(const char* string);
void println(const char* string);
void print_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color);
void println_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color);