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

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