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
|
#include "tty.h"
/* Hardware text mode color constants. */
enum vga_color {
COLOR_BLACK = 0,
COLOR_BLUE = 1,
COLOR_GREEN = 2,
COLOR_CYAN = 3,
COLOR_RED = 4,
COLOR_MAGENTA = 5,
COLOR_BROWN = 6,
COLOR_LIGHT_GREY = 7,
COLOR_DARK_GREY = 8,
COLOR_LIGHT_BLUE = 9,
COLOR_LIGHT_GREEN = 10,
COLOR_LIGHT_CYAN = 11,
COLOR_LIGHT_RED = 12,
COLOR_LIGHT_MAGENTA = 13,
COLOR_LIGHT_BROWN = 14,
COLOR_WHITE = 15,
};
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define VGA_TEXT_BUFFER 0xB8000
#define VGA_COLOR_SHIFT 8
static size_t tty_row;
static size_t tty_col;
static uint8_t tty_color;
static uint16_t* tty_buffer;
static inline uint8_t make_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
static inline uint16_t make_vgaentry(char c, uint8_t color)
{
return ((uint16_t) c) | (((uint16_t) color) << VGA_COLOR_SHIFT);
}
static inline void tty_putc_at(char c, uint8_t color, size_t x, size_t y)
{
const size_t i = y * VGA_WIDTH + x;
tty_buffer[i] = make_vgaentry(c, color);
}
void tty_setcolor(uint8_t color)
{
tty_color = color;
}
void tty_putc(char c)
{
if (c == '\n')
{
tty_col = 0;
tty_row++;
} else {
tty_putc_at(c, tty_color, tty_col, tty_row);
if (++tty_col == VGA_WIDTH) {
tty_col = 0;
if (++tty_row == VGA_HEIGHT) {
tty_row = 0;
}
}
}
}
void tty_puts(const char* data)
{
for (const char* p = data; *p != 0; p++)
tty_putc(*p);
}
void tty_init()
{
tty_row = 0;
tty_col = 0;
tty_buffer = (uint16_t*) VGA_TEXT_BUFFER;
tty_setcolor(make_color(COLOR_LIGHT_GREY, COLOR_BLACK));
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t i = y * VGA_WIDTH + x;
tty_buffer[i] = make_vgaentry(' ', tty_color);
}
}
}
|