diff options
-rw-r--r-- | kernel/drivers/term.c | 105 | ||||
-rw-r--r-- | kernel/drivers/tty.c | 14 | ||||
-rw-r--r-- | kernel/include/comus/drivers/term.h | 37 | ||||
-rw-r--r-- | kernel/include/comus/drivers/tty.h | 22 | ||||
-rw-r--r-- | kernel/lib/kprintf.c | 6 |
5 files changed, 152 insertions, 32 deletions
diff --git a/kernel/drivers/term.c b/kernel/drivers/term.c new file mode 100644 index 0000000..e9d3e50 --- /dev/null +++ b/kernel/drivers/term.c @@ -0,0 +1,105 @@ +#include <lib.h> +#include <comus/drivers/tty.h> +#include <comus/asm.h> +#include <comus/memory.h> + +#define VGA_ADDR 0xB8000 + +static const uint8_t width = 80; +static const uint8_t height = 25; +static volatile uint16_t *buffer = (uint16_t *)VGA_ADDR; + +// position +static uint32_t x = 0, y = 0; + +// color +static uint8_t fg = 15, bg = 0; + +// blank color +const uint16_t blank = (uint16_t)0 | 0 << 12 | 15 << 8; + +static void term_clear_line(int line) +{ + if (line < 0 || line >= height) + return; + for (uint8_t x = 0; x < width; x++) { + const size_t index = line * width + x; + buffer[index] = blank; + } +} + +static void term_clear(void) +{ + for (uint8_t y = 0; y < height; y++) + term_clear_line(y); +} + +static void term_scroll(int lines) +{ + cli(); + y -= lines; + if (!lines) + return; + if (lines >= height || lines <= -height) { + term_clear(); + } else if (lines > 0) { + memmovev(buffer, buffer + lines * width, 2 * (height - lines) * width); + term_clear_line(height - lines); + } else { + memmovev(buffer + lines * width, buffer + lines, + (height + lines) * width); + } + sti(); +} + +void tty_out(char c) +{ + if (buffer == NULL) + return; + + // handle special chars + switch (c) { + case '\n': + x = 0; + y++; + break; + case '\t': + x += 4; + break; + case '\v': + case '\f': + y++; + break; + case '\r': + x = 0; + break; + default: { + const size_t index = y * width + x; + buffer[index] = c | bg << 12 | fg << 8; + x++; + } + } + + // wrap cursor if needed + if (x >= width) { + x = 0; + y++; + } + if (y >= height) { + term_scroll(y - (height - 1)); + y = height - 1; + } + + // set cursor position on screen + const uint16_t pos = y * width + x; + outb(0x3D4, 0x0F); + outb(0x3D5, (uint8_t)(pos & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); +} + +void tty_out_str(const char *str) +{ + while (*str) + tty_out(*str++); +} diff --git a/kernel/drivers/tty.c b/kernel/drivers/tty.c index e9d3e50..d1cd553 100644 --- a/kernel/drivers/tty.c +++ b/kernel/drivers/tty.c @@ -1,5 +1,5 @@ #include <lib.h> -#include <comus/drivers/tty.h> +#include <comus/drivers/term.h> #include <comus/asm.h> #include <comus/memory.h> @@ -18,7 +18,7 @@ static uint8_t fg = 15, bg = 0; // blank color const uint16_t blank = (uint16_t)0 | 0 << 12 | 15 << 8; -static void term_clear_line(int line) +void term_clear_line(int line) { if (line < 0 || line >= height) return; @@ -28,13 +28,13 @@ static void term_clear_line(int line) } } -static void term_clear(void) +void term_clear(void) { for (uint8_t y = 0; y < height; y++) term_clear_line(y); } -static void term_scroll(int lines) +void term_scroll(int lines) { cli(); y -= lines; @@ -52,7 +52,7 @@ static void term_scroll(int lines) sti(); } -void tty_out(char c) +void term_out(char c) { if (buffer == NULL) return; @@ -98,8 +98,8 @@ void tty_out(char c) outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); } -void tty_out_str(const char *str) +void term_out_str(const char *str) { while (*str) - tty_out(*str++); + term_out(*str++); } diff --git a/kernel/include/comus/drivers/term.h b/kernel/include/comus/drivers/term.h new file mode 100644 index 0000000..ad55bfd --- /dev/null +++ b/kernel/include/comus/drivers/term.h @@ -0,0 +1,37 @@ +/** + * @file tty.h + * + * @author Freya Murphy <freya@freyacat.org> + * + * Terminal output + */ + +#ifndef TERM_H_ +#define TERM_H_ + +/** + * Output a character to the terminal + */ +void term_out(char c); + +/** + * Output a string to the terminal + */ +void term_out_str(const char *str); + +/** + * Clear all lines on the terminal + */ +void term_clear(void); + +/** + * Clear a specifc line on the terminal terminal + */ +void term_clear_line(int line); + +/** + * Scroll terminal + */ +void term_scroll(int lines); + +#endif /* tty.h */ diff --git a/kernel/include/comus/drivers/tty.h b/kernel/include/comus/drivers/tty.h deleted file mode 100644 index 55aec64..0000000 --- a/kernel/include/comus/drivers/tty.h +++ /dev/null @@ -1,22 +0,0 @@ -/** - * @file tty.h - * - * @author Freya Murphy <freya@freyacat.org> - * - * Terminal output - */ - -#ifndef TTY_H_ -#define TTY_H_ - -/** - * Output a character to the terminal - */ -void tty_out(char c); - -/** - * Output a string to the terminal - */ -void tty_out_str(const char *str); - -#endif /* tty.h */ diff --git a/kernel/lib/kprintf.c b/kernel/lib/kprintf.c index a76036f..40cf819 100644 --- a/kernel/lib/kprintf.c +++ b/kernel/lib/kprintf.c @@ -1,6 +1,6 @@ #include <lib.h> #include <comus/drivers/uart.h> -#include <comus/drivers/tty.h> +#include <comus/drivers/term.h> #define PRINTF_NUMERIC_BUF_LEN 50 @@ -565,12 +565,12 @@ size_t kvsnprintf(char *restrict s, size_t maxlen, const char *format, void kputc(char c) { - tty_out(c); + term_out(c); uart_out(c); } void kputs(const char *str) { - tty_out_str(str); + term_out_str(str); uart_out_str(str); } |