From 264e6ebaa0816d0d2070090ebd7a75d7767929cb Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Tue, 8 Jul 2014 11:34:16 +0200 Subject: Merge in parts of the experimental branch --- Makefile | 29 ++++++++--- kernel/console.c | 68 +++++++++++++++++++++++++ kernel/console.h | 27 ++++++++++ kernel/kernel.c | 151 +++++++++++++++++++++++++++++++++---------------------- 4 files changed, 206 insertions(+), 69 deletions(-) create mode 100644 kernel/console.c create mode 100644 kernel/console.h diff --git a/Makefile b/Makefile index 54aa45f..872c4e3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,12 @@ -# FoolOS Makefile -# -IMAGE_SIZE=4096 + ##################### + # # + # FoolOS Makefile # + # # + ##################### + + +IMAGE_SIZE=1474560 all: FoolOS.img @@ -17,12 +22,20 @@ mbr.bin: boot/mbr.asm kernel_entry.o: boot/kernel_entry.asm nasm -f elf $^ -o $@ -kernel.o: kernel/kernel.c - gcc -ffreestanding -m32 -o $@ -c $^ -fno-asynchronous-unwind-tables +kernel.o: kernel/kernel.c kernel/console.h + gcc -ffreestanding -m32 -o $@ -c $< -fno-asynchronous-unwind-tables -O0 + +console.o: kernel/console.c kernel/console.h + gcc -ffreestanding -m32 -o $@ -c $< -fno-asynchronous-unwind-tables -O0 + +kernel.bin: kernel_entry.o kernel.o console.o + ld -o $@ -Ttext 0x1000 --oformat binary -melf_i386 $^ -O0 + +dump: + vboxmanage debugvm FoolOs dumpguestcore --filename dump.elf + xxd dump.elf > dump.xxd -kernel.bin: kernel_entry.o kernel.o - ld -o $@ -Ttext 0x1000 --oformat binary -melf_i386 $^ clean: - rm *.bin *.o *.img + -rm *.bin *.o *.img dump.elf dump.xxd .PHONY: all clean diff --git a/kernel/console.c b/kernel/console.c new file mode 100644 index 0000000..9381eed --- /dev/null +++ b/kernel/console.c @@ -0,0 +1,68 @@ +#include + +#include "console.h" + +static int posx=0; +static int posy=0; + +void print_nextline() +{ + posx=0; + posy++; + if(posy>SCR_HEIGHT-2)posy=0; +} + +void print_hex(uint16_t val) +{ + int i; + + print_char_col(posx,posy,'0',SCR_WHITE); + posx++; + print_char_col(posx,posy,'x',SCR_WHITE); + posx++; + + for(i=0;i<4;i++) + { + print_single_hex((val&0xf000)>>12); + val=val << 4; + + + } + +} + +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(int x,int y,char *str) +{ + print_str_col(x,y,str,SCR_WHITE); +} + +void print_char(int x, int y, char c) +{ + print_char_col(x,y,c,SCR_WHITE); +} + +void print_str_col(int x,int y,char *str, char col) +{ + + while(*str!=0) + { + print_char_col(x++,y,*(str++),col); + } + +} + +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; +} diff --git a/kernel/console.h b/kernel/console.h new file mode 100644 index 0000000..e304773 --- /dev/null +++ b/kernel/console.h @@ -0,0 +1,27 @@ +#define SCR_VIDEOMEM 0xb8000 + +#define SCR_WIDTH 80 +#define SCR_HEIGHT 23 + +#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 + +//autoscroll +void print_nextline(); +void print_hex(uint16_t val); + +//no autoscroll +void print_str(int x,int y,char *str); +void print_str_col(int x,int y,char *str, char col); +void print_char(int x, int y, char c); +void print_char_col(int x, int y, char c, char col); diff --git a/kernel/kernel.c b/kernel/kernel.c index 05d04cc..c8b21cf 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,95 +1,125 @@ -#define SCR_WIDTH 80 -#define SCR_HEIGHT 23 +#include -#define SCR_CTRL 0x3D4 -#define SCR_DATA 0x3D5 +#include "console.h" // this will allow us to write to screen -#define SCR_BLACK 0x0 -#define SCR_BLUE 0x1 -#define SCR_GREEN 0x2 -#define SCR_CYAN 0x3 -#define SCR_RED 0x4 -// todo... -# define SCR_WHITE 0xf +//// 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]; -void print_char(int x, int y, char c, char col) +// interrupt descriptor table descriptor +static struct idt_desc { - char* video_mem=(char *)0xb8000+(x+y*SCR_WIDTH)*2; - video_mem[0]=c; - video_mem[1]=col; + uint16_t size; + uint32_t base; + +} idtd; + +volatile char col=100; +char col2=0; + +int cursor=0; +char last_code=0; + + +volatile void int_def_handler() +{ + __asm__("pusha"); + + col++; + print_str(5,5,"Interrupted"); + + __asm__("popa"); + __asm__("iret"); + } -float func(float x,float y) +void int_init(uint16_t sel) { - return x*x*y; + int i; + + idtd.size=sizeof(struct int_desc)*INT_MAX-1; // why the -1 ?? + idtd.base=(uint32_t)&idt[0]; + + for(i=0; i> 16) & 0xffff; + idt[irq].zeros=0; + idt[irq].flags=flags; + idt[irq].sel=sel; + } -void sleep(int i) + +void int_install() { - int x; + __asm__("lidt %0"::"m" (idtd)); - for(x=0;x300)time=0; - - } + print_str_col(0,0,str,col); + print_str_col(0,2,str,col2++); -} + int0(); + } -int cursor=0; -char last_code=0; + +} void int0() { int i=0; + char* int_count=(char *)0x7c00+3; char make_codes[]={ 0x1e, // A @@ -149,15 +179,14 @@ void int0() 0xac, // Z }; - char* int_count=(char *)0x7c00+3; - + print_str(6,6,*int_count); if(last_code==*int_count)return; for(i=0;i<26;i++) { if(make_codes[i]==*int_count) { - print_char(cursor,10,'A'+i,0xf); + print_char_col(cursor,10,'A'+i,0xf); } } @@ -165,7 +194,7 @@ void int0() { if(break_codes[i]==*int_count) { - print_char(cursor++,10,'a'+i,0xf); + print_char_col(cursor++,10,'a'+i,SCR_RED); } } -- cgit v1.2.3