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.

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