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 | * All Rights Reserved |
---|
6 | * |
---|
7 | * $Id: kernel.c 769 2006-04-14 07:30:53Z sam $ |
---|
8 | * |
---|
9 | * This library is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the Do What The Fuck You Want To |
---|
11 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * This file contains replacement functions for the standard C library |
---|
17 | * that must be used when building libcucul and libcaca into a kernel. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #ifdef __KERNEL__ |
---|
23 | |
---|
24 | #define IS_DIGIT(x) (x>='0' && x<='9') |
---|
25 | #define IS_ALPHA(x) (x>='A' && x<='z') |
---|
26 | #define IS_UPPER(x) (x>='A' && x<='Z') |
---|
27 | #define IS_LOWER(x) (x>='a' && x<='z') |
---|
28 | #define UPPER(x) (IS_LOWER(x)?(x+('A'-'a')):x) |
---|
29 | #define LOWER(x) (IS_UPPER(x)?(x-('a'-'A')):x) |
---|
30 | |
---|
31 | /* Our memory mapping */ |
---|
32 | static uint32_t *freemem = (uint32_t*) 0x00200000; |
---|
33 | |
---|
34 | /* Multiboot kernel entry point */ |
---|
35 | void cmain(unsigned long int magic, unsigned long int addr) |
---|
36 | { |
---|
37 | static char const text[] = "Booting libcaca kernel..."; |
---|
38 | char const *ptr = text; |
---|
39 | char *video = (char*)0xB8000; |
---|
40 | |
---|
41 | char *argv[] = { NULL }; |
---|
42 | int argc = 0; |
---|
43 | |
---|
44 | /* Print a text message to say hello */ |
---|
45 | while(*ptr) |
---|
46 | *video = *ptr++; video += 2; |
---|
47 | |
---|
48 | /* Launch the main program */ |
---|
49 | main(argc, argv); |
---|
50 | } |
---|
51 | |
---|
52 | /* stdlib.h functions */ |
---|
53 | void *malloc(size_t size) |
---|
54 | { |
---|
55 | uint32_t *p = freemem; |
---|
56 | if(!size) |
---|
57 | return NULL; |
---|
58 | size = (size + 0x7) / 4; |
---|
59 | *p = size; |
---|
60 | freemem += size + 1; |
---|
61 | return p + 1; |
---|
62 | } |
---|
63 | |
---|
64 | void free(void *ptr) |
---|
65 | { |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | void *realloc(void *ptr, size_t size) |
---|
70 | { |
---|
71 | uint32_t oldsize; |
---|
72 | void *p; |
---|
73 | |
---|
74 | if(!size) |
---|
75 | return NULL; |
---|
76 | |
---|
77 | if(!ptr) |
---|
78 | oldsize = 0; |
---|
79 | else |
---|
80 | { |
---|
81 | oldsize = ((uint32_t *)ptr)[-1]; |
---|
82 | if(oldsize >= size) |
---|
83 | return ptr; |
---|
84 | } |
---|
85 | |
---|
86 | p = malloc(size); |
---|
87 | memcpy(p, ptr, oldsize); |
---|
88 | return p; |
---|
89 | } |
---|
90 | |
---|
91 | char *getenv(const char *name) |
---|
92 | { |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | int rand(void) |
---|
97 | { |
---|
98 | static int seed = 0x68743284; |
---|
99 | seed = (seed * 0x7f32ba17) ^ 0xf893a735; |
---|
100 | return seed % RAND_MAX; |
---|
101 | } |
---|
102 | |
---|
103 | int abs(int j) |
---|
104 | { |
---|
105 | if(j < 0) |
---|
106 | return -j; |
---|
107 | return j; |
---|
108 | } |
---|
109 | |
---|
110 | void exit(int status) |
---|
111 | { |
---|
112 | /* FIXME: reboot? */ |
---|
113 | while(1); |
---|
114 | } |
---|
115 | |
---|
116 | /* string.h functions */ |
---|
117 | void *memset(void *s, int c, size_t n) |
---|
118 | { |
---|
119 | uint8_t *ptr = s; |
---|
120 | |
---|
121 | while(n--) |
---|
122 | *ptr++ = c; |
---|
123 | |
---|
124 | return s; |
---|
125 | } |
---|
126 | |
---|
127 | void *memcpy(void *dest, const void *src, size_t n) |
---|
128 | { |
---|
129 | uint8_t *destptr = dest; |
---|
130 | uint8_t const *srcptr = src; |
---|
131 | |
---|
132 | while(n--) |
---|
133 | *destptr++ = *srcptr++; |
---|
134 | |
---|
135 | return dest; |
---|
136 | } |
---|
137 | |
---|
138 | size_t strlen(const char *s) |
---|
139 | { |
---|
140 | int len = 0; |
---|
141 | |
---|
142 | while(*s++) |
---|
143 | len++; |
---|
144 | |
---|
145 | return len; |
---|
146 | } |
---|
147 | |
---|
148 | int strcasecmp(const char *s1, const char *s2) |
---|
149 | { |
---|
150 | while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2)) |
---|
151 | { |
---|
152 | s1++; |
---|
153 | s2++; |
---|
154 | } |
---|
155 | |
---|
156 | return (int)UPPER(*s1) - (int)UPPER(*s2); |
---|
157 | } |
---|
158 | |
---|
159 | /* stdarg.h functions */ |
---|
160 | int vsnprintf(char *str, size_t size, const char *format, va_list ap) |
---|
161 | { |
---|
162 | /* FIXME */ |
---|
163 | return 0; |
---|
164 | } |
---|
165 | |
---|
166 | /* stdio.h functions */ |
---|
167 | FILE *fopen(const char *path, const char *mode) |
---|
168 | { |
---|
169 | /* FIXME */ |
---|
170 | return NULL; |
---|
171 | } |
---|
172 | |
---|
173 | int feof(FILE *stream) |
---|
174 | { |
---|
175 | /* FIXME */ |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|
179 | char *fgets(char *s, int size, FILE *stream) |
---|
180 | { |
---|
181 | /* FIXME */ |
---|
182 | return NULL; |
---|
183 | } |
---|
184 | |
---|
185 | int fclose(FILE *fp) |
---|
186 | { |
---|
187 | /* FIXME */ |
---|
188 | return 0; |
---|
189 | } |
---|
190 | |
---|
191 | int printf(const char *format, ...) |
---|
192 | { |
---|
193 | /* FIXME */ |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | int sprintf(char *str, const char *format, ...) |
---|
198 | { |
---|
199 | /* FIXME */ |
---|
200 | return 0; |
---|
201 | } |
---|
202 | |
---|
203 | int sscanf(const char *str, const char *format, ...) |
---|
204 | { |
---|
205 | /* FIXME */ |
---|
206 | return 0; |
---|
207 | } |
---|
208 | |
---|
209 | /* unistd.h functions */ |
---|
210 | void usleep(unsigned long usec) |
---|
211 | { |
---|
212 | /* FIXME */ |
---|
213 | return; |
---|
214 | } |
---|
215 | |
---|
216 | /* time.h functions */ |
---|
217 | int gettimeofday(struct timeval *tv, struct timezone *tz) |
---|
218 | { |
---|
219 | static int usec = 0; |
---|
220 | static int sec = 0; |
---|
221 | |
---|
222 | /* FIXME */ |
---|
223 | usec += 10000; |
---|
224 | if(usec > 1000000) |
---|
225 | { |
---|
226 | sec++; |
---|
227 | usec -= 1000000; |
---|
228 | } |
---|
229 | |
---|
230 | tv->tv_sec = sec; |
---|
231 | tv->tv_usec = usec; |
---|
232 | |
---|
233 | return 0; |
---|
234 | } |
---|
235 | |
---|
236 | /* math.h functions */ |
---|
237 | double cos(double x) |
---|
238 | { |
---|
239 | double ret = 0.0; |
---|
240 | #ifdef HAVE_FSIN_FCOS |
---|
241 | asm volatile("fcos" : "=t" (ret) : "0" (x)); |
---|
242 | #else |
---|
243 | double x2; |
---|
244 | double num = 1.0; |
---|
245 | double fact = 1.0; |
---|
246 | int i; |
---|
247 | |
---|
248 | x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI); |
---|
249 | x2 = x * x; |
---|
250 | |
---|
251 | /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */ |
---|
252 | for(i = 0; i < 10; i++) |
---|
253 | { |
---|
254 | ret += num / fact; |
---|
255 | num *= - x2; |
---|
256 | fact *= (2 * i + 1) * (2 * i + 2); |
---|
257 | } |
---|
258 | #endif |
---|
259 | return ret; |
---|
260 | } |
---|
261 | |
---|
262 | double sin(double x) |
---|
263 | { |
---|
264 | double ret = 0.0; |
---|
265 | #ifdef HAVE_FSIN_FCOS |
---|
266 | asm volatile("fsin" : "=t" (ret) : "0" (x)); |
---|
267 | #else |
---|
268 | double x2; |
---|
269 | double num; |
---|
270 | double fact = 1.0; |
---|
271 | int i; |
---|
272 | |
---|
273 | x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI); |
---|
274 | x2 = x * x; |
---|
275 | num = x; |
---|
276 | |
---|
277 | /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */ |
---|
278 | for(i = 0; i < 10; i++) |
---|
279 | { |
---|
280 | ret += num / fact; |
---|
281 | num *= - x2; |
---|
282 | fact *= (2 * i + 2) * (2 * i + 3); |
---|
283 | } |
---|
284 | #endif |
---|
285 | return ret; |
---|
286 | } |
---|
287 | |
---|
288 | double sqrt(double x) |
---|
289 | { |
---|
290 | double ret = x; |
---|
291 | int i; |
---|
292 | |
---|
293 | /* This is Newton's method */ |
---|
294 | for(i = 0; i < 10; i++) |
---|
295 | ret = (ret * ret + x) / (ret * 2.0); |
---|
296 | |
---|
297 | return (double)ret; |
---|
298 | } |
---|
299 | |
---|
300 | #endif /* __KERNEL__ */ |
---|