From 439d724ce5939cab7a5c858d1829f212e01e0402 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sun, 1 Feb 2015 11:33:16 -0500 Subject: first real commit --- drivers/tty.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ drivers/tty.h | 9 +++++++ drivers/vga.h | 26 ++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 drivers/tty.c create mode 100644 drivers/tty.h create mode 100644 drivers/vga.h (limited to 'drivers') diff --git a/drivers/tty.c b/drivers/tty.c new file mode 100644 index 0000000..e938abd --- /dev/null +++ b/drivers/tty.c @@ -0,0 +1,86 @@ +#include +#include "io.h" +#include "tty.h" +#include "vga.h" + +static int term_x, term_y; +static uint8_t term_col; +/* VGA buffer starts at 0xB8000 */ +static uint16_t *term_buf; + +void tty_init(void) +{ + term_buf = (uint16_t*)0xB8000; + term_col = VGA_MAKE_COLOR(VGA_WHITE, VGA_BLACK); + tty_clear(); +} + +static void move_cursor(uint16_t cursor_idx) +{ + outb(0x3D4, 14); + outb(0x3D5, cursor_idx >> 8); // high byte + outb(0x3D4, 15); + outb(0x3D5, cursor_idx); // low byte +} + +static void update_cursor(void) +{ + move_cursor(term_y * VGA_WIDTH + term_x); +} + +void tty_clear(void) +{ + term_x = 0; + term_y = 0; + for(int y = 0; y < VGA_HEIGHT; ++y) + { + for(int x = 0; x < VGA_WIDTH; ++x) + { + term_buf[y * VGA_WIDTH + x] = VGA_MAKE_ENTRY(' ', term_col); + } + } +} + +void tty_set_color(uint8_t color) +{ + term_col = color; +} + +uint8_t tty_get_color(void) +{ + return term_col; +} + +void tty_putchar_at(char ch, uint8_t col, int x, int y) +{ + term_buf[y * VGA_WIDTH + x] = VGA_MAKE_ENTRY(ch, col); +} + +void tty_putchar(char ch) +{ + if(ch != '\n') + { + tty_putchar_at(ch, term_col, term_x, term_y); + if(++term_x == VGA_WIDTH) + { + term_x = 0; + if(++term_y == VGA_HEIGHT) + term_y = 0; + } + } + else + { + term_x = 0; + if(++term_y == VGA_HEIGHT) + term_y = 0; + } + update_cursor(); +} + +void tty_puts(const char *str) +{ + while(*str) + { + tty_putchar(*str++); + } +} diff --git a/drivers/tty.h b/drivers/tty.h new file mode 100644 index 0000000..0e142c6 --- /dev/null +++ b/drivers/tty.h @@ -0,0 +1,9 @@ +#include + +void tty_init(void); +void tty_clear(void); +void tty_set_color(uint8_t); +uint8_t tty_get_color(void); +void tty_putchar_at(char ch, uint8_t color, int x, int y); +void tty_putchar(char ch); +void tty_puts(const char*); diff --git a/drivers/vga.h b/drivers/vga.h new file mode 100644 index 0000000..bf13cba --- /dev/null +++ b/drivers/vga.h @@ -0,0 +1,26 @@ +#include + +enum vga_color_t { + VGA_BLACK = 0, + VGA_BLUE = 1, + VGA_GREEN = 2, + VGA_CYAN = 3, + VGA_RED = 4, + VGA_MAGENTA = 5, + VGA_BROWN = 6, + VGA_LIGHT_GRAY = 7, + VGA_DARK_GRAY = 8, + VGA_LIGHT_BLUE = 9, + VGA_LIGHT_GREEN = 10, + VGA_LIGHT_CYAN = 11, + VGA_LIGHT_RED = 12, + VGA_LIGHT_MAGENTA = 13, + VGA_LIGHT_BROWN = 14, + VGA_WHITE = 15 +}; + +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 + +#define VGA_MAKE_COLOR(fg, bg) (fg | bg << 4) +#define VGA_MAKE_ENTRY(ch, col) (((uint16_t)ch)|((uint16_t)col<<8)) -- cgit v1.1