stdlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Standard library
  3. * Contains basic functions, including timer and memory functions
  4. */
  5. // uses math.c
  6. #define UART_TX_ADDR 0xC02723
  7. // Timer I/O Addresses
  8. #define TIMER1_VAL 0xC02739
  9. #define TIMER1_CTRL 0xC0273A
  10. #define TIMER2_VAL 0xC0273B
  11. #define TIMER2_CTRL 0xC0273C
  12. #define TIMER3_VAL 0xC0273D
  13. #define TIMER3_CTRL 0xC0273E
  14. word timer1Value = 0;
  15. word timer2Value = 0;
  16. word timer3Value = 0;
  17. /*
  18. * TODO:
  19. * - Convert most of these functions to assembly
  20. */
  21. /*
  22. Copies n words from src to dest
  23. */
  24. void memcpy(word* dest, word* src, word n)
  25. {
  26. word i;
  27. for (i = 0; i < n; i++)
  28. {
  29. dest[i] = src[i];
  30. }
  31. }
  32. /*
  33. Sets n words from dest to val
  34. */
  35. void memset(word* dest, word val, word n)
  36. {
  37. word i;
  38. for (i = 0; i < n; i++)
  39. {
  40. dest[i] = val;
  41. }
  42. }
  43. char* memmove(char* dest, const char* src, word n)
  44. {
  45. char* from = src;
  46. char* to = dest;
  47. if (from == to || n == 0)
  48. return dest;
  49. if (to > from && to-from < (word)n)
  50. {
  51. /* to overlaps with from */
  52. /* <from......> */
  53. /* <to........> */
  54. /* copy in reverse, to avoid overwriting from */
  55. word i;
  56. for(i=n-1; i>=0; i--)
  57. to[i] = from[i];
  58. return dest;
  59. }
  60. if (from > to && from-to < (word)n)
  61. {
  62. /* to overlaps with from */
  63. /* <from......> */
  64. /* <to........> */
  65. /* copy forwards, to avoid overwriting from */
  66. word i;
  67. for(i=0; i<n; i++)
  68. to[i] = from[i];
  69. return dest;
  70. }
  71. memcpy(dest, src, n);
  72. return dest;
  73. }
  74. /*
  75. Compares n words between a and b
  76. Returns 1 if similar, 0 otherwise
  77. */
  78. word memcmp(word* a, word* b, word n)
  79. {
  80. word i;
  81. for (i = 0; i < n; i++)
  82. {
  83. if (a[i] != b[i])
  84. {
  85. return 0;
  86. }
  87. }
  88. return 1;
  89. }
  90. // Returns length of string
  91. word strlen(char* str)
  92. {
  93. word retval = 0;
  94. char chr = *str; // first character of str
  95. while (chr != 0) // continue until null value
  96. {
  97. retval += 1;
  98. str++; // go to next character address
  99. chr = *str; // get character from address
  100. }
  101. return retval;
  102. }
  103. /*
  104. Copies string from src to dest
  105. Returns number of characters copied
  106. */
  107. word strcpy(char* dest, char* src)
  108. {
  109. // write to buffer
  110. word i = 0;
  111. while (src[i] != 0)
  112. {
  113. dest[i] = src[i];
  114. i++;
  115. }
  116. // terminate
  117. dest[i] = 0;
  118. return i;
  119. }
  120. /*
  121. Appends string from src to dest
  122. Returns number of characters appended
  123. */
  124. word strcat(char* dest, char* src)
  125. {
  126. // move to end of destination
  127. word endOfDest = 0;
  128. while (dest[endOfDest] != 0)
  129. endOfDest++;
  130. // copy to end of destination
  131. return strcpy(dest+endOfDest, src);
  132. }
  133. /*
  134. Compares two strings a and b
  135. Returns 0 if similar
  136. otherwise returns the difference in the first non-matching character
  137. */
  138. word strcmp(char* s1, char* s2)
  139. {
  140. while(*s1 && (*s1 == *s2))
  141. {
  142. s1++;
  143. s2++;
  144. }
  145. return *s1 - *s2;
  146. }
  147. /*
  148. Returns a pointer to the first occurrence of the character c in the string s, or 0 if the character is not found.
  149. */
  150. char* strchr (const char *s, char c)
  151. {
  152. do {
  153. if (*s == c)
  154. {
  155. return (char*)s;
  156. }
  157. } while (*s++);
  158. return 0;
  159. }
  160. /*
  161. Returns a pointer to the last occurance of a character, or 0 if the character is not found.
  162. */
  163. char* strrchr (const char *s, int c)
  164. {
  165. char *rtnval = 0;
  166. do {
  167. if (*s == c)
  168. rtnval = (char*) s;
  169. } while (*s++);
  170. return (rtnval);
  171. }
  172. char * strtok_old_str;
  173. /*
  174. Parse str into tokens separated by characters in delim.
  175. If S is NULL, the last string strtok() was called with is used.
  176. Note that strtok() modifies the input string.
  177. For example:
  178. char s[] = "-abc-=-def";
  179. x = strtok(s, "-"); // x = "abc"
  180. x = strtok(NULL, "-="); // x = "def"
  181. x = strtok(NULL, "="); // x = NULL
  182. // s = "abc\0=-def\0"
  183. */
  184. char* strtok(char* str, const char* delim)
  185. {
  186. if (str != (word*)-1)
  187. strtok_old_str = str;
  188. if (strtok_old_str == (word*)-1)
  189. return (word*)-1;
  190. // Return reached end of string
  191. if (*strtok_old_str == 0)
  192. {
  193. return (word*)-1;
  194. }
  195. // Skip leading delimiters
  196. while (strchr(delim, *strtok_old_str) != 0)
  197. strtok_old_str++;
  198. // Find end of token
  199. char* start = strtok_old_str;
  200. while (*strtok_old_str != 0 && strchr(delim, *strtok_old_str) == 0)
  201. strtok_old_str++;
  202. if (*strtok_old_str == 0)
  203. {
  204. strtok_old_str = (word*)-1;
  205. return start;
  206. }
  207. *strtok_old_str = 0;
  208. strtok_old_str++;
  209. return start;
  210. }
  211. /*
  212. Compress a string made of one char per word, into a string made of one char per byte.
  213. */
  214. void strcompress(word* dest, char* src)
  215. {
  216. word i_src = 0;
  217. word i_dst = 0;
  218. word byte_offset = 0;
  219. word c = src[i_src];
  220. while (c != 0)
  221. {
  222. dest[i_dst] |= (c << byte_offset);
  223. if (byte_offset == 24)
  224. {
  225. byte_offset = 0;
  226. i_dst++;
  227. dest[i_dst] = 0;
  228. }
  229. else
  230. {
  231. byte_offset += 8;
  232. }
  233. i_src++;
  234. c = src[i_src];
  235. }
  236. }
  237. /*
  238. Decompress a string made of one char per byte, into a string made of one char per word.
  239. */
  240. void strdecompress(char* dest, word* src)
  241. {
  242. word i_src = 0;
  243. word i_dst = 0;
  244. word byte_offset = 0;
  245. while (1)
  246. {
  247. word c = (src[i_src] >> byte_offset) & 0xFF;
  248. if (c == 0)
  249. break;
  250. dest[i_dst++] = c;
  251. if (byte_offset == 24)
  252. {
  253. byte_offset = 0;
  254. i_src++;
  255. }
  256. else
  257. {
  258. byte_offset += 8;
  259. }
  260. }
  261. // Terminate
  262. dest[i_dst] = 0;
  263. }
  264. /**
  265. * Return the basename of a path
  266. * path: full path
  267. */
  268. char* basename(char *path)
  269. {
  270. char *base = strrchr(path, '/');
  271. return base ? base + 1 : path;
  272. }
  273. /**
  274. * Return the dirname of a path
  275. * output: buffer to store the dirname
  276. * path: full path
  277. */
  278. char* dirname(char* output, char *path)
  279. {
  280. strcpy(output, path);
  281. char *last_slash = strrchr(output, '/');
  282. if (last_slash != 0)
  283. {
  284. *last_slash = 0;
  285. // If the last slash is the first character, return "/"
  286. if (last_slash == output)
  287. {
  288. strcpy(output, "/");
  289. }
  290. } else
  291. {
  292. // No slash found, return "."
  293. strcpy(output, ".");
  294. }
  295. return output;
  296. }
  297. /*
  298. Recursive helper function for itoa
  299. Eventually returns the number of digits in n
  300. s is the output buffer
  301. */
  302. word itoar(word n, char *s)
  303. {
  304. word digit = MATH_modU(n, 10);
  305. word i = 0;
  306. n = MATH_divU(n,10);
  307. if ((unsigned int) n > 0)
  308. i += itoar(n, s);
  309. s[i++] = digit + '0';
  310. return i;
  311. }
  312. /*
  313. Converts integer n to characters.
  314. The characters are placed in the buffer s.
  315. The buffer is terminated with a 0 value.
  316. Uses recursion, division and mod to compute.
  317. */
  318. void itoa(word n, char *s)
  319. {
  320. // compute and fill the buffer
  321. word i = itoar(n, s);
  322. // end with terminator
  323. s[i] = 0;
  324. }
  325. /*
  326. Recursive helper function for itoa
  327. Eventually returns the number of digits in n
  328. s is the output buffer
  329. */
  330. word itoahr(word n, char *s)
  331. {
  332. word digit = MATH_modU(n, 16);
  333. word i = 0;
  334. n = MATH_divU(n,16);
  335. if ((unsigned int) n > 0)
  336. i += itoahr(n, s);
  337. char c;
  338. if (digit > 9)
  339. {
  340. c = digit + 'A' - 10;
  341. }
  342. else
  343. {
  344. c = digit + '0';
  345. }
  346. s[i++] = c;
  347. return i;
  348. }
  349. /*
  350. Converts integer n to hex string characters.
  351. The characters are placed in the buffer s.
  352. A prefix of 0x is added.
  353. The buffer is terminated with a 0 value.
  354. Uses recursion, division and mod to compute.
  355. */
  356. void itoah(word n, char *s)
  357. {
  358. // add prefix
  359. s[0] = '0';
  360. s[1] = 'x';
  361. s+=2;
  362. // compute and fill the buffer
  363. word i = itoahr(n, s);
  364. // end with terminator
  365. s[i] = 0;
  366. }
  367. // isalpha
  368. word isalpha(char c)
  369. {
  370. if (c >= 'A' && c <= 'Z')
  371. return 2;
  372. if (c >= 'a' && c <= 'z')
  373. return 1;
  374. return 0;
  375. }
  376. // isdigit
  377. word isdigit(char c)
  378. {
  379. if (c >= '0' && c <= '9')
  380. return 1;
  381. return 0;
  382. }
  383. // isalnum
  384. word isalnum(char c)
  385. {
  386. if (isdigit(c) || isalpha(c))
  387. return 1;
  388. return 0;
  389. }
  390. /*
  391. Converts string into int.
  392. Assumes the string is valid.
  393. */
  394. word strToInt(char* str)
  395. {
  396. word retval = 0;
  397. word multiplier = 1;
  398. word i = 0;
  399. while (str[i] != 0)
  400. {
  401. i++;
  402. }
  403. if (i == 0)
  404. return 0;
  405. i--;
  406. while (i > 0)
  407. {
  408. // Return 0 if not a digit
  409. if (str[i] < '0' || str[i] > '9')
  410. return 0;
  411. word currentDigit = str[i] - '0';
  412. word toAdd = multiplier * currentDigit;
  413. retval += toAdd;
  414. multiplier = multiplier * 10;
  415. i--;
  416. }
  417. // Check for negative
  418. if (str[i] == '-')
  419. {
  420. retval *= -1;
  421. }
  422. else
  423. {
  424. word currentDigit = str[i] - '0';
  425. word toAdd = multiplier * currentDigit;
  426. retval += toAdd;
  427. }
  428. return retval;
  429. }
  430. /*
  431. Speed optimized function to get the number of decimals for a given digit
  432. */
  433. word numberOfDecimals(word n)
  434. {
  435. if (n < 0) n = -n; // Ignore for now the INT_MIN case where this does not work
  436. if (n < 10) return 1;
  437. if (n < 100) return 2;
  438. if (n < 1000) return 3;
  439. if (n < 10000) return 4;
  440. if (n < 100000) return 5;
  441. if (n < 1000000) return 6;
  442. if (n < 10000000) return 7;
  443. if (n < 100000000) return 8;
  444. if (n < 1000000000) return 9;
  445. // Cannot be > 10 for a 32bit integer
  446. return 10;
  447. }
  448. /*
  449. Prints a single char c by writing it to UART_TX_ADDR
  450. */
  451. void uprintc(char c)
  452. {
  453. word *p = (word *)UART_TX_ADDR; // address of UART TX
  454. *p = (word)c; // write char over UART
  455. }
  456. /*
  457. Sends each character from str over UART
  458. by writing them to UART_TX_ADDR
  459. until a 0 value is found.
  460. Does not send a newline afterwards.
  461. */
  462. void uprint(char* str)
  463. {
  464. word *p = (word *)UART_TX_ADDR; // address of UART TX
  465. char chr = *str; // first character of str
  466. while (chr != 0) // continue until null value
  467. {
  468. *p = (word)chr; // write char over UART
  469. str++; // go to next character address
  470. chr = *str; // get character from address
  471. }
  472. }
  473. /*
  474. Same as uprint(char* str),
  475. except it sends a newline afterwards.
  476. */
  477. void uprintln(char* str)
  478. {
  479. uprint(str);
  480. uprintc('\n');
  481. }
  482. /*
  483. Prints decimal integer over UART
  484. */
  485. void uprintDec(word i)
  486. {
  487. char buffer[11];
  488. itoa(i, buffer);
  489. uprint(buffer);
  490. }
  491. /*
  492. Prints hex integer over UART
  493. */
  494. void uprintHex(word i)
  495. {
  496. char buffer[11];
  497. itoah(i, buffer);
  498. uprint(buffer);
  499. }
  500. /*
  501. Prints decimal integer over UART, with newline
  502. */
  503. void uprintlnDec(word i)
  504. {
  505. char buffer[11];
  506. itoa(i, buffer);
  507. uprint(buffer);
  508. uprintc('\n');
  509. }
  510. /*
  511. Prints hex integer over UART, with newline
  512. */
  513. void uprintlnHex(word i)
  514. {
  515. char buffer[11];
  516. itoah(i, buffer);
  517. uprint(buffer);
  518. uprintc('\n');
  519. }
  520. // sleeps ms using timer1.
  521. // blocking.
  522. // requires int1() to set timer1Value to 1:
  523. /*
  524. timer1Value = 1; // notify ending of timer1
  525. */
  526. void delay(word ms)
  527. {
  528. // clear result
  529. timer1Value = 0;
  530. // set timer
  531. word *p = (word *) TIMER1_VAL;
  532. *p = ms;
  533. // start timer
  534. word *q = (word *) TIMER1_CTRL;
  535. *q = 1;
  536. // wait until timer done
  537. while (timer1Value == 0);
  538. }
  539. // Returns milliseconds since last reset
  540. word millis()
  541. {
  542. word retval = 0;
  543. asm(
  544. "load32 0xC0274A r2\n" // millis addr
  545. "read 0 r2 r2\n" // read millis
  546. "write -4 r14 r2\n" // write to stack to return
  547. );
  548. return retval;
  549. }
  550. // Converts char c to uppercase if possible
  551. char toUpper(char c)
  552. {
  553. if (c>96 && c<123)
  554. c = c ^ 0x20;
  555. return c;
  556. }
  557. // Converts string str to uppercase if possible
  558. void strToUpper(char* str)
  559. {
  560. char chr = *str; // first character of str
  561. while (chr != 0) // continue until null value
  562. {
  563. *str = toUpper(chr); // uppercase char
  564. str++; // go to next character address
  565. chr = *str; // get character from address
  566. }
  567. }
  568. /*
  569. For debugging
  570. Prints a hex dump of size 'len' for each word starting from 'addr'
  571. Values are printed over UART
  572. */
  573. void hexdump(char* addr, word len, word words_per_line)
  574. {
  575. char buf[16];
  576. word i;
  577. for (i = 0; i < len; i++)
  578. {
  579. // newline every words_per_line words
  580. if (i != 0 && MATH_modU(i, words_per_line) == 0)
  581. uprintc('\n');
  582. itoah(addr[i], buf);
  583. uprint(buf);
  584. uprintc(' ');
  585. }
  586. }