Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

661 строка
12 KiB

  1. /*
  2. * libcaca
  3. * libcaca Colour ASCII-Art library
  4. * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
  5. * 2009 Jean-Yves Lamoureux <jylam@lnxscene.org>
  6. * All Rights Reserved
  7. *
  8. * $Id: kernel.h 4154 2009-12-20 13:33:11Z jylam $
  9. *
  10. * This library is free software. It comes without any warranty, to
  11. * the extent permitted by applicable law. You can redistribute it
  12. * and/or modify it under the terms of the Do What The Fuck You Want
  13. * To Public License, Version 2, as published by Sam Hocevar. See
  14. * http://sam.zoy.org/wtfpl/COPYING for more details.
  15. */
  16. #include "config.h"
  17. #include "caca_types.h"
  18. #include "klibc.h"
  19. #include "drivers/timer.h"
  20. #include "kernel.h"
  21. void htoa(unsigned int value, char s[]);
  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 default seed for random number generator */
  29. static int seed = 0x68743284;
  30. /* Our memory mapping */
  31. static uint32_t *freemem = (uint32_t *) 0x00200000;
  32. int kX = 0;
  33. int kY = 0;
  34. void scroll(void)
  35. {
  36. unsigned char *video, *tmp;
  37. for (video = (unsigned char *)0xB8000; video < (unsigned char *)0xB8FA0;
  38. video++)
  39. {
  40. tmp = (unsigned char *)(video + 1 * 160);
  41. if (tmp < (unsigned char *)0xB8FA0)
  42. *video = *tmp;
  43. else
  44. *video = 0;
  45. }
  46. kY -= 1;
  47. if (kY < 0)
  48. kY = 0;
  49. }
  50. void putcar(unsigned char c)
  51. {
  52. unsigned char *video;
  53. if (c == 10)
  54. {
  55. kX = 0;
  56. kY++;
  57. }
  58. else
  59. {
  60. video = (unsigned char *)(0xB8000 + 2 * kX + 160 * kY);
  61. *video = c;
  62. *(video + 1) = 0x07;
  63. kX++;
  64. if (kX > 79)
  65. {
  66. kX = 0;
  67. kY++;
  68. }
  69. if (kY >= 24)
  70. {
  71. scroll();
  72. }
  73. }
  74. }
  75. void print(char *str)
  76. {
  77. char const *ptr = str;
  78. while (*ptr)
  79. {
  80. putcar(*ptr++);
  81. }
  82. }
  83. void clearscreen(void)
  84. {
  85. int x, y;
  86. kX = 0;
  87. kY = 0;
  88. for (y = 0; y < 25; y++)
  89. for (x = 0; x < 80; x++)
  90. {
  91. putcar(' ');
  92. }
  93. kX = 0;
  94. kY = 0;
  95. }
  96. /* stdlib.h functions */
  97. void *malloc(size_t size)
  98. {
  99. uint32_t *p = freemem;
  100. if (!size)
  101. return NULL;
  102. size = (size + 0x7) / 4;
  103. *p = size;
  104. freemem += size + 1;
  105. return p + 1;
  106. }
  107. void free(void *ptr)
  108. {
  109. return;
  110. }
  111. void *realloc(void *ptr, size_t size)
  112. {
  113. uint32_t oldsize;
  114. void *p;
  115. if (!size)
  116. return NULL;
  117. if (!ptr)
  118. oldsize = 0;
  119. else
  120. {
  121. oldsize = ((uint32_t *) ptr)[-1];
  122. if (oldsize >= size)
  123. return ptr;
  124. }
  125. p = malloc(size);
  126. memcpy(p, ptr, oldsize);
  127. return p;
  128. }
  129. char *getenv(const char *name)
  130. {
  131. return NULL;
  132. }
  133. int getpid(void)
  134. {
  135. return 0x1337;
  136. }
  137. void srand(unsigned int s)
  138. {
  139. seed = rand();
  140. }
  141. int time(void *dummy)
  142. {
  143. return rand();
  144. }
  145. int rand(void)
  146. {
  147. seed = (seed * 0x7f32ba17) ^ 0xf893a735;
  148. return seed % RAND_MAX;
  149. }
  150. int abs(int j)
  151. {
  152. if (j < 0)
  153. return -j;
  154. return j;
  155. }
  156. void exit(int status)
  157. {
  158. /* FIXME: reboot? */
  159. while (1);
  160. }
  161. int atexit(void (*function) (void))
  162. {
  163. /* FIXME: register function */
  164. return 0;
  165. }
  166. /* string.h functions */
  167. void *memset(void *s, int c, size_t n)
  168. {
  169. uint8_t *ptr = s;
  170. while (n--)
  171. *ptr++ = c;
  172. return s;
  173. }
  174. void *memcpy(void *dest, const void *src, size_t n)
  175. {
  176. uint8_t *destptr = dest;
  177. uint8_t const *srcptr = src;
  178. while (n--)
  179. *destptr++ = *srcptr++;
  180. return dest;
  181. }
  182. void *memmove(void *dest, const void *src, size_t n)
  183. {
  184. memcpy(freemem, src, n);
  185. memcpy(dest, freemem, n);
  186. return dest;
  187. }
  188. size_t strlen(const char *s)
  189. {
  190. int len = 0;
  191. while (*s++)
  192. len++;
  193. return len;
  194. }
  195. int strcmp(const char *s1, const char *s2)
  196. {
  197. while (*s1 && *s1 == *s2)
  198. {
  199. s1++;
  200. s2++;
  201. }
  202. return (int)*s1 - (int)*s2;
  203. }
  204. int strcasecmp(const char *s1, const char *s2)
  205. {
  206. while (*s1 && *s2 && UPPER(*s1) == UPPER(*s2))
  207. {
  208. s1++;
  209. s2++;
  210. }
  211. return (int)UPPER(*s1) - (int)UPPER(*s2);
  212. }
  213. int memcmp(const void *_s1, const void *_s2, size_t n)
  214. {
  215. uint8_t const *s1 = _s1, *s2 = _s2;
  216. while (n--)
  217. {
  218. if (*s1 != *s2)
  219. return (int)*s1 - (int)*s2;
  220. s1++;
  221. s2++;
  222. }
  223. return 0;
  224. }
  225. char *strdup(const char *s)
  226. {
  227. char *new;
  228. unsigned int len = strlen(s);
  229. new = malloc(len + 1);
  230. memcpy(new, s, len + 1);
  231. return new;
  232. }
  233. char *strchr(const char *s, int c)
  234. {
  235. do
  236. if (*s == c)
  237. return (char *)(intptr_t) s;
  238. while (*s++);
  239. return NULL;
  240. }
  241. /* stdarg.h functions */
  242. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  243. {
  244. /* FIXME */
  245. return 0;
  246. }
  247. /* stdio.h functions */
  248. FILE *fopen(const char *path, const char *mode)
  249. {
  250. /* FIXME */
  251. return NULL;
  252. }
  253. int feof(FILE * stream)
  254. {
  255. /* FIXME */
  256. return 0;
  257. }
  258. char *fgets(char *s, int size, FILE * stream)
  259. {
  260. /* FIXME */
  261. return NULL;
  262. }
  263. size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE * stream)
  264. {
  265. return 0;
  266. }
  267. int fclose(FILE * fp)
  268. {
  269. /* FIXME */
  270. return 0;
  271. }
  272. int printf(const char *fmt, ...)
  273. {
  274. char str[200];
  275. char tmp[100];
  276. args_list args;
  277. args_start(args, fmt);
  278. char *s;
  279. int ptr = 0;
  280. int i = 0;
  281. for (; fmt[i]; ++i)
  282. {
  283. if ((fmt[i] != '%') && (fmt[i] != '\\'))
  284. {
  285. str[ptr++] = fmt[i];
  286. continue;
  287. }
  288. else if (fmt[i] == '\\')
  289. {
  290. switch (fmt[++i])
  291. {
  292. case 'a':
  293. str[ptr++] = '\a';
  294. break;
  295. case 'b':
  296. str[ptr++] = '\b';
  297. break;
  298. case 't':
  299. str[ptr++] = '\t';
  300. break;
  301. case 'n':
  302. str[ptr++] = '\n';
  303. break;
  304. case 'r':
  305. str[ptr++] = '\r';
  306. break;
  307. case '\\':
  308. str[ptr++] = '\\';
  309. break;
  310. }
  311. continue;
  312. }
  313. switch (fmt[++i])
  314. {
  315. case 's':
  316. s = (char *)args_next(args, char *);
  317. while (*s)
  318. str[ptr++] = *s++;
  319. break;
  320. case 'c':
  321. str[ptr++] = (char)args_next(args, int);
  322. break;
  323. case 'p':
  324. case 'x':
  325. htoa((unsigned long)args_next(args, unsigned long), tmp);
  326. memcpy(&str[ptr], tmp, strlen(tmp));
  327. ptr += strlen(tmp);
  328. break;
  329. case 'd':
  330. itoa((unsigned long)args_next(args, unsigned long), tmp);
  331. memcpy(&str[ptr], tmp, strlen(tmp));
  332. ptr += strlen(tmp);
  333. break;
  334. case '%':
  335. str[ptr++] = '%';
  336. break;
  337. default:
  338. str[ptr++] = fmt[i];
  339. break;
  340. }
  341. }
  342. str[ptr] = '\0';
  343. args_end(args);
  344. print(str);
  345. return 0;
  346. }
  347. int fprintf(FILE * stream, const char *format, ...)
  348. {
  349. /* FIXME */
  350. return 0;
  351. }
  352. int fflush(FILE * stream)
  353. {
  354. /* FIXME */
  355. return 0;
  356. }
  357. int sprintf(char *str, const char *fmt, ...)
  358. {
  359. char tmp[100];
  360. args_list args;
  361. args_start(args, fmt);
  362. char *s;
  363. int ptr = 0;
  364. int i = 0;
  365. for (; fmt[i]; ++i)
  366. {
  367. if ((fmt[i] != '%') && (fmt[i] != '\\'))
  368. {
  369. str[ptr++] = fmt[i];
  370. continue;
  371. }
  372. else if (fmt[i] == '\\')
  373. {
  374. switch (fmt[++i])
  375. {
  376. case 'a':
  377. str[ptr++] = '\a';
  378. break;
  379. case 'b':
  380. str[ptr++] = '\b';
  381. break;
  382. case 't':
  383. str[ptr++] = '\t';
  384. break;
  385. case 'n':
  386. str[ptr++] = '\n';
  387. break;
  388. case 'r':
  389. str[ptr++] = '\r';
  390. break;
  391. case '\\':
  392. str[ptr++] = '\\';
  393. break;
  394. }
  395. continue;
  396. }
  397. switch (fmt[++i])
  398. {
  399. case 's':
  400. s = (char *)args_next(args, char *);
  401. while (*s)
  402. str[ptr++] = *s++;
  403. break;
  404. case 'c':
  405. str[ptr++] = (char)args_next(args, int);
  406. break;
  407. case 'p':
  408. case 'x':
  409. htoa((unsigned long)args_next(args, unsigned long), tmp);
  410. memcpy(&str[ptr], tmp, strlen(tmp));
  411. ptr += strlen(tmp);
  412. break;
  413. case 'd':
  414. itoa((unsigned long)args_next(args, unsigned long), tmp);
  415. memcpy(&str[ptr], tmp, strlen(tmp));
  416. ptr += strlen(tmp);
  417. break;
  418. case '%':
  419. str[ptr++] = '%';
  420. break;
  421. default:
  422. str[ptr++] = fmt[i];
  423. break;
  424. }
  425. }
  426. str[ptr] = '\0';
  427. args_end(args);
  428. return 0;
  429. }
  430. int sscanf(const char *str, const char *format, ...)
  431. {
  432. /* FIXME */
  433. return 0;
  434. }
  435. /* unistd.h functions */
  436. void usleep(unsigned long usec)
  437. {
  438. u32 start = ticks;
  439. signed int diff = 0;
  440. while (1)
  441. {
  442. diff = (signed int)(ticks - start);
  443. if (diff >= (signed int)(usec / 20))
  444. break;
  445. }
  446. }
  447. void sleep(unsigned long sec)
  448. {
  449. usleep(sec * 1000);
  450. }
  451. int gettimeofday(struct timeval *tv, struct timezone *tz)
  452. {
  453. static int usec = 0;
  454. static int sec = 0;
  455. /* FIXME */
  456. usec += 10000;
  457. if (usec > 1000000)
  458. {
  459. sec++;
  460. usec -= 1000000;
  461. }
  462. tv->tv_sec = sec;
  463. tv->tv_usec = usec;
  464. return 0;
  465. }
  466. /* math.h functions */
  467. double cos(double x)
  468. {
  469. double ret = 0.0;
  470. #ifdef HAVE_FSIN_FCOS
  471. asm volatile ("fcos":"=t" (ret):"0"(x));
  472. #else
  473. double x2;
  474. double num = 1.0;
  475. double fact = 1.0;
  476. int i;
  477. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  478. x2 = x * x;
  479. /* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
  480. for (i = 0; i < 10; i++)
  481. {
  482. ret += num / fact;
  483. num *= -x2;
  484. fact *= (2 * i + 1) * (2 * i + 2);
  485. }
  486. #endif
  487. return ret;
  488. }
  489. double sin(double x)
  490. {
  491. double ret = 0.0;
  492. #ifdef HAVE_FSIN_FCOS
  493. asm volatile ("fsin":"=t" (ret):"0"(x));
  494. #else
  495. double x2;
  496. double num;
  497. double fact = 1.0;
  498. int i;
  499. x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
  500. x2 = x * x;
  501. num = x;
  502. /* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
  503. for (i = 0; i < 10; i++)
  504. {
  505. ret += num / fact;
  506. num *= -x2;
  507. fact *= (2 * i + 2) * (2 * i + 3);
  508. }
  509. #endif
  510. return ret;
  511. }
  512. double sqrt(double x)
  513. {
  514. double ret = x;
  515. int i;
  516. /* This is Newton's method */
  517. for (i = 0; i < 10; i++)
  518. ret = (ret * ret + x) / (ret * 2.0);
  519. return ret;
  520. }
  521. /* reverse: reverse string s in place */
  522. void reverse(char s[])
  523. {
  524. int i, j;
  525. char c;
  526. for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
  527. {
  528. c = s[i];
  529. s[i] = s[j];
  530. s[j] = c;
  531. }
  532. }
  533. /* itoa implementation, by Kernighan and Ritchie's The C Programming Language */
  534. void itoa(int n, char s[])
  535. {
  536. int i, sign;
  537. if ((sign = n) < 0) /* record sign */
  538. n = -n; /* make n positive */
  539. i = 0;
  540. do
  541. { /* generate digits in reverse order */
  542. s[i++] = n % 10 + '0'; /* get next digit */
  543. }
  544. while ((n /= 10) > 0); /* delete it */
  545. if (sign < 0)
  546. s[i++] = '-';
  547. s[i] = '\0';
  548. reverse(s);
  549. }
  550. void htoa(unsigned int value, char s[])
  551. {
  552. int i = 8;
  553. int ptr = 0;
  554. while (i-- > 0)
  555. {
  556. s[ptr++] = "0123456789abcdef"[(value >> (i * 4)) & 0xf];
  557. }
  558. s[ptr] = 0;
  559. }
  560. /* errno.h stuff */
  561. int errno = 0;