STDLIB.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. Appends string from src to dest up to n characters
  135. Returns pointer to resulting dest string
  136. */
  137. char *strncat(char *dest, char *src, word n)
  138. {
  139. word len1 = strlen(dest);
  140. word len2 = strlen(src);
  141. if (len2 < n)
  142. {
  143. strcpy(&dest[len1], src);
  144. }
  145. else
  146. {
  147. strncpy(&dest[len1], src, n);
  148. dest[len1 + n] = '\0';
  149. }
  150. return dest;
  151. }
  152. /*
  153. Compares two strings a and b
  154. Returns 0 if similar
  155. otherwise returns the difference in the first non-matching character
  156. */
  157. word strcmp(char* s1, char* s2)
  158. {
  159. while(*s1 && (*s1 == *s2))
  160. {
  161. s1++;
  162. s2++;
  163. }
  164. return *s1 - *s2;
  165. }
  166. /*
  167. Returns a pointer to the first occurrence of the character c in the string s, or 0 if the character is not found.
  168. */
  169. char* strchr (const char *s, char c)
  170. {
  171. do {
  172. if (*s == c)
  173. {
  174. return (char*)s;
  175. }
  176. } while (*s++);
  177. return 0;
  178. }
  179. /*
  180. Returns a pointer to the last occurance of a character, or 0 if the character is not found.
  181. */
  182. char* strrchr (const char *s, int c)
  183. {
  184. char *rtnval = 0;
  185. do {
  186. if (*s == c)
  187. rtnval = (char*) s;
  188. } while (*s++);
  189. return (rtnval);
  190. }
  191. char * strtok_old_str;
  192. /*
  193. Parse str into tokens separated by characters in delim.
  194. If S is NULL, the last string strtok() was called with is used.
  195. Note that strtok() modifies the input string.
  196. For example:
  197. char s[] = "-abc-=-def";
  198. x = strtok(s, "-"); // x = "abc"
  199. x = strtok(NULL, "-="); // x = "def"
  200. x = strtok(NULL, "="); // x = NULL
  201. // s = "abc\0=-def\0"
  202. */
  203. char* strtok(char* str, const char* delim)
  204. {
  205. if (str != (word*)-1)
  206. strtok_old_str = str;
  207. if (strtok_old_str == (word*)-1)
  208. return (word*)-1;
  209. // Return reached end of string
  210. if (*strtok_old_str == 0)
  211. {
  212. return (word*)-1;
  213. }
  214. // Skip leading delimiters
  215. while (strchr(delim, *strtok_old_str) != 0)
  216. strtok_old_str++;
  217. // Find end of token
  218. char* start = strtok_old_str;
  219. while (*strtok_old_str != 0 && strchr(delim, *strtok_old_str) == 0)
  220. strtok_old_str++;
  221. if (*strtok_old_str == 0)
  222. {
  223. strtok_old_str = (word*)-1;
  224. return start;
  225. }
  226. *strtok_old_str = 0;
  227. strtok_old_str++;
  228. return start;
  229. }
  230. /*
  231. Compress a string made of one char per word, into a string made of one char per byte.
  232. */
  233. void strcompress(word* dest, char* src)
  234. {
  235. word i_src = 0;
  236. word i_dst = 0;
  237. word byte_offset = 0;
  238. word c = src[i_src];
  239. while (c != 0)
  240. {
  241. dest[i_dst] |= (c << byte_offset);
  242. if (byte_offset == 24)
  243. {
  244. byte_offset = 0;
  245. i_dst++;
  246. dest[i_dst] = 0;
  247. }
  248. else
  249. {
  250. byte_offset += 8;
  251. }
  252. i_src++;
  253. c = src[i_src];
  254. }
  255. }
  256. /*
  257. Decompress a string made of one char per byte, into a string made of one char per word.
  258. */
  259. void strdecompress(char* dest, word* src)
  260. {
  261. word i_src = 0;
  262. word i_dst = 0;
  263. word byte_offset = 0;
  264. while (1)
  265. {
  266. word c = (src[i_src] >> byte_offset) & 0xFF;
  267. if (c == 0)
  268. break;
  269. dest[i_dst++] = c;
  270. if (byte_offset == 24)
  271. {
  272. byte_offset = 0;
  273. i_src++;
  274. }
  275. else
  276. {
  277. byte_offset += 8;
  278. }
  279. }
  280. // Terminate
  281. dest[i_dst] = 0;
  282. }
  283. /**
  284. * Return the basename of a path
  285. * path: full path
  286. */
  287. char* basename(char *path)
  288. {
  289. char *base = strrchr(path, '/');
  290. return base ? base + 1 : path;
  291. }
  292. /**
  293. * Return the dirname of a path
  294. * output: buffer to store the dirname
  295. * path: full path
  296. */
  297. char* dirname(char* output, char *path)
  298. {
  299. strcpy(output, path);
  300. char *last_slash = strrchr(output, '/');
  301. if (last_slash != 0)
  302. {
  303. *last_slash = 0;
  304. // If the last slash is the first character, return "/"
  305. if (last_slash == output)
  306. {
  307. strcpy(output, "/");
  308. }
  309. } else
  310. {
  311. // No slash found, return "."
  312. strcpy(output, ".");
  313. }
  314. return output;
  315. }
  316. /*
  317. Recursive helper function for itoa
  318. Eventually returns the number of digits in n
  319. s is the output buffer
  320. */
  321. word itoar(word n, char *s)
  322. {
  323. word digit = MATH_modU(n, 10);
  324. word i = 0;
  325. n = MATH_divU(n,10);
  326. if ((unsigned int) n > 0)
  327. i += itoar(n, s);
  328. s[i++] = digit + '0';
  329. return i;
  330. }
  331. /*
  332. Converts integer n to characters.
  333. The characters are placed in the buffer s.
  334. The buffer is terminated with a 0 value.
  335. Uses recursion, division and mod to compute.
  336. */
  337. void itoa(word n, char *s)
  338. {
  339. // compute and fill the buffer
  340. word i = itoar(n, s);
  341. // end with terminator
  342. s[i] = 0;
  343. }
  344. /*
  345. Recursive helper function for itoa
  346. Eventually returns the number of digits in n
  347. s is the output buffer
  348. */
  349. word itoahr(word n, char *s)
  350. {
  351. word digit = MATH_modU(n, 16);
  352. word i = 0;
  353. n = MATH_divU(n,16);
  354. if ((unsigned int) n > 0)
  355. i += itoahr(n, s);
  356. char c;
  357. if (digit > 9)
  358. {
  359. c = digit + 'A' - 10;
  360. }
  361. else
  362. {
  363. c = digit + '0';
  364. }
  365. s[i++] = c;
  366. return i;
  367. }
  368. /*
  369. Converts integer n to hex string characters.
  370. The characters are placed in the buffer s.
  371. A prefix of 0x is added.
  372. The buffer is terminated with a 0 value.
  373. Uses recursion, division and mod to compute.
  374. */
  375. void itoah(word n, char *s)
  376. {
  377. // add prefix
  378. s[0] = '0';
  379. s[1] = 'x';
  380. s+=2;
  381. // compute and fill the buffer
  382. word i = itoahr(n, s);
  383. // end with terminator
  384. s[i] = 0;
  385. }
  386. // isalpha
  387. word isalpha(char c)
  388. {
  389. if (c >= 'A' && c <= 'Z')
  390. return 2;
  391. if (c >= 'a' && c <= 'z')
  392. return 1;
  393. return 0;
  394. }
  395. // isdigit
  396. word isdigit(char c)
  397. {
  398. if (c >= '0' && c <= '9')
  399. return 1;
  400. return 0;
  401. }
  402. // isalnum
  403. word isalnum(char c)
  404. {
  405. if (isdigit(c) || isalpha(c))
  406. return 1;
  407. return 0;
  408. }
  409. /*
  410. Converts string into int.
  411. Assumes the string is valid.
  412. */
  413. word strToInt(char* str)
  414. {
  415. word retval = 0;
  416. word multiplier = 1;
  417. word i = 0;
  418. while (str[i] != 0)
  419. {
  420. i++;
  421. }
  422. if (i == 0)
  423. return 0;
  424. i--;
  425. while (i > 0)
  426. {
  427. // Return 0 if not a digit
  428. if (str[i] < '0' || str[i] > '9')
  429. return 0;
  430. word currentDigit = str[i] - '0';
  431. word toAdd = multiplier * currentDigit;
  432. retval += toAdd;
  433. multiplier = multiplier * 10;
  434. i--;
  435. }
  436. // Check for negative
  437. if (str[i] == '-')
  438. {
  439. retval *= -1;
  440. }
  441. else
  442. {
  443. word currentDigit = str[i] - '0';
  444. word toAdd = multiplier * currentDigit;
  445. retval += toAdd;
  446. }
  447. return retval;
  448. }
  449. /*
  450. Speed optimized function to get the number of decimals for a given digit
  451. */
  452. word numberOfDecimals(word n)
  453. {
  454. if (n < 0) n = -n; // Ignore for now the INT_MIN case where this does not work
  455. if (n < 10) return 1;
  456. if (n < 100) return 2;
  457. if (n < 1000) return 3;
  458. if (n < 10000) return 4;
  459. if (n < 100000) return 5;
  460. if (n < 1000000) return 6;
  461. if (n < 10000000) return 7;
  462. if (n < 100000000) return 8;
  463. if (n < 1000000000) return 9;
  464. // Cannot be > 10 for a 32bit integer
  465. return 10;
  466. }
  467. /*
  468. Prints a single char c by writing it to UART_TX_ADDR
  469. */
  470. void uprintc(char c)
  471. {
  472. word *p = (word *)UART_TX_ADDR; // address of UART TX
  473. *p = (word)c; // write char over UART
  474. }
  475. /*
  476. Sends each character from str over UART
  477. by writing them to UART_TX_ADDR
  478. until a 0 value is found.
  479. Does not send a newline afterwards.
  480. */
  481. void uprint(char* str)
  482. {
  483. word *p = (word *)UART_TX_ADDR; // address of UART TX
  484. char chr = *str; // first character of str
  485. while (chr != 0) // continue until null value
  486. {
  487. *p = (word)chr; // write char over UART
  488. str++; // go to next character address
  489. chr = *str; // get character from address
  490. }
  491. }
  492. /*
  493. Same as uprint(char* str),
  494. except it sends a newline afterwards.
  495. */
  496. void uprintln(char* str)
  497. {
  498. uprint(str);
  499. uprintc('\n');
  500. }
  501. /*
  502. Prints decimal integer over UART
  503. */
  504. void uprintDec(word i)
  505. {
  506. char buffer[11];
  507. itoa(i, buffer);
  508. uprint(buffer);
  509. }
  510. /*
  511. Prints hex integer over UART
  512. */
  513. void uprintHex(word i)
  514. {
  515. char buffer[11];
  516. itoah(i, buffer);
  517. uprint(buffer);
  518. }
  519. /*
  520. Prints decimal integer over UART, with newline
  521. */
  522. void uprintlnDec(word i)
  523. {
  524. char buffer[11];
  525. itoa(i, buffer);
  526. uprint(buffer);
  527. uprintc('\n');
  528. }
  529. /*
  530. Prints hex integer over UART, with newline
  531. */
  532. void uprintlnHex(word i)
  533. {
  534. char buffer[11];
  535. itoah(i, buffer);
  536. uprint(buffer);
  537. uprintc('\n');
  538. }
  539. // sleeps ms using timer1.
  540. // blocking.
  541. // requires int1() to set timer1Value to 1:
  542. /*
  543. timer1Value = 1; // notify ending of timer1
  544. */
  545. void delay(word ms)
  546. {
  547. // clear result
  548. timer1Value = 0;
  549. // set timer
  550. word *p = (word *) TIMER1_VAL;
  551. *p = ms;
  552. // start timer
  553. word *q = (word *) TIMER1_CTRL;
  554. *q = 1;
  555. // wait until timer done
  556. while (timer1Value == 0);
  557. }
  558. // Returns milliseconds since last reset
  559. word millis()
  560. {
  561. word retval = 0;
  562. asm(
  563. "load32 0xC0274A r2\n" // millis addr
  564. "read 0 r2 r2\n" // read millis
  565. "write -4 r14 r2\n" // write to stack to return
  566. );
  567. return retval;
  568. }
  569. // Returns interrupt ID by using the readintid asm instruction
  570. word getIntID()
  571. {
  572. word retval = 0;
  573. asm(
  574. "readintid r2 ;reads interrupt id to r2\n"
  575. "write -4 r14 r2 ;write to stack to return\n"
  576. );
  577. return retval;
  578. }
  579. // Converts char c to uppercase if possible
  580. char toUpper(char c)
  581. {
  582. if (c>96 && c<123)
  583. c = c ^ 0x20;
  584. return c;
  585. }
  586. // Converts string str to uppercase if possible
  587. void strToUpper(char* str)
  588. {
  589. char chr = *str; // first character of str
  590. while (chr != 0) // continue until null value
  591. {
  592. *str = toUpper(chr); // uppercase char
  593. str++; // go to next character address
  594. chr = *str; // get character from address
  595. }
  596. }
  597. /*
  598. For debugging
  599. Prints a hex dump of size 'len' for each word starting from 'addr'
  600. Values are printed over UART
  601. */
  602. void hexdump(char* addr, word len, word words_per_line)
  603. {
  604. char buf[16];
  605. word i;
  606. for (i = 0; i < len; i++)
  607. {
  608. // newline every words_per_line words
  609. if (i != 0 && MATH_modU(i, words_per_line) == 0)
  610. uprintc('\n');
  611. itoah(addr[i], buf);
  612. uprint(buf);
  613. uprintc(' ');
  614. }
  615. }