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.
 
 
 
 
 
 

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