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

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

Add atexit() and EOF to the kernel.

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