basic os stuff
This commit is contained in:
79
kmain.c
Normal file
79
kmain.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#define BUF_SIZE 64
|
||||
#define HEAP_SIZE 8192
|
||||
static char heap[HEAP_SIZE];
|
||||
static int heap_top = 0;
|
||||
|
||||
void* kmalloc(int size) {
|
||||
void* ptr;
|
||||
if (heap_top + size > HEAP_SIZE) return 0;
|
||||
ptr = &heap[heap_top];
|
||||
heap_top += size;
|
||||
return ptr;
|
||||
}
|
||||
void print_char(char c);
|
||||
char get_char(void);
|
||||
int check_key(void);
|
||||
void set_cursor(void);
|
||||
void set_cursor_pos(char row, char col);
|
||||
int get_cursor_pos();
|
||||
int str_eq(char* a, char* b) {
|
||||
int i = 0;
|
||||
while (a[i] && b[i]) {
|
||||
if (a[i] != b[i]) return 0;
|
||||
i++;
|
||||
}
|
||||
return a[i] == b[i];
|
||||
}
|
||||
void print_string(char* str){
|
||||
int i = 0;
|
||||
while (str[i] != '\0'){
|
||||
print_char(str[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
void print_hex(unsigned int n) {
|
||||
char hex[] = "0x0000";
|
||||
int i;
|
||||
int nibble;
|
||||
for (i = 5; i >= 2; i--) {
|
||||
nibble = n & 0xF;
|
||||
hex[i] = nibble < 10 ? '0' + nibble : 'A' + nibble - 10;
|
||||
n >>= 4;
|
||||
}
|
||||
print_string(hex);
|
||||
}
|
||||
|
||||
|
||||
void read_line(char* buf) {
|
||||
int i = 0;
|
||||
char c;
|
||||
while (i < BUF_SIZE - 1) {
|
||||
c = get_char();
|
||||
if (c == '\r') break; /* Enter */
|
||||
if (c == '\b' && i > 0) { /* Backspace */
|
||||
i--;
|
||||
print_char('\b');
|
||||
print_char(' ');
|
||||
print_char('\b');
|
||||
} else {
|
||||
buf[i++] = c;
|
||||
print_char(c); /* echo */
|
||||
}
|
||||
}
|
||||
buf[i] = '\0';
|
||||
}
|
||||
void kmain(void) {
|
||||
char buf[BUF_SIZE];
|
||||
set_cursor();
|
||||
print_string("senkOS> ");
|
||||
while (1) {
|
||||
read_line(buf);
|
||||
print_char('\n');
|
||||
if (str_eq(buf, "hello")) {
|
||||
print_string("world\n");
|
||||
} else {
|
||||
print_string("unknown command\n");
|
||||
}
|
||||
print_string("senkOS> ");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user