Skip to content

Commit 23741e4

Browse files
committed
New version, License & new implementations
1 parent 1365047 commit 23741e4

File tree

1 file changed

+136
-14
lines changed

1 file changed

+136
-14
lines changed

kernel/kernel.c

Lines changed: 136 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1+
/**
2+
* @file kernel.c
3+
* @author Owen Boreham (owenkadeboreham@gmail.com)
4+
* @version 0.1.3
5+
* @date 2021-07-06
6+
*
7+
* @copyright Copyright (c) 2021 TinyKernel
8+
* This file is part of TinyKernel which is released
9+
* under Apache License 2.0. See file LICENSE or go
10+
* to https://www.apache.org/licenses/LICENSE-2.0 for
11+
* full license details.
12+
*/
13+
14+
115
#include "kernel.h"
216

17+
// Index for video buffer array
18+
uint32 vga_index;
19+
// Counter to store new lines
20+
static uint32 next_line_index = 1;
21+
// Fore & Back color values
22+
uint8 g_fore_color = WHITE, g_back_color = BLUE;
23+
// digit ascii code for printing integers
24+
int digit_ascii_codes[10] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
25+
26+
327
/*
428
* 16 bit video buffer elements(register ax)
529
* 8 bits(ah) higher :
@@ -8,7 +32,7 @@
832
* 8 bits(al) lower :
933
* 8 bits : ASCII character to print
1034
* */
11-
uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color)
35+
uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color)
1236
{
1337
uint16 ax = 0;
1438
uint8 ah = 0, al = 0;
@@ -28,33 +52,131 @@ uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color)
2852
void clear_vga_buffer(uint16 **buffer, uint8 fore_color, uint8 back_color)
2953
{
3054
uint32 i;
31-
for(i = 0; i < BUFSIZE; i++) {
55+
for(i = 0; i < BUFSIZE; i++)
56+
{
3257
(*buffer)[i] = vga_entry(NULL, fore_color, back_color);
3358
}
59+
next_line_index = 1;
60+
vga_index = 0;
3461
}
3562

3663
// Initialize vga buffer
3764
void init_vga(uint8 fore_color, uint8 back_color)
3865
{
3966
vga_buffer = (uint16*)VGA_ADDRESS; // Point vga_buffer pointer to VGA_ADDRESS
4067
clear_vga_buffer(&vga_buffer, fore_color, back_color);
68+
g_fore_color = fore_color;
69+
g_back_color = back_color;
70+
}
71+
72+
void default_colors()
73+
{
74+
g_fore_color = WHITE;
75+
g_back_color = BLACK;
76+
}
77+
78+
/*
79+
Increase vga_index by width of row(80)
80+
*/
81+
void print_new_line()
82+
{
83+
if (next_line_index >= 55)
84+
{
85+
next_line_index = 0;
86+
clear_vga_buffer(&vga_buffer, g_fore_color, g_back_color);
87+
}
88+
vga_index = 80 * next_line_index;
89+
next_line_index++;
90+
}
91+
92+
// Assign ascii character to video buffer
93+
void print_char(char ch)
94+
{
95+
vga_buffer[vga_index] = vga_entry(ch, g_fore_color, g_back_color);
96+
vga_index++;
97+
}
98+
99+
uint32 strlen(const char* str)
100+
{
101+
uint32 length = 0;
102+
while (str[length])
103+
length++;
104+
return length;
105+
}
106+
107+
uint32 digit_count(int num)
108+
{
109+
uint32 count = 0;
110+
if (num == 0)
111+
return 1;
112+
while(num > 0)
113+
{
114+
count++;
115+
num = num/10;
116+
}
117+
return count;
118+
}
119+
120+
void itoa(int num, char *number)
121+
{
122+
int dgcount = digit_count(num);
123+
int index = dgcount - 1;
124+
char x;
125+
if (num == 0 && dgcount == 1)
126+
{
127+
number[0] = '0';
128+
number[1] = '\0';
129+
}
130+
else
131+
{
132+
while (num != 0)
133+
{
134+
x = num % 10;
135+
number[index] = x + '0';
136+
index--;
137+
num = num / 10;
138+
}
139+
number[dgcount] = '\0';
140+
}
141+
}
142+
143+
// Print string is calling print_char
144+
void print_string(char *str)
145+
{
146+
uint32 index = 0;
147+
while (str[index])
148+
{
149+
print_char(str[index]);
150+
index++;
151+
}
152+
}
153+
154+
// Print int by converting it into string
155+
// then print string
156+
void print_int(int num)
157+
{
158+
char str_num[digit_count(num)+1];
159+
itoa(num, str_num);
160+
print_string(str_num);
41161
}
42162

43163
void kernel_entry()
44164
{
45165
// First init vga with fore & back colors
46166
init_vga(WHITE, BLACK);
47167

48-
// Assign each ASCII character to video buffer
49-
vga_buffer[0] = vga_entry('H', WHITE, BLACK);
50-
vga_buffer[1] = vga_entry('e', WHITE, BLACK);
51-
vga_buffer[2] = vga_entry('l', WHITE, BLACK);
52-
vga_buffer[3] = vga_entry('l', WHITE, BLACK);
53-
vga_buffer[4] = vga_entry('o', WHITE, BLACK);
54-
vga_buffer[5] = vga_entry(' ', WHITE, BLACK);
55-
vga_buffer[6] = vga_entry('W', WHITE, BLACK);
56-
vga_buffer[7] = vga_entry('o', WHITE, BLACK);
57-
vga_buffer[8] = vga_entry('r', WHITE, BLACK);
58-
vga_buffer[9] = vga_entry('l', WHITE, BLACK);
59-
vga_buffer[10] = vga_entry('d', WHITE, BLACK);
168+
/**
169+
* @brief Printing "Hello World!\n", "1234456789\n", "Goodbye World!"
170+
* Call the above function to print something
171+
* here to change the fore & back color,
172+
* assign g_fore_color & g_back_color to color values
173+
* g_fore_color = BRIGHT_RED;
174+
*/
175+
print_string("Hello World!");
176+
print_new_line();
177+
g_fore_color = BRIGHT_GREEN;
178+
print_int(123456789);
179+
default_colors();
180+
print_new_line();
181+
print_string("Goodbye World!");
60182
}

0 commit comments

Comments
 (0)