Refactorings

This commit is contained in:
Baptiste Wicht 2013-10-28 20:24:15 +01:00
parent 3714e6941e
commit 16988ce0cb
6 changed files with 144 additions and 104 deletions

View File

@ -12,7 +12,7 @@ micro_kernel.bin: $(MICRO_KERNEL_SRC) $(MICRO_KERNEL_UTILS_SRC)
KERNEL_FLAGS=-masm=intel -Ikernel/include/ -O1 -std=c++11 -Wall -Wextra -fno-exceptions -fno-rtti -ffreestanding
KERNEL_LINK_FLAGS=-std=c++11 -T linker.ld -ffreestanding -O1 -nostdlib
KERNEL_O_FILES=kernel.o keyboard.o console.o kernel_utils.o timer.o
KERNEL_O_FILES=kernel.o keyboard.o console.o kernel_utils.o timer.o shell.o
kernel.o: kernel/src/kernel.cpp
g++ $(KERNEL_FLAGS) -c kernel/src/kernel.cpp -o kernel.o
@ -29,6 +29,9 @@ kernel_utils.o: kernel/src/kernel_utils.cpp
timer.o: kernel/src/timer.cpp
g++ $(KERNEL_FLAGS) -c kernel/src/timer.cpp -o timer.o
shell.o: kernel/src/shell.cpp
g++ $(KERNEL_FLAGS) -c kernel/src/shell.cpp -o shell.o
kernel.bin: $(KERNEL_O_FILES)
g++ $(KERNEL_LINK_FLAGS) -o kernel.bin.o $(KERNEL_O_FILES)
objcopy -R .note -R .comment -S -O binary kernel.bin.o kernel.bin

View File

@ -1,6 +1,8 @@
#ifndef CONSOLE_H
#define CONSOLE_H
#include <cstddef>
void set_column(long column);
long get_column();

6
kernel/include/shell.hpp Normal file
View File

@ -0,0 +1,6 @@
#ifndef SHELL_H
#define SHELL_
void init_shell();
#endif

View File

@ -94,6 +94,9 @@ void k_print(const char* string){
}
void wipeout(){
current_line = 0;
current_column = 0;
for(int line = 0; line < 25; ++line){
for(std::size_t column = 0; column < 80; ++column){
k_print(' ');

View File

@ -1,118 +1,18 @@
#include <cstddef>
#include <array>
#include "types.hpp"
#include "keyboard.hpp"
#include "kernel_utils.hpp"
#include "console.hpp"
#include "timer.hpp"
#include "shell.hpp"
void keyboard_handler();
void clear_command();
extern "C" {
void __attribute__ ((section ("main_section"))) kernel_main(){
wipeout();
k_print("thor> ");
register_irq_handler<1>(keyboard_handler);
init_shell();
install_timer();
return;
}
}
std::size_t current_input_length = 0;
char current_input[50];
void exec_command();
void keyboard_handler(){
uint8_t key = in_byte(0x60);
if(key & 0x80){
//TODO Handle shift
} else {
if(key == 0x1C){
current_input[current_input_length] = '\0';
k_print_line();
exec_command();
current_input_length = 0;
k_print("thor> ");
} else if(key == 0x0E){
set_column(get_column() - 1);
k_print(' ');
set_column(get_column() - 1);
--current_input_length;
} else {
auto qwertz_key = key_to_ascii(key);
if(qwertz_key > 0){
current_input[current_input_length++] = qwertz_key;
k_print(qwertz_key);
}
}
}
}
bool str_equals(const char* a, const char* b){
while(*a && *a == *b){
++a;
++b;
}
return *a == *b;
}
struct command_definition {
const char* name;
void (*function)();
};
void reboot_command();
void help_command();
void uptime_command();
std::array<command_definition, 3> commands = {{
{"reboot", reboot_command},
{"help", help_command},
{"uptime", uptime_command}
}};
void reboot_command(){
interrupt<60>();
}
void help_command(){
k_print_line("Available commands:");
for(auto& command : commands){
k_print(" ");
k_print_line(command.name);
}
}
void uptime_command(){
k_print("Uptime: ");
k_print(timer_seconds());
k_print_line("s");
}
void exec_command(){
for(auto& command : commands){
if(str_equals(current_input, command.name)){
command.function();
return;
}
}
k_print("The command \"");
k_print(current_input);
k_print_line("\" does not exist");
}

126
kernel/src/shell.cpp Normal file
View File

@ -0,0 +1,126 @@
#include <cstddef>
#include <array>
#include "types.hpp"
#include "keyboard.hpp"
#include "kernel_utils.hpp"
#include "console.hpp"
#include "shell.hpp"
#include "timer.hpp"
namespace {
//Declarations of the different functions
void reboot_command();
void help_command();
void uptime_command();
void clear_command();
std::size_t current_input_length = 0;
char current_input[50];
void exec_command();
void keyboard_handler(){
uint8_t key = in_byte(0x60);
if(key & 0x80){
//TODO Handle shift
} else {
if(key == 0x1C){
current_input[current_input_length] = '\0';
k_print_line();
exec_command();
current_input_length = 0;
k_print("thor> ");
} else if(key == 0x0E){
set_column(get_column() - 1);
k_print(' ');
set_column(get_column() - 1);
--current_input_length;
} else {
auto qwertz_key = key_to_ascii(key);
if(qwertz_key > 0){
current_input[current_input_length++] = qwertz_key;
k_print(qwertz_key);
}
}
}
}
bool str_equals(const char* a, const char* b){
while(*a && *a == *b){
++a;
++b;
}
return *a == *b;
}
struct command_definition {
const char* name;
void (*function)();
};
std::array<command_definition, 4> commands = {{
{"reboot", reboot_command},
{"help", help_command},
{"uptime", uptime_command},
{"clear", clear_command}
}};
void reboot_command(){
interrupt<60>();
}
void help_command(){
k_print_line("Available commands:");
for(auto& command : commands){
k_print(" ");
k_print_line(command.name);
}
}
void uptime_command(){
k_print("Uptime: ");
k_print(timer_seconds());
k_print_line("s");
}
void exec_command(){
for(auto& command : commands){
if(str_equals(current_input, command.name)){
command.function();
return;
}
}
k_print("The command \"");
k_print(current_input);
k_print_line("\" does not exist");
}
void clear_command(){
wipeout();
}
} //end of anonymous namespace
void init_shell(){
current_input_length = 0;
clear_command();
k_print("thor> ");
register_irq_handler<1>(keyboard_handler);
}