-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstdio.c
157 lines (134 loc) · 3.4 KB
/
stdio.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
#include <stdarg.h>
#include <stdint.h>
#include "stdio.h"
#include "io.h"
#define VGA_WRITE_START 0xffff8000000b8000ull
#define DEFAULT_FORCOLOR 0x0d
#define DEFAULT_BACKCOLOR 0x07
#define WIDTH 80
#define HEIGHT 25
uint16_t screen[WIDTH * HEIGHT];
int cur_x = 0;
int cur_y = 0;
static void writec(char c, unsigned x, unsigned y, uint8_t forecolor, uint8_t backcolor) {
uint16_t colors = (backcolor << 4) | (forecolor & 0x0F);
uint16_t write_arg = c | colors << 8;
unsigned pos = y * WIDTH + x;
screen[pos] = write_arg;
((uint16_t*)VGA_WRITE_START)[pos] = screen[pos];
}
void setcursor(unsigned x, unsigned y) {
if (x >= WIDTH)
return;
if (y >= HEIGHT)
return;
unsigned addr = x + y * 80;
outb(0x3d4, 0xe);
outb(0x3d5, addr >> 8);
outb(0x3d4, 0xf);
outb(0x3d5, addr);
}
static void scroll(void) {
for (unsigned y = 1; y < HEIGHT; y++) {
for (unsigned x = 0; x < WIDTH; x++) {
unsigned src_pos = y * WIDTH + x;
unsigned dst_pos = (y - 1) * WIDTH + x;
screen[dst_pos] = screen[src_pos];
((uint16_t*)VGA_WRITE_START)[dst_pos] = screen[dst_pos];
}
}
for (unsigned x = 0; x < WIDTH; x++) {
writec(' ', x, HEIGHT - 1, DEFAULT_FORCOLOR, DEFAULT_BACKCOLOR);
}
}
void clrscr(void) {
for (unsigned x = 0; x < WIDTH; x++) {
for (unsigned y = 0; y < HEIGHT; y++) {
writec(' ', x, y, DEFAULT_FORCOLOR, DEFAULT_BACKCOLOR);
}
}
setcursor(0, 0);
cur_x = cur_y = 0;
}
void putc(uint8_t c) {
if (c == '\n') {
cur_x = 0;
cur_y++;
}
else {
writec(c, cur_x, cur_y, DEFAULT_FORCOLOR, DEFAULT_BACKCOLOR);
cur_y = (cur_x + 1 == WIDTH) ? cur_y + 1 : cur_y;
cur_x = (cur_x + 1) % WIDTH;
}
if (cur_y == HEIGHT) {
scroll();
cur_y--;
}
setcursor(cur_x, cur_y);
}
static void print_char(char c) {
putc(c);
}
static void print_str(uint8_t* str) {
while (*str) {
putc(*str);
str++;
}
}
static void print_num(uint32_t num, uint8_t base) {
uint32_t tmp;
uint8_t i = 0;
uint8_t digits[10] = { 0 };
if (!num) {
putc('0');
return;
}
while (num) {
tmp = num;
num /= base;
digits[i] = "0123456789abcdef"[tmp - (num * base)];
i++;
}
for (int j = i - 1; j >= 0; j--) {
putc(digits[j]);
}
}
void printf(const char* format, ...) {
va_list va;
va_start(va, format);
uint8_t c, b;
uint8_t* str;
uint32_t num;
while (*format) {
c = *format;
if (c != '%') {
putc(c);
}
else {
format++;
c = *format;
switch(c) {
case 'c':
b = (uint8_t) va_arg(va, uint32_t);
print_char(b);
break;
case 's':
str = va_arg(va, uint8_t*);
print_str(str);
break;
case 'd':
num = va_arg(va, uint32_t);
print_num(num, 10);
break;
case 'x':
num = va_arg(va, uint32_t);
putc('0');
putc('x');
print_num(num, 16);
break;
}
}
format++;
}
va_end(va);
}