1
0

STDLIB.C 11 KB

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