-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
342 lines (285 loc) · 8.19 KB
/
main.cpp
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
#include "system.h"
#include "multiboot2.h"
void debug_addr(const char* title, uintptr_t addr)
{
size_t ud = (size_t)addr;
char buf[50];
char *p = buf;
debug_print(title);
debug_print("0x");
do
{
int remainder = ud % 16;
*p++ = (remainder < 10) ? remainder + '0' : remainder + 'A' - 10;
} while (ud /= 16);
*p = 0;
char *p1 = buf;
char *p2 = p - 1;
while (p1 < p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
debug_print(buf);
}
void debug_size(const char* title, size_t nsize, bool bAppendBytes)
{
size_t ud = nsize;
char buf[50];
char *p = buf;
debug_print(title);
do
{
int remainder = ud % 10;
*p++ = remainder + '0';
} while (ud /= 10);
*p = 0;
char *p1 = buf;
char *p2 = p - 1;
while (p1 < p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
debug_print(buf);
if (bAppendBytes)
debug_print(" bytes");
}
uint64_t read_tsc(void)
{
uint64_t retval;
asm volatile(".byte 0x10, 0x31" : "=A"(retval));
return retval;
}
namespace system {
k_object *root = nullptr;
}
extern "C"
void kmain(uintptr_t mbi)
{
debug_init();
debug_print("\n\033[1;31m------------------- C++ Started ------------------\033[0m\n");
// Testing local variable instance addresses
// Most likely it's using lower half address.
int intLocalVar = 12345678;
static int sValue = 1;
DEBUG_PTR("\n[kmain] Local Integer Variable address (stack) => ", &intLocalVar);
DEBUG_PTR("\n[constant] Testing constant addr => ", "This is a test");
DEBUG_PTR("\n[static] Testing static local var => ", &sValue);
debug_addr("\n[kmain] MBI => ", mbi);
debug_size("; size = ", *((unsigned*)mbi), true);
multiboot_tag* tag = reinterpret_cast<multiboot_tag*>(mbi + 8);
multiboot_tag_module *module_tag = nullptr;
uintptr_t pos = mbi + 8;
uintptr_t vga = 0xb8000;
multiboot_tag_framebuffer *fbtag = nullptr;
debug_size("\n[kmain] MBI First tag -> ", tag->type);
while (tag->type != MULTIBOOT_TAG_TYPE_END)
{
if (tag->type == MULTIBOOT_TAG_TYPE_FRAMEBUFFER)
{
fbtag = reinterpret_cast<multiboot_tag_framebuffer*>(tag);
debug_addr("\n[kmain] Framebuffer found at ", fbtag->common.framebuffer_addr);
debug_size("\n width : ", fbtag->common.framebuffer_width);
debug_size("\n height : ", fbtag->common.framebuffer_height);
debug_size("\n bpp : ", fbtag->common.framebuffer_bpp);
debug_size("\n pitch : ", fbtag->common.framebuffer_pitch);
if (fbtag->common.framebuffer_type == 2)
{
debug_print("\nDummy VGA/Text 80x25 framebuffer console.\nClearing the screen (using turquoise color).\n");
char *ptr = (char*)(fbtag->common.framebuffer_addr);
for (int i = 0; i < (80 * 25); i++)
{
ptr[0] = 0x20;
ptr[1] = 0x3f;
ptr += 2;
}
vga = 0xffffffff80000000 + fbtag->common.framebuffer_addr;
}
else
{
if (fbtag->common.framebuffer_addr == 0xFD000000)
debug_print("\nQEMU VBE based framebuffer.\n");
else if (fbtag->common.framebuffer_addr == 0x80000000)
debug_print("\nQEMU UEFI based framebuffer.\n");
else
debug_print("\nMost likely we are on REAL hardware, and this text may not be displayed.\n");
debug_print("[kmain] Framebuffer object will be created after we initialize the heap.\n");
}
}
else if (tag->type == MULTIBOOT_TAG_TYPE_MODULE)
{
module_tag = reinterpret_cast<multiboot_tag_module*>(tag);
DEBUG_PTR("\n[kmain] Found a module at => ", module_tag);
debug_print("; cmdline = ");
debug_print(module_tag->cmdline);
if (k_str_equal(module_tag->cmdline, "font", k_strlen("font")))
debug_print("\n[kmain] The module is a font.");
debug_print("\n");
}
pos = (pos + tag->size + 7) & ~7;
tag = reinterpret_cast<multiboot_tag*>(pos);
}
uintptr_t heap_start = (uintptr_t)&kernel_virt_end;
system::k_object *pHeap = reinterpret_cast<system::k_object*>(heap_start);
pHeap->length = 4096;
pHeap->data = heap_start + sizeof(system::k_object);
pHeap->next = nullptr;
pHeap->limit = pHeap->data + 4096;
if (pHeap->limit & 0xFFFFFFFFFFFFF000)
{
pHeap->limit &= 0xFFFFFFFFFFFFF000;
pHeap->limit += 0x1000;
}
system::root = pHeap;
debug_size("\n[kmain] testing k_strlen() with \'This is a test\' => ",
k_strlen("This is a test"));
debug_print("\n\033[1;31m[kmain] Heap initialized.\n\033[0m");
system::String *pStr = new system::String();
pStr->Copy("\033[1;33m[kmain] system::String using heap.\n\033[0m");
pStr->Debug();
delete pStr;
pStr = new system::String("\033[1;34m[kmain] system::String using heap, and designated initializer.\n\033[0m");
pStr->Debug();
delete pStr;
debug_print("[kmain] Testing framebuffer\n");
system::fbdev = new system::framebuffer((uintptr_t)fbtag);
if (system::fbdev->virt)
{
debug_print("\n[kmain] Looks like the framebuffer is ready, clearing screen.\n");
system::fbdev->clear();
if (module_tag)
{
system::fbdev->font = reinterpret_cast<PSF_FONT*>(module_tag->mod_start);
system::fbdev->show_test();
}
}
system::box *pBox = new system::box();
pBox->set_title("Terminal");
pBox->show(10, 50);
system::box *pRTCBox = new system::box();
pRTCBox->set_title("RTC Time");
pRTCBox->show(25, 200);
debug_print("\n[kmain] Time to test the keyboard inputs.\n");
char lastChar = system::getchar();
char arr[80];
while (lastChar)
{
// display current time using new RTC function
while (k_rtc_update_in_progress());
uint8_t hours = k_get_rtc(RTC_HOURS);
uint8_t minutes = k_get_rtc(RTC_MINUTES);
uint8_t secs = k_get_rtc(RTC_SECONDS);
////////////////////////////////////////////////
// actually we must convert these values
// otherwise it will be displayed incorrectly
// if these are in BCD.
//
// Or we can choose to display BCD values as 'HEX',
// but of course without '0x' prefixes.
////////////////////////////////////////////////
uint8_t regb = k_rtc_get_value(0x0b);
if ((regb & 0x04) == 0)
{
//////////////////////////////////////////////////////////////////////////////
// This means the values are in BCD format.
// If the time is 22:16:34, then it can be
// displayed 'correctly' as:
// 0x22:0x16:0x34
// Where 0x22 is actually 34, 0x16 is 22, and 0x34 surely is 52
//
// Confused?
// :-)
/////////////////////////////////////////////////////////////////////////////
hours = ((hours & 0xf) + (((hours & 0x70) / 16) * 10)) | (hours & 0x80);
minutes = (minutes & 0xf) + ((minutes / 16) * 10);
secs = (secs & 0xf) + ((secs / 16) * 10);
}
arr[0] = 0;
char *pos = k_strcat_l(arr, "Current Time: ", hours);
pos = k_strcat_l(pos, ":", minutes);
pos = k_strcat_l(pos, ":", secs);
pRTCBox->set_text(arr);
pRTCBox->show(50, 250);
int nIndex = 0;
if (lastChar == -1)
{
lastChar = system::getchar();
continue;
}
arr[nIndex] = lastChar;
arr[nIndex+1] = 0;
nIndex++;
if (nIndex >= 80)
{
pBox->set_text("cmd> ");
lastChar = system::getchar();
pBox->show(10, 50);
continue;
}
pBox->set_text(arr);
lastChar = system::getchar();
pBox->show(10, 50);
}
}
char *k_strcat(char *p1, const char *p2)
{
char *dest = p1;
char *src = const_cast<char*>(p2);
while (*src)
*dest++ = *src++;
*dest = 0;
return dest;
}
char *k_strcat_x(char *p1, const char *title, uintptr_t value)
{
char *last = k_strcat(p1, title);
*last++ = '0';
*last++ = 'x';
size_t ud = value;
char *p = last;
do
{
int remainder = ud % 16;
*last++ = (remainder < 10) ? remainder + '0' : remainder + 'A' - 10;
} while (ud /= 16);
*last = 0;
char *p2 = last - 1;
while (p < p2)
{
char tmp = *p;
*p = *p2;
*p2 = tmp;
p++;
p2--;
}
return last;
}
char *k_strcat_l(char *p1, const char *title, size_t value)
{
char *last = k_strcat(p1, title);
size_t ud = value;
char *p = last;
do
{
int remainder = ud % 10;
*last++ = remainder + '0';
} while (ud /= 10);
*last = 0;
char *p2 = last - 1;
while (p < p2)
{
char tmp = *p;
*p = *p2;
*p2 = tmp;
p++;
p2--;
}
return last;
}