You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
пре 19 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. return (int)UPPER(*s1) - (int)UPPER(*s2);
  119. }
  120. /* stdarg.h functions */
  121. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  122. {
  123. /* FIXME */
  124. return 0;
  125. }
  126. /* stdio.h functions */
  127. FILE *fopen(const char *path, const char *mode)
  128. {
  129. /* FIXME */
  130. return NULL;
  131. }
  132. int feof(FILE *stream)
  133. {
  134. /* FIXME */
  135. return 0;
  136. }
  137. char *fgets(char *s, int size, FILE *stream)
  138. {
  139. /* FIXME */
  140. return NULL;
  141. }
  142. int fclose(FILE *fp)
  143. {
  144. /* FIXME */
  145. return 0;
  146. }
  147. int printf(const char *format, ...)
  148. {
  149. /* FIXME */
  150. return 0;
  151. }
  152. int sprintf(char *str, const char *format, ...)
  153. {
  154. /* FIXME */
  155. return 0;
  156. }
  157. int sscanf(const char *str, const char *format, ...)
  158. {
  159. /* FIXME */
  160. return 0;
  161. }
  162. /* unistd.h functions */
  163. void usleep(unsigned long usec)
  164. {
  165. /* FIXME */
  166. return;
  167. }
  168. /* time.h functions */
  169. int gettimeofday(struct timeval *tv, struct timezone *tz)
  170. {
  171. static int usec = 0;
  172. static int sec = 0;
  173. /* FIXME */
  174. usec += 10000;
  175. if(usec > 1000000)
  176. {
  177. sec++;
  178. usec -= 1000000;
  179. }
  180. tv->tv_sec = sec;
  181. tv->tv_usec = usec;
  182. return 0;
  183. }
  184. /* math.h functions */
  185. double cos(double x)
  186. {
  187. double ret = 0.0;
  188. double x2;
  189. double num = 1.0;
  190. double fact = 1.0;
  191. int i;
  192. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  193. x2 = x * x;
  194. /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
  195. for(i = 0; i < 10; i++)
  196. {
  197. ret += num / fact;
  198. num *= - x2;
  199. fact *= (2 * i + 1) * (2 * i + 2);
  200. }
  201. return ret;
  202. }
  203. double sin(double x)
  204. {
  205. double ret = 0.0;
  206. double x2;
  207. double num;
  208. double fact = 1.0;
  209. int i;
  210. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  211. x2 = x * x;
  212. num = x;
  213. /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
  214. for(i = 0; i < 10; i++)
  215. {
  216. ret += num / fact;
  217. num *= - x2;
  218. fact *= (2 * i + 2) * (2 * i + 3);
  219. }
  220. return ret;
  221. }
  222. double sqrt(double x)
  223. {
  224. double ret = x;
  225. int i;
  226. /* This is Newton's method */
  227. for(i = 0; i < 10; i++)
  228. ret = (ret * ret + x) / (ret * 2.0);
  229. return (double)ret;
  230. }
  231. #endif /* __KERNEL__ */