Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

395 рядки
6.4 KiB

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