diff options
Diffstat (limited to 'kernel/tty.c')
-rw-r--r-- | kernel/tty.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/kernel/tty.c b/kernel/tty.c new file mode 100644 index 0000000..4cb2cf1 --- /dev/null +++ b/kernel/tty.c @@ -0,0 +1,83 @@ +#include "tty.h" + +/* Hardware text mode color constants. */ +enum vga_color { + COLOR_BLACK = 0, + COLOR_BLUE = 1, + COLOR_GREEN = 2, + COLOR_CYAN = 3, + COLOR_RED = 4, + COLOR_MAGENTA = 5, + COLOR_BROWN = 6, + COLOR_LIGHT_GREY = 7, + COLOR_DARK_GREY = 8, + COLOR_LIGHT_BLUE = 9, + COLOR_LIGHT_GREEN = 10, + COLOR_LIGHT_CYAN = 11, + COLOR_LIGHT_RED = 12, + COLOR_LIGHT_MAGENTA = 13, + COLOR_LIGHT_BROWN = 14, + COLOR_WHITE = 15, +}; + +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 +#define VGA_TEXT_BUFFER 0xB8000 +#define VGA_COLOR_SHIFT 8 + +static size_t tty_row; +static size_t tty_col; +static uint8_t tty_color; +static uint16_t* tty_buffer; + +static inline uint8_t make_color(enum vga_color fg, enum vga_color bg) +{ + return fg | bg << 4; +} + +static inline uint16_t make_vgaentry(char c, uint8_t color) +{ + return ((uint16_t) c) | (((uint16_t) color) << VGA_COLOR_SHIFT); +} + +static inline void tty_putentryat(char c, uint8_t color, size_t x, size_t y) +{ + const size_t i = y * VGA_WIDTH + x; + tty_buffer[i] = make_vgaentry(c, color); +} + +void tty_setcolor(uint8_t color) +{ + tty_color = color; +} + +void tty_putchar(char c) +{ + tty_putentryat(c, tty_color, tty_col, tty_row); + if (++tty_col == VGA_WIDTH) { + tty_col = 0; + if (++tty_row == VGA_HEIGHT) { + tty_row = 0; + } + } +} + +void tty_writestring(const char* data) +{ + for (const char* p = data; *p != 0; p++) + tty_putchar(*p); +} + +void tty_init() +{ + tty_row = 0; + tty_col = 0; + tty_buffer = (uint16_t*) VGA_TEXT_BUFFER; + tty_setcolor(make_color(COLOR_LIGHT_GREY, COLOR_BLACK)); + for (size_t y = 0; y < VGA_HEIGHT; y++) { + for (size_t x = 0; x < VGA_WIDTH; x++) { + const size_t i = y * VGA_WIDTH + x; + tty_buffer[i] = make_vgaentry(' ', tty_color); + } + } +} |