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

Last change on this file since 652 was 652, checked in by Sam Hocevar, 15 years ago
  • Use the asm versions of sin and cos when available.
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
3 *  libcaca       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 652 2006-03-21 09:48:02Z 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    size = (size + 0x7) / 4;
59    *p = size;
60    freemem += size + 1;
61    return p + 1;
62}
63
64void free(void *ptr)
65{
66    return;
67}
68
69void *realloc(void *ptr, size_t size)
70{
71   uint32_t oldsize = ((uint32_t *)ptr)[-1];
72    void *p;
73    if(oldsize >= size)
74        return ptr;
75
76    p = malloc(size);
77    memcpy(p, ptr, oldsize);
78    return p;
79}
80
81char *getenv(const char *name)
82{
83    return NULL;
84}
85
86int rand(void)
87{
88    static int seed = 0x68743284;
89    seed = (seed * 0x7f32ba17) ^ 0xf893a735;
90    return seed % RAND_MAX;
91}
92
93int abs(int j)
94{
95    if(j < 0)
96        return -j;
97    return j;
98}
99
100void exit(int status)
101{
102    /* FIXME: reboot? */
103    while(1);
104}
105
106/* string.h functions */
107void *memset(void *s, int c, size_t n)
108{
109    uint8_t *ptr = s;
110
111    while(n--)
112        *ptr++ = c;
113
114    return s;
115}
116
117void *memcpy(void *dest, const void *src, size_t n)
118{
119    uint8_t *destptr = dest;
120    uint8_t const *srcptr = src;
121
122    while(n--)
123        *destptr++ = *srcptr++;
124
125    return dest;
126}
127
128size_t strlen(const char *s)
129{
130    int len = 0;
131
132    while(*s++)
133        len++;
134
135    return len;
136}
137
138int strcasecmp(const char *s1, const char *s2)
139{
140    while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
141    {
142        s1++;
143        s2++;
144    }
145
146    return (int)UPPER(*s1) - (int)UPPER(*s2);
147}
148
149/* stdarg.h functions */
150int vsnprintf(char *str, size_t size, const char *format, va_list ap)
151{
152    /* FIXME */
153    return 0;
154}
155
156/* stdio.h functions */
157FILE *fopen(const char *path, const char *mode)
158{
159    /* FIXME */
160    return NULL;
161}
162
163int feof(FILE *stream)
164{
165    /* FIXME */
166    return 0;
167}
168
169char *fgets(char *s, int size, FILE *stream)
170{
171    /* FIXME */
172    return NULL;
173}
174
175int fclose(FILE *fp)
176{
177    /* FIXME */
178    return 0;
179}
180
181int printf(const char *format, ...)
182{
183    /* FIXME */
184    return 0;
185}
186
187int sprintf(char *str, const char *format, ...)
188{
189    /* FIXME */
190    return 0;
191}
192
193int sscanf(const char *str, const char *format, ...)
194{
195    /* FIXME */
196    return 0;
197}
198
199/* unistd.h functions */
200void usleep(unsigned long usec)
201{
202    /* FIXME */
203    return;
204}
205
206/* time.h functions */
207int gettimeofday(struct timeval *tv, struct timezone *tz)
208{
209    static int usec = 0;
210    static int sec = 0;
211
212    /* FIXME */
213    usec += 10000;
214    if(usec > 1000000)
215    {
216        sec++;
217        usec -= 1000000;
218    }
219
220    tv->tv_sec = sec;
221    tv->tv_usec = usec;
222
223    return 0;
224}
225
226/* math.h functions */
227double cos(double x)
228{
229    double ret = 0.0;
230#ifdef HAVE_FSIN_FCOS
231    asm volatile("fcos" : "=t" (ret) : "0" (x));
232#else
233    double x2;
234    double num = 1.0;
235    double fact = 1.0;
236    int i;
237
238    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
239    x2 = x * x;
240
241    /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
242    for(i = 0; i < 10; i++)
243    {
244        ret += num / fact;
245        num *= - x2;
246        fact *= (2 * i + 1) * (2 * i + 2);
247    }
248#endif
249    return ret;
250}
251
252double sin(double x)
253{
254    double ret = 0.0;
255#ifdef HAVE_FSIN_FCOS
256    asm volatile("fsin" : "=t" (ret) : "0" (x));
257#else
258    double x2;
259    double num;
260    double fact = 1.0;
261    int i;
262
263    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
264    x2 = x * x;
265    num = x;
266
267    /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
268    for(i = 0; i < 10; i++)
269    {
270        ret += num / fact;
271        num *= - x2;
272        fact *= (2 * i + 2) * (2 * i + 3);
273    }
274#endif
275    return ret;
276}
277
278double sqrt(double x)
279{
280    double ret = x;
281    int i;
282
283    /* This is Newton's method */
284    for(i = 0; i < 10; i++)
285        ret = (ret * ret + x) / (ret * 2.0);
286
287    return (double)ret;
288}
289
290#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.