refactor: use a more decent physical memory map
[lunaix-os.git] / lunaix-os / kernel / tty / tty.c
1 #include <klibc/string.h>
2 #include <lunaix/spike.h>
3 #include <lunaix/tty/console.h>
4 #include <lunaix/tty/tty.h>
5 #include <stdint.h>
6
7 #include <sys/port_io.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     port_wrbyte(0x3D4, 0x0A);
30     port_wrbyte(0x3D5, (port_rdbyte(0x3D5) & 0xC0) | 13);
31
32     port_wrbyte(0x3D4, 0x0B);
33     port_wrbyte(0x3D5, (port_rdbyte(0x3D5) & 0xE0) | 15);
34 }
35
36 void
37 tty_set_cursor(u8_t x, u8_t y)
38 {
39     if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
40         x = y = 0;
41     }
42     u32_t pos = y * TTY_WIDTH + x;
43     port_wrbyte(0x3D4, 14);
44     port_wrbyte(0x3D5, pos / 256);
45     port_wrbyte(0x3D4, 15);
46     port_wrbyte(0x3D5, pos % 256);
47 }
48
49 void
50 tty_set_theme(vga_attribute fg, vga_attribute bg)
51 {
52     tty_theme_color = (bg << 4 | fg) << 8;
53 }
54
55 void
56 tty_flush_buffer(struct fifo_buf* buf)
57 {
58     int x = 0, y = 0;
59
60     // Clear screen
61     tty_clear();
62
63     char chr;
64     int state = 0;
65     int g[2] = { 0, 0 };
66     vga_attribute current_theme = tty_theme_color;
67     while (fifo_readone_async(buf, (u8_t*)&chr)) {
68         if (state == 0 && chr == '\033') {
69             state = 1;
70         } else if (state == 1 && chr == '[') {
71             state = 2;
72         } else if (state > 1) {
73             if ('0' <= chr && chr <= '9') {
74                 g[state - 2] = (chr - '0') + g[state - 2] * 10;
75             } else if (chr == ';' && state == 2) {
76                 state = 3;
77             } else {
78                 if (g[0] == 39 && g[1] == 49) {
79                     current_theme = tty_theme_color;
80                 } else {
81                     current_theme = (g[1] << 4 | g[0]) << 8;
82                 }
83                 g[0] = 0;
84                 g[1] = 0;
85                 state = 0;
86             }
87         } else {
88             state = 0;
89             switch (chr) {
90                 case '\t':
91                     x += 4;
92                     break;
93                 case '\n':
94                     y++;
95                     // fall through
96                 case '\r':
97                     x = 0;
98                     break;
99                 // case '\x08':
100                 //     *(tty_vga_buffer + x + y * TTY_WIDTH) =
101                 //       (current_theme | 0x20);
102                 //     break;
103                 default:
104                     *(tty_vga_buffer + x + y * TTY_WIDTH) =
105                       (current_theme | chr);
106                     (x)++;
107                     break;
108             }
109
110             if (x >= TTY_WIDTH) {
111                 x = 0;
112                 y++;
113             }
114             if (y >= TTY_HEIGHT) {
115                 y--;
116                 break;
117             }
118         }
119     }
120     tty_set_cursor(x, y);
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 }