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.
 
 
 
 
 
 

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