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