1
0

STDLIB.C 12 KB

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