-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
45 lines (37 loc) · 1.21 KB
/
kernel.c
File metadata and controls
45 lines (37 loc) · 1.21 KB
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
/* kernel.c */
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
#define VID_MEM ((uint8_t*)0xb8000)
#define LIGHT_GREY_ON_BLACK 0x07
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
void clear_screen(uint8_t *vidptr) {
uint32_t j = 0;
/* This loop clears the screen */
/* 25 lines and 80 columns each element takes 2 bytes */
while(j < SCREEN_HEIGHT * SCREEN_WIDTH * 2) {
// Clear the screen with ' ' <- Space character
vidptr[j] = ' ';
// Attribute-byte - light grey on black screen
vidptr[j+1] = LIGHT_GREY_ON_BLACK;
j += 2;
}
}
void write_string(uint8_t *vidptr, const char *str) {
uint32_t i = 0;
while (*str) {
vidptr[i] = *str; // ASCII character
vidptr[i + 1] = LIGHT_GREY_ON_BLACK; // Attribute byte
++str;
i += 2; // Advance by 2 since we write 2 bytes each time
}
}
void kmain(void) {
const char *str = "Hello, Kernel! This is my first kernel";
uint8_t *vidptr = VID_MEM; // Video Memory begins here
// Clear the screen with a black screen and grey chars
clear_screen(vidptr);
// Write the buffer to the cleared screen
write_string(vidptr, str);
return;
}