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

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