-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.c
204 lines (181 loc) · 4.33 KB
/
tty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#include "tty.h"
#include "vga.h"
#include "io.h"
#include "string.h"
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
size_t terminal_row;
size_t terminal_column;
uint8_t terminal_color;
uint16_t *terminal_buffer;
void terminal_initialize(void)
{
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_buffer = (uint16_t *)0xB8000;
for (size_t i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++)
{
terminal_buffer[i] = vga_entry(' ', terminal_color);
}
}
void terminal_setcolor(uint8_t color)
{
terminal_color = color;
}
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
static uint16_t terminal_pos(uint8_t x, uint8_t y)
{
return y * VGA_WIDTH + x;
}
void terminal_scroll(void)
{
for (size_t y = 0; y < VGA_HEIGHT - 1; y++)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
{
terminal_buffer[y * VGA_WIDTH + x] = terminal_buffer[(y + 1) * VGA_WIDTH + x];
}
}
for (size_t x = 0; x < VGA_WIDTH; x++)
{
terminal_buffer[(VGA_HEIGHT - 1) * VGA_WIDTH + x] = vga_entry(' ', terminal_color);
}
terminal_row = VGA_HEIGHT - 1;
}
void terminal_update_cursor(void)
{
uint16_t pos = terminal_row * VGA_WIDTH + terminal_column;
outb(0x3D4, 14);
outb(0x3D5, pos >> 8);
outb(0x3D4, 15);
outb(0x3D5, pos);
}
static void terminal_backspace(void)
{
if (terminal_column == 0)
{
terminal_column = VGA_WIDTH - 1;
if (terminal_row > 0)
{
--terminal_row;
}
}
else
{
--terminal_column;
}
terminal_buffer[terminal_pos(terminal_column, terminal_row)] = vga_entry(' ', terminal_color);
}
void terminal_putchar(char c)
{
if (c == '\n')
{
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
{
terminal_scroll();
}
}
else if (c == '\b')
{
terminal_backspace();
}
else
{
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH)
{
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
{
terminal_scroll();
}
}
}
terminal_update_cursor();
}
void terminal_write(const char *data, size_t size)
{
for (size_t i = 0; i < size; i++)
{
terminal_putchar(data[i]);
}
}
void terminal_print(const char *data)
{
terminal_write(data, strlen(data));
}
static void print_number(int value, int base, int padding)
{
char buffer[32];
itoa(value, buffer, base);
int len = strlen(buffer);
while (padding > len)
{
terminal_putchar('0');
padding--;
}
terminal_print(buffer);
}
void terminal_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
const char *p = format;
while (*p)
{
if (*p == '%' && (*(p + 1) == 's' || *(p + 1) == 'd' || *(p + 1) == 'x' || *(p + 1) == '0'))
{
p++;
int padding = 0;
if (*p == '0')
{
p++;
padding = *p - '0';
p++;
}
if (*p == 's')
{
terminal_print(va_arg(args, const char *));
}
else if (*p == 'd')
{
print_number(va_arg(args, int), 10, padding);
}
else if (*p == 'x')
{
print_number(va_arg(args, unsigned int), 16, padding);
}
}
else if (*p == '&' && ((*(p + 1) >= '0' && *(p + 1) <= '9') || (*(p + 1) >= 'a' && *(p + 1) <= 'f')))
{
terminal_setcolor(vga_entry_color(vga_color_from_char(*(p + 1)), VGA_COLOR_BLACK));
p++;
}
else
{
terminal_putchar(*p);
}
p++;
}
va_end(args);
}
void terminal_clear(void)
{
for (size_t i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++)
{
terminal_buffer[i] = vga_entry(' ', terminal_color);
}
terminal_column = 0;
terminal_row = 0;
terminal_update_cursor();
}