-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugDisplay.c
executable file
·345 lines (278 loc) · 7.24 KB
/
DebugDisplay.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//****************************************************************************
//**
//** DebugDisplay.cpp
//** - Provides display capabilities for debugging. Because it is
//** specifically for debugging and not final release, we don't
//** care for portability here
//**
//****************************************************************************
//============================================================================
// IMPLEMENTATION HEADERS
//============================================================================
#include "DebugDisplay.h"
#include "string.h"
//============================================================================
// IMPLEMENTATION PRIVATE DEFINITIONS / ENUMERATIONS / SIMPLE TYPEDEFS
//============================================================================
//============================================================================
// IMPLEMENTATION PRIVATE CLASS PROTOTYPES / EXTERNAL CLASS REFERENCES
//============================================================================
//============================================================================
// IMPLEMENTATION PRIVATE STRUCTURES / UTILITY CLASSES
//============================================================================
//============================================================================
// IMPLEMENTATION REQUIRED EXTERNAL REFERENCES (AVOID)
//============================================================================
//============================================================================
// IMPLEMENTATION PRIVATE DATA
//============================================================================
//! video memory
uint16_t *video_memory = (uint16_t *)0xB8000;
//! current position
uint8_t cursor_x = 0;
uint8_t cursor_y = 0;
//! current color
uint8_t _color = 0;
//============================================================================
// INTERFACE DATA
//============================================================================
//============================================================================
// IMPLEMENTATION PRIVATE FUNCTION PROTOTYPES
//============================================================================
//============================================================================
// IMPLEMENTATION PRIVATE FUNCTIONS
//============================================================================
//! Updates hardware cursor
void DebugUpdateCur(int x, int y)
{
// get location
uint16_t cursorLocation = y * 80 + x;
// send location to vga controller to set cursor
// outportb(0x3D4, 14);
// outportb(0x3D5, cursorLocation >> 8); // Send the high byte.
// outportb(0x3D4, 15);
// outportb(0x3D5, cursorLocation); // Send the low byte.
}
void scroll()
{
if (cursor_y >= 25)
{
uint16_t attribute = _color << 8;
//! move current display up one line
for (int i = 0 * 80; i < 24 * 80; i++)
video_memory[i] = video_memory[i + 80];
//! clear the bottom line
for (int i = 24 * 80; i < 25 * 80; i++)
video_memory[i] = attribute | ' ';
cursor_y = 24;
}
}
//! Displays a character
void DebugPutc(unsigned char c)
{
uint16_t attribute = _color << 8;
//! backspace character
if (c == 0x08 && cursor_x)
cursor_x--;
//! tab character
else if (c == 0x09)
cursor_x = (cursor_x + 8) & ~(8 - 1);
//! carriage return
else if (c == '\r')
cursor_x = 0;
//! new line
else if (c == '\n')
{
cursor_x = 0;
cursor_y++;
}
//! printable characters
else if (c >= ' ')
{
//! display character on screen
uint16_t *location = video_memory + (cursor_y * 80 + cursor_x);
*location = c | attribute;
cursor_x++;
}
//! if we are at edge of row, go to new line
if (cursor_x >= 80)
{
cursor_x = 0;
cursor_y++;
}
//! if we are at the last line, scroll up
if (cursor_y >= 25)
scroll();
//! update hardware cursor
DebugUpdateCur(cursor_x, cursor_y);
}
char tbuf[32];
char bchars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
void itoa(unsigned i, unsigned base, char *buf)
{
int pos = 0;
int opos = 0;
int top = 0;
if (i == 0 || base > 16)
{
buf[0] = '0';
buf[1] = '\0';
return;
}
while (i != 0)
{
tbuf[pos] = bchars[i % base];
pos++;
i /= base;
}
top = pos--;
for (opos = 0; opos < top; pos--, opos++)
{
buf[opos] = tbuf[pos];
}
buf[opos] = 0;
}
// NOTE: Using long long to prevent sign is changed due to hex memory address beyonds long's scope
void itoa_s(long long i, unsigned base, char *buf)
{
if (base > 16)
return;
if (i < 0)
{
*buf++ = '-';
i *= -1;
}
itoa(i, base, buf);
}
//============================================================================
// INTERFACE FUNCTIONS
//============================================================================
//! Sets new font color
unsigned DebugSetColor(const unsigned c)
{
unsigned t = _color;
_color = c;
return t;
}
//! Sets new position
void DebugGotoXY(unsigned x, unsigned y)
{
if (cursor_x <= 80)
cursor_x = x;
if (cursor_y <= 25)
cursor_y = y;
//! update hardware cursor to new position
DebugUpdateCur(cursor_x, cursor_y);
}
//! returns position
void DebugGetXY(unsigned *x, unsigned *y)
{
if (x == 0 || y == 0)
return;
*x = cursor_x;
*y = cursor_y;
}
//! returns horzontal width
int DebugGetHorz()
{
return 80;
}
//! returns vertical height
int DebugGetVert()
{
return 24;
}
//! Clear screen
void DebugClrScr(const uint8_t c)
{
//! clear video memory by writing space characters to it
for (int i = 0; i < 80 * 25; i++)
video_memory[i] = ' ' | (c << 8);
//! move position back to start
DebugGotoXY(0, 0);
}
//! Displays a string
void DebugPuts(char *str)
{
if (!str)
return;
//! err... displays a string
for (unsigned int i = 0; i < strlen(str); i++)
DebugPutc(str[i]);
}
//! Displays a formatted string
int DebugPrintf(const char *str, ...)
{
if (!str)
return 0;
va_list args;
va_start(args, str);
size_t i;
for (i = 0; i < strlen(str); i++)
{
switch (str[i])
{
case '%':
switch (str[i + 1])
{
/*** characters ***/
case 'c':
{
char c = va_arg(args, int);
DebugPutc(c);
i++; // go to next character
break;
}
/*** address of ***/
case 's':
{
char *c = va_arg(args, char *);
char str[64];
strcpy(str, (const char *)c);
DebugPuts(str);
i++; // go to next character
break;
}
/*** integers ***/
case 'd':
case 'i':
{
int c = va_arg(args, int);
char str[32] = {0};
itoa_s(c, 10, str);
DebugPuts(str);
i++; // go to next character
break;
}
/*** display in hex ***/
case 'X':
case 'x':
{
unsigned int c = va_arg(args, unsigned int);
char str[32] = {0};
itoa_s(c, 16, str);
DebugPuts(str);
i++; // go to next character
break;
}
default:
va_end(args);
return 1;
}
break;
default:
DebugPutc(str[i]);
break;
}
}
va_end(args);
return i;
}
//============================================================================
// INTERFACE CLASS BODIES
//============================================================================
//****************************************************************************
//**
//** END[DebugDisplay.cpp]
//**
//****************************************************************************