This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
191 lines (176 loc) · 3.56 KB
/
print.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
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
/*
Formatted print utilities: kprintf, panic
*/
#include <stdarg.h>
#include "defs.h"
char digits[] = "0123456789ABCDEF";
/*
Print an integer with given number base,
either 10 or 16.
*/
void kprint_int(int num, int base)
{
if (base != 10 && base != 16)
{
panic("Unsupported number system!");
}
else if (num == 0)
{
uart_putchar('0');
return;
}
if (num < 0)
{
if (base == 10)
{
uart_putchar('-');
num = -num;
}
else
{
kprint_ptr(num);
return;
}
}
uint8 reverse_digits[32];
int i = 0;
while (num != 0)
{
reverse_digits[i] = digits[num % base];
num /= base;
i++;
}
i--;
while (i >= 0)
{
uart_putchar(reverse_digits[i]);
i--;
}
return;
}
/*
Print the value of a pinter in hexidecimal.
*/
void kprint_ptr(uint32 p)
{
uint8 digit;
uint8 i = 7;
uint32 four_bits;
while (1)
{
four_bits = p & (15 << i * 4);
digit = four_bits >> i * 4;
uart_putchar(digits[digit]);
if (i == 0)
{
break;
}
i--;
}
return;
}
/*
Formatted print.
Supported:
%d - decimal number
%x - hexidecimal number
%p - pointer value
%s - string
%c - character
*/
void kprintf(char *s, ...)
{
// printf, only support %d, %x, %p, %s and %c
va_list args;
va_start(args, s);
int i = 0;
uint8 c = s[i];
while (c)
{
// break until null char
if (c != '%')
{
uart_putchar(c);
}
else
{
c = s[++i];
switch (c)
{
case 'd':
kprint_int(va_arg(args, int), 10);
break;
case 'x':
kprint_int(va_arg(args, int), 16);
break;
case 'p':
kprint_ptr(va_arg(args, uint32));
break;
case 's':
uart_puts(va_arg(args, char *));
break;
case 'c':
uart_putchar(va_arg(args, int));
break;
case '%':
uart_putchar(c);
break;
default:
panic("Unknown format specifier.");
break;
}
}
i++;
c = s[i];
}
}
/*
Panic with specified prompt.
*/
void panic(char *s)
{
uart_puts("\nPANIC @ ");
uart_puts(s);
uart_puts("\n");
while (1)
;
return;
}
/*
Test cases for printing values.
*/
void test_print(void)
{
// kprint_ptr test
kprint_ptr(2);
uart_putchar('\n');
kprint_ptr(15);
uart_putchar('\n');
kprint_ptr(0xABCD98);
uart_putchar('\n');
kprint_ptr(123456789); // 075B CD15
uart_putchar('\n');
kprint_ptr(4294967295); // FFFF FFFF
uart_putchar('\n');
// kprint_int test
// base 10 part
kprint_int(-235, 10);
uart_putchar('\n');
kprint_int(0xABCD, 10); // 43981
uart_putchar('\n');
kprint_int(987654321, 10);
uart_putchar('\n');
// base 16 part
kprint_int(-3, 16); // FFFF FFFD
uart_putchar('\n');
kprint_int(0xABCD, 16);
uart_putchar('\n');
kprint_int(987654321, 16); // 3ADE 68B1
uart_putchar('\n');
// kprintf test
kprintf("Blah blah blah\n");
kprintf("Today is %d %s %d\n", 2022, "March", 4);
kprintf("Function puts is at %p\n", uart_puts);
kprintf("Number %d in hex is %x\n", 13579, 13579);
kprintf("Test %c %% char...\n", 'a');
}