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

Last change on this file since 608 was 608, checked in by Jean-Yves Lamoureux, 15 years ago
  • Fixed UPPER() for strcasecmp
  • Property svn:keywords set to Id
File size: 4.9 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 608 2006-03-14 19:01:19Z jylam $
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    /* Either *s1 or *s2 is 0 */
147    return (int)UPPER(*s1) - (int)UPPER(*s2);
148}
149
150/* stdarg.h functions */
151int vsnprintf(char *str, size_t size, const char *format, va_list ap)
152{
153    /* FIXME */
154    return 0;
155}
156
157/* stdio.h functions */
158FILE *fopen(const char *path, const char *mode)
159{
160    /* FIXME */
161    return NULL;
162}
163
164int feof(FILE *stream)
165{
166    /* FIXME */
167    return 0;
168}
169
170char *fgets(char *s, int size, FILE *stream)
171{
172    /* FIXME */
173    return NULL;
174}
175
176int fclose(FILE *fp)
177{
178    /* FIXME */
179    return 0;
180}
181
182int printf(const char *format, ...)
183{
184    /* FIXME */
185    return 0;
186}
187
188int sprintf(char *str, const char *format, ...)
189{
190    /* FIXME */
191    return 0;
192}
193
194int sscanf(const char *str, const char *format, ...)
195{
196    /* FIXME */
197    return 0;
198}
199
200/* unistd.h functions */
201void usleep(unsigned long usec)
202{
203    /* FIXME */
204    return;
205}
206
207/* time.h functions */
208int gettimeofday(struct timeval *tv, struct timezone *tz)
209{
210    static int usec = 0;
211    static int sec = 0;
212
213    /* FIXME */
214    usec += 10000;
215    if(usec > 1000000)
216    {
217        sec++;
218        usec -= 1000000;
219    }
220
221    tv->tv_sec = sec;
222    tv->tv_usec = usec;
223
224    return 0;
225}
226
227/* math.h functions */
228double cos(double x)
229{
230    double ret = 0.0;
231    double x2;
232    double num = 1.0;
233    double fact = 1.0;
234    int i;
235
236    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
237    x2 = x * x;
238
239    /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
240    for(i = 0; i < 10; i++)
241    {
242        ret += num / fact;
243        num *= - x2;
244        fact *= (2 * i + 1) * (2 * i + 2);
245    }
246
247    return ret;
248}
249
250double sin(double x)
251{
252    double ret = 0.0;
253    double x2;
254    double num;
255    double fact = 1.0;
256    int i;
257
258    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
259    x2 = x * x;
260    num = x;
261
262    /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
263    for(i = 0; i < 10; i++)
264    {
265        ret += num / fact;
266        num *= - x2;
267        fact *= (2 * i + 2) * (2 * i + 3);
268    }
269
270    return ret;
271}
272
273double sqrt(double x)
274{
275    double ret = x;
276    int i;
277
278    /* This is Newton's method */
279    for(i = 0; i < 10; i++)
280        ret = (ret * ret + x) / (ret * 2.0);
281
282    return (double)ret;
283}
284
285#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.