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

Last change on this file since 1049 was 1049, checked in by Sam Hocevar, 14 years ago
  • A few coding style fixes.
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  libcaca       Colour ASCII-Art library
4 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
5 *                All Rights Reserved
6 *
7 *  $Id: kernel.c 1049 2006-09-17 12:58:12Z sam $
8 *
9 *  This library is free software; you can redistribute it and/or
10 *  modify it under the terms of the Do What The Fuck You Want To
11 *  Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains replacement functions for the standard C library
17 *  that must be used when building libcucul and libcaca into a kernel.
18 */
19
20#include "config.h"
21#include "common.h"
22
23#ifdef __KERNEL__
24
25#define IS_DIGIT(x) (x>='0' && x<='9')
26#define IS_ALPHA(x) (x>='A' && x<='z')
27#define IS_UPPER(x) (x>='A' && x<='Z')
28#define IS_LOWER(x) (x>='a' && x<='z')
29#define UPPER(x) (IS_LOWER(x)?(x+('A'-'a')):x)
30#define LOWER(x) (IS_UPPER(x)?(x-('a'-'A')):x)
31
32/* Our default seed for random number generator */
33static int seed = 0x68743284;
34
35/* Our memory mapping */
36static uint32_t *freemem = (uint32_t*) 0x00200000;
37
38/* Multiboot kernel entry point */
39void cmain(unsigned long int magic, unsigned long int addr)
40{
41    static char const text[] = "Booting libcaca kernel...";
42    char const *ptr = text;
43    char *video = (char*)0xB8000;
44
45    char *argv[] = { NULL };
46    int argc = 0;
47
48    /* Print a text message to say hello */
49    while(*ptr)
50        *video = *ptr++; video += 2;
51
52    /* Launch the main program */
53    main(argc, argv);
54}
55
56/* stdlib.h functions */
57void *malloc(size_t size)
58{
59    uint32_t *p = freemem;
60    if(!size)
61        return NULL;
62    size = (size + 0x7) / 4;
63    *p = size;
64    freemem += size + 1;
65    return p + 1;
66}
67
68void free(void *ptr)
69{
70    return;
71}
72
73void *realloc(void *ptr, size_t size)
74{
75    uint32_t oldsize;
76    void *p;
77
78    if(!size)
79        return NULL;
80
81    if(!ptr)
82        oldsize = 0;
83    else
84    {
85        oldsize = ((uint32_t *)ptr)[-1];
86        if(oldsize >= size)
87            return ptr;
88    }
89
90    p = malloc(size);
91    memcpy(p, ptr, oldsize);
92    return p;
93}
94
95char *getenv(const char *name)
96{
97    return NULL;
98}
99
100int getpid(void)
101{
102    return 0x1337;
103}
104
105void srand(unsigned int s)
106{
107    seed = rand();
108}
109
110int time(void *dummy)
111{
112    return rand();
113}
114
115int rand(void)
116{
117    seed = (seed * 0x7f32ba17) ^ 0xf893a735;
118    return seed % RAND_MAX;
119}
120
121int abs(int j)
122{
123    if(j < 0)
124        return -j;
125    return j;
126}
127
128void exit(int status)
129{
130    /* FIXME: reboot? */
131    while(1);
132}
133
134/* string.h functions */
135void *memset(void *s, int c, size_t n)
136{
137    uint8_t *ptr = s;
138
139    while(n--)
140        *ptr++ = c;
141
142    return s;
143}
144
145void *memcpy(void *dest, const void *src, size_t n)
146{
147    uint8_t *destptr = dest;
148    uint8_t const *srcptr = src;
149
150    while(n--)
151        *destptr++ = *srcptr++;
152
153    return dest;
154}
155
156size_t strlen(const char *s)
157{
158    int len = 0;
159
160    while(*s++)
161        len++;
162
163    return len;
164}
165
166int strcasecmp(const char *s1, const char *s2)
167{
168    while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
169    {
170        s1++;
171        s2++;
172    }
173
174    return (int)UPPER(*s1) - (int)UPPER(*s2);
175}
176
177int memcmp(const char *s1, const char *s2, size_t n)
178{
179    while(n--)
180    {
181        if(*s1 != *s2)
182            return *s1 - *s2;
183        *s1++;
184        *s2++;
185    }
186    return 0;
187}
188
189/* stdarg.h functions */
190int vsnprintf(char *str, size_t size, const char *format, va_list ap)
191{
192    /* FIXME */
193    return 0;
194}
195
196/* stdio.h functions */
197FILE *fopen(const char *path, const char *mode)
198{
199    /* FIXME */
200    return NULL;
201}
202
203int feof(FILE *stream)
204{
205    /* FIXME */
206    return 0;
207}
208
209char *fgets(char *s, int size, FILE *stream)
210{
211    /* FIXME */
212    return NULL;
213}
214
215size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
216{
217    return 0;
218}
219
220int fclose(FILE *fp)
221{
222    /* FIXME */
223    return 0;
224}
225
226int printf(const char *format, ...)
227{
228    /* FIXME */
229    return 0;
230}
231
232int sprintf(char *str, const char *format, ...)
233{
234    /* FIXME */
235    return 0;
236}
237
238int sscanf(const char *str, const char *format, ...)
239{
240    /* FIXME */
241    return 0;
242}
243
244/* unistd.h functions */
245void usleep(unsigned long usec)
246{
247    /* FIXME */
248    return;
249}
250
251/* time.h functions */
252int gettimeofday(struct timeval *tv, struct timezone *tz)
253{
254    static int usec = 0;
255    static int sec = 0;
256
257    /* FIXME */
258    usec += 10000;
259    if(usec > 1000000)
260    {
261        sec++;
262        usec -= 1000000;
263    }
264
265    tv->tv_sec = sec;
266    tv->tv_usec = usec;
267
268    return 0;
269}
270
271/* math.h functions */
272double cos(double x)
273{
274    double ret = 0.0;
275#ifdef HAVE_FSIN_FCOS
276    asm volatile("fcos" : "=t" (ret) : "0" (x));
277#else
278    double x2;
279    double num = 1.0;
280    double fact = 1.0;
281    int i;
282
283    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
284    x2 = x * x;
285
286    /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
287    for(i = 0; i < 10; i++)
288    {
289        ret += num / fact;
290        num *= - x2;
291        fact *= (2 * i + 1) * (2 * i + 2);
292    }
293#endif
294    return ret;
295}
296
297double sin(double x)
298{
299    double ret = 0.0;
300#ifdef HAVE_FSIN_FCOS
301    asm volatile("fsin" : "=t" (ret) : "0" (x));
302#else
303    double x2;
304    double num;
305    double fact = 1.0;
306    int i;
307
308    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
309    x2 = x * x;
310    num = x;
311
312    /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
313    for(i = 0; i < 10; i++)
314    {
315        ret += num / fact;
316        num *= - x2;
317        fact *= (2 * i + 2) * (2 * i + 3);
318    }
319#endif
320    return ret;
321}
322
323double sqrt(double x)
324{
325    double ret = x;
326    int i;
327
328    /* This is Newton's method */
329    for(i = 0; i < 10; i++)
330        ret = (ret * ret + x) / (ret * 2.0);
331
332    return ret;
333}
334
335
336/* XXX FIXME Converts only from little endian to big endian (x86) */
337unsigned int htonl(unsigned int hostlong)
338{
339    return ((hostlong&0xFFFF0000)>>16)|((hostlong&0x0000FFFFFF)<<16);
340}
341
342/* XXX FIXME Converts only from little endian to big endian (x86) */
343unsigned short htons(unsigned short hostlong)
344{
345    return ((hostlong&0xFF00)>>8)|((hostlong&0x00FF)<<8);
346}
347
348
349
350#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.