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
|
#include "print.h"
#include "asm.h"
#include "str.h"
#include <stdarg.h>
#include <stdint.h>
// Constants
static const size_t NUM_COLS = 80;
static const size_t NUM_ROWS = 25;
// Char struct
struct Char {
uint8_t character;
uint8_t color;
};
// Set video mem buffer
struct Char *buffer = (struct Char *)0xb8000;
// Set current row and col
size_t col = 0;
size_t row = 0;
// Default colors
uint8_t color = PRINT_COLOR_WHITE | PRINT_COLOR_BLACK << 4;
// clear row
void clear_row(size_t row) {
// Define empty character
struct Char empty = (struct Char){
' ',
color,
};
// For every col in row, set vmem to empty
for (size_t col = 0; col < NUM_COLS; col++) {
buffer[col + NUM_COLS * row] = empty;
}
}
void delete_char() {
if (col == 0) {
row--;
col = NUM_COLS;
}
col--;
buffer[col + NUM_COLS * row] = (struct Char){
' ',
color,
};
}
// Call clear_row for every row
void print_clear() {
for (size_t i = 0; i < NUM_ROWS; i++) {
clear_row(i);
}
col = 0;
row = 0;
}
// newline
void print_newline() {
// Reset col and iterate row if not at bottom of screen
col = 0;
if (row < NUM_ROWS - 1) {
row++;
return;
}
// If at bottom of screen, move all the text up one row
for (size_t row = 1; row < NUM_ROWS; row++) {
for (size_t col = 0; col < NUM_COLS; col++) {
struct Char character = buffer[col + NUM_COLS * row];
buffer[col + NUM_COLS * (row - 1)] = character;
}
}
// Clear
clear_row(NUM_ROWS - 1);
}
void update_cursor() {
uint16_t pos = col + NUM_COLS * row;
outb(0x3D4, 0x0F);
outb(0x3D5, (uint8_t)(pos & 0xFF));
outb(0x3D4, 0x0E);
outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF));
}
// printchar
void print_char(char character) {
// If \n, call newline
if (character == '\n') {
print_newline();
return;
}
if (character == '\t') {
print_str(" \0");
return;
}
// if cols is too large, overflow
if (col >= NUM_COLS) {
print_newline();
}
// set video memeory to requested character
buffer[col + NUM_COLS * row] = (struct Char){
(uint8_t)character,
color,
};
// iterate col
col++;
}
// print_str
int print_str(char *str) {
// while character != '\0' print char
size_t i;
for (i = 0; str[i] != '\0'; ++i)
print_char(str[i]);
return i;
}
// Set color byte to requested colors using color enum from print.h
void print_set_color(uint8_t foreground, uint8_t background) {
color = foreground + (background << 4);
}
int printf(const char *str, ...) {
va_list args;
va_start(args, str);
char temp_str[256] = "";
int len = 0;
size_t i;
for (i = 0; str[i] != '\0'; ++i) {
if (str[i] == '%') {
switch (str[++i]) {
case 'i':
case 'd':
len += print_str(itoa(va_arg(args, int), temp_str, 10));
break;
case 'x':
len += print_str("0x");
len += print_str(itoa(va_arg(args, int), temp_str, 16));
break;
case 's':
len += print_str(va_arg(args, char *));
break;
case 'c':
print_char(va_arg(args, int));
len++;
break;
case '%':
print_char('%');
len++;
case 'n':
break;
}
continue;
}
print_char(str[i]);
}
va_end(args);
return i;
}
|