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

Last change on this file since 1065 was 1065, checked in by Sam Hocevar, 14 years ago
  • Added an fprintf stub to the kernel.
  • Property svn:keywords set to Id
File size: 6.2 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 1065 2006-09-19 08:26: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 fprintf(FILE *stream, const char *format, ...)
236{
237    /* FIXME */
238    return 0;
239}
240
241int sprintf(char *str, const char *format, ...)
242{
243    /* FIXME */
244    return 0;
245}
246
247int sscanf(const char *str, const char *format, ...)
248{
249    /* FIXME */
250    return 0;
251}
252
253/* unistd.h functions */
254void usleep(unsigned long usec)
255{
256    /* FIXME */
257    return;
258}
259
260/* time.h functions */
261int gettimeofday(struct timeval *tv, struct timezone *tz)
262{
263    static int usec = 0;
264    static int sec = 0;
265
266    /* FIXME */
267    usec += 10000;
268    if(usec > 1000000)
269    {
270        sec++;
271        usec -= 1000000;
272    }
273
274    tv->tv_sec = sec;
275    tv->tv_usec = usec;
276
277    return 0;
278}
279
280/* math.h functions */
281double cos(double x)
282{
283    double ret = 0.0;
284#ifdef HAVE_FSIN_FCOS
285    asm volatile("fcos" : "=t" (ret) : "0" (x));
286#else
287    double x2;
288    double num = 1.0;
289    double fact = 1.0;
290    int i;
291
292    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
293    x2 = x * x;
294
295    /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
296    for(i = 0; i < 10; i++)
297    {
298        ret += num / fact;
299        num *= - x2;
300        fact *= (2 * i + 1) * (2 * i + 2);
301    }
302#endif
303    return ret;
304}
305
306double sin(double x)
307{
308    double ret = 0.0;
309#ifdef HAVE_FSIN_FCOS
310    asm volatile("fsin" : "=t" (ret) : "0" (x));
311#else
312    double x2;
313    double num;
314    double fact = 1.0;
315    int i;
316
317    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
318    x2 = x * x;
319    num = x;
320
321    /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
322    for(i = 0; i < 10; i++)
323    {
324        ret += num / fact;
325        num *= - x2;
326        fact *= (2 * i + 2) * (2 * i + 3);
327    }
328#endif
329    return ret;
330}
331
332double sqrt(double x)
333{
334    double ret = x;
335    int i;
336
337    /* This is Newton's method */
338    for(i = 0; i < 10; i++)
339        ret = (ret * ret + x) / (ret * 2.0);
340
341    return ret;
342}
343
344/* errno.h stuff */
345int errno = 0;
346
347/* arpa/inet.h functions */
348
349/* XXX FIXME Converts only from little endian to big endian (x86) */
350unsigned int htonl(unsigned int hostlong)
351{
352    return ((hostlong&0xFFFF0000)>>16)|((hostlong&0x0000FFFFFF)<<16);
353}
354
355/* XXX FIXME Converts only from little endian to big endian (x86) */
356unsigned short htons(unsigned short hostlong)
357{
358    return ((hostlong&0xFF00)>>8)|((hostlong&0x00FF)<<8);
359}
360
361#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.