This commit is contained in:
nub31
2025-08-23 16:24:45 +02:00
parent d11c1072f9
commit 9be2f8a948
3 changed files with 79 additions and 53 deletions

View File

@@ -1,52 +1,6 @@
#include "print.h" #include "print.h"
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char* itoa(int value, char* result, int base)
{
// check that the base if valid
if (base < 2 || base > 36)
{
*result = '\0';
return result;
}
char *ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do
{
tmp_value = value;
value /= base;
*ptr++ =
"zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
} while (value);
// Apply negative sign
if (tmp_value < 0)
*ptr++ = '-';
*ptr-- = '\0';
// Reverse the string
while (ptr1 < ptr)
{
tmp_char = *ptr;
*ptr-- = *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
void kernel_init(void) void kernel_init(void)
{ {
for (int i = 0; i < 100; i++) print("Starting nub-os\n");
{
// char buf[10];
// itoa(i, buf, 10);
// kernel_print(buf);
kernel_print("Starting nub-osStarting");
}
} }

View File

@@ -1,6 +1,5 @@
#include "print.h" #include "print.h"
#include <stdint.h> #include <stdint.h>
#include <string.h>
#define ROWS 25 #define ROWS 25
#define COLUMNS 80 #define COLUMNS 80
@@ -47,14 +46,80 @@ typedef struct
uint8_t color; uint8_t color;
} vga_char; } vga_char;
static vga_char* print_buf = (vga_char*)BUF_START; static vga_char* vga_buffer = (vga_char*)BUF_START;
static int cursor_row = 0;
static int cursor_col = 0;
void kernel_print(const char* string) void scroll(void)
{ {
for (int row = 1; row < ROWS; row++)
{
for (int col = 0; col < COLUMNS; col++)
{
vga_buffer[(row - 1) * COLUMNS + col] = vga_buffer[row * COLUMNS + col];
}
}
for (int col = 0; col < COLUMNS; col++)
{
vga_buffer[(ROWS - 1) * COLUMNS + col] = (vga_char){ ' ', FG_WHITE | BG_BLACK << 4 };
}
}
void put_char(char c, uint8_t color)
{
if (c == '\n')
{
cursor_col = 0;
cursor_row++;
}
else
{
vga_buffer[cursor_row * COLUMNS + cursor_col] = (vga_char){ c, color };
cursor_col++;
}
if (cursor_col >= COLUMNS)
{
cursor_col = 0;
cursor_row++;
}
if (cursor_row >= ROWS)
{
scroll();
cursor_row = ROWS - 1;
}
}
void print(const char* string)
{
uint8_t color = FG_WHITE | BG_BLACK << 4;
for (int i = 0; string[i]; i++) for (int i = 0; string[i]; i++)
{ {
// todo(nub): scroll if overflowing put_char(string[i], color);
}
}
*print_buf++ = (vga_char){ string[i], FG_WHITE | BG_BLACK << 4 }; void clear_screen(void)
{
uint8_t color = FG_WHITE | BG_BLACK << 4;
for (int i = 0; i < ROWS * COLUMNS; i++)
{
vga_buffer[i] = (vga_char){ ' ', color };
}
cursor_row = 0;
cursor_col = 0;
}
void set_cursor_position(int row, int col)
{
if (row >= 0 && row < ROWS && col >= 0 && col < COLUMNS)
{
cursor_row = row;
cursor_col = col;
} }
} }

View File

@@ -1 +1,8 @@
#include <stdint.h>
void kernel_print(const char* string); void kernel_print(const char* string);
void put_char(char c, uint8_t color);
void print(const char* string);
void clear_screen(void);
void set_cursor_position(int row, int col);