25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

389 lines
6.3 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. /* string.h functions */
  113. void *memset(void *s, int c, size_t n)
  114. {
  115. uint8_t *ptr = s;
  116. while(n--)
  117. *ptr++ = c;
  118. return s;
  119. }
  120. void *memcpy(void *dest, const void *src, size_t n)
  121. {
  122. uint8_t *destptr = dest;
  123. uint8_t const *srcptr = src;
  124. while(n--)
  125. *destptr++ = *srcptr++;
  126. return dest;
  127. }
  128. size_t strlen(const char *s)
  129. {
  130. int len = 0;
  131. while(*s++)
  132. len++;
  133. return len;
  134. }
  135. int strcmp(const char *s1, const char *s2)
  136. {
  137. while(*s1 && *s1 == *s2)
  138. {
  139. s1++;
  140. s2++;
  141. }
  142. return (int)*s1 - (int)*s2;
  143. }
  144. int strcasecmp(const char *s1, const char *s2)
  145. {
  146. while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
  147. {
  148. s1++;
  149. s2++;
  150. }
  151. return (int)UPPER(*s1) - (int)UPPER(*s2);
  152. }
  153. int memcmp(const void *_s1, const void *_s2, size_t n)
  154. {
  155. uint8_t const *s1 = _s1, *s2 = _s2;
  156. while(n--)
  157. {
  158. if(*s1 != *s2)
  159. return (int)*s1 - (int)*s2;
  160. s1++;
  161. s2++;
  162. }
  163. return 0;
  164. }
  165. char *strdup(const char *s)
  166. {
  167. char *new;
  168. unsigned int len = strlen(s);
  169. new = malloc(len + 1);
  170. memcpy(new, s, len + 1);
  171. return new;
  172. }
  173. char *strchr(const char *s, int c)
  174. {
  175. do
  176. if(*s == c)
  177. return (char *)(intptr_t)s;
  178. while(*s++);
  179. return NULL;
  180. }
  181. /* stdarg.h functions */
  182. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  183. {
  184. /* FIXME */
  185. return 0;
  186. }
  187. /* stdio.h functions */
  188. FILE *fopen(const char *path, const char *mode)
  189. {
  190. /* FIXME */
  191. return NULL;
  192. }
  193. int feof(FILE *stream)
  194. {
  195. /* FIXME */
  196. return 0;
  197. }
  198. char *fgets(char *s, int size, FILE *stream)
  199. {
  200. /* FIXME */
  201. return NULL;
  202. }
  203. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  204. {
  205. return 0;
  206. }
  207. int fclose(FILE *fp)
  208. {
  209. /* FIXME */
  210. return 0;
  211. }
  212. int printf(const char *format, ...)
  213. {
  214. /* FIXME */
  215. return 0;
  216. }
  217. int fprintf(FILE *stream, const char *format, ...)
  218. {
  219. /* FIXME */
  220. return 0;
  221. }
  222. int fflush(FILE *stream)
  223. {
  224. /* FIXME */
  225. return 0;
  226. }
  227. int sprintf(char *str, const char *format, ...)
  228. {
  229. /* FIXME */
  230. return 0;
  231. }
  232. int sscanf(const char *str, const char *format, ...)
  233. {
  234. /* FIXME */
  235. return 0;
  236. }
  237. /* unistd.h functions */
  238. void usleep(unsigned long usec)
  239. {
  240. /* FIXME */
  241. return;
  242. }
  243. /* time.h functions */
  244. int gettimeofday(struct timeval *tv, struct timezone *tz)
  245. {
  246. static int usec = 0;
  247. static int sec = 0;
  248. /* FIXME */
  249. usec += 10000;
  250. if(usec > 1000000)
  251. {
  252. sec++;
  253. usec -= 1000000;
  254. }
  255. tv->tv_sec = sec;
  256. tv->tv_usec = usec;
  257. return 0;
  258. }
  259. /* math.h functions */
  260. double cos(double x)
  261. {
  262. double ret = 0.0;
  263. #ifdef HAVE_FSIN_FCOS
  264. asm volatile("fcos" : "=t" (ret) : "0" (x));
  265. #else
  266. double x2;
  267. double num = 1.0;
  268. double fact = 1.0;
  269. int i;
  270. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  271. x2 = x * x;
  272. /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
  273. for(i = 0; i < 10; i++)
  274. {
  275. ret += num / fact;
  276. num *= - x2;
  277. fact *= (2 * i + 1) * (2 * i + 2);
  278. }
  279. #endif
  280. return ret;
  281. }
  282. double sin(double x)
  283. {
  284. double ret = 0.0;
  285. #ifdef HAVE_FSIN_FCOS
  286. asm volatile("fsin" : "=t" (ret) : "0" (x));
  287. #else
  288. double x2;
  289. double num;
  290. double fact = 1.0;
  291. int i;
  292. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  293. x2 = x * x;
  294. num = x;
  295. /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
  296. for(i = 0; i < 10; i++)
  297. {
  298. ret += num / fact;
  299. num *= - x2;
  300. fact *= (2 * i + 2) * (2 * i + 3);
  301. }
  302. #endif
  303. return ret;
  304. }
  305. double sqrt(double x)
  306. {
  307. double ret = x;
  308. int i;
  309. /* This is Newton's method */
  310. for(i = 0; i < 10; i++)
  311. ret = (ret * ret + x) / (ret * 2.0);
  312. return ret;
  313. }
  314. /* errno.h stuff */
  315. int errno = 0;
  316. #endif /* __KERNEL__ */