From c4cc22b897fed06d040d8fdcc0b40b0f0dcf5bcf Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Tue, 8 Jul 2014 19:09:31 +0200 Subject: Further celanup and a little modularization --- kernel/console.c | 94 +++++++++---------- kernel/console.h | 29 +++--- kernel/interrupts.c | 115 ++++++++++++++++++++++++ kernel/interrupts.h | 10 +++ kernel/kernel.c | 255 +++------------------------------------------------- kernel/kernel.h | 7 ++ kernel/keyboard.c | 118 ++++++++++++++++++++++++ 7 files changed, 328 insertions(+), 300 deletions(-) create mode 100644 kernel/interrupts.c create mode 100644 kernel/interrupts.h create mode 100644 kernel/keyboard.c (limited to 'kernel') diff --git a/kernel/console.c b/kernel/console.c index f17dbe3..4c52e81 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -3,6 +3,46 @@ static int posx=0; static int posy=0; +// helper_funcs + +void print_char_col(int x, int y, char c, char col) +{ + char* video_mem=(char *)SCR_VIDEOMEM+(x+y*SCR_WIDTH)*2; + video_mem[0]=c; + video_mem[1]=col; +} + +void print_char(int x, int y, char c) +{ + print_char_col(x,y,c,SCR_WHITE); +} + +void print_single_hex(int i) +{ + if(i<10)print_char_col(posx,posy,'0'+i,SCR_GREEN); + else if(i<16)print_char_col(posx,posy,'A'+i-10,SCR_GREEN); + posx++; + +} + +void print_str_col(int x,int y,char *str, char col) +{ + + while(*str!=0) + { + print_char_col(x++,y,*(str++),col); + } + +} + +void print_str(int x,int y,char *str) +{ + print_str_col(x,y,str,SCR_WHITE); +} + + +// +// void scr_clear() { int x,y; @@ -10,14 +50,20 @@ void scr_clear() for(x=0;x //needed for uint16_t +#include "kernel.h" #define SCR_VIDEOMEM 0xb8000 @@ -11,20 +11,29 @@ #define SCR_CTRL 0x3D4 #define SCR_DATA 0x3D5 -#define SCR_BLACK 0x0 -#define SCR_BLUE 0x1 -#define SCR_GREEN 0x2 -#define SCR_CYAN 0x3 -#define SCR_RED 0x4 - -// TODO: more colors here... -# define SCR_WHITE 0xf +// colors +#define SCR_BLACK 0x0 +#define SCR_BLUE 0x1 +#define SCR_GREEN 0x2 +#define SCR_CYAN 0x3 +#define SCR_RED 0x4 +#define SCR_VIOLET 0x5 +#define SCR_BROWN 0x6 +#define SCR_GRAY1 0x7 +#define SCR_GRAY2 0x8 +#define SCR_LBLUE 0x9 +#define SCR_LGREEN 0xa +#define SCR_LCYAN 0xb +#define SCR_LRED 0xc +#define SCR_PINK 0xd +#define SCR_YELLOW 0xe +#define SCR_WHITE 0xf //autoscroll void scr_clear(); void scr_nextline(); void scr_put_string(char *str); +void scr_put_string_nl(char *str); void scr_put_hex(uint16_t val); -//void scr_put_int(uint16_t int); #endif diff --git a/kernel/interrupts.c b/kernel/interrupts.c new file mode 100644 index 0000000..198822e --- /dev/null +++ b/kernel/interrupts.c @@ -0,0 +1,115 @@ +#include "interrupts.h" + +// the interrupt descriptor table +static struct int_desc +{ + uint16_t addrLo; + uint16_t sel; + uint8_t zeros; + uint8_t flags; + uint16_t addrHi; + +} idt[INT_MAX]; + +// interrupt descriptor table descriptor +static struct idt_desc +{ + uint16_t size; + uint16_t baseLo; + uint16_t baseHi; + +} idtd; + +// disable interrupts +void int_disable() +{ + __asm__("cli"); +} + +// enable interrupts +void int_enable() +{ + __asm__("sti"); +} + + +// default handler +void int_def_handler() +{ + __asm__("pusha"); + + int_unhandled++; + + // todo also the other pic!// TODO + __asm__("mov $0x20, %al"); + __asm__("out %al, $0x20"); + + __asm__("popa"); + __asm__("leave"); + __asm__("iret"); + +} + + +//set a handler for a specific interrupt +void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr) +{ + + uint64_t base=(uint64_t)&(*addr); // TODO! + + idt[irq].addrLo = base & 0xffff; + idt[irq].addrHi = (base >> 16) & 0xffff; + idt[irq].addrHi = 0x0; + idt[irq].zeros=0; + idt[irq].flags=flags; + idt[irq].sel=sel; + +} + +// set default handler for all interrupts for a start +void int_init(uint16_t sel) +{ + int i; + for(i=0; i "); + print_hex(*(ptr3+offset*4)); //addrLo + print_hex(*(ptr3+1+offset*4)); //sel + print_hex(*(ptr3+2+offset*4)); //zeros & flags + print_hex(*(ptr3+3+offset*4)); //addrHi + print_nextline(); + } +} +*/ + diff --git a/kernel/interrupts.h b/kernel/interrupts.h new file mode 100644 index 0000000..7c3e119 --- /dev/null +++ b/kernel/interrupts.h @@ -0,0 +1,10 @@ +#ifndef INTERRUPTS_H +#define INTERRUPTS_H + +#include "kernel.h" + +#define INT_MAX 255 // size of our interrupts table + +int int_unhandled=0; // counting unhandled interrupts + +#endif diff --git a/kernel/kernel.c b/kernel/kernel.c index bdf1ec1..db9c60e 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,162 +1,21 @@ +#include "kernel.h" // general kernel config & includes +#include "console.h" // this will allow us to write to screen -#include -#include "kernel.h" // general kernel config -#include "console.h" // this will allow us to write to screen +// TODO: cleanup . how can i compile it without the includes!?? -// garbage -int unhandled=0; -char col=100; -char col2=0; - -int cursor=0; -char last_code=0; - -uint8_t kb_in; - -//// interrupt stuff ///// -// -#define INT_MAX 255 -// - -// interrupt descriptor table -static struct int_desc -{ - uint16_t addrLo; - uint16_t sel; - uint8_t zeros; - uint8_t flags; - uint16_t addrHi; - -} idt[INT_MAX]; - -// interrupt descriptor table descriptor -static struct idt_desc -{ - uint16_t size; - uint16_t baseLo; - uint16_t baseHi; - -} idtd; - -void int_def_handler() -{ - __asm__("pusha"); - - unhandled++; - - // todo also the other pic!// TODO - __asm__("mov $0x20, %al"); - __asm__("out %al, $0x20"); - - __asm__("popa"); - __asm__("leave"); - __asm__("iret"); - -} - -void int_kb_handler() -{ - __asm__("pusha"); - - __asm__("in $0x60, %al"); - __asm__("mov %%al, %0"::"m" (kb_in)); - - int0(kb_in); //TODO!! - - __asm__("mov $0x20, %al"); - __asm__("out %al, $0x20"); - - __asm__("popa"); - __asm__("leave"); - __asm__("iret"); - -} - -void int_init(uint16_t sel) -{ - int i; - - for(i=0; i> 16) & 0xffff; - idt[irq].addrHi = 0x0; - idt[irq].zeros=0; - idt[irq].flags=flags; - idt[irq].sel=sel; - -} - - -void int_install() -{ - - idtd.size=8*34; - idtd.baseHi=0x0000; - idtd.baseLo=&idt[0]; - - __asm__("lidt %0"::"m" (idtd)); - -} -/* -void int_show() -{ - uint16_t *ptr3=&idt[0]; - - print_string("idt located: "); - print_hex(ptr3); - print_nextline(); - - - // first two bytes as chars - int offset; - for(offset=32;offset<40;offset++) - { - print_hex(offset-32); //irq - print_string(" -> "); - print_hex(*(ptr3+offset*4)); //addrLo - print_hex(*(ptr3+1+offset*4)); //sel - print_hex(*(ptr3+2+offset*4)); //zeros & flags - print_hex(*(ptr3+3+offset*4)); //addrHi - print_nextline(); - } -} -*/ - -void int_disable() -{ - __asm__("cli"); -} - -void int_enable() -{ - __asm__("sti"); -} +void int_kb_handler(); ////////// KERNEL MAIN///// ///// // void kernel_main() { - + // clear console scr_clear(); // hello message - scr_put_string(KERNEL_HELLO_MESSAGE); - scr_nextline(); - scr_nextline(); + scr_put_string_nl(KERNEL_HELLO_MESSAGE); // init interrupt decriptor table // install and enable! @@ -164,111 +23,17 @@ void kernel_main() int_install(); int_enable(); - scr_put_string("Interrupts Up and Running"); - scr_nextline(); + // install keyboard handler + int_install_ir(33, 0b10001110, 0x08,&int_kb_handler); + + scr_put_string_nl("Interrupts are up and running"); // kernel main loop while(1) { -// print_nextline(); - -// print_str_col(0,0,"ABC",col); - print_str_col(1,SCR_HEIGHT,"(*)",col2++); - - //int0(); } - - } - -/// keyboard driver //// - -void int0(uint8_t in) -{ - int i=0; - // - //print_hex(int_count); - - uint8_t make_codes[]={ - 0x1e, // A - 0x30, // B - 0x2e, // C - 0x20, // D - 0x12, // E - 0x21, // F - 0x22, // G - 0x23, // H - 0x17, // I - 0x24, // J - 0x25, // K - 0x26, // L - 0x32, // M - 0x31, // N - 0x18, // O - 0x19, // P - 0x10, // Q - 0x13, // R - 0x1F, // S - 0x14, // T - 0x16, // U - 0x2F, // V - 0x11, // W - 0x2D, // X - 0x15, // Y - 0x2c, // Z - }; - - uint8_t break_codes[]={ - 0x9e, // A - 0xb0, // B - 0xae, // C - 0xa0, // D - 0x92, // E - 0xa1, // F - 0xa2, // G - 0xa3, // H - 0x97, // I - 0xa4, // J - 0xa5, // K - 0xa6, // L - 0xb2, // M - 0xb1, // N - 0x98, // O - 0x99, // P - 0x90, // Q - 0x93, // R - 0x9F, // S - 0x94, // T - 0x96, // U - 0xaF, // V - 0x91, // W - 0xaD, // X - 0x95, // Y - 0xac, // Z - }; - - //if(last_code==*int_count)return; - - for(i=0;i<26;i++) - { - if(make_codes[i]==in) - { - print_char_col(cursor,20,'A'+i,0xf); - } - } - - for(i=0;i<26;i++) - { - if(break_codes[i]==in) - { - print_char_col(cursor++,20,'a'+i,SCR_RED); - } - } - - last_code=in; - -} diff --git a/kernel/kernel.h b/kernel/kernel.h index 8df13ad..0dc7306 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -1 +1,8 @@ +#ifndef KERNEL_H +#define KERNEL_H + +#include //needed for uint16_t + #define KERNEL_HELLO_MESSAGE "FoolOs 0.0.1" + +#endif diff --git a/kernel/keyboard.c b/kernel/keyboard.c new file mode 100644 index 0000000..d1175c1 --- /dev/null +++ b/kernel/keyboard.c @@ -0,0 +1,118 @@ + +#include "kernel.h" +#include "console.h" + +/// keyboard driver //// + +static uint8_t kb_in; +static int cursor=0; +static char last_code=0; + +void int0(uint8_t in) +{ + int i=0; + // + //print_hex(int_count); + + uint8_t make_codes[]={ + 0x1e, // A + 0x30, // B + 0x2e, // C + 0x20, // D + 0x12, // E + 0x21, // F + 0x22, // G + 0x23, // H + 0x17, // I + 0x24, // J + 0x25, // K + 0x26, // L + 0x32, // M + 0x31, // N + 0x18, // O + 0x19, // P + 0x10, // Q + 0x13, // R + 0x1F, // S + 0x14, // T + 0x16, // U + 0x2F, // V + 0x11, // W + 0x2D, // X + 0x15, // Y + 0x2c, // Z + }; + + uint8_t break_codes[]={ + 0x9e, // A + 0xb0, // B + 0xae, // C + 0xa0, // D + 0x92, // E + 0xa1, // F + 0xa2, // G + 0xa3, // H + 0x97, // I + 0xa4, // J + 0xa5, // K + 0xa6, // L + 0xb2, // M + 0xb1, // N + 0x98, // O + 0x99, // P + 0x90, // Q + 0x93, // R + 0x9F, // S + 0x94, // T + 0x96, // U + 0xaF, // V + 0x91, // W + 0xaD, // X + 0x95, // Y + 0xac, // Z + }; + + //if(last_code==*int_count)return; + + for(i=0;i<26;i++) + { + if(make_codes[i]==in) + { + print_char_col(cursor,20,'A'+i,0xf); + } + } + + for(i=0;i<26;i++) + { + if(break_codes[i]==in) + { + print_char_col(cursor++,20,'a'+i,SCR_RED); + } + } + + last_code=in; + +} + +void int_kb_handler() +{ + __asm__("pusha"); + + __asm__("in $0x60, %al"); + __asm__("mov %%al, %0"::"m" (kb_in)); + + // scr_nextline(); + //scr_put_string("irq 1 -> kb scancodes : "); + //scr_put_hex(kb_in); + + int0(kb_in); //TODO!! + + __asm__("mov $0x20, %al"); + __asm__("out %al, $0x20"); + + __asm__("popa"); + __asm__("leave"); + __asm__("iret"); + +} + -- cgit v1.2.3