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.
 
 
 
 
 
 

273 lines
4.5 KiB

  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. /* Our memory mapping */
  23. static uint32_t *freemem = (uint32_t*) 0x00200000;
  24. /* Multiboot kernel entry point */
  25. void cmain(unsigned long int magic, unsigned long int addr)
  26. {
  27. static char const text[] = "Booting libcaca kernel...";
  28. char const *ptr = text;
  29. char *video = (char*)0xB8000;
  30. char *argv[] = { NULL };
  31. int argc = 0;
  32. /* Print a text message to say hello */
  33. while(*ptr)
  34. *video = *ptr++; video += 2;
  35. /* Launch the main program */
  36. main(argc, argv);
  37. }
  38. /* stdlib.h functions */
  39. void *malloc(size_t size)
  40. {
  41. uint32_t *p = freemem;
  42. size = (size + 0x7) / 4;
  43. *p = size;
  44. freemem += size + 1;
  45. return p + 1;
  46. }
  47. void free(void *ptr)
  48. {
  49. return;
  50. }
  51. void *realloc(void *ptr, size_t size)
  52. {
  53. uint32_t oldsize = ((uint32_t *)ptr)[-1];
  54. void *p;
  55. if(oldsize >= size)
  56. return ptr;
  57. p = malloc(size);
  58. memcpy(p, ptr, oldsize);
  59. return p;
  60. }
  61. char *getenv(const char *name)
  62. {
  63. return NULL;
  64. }
  65. int rand(void)
  66. {
  67. static int seed = 0x68743284;
  68. seed = (seed * 0x7f32ba17) ^ 0xf893a735;
  69. return seed % RAND_MAX;
  70. }
  71. int abs(int j)
  72. {
  73. if(j < 0)
  74. return -j;
  75. return j;
  76. }
  77. void exit(int status)
  78. {
  79. /* FIXME: reboot? */
  80. while(1);
  81. }
  82. /* string.h functions */
  83. void *memset(void *s, int c, size_t n)
  84. {
  85. uint8_t *ptr = s;
  86. while(n--)
  87. *ptr++ = c;
  88. return s;
  89. }
  90. void *memcpy(void *dest, const void *src, size_t n)
  91. {
  92. uint8_t *destptr = dest;
  93. uint8_t const *srcptr = src;
  94. while(n--)
  95. *destptr++ = *srcptr++;
  96. return dest;
  97. }
  98. size_t strlen(const char *s)
  99. {
  100. int len = 0;
  101. while(*s++)
  102. len++;
  103. return len;
  104. }
  105. int strcasecmp(const char *s1, const char *s2)
  106. {
  107. /* FIXME */
  108. return -1;
  109. }
  110. /* stdarg.h functions */
  111. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  112. {
  113. /* FIXME */
  114. return 0;
  115. }
  116. /* stdio.h functions */
  117. FILE *fopen(const char *path, const char *mode)
  118. {
  119. /* FIXME */
  120. return NULL;
  121. }
  122. int feof(FILE *stream)
  123. {
  124. /* FIXME */
  125. return 0;
  126. }
  127. char *fgets(char *s, int size, FILE *stream)
  128. {
  129. /* FIXME */
  130. return NULL;
  131. }
  132. int fclose(FILE *fp)
  133. {
  134. /* FIXME */
  135. return 0;
  136. }
  137. int printf(const char *format, ...)
  138. {
  139. /* FIXME */
  140. return 0;
  141. }
  142. int sprintf(char *str, const char *format, ...)
  143. {
  144. /* FIXME */
  145. return 0;
  146. }
  147. int sscanf(const char *str, const char *format, ...)
  148. {
  149. /* FIXME */
  150. return 0;
  151. }
  152. /* unistd.h functions */
  153. void usleep(unsigned long usec)
  154. {
  155. /* FIXME */
  156. return;
  157. }
  158. /* time.h functions */
  159. int gettimeofday(struct timeval *tv, struct timezone *tz)
  160. {
  161. static int usec = 0;
  162. static int sec = 0;
  163. /* FIXME */
  164. usec += 10000;
  165. if(usec > 1000000)
  166. {
  167. sec++;
  168. usec -= 1000000;
  169. }
  170. tv->tv_sec = sec;
  171. tv->tv_usec = usec;
  172. return 0;
  173. }
  174. /* math.h functions */
  175. double cos(double x)
  176. {
  177. double ret = 0.0;
  178. double x2;
  179. double num = 1.0;
  180. double fact = 1.0;
  181. int i;
  182. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  183. x2 = x * x;
  184. /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
  185. for(i = 0; i < 10; i++)
  186. {
  187. ret += num / fact;
  188. num *= - x2;
  189. fact *= (2 * i + 1) * (2 * i + 2);
  190. }
  191. return ret;
  192. }
  193. double sin(double x)
  194. {
  195. double ret = 0.0;
  196. double x2;
  197. double num;
  198. double fact = 1.0;
  199. int i;
  200. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  201. x2 = x * x;
  202. num = x;
  203. /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
  204. for(i = 0; i < 10; i++)
  205. {
  206. ret += num / fact;
  207. num *= - x2;
  208. fact *= (2 * i + 2) * (2 * i + 3);
  209. }
  210. return ret;
  211. }
  212. double sqrt(double x)
  213. {
  214. double ret = x;
  215. int i;
  216. /* This is Newton's method */
  217. for(i = 0; i < 10; i++)
  218. ret = (ret * ret + x) / (ret * 2.0);
  219. return ret;
  220. }
  221. #endif /* __KERNEL__ */