5-malloc.md (#25)
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
index bef5c5a90213a1271c7d10cca1a7bdb6f67a2efb..e5a913f2c97256f76203c1ae29fa808c76596413 100644 (file)
-#include <libc/string.h>
+#include <klibc/string.h>
+#include <lunaix/spike.h>
 #include <lunaix/tty/tty.h>
 #include <stdint.h>
 
-#define TTY_WIDTH 80
-#define TTY_HEIGHT 25
+#include <sys/port_io.h>
 
-vga_attribute* buffer = (vga_attribute*)0xB8000;
+vga_attribute* tty_vga_buffer;
 
-vga_attribute theme_color = VGA_COLOR_BLACK;
+vga_attribute tty_theme_color = VGA_COLOR_BLACK;
 
-uint32_t tty_x = 0;
-uint16_t tty_y = 0;
+inline void
+tty_clear()
+{
+    asm volatile("rep stosw" ::"D"(tty_vga_buffer),
+                 "c"(TTY_HEIGHT * TTY_WIDTH),
+                 "a"(tty_theme_color)
+                 : "memory");
+}
 
 void
-tty_set_theme(vga_attribute fg, vga_attribute bg)
+tty_init(void* vga_buf)
 {
-    theme_color = (bg << 4 | fg) << 8;
+    tty_vga_buffer = (vga_attribute*)vga_buf;
+
+    tty_clear();
+
+    port_wrbyte(0x3D4, 0x0A);
+    port_wrbyte(0x3D5, (port_rdbyte(0x3D5) & 0xC0) | 13);
+
+    port_wrbyte(0x3D4, 0x0B);
+    port_wrbyte(0x3D5, (port_rdbyte(0x3D5) & 0xE0) | 15);
 }
 
 void
-tty_put_char(char chr)
+tty_set_cursor(u8_t x, u8_t y)
 {
-    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 (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
+        x = y = 0;
     }
+    u32_t pos = y * TTY_WIDTH + x;
+    port_wrbyte(0x3D4, 14);
+    port_wrbyte(0x3D5, pos / 256);
+    port_wrbyte(0x3D4, 15);
+    port_wrbyte(0x3D5, pos % 256);
+}
 
-    if (tty_x >= TTY_WIDTH) {
-        tty_x = 0;
-        tty_y++;
-    }
-    if (tty_y >= TTY_HEIGHT) {
-        tty_scroll_up();
-    }
+void
+tty_set_theme(vga_attribute fg, vga_attribute bg)
+{
+    tty_theme_color = (bg << 4 | fg) << 8;
 }
 
 void
-tty_put_str(char* str)
+tty_flush_buffer(struct fifo_buf* buf)
 {
-    while (*str != '\0') {
-        tty_put_char(*str);
-        str++;
+    int x = 0, y = 0;
+
+    // Clear screen
+    tty_clear();
+
+    char chr;
+    while (fifo_readone_async(buf, (u8_t*)&chr)) {
+        switch (chr) {
+            case '\t':
+                x += 4;
+                break;
+            case '\n':
+                y++;
+                // fall through
+            case '\r':
+                x = 0;
+                break;
+            default:
+                *(tty_vga_buffer + x + y * TTY_WIDTH) =
+                    (tty_theme_color | chr);
+                (x)++;
+                break;
+        }
+
+        if (x >= TTY_WIDTH) {
+            x = 0;
+            y++;
+        }
+        if (y >= TTY_HEIGHT) {
+            y--;
+            break;
+        }
     }
+
+    tty_set_cursor(x, y);
 }
 
 void
-tty_scroll_up()
+tty_clear_line(int line_num)
 {
-    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;
+    asm volatile("rep stosw" ::"D"(tty_vga_buffer + line_num * TTY_WIDTH),
+                 "c"(TTY_WIDTH),
+                 "a"(tty_theme_color)
+                 : "memory");
 }
 
 void
-tty_clear()
+tty_put_str_at(char* str, int x, int y)
 {
-    for (uint32_t i = 0; i < TTY_WIDTH * TTY_HEIGHT; i++) {
-        *(buffer + i) = theme_color;
+    char c;
+    while ((c = (*str)) && y < TTY_HEIGHT) {
+        *(tty_vga_buffer + x + y * TTY_WIDTH) = c | tty_theme_color;
+        x++;
+        if (x >= TTY_WIDTH) {
+            y++;
+            x = 0;
+        }
+        str++;
     }
-    tty_x = 0;
-    tty_y = 0;
 }
\ No newline at end of file