source: libcaca/trunk/kernel/kernel.c @ 4148

Last change on this file since 4148 was 4148, checked in by Sam Hocevar, 10 years ago

Update my e-mail address everywhere.

  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/*
2 *  libcaca      Canvas for ultrafast compositing of Unicode letters
3 *  libcaca       Colour ASCII-Art library
4 *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
5 *                2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
6 *                All Rights Reserved
7 *
8 *  $Id: kernel.c 4148 2009-12-19 14:38:38Z sam $
9 *
10 *  This library is free software. It comes without any warranty, to
11 *  the extent permitted by applicable law. You can redistribute it
12 *  and/or modify it under the terms of the Do What The Fuck You Want
13 *  To Public License, Version 2, as published by Sam Hocevar. See
14 *  http://sam.zoy.org/wtfpl/COPYING for more details.
15 */
16
17/*
18 *  This file contains replacement functions for the standard C library
19 *  that must be used when building libcaca and libcaca into a kernel.
20 */
21
22#include "config.h"
23
24#include "caca_types.h"
25
26#ifdef __KERNEL__
27
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)
34
35/* Our default seed for random number generator */
36static int seed = 0x68743284;
37
38/* Our memory mapping */
39static uint32_t *freemem = (uint32_t*) 0x00200000;
40
41/* Multiboot kernel entry point */
42void 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;
47
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);
57}
58
59/* stdlib.h functions */
60void *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
71void free(void *ptr)
72{
73    return;
74}
75
76void *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
98char *getenv(const char *name)
99{
100    return NULL;
101}
102
103int getpid(void)
104{
105    return 0x1337;
106}
107
108void srand(unsigned int s)
109{
110    seed = rand();
111}
112
113int time(void *dummy)
114{
115    return rand();
116}
117
118int rand(void)
119{
120    seed = (seed * 0x7f32ba17) ^ 0xf893a735;
121    return seed % RAND_MAX;
122}
123
124int abs(int j)
125{
126    if(j < 0)
127        return -j;
128    return j;
129}
130
131void exit(int status)
132{
133    /* FIXME: reboot? */
134    while(1);
135}
136
137int atexit(void (*function)(void))
138{
139    /* FIXME: register function */
140    return 0;
141}
142
143/* string.h functions */
144void *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
154void *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
165void *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
172size_t strlen(const char *s)
173{
174    int len = 0;
175
176    while(*s++)
177        len++;
178
179    return len;
180}
181
182int 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
193int 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
204int 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
218char *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
229char *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 */
240int vsnprintf(char *str, size_t size, const char *format, va_list ap)
241{
242    /* FIXME */
243    return 0;
244}
245
246/* stdio.h functions */
247FILE *fopen(const char *path, const char *mode)
248{
249    /* FIXME */
250    return NULL;
251}
252
253int feof(FILE *stream)
254{
255    /* FIXME */
256    return 0;
257}
258
259char *fgets(char *s, int size, FILE *stream)
260{
261    /* FIXME */
262    return NULL;
263}
264
265size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
266{
267    return 0;
268}
269
270int fclose(FILE *fp)
271{
272    /* FIXME */
273    return 0;
274}
275
276int printf(const char *format, ...)
277{
278    /* FIXME */
279    return 0;
280}
281
282int fprintf(FILE *stream, const char *format, ...)
283{
284    /* FIXME */
285    return 0;
286}
287
288int fflush(FILE *stream)
289{
290    /* FIXME */
291    return 0;
292}
293
294int sprintf(char *str, const char *format, ...)
295{
296    /* FIXME */
297    return 0;
298}
299
300int sscanf(const char *str, const char *format, ...)
301{
302    /* FIXME */
303    return 0;
304}
305
306/* unistd.h functions */
307void usleep(unsigned long usec)
308{
309    /* FIXME */
310    return;
311}
312
313/* time.h functions */
314int 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 */
334double 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
359double 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
385double 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
399int errno = 0;
400
401#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.