-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.cpp
More file actions
310 lines (268 loc) · 10.6 KB
/
console.cpp
File metadata and controls
310 lines (268 loc) · 10.6 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* @file console.cpp
* @brief Console
*
* @date 13/01/2025
* @version 1.0.0
*/
#include <console.h>
int cursorRow = 0;
int cursorCol = 0;
Port8Bit port_1(0x3D4);
Port8Bit port_2(0x3D5);
TextColor combineColors(TextColor foreground, TextColor background) {
return (TextColor)((background << 4) | foreground);
}
// Simple Wrapper Function
void MSGPrintf(TextColor cTag, const char* tag, const char* format, ...) {
printf(cTag, "[%s]", tag); // Print the tag
printf(LIGHT_GRAY, ":");
if (!format || !*format) {
return; // Do nothing if the format is null or empty
}
// Check if the format string contains placeholders
bool hasPlaceholders = false;
for (const char* p = format; *p != '\0'; p++) {
if (*p == '%') {
hasPlaceholders = true;
break;
}
}
// If there are no placeholders, print the format string as is
if (!hasPlaceholders) {
printf(LIGHT_GRAY, format);
return;
}
// Process the format string with arguments
va_list args;
va_start(args, format);
for (const char* p = format; *p != '\0'; p++) {
if (*p == '%') {
p++;
switch (*p) {
case 's': { // String
const char* str = va_arg(args, const char*);
printf(LIGHT_GRAY, str);
break;
}
case 'd': { // Decimal integer
int num = va_arg(args, int);
printf(LIGHT_GRAY, "%d", num);
break;
}
case 'x': { // Hexadecimal
int num = va_arg(args, int);
printf(LIGHT_GRAY, "%x", num);
break;
}
default:
printf(LIGHT_GRAY, "%%");
printf(LIGHT_GRAY, "%c", *p);
break;
}
} else {
printf(LIGHT_GRAY, "%c", *p);
}
}
va_end(args);
}
void scrollScreen() {
unsigned short* VideoMemory = (unsigned short*)VIDEO_MEMORY_ADDRESS;
// Scroll all rows up by one
for (int row = 1; row < SCREEN_HEIGHT; row++) {
for (int col = 0; col < SCREEN_WIDTH; col++) {
VideoMemory[(row - 1) * SCREEN_WIDTH + col] = VideoMemory[row * SCREEN_WIDTH + col];
}
}
// Clear the last row
unsigned short blank = 0x20 | (WHITE << 8); // Space with white text on black background
for (int col = 0; col < SCREEN_WIDTH; col++) {
VideoMemory[(SCREEN_HEIGHT - 1) * SCREEN_WIDTH + col] = blank;
}
// Adjust the cursor position if it moves out of bounds
if (cursorRow > SCREEN_HEIGHT - 1) {
cursorRow = SCREEN_HEIGHT - 1;
}
}
void updateCursor(int row, int col) {
unsigned short position = row * SCREEN_WIDTH + col;
// Set cursor start and enable blinking
port_1.Write(0x0A); // Cursor Start Register
port_2.Write(0x06); // Start at scanline 6 (enables blinking)
// Set cursor end
port_1.Write(0x0B); // Cursor End Register
port_2.Write(0x0F); // End at scanline 15
// Update the cursor position
port_1.Write(0x0E); // High byte of cursor position
port_2.Write((position >> 8) & 0xFF);
port_1.Write(0x0F); // Low byte of cursor position
port_2.Write(position & 0xFF);
}
void clearScreen() {
unsigned short* VideoMemory = (unsigned short*)VIDEO_MEMORY_ADDRESS;
unsigned short blank = 0x20 | (WHITE << 8); // Space with white text on black background
for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
VideoMemory[i] = blank;
}
cursorRow = 0;
cursorCol = 0;
updateCursor(0, 0);
}
void printf(TextColor color, const char* format, ...) {
unsigned short* VideoMemory = (unsigned short*)VIDEO_MEMORY_ADDRESS;
va_list args;
va_start(args, format);
for (int i = 0; format[i] != '\0'; i++) {
if (format[i] == '%') {
i++;
switch (format[i]) {
case 'd': { // Signed Integer
int num = va_arg(args, int);
char buffer[12]; // Enough for -2147483648 + '\0'
int index = 11; // Start filling from the end
buffer[index] = '\0'; // Null-terminate
if (num == 0) {
buffer[--index] = '0';
} else if (num == -2147483648) { // Special case for INT_MIN
const char* minStr = "-2147483648";
for (int j = 0; minStr[j] != '\0'; j++) {
int position = cursorRow * SCREEN_WIDTH + cursorCol;
VideoMemory[position] = (color << 8) | minStr[j];
cursorCol++;
if (cursorCol >= SCREEN_WIDTH) {
cursorCol = 0;
cursorRow++;
}
if (cursorRow >= SCREEN_HEIGHT) {
scrollScreen();
cursorRow = SCREEN_HEIGHT - 1;
}
}
break; // Exit the case here
} else {
bool isNegative = (num < 0);
if (isNegative) num = -num;
while (num > 0) {
buffer[--index] = (num % 10) + '0';
num /= 10;
}
if (isNegative) buffer[--index] = '-';
}
for (int j = index; buffer[j] != '\0'; j++) {
int position = cursorRow * SCREEN_WIDTH + cursorCol;
VideoMemory[position] = (color << 8) | buffer[j];
cursorCol++;
if (cursorCol >= SCREEN_WIDTH) {
cursorCol = 0;
cursorRow++;
}
if (cursorRow >= SCREEN_HEIGHT) {
scrollScreen();
cursorRow = SCREEN_HEIGHT - 1;
}
}
break;
}
case 'u': { // Unsigned Integer
uint32_t num = va_arg(args, uint32_t);
char buffer[11]; // Enough for 0 to 4294967295
int index = 10; // Start filling from the end
buffer[index] = '\0'; // Null-terminate
if (num == 0) {
buffer[--index] = '0';
} else {
while (num > 0) {
buffer[--index] = (num % 10) + '0';
num /= 10;
}
}
for (int j = index; buffer[j] != '\0'; j++) {
int position = cursorRow * SCREEN_WIDTH + cursorCol;
VideoMemory[position] = (color << 8) | buffer[j];
cursorCol++;
if (cursorCol >= SCREEN_WIDTH) {
cursorCol = 0;
cursorRow++;
}
if (cursorRow >= SCREEN_HEIGHT) {
scrollScreen();
cursorRow = SCREEN_HEIGHT - 1;
}
}
break;
}
case 'x': { // Hexadecimal
uint32_t num = va_arg(args, uint32_t);
char buffer[9]; // Enough for 8 hex digits + '\0'
int index = 8; // Start filling from the end
buffer[index] = '\0'; // Null-terminate
const char* hexDigits = "0123456789ABCDEF";
if (num == 0) {
buffer[--index] = '0';
} else {
while (num > 0) {
buffer[--index] = hexDigits[num % 16];
num /= 16;
}
}
for (int j = index; buffer[j] != '\0'; j++) {
int position = cursorRow * SCREEN_WIDTH + cursorCol;
VideoMemory[position] = (color << 8) | buffer[j];
cursorCol++;
if (cursorCol >= SCREEN_WIDTH) {
cursorCol = 0;
cursorRow++;
}
if (cursorRow >= SCREEN_HEIGHT) {
scrollScreen();
cursorRow = SCREEN_HEIGHT - 1;
}
}
break;
}
case 's': { // String
const char* str = va_arg(args, const char*);
for (int j = 0; str[j] != '\0'; j++) {
int position = cursorRow * SCREEN_WIDTH + cursorCol;
VideoMemory[position] = (color << 8) | str[j];
cursorCol++;
if (cursorCol >= SCREEN_WIDTH) {
cursorCol = 0;
cursorRow++;
}
if (cursorRow >= SCREEN_HEIGHT) {
scrollScreen();
cursorRow = SCREEN_HEIGHT - 1;
}
}
break;
}
default:
break;
}
} else if (format[i] == '\n') {
cursorRow++;
cursorCol = 0; // Reset column to the beginning
// Handle scrolling if we exceed the screen height
if (cursorRow >= SCREEN_HEIGHT) {
scrollScreen();
cursorRow = SCREEN_HEIGHT - 1; // Move cursor to the last row
cursorCol = 0; // Ensure cursor starts at the beginning of the row
}
} else {
int position = cursorRow * SCREEN_WIDTH + cursorCol;
VideoMemory[position] = (color << 8) | format[i];
cursorCol++;
if (cursorCol >= SCREEN_WIDTH) {
cursorCol = 0;
cursorRow++;
}
if (cursorRow >= SCREEN_HEIGHT) {
scrollScreen();
cursorRow = SCREEN_HEIGHT - 1;
}
}
}
va_end(args);
updateCursor(cursorRow, cursorCol);
}