From 7a912d1b668ab86ffe088eca3ac7e6f78a04a0c5 Mon Sep 17 00:00:00 2001 From: Tyler Murphy <=> Date: Mon, 17 Jul 2023 19:34:52 -0400 Subject: refactoring --- kernel/src/term.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 kernel/src/term.c (limited to 'kernel/src/term.c') diff --git a/kernel/src/term.c b/kernel/src/term.c new file mode 100644 index 0000000..4b42a6a --- /dev/null +++ b/kernel/src/term.c @@ -0,0 +1,135 @@ + +#include +#include +#include +#include +#include + +#include "drivers/vga.h" +#include "term.h" + +uint16_t buffer[TERM_W * TERM_H * sizeof(uint16_t)]; +uint8_t x, y; +uint8_t color; + +const uint16_t blank = (uint16_t) 0 | VGA_BLACK << 12 | VGA_WHITE << 8; + +static void term_clear_line(int y) { + if (y < 0 || y >= TERM_H) + return; + for (uint8_t x = 0; x < TERM_W; x++) { + const size_t index = y * TERM_W + x; + buffer[index] = blank; + } +} + +void term_init (void) { + x = 0; + y = 0; + term_setfg(VGA_WHITE); + term_setbg(VGA_BLACK); + term_clear(); +} + +void term_setpos(uint8_t xp, uint8_t yp) { + x = xp; + y = yp; + vgatext_cur_mov(x, y); +} + +void term_scroll (int lines) { + arch_disable_int(); + y -= lines; + if (!lines) return; + if(lines >= TERM_H || lines <= -TERM_H) { + term_clear(); + } else if(lines > 0) { + memmove(buffer, buffer + lines * TERM_W, 2 * (TERM_H - lines) * TERM_W); + term_clear_line(TERM_H - lines); + } else { + memmove(buffer + lines * TERM_W, buffer + lines, (TERM_H + lines) * TERM_W); + } + arch_enable_int(); +} + +void term_setfg(enum vga_color c) { + color = (color & 0xF0) | c; +} + +void term_setbg(enum vga_color c) { + color = (color & 0x0F) | c << 4; +} + +void term_clear (void) { + for (uint8_t y = 0; y < TERM_H; y++) + term_clear_line(y); +} + +uint32_t term_save(void) { + uint32_t state = 0; + state |= (uint32_t) x << 16; + state |= (uint32_t) y << 8; + state |= (uint32_t) color << 0; + return state; +} + +void term_load(uint32_t state) { + x = (uint8_t) (state >> 16); + y = (uint8_t) (state >> 8); + color = (uint8_t) (state >> 0); + vgatext_cur_mov(x, y); +} + +uint16_t term_save_col(void) { + return color; +} + +void term_load_col(uint16_t c) { + color = c; +} + +void putchar(int c) { + 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 * TERM_W + x; + buffer[index] = c | (uint16_t) color << 8; + x++; + } + } + + if (x >= TERM_W) { + x = 0; + y++; + } + + if (y >= TERM_H) { + term_scroll(y - (TERM_H - 1)); + y = TERM_H - 1; + } + + vgatext_cur_mov(x, y); +} + +bool term_newline(void) { + return x == 0; +} + +void term_flush(void) { + arch_disable_int(); + vgatext_write_buf(buffer); + arch_enable_int(); +} -- cgit v1.2.3-freya