-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
214 lines (182 loc) · 6.24 KB
/
Copy pathkernel.c
File metadata and controls
214 lines (182 loc) · 6.24 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stddef.h>
#include <stdint.h>
// Core OS includes
#include "idt.h"
#include "isr.h"
#include "pic.h"
#include "io.h" // Include for inb/outb if needed directly, or rely on drivers
// Driver/Module includes
#include "keyboard.h"
#include "shell.h"
#include "memory.h"
#include "filesystem.h"
// VGA color constants
enum vga_color {
VGA_BLACK = 0,
VGA_BLUE = 1,
VGA_GREEN = 2,
VGA_CYAN = 3,
VGA_RED = 4,
VGA_MAGENTA = 5,
VGA_BROWN = 6,
VGA_LIGHT_GREY = 7,
VGA_DARK_GREY = 8,
VGA_LIGHT_BLUE = 9,
VGA_LIGHT_GREEN = 10,
VGA_LIGHT_CYAN = 11,
VGA_LIGHT_RED = 12,
VGA_LIGHT_MAGENTA = 13,
VGA_LIGHT_BROWN = 14,
VGA_WHITE = 15,
};
static uint16_t* const VGA_MEMORY = (uint16_t*)0xB8000;
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static size_t terminal_row = 0;
static size_t terminal_column = 0;
// Create a VGA entry from character and color
static inline uint16_t vga_entry(unsigned char c, uint8_t color) {
return (uint16_t) c | (uint16_t) color << 8;
}
// Initialize the terminal
void terminal_initialize(void) {
uint8_t color = VGA_LIGHT_GREY | VGA_BLACK << 4;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
VGA_MEMORY[index] = vga_entry(' ', color);
}
}
terminal_reset_position();
}
// Reset cursor position
void terminal_reset_position() {
terminal_row = 0;
terminal_column = 0;
}
// Clear the terminal and reset position
void terminal_clear() {
terminal_initialize();
terminal_reset_position();
const char* welcome = "Welcome to MyOS!\nType 'help' for available commands.\n\n";
terminal_putstr(welcome);
}
// Put a single character at specified position
void terminal_putchar_at(char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
VGA_MEMORY[index] = vga_entry(c, color);
}
// Add this function before terminal_putchar
void terminal_scroll() {
uint8_t color = VGA_LIGHT_GREY | VGA_BLACK << 4;
// Move all lines up one position
for (size_t y = 0; y < VGA_HEIGHT - 1; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
size_t current_index = y * VGA_WIDTH + x;
size_t next_index = (y + 1) * VGA_WIDTH + x;
VGA_MEMORY[current_index] = VGA_MEMORY[next_index];
}
}
// Clear the last line
for (size_t x = 0; x < VGA_WIDTH; x++) {
size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x;
VGA_MEMORY[index] = vga_entry(' ', color);
}
}
// Modify the terminal_putchar function to use scrolling
void terminal_putchar(char c) {
uint8_t color = VGA_LIGHT_GREY | VGA_BLACK << 4;
// Handle newline character
if (c == '\n') {
terminal_column = 0;
terminal_row++;
if (terminal_row == VGA_HEIGHT) {
terminal_scroll();
terminal_row = VGA_HEIGHT - 1;
}
return;
}
terminal_putchar_at(c, color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
terminal_row++;
if (terminal_row == VGA_HEIGHT) {
terminal_scroll();
terminal_row = VGA_HEIGHT - 1;
}
}
}
// Handle backspace
void terminal_backspace() {
if (terminal_column > 0) {
terminal_column--;
terminal_putchar_at(' ', VGA_LIGHT_GREY | VGA_BLACK << 4, terminal_column, terminal_row);
}
else if (terminal_row > 0) {
terminal_row--;
terminal_column = VGA_WIDTH - 1;
terminal_putchar_at(' ', VGA_LIGHT_GREY | VGA_BLACK << 4, terminal_column, terminal_row);
}
}
void terminal_putstr(const char* str) {
for (size_t i = 0; str[i] != '\0'; i++) {
terminal_putchar(str[i]);
}
}
// Declare the external initialization function from memory.c
extern void initialize_static_memory();
void kernel_main(void) {
terminal_clear(); // Initialize terminal first for output
//terminal_putstr("Initializing Memory...\n");
initialize_static_memory(); // Initialize the memory allocator
//terminal_putstr("Memory Initialized.\n");
//terminal_putstr("Initializing Filesystem...\n");
fs_init(); // Initialize filesystem
//terminal_putstr("Filesystem Initialized.\n");
//terminal_putstr("Initializing Interrupts...\n");
idt_init(); // Set up IDT entries
pic_remap(0x20, 0x28); // Remap PIC: IRQs 0-7 -> 32-39, IRQs 8-15 -> 40-47
isr_install_handlers(); // Clear C handler table
//terminal_putstr("Interrupts Initialized.\n");
//terminal_putstr("Initializing Keyboard...\n");
keyboard_init_interrupts(); // Register keyboard IRQ handler
//terminal_putstr("Keyboard Initialized.\n");
// --- Enable Interrupts ---
//terminal_putstr("Enabling Interrupts (STI)...\n");
asm volatile ("sti");
//terminal_putstr("Interrupts Enabled.\n");
//terminal_putstr("Initializing Shell...\n");
init_shell(); // Display initial prompt
//terminal_putstr("Shell Initialized. Ready for input.\n");
char buffer[MAX_COMMAND_LENGTH];
int buffer_pos = 0;
// Interrupt-driven input loop
while(1) {
char c = keyboard_getchar(); // Read from the keyboard buffer (blocks if empty)
if(c == '\n') {
terminal_putchar('\n');
buffer[buffer_pos] = '\0'; // Null-terminate command
if (buffer_pos > 0) { // Process only if command is not empty
shell_process_command(buffer);
} else {
shell_prompt(); // Show prompt again if only Enter was pressed
}
buffer_pos = 0; // Reset buffer
} else if(c == '\b') { // Handle backspace
if (buffer_pos > 0) {
terminal_backspace();
buffer_pos--;
}
} else { // Handle regular characters
if(buffer_pos < MAX_COMMAND_LENGTH - 1) {
buffer[buffer_pos++] = c;
terminal_putchar(c); // Echo character
} else {
// Optional: Handle command buffer full (e.g., beep)
}
}
}
// Kernel should not exit
terminal_putstr("\nKernel Halted Unexpectedly.\n");
asm volatile ("cli; hlt");
}