You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

390 lines
6.5 KiB

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