From 7c06c75d6cf463d281f5269afe8e34d720ed9114 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 15 Nov 2022 09:26:14 +0200 Subject: [PATCH] Move cursor in terminal when writing to it --- kernel/arch/i386/tty.cpp | 24 ++++++++++++++++++++++++ kernel/include/kernel/tty.h | 1 + 2 files changed, 25 insertions(+) diff --git a/kernel/arch/i386/tty.cpp b/kernel/arch/i386/tty.cpp index 9c8af8b6c2..d723227815 100644 --- a/kernel/arch/i386/tty.cpp +++ b/kernel/arch/i386/tty.cpp @@ -56,6 +56,28 @@ void terminal_clear_line(size_t line) terminal_putentryat(' ', terminal_color, x, line); } + +static inline void outb(uint16_t port, uint8_t value) +{ + asm volatile ("outb %0, %1" : : "a" (value), "Nd" (port)); +} +static void terminal_update_cursor() +{ + uint16_t pos = terminal_row * VGA_WIDTH + terminal_col; + + outb(0x3D4, 0x0F); + outb(0x3D5, (uint8_t) (pos & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF)); +} + +void terminal_set_cursor_pos(int x, int y) +{ + terminal_row = y; + terminal_col = x; + terminal_update_cursor(); +} + void terminal_putchar(char c) { if (c == '\n') @@ -84,6 +106,8 @@ void terminal_putchar(char c) terminal_col = 0; terminal_row = VGA_HEIGHT - 1; } + + terminal_update_cursor(); } void terminal_write(const char* data, size_t size) diff --git a/kernel/include/kernel/tty.h b/kernel/include/kernel/tty.h index eb318d5f66..15f5fa7b79 100644 --- a/kernel/include/kernel/tty.h +++ b/kernel/include/kernel/tty.h @@ -8,3 +8,4 @@ void terminal_initialize(); void terminal_putchar(char c); void terminal_write(const char* data, size_t size); void terminal_writestring(const char* data); +void terminal_set_cursor_pos(int x, int y); \ No newline at end of file