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

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