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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
#include <stdbool.h>
#include "kprintf.h"
#include "asm_inline.h"
// #define KDEBUG
int check_transmission() {
return inb(COM1 + 5) & 0x20;
}
void write_serial_char(char a) {
while (check_transmission() == 0);
outb(COM1,a);
}
void write_serial_string(const char* str)
{
while(*str != '\0')
{
if(*str == '\n')
{
write_serial_char('\r');
}
write_serial_char(*str);
str++;
}
}
char* convert_U_integer(uint64_t n, uint16_t base, bool caps)
{
static char* format = "0123456789abcdef";
static char* C_format = "0123456789ABCDEF";
static char buffer[50];
char *ptr;
ptr = &buffer[49];
*ptr = '\0';
do
{
if(caps)
{
*--ptr = C_format[n%base];
}
else
{
*--ptr = format[n%base];
}
n /= base;
} while(n != 0);
return ptr;
}
void kprintf(const char* str, ...)
{
va_list args;
va_start(args, str);
while(*str)
{
if(*str == '%')
{
#ifdef KDEBUG
write_serial_string("{stradv}");
#endif
str++;
if(*str == 'i' || *str == 'd')
{
#ifdef KDEBUG
write_serial_string("[INT]");
#endif
int32_t n = va_arg(args, int32_t);
char* s = convert_U_integer(n, 10, false);
write_serial_string(s);
}
else if(*str == 'u')
{
#ifdef KDEBUG
write_serial_string("[UINT]");
#endif
uint32_t n = va_arg(args, uint32_t);
char* s = convert_U_integer(n, 10, false);
write_serial_string(s);
}
else if(*str == 'x')
{
#ifdef KDEBUG
write_serial_string("[HEX]");
#endif
uint32_t n = va_arg(args, uint32_t);
char* s = convert_U_integer(n, 16, false);
write_serial_string("0x");
write_serial_string(s);
}
else if(*str == 'X')
{
#ifdef KDEBUG
write_serial_string("[HEX]");
#endif
uint32_t n = va_arg(args, uint32_t);
char* s = convert_U_integer(n, 16, true);
write_serial_string("0x");
write_serial_string(s);
}
else if(*str == 'l')
{
#ifdef KDEBUG
write_serial_string("[LONG]");
write_serial_string("{ladv}");
#endif
str++;
if(*str == 'i' || *str == 'd')
{
#ifdef KDEBUG
write_serial_string("[INT]");
#endif
int64_t n = va_arg(args, int64_t);
char* s = convert_U_integer(n, 10, false);
write_serial_string(s);
}
else if(*str == 'u')
{
#ifdef KDEBUG
write_serial_string("[UINT]");
#endif
uint64_t n = va_arg(args, uint64_t);
char* s = convert_U_integer(n, 10, false);
write_serial_string(s);
}
else if(*str == 'x')
{
#ifdef KDEBUG
write_serial_string("[HEX]");
#endif
uint64_t n = va_arg(args, uint64_t);
char* s = convert_U_integer(n, 16, false);
write_serial_string("0x");
write_serial_string(s);
}
else if(*str == 'X')
{
#ifdef KDEBUG
write_serial_string("[HEX]");
#endif
uint64_t n = va_arg(args, uint64_t);
char* s = convert_U_integer(n, 16, true);
write_serial_string("0x");
write_serial_string(s);
}
}
else if(*str == 'h')
{
#ifdef KDEBUG
write_serial_string("[SHORT]");
write_serial_string("{ladv}");
#endif
str++;
if(*str == 'i' || *str == 'd')
{
#ifdef KDEBUG
write_serial_string("[INT]");
#endif
int16_t n = va_arg(args, int);
char* s = convert_U_integer(n, 10, false);
write_serial_string(s);
}
else if(*str == 'u')
{
#ifdef KDEBUG
write_serial_string("[UINT]");
#endif
uint16_t n = va_arg(args, int);
char* s = convert_U_integer(n, 10, false);
write_serial_string(s);
}
else if(*str == 'x')
{
#ifdef KDEBUG
write_serial_string("[HEX]");
#endif
uint16_t n = va_arg(args, int);
char* s = convert_U_integer(n, 16, false);
write_serial_string("0x");
write_serial_string(s);
}
else if(*str == 'X')
{
#ifdef KDEBUG
write_serial_string("[HEX]");
#endif
uint16_t n = va_arg(args, int);
char* s = convert_U_integer(n, 16, true);
write_serial_string("0x");
write_serial_string(s);
}
}
else if(*str == 'p')
{
#ifdef KDEBUG
write_serial_string("[PTR]");
#endif
void* p = va_arg(args, void*);
uint64_t n = (uint64_t)p;
char* s = convert_U_integer(n, 16, true);
write_serial_string("0x");
write_serial_string(s);
}
else if(*str == 'c')
{
#ifdef KDEBUG
write_serial_string("[CHAR]");
#endif
char c = va_arg(args, int);
write_serial_char(c);
}
else if(*str == 's')
{
#ifdef KDEBUG
write_serial_string("[STR]");
#endif
char* s = va_arg(args, char*);
write_serial_string(s);
}
else
{
write_serial_string("\nFailed to write malformed kprintf: ");
write_serial_char(*str);
write_serial_string("\n");
va_end(args);
return;
}
#ifdef KDEBUG
write_serial_string("{pend}");
#endif
}
else if(*str == '\n')
{
write_serial_char('\r');
write_serial_char('\n');
}
else
{
write_serial_char(*str);
}
#ifdef KDEBUG
write_serial_string("{oadv}");
#endif
str++;
}
//write_serial_string(str);
va_end(args);
}
|