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

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