course 6 - interrupts
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
index cb4f1b4bc76cc69f8ea68a1e4ba2136bf0c0143a..bef5c5a90213a1271c7d10cca1a7bdb6f67a2efb 100644 (file)
@@ -1,58 +1,78 @@
+#include <libc/string.h>
 #include <lunaix/tty/tty.h>
 #include <stdint.h>
 
 #define TTY_WIDTH 80
 #define TTY_HEIGHT 25
 
-vga_atrributes *buffer = 0xB8000;
+vga_attribute* buffer = (vga_attribute*)0xB8000;
 
-vga_atrributes theme_color = VGA_COLOR_BLACK;
+vga_attribute theme_color = VGA_COLOR_BLACK;
 
-uint32_t TTY_COLUMN = 0;
-uint16_t TTY_ROW = 0;
+uint32_t tty_x = 0;
+uint16_t tty_y = 0;
 
-void tty_set_theme(vga_atrributes fg, vga_atrributes bg) {
+void
+tty_set_theme(vga_attribute fg, vga_attribute bg)
+{
     theme_color = (bg << 4 | fg) << 8;
 }
 
-void tty_put_char(char chr) {
-    if (chr == '\n') {
-        TTY_COLUMN = 0;
-        TTY_ROW++;
-    }
-    else if (chr == '\r') {
-        TTY_COLUMN = 0;
-    }
-    else {
-        *(buffer + TTY_COLUMN + TTY_ROW * TTY_WIDTH) = (theme_color | chr);
-        TTY_COLUMN++;
-        if (TTY_COLUMN >= TTY_WIDTH) {
-            TTY_COLUMN = 0;
-            TTY_ROW++;
-        }
+void
+tty_put_char(char chr)
+{
+    switch (chr) {
+        case '\t':
+            tty_x += 4;
+            break;
+        case '\n':
+            tty_x = 0;
+            tty_y++;
+            break;
+        case '\r':
+            tty_x = 0;
+            break;
+        default:
+            *(buffer + tty_x + tty_y * TTY_WIDTH) = (theme_color | chr);
+            tty_x++;
+            break;
     }
 
-    if (TTY_ROW >= TTY_HEIGHT) {
+    if (tty_x >= TTY_WIDTH) {
+        tty_x = 0;
+        tty_y++;
+    }
+    if (tty_y >= TTY_HEIGHT) {
         tty_scroll_up();
-        TTY_ROW--;
-    } 
+    }
 }
 
-void tty_put_str(char* str) {
+void
+tty_put_str(char* str)
+{
     while (*str != '\0') {
         tty_put_char(*str);
         str++;
     }
 }
 
-void tty_scroll_up() {
-    // TODO use memcpy
+void
+tty_scroll_up()
+{
+    size_t last_line = TTY_WIDTH * (TTY_HEIGHT - 1);
+    memcpy(buffer, buffer + TTY_WIDTH, last_line);
+    for (size_t i = 0; i < TTY_WIDTH; i++) {
+        *(buffer + i + last_line) = theme_color;
+    }
+    tty_y = tty_y == 0 ? 0 : tty_y - 1;
 }
 
-void tty_clear() {
-    for (uint32_t x = 0; x < TTY_WIDTH; x++) {
-        for (uint32_t y = 0; y < TTY_HEIGHT; y++) {
-            *(buffer + x + y * TTY_WIDTH) = theme_color;
-        }
+void
+tty_clear()
+{
+    for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
+        *(buffer + i) = theme_color;
     }
+    tty_x = 0;
+    tty_y = 0;
 }
\ No newline at end of file