Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

kernel.c 6.6 KiB

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