-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.c
165 lines (140 loc) · 3.16 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "console.h"
#include "serial.h"
//position on the screen
static int x = 0;
static int y = 0;
//video adress
static char* video = (char*) 0xb8000;
//defines screen width and height
const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT= 25;
static inline void outb(uint16_t port, uint8_t val) {
asm volatile ( "outb %0, %1" : : "a" (val), "Nd" (port));
}
void update_cursor(int row, int col)
{
unsigned short position=(row*80) + col;
// cursor LOW port to vga INDEX register
outb(0x3D4, 0x0F);
outb(0x3D5, (unsigned char)(position&0xFF));
// cursor HIGH port to vga INDEX register
outb(0x3D4, 0x0E);
outb(0x3D5, (unsigned char )((position>>8)&0xFF));
}
/*
The function clear the screen by deleting every character
*/
void clear_screen(void) {
//loop through all characteres
for(int i = 0; i < SCREEN_WIDTH*SCREEN_HEIGHT*2;i+= 2) {
//clear the characteres
video[i] = 0;
video[i+1] = 0x07;
}
//set pointer position to zero
x = y = 0;
}
void kputc(char c)
{
if ((c == '\n') || (x > 79)) {
x = 0;
y++;
}
writeCharacter(c);
if (c == '\n') {
return;
}
if (y > 24) {
int i;
for (i = 0; i < 2 * 24 * 80; i++) {
video[i] = video[i + 160];
}
for (; i < 2 * 25 * 80; i++) {
video[i] = 0;
}
y--;
}
video[2 * (y * 80 + x)] = c;
video[2 * (y * 80 + x) + 1] = 0x07;
x++;
update_cursor(y,x);
}
void kdelc() {
video[2 * (y * 80 + x)] = 0;
video[2 * (y * 80 +x) + 1] = 0x07;
x--;
if(x == 0) {
if(y != 0) {
y--;
x = 80;
}
}
update_cursor(y,x);
}
static void kputs(const char* s) {
while(*s) {
kputc(*s++);
}
}
static void kputn(unsigned long int x, int base)
{
char buf[65];
const char* digits = "0123456789abcdefghijklmnopqrstuvwxyz";
char* p;
if (base > 36) {
return;
}
p = buf + 64;
*p = '\0';
do {
*--p = digits[x % base];
x /= base;
} while (x);
kputs(p);
}
void kprintf(const char* fmt, ...)
{
va_list ap;
const char* s;
unsigned long n;
va_start(ap, fmt);
while (*fmt) {
if (*fmt == '%') {
fmt++;
switch (*fmt) {
case 's':
s = va_arg(ap, char*);
kputs(s);
break;
case 'b':
n = va_arg(ap, unsigned long int);
kputn(n, 2);
break;
case 'd':
case 'u':
n = va_arg(ap, unsigned long int);
kputn(n, 10);
break;
case 'x':
case 'p':
n = va_arg(ap, unsigned long int);
kputn(n, 16);
break;
case '%':
kputc('%');
break;
case '\0':
goto out;
default:
kputc('%');
kputc(*fmt);
break;
}
} else {
kputc(*fmt);
}
fmt++;
}
out:
va_end(ap);
}