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

Last change on this file since 1415 was 1415, checked in by Sam Hocevar, 13 years ago
  • Implemented strcmp() and strdup() and added an fflush() stub to the kernel code.
  • Property svn:keywords set to Id
File size: 6.5 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 1415 2006-11-16 11:23:56Z 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 strcmp(const char *s1, const char *s2)
168{
169    while(*s1 && *s1 == *s2)
170    {
171        s1++;
172        s2++;
173    }
174
175    return (int)*s1 - (int)*s2;
176}
177
178int strcasecmp(const char *s1, const char *s2)
179{
180    while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
181    {
182        s1++;
183        s2++;
184    }
185
186    return (int)UPPER(*s1) - (int)UPPER(*s2);
187}
188
189int memcmp(const void *_s1, const void *_s2, size_t n)
190{
191    unsigned char const *s1 = _s1, *s2 = _s2;
192
193    while(n--)
194    {
195        if(*s1 != *s2)
196            return (int)*s1 - (int)*s2;
197        s1++;
198        s2++;
199    }
200    return 0;
201}
202
203char *strdup(const char *s)
204{
205    char *new;
206    unsigned int len = strlen(s);
207
208    new = malloc(len + 1);
209    memcpy(new, s, len + 1);
210
211    return new;
212}
213
214/* stdarg.h functions */
215int vsnprintf(char *str, size_t size, const char *format, va_list ap)
216{
217    /* FIXME */
218    return 0;
219}
220
221/* stdio.h functions */
222FILE *fopen(const char *path, const char *mode)
223{
224    /* FIXME */
225    return NULL;
226}
227
228int feof(FILE *stream)
229{
230    /* FIXME */
231    return 0;
232}
233
234char *fgets(char *s, int size, FILE *stream)
235{
236    /* FIXME */
237    return NULL;
238}
239
240size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
241{
242    return 0;
243}
244
245int fclose(FILE *fp)
246{
247    /* FIXME */
248    return 0;
249}
250
251int printf(const char *format, ...)
252{
253    /* FIXME */
254    return 0;
255}
256
257int fprintf(FILE *stream, const char *format, ...)
258{
259    /* FIXME */
260    return 0;
261}
262
263int fflush(FILE *stream)
264{
265    /* FIXME */
266    return 0;
267}
268
269int sprintf(char *str, const char *format, ...)
270{
271    /* FIXME */
272    return 0;
273}
274
275int sscanf(const char *str, const char *format, ...)
276{
277    /* FIXME */
278    return 0;
279}
280
281/* unistd.h functions */
282void usleep(unsigned long usec)
283{
284    /* FIXME */
285    return;
286}
287
288/* time.h functions */
289int gettimeofday(struct timeval *tv, struct timezone *tz)
290{
291    static int usec = 0;
292    static int sec = 0;
293
294    /* FIXME */
295    usec += 10000;
296    if(usec > 1000000)
297    {
298        sec++;
299        usec -= 1000000;
300    }
301
302    tv->tv_sec = sec;
303    tv->tv_usec = usec;
304
305    return 0;
306}
307
308/* math.h functions */
309double cos(double x)
310{
311    double ret = 0.0;
312#ifdef HAVE_FSIN_FCOS
313    asm volatile("fcos" : "=t" (ret) : "0" (x));
314#else
315    double x2;
316    double num = 1.0;
317    double fact = 1.0;
318    int i;
319
320    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
321    x2 = x * x;
322
323    /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
324    for(i = 0; i < 10; i++)
325    {
326        ret += num / fact;
327        num *= - x2;
328        fact *= (2 * i + 1) * (2 * i + 2);
329    }
330#endif
331    return ret;
332}
333
334double sin(double x)
335{
336    double ret = 0.0;
337#ifdef HAVE_FSIN_FCOS
338    asm volatile("fsin" : "=t" (ret) : "0" (x));
339#else
340    double x2;
341    double num;
342    double fact = 1.0;
343    int i;
344
345    x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
346    x2 = x * x;
347    num = x;
348
349    /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
350    for(i = 0; i < 10; i++)
351    {
352        ret += num / fact;
353        num *= - x2;
354        fact *= (2 * i + 2) * (2 * i + 3);
355    }
356#endif
357    return ret;
358}
359
360double sqrt(double x)
361{
362    double ret = x;
363    int i;
364
365    /* This is Newton's method */
366    for(i = 0; i < 10; i++)
367        ret = (ret * ret + x) / (ret * 2.0);
368
369    return ret;
370}
371
372/* errno.h stuff */
373int errno = 0;
374
375/* arpa/inet.h functions */
376
377/* XXX FIXME Converts only from little endian to big endian (x86) */
378unsigned int htonl(unsigned int hostlong)
379{
380    return ((hostlong&0xFFFF0000)>>16)|((hostlong&0x0000FFFFFF)<<16);
381}
382
383/* XXX FIXME Converts only from little endian to big endian (x86) */
384unsigned short htons(unsigned short hostlong)
385{
386    return ((hostlong&0xFF00)>>8)|((hostlong&0x00FF)<<8);
387}
388
389#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.