選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

kernel.c 5.9 KiB

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