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

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