forked from anshugupta673/x86_64-decOS-MSC-KIIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
166 lines (134 loc) · 4.11 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
#include "stdint.h"
#include "stdarg.h"
#include "print.h"
#include "lib.h"
#include "memory.h"
static struct ScreenBuffer screen_buffer = {(char*)P2V(0xb8000), 0, 0};
static int udecimal_to_string(char *buffer, int position, uint64_t digits)
{
char digits_map[10] = "0123456789";
char digits_buffer[25];
int size = 0;
do {
digits_buffer[size++] = digits_map[digits % 10];
digits /= 10;
} while (digits != 0);
for (int i = size-1; i >= 0; i--) {
buffer[position++] = digits_buffer[i];
}
return size;
}
static int decimal_to_string(char *buffer, int position, int64_t digits)
{
int size = 0;
if (digits < 0) {
digits = -digits;
buffer[position++] = '-';
size = 1;
}
size += udecimal_to_string(buffer, position, (uint64_t)digits);
return size;
}
static int hex_to_string(char *buffer, int position, uint64_t digits)
{
char digits_buffer[25];
char digits_map[16] = "0123456789ABCDEF";
int size = 0;
do {
digits_buffer[size++] = digits_map[digits % 16];
digits /= 16;
} while (digits != 0);
for (int i = size-1; i >= 0; i--) {
buffer[position++] = digits_buffer[i];
}
buffer[position++] = 'H';
return size+1;
}
static int read_string(char *buffer, int position, const char *string)
{
int index = 0;
for (index = 0; string[index] != '\0'; index++) {
buffer[position++] = string[index];
}
return index;
}
void write_screen(const char *buffer, int size, char color)
{
struct ScreenBuffer *sb = &screen_buffer;
int column = sb->column;
int row = sb->row;
for (int i = 0; i < size; i++) {
if (buffer[i] == '\n') {
column = 0;
row++;
}
else if (buffer[i] == '\b') {
if (column == 0 && row == 0) {
continue;
}
if (column == 0) {
row--;
column = 80;
}
column -= 1;
sb->buffer[column*2+row*LINE_SIZE] = 0;
sb->buffer[column*2+row*LINE_SIZE+1] = 0;
}
else {
sb->buffer[column*2+row*LINE_SIZE] = buffer[i];
sb->buffer[column*2+row*LINE_SIZE+1] = color;
column++;
if (column >= 80) {
column=0;
row++;
}
}
if (row >= 25) {
memcpy(sb->buffer,sb->buffer+LINE_SIZE,LINE_SIZE*24);
memset(sb->buffer+LINE_SIZE*24,0,LINE_SIZE);
row--;
}
}
sb->column = column;
sb->row = row;
}
int printk(const char *format, ...)
{
char buffer[1024];
int buffer_size = 0;
int64_t integer = 0;
char *string = 0;
va_list args;
va_start(args,format);
for (int i = 0; format[i] != '\0'; i++) {
if (format[i] != '%') {
buffer[buffer_size++] = format[i];
}
else {
switch (format[++i]) {
case 'x':
integer = va_arg(args, int64_t);
buffer_size += hex_to_string(buffer, buffer_size, (uint64_t)integer);
break;
case 'u':
integer = va_arg(args, int64_t);
buffer_size += udecimal_to_string(buffer, buffer_size, (uint64_t)integer);
break;
case 'd':
integer = va_arg(args, int64_t);
buffer_size += decimal_to_string(buffer, buffer_size, integer);
break;
case 's':
string = va_arg(args, char*);
buffer_size += read_string(buffer, buffer_size, string);
break;
default:
buffer[buffer_size++] = '%';
i--;
}
}
}
write_screen(buffer, buffer_size, 0xf);
va_end(args);
return buffer_size;
}