-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.c
147 lines (129 loc) · 2.82 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
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
#include "kernel.h"
//index for video buffer array
uint32 vga_index;
//counter to store new lines
static uint32 next_line_index = 1;
//fore & back color values
uint8 g_fore_color = WHITE, g_back_color = BLUE;
//digit ascii code for printing integers
int digit_ascii_codes[10] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
/*
16 bit video buffer elements(register ax)
8 bits(ah) higher :
lower 4 bits - forec olor
higher 4 bits - back color
8 bits(al) lower :
8 bits : ASCII character to print
*/
uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color)
{
uint16 ax = 0;
uint8 ah = 0, al = 0;
ah = back_color;
ah <<= 4;
ah |= fore_color;
ax = ah;
ax <<= 8;
al = ch;
ax |= al;
return ax;
}
//clear video buffer array
void clear_vga_buffer(uint16 **buffer, uint8 fore_color, uint8 back_color)
{
uint32 i;
for(i = 0; i < BUFSIZE; i++){
(*buffer)[i] = vga_entry(NULL, fore_color, back_color);
}
next_line_index = 1;
vga_index = 0;
}
//initialize vga buffer
void init_vga(uint8 fore_color, uint8 back_color)
{
vga_buffer = (uint16*)VGA_ADDRESS;
clear_vga_buffer(&vga_buffer, fore_color, back_color);
g_fore_color = fore_color;
g_back_color = back_color;
}
/*
increase vga_index by width of row(80)
*/
void print_new_line()
{
if(next_line_index >= 55){
next_line_index = 0;
clear_vga_buffer(&vga_buffer, g_fore_color, g_back_color);
}
vga_index = 80*next_line_index;
next_line_index++;
}
//assign ascii character to video buffer
void print_char(char ch)
{
vga_buffer[vga_index] = vga_entry(ch, g_fore_color, g_back_color);
vga_index++;
}
uint32 strlen(const char* str)
{
uint32 length = 0;
while(str[length])
length++;
return length;
}
uint32 digit_count(int num)
{
uint32 count = 0;
if(num == 0)
return 1;
while(num > 0){
count++;
num = num/10;
}
return count;
}
void itoa(int num, char *number)
{
int dgcount = digit_count(num);
int index = dgcount - 1;
char x;
if(num == 0 && dgcount == 1){
number[0] = '0';
number[1] = '\0';
}else{
while(num != 0){
x = num % 10;
number[index] = x + '0';
index--;
num = num / 10;
}
number[dgcount] = '\0';
}
}
//print string by calling print_char
void print_string(char *str)
{
uint32 index = 0;
while(str[index]){
print_char(str[index]);
index++;
}
}
//print int by converting it into string
//& then printing string
void print_int(int num)
{
char str_num[digit_count(num)+1];
itoa(num, str_num);
print_string(str_num);
}
void kernel_entry()
{
//first init vga with fore & back colors
init_vga(YELLOW, BLACK);
print_string("Rhoudveine Os Ver alpha 00001");
print_new_line();;
print_string("The Kernel Is Under Developement By Reyhank45 And Owned By Altec.INC");
print_new_line();
print_string("Boot Completed!");
}