refactor: use a more decent physical memory map
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
index b4913559410ffb7993a85fe04979ef5d240c5571..6f988552822e239af0d7903b3b71a12606c19816 100644 (file)
@@ -1,33 +1,49 @@
-#include <hal/io.h>
 #include <klibc/string.h>
-#include <lunaix/common.h>
 #include <lunaix/spike.h>
 #include <lunaix/tty/console.h>
 #include <lunaix/tty/tty.h>
 #include <stdint.h>
 
-vga_attribute* tty_vga_buffer = (vga_attribute*)VGA_BUFFER_PADDR;
+#include <sys/port_io.h>
+
+vga_attribute* tty_vga_buffer;
 
 vga_attribute tty_theme_color = VGA_COLOR_BLACK;
 
-#define TTY_CLEAR                                                              \
-    asm volatile("rep stosw" ::"D"(tty_vga_buffer),                            \
-                 "c"(TTY_HEIGHT * TTY_WIDTH),                                  \
-                 "a"(tty_theme_color)                                          \
+inline void
+tty_clear()
+{
+    asm volatile("rep stosw" ::"D"(tty_vga_buffer),
+                 "c"(TTY_HEIGHT * TTY_WIDTH),
+                 "a"(tty_theme_color)
                  : "memory");
+}
 
 void
 tty_init(void* vga_buf)
 {
     tty_vga_buffer = (vga_attribute*)vga_buf;
 
-    TTY_CLEAR
+    tty_clear();
+
+    port_wrbyte(0x3D4, 0x0A);
+    port_wrbyte(0x3D5, (port_rdbyte(0x3D5) & 0xC0) | 13);
 
-    io_outb(0x3D4, 0x0A);
-    io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
+    port_wrbyte(0x3D4, 0x0B);
+    port_wrbyte(0x3D5, (port_rdbyte(0x3D5) & 0xE0) | 15);
+}
 
-    io_outb(0x3D4, 0x0B);
-    io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
+void
+tty_set_cursor(u8_t x, u8_t y)
+{
+    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);
 }
 
 void
@@ -36,24 +52,20 @@ tty_set_theme(vga_attribute fg, vga_attribute bg)
     tty_theme_color = (bg << 4 | fg) << 8;
 }
 
-size_t
-tty_flush_buffer(char* data, size_t pos, size_t limit, size_t buf_size)
+void
+tty_flush_buffer(struct fifo_buf* buf)
 {
     int x = 0, y = 0;
 
     // Clear screen
-    TTY_CLEAR
+    tty_clear();
 
+    char chr;
     int state = 0;
     int g[2] = { 0, 0 };
     vga_attribute current_theme = tty_theme_color;
-    while (1) {
-        size_t ptr = pos % buf_size;
-        if (pos == limit) {
-            break;
-        }
-        char chr = data[pos];
-        if (state == 0 && chr == '\x033') {
+    while (fifo_readone_async(buf, (u8_t*)&chr)) {
+        if (state == 0 && chr == '\033') {
             state = 1;
         } else if (state == 1 && chr == '[') {
             state = 2;
@@ -84,11 +96,10 @@ tty_flush_buffer(char* data, size_t pos, size_t limit, size_t buf_size)
                 case '\r':
                     x = 0;
                     break;
-                case '\x08':
-                    x = x ? x - 1 : 0;
-                    *(tty_vga_buffer + x + y * TTY_WIDTH) =
-                      (current_theme | 0x20);
-                    break;
+                // case '\x08':
+                //     *(tty_vga_buffer + x + y * TTY_WIDTH) =
+                //       (current_theme | 0x20);
+                //     break;
                 default:
                     *(tty_vga_buffer + x + y * TTY_WIDTH) =
                       (current_theme | chr);
@@ -105,23 +116,8 @@ tty_flush_buffer(char* data, size_t pos, size_t limit, size_t buf_size)
                 break;
             }
         }
-        pos++;
     }
     tty_set_cursor(x, y);
-    return pos;
-}
-
-void
-tty_set_cursor(uint8_t x, uint8_t y)
-{
-    if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
-        x = y = 0;
-    }
-    uint32_t pos = y * TTY_WIDTH + x;
-    io_outb(0x3D4, 14);
-    io_outb(0x3D5, pos / 256);
-    io_outb(0x3D4, 15);
-    io_outb(0x3D5, pos % 256);
 }
 
 void