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 2144 2007-12-16 14:29:19Z sam $ |
---|
9 | * |
---|
10 | * This library is free software. It comes 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 */ |
---|
35 | static int seed = 0x68743284; |
---|
36 | |
---|
37 | /* Our memory mapping */ |
---|
38 | static uint32_t *freemem = (uint32_t*) 0x00200000; |
---|
39 | |
---|
40 | /* Multiboot kernel entry point */ |
---|
41 | void 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 */ |
---|
59 | void *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 | |
---|
70 | void free(void *ptr) |
---|
71 | { |
---|
72 | return; |
---|
73 | } |
---|
74 | |
---|
75 | void *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 | |
---|
97 | char *getenv(const char *name) |
---|
98 | { |
---|
99 | return NULL; |
---|
100 | } |
---|
101 | |
---|
102 | int getpid(void) |
---|
103 | { |
---|
104 | return 0x1337; |
---|
105 | } |
---|
106 | |
---|
107 | void srand(unsigned int s) |
---|
108 | { |
---|
109 | seed = rand(); |
---|
110 | } |
---|
111 | |
---|
112 | int time(void *dummy) |
---|
113 | { |
---|
114 | return rand(); |
---|
115 | } |
---|
116 | |
---|
117 | int rand(void) |
---|
118 | { |
---|
119 | seed = (seed * 0x7f32ba17) ^ 0xf893a735; |
---|
120 | return seed % RAND_MAX; |
---|
121 | } |
---|
122 | |
---|
123 | int abs(int j) |
---|
124 | { |
---|
125 | if(j < 0) |
---|
126 | return -j; |
---|
127 | return j; |
---|
128 | } |
---|
129 | |
---|
130 | void exit(int status) |
---|
131 | { |
---|
132 | /* FIXME: reboot? */ |
---|
133 | while(1); |
---|
134 | } |
---|
135 | |
---|
136 | /* string.h functions */ |
---|
137 | void *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 | |
---|
147 | void *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 | |
---|
158 | size_t strlen(const char *s) |
---|
159 | { |
---|
160 | int len = 0; |
---|
161 | |
---|
162 | while(*s++) |
---|
163 | len++; |
---|
164 | |
---|
165 | return len; |
---|
166 | } |
---|
167 | |
---|
168 | int 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 | |
---|
179 | int 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 | |
---|
190 | int 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 | |
---|
204 | char *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 | char *strchr(const char *s, int c) |
---|
216 | { |
---|
217 | do |
---|
218 | if(*s == c) |
---|
219 | return (char *)(intptr_t)s; |
---|
220 | while(*s++); |
---|
221 | |
---|
222 | return NULL; |
---|
223 | } |
---|
224 | |
---|
225 | /* stdarg.h functions */ |
---|
226 | int vsnprintf(char *str, size_t size, const char *format, va_list ap) |
---|
227 | { |
---|
228 | /* FIXME */ |
---|
229 | return 0; |
---|
230 | } |
---|
231 | |
---|
232 | /* stdio.h functions */ |
---|
233 | FILE *fopen(const char *path, const char *mode) |
---|
234 | { |
---|
235 | /* FIXME */ |
---|
236 | return NULL; |
---|
237 | } |
---|
238 | |
---|
239 | int feof(FILE *stream) |
---|
240 | { |
---|
241 | /* FIXME */ |
---|
242 | return 0; |
---|
243 | } |
---|
244 | |
---|
245 | char *fgets(char *s, int size, FILE *stream) |
---|
246 | { |
---|
247 | /* FIXME */ |
---|
248 | return NULL; |
---|
249 | } |
---|
250 | |
---|
251 | size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
---|
252 | { |
---|
253 | return 0; |
---|
254 | } |
---|
255 | |
---|
256 | int fclose(FILE *fp) |
---|
257 | { |
---|
258 | /* FIXME */ |
---|
259 | return 0; |
---|
260 | } |
---|
261 | |
---|
262 | int printf(const char *format, ...) |
---|
263 | { |
---|
264 | /* FIXME */ |
---|
265 | return 0; |
---|
266 | } |
---|
267 | |
---|
268 | int fprintf(FILE *stream, const char *format, ...) |
---|
269 | { |
---|
270 | /* FIXME */ |
---|
271 | return 0; |
---|
272 | } |
---|
273 | |
---|
274 | int fflush(FILE *stream) |
---|
275 | { |
---|
276 | /* FIXME */ |
---|
277 | return 0; |
---|
278 | } |
---|
279 | |
---|
280 | int sprintf(char *str, const char *format, ...) |
---|
281 | { |
---|
282 | /* FIXME */ |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|
286 | int sscanf(const char *str, const char *format, ...) |
---|
287 | { |
---|
288 | /* FIXME */ |
---|
289 | return 0; |
---|
290 | } |
---|
291 | |
---|
292 | /* unistd.h functions */ |
---|
293 | void usleep(unsigned long usec) |
---|
294 | { |
---|
295 | /* FIXME */ |
---|
296 | return; |
---|
297 | } |
---|
298 | |
---|
299 | /* time.h functions */ |
---|
300 | int gettimeofday(struct timeval *tv, struct timezone *tz) |
---|
301 | { |
---|
302 | static int usec = 0; |
---|
303 | static int sec = 0; |
---|
304 | |
---|
305 | /* FIXME */ |
---|
306 | usec += 10000; |
---|
307 | if(usec > 1000000) |
---|
308 | { |
---|
309 | sec++; |
---|
310 | usec -= 1000000; |
---|
311 | } |
---|
312 | |
---|
313 | tv->tv_sec = sec; |
---|
314 | tv->tv_usec = usec; |
---|
315 | |
---|
316 | return 0; |
---|
317 | } |
---|
318 | |
---|
319 | /* math.h functions */ |
---|
320 | double cos(double x) |
---|
321 | { |
---|
322 | double ret = 0.0; |
---|
323 | #ifdef HAVE_FSIN_FCOS |
---|
324 | asm volatile("fcos" : "=t" (ret) : "0" (x)); |
---|
325 | #else |
---|
326 | double x2; |
---|
327 | double num = 1.0; |
---|
328 | double fact = 1.0; |
---|
329 | int i; |
---|
330 | |
---|
331 | x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI); |
---|
332 | x2 = x * x; |
---|
333 | |
---|
334 | /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */ |
---|
335 | for(i = 0; i < 10; i++) |
---|
336 | { |
---|
337 | ret += num / fact; |
---|
338 | num *= - x2; |
---|
339 | fact *= (2 * i + 1) * (2 * i + 2); |
---|
340 | } |
---|
341 | #endif |
---|
342 | return ret; |
---|
343 | } |
---|
344 | |
---|
345 | double sin(double x) |
---|
346 | { |
---|
347 | double ret = 0.0; |
---|
348 | #ifdef HAVE_FSIN_FCOS |
---|
349 | asm volatile("fsin" : "=t" (ret) : "0" (x)); |
---|
350 | #else |
---|
351 | double x2; |
---|
352 | double num; |
---|
353 | double fact = 1.0; |
---|
354 | int i; |
---|
355 | |
---|
356 | x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI); |
---|
357 | x2 = x * x; |
---|
358 | num = x; |
---|
359 | |
---|
360 | /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */ |
---|
361 | for(i = 0; i < 10; i++) |
---|
362 | { |
---|
363 | ret += num / fact; |
---|
364 | num *= - x2; |
---|
365 | fact *= (2 * i + 2) * (2 * i + 3); |
---|
366 | } |
---|
367 | #endif |
---|
368 | return ret; |
---|
369 | } |
---|
370 | |
---|
371 | double sqrt(double x) |
---|
372 | { |
---|
373 | double ret = x; |
---|
374 | int i; |
---|
375 | |
---|
376 | /* This is Newton's method */ |
---|
377 | for(i = 0; i < 10; i++) |
---|
378 | ret = (ret * ret + x) / (ret * 2.0); |
---|
379 | |
---|
380 | return ret; |
---|
381 | } |
---|
382 | |
---|
383 | /* errno.h stuff */ |
---|
384 | int errno = 0; |
---|
385 | |
---|
386 | /* arpa/inet.h functions */ |
---|
387 | |
---|
388 | /* XXX FIXME Converts only from little endian to big endian (x86) */ |
---|
389 | unsigned int htonl(unsigned int hostlong) |
---|
390 | { |
---|
391 | return ((hostlong&0xFFFF0000)>>16)|((hostlong&0x0000FFFFFF)<<16); |
---|
392 | } |
---|
393 | |
---|
394 | /* XXX FIXME Converts only from little endian to big endian (x86) */ |
---|
395 | unsigned short htons(unsigned short hostlong) |
---|
396 | { |
---|
397 | return ((hostlong&0xFF00)>>8)|((hostlong&0x00FF)<<8); |
---|
398 | } |
---|
399 | |
---|
400 | #endif /* __KERNEL__ */ |
---|