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
+
1
15
#include "kernel.h"
2
16
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
+
3
27
/*
4
28
* 16 bit video buffer elements(register ax)
5
29
* 8 bits(ah) higher :
8
32
* 8 bits(al) lower :
9
33
* 8 bits : ASCII character to print
10
34
* */
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 )
12
36
{
13
37
uint16 ax = 0 ;
14
38
uint8 ah = 0 , al = 0 ;
@@ -28,33 +52,131 @@ uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color)
28
52
void clear_vga_buffer (uint16 * * buffer , uint8 fore_color , uint8 back_color )
29
53
{
30
54
uint32 i ;
31
- for (i = 0 ; i < BUFSIZE ; i ++ ) {
55
+ for (i = 0 ; i < BUFSIZE ; i ++ )
56
+ {
32
57
(* buffer )[i ] = vga_entry (NULL , fore_color , back_color );
33
58
}
59
+ next_line_index = 1 ;
60
+ vga_index = 0 ;
34
61
}
35
62
36
63
// Initialize vga buffer
37
64
void init_vga (uint8 fore_color , uint8 back_color )
38
65
{
39
66
vga_buffer = (uint16 * )VGA_ADDRESS ; // Point vga_buffer pointer to VGA_ADDRESS
40
67
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 );
41
161
}
42
162
43
163
void kernel_entry ()
44
164
{
45
165
// First init vga with fore & back colors
46
166
init_vga (WHITE , BLACK );
47
167
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!" );
60
182
}
0 commit comments