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.
 
 
 
 
 
 

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