Changeset 4154


Ignore:
Timestamp:
12/20/09 14:33:11 (3 years ago)
Author:
jylam
Message:
  • Total kernel rewrite
Location:
libcaca/trunk
Files:
21 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/configure.ac

    r4144 r4154  
    309309if test "${enable_vga}" = "yes"; then 
    310310  ac_cv_my_have_vga="yes" 
    311   CPPFLAGS="${CPPFLAGS} -I. -D__KERNEL__ -nostdinc -include kernel/kernel.h -fno-stack-protector" 
     311  CPPFLAGS="${CPPFLAGS} -I. -D__KERNEL__ -nostdinc -include kernel/klibc.h -fno-stack-protector" 
    312312  CFLAGS="${CFLAGS} -fno-builtin -O2 -Wall -fno-stack-protector" 
    313313  CCASFLAGS="${CCASFLAGS} -I. -fno-stack-protector" 
  • libcaca/trunk/kernel/kernel.c

    r4148 r4154  
    1 /* 
    2  *  libcaca      Canvas for ultrafast compositing of Unicode letters 
     1/*  
     2 *  libcaca       
    33 *  libcaca       Colour ASCII-Art library 
    4  *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net> 
    5  *                2006 Jean-Yves Lamoureux <jylam@lnxscene.org> 
     4 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org> 
     5 *                2006-2009 Jean-Yves Lamoureux <jylam@lnxscene.org> 
    66 *                All Rights Reserved 
    77 * 
     
    1515 */ 
    1616 
    17 /* 
     17/*  
    1818 *  This file contains replacement functions for the standard C library 
    1919 *  that must be used when building libcaca and libcaca into a kernel. 
     
    2121 
    2222#include "config.h" 
    23  
    2423#include "caca_types.h" 
    2524 
    2625#ifdef __KERNEL__ 
    2726 
    28 #define IS_DIGIT(x) (x>='0' && x<='9') 
    29 #define IS_ALPHA(x) (x>='A' && x<='z') 
    30 #define IS_UPPER(x) (x>='A' && x<='Z') 
    31 #define IS_LOWER(x) (x>='a' && x<='z') 
    32 #define UPPER(x) (IS_LOWER(x)?(x+('A'-'a')):x) 
    33 #define LOWER(x) (IS_UPPER(x)?(x-('a'-'A')):x) 
     27#include "klibc.h" 
     28#include "boot/stage2.h" 
     29#include "kernel.h" 
     30#include "drivers/processor.h" 
     31#include "drivers/floppy.h" 
     32#include "drivers/timer.h" 
    3433 
    35 /* Our default seed for random number generator */ 
    36 static int seed = 0x68743284; 
     34extern char const * const * caca_get_display_driver_list(void); 
    3735 
    38 /* Our memory mapping */ 
    39 static uint32_t *freemem = (uint32_t*) 0x00200000; 
     36/* C entry point, called from stage2 */ 
     37int kmain(void) 
     38{ 
     39    struct processor_info processor_info; 
     40    struct floppy_info    floppy_info; 
     41     
     42    printf("_start at 0x%x\n", _start); 
     43    printf("kmain() at 0x%x\n", kmain); 
     44     
     45    processor_get_info(&processor_info); 
     46    processor_print_info(&processor_info); 
    4047 
    41 /* Multiboot kernel entry point */ 
    42 void cmain(unsigned long int magic, unsigned long int addr) 
    43 { 
    44     static char const text[] = "Booting libcaca kernel..."; 
    45     char const *ptr = text; 
    46     char *video = (char*)0xB8000; 
     48    floppy_get_info(&floppy_info); 
     49    floppy_print_info(&floppy_info); 
    4750 
    48     char *argv[] = { NULL }; 
    49     int argc = 0; 
    50  
    51     /* Print a text message to say hello */ 
    52     while(*ptr) 
    53         *video = *ptr++; video += 2; 
    54  
    55     /* Launch the main program */ 
    56     main(argc, argv); 
     51    enable_interrupt(1);  // Enable Keyboard Interrupt (IRQ1) 
     52    enable_interrupt(0);  // Enable IRQ0 (timer) 
     53    enable_interrupt(13); 
     54    timer_phase(100);     // Fire IRQ0 each 1/100s 
     55     
     56   
     57    printf("Waiting 1s\n"); 
     58    sleep(1); 
     59    printf("Waiting 2s\n"); 
     60    sleep(2); 
     61    printf("Waiting 3s\n"); 
     62    sleep(3); 
     63    printf("Ok\n"); 
     64    //caca_get_display_driver_list(); 
     65     
     66     
     67    while (1) 
     68    { 
     69         
     70    } 
    5771} 
    5872 
    59 /* stdlib.h functions */ 
    60 void *malloc(size_t size) 
    61 { 
    62     uint32_t *p = freemem; 
    63     if(!size) 
    64         return NULL; 
    65     size = (size + 0x7) / 4; 
    66     *p = size; 
    67     freemem += size + 1; 
    68     return p + 1; 
    69 } 
    70  
    71 void free(void *ptr) 
    72 { 
    73     return; 
    74 } 
    75  
    76 void *realloc(void *ptr, size_t size) 
    77 { 
    78     uint32_t oldsize; 
    79     void *p; 
    80  
    81     if(!size) 
    82         return NULL; 
    83  
    84     if(!ptr) 
    85         oldsize = 0; 
    86     else 
    87     { 
    88         oldsize = ((uint32_t *)ptr)[-1]; 
    89         if(oldsize >= size) 
    90             return ptr; 
    91     } 
    92  
    93     p = malloc(size); 
    94     memcpy(p, ptr, oldsize); 
    95     return p; 
    96 } 
    97  
    98 char *getenv(const char *name) 
    99 { 
    100     return NULL; 
    101 } 
    102  
    103 int getpid(void) 
    104 { 
    105     return 0x1337; 
    106 } 
    107  
    108 void srand(unsigned int s) 
    109 { 
    110     seed = rand(); 
    111 } 
    112  
    113 int time(void *dummy) 
    114 { 
    115     return rand(); 
    116 } 
    117  
    118 int rand(void) 
    119 { 
    120     seed = (seed * 0x7f32ba17) ^ 0xf893a735; 
    121     return seed % RAND_MAX; 
    122 } 
    123  
    124 int abs(int j) 
    125 { 
    126     if(j < 0) 
    127         return -j; 
    128     return j; 
    129 } 
    130  
    131 void exit(int status) 
    132 { 
    133     /* FIXME: reboot? */ 
    134     while(1); 
    135 } 
    136  
    137 int atexit(void (*function)(void)) 
    138 { 
    139     /* FIXME: register function */ 
    140     return 0; 
    141 } 
    142  
    143 /* string.h functions */ 
    144 void *memset(void *s, int c, size_t n) 
    145 { 
    146     uint8_t *ptr = s; 
    147  
    148     while(n--) 
    149         *ptr++ = c; 
    150  
    151     return s; 
    152 } 
    153  
    154 void *memcpy(void *dest, const void *src, size_t n) 
    155 { 
    156     uint8_t *destptr = dest; 
    157     uint8_t const *srcptr = src; 
    158  
    159     while(n--) 
    160         *destptr++ = *srcptr++; 
    161  
    162     return dest; 
    163 } 
    164  
    165 void *memmove(void *dest, const void *src, size_t n) 
    166 { 
    167     memcpy(freemem, src, n); 
    168     memcpy(dest, freemem, n); 
    169     return dest; 
    170 } 
    171  
    172 size_t strlen(const char *s) 
    173 { 
    174     int len = 0; 
    175  
    176     while(*s++) 
    177         len++; 
    178  
    179     return len; 
    180 } 
    181  
    182 int strcmp(const char *s1, const char *s2) 
    183 { 
    184     while(*s1 && *s1 == *s2) 
    185     { 
    186         s1++; 
    187         s2++; 
    188     } 
    189  
    190     return (int)*s1 - (int)*s2; 
    191 } 
    192  
    193 int strcasecmp(const char *s1, const char *s2) 
    194 { 
    195     while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2)) 
    196     { 
    197         s1++; 
    198         s2++; 
    199     } 
    200  
    201     return (int)UPPER(*s1) - (int)UPPER(*s2); 
    202 } 
    203  
    204 int memcmp(const void *_s1, const void *_s2, size_t n) 
    205 { 
    206     uint8_t const *s1 = _s1, *s2 = _s2; 
    207  
    208     while(n--) 
    209     { 
    210         if(*s1 != *s2) 
    211             return (int)*s1 - (int)*s2; 
    212         s1++; 
    213         s2++; 
    214     } 
    215     return 0; 
    216 } 
    217  
    218 char *strdup(const char *s) 
    219 { 
    220     char *new; 
    221     unsigned int len = strlen(s); 
    222  
    223     new = malloc(len + 1); 
    224     memcpy(new, s, len + 1); 
    225  
    226     return new; 
    227 } 
    228  
    229 char *strchr(const char *s, int c) 
    230 { 
    231     do 
    232         if(*s == c) 
    233             return (char *)(intptr_t)s; 
    234     while(*s++); 
    235  
    236     return NULL; 
    237 } 
    238  
    239 /* stdarg.h functions */ 
    240 int vsnprintf(char *str, size_t size, const char *format, va_list ap) 
    241 { 
    242     /* FIXME */ 
    243     return 0; 
    244 } 
    245  
    246 /* stdio.h functions */ 
    247 FILE *fopen(const char *path, const char *mode) 
    248 { 
    249     /* FIXME */ 
    250     return NULL; 
    251 } 
    252  
    253 int feof(FILE *stream) 
    254 { 
    255     /* FIXME */ 
    256     return 0; 
    257 } 
    258  
    259 char *fgets(char *s, int size, FILE *stream) 
    260 { 
    261     /* FIXME */ 
    262     return NULL; 
    263 } 
    264  
    265 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) 
    266 { 
    267     return 0; 
    268 } 
    269  
    270 int fclose(FILE *fp) 
    271 { 
    272     /* FIXME */ 
    273     return 0; 
    274 } 
    275  
    276 int printf(const char *format, ...) 
    277 { 
    278     /* FIXME */ 
    279     return 0; 
    280 } 
    281  
    282 int fprintf(FILE *stream, const char *format, ...) 
    283 { 
    284     /* FIXME */ 
    285     return 0; 
    286 } 
    287  
    288 int fflush(FILE *stream) 
    289 { 
    290     /* FIXME */ 
    291     return 0; 
    292 } 
    293  
    294 int sprintf(char *str, const char *format, ...) 
    295 { 
    296     /* FIXME */ 
    297     return 0; 
    298 } 
    299  
    300 int sscanf(const char *str, const char *format, ...) 
    301 { 
    302     /* FIXME */ 
    303     return 0; 
    304 } 
    305  
    306 /* unistd.h functions */ 
    307 void usleep(unsigned long usec) 
    308 { 
    309     /* FIXME */ 
    310     return; 
    311 } 
    312  
    313 /* time.h functions */ 
    314 int gettimeofday(struct timeval *tv, struct timezone *tz) 
    315 { 
    316     static int usec = 0; 
    317     static int sec = 0; 
    318  
    319     /* FIXME */ 
    320     usec += 10000; 
    321     if(usec > 1000000) 
    322     { 
    323         sec++; 
    324         usec -= 1000000; 
    325     } 
    326  
    327     tv->tv_sec = sec; 
    328     tv->tv_usec = usec; 
    329  
    330     return 0; 
    331 } 
    332  
    333 /* math.h functions */ 
    334 double cos(double x) 
    335 { 
    336     double ret = 0.0; 
    337 #ifdef HAVE_FSIN_FCOS 
    338     asm volatile("fcos" : "=t" (ret) : "0" (x)); 
    339 #else 
    340     double x2; 
    341     double num = 1.0; 
    342     double fact = 1.0; 
    343     int i; 
    344  
    345     x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI); 
    346     x2 = x * x; 
    347  
    348     /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */ 
    349     for(i = 0; i < 10; i++) 
    350     { 
    351         ret += num / fact; 
    352         num *= - x2; 
    353         fact *= (2 * i + 1) * (2 * i + 2); 
    354     } 
    355 #endif 
    356     return ret; 
    357 } 
    358  
    359 double sin(double x) 
    360 { 
    361     double ret = 0.0; 
    362 #ifdef HAVE_FSIN_FCOS 
    363     asm volatile("fsin" : "=t" (ret) : "0" (x)); 
    364 #else 
    365     double x2; 
    366     double num; 
    367     double fact = 1.0; 
    368     int i; 
    369  
    370     x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI); 
    371     x2 = x * x; 
    372     num = x; 
    373  
    374     /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */ 
    375     for(i = 0; i < 10; i++) 
    376     { 
    377         ret += num / fact; 
    378         num *= - x2; 
    379         fact *= (2 * i + 2) * (2 * i + 3); 
    380     } 
    381 #endif 
    382     return ret; 
    383 } 
    384  
    385 double sqrt(double x) 
    386 { 
    387     double ret = x; 
    388     int i; 
    389  
    390     /* This is Newton's method */ 
    391     for(i = 0; i < 10; i++) 
    392         ret = (ret * ret + x) / (ret * 2.0); 
    393  
    394     return ret; 
    395 } 
    396  
    397 /* errno.h stuff */ 
    398  
    399 int errno = 0; 
    40073 
    40174#endif /* __KERNEL__ */ 
  • libcaca/trunk/kernel/kernel.h

    r4148 r4154  
    22 *  libcaca      Canvas for ultrafast compositing of Unicode letters 
    33 *  libcaca       Colour ASCII-Art library 
    4  *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net> 
     4 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org> 
    55 *                All Rights Reserved 
    66 * 
     
    1919 */ 
    2020 
    21 /* Various defines */ 
    22 #define NULL ((void *)0) 
    23 #define EOF (-1) 
    24 #define BUFSIZ 4096 
    25 #define RAND_MAX ((unsigned int)0x8000000) 
    26 #define INT_MAX ((int)0x7fffffff) 
    27 #define M_PI 3.14159265358979323846 
    28 #define __BYTE_ORDER 1 
    29 #define __BIG_ENDIAN 2 
    3021 
    31 /* Assembly code for outb and inb */ 
    32 extern inline void outb(unsigned char val, unsigned short port); 
    33 extern inline unsigned char inb(unsigned short port); 
    3422 
    35 extern inline void outb(unsigned char val, unsigned short port) 
    36 { 
    37     __asm__ __volatile__ ("outb %b0,%w1" : : "a" (val), "Nd" (port)); 
    38 } 
    3923 
    40 extern inline unsigned char inb(unsigned short port) 
    41 { 
    42     unsigned char tmp; 
    43     __asm__ __volatile__ ("inb %w1,%0" : "=a" (tmp) : "Nd" (port)); 
    44     return tmp; 
    45 } 
     24extern void init_gdt(void); 
     25void init_pic(void); 
     26void init_idt(void); 
     27void putcar(unsigned char c); 
     28void dump_gdt(void); 
    4629 
    47 /* Various typedefs -- some are x86-specific */ 
    48 #define CUSTOM_INTTYPES 
    49 typedef unsigned int size_t; 
     30void disable_interrupt(char i); 
     31void enable_interrupt(char i); 
    5032 
    51 typedef struct file 
    52 { 
    53     void *mem; 
    54 } FILE; 
    5533 
    56 struct timeval { 
    57     int tv_sec; 
    58     int tv_usec; 
    59 }; 
     34#define cli             __asm__("cli"::) 
     35#define sti             __asm__("sti"::) 
    6036 
    61 struct timezone { 
    62     int tz_minuteswest; 
    63     int tz_dsttime; 
    64 }; 
    6537 
    66 /* Multiboot kernel entry point */ 
    67 void cmain(unsigned long int magic, unsigned long int addr); 
    6838 
    6939/* The application's entry point */ 
    7040int main(int argc, char *argv[]); 
    7141 
    72 /* stdlib.h functions */ 
    73 void *malloc(size_t size); 
    74 void free(void *ptr); 
    75 void *realloc(void *ptr, size_t size); 
    76 char *getenv(const char *name); 
    77 int rand(void); 
    78 int abs(int j); 
    79 void exit(int status); 
    80 void srand(unsigned int s); 
    81 int atexit(void (*function)(void)); 
    82 FILE *stdin, *stdout, *stderr; 
    83  
    84 /* string.h functions */ 
    85 void *memset(void *s, int c, size_t n); 
    86 void *memcpy(void *dest, const void *src, size_t n); 
    87 void *memmove(void *dest, const void *src, size_t n); 
    88 size_t strlen(const char *s); 
    89 int strcmp(const char *s1, const char *s2); 
    90 int strcasecmp(const char *s1, const char *s2); 
    91 int memcmp(const void *s1, const void *s2, size_t n); 
    92 char *strdup(const char *s); 
    93 char *strchr(const char *s, int c); 
    94  
    95 /* stdarg.h functions */ 
    96 typedef void * va_list; 
    97 #define va_start(v,a) v = (void *)((uintptr_t)(&a) + sizeof(a)) 
    98 #define va_end(v) 
    99 int vsnprintf(char *str, size_t size, const char *format, va_list ap); 
    100  
    101 /* stdio.h functions */ 
    102 FILE *fopen(const char *path, const char *mode); 
    103 int feof(FILE *stream); 
    104 char *fgets(char *s, int size, FILE *stream); 
    105 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); 
    106 int fclose(FILE *fp); 
    107 int printf(const char *format, ...); 
    108 int fprintf(FILE *stream, const char *format, ...); 
    109 int fflush(FILE *stream); 
    110 int sprintf(char *str, const char *format, ...); 
    111 int sscanf(const char *str, const char *format, ...); 
    112  
    113 /* unistd.h functions */ 
    114 void usleep(unsigned long usec); 
    115 int getpid(void); 
    116  
    117 /* time.h functions */ 
    118 int gettimeofday(struct timeval *tv, struct timezone *tz); 
    119 int time(void *); 
    120  
    121 /* math.h functions */ 
    122 double cos(double x); 
    123 double sin(double x); 
    124 double sqrt(double x); 
    125  
    126 /* errno.h functions */ 
    127 #define ENOENT   2 /* No such file or directory */ 
    128 #define ENOMEM  12 /* Out of memory */ 
    129 #define EBUSY   16 /* Device or resource busy */ 
    130 #define ENODEV  19 /* No such device */ 
    131 #define EINVAL  22 /* Invalid argument */ 
    132 #define ENOTTY  25 /* Not a typewriter */ 
    133 #define ENOSYS  38 /* Function not implemented */ 
    134 extern int errno; 
    135  
    136 /* arpa/inet.h functions */ 
    137 unsigned int htonl(unsigned int hostlong); 
    138 unsigned short htons(unsigned short hostlong); 
    139  
Note: See TracChangeset for help on using the changeset viewer.