Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

19 роки тому
19 роки тому
19 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * libcucul Unicode canvas library
  3. * libcaca 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. size = (size + 0x7) / 4;
  49. *p = size;
  50. freemem += size + 1;
  51. return p + 1;
  52. }
  53. void free(void *ptr)
  54. {
  55. return;
  56. }
  57. void *realloc(void *ptr, size_t size)
  58. {
  59. uint32_t oldsize = ((uint32_t *)ptr)[-1];
  60. void *p;
  61. if(oldsize >= size)
  62. return ptr;
  63. p = malloc(size);
  64. memcpy(p, ptr, oldsize);
  65. return p;
  66. }
  67. char *getenv(const char *name)
  68. {
  69. return NULL;
  70. }
  71. int rand(void)
  72. {
  73. static int seed = 0x68743284;
  74. seed = (seed * 0x7f32ba17) ^ 0xf893a735;
  75. return seed % RAND_MAX;
  76. }
  77. int abs(int j)
  78. {
  79. if(j < 0)
  80. return -j;
  81. return j;
  82. }
  83. void exit(int status)
  84. {
  85. /* FIXME: reboot? */
  86. while(1);
  87. }
  88. /* string.h functions */
  89. void *memset(void *s, int c, size_t n)
  90. {
  91. uint8_t *ptr = s;
  92. while(n--)
  93. *ptr++ = c;
  94. return s;
  95. }
  96. void *memcpy(void *dest, const void *src, size_t n)
  97. {
  98. uint8_t *destptr = dest;
  99. uint8_t const *srcptr = src;
  100. while(n--)
  101. *destptr++ = *srcptr++;
  102. return dest;
  103. }
  104. size_t strlen(const char *s)
  105. {
  106. int len = 0;
  107. while(*s++)
  108. len++;
  109. return len;
  110. }
  111. int strcasecmp(const char *s1, const char *s2)
  112. {
  113. while(*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
  114. {
  115. s1++;
  116. s2++;
  117. }
  118. /* Either *s1 or *s2 is 0 */
  119. return (int)UPPER(*s1) - (int)UPPER(*s2);
  120. }
  121. /* stdarg.h functions */
  122. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  123. {
  124. /* FIXME */
  125. return 0;
  126. }
  127. /* stdio.h functions */
  128. FILE *fopen(const char *path, const char *mode)
  129. {
  130. /* FIXME */
  131. return NULL;
  132. }
  133. int feof(FILE *stream)
  134. {
  135. /* FIXME */
  136. return 0;
  137. }
  138. char *fgets(char *s, int size, FILE *stream)
  139. {
  140. /* FIXME */
  141. return NULL;
  142. }
  143. int fclose(FILE *fp)
  144. {
  145. /* FIXME */
  146. return 0;
  147. }
  148. int printf(const char *format, ...)
  149. {
  150. /* FIXME */
  151. return 0;
  152. }
  153. int sprintf(char *str, const char *format, ...)
  154. {
  155. /* FIXME */
  156. return 0;
  157. }
  158. int sscanf(const char *str, const char *format, ...)
  159. {
  160. /* FIXME */
  161. return 0;
  162. }
  163. /* unistd.h functions */
  164. void usleep(unsigned long usec)
  165. {
  166. /* FIXME */
  167. return;
  168. }
  169. /* time.h functions */
  170. int gettimeofday(struct timeval *tv, struct timezone *tz)
  171. {
  172. static int usec = 0;
  173. static int sec = 0;
  174. /* FIXME */
  175. usec += 10000;
  176. if(usec > 1000000)
  177. {
  178. sec++;
  179. usec -= 1000000;
  180. }
  181. tv->tv_sec = sec;
  182. tv->tv_usec = usec;
  183. return 0;
  184. }
  185. /* math.h functions */
  186. double cos(double x)
  187. {
  188. double ret = 0.0;
  189. double x2;
  190. double num = 1.0;
  191. double fact = 1.0;
  192. int i;
  193. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  194. x2 = x * x;
  195. /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
  196. for(i = 0; i < 10; i++)
  197. {
  198. ret += num / fact;
  199. num *= - x2;
  200. fact *= (2 * i + 1) * (2 * i + 2);
  201. }
  202. return ret;
  203. }
  204. double sin(double x)
  205. {
  206. double ret = 0.0;
  207. double x2;
  208. double num;
  209. double fact = 1.0;
  210. int i;
  211. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  212. x2 = x * x;
  213. num = x;
  214. /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
  215. for(i = 0; i < 10; i++)
  216. {
  217. ret += num / fact;
  218. num *= - x2;
  219. fact *= (2 * i + 2) * (2 * i + 3);
  220. }
  221. return ret;
  222. }
  223. double sqrt(double x)
  224. {
  225. double ret = x;
  226. int i;
  227. /* This is Newton's method */
  228. for(i = 0; i < 10; i++)
  229. ret = (ret * ret + x) / (ret * 2.0);
  230. return (double)ret;
  231. }
  232. #endif /* __KERNEL__ */