Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

kernel.c 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 strcasecmp(const char *s1, const char *s2)
  135. {
  136. while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
  137. {
  138. s1++;
  139. s2++;
  140. }
  141. return (int)UPPER(*s1) - (int)UPPER(*s2);
  142. }
  143. int memcmp(const void *_s1, const void *_s2, size_t n)
  144. {
  145. unsigned char const *s1 = _s1, *s2 = _s2;
  146. while(n--)
  147. {
  148. if(*s1 != *s2)
  149. return (int)*s1 - (int)*s2;
  150. s1++;
  151. s2++;
  152. }
  153. return 0;
  154. }
  155. /* stdarg.h functions */
  156. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  157. {
  158. /* FIXME */
  159. return 0;
  160. }
  161. /* stdio.h functions */
  162. FILE *fopen(const char *path, const char *mode)
  163. {
  164. /* FIXME */
  165. return NULL;
  166. }
  167. int feof(FILE *stream)
  168. {
  169. /* FIXME */
  170. return 0;
  171. }
  172. char *fgets(char *s, int size, FILE *stream)
  173. {
  174. /* FIXME */
  175. return NULL;
  176. }
  177. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  178. {
  179. return 0;
  180. }
  181. int fclose(FILE *fp)
  182. {
  183. /* FIXME */
  184. return 0;
  185. }
  186. int printf(const char *format, ...)
  187. {
  188. /* FIXME */
  189. return 0;
  190. }
  191. int fprintf(FILE *stream, const char *format, ...)
  192. {
  193. /* FIXME */
  194. return 0;
  195. }
  196. int sprintf(char *str, const char *format, ...)
  197. {
  198. /* FIXME */
  199. return 0;
  200. }
  201. int sscanf(const char *str, const char *format, ...)
  202. {
  203. /* FIXME */
  204. return 0;
  205. }
  206. /* unistd.h functions */
  207. void usleep(unsigned long usec)
  208. {
  209. /* FIXME */
  210. return;
  211. }
  212. /* time.h functions */
  213. int gettimeofday(struct timeval *tv, struct timezone *tz)
  214. {
  215. static int usec = 0;
  216. static int sec = 0;
  217. /* FIXME */
  218. usec += 10000;
  219. if(usec > 1000000)
  220. {
  221. sec++;
  222. usec -= 1000000;
  223. }
  224. tv->tv_sec = sec;
  225. tv->tv_usec = usec;
  226. return 0;
  227. }
  228. /* math.h functions */
  229. double cos(double x)
  230. {
  231. double ret = 0.0;
  232. #ifdef HAVE_FSIN_FCOS
  233. asm volatile("fcos" : "=t" (ret) : "0" (x));
  234. #else
  235. double x2;
  236. double num = 1.0;
  237. double fact = 1.0;
  238. int i;
  239. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  240. x2 = x * x;
  241. /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
  242. for(i = 0; i < 10; i++)
  243. {
  244. ret += num / fact;
  245. num *= - x2;
  246. fact *= (2 * i + 1) * (2 * i + 2);
  247. }
  248. #endif
  249. return ret;
  250. }
  251. double sin(double x)
  252. {
  253. double ret = 0.0;
  254. #ifdef HAVE_FSIN_FCOS
  255. asm volatile("fsin" : "=t" (ret) : "0" (x));
  256. #else
  257. double x2;
  258. double num;
  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. num = x;
  264. /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
  265. for(i = 0; i < 10; i++)
  266. {
  267. ret += num / fact;
  268. num *= - x2;
  269. fact *= (2 * i + 2) * (2 * i + 3);
  270. }
  271. #endif
  272. return ret;
  273. }
  274. double sqrt(double x)
  275. {
  276. double ret = x;
  277. int i;
  278. /* This is Newton's method */
  279. for(i = 0; i < 10; i++)
  280. ret = (ret * ret + x) / (ret * 2.0);
  281. return ret;
  282. }
  283. /* errno.h stuff */
  284. int errno = 0;
  285. /* arpa/inet.h functions */
  286. /* XXX FIXME Converts only from little endian to big endian (x86) */
  287. unsigned int htonl(unsigned int hostlong)
  288. {
  289. return ((hostlong&0xFFFF0000)>>16)|((hostlong&0x0000FFFFFF)<<16);
  290. }
  291. /* XXX FIXME Converts only from little endian to big endian (x86) */
  292. unsigned short htons(unsigned short hostlong)
  293. {
  294. return ((hostlong&0xFF00)>>8)|((hostlong&0x00FF)<<8);
  295. }
  296. #endif /* __KERNEL__ */