5-malloc.md (#25)
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
index 6db2417a7d81c5524e2c66f0c3460c3aeed4ab11..e5a913f2c97256f76203c1ae29fa808c76596413 100644 (file)
@@ -1,12 +1,11 @@
-#include <hal/io.h>
 #include <klibc/string.h>
 #include <klibc/string.h>
-#include <lunaix/common.h>
 #include <lunaix/spike.h>
 #include <lunaix/spike.h>
-#include <lunaix/tty/console.h>
 #include <lunaix/tty/tty.h>
 #include <stdint.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;
 
 
 vga_attribute tty_theme_color = VGA_COLOR_BLACK;
 
@@ -26,11 +25,24 @@ tty_init(void* vga_buf)
 
     tty_clear();
 
 
     tty_clear();
 
-    io_outb(0x3D4, 0x0A);
-    io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
+    port_wrbyte(0x3D4, 0x0A);
+    port_wrbyte(0x3D5, (port_rdbyte(0x3D5) & 0xC0) | 13);
 
 
-    io_outb(0x3D4, 0x0B);
-    io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
+    port_wrbyte(0x3D4, 0x0B);
+    port_wrbyte(0x3D5, (port_rdbyte(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
 }
 
 void
@@ -48,77 +60,35 @@ tty_flush_buffer(struct fifo_buf* buf)
     tty_clear();
 
     char chr;
     tty_clear();
 
     char chr;
-    int state = 0;
-    int g[2] = { 0, 0 };
-    vga_attribute current_theme = tty_theme_color;
-    while (fifo_readone_async(buf, &chr)) {
-        if (state == 0 && chr == '\033') {
-            state = 1;
-        } else if (state == 1 && chr == '[') {
-            state = 2;
-        } else if (state > 1) {
-            if ('0' <= chr && chr <= '9') {
-                g[state - 2] = (chr - '0') + g[state - 2] * 10;
-            } else if (chr == ';' && state == 2) {
-                state = 3;
-            } else {
-                if (g[0] == 39 && g[1] == 49) {
-                    current_theme = tty_theme_color;
-                } else {
-                    current_theme = (g[1] << 4 | g[0]) << 8;
-                }
-                g[0] = 0;
-                g[1] = 0;
-                state = 0;
-            }
-        } else {
-            state = 0;
-            switch (chr) {
-                case '\t':
-                    x += 4;
-                    break;
-                case '\n':
-                    y++;
-                    // fall through
-                case '\r':
-                    x = 0;
-                    break;
-                case '\x08':
-                    x = x ? x - 1 : 0;
-                    *(tty_vga_buffer + x + y * TTY_WIDTH) =
-                      (current_theme | 0x20);
-                    break;
-                default:
-                    *(tty_vga_buffer + x + y * TTY_WIDTH) =
-                      (current_theme | chr);
-                    (x)++;
-                    break;
-            }
-
-            if (x >= TTY_WIDTH) {
-                x = 0;
+    while (fifo_readone_async(buf, (u8_t*)&chr)) {
+        switch (chr) {
+            case '\t':
+                x += 4;
+                break;
+            case '\n':
                 y++;
                 y++;
-            }
-            if (y >= TTY_HEIGHT) {
-                y--;
+                // fall through
+            case '\r':
+                x = 0;
+                break;
+            default:
+                *(tty_vga_buffer + x + y * TTY_WIDTH) =
+                    (tty_theme_color | chr);
+                (x)++;
                 break;
                 break;
-            }
         }
         }
-    }
-    tty_set_cursor(x, y);
-}
 
 
-void
-tty_set_cursor(uint8_t x, uint8_t y)
-{
-    if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
-        x = y = 0;
+        if (x >= TTY_WIDTH) {
+            x = 0;
+            y++;
+        }
+        if (y >= TTY_HEIGHT) {
+            y--;
+            break;
+        }
     }
     }
-    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);
+
+    tty_set_cursor(x, y);
 }
 
 void
 }
 
 void