-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.c
executable file
·99 lines (82 loc) · 1.62 KB
/
kernel.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
//This code based on https://www.codeproject.com/Articles/1225196/Create-Your-Own-Kernel-In-C
//License: Code Project Open License (CPOL)
#include"kernel.h"
static int Y_INDEX = 1;
static UINT16 VGA_DefaultEntry(unsigned char to_print) {
return (UINT16) to_print | (UINT16)WHITE_COLOR << 8;
}
void Clear_VGA_Buffer(UINT16 **buffer)
{
for(int i=0;i<BUFSIZE;i++){
(*buffer)[i] = '\0';
}
Y_INDEX = 1;
VGA_INDEX = 0;
}
void InitTerminal()
{
TERMINAL_BUFFER = (UINT16*) VGA_ADDRESS;
Clear_VGA_Buffer(&TERMINAL_BUFFER);
}
int digitCount(int num)
{
int count = 0;
if(num == 0)
return 1;
while(num > 0){
count++;
num = num/10;
}
return count;
}
void itoa(int num, char *number)
{
int digit_count = digitCount(num);
int index = digit_count - 1;
char x;
if(num == 0 && digit_count == 1){
number[0] = '0';
number[1] = '\0';
}else{
while(num != 0){
x = num % 10;
number[index] = x + '0';
index--;
num = num / 10;
}
number[digit_count] = '\0';
}
}
void printNewLine()
{
if(Y_INDEX >= 55){
Y_INDEX = 0;
Clear_VGA_Buffer(&TERMINAL_BUFFER);
}
VGA_INDEX = 80*Y_INDEX;
Y_INDEX++;
}
void printString(char *str)
{
int index = 0;
while(str[index]){
TERMINAL_BUFFER[VGA_INDEX] = VGA_DefaultEntry(str[index]);
index++;
VGA_INDEX++;
}
}
void printInt(int num)
{
char str_num[digitCount(num)+1];
itoa(num, str_num);
printString(str_num);
}
void KERNEL_MAIN()
{
TERMINAL_BUFFER = (UINT16*) VGA_ADDRESS;
printString("Hello World!");
printNewLine();
printInt(1234567890);
printNewLine();
printString("GoodBye World!");
}