From 832aa58bb7fc2b942a31278fdabe6eab527989d8 Mon Sep 17 00:00:00 2001 From: Gitea Date: Tue, 1 Dec 2020 21:00:28 -0600 Subject: [PATCH] Working TTY --- arch/i386/tty.c | 82 ++++++++++++++++++++++++++++++++++++++++++++ arch/i386/vga.h | 33 ++++++++++++++++++ include/kernel/tty.h | 12 +++++++ 3 files changed, 127 insertions(+) create mode 100755 arch/i386/tty.c create mode 100755 arch/i386/vga.h create mode 100755 include/kernel/tty.h diff --git a/arch/i386/tty.c b/arch/i386/tty.c new file mode 100755 index 0000000..4c342f3 --- /dev/null +++ b/arch/i386/tty.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +#include + +#include "vga.h" + +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 24; +static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000 ; + +static size_t term_row; +static size_t term_col; +static uint8_t term_color; +static uint16_t* term_buf; + +void term_init(void) { + term_row = 0; + term_col = 0; + term_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); + term_buf = VGA_MEMORY; + for(size_t y = 0; y < VGA_HEIGHT; y++) { + for(size_t x = 0; x < VGA_WIDTH; x++) { + const size_t ind = y * VGA_WIDTH + x; + term_buf[ind] = vga_entry(' ', term_color); + } + } +} + +void term_scroll(void) { + for(size_t y = 0; y < VGA_HEIGHT; y++) { + for(size_t x = 0; x < VGA_WIDTH; x++) { + const size_t ind = y * VGA_WIDTH + x; + term_buf[ind] = term_buf[(y+1) * VGA_WIDTH + x]; + } + } + + for(size_t x = 0; x < VGA_WIDTH; x++) { + const size_t ind = VGA_HEIGHT * VGA_WIDTH + x; + term_buf[ind] = vga_entry(' ', term_color); + } +} + +void term_setcolor(uint8_t color) { + term_color = color; +} + +void term_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) { + const size_t ind = y * VGA_WIDTH + x; + term_buf[ind] = vga_entry(c, color); +} + +void term_putc(char c) { + unsigned char uc = c; + if(term_row > VGA_HEIGHT) { + term_scroll(); + term_row--; + } + switch(uc) { + case '\n': term_row++; term_col = 0; break; + default: term_putentryat(uc, term_color, term_col, term_row); break; + } + + if(uc != '\n' && ++term_col == VGA_WIDTH) { + term_col = 0; + if(++term_row == VGA_HEIGHT) { + term_scroll(); + } + } +} + +void term_write(const char* str, size_t size) { + for(size_t i = 0; i < size; i++) { + term_putc(str[i]); + } +} + +void term_writestr(const char* str) { + term_write(str, strlen(str)); +} diff --git a/arch/i386/vga.h b/arch/i386/vga.h new file mode 100755 index 0000000..f97e80e --- /dev/null +++ b/arch/i386/vga.h @@ -0,0 +1,33 @@ +#ifndef _ARCH_I386_VGA_H +#define _ARCH_I386_VGA_H + +#include + +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE= 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_WHITE = 15, +}; + +static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { + return fg | bg << 4; +} + +static inline uint16_t vga_entry(unsigned char uc, uint8_t color) { + return (uint16_t) uc | (uint16_t) color << 8; +} + +#endif diff --git a/include/kernel/tty.h b/include/kernel/tty.h new file mode 100755 index 0000000..35218b4 --- /dev/null +++ b/include/kernel/tty.h @@ -0,0 +1,12 @@ +#ifndef _KERN_TTY_H +#define _KERN_TTY_H + +#include + +void term_init(void); +void term_scroll(void); +void term_putc(char c); +void term_write(const char* str, size_t size); +void term_writestr(const char* str); + +#endif