From db22b587966b4a4eaa47536f32ca812532446bcb Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Mon, 18 May 2015 00:48:07 +0200 Subject: heavy refactoring underway --- .gdbinit | 5 +- Makefile | 1 + driver/console.c | 33 ++++--- driver/keyboard.c | 2 +- fs/ext2.c | 6 +- fs/file.h | 7 +- fs/ringbuffer.c | 71 ++++++++++++++ fs/ringbuffer.h | 27 ++++++ kernel/acpi.c | 10 +- kernel/kernel.c | 35 +++++-- kernel/kernel.h | 5 +- kernel/kmalloc.c | 4 +- kernel/mp.c | 7 +- kernel/syscalls.c | 6 +- kernel/task.c | 1 - lib/logger/log.c | 79 +++++++++++++++ lib/logger/log.h | 13 +++ lib/printf/printf.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/printf/printf.h | 125 ++++++++++++++++++++++++ lib/string/string.c | 43 +++++++++ lib/string/string.h | 8 ++ xxx/lib/printf/printf.c | 250 ------------------------------------------------ xxx/lib/printf/printf.h | 125 ------------------------ xxx/lib/string/string.c | 37 ------- xxx/lib/string/string.h | 3 - 25 files changed, 688 insertions(+), 465 deletions(-) create mode 100644 fs/ringbuffer.c create mode 100644 fs/ringbuffer.h create mode 100644 lib/logger/log.c create mode 100644 lib/logger/log.h create mode 100644 lib/printf/printf.c create mode 100644 lib/printf/printf.h create mode 100644 lib/string/string.c create mode 100644 lib/string/string.h delete mode 100644 xxx/lib/printf/printf.c delete mode 100644 xxx/lib/printf/printf.h delete mode 100644 xxx/lib/string/string.c delete mode 100644 xxx/lib/string/string.h diff --git a/.gdbinit b/.gdbinit index be3cb95..4a45ea3 100644 --- a/.gdbinit +++ b/.gdbinit @@ -1,4 +1,3 @@ file foolos.img -target remote localhost:1234 -set architecture i386 -break kernel_main +target remote :1234 + diff --git a/Makefile b/Makefile index 92b60e2..aee113d 100644 --- a/Makefile +++ b/Makefile @@ -69,6 +69,7 @@ SOURCES+=$(wildcard ./kernel/*.c) SOURCES+=$(wildcard ./fs/*.c) SOURCES+=$(wildcard ./driver/*.c) SOURCES+=$(wildcard ./terminal/*.c) +SOURCES+=$(wildcard ./lib/*/*.c) #derive kernel object files OBJECTS=$(patsubst %.c, %.o, $(SOURCES)) diff --git a/driver/console.c b/driver/console.c index b6958c5..fcbf6e1 100644 --- a/driver/console.c +++ b/driver/console.c @@ -36,7 +36,6 @@ static void print_char_col(int x, int y, char c, char col) // glue func for vt52 terminal void console_put_char(uint8_t c,uint8_t color, uint32_t x, uint32_t y) { - print_char_col(x,y,c, color); } // @@ -132,22 +131,6 @@ static void scr_nextline() #endif } -static void scr_put_char(char ch,char col) -{ - - if(ch=='\n')scr_nextline(); - else if(posx=SCR_WIDTH)scr_nextline(); -#endif - -} - /* void scr_put_hex(uint16_t val) { @@ -178,6 +161,22 @@ void scr_put_hex32(uint32_t val) } */ +static void scr_put_char(char ch,char col) +{ + + if(ch=='\n')scr_nextline(); + else if(posx=SCR_WIDTH)scr_nextline(); +#endif + +} + static void scr_put_string(char *str, char col) { while(*str!=0) diff --git a/driver/keyboard.c b/driver/keyboard.c index 4366127..2323b04 100644 --- a/driver/keyboard.c +++ b/driver/keyboard.c @@ -2,7 +2,7 @@ #define FOOLOS_MODULE_NAME "keyboard" -#include "x86.h" +#include "kernel/x86.h" /// idiots keyboard driver //// // http://www.computer-engineering.org/ps2keyboard/scancodes1.html diff --git a/fs/ext2.c b/fs/ext2.c index 858b7d6..4f00000 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -6,7 +6,6 @@ #include #include -#include "lib/string/string.h" #include "lib/logger/log.h" #include "fs.h" #include "ext2.h" @@ -272,7 +271,7 @@ int ext2_filename_to_inode_traverse(uint8_t *ram, char *path,int inode_start) for(int i=0;iback-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 fifo_get(fifo* 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 new file mode 100644 index 0000000..31d00cf --- /dev/null +++ b/fs/ringbuffer.h @@ -0,0 +1,27 @@ +#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/acpi.c b/kernel/acpi.c index 3aec83b..d7a1749 100644 --- a/kernel/acpi.c +++ b/kernel/acpi.c @@ -3,10 +3,10 @@ #define FOOLOS_MODULE_NAME "acpi" -#include "lib/logger/log.h" #include -#include "lib/string/string.h" +#include #include "smp.h" +#include "lib/logger/log.h" typedef struct acpi_rsdt_struct @@ -91,7 +91,7 @@ void acpi_check_madt(uint32_t *madt,smp_processors *procdata) acpi_madt *table=(acpi_madt *)*madt; log(FOOLOS_MODULE_NAME,FOOLOS_LOG_DEBUG,"Looking for MADT Table at %08X.",table); - if(strcmp("APIC",table->sig,4)) + if(!strcmp_l("APIC",table->sig,4)) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Found MADT Table at 0x%08X",table); uint8_t *end=(uint8_t *)table; @@ -115,7 +115,7 @@ void acpi_read_rsdt(acpi_rsdt *rsdt,smp_processors *procdata) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Reading RSDT Table at 0x%08X",rsdt); - if(!strcmp("RSDT",rsdt->sig,4)) + if(strcmp_l("RSDT",rsdt->sig,4)) panic(FOOLOS_MODULE_NAME,"Signature MISMATCH!"); int entries=(rsdt->length-sizeof(acpi_rsdt))/4; @@ -146,7 +146,7 @@ bool acpi_find(smp_processors *procdata) while(search<=(char *)0xfffff) { - if(strcmp("RSD PTR ",search,8)) // notice trailing space in "RSD PTR " + if(!strcmp_l("RSD PTR ",search,8)) // notice trailing space in "RSD PTR " { uint8_t checksum=0; for(int i=0;i<20;i++) diff --git a/kernel/kernel.c b/kernel/kernel.c index 098fc64..0fb337f 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -22,12 +22,13 @@ #include // for built-in shell -#include "lib/buffer/ringbuffer.h" #include "task.h" -#include "video/console.h" #include "multiboot.h" #include "terminal/vt52.h" +#include "driver/console.h" + +#include "fs/fifo.h" // // The Foolish structure of Fool OS! @@ -38,15 +39,30 @@ fool_os *get_fool() { return &foolos; } +// +// +// + +// stdio init : TODO: move away! +static vt52_tty tty1; + static void put_kb(uint8_t c) { - vt52_kb(get_fool()->tty1,c); + vt52_kb(&tty1,c); } -void kernel_main(uint32_t eax,uint32_t ebx) +static void stdin_put_char(uint8_t c) { + vt52_kb(&tty1,c); +} + + +static void init_stdio() +{ + fifo in=fifo_init(1); + fifo out=fifo_init(1); // // Setup terminal output / input @@ -56,14 +72,21 @@ void kernel_main(uint32_t eax,uint32_t ebx) screen.update_cursor=update_cursor; term_in input; + input.put_char=stdin_put_char; - vt52_tty tty=vt52_init(&screen,&input); - get_fool()->tty1=&tty; + tty1=vt52_init(&screen,&input); keyboard_init(put_kb); +} + +void kernel_main(uint32_t eax,uint32_t ebx) +{ + + init_stdio(); + // // PR // diff --git a/kernel/kernel.h b/kernel/kernel.h index b6eccb8..fb4e44d 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -3,11 +3,12 @@ #define KERNEL_VERSION "FoolOs 0.2.1" -#include "terminal/vt52.h"; +#include "fs/fifo.h"; typedef struct fool_os_struct { - vt52_tty *tty1; + fifo fifo_stdin; + fifo fifo_stdout; }fool_os; diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c index a3b4ab6..d235f55 100644 --- a/kernel/kmalloc.c +++ b/kernel/kmalloc.c @@ -22,7 +22,7 @@ void kmallocinit() next*=4096; } - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"kmalloc_init: 0x%08X",next); +// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"kmalloc_init: 0x%08X",next); init=1; } @@ -38,7 +38,7 @@ uint32_t kballoc(uint32_t size) { panic(FOOLOS_MODULE_NAME,"kballoc ran out of memory! maybe increase MEM_SIZE in kmalloc.c?"); } - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"(%d) : 0x%08X (~%dKB left)",size,old,(MEM_SIZE-next+first)/1024); + // log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"(%d) : 0x%08X (~%dKB left)",size,old,(MEM_SIZE-next+first)/1024); return old; } diff --git a/kernel/mp.c b/kernel/mp.c index 8312821..abea5cb 100644 --- a/kernel/mp.c +++ b/kernel/mp.c @@ -1,8 +1,9 @@ #define FOOLOS_MODULE_NAME "mp" +#include + #include "x86.h" #include "lib/logger/log.h" // logger facilities -#include "lib/string/string.h" #include "smp.h" @@ -153,7 +154,7 @@ bool mp_find(smp_processors *procdata) uint8_t *addr=0x8000; while(addr<=0xfffff) { - if(strcmp("_MP_",addr,4)) + if(!strcmp_l("_MP_",addr,4)) { // log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Found at 0x%04X",addr); if(do_mp_fps(addr,procdata))return true; @@ -164,7 +165,7 @@ bool mp_find(smp_processors *procdata) addr=0x190000-1025; while(addr<=0x190000+1024) { - if(strcmp("_MP_",addr,4)) + if(!strcmp_l("_MP_",addr,4)) { // log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Found at 0x%04X",addr); if(do_mp_fps(addr,procdata))return true; diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 1d63ba4..9900567 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -1,6 +1,5 @@ #define FOOLOS_MODULE_NAME "syscalls" -#include "lib/buffer/ringbuffer.h" #include "lib/logger/log.h" #include "fs/fs.h" #include "fs/ext2.h" @@ -44,7 +43,7 @@ int syscall_write(int file, char *buf, int len) //stderr and stdout go to console for(int i=0;itty1,buf[i]); + fifo_put(&get_fool()->fifo_stdout,buf[i]); } lock_release(2); //x86_int_enable(); @@ -75,7 +74,8 @@ int syscall_read(int file, char *buf, int len) while(1) { - bool ret=ringbuffer_get(&c); +// bool ret=ringbuffer_get(&c); + bool ret=false; if(ret) { diff --git a/kernel/task.c b/kernel/task.c index 36ccafe..8bb6c13 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -3,7 +3,6 @@ // #include "kernel.h" #include "lib/logger/log.h" // logger facilities -#include "lib/buffer/ringbuffer.h" #include "mem.h" #include "timer.h" #include "x86.h" diff --git a/lib/logger/log.c b/lib/logger/log.c new file mode 100644 index 0000000..9348f01 --- /dev/null +++ b/lib/logger/log.c @@ -0,0 +1,79 @@ +#define FOOLOS_MODULE_NAME "log" + +#include +#include + +#include "log.h" + +#include "kernel/kernel.h" +#include "kernel/config.h" +#include "terminal/vt52.h" +#include "kernel/timer.h" + + +static char buffer[LOG_BUF_SIZE]; +static int first=0; +static int last=0; +static bool init=true;// + + +static void log_string(char *str) +{ + while(*str!=0) + { + fifo_put(&get_fool()->fifo_stdout,*(str++)); + } +} + +void log(char *module_name, int log_level, char *format_string, ...) +{ + + #ifdef FOOLOS_LOG_OFF + return; + #endif + + if(log_level=LOG_BUF_SIZE)first=(first+1)%LOG_BUF_SIZE; + if(last>first)if(LOG_BUF_SIZE-last+first>=LOG_BUF_SIZE)first=(first+1)%LOG_BUF_SIZE; + } + + +} + +void panic(char *module_name, char *message) +{ + char buf_log[256]; + tfp_sprintf(buf_log,"KERNEL PANIC !! %s: %s\n",module_name,message); + log_string(buf_log); + + while(1) + { + asm volatile("cli"); + asm volatile("hlt"); + } + +} diff --git a/lib/logger/log.h b/lib/logger/log.h new file mode 100644 index 0000000..38f1219 --- /dev/null +++ b/lib/logger/log.h @@ -0,0 +1,13 @@ +#ifndef FOOLOS_LOG_H +#define FOOLOS_LOG_H + +#define FOOLOS_LOG_ERROR 5 +#define FOOLOS_LOG_WARNING 4 +#define FOOLOS_LOG_INFO 3 +#define FOOLOS_LOG_DEBUG 2 +#define FOOLOS_LOG_FINE 1 + +void log(char *module_name, int prio, char *format_string, ...); +void panic(char *module_name, char *format_string); + +#endif diff --git a/lib/printf/printf.c b/lib/printf/printf.c new file mode 100644 index 0000000..23d3d46 --- /dev/null +++ b/lib/printf/printf.c @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its + * contributors may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ + +#include "printf.h" + +typedef void (*putcf) (void*,char); +static putcf stdout_putf; +static void* stdout_putp; + + +#ifdef PRINTF_LONG_SUPPORT + +static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf) + { + int n=0; + unsigned int d=1; + while (num/d >= base) + d*=base; + while (d!=0) { + int dgt = num / d; + num%=d; + d/=base; + if (n || dgt>0|| d==0) { + *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10); + ++n; + } + } + *bf=0; + } + +static void li2a (long num, char * bf) + { + if (num<0) { + num=-num; + *bf++ = '-'; + } + uli2a(num,10,0,bf); + } + +#endif + +static void ui2a(unsigned int num, unsigned int base, int uc,char * bf) + { + int n=0; + unsigned int d=1; + while (num/d >= base) + d*=base; + while (d!=0) { + int dgt = num / d; + num%= d; + d/=base; + if (n || dgt>0 || d==0) { + *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10); + ++n; + } + } + *bf=0; + } + +static void i2a (int num, char * bf) + { + if (num<0) { + num=-num; + *bf++ = '-'; + } + ui2a(num,10,0,bf); + } + +static int a2d(char ch) + { + if (ch>='0' && ch<='9') + return ch-'0'; + else if (ch>='a' && ch<='f') + return ch-'a'+10; + else if (ch>='A' && ch<='F') + return ch-'A'+10; + else return -1; + } + +static char a2i(char ch, char** src,int base,int* nump) + { + char* p= *src; + int num=0; + int digit; + while ((digit=a2d(ch))>=0) { + if (digit>base) break; + num=num*base+digit; + ch=*p++; + } + *src=p; + *nump=num; + return ch; + } + +static void putchw(void* putp,putcf putf,int n, char z, char* bf) + { + char fc=z? '0' : ' '; + char ch; + char* p=bf; + while (*p++ && n > 0) + n--; + while (n-- > 0) + putf(putp,fc); + while ((ch= *bf++)) + putf(putp,ch); + } + +void tfp_format(void* putp,putcf putf,char *fmt, va_list va) + { + char bf[12]; + + char ch; + + + while ((ch=*(fmt++))) { + if (ch!='%') + putf(putp,ch); + else { + char lz=0; +#ifdef PRINTF_LONG_SUPPORT + char lng=0; +#endif + int w=0; + ch=*(fmt++); + if (ch=='0') { + ch=*(fmt++); + lz=1; + } + if (ch>='0' && ch<='9') { + ch=a2i(ch,&fmt,10,&w); + } +#ifdef PRINTF_LONG_SUPPORT + if (ch=='l') { + ch=*(fmt++); + lng=1; + } +#endif + switch (ch) { + case 0: + goto abort; + case 'u' : { +#ifdef PRINTF_LONG_SUPPORT + if (lng) + uli2a(va_arg(va, unsigned long int),10,0,bf); + else +#endif + ui2a(va_arg(va, unsigned int),10,0,bf); + putchw(putp,putf,w,lz,bf); + break; + } + case 'd' : { +#ifdef PRINTF_LONG_SUPPORT + if (lng) + li2a(va_arg(va, unsigned long int),bf); + else +#endif + i2a(va_arg(va, int),bf); + putchw(putp,putf,w,lz,bf); + break; + } + case 'x': case 'X' : +#ifdef PRINTF_LONG_SUPPORT + if (lng) + uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf); + else +#endif + ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf); + putchw(putp,putf,w,lz,bf); + break; + case 'c' : + putf(putp,(char)(va_arg(va, int))); + break; + case 's' : + putchw(putp,putf,w,0,va_arg(va, char*)); + break; + case '%' : + putf(putp,ch); + default: + break; + } + } + } + abort:; + } + + +void init_printf(void* putp,void (*putf) (void*,char)) + { + stdout_putf=putf; + stdout_putp=putp; + } + +void tfp_printf(char *fmt, ...) + { + va_list va; + va_start(va,fmt); + tfp_format(stdout_putp,stdout_putf,fmt,va); + va_end(va); + } + +static void putcp(void* p,char c) + { + *(*((char**)p))++ = c; + } + + + +void tfp_vsprintf(char* s,char *fmt, va_list va) + { + tfp_format(&s,putcp,fmt,va); + putcp(&s,0); + } + +void tfp_sprintf(char* s,char *fmt, ...) + { + va_list va; + va_start(va,fmt); + tfp_vsprintf(s,fmt,va); + va_end(va); + } + + diff --git a/lib/printf/printf.h b/lib/printf/printf.h new file mode 100644 index 0000000..0e65915 --- /dev/null +++ b/lib/printf/printf.h @@ -0,0 +1,125 @@ +/* +File: printf.h + +Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or other +materials provided with the distribution. + +Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its +contributors may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE. + +---------------------------------------------------------------------- + +This library is realy just two files: 'printf.h' and 'printf.c'. + +They provide a simple and small (+200 loc) printf functionality to +be used in embedded systems. + +I've found them so usefull in debugging that I do not bother with a +debugger at all. + +They are distributed in source form, so to use them, just compile them +into your project. + +Two printf variants are provided: printf and sprintf. + +The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'. + +Zero padding and field width are also supported. + +If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the +long specifier is also +supported. Note that this will pull in some long math routines (pun intended!) +and thus make your executable noticably longer. + +The memory foot print of course depends on the target cpu, compiler and +compiler options, but a rough guestimate (based on a H8S target) is about +1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space. +Not too bad. Your milage may vary. By hacking the source code you can +get rid of some hunred bytes, I'm sure, but personally I feel the balance of +functionality and flexibility versus code size is close to optimal for +many embedded systems. + +To use the printf you need to supply your own character output function, +something like : + +void putc ( void* p, char c) + { + while (!SERIAL_PORT_EMPTY) ; + SERIAL_PORT_TX_REGISTER = c; + } + +Before you can call printf you need to initialize it to use your +character output function with something like: + +init_printf(NULL,putc); + +Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc', +the NULL (or any pointer) you pass into the 'init_printf' will eventually be +passed to your 'putc' routine. This allows you to pass some storage space (or +anything realy) to the character output function, if necessary. +This is not often needed but it was implemented like that because it made +implementing the sprintf function so neat (look at the source code). + +The code is re-entrant, except for the 'init_printf' function, so it +is safe to call it from interupts too, although this may result in mixed output. +If you rely on re-entrancy, take care that your 'putc' function is re-entrant! + +The printf and sprintf functions are actually macros that translate to +'tfp_printf' and 'tfp_sprintf'. This makes it possible +to use them along with 'stdio.h' printf's in a single source file. +You just need to undef the names before you include the 'stdio.h'. +Note that these are not function like macros, so if you have variables +or struct members with these names, things will explode in your face. +Without variadic macros this is the best we can do to wrap these +fucnction. If it is a problem just give up the macros and use the +functions directly or rename them. + +For further details see source code. + +regs Kusti, 23.10.2004 +*/ + + +#ifndef __TFP_PRINTF__ +#define __TFP_PRINTF__ + +#include + +void init_printf(void* putp,void (*putf) (void*,char)); + +void tfp_printf(char *fmt, ...); +void tfp_sprintf(char* s,char *fmt, ...); +void tfp_vsprintf(char* s,char *fmt, va_list va); + +void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va); + +#define printf tfp_printf +#define sprintf tfp_sprintf + +#endif + + + diff --git a/lib/string/string.c b/lib/string/string.c new file mode 100644 index 0000000..be2c7ed --- /dev/null +++ b/lib/string/string.c @@ -0,0 +1,43 @@ +#include +#include "string.h" + +void* memcpy(void* restrict dstptr, const void* restrict srcptr, int size) +{ + unsigned char* dst = (unsigned char*) dstptr; + const unsigned char* src = (const unsigned char*) srcptr; + for ( int i = 0; i < size; i++ ) + dst[i] = src[i]; + return dstptr; +} + + +// extra function for non-null terminated +//length 0 for null terminated strings; +int strcmp_l(char *str1, char *str2, int length) +{ + int i=0; + while(true) + { + + if(str1[i]!=str2[i])return str1[i]-str2[i]; + i++; + if(i==length) return 0; + + if( length==0 && (str1[i]==0 || str2[i]==0 ) ) + return str1[i]-str2[i]; + + } + +} +static int strcmp(char *str1, char *str2) +{ + return strcmp_l(str1,str2,0); +} + + +int strlen(const char* string) +{ + int result = 0; + while ( string[result] )result++; + return result; +} diff --git a/lib/string/string.h b/lib/string/string.h new file mode 100644 index 0000000..e41eb94 --- /dev/null +++ b/lib/string/string.h @@ -0,0 +1,8 @@ +#ifndef STRING_H +#define STRING_H + + +void* memcpy(void* restrict dstptr, const void* restrict srcptr, int size); +int strcmp_l(char *str1, char *str2, int length); + +#endif diff --git a/xxx/lib/printf/printf.c b/xxx/lib/printf/printf.c deleted file mode 100644 index 23d3d46..0000000 --- a/xxx/lib/printf/printf.c +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list - * of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its - * contributors may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ - -#include "printf.h" - -typedef void (*putcf) (void*,char); -static putcf stdout_putf; -static void* stdout_putp; - - -#ifdef PRINTF_LONG_SUPPORT - -static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf) - { - int n=0; - unsigned int d=1; - while (num/d >= base) - d*=base; - while (d!=0) { - int dgt = num / d; - num%=d; - d/=base; - if (n || dgt>0|| d==0) { - *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10); - ++n; - } - } - *bf=0; - } - -static void li2a (long num, char * bf) - { - if (num<0) { - num=-num; - *bf++ = '-'; - } - uli2a(num,10,0,bf); - } - -#endif - -static void ui2a(unsigned int num, unsigned int base, int uc,char * bf) - { - int n=0; - unsigned int d=1; - while (num/d >= base) - d*=base; - while (d!=0) { - int dgt = num / d; - num%= d; - d/=base; - if (n || dgt>0 || d==0) { - *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10); - ++n; - } - } - *bf=0; - } - -static void i2a (int num, char * bf) - { - if (num<0) { - num=-num; - *bf++ = '-'; - } - ui2a(num,10,0,bf); - } - -static int a2d(char ch) - { - if (ch>='0' && ch<='9') - return ch-'0'; - else if (ch>='a' && ch<='f') - return ch-'a'+10; - else if (ch>='A' && ch<='F') - return ch-'A'+10; - else return -1; - } - -static char a2i(char ch, char** src,int base,int* nump) - { - char* p= *src; - int num=0; - int digit; - while ((digit=a2d(ch))>=0) { - if (digit>base) break; - num=num*base+digit; - ch=*p++; - } - *src=p; - *nump=num; - return ch; - } - -static void putchw(void* putp,putcf putf,int n, char z, char* bf) - { - char fc=z? '0' : ' '; - char ch; - char* p=bf; - while (*p++ && n > 0) - n--; - while (n-- > 0) - putf(putp,fc); - while ((ch= *bf++)) - putf(putp,ch); - } - -void tfp_format(void* putp,putcf putf,char *fmt, va_list va) - { - char bf[12]; - - char ch; - - - while ((ch=*(fmt++))) { - if (ch!='%') - putf(putp,ch); - else { - char lz=0; -#ifdef PRINTF_LONG_SUPPORT - char lng=0; -#endif - int w=0; - ch=*(fmt++); - if (ch=='0') { - ch=*(fmt++); - lz=1; - } - if (ch>='0' && ch<='9') { - ch=a2i(ch,&fmt,10,&w); - } -#ifdef PRINTF_LONG_SUPPORT - if (ch=='l') { - ch=*(fmt++); - lng=1; - } -#endif - switch (ch) { - case 0: - goto abort; - case 'u' : { -#ifdef PRINTF_LONG_SUPPORT - if (lng) - uli2a(va_arg(va, unsigned long int),10,0,bf); - else -#endif - ui2a(va_arg(va, unsigned int),10,0,bf); - putchw(putp,putf,w,lz,bf); - break; - } - case 'd' : { -#ifdef PRINTF_LONG_SUPPORT - if (lng) - li2a(va_arg(va, unsigned long int),bf); - else -#endif - i2a(va_arg(va, int),bf); - putchw(putp,putf,w,lz,bf); - break; - } - case 'x': case 'X' : -#ifdef PRINTF_LONG_SUPPORT - if (lng) - uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf); - else -#endif - ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf); - putchw(putp,putf,w,lz,bf); - break; - case 'c' : - putf(putp,(char)(va_arg(va, int))); - break; - case 's' : - putchw(putp,putf,w,0,va_arg(va, char*)); - break; - case '%' : - putf(putp,ch); - default: - break; - } - } - } - abort:; - } - - -void init_printf(void* putp,void (*putf) (void*,char)) - { - stdout_putf=putf; - stdout_putp=putp; - } - -void tfp_printf(char *fmt, ...) - { - va_list va; - va_start(va,fmt); - tfp_format(stdout_putp,stdout_putf,fmt,va); - va_end(va); - } - -static void putcp(void* p,char c) - { - *(*((char**)p))++ = c; - } - - - -void tfp_vsprintf(char* s,char *fmt, va_list va) - { - tfp_format(&s,putcp,fmt,va); - putcp(&s,0); - } - -void tfp_sprintf(char* s,char *fmt, ...) - { - va_list va; - va_start(va,fmt); - tfp_vsprintf(s,fmt,va); - va_end(va); - } - - diff --git a/xxx/lib/printf/printf.h b/xxx/lib/printf/printf.h deleted file mode 100644 index 0e65915..0000000 --- a/xxx/lib/printf/printf.h +++ /dev/null @@ -1,125 +0,0 @@ -/* -File: printf.h - -Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs - -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -Redistributions of source code must retain the above copyright notice, this list -of conditions and the following disclaimer. - -Redistributions in binary form must reproduce the above copyright notice, this -list of conditions and the following disclaimer in the documentation and/or other -materials provided with the distribution. - -Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its -contributors may be used to endorse or promote products derived from this software -without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, -INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, -OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY -OF SUCH DAMAGE. - ----------------------------------------------------------------------- - -This library is realy just two files: 'printf.h' and 'printf.c'. - -They provide a simple and small (+200 loc) printf functionality to -be used in embedded systems. - -I've found them so usefull in debugging that I do not bother with a -debugger at all. - -They are distributed in source form, so to use them, just compile them -into your project. - -Two printf variants are provided: printf and sprintf. - -The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'. - -Zero padding and field width are also supported. - -If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the -long specifier is also -supported. Note that this will pull in some long math routines (pun intended!) -and thus make your executable noticably longer. - -The memory foot print of course depends on the target cpu, compiler and -compiler options, but a rough guestimate (based on a H8S target) is about -1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space. -Not too bad. Your milage may vary. By hacking the source code you can -get rid of some hunred bytes, I'm sure, but personally I feel the balance of -functionality and flexibility versus code size is close to optimal for -many embedded systems. - -To use the printf you need to supply your own character output function, -something like : - -void putc ( void* p, char c) - { - while (!SERIAL_PORT_EMPTY) ; - SERIAL_PORT_TX_REGISTER = c; - } - -Before you can call printf you need to initialize it to use your -character output function with something like: - -init_printf(NULL,putc); - -Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc', -the NULL (or any pointer) you pass into the 'init_printf' will eventually be -passed to your 'putc' routine. This allows you to pass some storage space (or -anything realy) to the character output function, if necessary. -This is not often needed but it was implemented like that because it made -implementing the sprintf function so neat (look at the source code). - -The code is re-entrant, except for the 'init_printf' function, so it -is safe to call it from interupts too, although this may result in mixed output. -If you rely on re-entrancy, take care that your 'putc' function is re-entrant! - -The printf and sprintf functions are actually macros that translate to -'tfp_printf' and 'tfp_sprintf'. This makes it possible -to use them along with 'stdio.h' printf's in a single source file. -You just need to undef the names before you include the 'stdio.h'. -Note that these are not function like macros, so if you have variables -or struct members with these names, things will explode in your face. -Without variadic macros this is the best we can do to wrap these -fucnction. If it is a problem just give up the macros and use the -functions directly or rename them. - -For further details see source code. - -regs Kusti, 23.10.2004 -*/ - - -#ifndef __TFP_PRINTF__ -#define __TFP_PRINTF__ - -#include - -void init_printf(void* putp,void (*putf) (void*,char)); - -void tfp_printf(char *fmt, ...); -void tfp_sprintf(char* s,char *fmt, ...); -void tfp_vsprintf(char* s,char *fmt, va_list va); - -void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va); - -#define printf tfp_printf -#define sprintf tfp_sprintf - -#endif - - - diff --git a/xxx/lib/string/string.c b/xxx/lib/string/string.c deleted file mode 100644 index 729c509..0000000 --- a/xxx/lib/string/string.c +++ /dev/null @@ -1,37 +0,0 @@ -#include - -//length 0 for null terminated strings; -bool strcmp(char *str1, char *str2, int length) -{ - int i=0; - while(true) - { - if(str1[i]!=str2[i])return false; - i++; - - if(i==length) return true; - if(str1[i]==0||str2[i]==0) - { - if(str1[i]==str2[i])return true; - return false; - } - } - -} - -void* memcpy(void* restrict dstptr, const void* restrict srcptr, int size) -{ - unsigned char* dst = (unsigned char*) dstptr; - const unsigned char* src = (const unsigned char*) srcptr; - for ( int i = 0; i < size; i++ ) - dst[i] = src[i]; - return dstptr; -} - -int strlen(const char* string) -{ - int result = 0; - while ( string[result] ) - result++; - return result; -} diff --git a/xxx/lib/string/string.h b/xxx/lib/string/string.h deleted file mode 100644 index a804de9..0000000 --- a/xxx/lib/string/string.h +++ /dev/null @@ -1,3 +0,0 @@ -#include -bool strcmp(char *str1, char *str2, int length); -void* memcpy(void* restrict dstptr, const void* restrict srcptr, int size); -- cgit v1.2.3