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

Last change on this file since 2821 was 2821, checked in by Sam Hocevar, 11 years ago

Starting refactoring to get rid of libcucul. The initial reason for the
split is rendered moot by the plugin system: when enabled, binaries do
not link directly with libX11 or libGL. I hope this is a step towards
more consisteny and clarity.

  • Property svn:keywords set to Id
File size: 6.3 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@zoy.org>
5 *                2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
6 *                All Rights Reserved
7 *
8 *  $Id: kernel.c 2821 2008-09-27 13:12:46Z 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
137/* string.h functions */
138void *memset(void *s, int c, size_t n)
139{
140    uint8_t *ptr = s;
141
142    while(n--)
143        *ptr++ = c;
144
145    return s;
146}
147
148void *memcpy(void *dest, const void *src, size_t n)
149{
150    uint8_t *destptr = dest;
151    uint8_t const *srcptr = src;
152
153    while(n--)
154        *destptr++ = *srcptr++;
155
156    return dest;
157}
158
159size_t strlen(const char *s)
160{
161    int len = 0;
162
163    while(*s++)
164        len++;
165
166    return len;
167}
168
169int strcmp(const char *s1, const char *s2)
170{
171    while(*s1 && *s1 == *s2)
172    {
173        s1++;
174        s2++;
175    }
176
177    return (int)*s1 - (int)*s2;
178}
179
180int strcasecmp(const char *s1, const char *s2)
181{
182    while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
183    {
184        s1++;
185        s2++;
186    }
187
188    return (int)UPPER(*s1) - (int)UPPER(*s2);
189}
190
191int memcmp(const void *_s1, const void *_s2, size_t n)
192{
193    uint8_t const *s1 = _s1, *s2 = _s2;
194
195    while(n--)
196    {
197        if(*s1 != *s2)
198            return (int)*s1 - (int)*s2;
199        s1++;
200        s2++;
201    }
202    return 0;
203}
204
205char *strdup(const char *s)
206{
207    char *new;
208    unsigned int len = strlen(s);
209
210    new = malloc(len + 1);
211    memcpy(new, s, len + 1);
212
213    return new;
214}
215
216char *strchr(const char *s, int c)
217{
218    do
219        if(*s == c)
220            return (char *)(intptr_t)s;
221    while(*s++);
222
223    return NULL;
224}
225
226/* stdarg.h functions */
227int vsnprintf(char *str, size_t size, const char *format, va_list ap)
228{
229    /* FIXME */
230    return 0;
231}
232
233/* stdio.h functions */
234FILE *fopen(const char *path, const char *mode)
235{
236    /* FIXME */
237    return NULL;
238}
239
240int feof(FILE *stream)
241{
242    /* FIXME */
243    return 0;
244}
245
246char *fgets(char *s, int size, FILE *stream)
247{
248    /* FIXME */
249    return NULL;
250}
251
252size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
253{
254    return 0;
255}
256
257int fclose(FILE *fp)
258{
259    /* FIXME */
260    return 0;
261}
262
263int printf(const char *format, ...)
264{
265    /* FIXME */
266    return 0;
267}
268
269int fprintf(FILE *stream, const char *format, ...)
270{
271    /* FIXME */
272    return 0;
273}
274
275int fflush(FILE *stream)
276{
277    /* FIXME */
278    return 0;
279}
280
281int sprintf(char *str, const char *format, ...)
282{
283    /* FIXME */
284    return 0;
285}
286
287int sscanf(const char *str, const char *format, ...)
288{
289    /* FIXME */
290    return 0;
291}
292
293/* unistd.h functions */
294void usleep(unsigned long usec)
295{
296    /* FIXME */
297    return;
298}
299
300/* time.h functions */
301int gettimeofday(struct timeval *tv, struct timezone *tz)
302{
303    static int usec = 0;
304    static int sec = 0;
305
306    /* FIXME */
307    usec += 10000;
308    if(usec > 1000000)
309    {
310        sec++;
311        usec -= 1000000;
312    }
313
314    tv->tv_sec = sec;
315    tv->tv_usec = usec;
316
317    return 0;
318}
319
320/* math.h functions */
321double cos(double x)
322{
323    double ret = 0.0;
324#ifdef HAVE_FSIN_FCOS
325    asm volatile("fcos" : "=t" (ret) : "0" (x));
326#else
327    double x2;
328    double num = 1.0;
329    double fact = 1.0;
330    int i;
331
332    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
333    x2 = x * x;
334
335    /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
336    for(i = 0; i < 10; i++)
337    {
338        ret += num / fact;
339        num *= - x2;
340        fact *= (2 * i + 1) * (2 * i + 2);
341    }
342#endif
343    return ret;
344}
345
346double sin(double x)
347{
348    double ret = 0.0;
349#ifdef HAVE_FSIN_FCOS
350    asm volatile("fsin" : "=t" (ret) : "0" (x));
351#else
352    double x2;
353    double num;
354    double fact = 1.0;
355    int i;
356
357    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
358    x2 = x * x;
359    num = x;
360
361    /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
362    for(i = 0; i < 10; i++)
363    {
364        ret += num / fact;
365        num *= - x2;
366        fact *= (2 * i + 2) * (2 * i + 3);
367    }
368#endif
369    return ret;
370}
371
372double sqrt(double x)
373{
374    double ret = x;
375    int i;
376
377    /* This is Newton's method */
378    for(i = 0; i < 10; i++)
379        ret = (ret * ret + x) / (ret * 2.0);
380
381    return ret;
382}
383
384/* errno.h stuff */
385
386int errno = 0;
387
388#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.