No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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