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

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