-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintk.cpp
165 lines (157 loc) · 4.96 KB
/
printk.cpp
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 "printk.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "vga.h"
#include "string.h"
#include "serial.h"
int atoi_display(unsigned long long abs_val) {
int written = 0;
unsigned long long shifter = 1000000000000000000; // Length of largest 64-bit unsigned
bool first_digit = false;
while (shifter) {
int digit = abs_val / shifter;
if (!first_digit && (shifter == 1 || digit > 0))
first_digit = true;
if (first_digit) {
VGA::vga.display_char((char)digit + '0');
VGA::vga.increment_cursor();
COM1.write_serial((char)digit + '0');
written++;
}
abs_val -= digit * shifter;
shifter /= 10;
}
return written;
}
int print_decimal(int value) {
int written = 0;
if (value < 0) {
VGA::vga.display_char('-');
VGA::vga.increment_cursor();
COM1.write_serial('-');
value *= -1;
written++;
}
written += atoi_display((uint64_t)value);
return written;
}
int print_unsigned(unsigned value) {
return atoi_display((uint64_t)value);
}
// char a represents base for displaying letters in hex a:lowercase A:uppercase
int atoi_base16_display(uint64_t value, char a) {
VGA::vga.display_string("0x");
COM1.write_serial("0x");
int written = 2;
int index = 15; // hex char index of 64bit value (16 indexes)
// uint64_t mask = 0xf << (64 - 4);
uint64_t mask = 0xf000000000000000;
bool first_char = false;
while (mask) {
char nibble = (char)((value & mask) >> (index * 4));
if (!first_char && (index == 0 || nibble > 0))
first_char = true;
if (first_char) {
if (nibble > 9) {
VGA::vga.display_char(nibble - 10 + a);
COM1.write_serial(nibble - 10 + a);
} else {
VGA::vga.display_char(nibble + '0');
COM1.write_serial(nibble + '0');
}
VGA::vga.increment_cursor();
written++;
}
index--;
mask >>= 4;
}
return written;
}
int print_hex(uint64_t value, char a) {
return atoi_base16_display((uint64_t)value, a);
}
int print_hex(uint64_t value) {
return print_hex(value, 'A');
}
// Printk will interpret \n as \n\r
int printk(const char *fmt, ...) {
int written = 0;
va_list vl;
va_start(vl, fmt);
while (*fmt) {
if (*fmt == '%') {
switch (*(fmt + 1)) {
case '%':
VGA::vga.display_char('%');
VGA::vga.increment_cursor();
COM1.write_serial('%');
written++;
break;
case 'd':
written += print_decimal(va_arg(vl, int));
break;
case 'u':
written += print_unsigned((unsigned)va_arg(vl, unsigned));
break;
case 'x':
written += print_hex(va_arg(vl, unsigned));
break;
case 'c':
{
char a = (char)va_arg(vl, int);
if (a == '\n') {
VGA::vga.display_char('\n');
VGA::vga.display_char('\r');
COM1.write_serial('\n');
} else if (a == '\r') {
} else {
VGA::vga.display_char(a); // variadic upconverts to int
VGA::vga.increment_cursor();
COM1.write_serial(a);
written++;
}
break;
}
case 'p':
written += print_hex((uint64_t)va_arg(vl, void *));
break;
case 'h':
break;
case 'l':
break;
case 'q':
break;
case 's':
{
const char *str = va_arg(vl, const char *);
VGA::vga.display_string(str);
COM1.write_serial(str);
written += strlen(str);
break;
}
case NULL:
return written;
default:
break;
}
fmt++;
} else {
if (*fmt == '\n') {
VGA::vga.display_char('\n');
VGA::vga.display_char('\r');
COM1.write_serial('\n');
} else if (*fmt == '\r') {
} else {
VGA::vga.display_char(*fmt);
VGA::vga.increment_cursor();
COM1.write_serial(*fmt);
}
written++;
}
fmt++;
}
va_end(vl);
return written;
}