From 917ccf0465244e9081f42b9b61e1cc4e307f3042 Mon Sep 17 00:00:00 2001 From: 0x35c Date: Thu, 12 Dec 2024 23:14:20 +0100 Subject: [PATCH] wip: good text support (scrolling not quite working rn) --- .gitignore | 1 + headers/font.h | 2 + headers/terminal.h | 9 ++-- src/drivers/keyboard.c | 2 - src/kernel.c | 6 +-- src/terminal/put.c | 99 +++++++++++++++++++++++++----------------- 6 files changed, 70 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 6d4378c..33ec3ec 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ obj .* compile_commands.json tags +fonts diff --git a/headers/font.h b/headers/font.h index b214f0f..c6522e8 100644 --- a/headers/font.h +++ b/headers/font.h @@ -2,6 +2,8 @@ #include +#define FONT_SIZE 13 + struct font { uint32_t height; uint32_t width; diff --git a/headers/terminal.h b/headers/terminal.h index 9b0024f..2980218 100644 --- a/headers/terminal.h +++ b/headers/terminal.h @@ -5,9 +5,10 @@ #include #include -#define SCREEN_WIDTH 80 -#define SCREEN_HEIGHT 25 -#define TERM_BUF ((uint16_t *)0xC03FF000) +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 +#define SCREEN_WIDTH 1024 +#define SCREEN_HEIGHT 768 #define TERM_COUNT 10 struct screen { @@ -15,7 +16,7 @@ struct screen { size_t column; uint32_t fg_color; uint32_t bg_color; - uint16_t buffer[SCREEN_WIDTH * SCREEN_HEIGHT]; + uint16_t buffer[VGA_WIDTH * VGA_HEIGHT]; uint32_t default_color; char line[256]; }; diff --git a/src/drivers/keyboard.c b/src/drivers/keyboard.c index 2631449..30c7a03 100644 --- a/src/drivers/keyboard.c +++ b/src/drivers/keyboard.c @@ -5,8 +5,6 @@ #include #include -extern struct screen *screen; - static bool new_input_indicator = false; static struct key_event new_input = {}; diff --git a/src/kernel.c b/src/kernel.c index 3d46e62..0113d29 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -31,7 +31,7 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic) { - /* terminal_initialize(); */ + terminal_initialize(); init_gdt(); init_idt(); init_memory(mbd, magic); @@ -42,7 +42,7 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic) /* "complex 8*unknown quantity -byte descriptor table. -- Troy " */ /* "Martin 03:50, 22 March 2009 (UTC)\n"); */ - terminal_putchar('A'); + /* terminal_putchar('A'); */ /* memset(display.buff, 255, 1024 * 1024); */ - /* shell_init(); */ + shell_init(); } diff --git a/src/terminal/put.c b/src/terminal/put.c index ca07e17..34974ca 100644 --- a/src/terminal/put.c +++ b/src/terminal/put.c @@ -1,6 +1,8 @@ #include "ctype.h" +#include "debug.h" #include "font.h" -#include "fonts/eating_pasta_regular_32.h" +#include "fonts/eating_pasta_regular_13.h" +#include "fonts/minecraft_medium_13.h" #include "kprintf.h" #include "shell.h" #include "string.h" @@ -14,6 +16,7 @@ static struct screen screens[TERM_COUNT]; struct screen *screen = &screens[0]; +struct font *current_font = eating_pasta_regular_13_font; void terminal_initialize(void) { @@ -22,7 +25,8 @@ void terminal_initialize(void) screens[i].column = 0; screens[i].default_color = 0xffffff; screens[i].fg_color = screens[i].default_color; - memcpy(screens[i].buffer, TERM_BUF, sizeof(screen->buffer)); + /* memcpy(screens[i].buffer, TERM_BUF, sizeof(screen->buffer)); + */ } } @@ -65,39 +69,49 @@ uint32_t terminal_get_default_color(void) uint8_t terminal_get_char(int x, int y) { - return screen->buffer[y * SCREEN_WIDTH + x]; + return screen->buffer[y * VGA_WIDTH + x]; } -void terminal_putentryat(char c, uint32_t fg_color, uint32_t bg_color, size_t x, - size_t y) +void terminal_putentryat(struct font node, uint32_t fg_color, uint32_t bg_color, + size_t x, size_t y) { - struct font node = eating_pasta_regular_32_font[(int)c]; char *glyph = node.bitmap; for (size_t cy = 0; cy < node.height; cy++) { for (size_t cx = 0; cx < node.width; cx++) { - if (glyph[cy * node.width + cx] == '#') - put_pixel(fg_color, x + cx, y + cy); - else if (bg_color) - put_pixel(bg_color, x + cx, y + cy); + put_pixel((glyph[cy * node.width + cx] == '#') + ? fg_color + : bg_color, + x + cx, y + cy); + /* put_pixel((fg_color, x + cx, y + cy); */ + /* else if (bg_color) */ + /* put_pixel(bg_color, x + cx, y + cy); */ } } } +static struct font get_font_node(int c) +{ + return current_font[c]; +} + static void terminal_scroll(void) { - // TODO - /* screen->row--; */ - /* for (size_t i = 0; i < SCREEN_WIDTH * (SCREEN_HEIGHT - 1); i++) */ - /* TERM_BUF[i] = TERM_BUF[i + SCREEN_WIDTH]; */ - /* for (size_t i = 0; i < SCREEN_WIDTH; i++) */ - /* terminal_putentryat(' ', VGA_COLOR_WHITE, i, SCREEN_HEIGHT - 1); - */ + screen->row--; + for (size_t i = 0; i < VGA_WIDTH * (VGA_HEIGHT - 1); i++) { + const uint32_t x = (i % VGA_WIDTH) * FONT_SIZE; + const uint32_t y = (i / VGA_WIDTH) * FONT_SIZE; + terminal_putentryat(get_font_node(screen->buffer[i]), + screen->fg_color, screen->bg_color, x, y); + } + /* for (size_t i = 0; i < VGA_WIDTH; i++) */ + /* terminal_putentryat(get_font_node(' '), 0, 0, i * FONT_SIZE, */ + /* VGA_HEIGHT - 1); */ } static void terminal_new_line(void) { screen->column = 0; - if (++screen->row == SCREEN_HEIGHT) + if (++screen->row == VGA_HEIGHT) terminal_scroll(); } @@ -105,15 +119,18 @@ void terminal_change_default_color(uint32_t color) { // TODO /* terminal_set_color(color); */ - /* for (size_t y = 0; y < SCREEN_HEIGHT; y++) { */ - /* for (size_t x = 0; x < SCREEN_WIDTH; x++) { */ - /* const size_t index = y * SCREEN_WIDTH + x; */ - /* uint8_t entry_color = get_entry_color(TERM_BUF[index]); + /* for (size_t y = 0; y < VGA_HEIGHT; y++) { */ + /* for (size_t x = 0; x < VGA_WIDTH; x++) { */ + /* const size_t index = y * VGA_WIDTH + x; */ + /* uint8_t entry_color = + * get_entry_color(TERM_BUF[index]); */ /* TERM_BUF[index] = vga_entry( */ /* TERM_BUF[index], */ - /* entry_color == screen->default_color ? color */ - /* : entry_color); + /* entry_color == screen->default_color ? color + */ + /* : + * entry_color); */ /* } */ /* } */ @@ -131,10 +148,11 @@ void terminal_change_default_fg_color(uint32_t fg_color) void terminal_clear(void) { // TODO - /* for (size_t y = 0; y < SCREEN_HEIGHT; y++) { */ - /* for (size_t x = 0; x < SCREEN_WIDTH; x++) { */ - /* const size_t index = y * SCREEN_WIDTH + x; */ - /* TERM_BUF[index] = vga_entry(' ', screen->color); */ + /* for (size_t y = 0; y < VGA_HEIGHT; y++) { */ + /* for (size_t x = 0; x < VGA_WIDTH; x++) { */ + /* const size_t index = y * VGA_WIDTH + x; */ + /* TERM_BUF[index] = vga_entry(' ', screen->color); + */ /* } */ /* } */ /* memcpy(screen->buffer, TERM_BUF, sizeof(screen->buffer)); */ @@ -144,16 +162,17 @@ void terminal_clear(void) int terminal_putchar(char c) { - terminal_putentryat(c, 0xCCFFE5, 0xCCCC00, 10, 10); if (c == '\r') screen->column = 0; else if (c == '\n') terminal_new_line(); if (!isprint(c)) return 1; - terminal_putentryat(c, screen->fg_color, screen->bg_color, - screen->column, screen->row); - if (++screen->column == SCREEN_WIDTH) + screen->buffer[screen->column + screen->row * VGA_WIDTH] = c; + terminal_putentryat(get_font_node(c), screen->fg_color, + screen->bg_color, screen->column * FONT_SIZE, + screen->row * FONT_SIZE); + if (++screen->column == VGA_WIDTH) terminal_new_line(); return 1; } @@ -175,12 +194,12 @@ int terminal_writestring(const char *data) void update_cursor(void) { - uint16_t pos = screen->row * SCREEN_WIDTH + screen->column; + /* uint16_t pos = screen->row * VGA_WIDTH + screen->column; */ - outb(0x3D4, 0x0F); - outb(0x3D5, pos & 0xFF); - outb(0x3D4, 0x0E); - outb(0x3D5, (pos >> 8) & 0xFF); + /* outb(0x3D4, 0x0F); */ + /* outb(0x3D5, pos & 0xFF); */ + /* outb(0x3D4, 0x0E); */ + /* outb(0x3D5, (pos >> 8) & 0xFF); */ } void move_cursor(int direction) @@ -190,14 +209,14 @@ void move_cursor(int direction) if (screen->column) { screen->column--; } else if (screen->row) { - screen->column = SCREEN_WIDTH - 1; + screen->column = VGA_WIDTH - 1; screen->row--; } break; case RIGHT: - if (screen->column < SCREEN_WIDTH - 1) { + if (screen->column < VGA_WIDTH - 1) { screen->column++; - } else if (screen->row < SCREEN_HEIGHT - 1) { + } else if (screen->row < VGA_HEIGHT - 1) { screen->column = 0; screen->row++; } else {