-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.c
142 lines (123 loc) · 2.22 KB
/
console.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
#include "kernel.h"
enum {
Black,
Blue,
Green,
Cyan,
Red,
Purple,
Yellow,
White,
Bright,
Blink = 0x80,
};
enum {
Background = Black,
Foreground = White,
Width = 80,
Height = 24,
Attr = (Background << 4 | Foreground) << 8,
};
#define CRTPORT 0x3d4
u8
crt_read(u8 index)
{
outb(CRTPORT, index);
return inb(CRTPORT + 1);
}
void
crt_write(u8 index, u8 data)
{
outb(CRTPORT, index);
outb(CRTPORT + 1, data);
}
void
cursor_move(u16 offset)
{
crt_write(14, offset >> 8);
crt_write(15, offset);
}
u16
cursor_address(void)
{
return crt_read(14) << 8 | crt_read(15);
}
void
kputc(u8 c)
{
u16 *video = (u16 *) 0xb8000;
u16 offset = cursor_address();
if (c == '\n') {
do
video[offset++] = ' ' | Attr;
while (offset % 80);
cursor_move(offset);
return;
}
video[offset] = c | Attr;
cursor_move(offset + 1);
}
void
kputs(const char *s)
{
while (*s)
kputc(*s++);
}
int
itoa(int sval, char *str, int base)
{
static char digits[] = "0123456789abcdef";
int i = 0, j = 32, k = 32;
unsigned int val = sval;
if (val < 0 && base == 10)
str[i++] = '-';
do {
str[k--] = digits[val % base];
val /= base;
} while (val != 0);
i += 32 - k;
str[i--] = '\0';
while (i >= 0)
str[i--] = str[j--];
return 0;
}
void
kprintf(const char *fmt, ...)
{
const char *p;
char buf[33];
u32 *argp = (u32 *) &fmt + 1;
for (p = fmt; *p; p++) {
if (*p != '%') {
kputc(*p);
continue;
}
*p++;
switch (*p) {
case 'd':
itoa(*(int *) argp++, buf, 10);
kputs((char *) buf);
break;
case 's':
kputs(*(char **) argp++);
break;
case 'x':
itoa(*(int *) argp++, buf, 16);
kputs((char *) buf);
break;
default:
kputc('%');
kputc(*p);
break;
}
}
}
void
kcls(void)
{
int i;
cursor_move(0);
for (i = 0; i < 80 * 24; i++)
kputc(' ');
cursor_move(0);
}