feat: (ahci) support multiple AHCI controller
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
1 #include <hal/io.h>
2 #include <klibc/string.h>
3 #include <lunaix/common.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/tty/console.h>
6 #include <lunaix/tty/tty.h>
7 #include <stdint.h>
8
9 vga_attribute* tty_vga_buffer;
10
11 vga_attribute tty_theme_color = VGA_COLOR_BLACK;
12
13 inline void
14 tty_clear()
15 {
16     asm volatile("rep stosw" ::"D"(tty_vga_buffer),
17                  "c"(TTY_HEIGHT * TTY_WIDTH),
18                  "a"(tty_theme_color)
19                  : "memory");
20 }
21
22 void
23 tty_init(void* vga_buf)
24 {
25     tty_vga_buffer = (vga_attribute*)vga_buf;
26
27     tty_clear();
28
29     io_outb(0x3D4, 0x0A);
30     io_outb(0x3D5, (io_inb(0x3D5) & 0xC0) | 13);
31
32     io_outb(0x3D4, 0x0B);
33     io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
34 }
35
36 void
37 tty_set_theme(vga_attribute fg, vga_attribute bg)
38 {
39     tty_theme_color = (bg << 4 | fg) << 8;
40 }
41
42 void
43 tty_flush_buffer(struct fifo_buf* buf)
44 {
45     int x = 0, y = 0;
46
47     // Clear screen
48     tty_clear();
49
50     char chr;
51     int state = 0;
52     int g[2] = { 0, 0 };
53     vga_attribute current_theme = tty_theme_color;
54     while (fifo_readone_async(buf, &chr)) {
55         if (state == 0 && chr == '\033') {
56             state = 1;
57         } else if (state == 1 && chr == '[') {
58             state = 2;
59         } else if (state > 1) {
60             if ('0' <= chr && chr <= '9') {
61                 g[state - 2] = (chr - '0') + g[state - 2] * 10;
62             } else if (chr == ';' && state == 2) {
63                 state = 3;
64             } else {
65                 if (g[0] == 39 && g[1] == 49) {
66                     current_theme = tty_theme_color;
67                 } else {
68                     current_theme = (g[1] << 4 | g[0]) << 8;
69                 }
70                 g[0] = 0;
71                 g[1] = 0;
72                 state = 0;
73             }
74         } else {
75             state = 0;
76             switch (chr) {
77                 case '\t':
78                     x += 4;
79                     break;
80                 case '\n':
81                     y++;
82                     // fall through
83                 case '\r':
84                     x = 0;
85                     break;
86                 // case '\x08':
87                 //     *(tty_vga_buffer + x + y * TTY_WIDTH) =
88                 //       (current_theme | 0x20);
89                 //     break;
90                 default:
91                     *(tty_vga_buffer + x + y * TTY_WIDTH) =
92                       (current_theme | chr);
93                     (x)++;
94                     break;
95             }
96
97             if (x >= TTY_WIDTH) {
98                 x = 0;
99                 y++;
100             }
101             if (y >= TTY_HEIGHT) {
102                 y--;
103                 break;
104             }
105         }
106     }
107     tty_set_cursor(x, y);
108 }
109
110 void
111 tty_set_cursor(uint8_t x, uint8_t y)
112 {
113     if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
114         x = y = 0;
115     }
116     uint32_t pos = y * TTY_WIDTH + x;
117     io_outb(0x3D4, 14);
118     io_outb(0x3D5, pos / 256);
119     io_outb(0x3D4, 15);
120     io_outb(0x3D5, pos % 256);
121 }
122
123 void
124 tty_clear_line(int line_num)
125 {
126     asm volatile("rep stosw" ::"D"(tty_vga_buffer + line_num * TTY_WIDTH),
127                  "c"(TTY_WIDTH),
128                  "a"(tty_theme_color)
129                  : "memory");
130 }
131
132 void
133 tty_put_str_at(char* str, int x, int y)
134 {
135     char c;
136     while ((c = (*str)) && y < TTY_HEIGHT) {
137         *(tty_vga_buffer + x + y * TTY_WIDTH) = c | tty_theme_color;
138         x++;
139         if (x >= TTY_WIDTH) {
140             y++;
141             x = 0;
142         }
143         str++;
144     }
145 }