25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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