From a6184be79e3918764d5e683796afbd8e8ccba018 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Mon, 18 May 2015 09:20:01 +0200 Subject: fifo through ringbuffer(stdin) and vt52(stdout) finally working! --- Makefile | 2 +- fs/ringbuffer.c | 71 ------------------------------------------ fs/ringbuffer.h | 27 ---------------- kernel/config.h | 2 +- kernel/fifo.c | 2 +- kernel/fifo.h | 6 ++-- kernel/kernel.c | 29 +++++++++++------- kernel/ringbuffer.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ kernel/ringbuffer.h | 27 ++++++++++++++++ kernel/syscalls.c | 9 +++--- terminal/vt52.c | 13 ++++---- userspace/foolshell.c | 8 ----- 12 files changed, 147 insertions(+), 134 deletions(-) delete mode 100644 fs/ringbuffer.c delete mode 100644 fs/ringbuffer.h create mode 100644 kernel/ringbuffer.c create mode 100644 kernel/ringbuffer.h diff --git a/Makefile b/Makefile index aee113d..f766fd7 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ CFLAGS+=-ffreestanding #CFLATS+=-Wall #CFLAGS+=-Wextra #CFLAGS+=-O3 -#CFLAGS+=-O0 +CFLAGS+=-O0 #CFLAGS+=-nostdlib CFLAGS+=-std=gnu11 CFLAGS+=-I. diff --git a/fs/ringbuffer.c b/fs/ringbuffer.c deleted file mode 100644 index 449ad88..0000000 --- a/fs/ringbuffer.c +++ /dev/null @@ -1,71 +0,0 @@ -#define FOOLOS_MODULE_NAME "ringbuffer" -#include "lib/logger/log.h" - -#include "ringbuffer.h" - -static int sl=9; - -ringbuffer ringbuffer_init(uint32_t size) -{ - ringbuffer f; - f.data=kballoc(size); - f.size=size*4096; - f.front=f.size-1; - f.back=f.size-1; - return f; -} - -bool ringbuffer_put(ringbuffer* f,uint8_t c) -{ - x86_int_disable(); - lock_spin(sl); - - if((f->back-1+f->size)%f->size==f->front) - { - lock_release(sl); - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"unlocked by put"); - x86_int_enable(); - return false; - } - - f->data[f->back]=c; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"put %d %d (%c)", f->back, f->front,c); - f->back--; - f->back+=f->size; - f->back%=f->size; - - lock_release(sl); - x86_int_enable(); - - return true; -} - -uint8_t ringbuffer_get(ringbuffer* f) // blocking -{ - - char c; - - x86_int_disable(); - lock_spin(sl); - - if(f->front==f->back) - { - lock_release(sl); - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"unlocked by get"); - x86_int_enable(); - c='_'; - return false; - } - - c=f->data[f->front]; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_FINE,"get %d %d (%c)", f->back, f->front,c); - - f->front--; - f->front+=f->size; - f->front%=f->size; - - lock_release(sl); - x86_int_enable(); - - return true; -} diff --git a/fs/ringbuffer.h b/fs/ringbuffer.h deleted file mode 100644 index 31d00cf..0000000 --- a/fs/ringbuffer.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef RINGBUFFER_H -#define RINGBUFFER_H - -#include -#include - -// Simple FIRST IN FIRST OUT -// requires kballoc - block allocation -typedef struct ringbuffer_struct -{ - uint32_t size; - uint32_t front; - uint32_t back; - - uint8_t *data; - -}ringbuffer; - -// create new fifo of given size (in blocks) -ringbuffer ringbuffer_init(uint32_t blocks); - -// true on success -bool ringbuffer_put(ringbuffer*,uint8_t); -uint8_t ringbuffer_get(ringbuffer*); // blocking -bool ringbuffer_has(ringbuffer*); // check if somehting waiting? - -#endif diff --git a/kernel/config.h b/kernel/config.h index c4f9af1..0a6fd3c 100644 --- a/kernel/config.h +++ b/kernel/config.h @@ -8,7 +8,7 @@ #define FOOLOS_CONFIG_H #define FOOLOS_CONSOLE_AUTOBREAK // add newline automatically at end of line -//#define FOOLOS_LOG_OFF // do not log anything +#define FOOLOS_LOG_OFF // do not log anything #define FOOLOS_CONSOLE // otherwise VESA will be used! #define FOOLSOS_SHOW_VESAMODES #define MEM_PRINT_MEMORYMAP diff --git a/kernel/fifo.c b/kernel/fifo.c index abd6c46..314e9ff 100644 --- a/kernel/fifo.c +++ b/kernel/fifo.c @@ -5,7 +5,7 @@ bool fifo_put(fifo* f,uint8_t c) return f->put(f->data,c); } -uint8_t fifo_get(fifo* f) +volatile uint8_t fifo_get(fifo* f) { return f->get(f->data); } diff --git a/kernel/fifo.h b/kernel/fifo.h index bae7d57..e0a81e5 100644 --- a/kernel/fifo.h +++ b/kernel/fifo.h @@ -17,9 +17,9 @@ typedef struct fifo_struct }fifo; -bool fifo_put(fifo*,uint8_t); -uint8_t fifo_get(fifo*); -bool fifo_has(fifo*); +volatile bool fifo_put(fifo*,uint8_t); +volatile uint8_t fifo_get(fifo*); +volatile bool fifo_has(fifo*); #endif diff --git a/kernel/kernel.c b/kernel/kernel.c index dd3f502..5350109 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -18,6 +18,7 @@ #include "mp.h" #include "interrupts.h" #include "multiboot.h" +#include "ringbuffer.h" #include @@ -41,12 +42,16 @@ fool_os *get_fool() } // // -// // stdio init : TODO: move away! static vt52_tty tty1; +static term_out screen; +static term_in input; + +static ringbuffer stdin_buf; + static void put_kb(uint8_t c) { @@ -55,30 +60,32 @@ static void put_kb(uint8_t c) static void stdin_put_char(uint8_t c) { - vt52_kb(&tty1,c); + ringbuffer_put(&stdin_buf,c); } - static void init_stdio() { -// get_fool().std_in - // // Setup terminal output / input // - term_out screen; + screen.put_char=console_put_char; screen.update_cursor=update_cursor; - term_in input; input.put_char=stdin_put_char; tty1=vt52_init(&screen,&input); + get_fool()->std_out.data=&tty1; get_fool()->std_out.put=vt52_put; - keyboard_init(put_kb); + stdin_buf=ringbuffer_init(1); + get_fool()->std_in.data=&stdin_buf; + get_fool()->std_in.put=ringbuffer_put; + get_fool()->std_in.get=ringbuffer_get; + get_fool()->std_in.has=ringbuffer_has; + keyboard_init(put_kb); } @@ -147,8 +154,6 @@ void kernel_main(uint32_t eax,uint32_t ebx) smp_log_procdata(&procdata); smp_start_aps(&procdata,"/boot/mp.bin"); //will be copied over mbr - - // // Activate Virtual Memory (paging) // @@ -161,8 +166,10 @@ void kernel_main(uint32_t eax,uint32_t ebx) // Its driver will be hopefully implemented one day ;) TODO // //pci_init(); - + //empty stdin! + while(fifo_has(&get_fool()->std_in)) + fifo_get(&get_fool()->std_in); // // Initialize Multitasking diff --git a/kernel/ringbuffer.c b/kernel/ringbuffer.c new file mode 100644 index 0000000..a749b16 --- /dev/null +++ b/kernel/ringbuffer.c @@ -0,0 +1,85 @@ +#define FOOLOS_MODULE_NAME "ringbuffer" +#include "lib/logger/log.h" + +// TODO: why do we disable interrupts? (Eg. kb input) + +#include "ringbuffer.h" + +static int sl=9; + +ringbuffer ringbuffer_init(uint32_t size) +{ + ringbuffer f; + f.data=kballoc(size); + f.size=size*4096; + f.front=f.size-1; + f.back=f.size-1; + f.front=f.size-1; + f.back=f.size-1; + return f; +} + +volatile bool ringbuffer_put(ringbuffer* f,uint8_t c) +{ + x86_int_disable(); + lock_spin(sl); + + if((f->back-1+f->size)%f->size==f->front) + { + lock_release(sl); + x86_int_enable(); + return false; + } + + f->data[f->back]=c; + f->back--; + f->back+=f->size; + f->back%=f->size; + + lock_release(sl); + x86_int_enable(); + + return true; +} + +volatile bool ringbuffer_has(ringbuffer* f) +{ + bool res=true; + + x86_int_disable(); + lock_spin(sl); + + if(f->front==f->back) + res=false; + + lock_release(sl); + x86_int_enable(); + + return res; +} + +volatile uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check first +{ + char c; + + x86_int_disable(); + lock_spin(sl); + + if(f->front==f->back) + { + lock_release(sl); + x86_int_enable(); + return ' '; + } + + c=f->data[f->front]; + + f->front--; + f->front+=f->size; + f->front%=f->size; + + lock_release(sl); + x86_int_enable(); + + return c; +} diff --git a/kernel/ringbuffer.h b/kernel/ringbuffer.h new file mode 100644 index 0000000..c79fdcf --- /dev/null +++ b/kernel/ringbuffer.h @@ -0,0 +1,27 @@ +#ifndef RINGBUFFER_H +#define RINGBUFFER_H + +#include +#include + +// Simple FIRST IN FIRST OUT +// requires kballoc - block allocation +typedef volatile struct ringbuffer_struct +{ + uint32_t size; + uint32_t front; + uint32_t back; + + uint8_t *data; + +}ringbuffer; + +// create new fifo of given size (in blocks) +ringbuffer ringbuffer_init(uint32_t blocks); + +// true on success +bool ringbuffer_put(ringbuffer*,uint8_t); +uint8_t ringbuffer_get(ringbuffer*); // non-blocking please check first +bool ringbuffer_has(ringbuffer*); // check if somehting waiting? + +#endif diff --git a/kernel/syscalls.c b/kernel/syscalls.c index cd54d7b..7ad977e 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -43,14 +43,13 @@ int syscall_write(int file, char *buf, int len) //stderr and stdout go to console for(int i=0;ififo_stdout,buf[i]); + fifo_put(&get_fool()->std_out,buf[i]); } lock_release(2); //x86_int_enable(); return len; } -// TODO: /dev/kb int syscall_read(int file, char *buf, int len) { static bool eof=false; @@ -59,7 +58,6 @@ int syscall_read(int file, char *buf, int len) log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"read(file=%d, buf=0x%08X, len=%d)", file,buf,len); #endif - // stdin TODO: other descroptiors! if(file!=0) panic(FOOLOS_MODULE_NAME,"unhandled syscall: read (only stdin)"); { @@ -74,8 +72,9 @@ int syscall_read(int file, char *buf, int len) while(1) { -// bool ret=ringbuffer_get(&c); - bool ret=false; + while(!fifo_has(&get_fool()->std_in)); + c=fifo_get(&get_fool()->std_in); + bool ret=true; if(ret) { diff --git a/terminal/vt52.c b/terminal/vt52.c index 66fd5f1..9197347 100644 --- a/terminal/vt52.c +++ b/terminal/vt52.c @@ -6,8 +6,8 @@ #include "vt52.h" #include "kernel/kmalloc.h" -#define VT52_WIDTH 80 -#define VT52_HEIGHT 25 +#define VT52_WIDTH 75 +#define VT52_HEIGHT 24 #define VT52_ESC_1 0x1b //ESC @@ -130,7 +130,7 @@ bool vt52_put(vt52_tty *tty, uint8_t c) case VT52_ERASE_SCR: for(uint32_t y=tty->y+1;yheight;y++) { - for(uint32_t x=0;xwidth-1;x++) + for(uint32_t x=0;xwidth;x++) { uint32_t c=tty->data[index(tty,x,y+1)]; tty->data[index(tty,x,y)] = c; @@ -139,7 +139,7 @@ bool vt52_put(vt52_tty *tty, uint8_t c) } case VT52_ERASE_LINE: - for(uint32_t x=tty->x;xwidth-1;x++) + for(uint32_t x=tty->x;xwidth;x++) { uint32_t c=tty->data[index(tty,x,tty->y)]; tty->data[index(tty,x,tty->y)] = c; @@ -165,7 +165,7 @@ bool vt52_put(vt52_tty *tty, uint8_t c) else { tty->y++; - tty->x=0; + tty->x=-1; } tty->x++; @@ -182,9 +182,10 @@ bool vt52_put(vt52_tty *tty, uint8_t c) tty->y--; for(uint32_t y=0;yheight;y++) { - for(uint32_t x=0;xwidth-1;x++) + for(uint32_t x=0;xwidth;x++) { uint32_t c=tty->data[index(tty,x,y+1)]; + if(y==tty->height-1)c=' '; tty->data[index(tty,x,y)] = c; tty->screen->put_char(c,0xf,x,y); } diff --git a/userspace/foolshell.c b/userspace/foolshell.c index 84d24b0..8496b05 100644 --- a/userspace/foolshell.c +++ b/userspace/foolshell.c @@ -45,14 +45,6 @@ int main(int argc, char **argv) if(!silent)hello(); char *buf=malloc(256); - // printf("malloc returned: 0x%08X\n",buf); - // - while(1) - { - fgets(buf,2,stdin); - buf[1]=0; - puts(buf); - } while(1) -- cgit v1.2.3