stdlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. Compares n words between a and b
  34. Returns 1 if similar, 0 otherwise
  35. */
  36. word memcmp(word* a, word* b, word n)
  37. {
  38. word i;
  39. for (i = 0; i < n; i++)
  40. {
  41. if (a[i] != b[i])
  42. {
  43. return 0;
  44. }
  45. }
  46. return 1;
  47. }
  48. /*
  49. Sets n words from dest to val
  50. */
  51. void memset(word* dest, word val, word n)
  52. {
  53. word i;
  54. for (i = 0; i < n; i++)
  55. {
  56. dest[i] = val;
  57. }
  58. }
  59. // Returns length of string
  60. word strlen(char* str)
  61. {
  62. word retval = 0;
  63. char chr = *str; // first character of str
  64. while (chr != 0) // continue until null value
  65. {
  66. retval += 1;
  67. str++; // go to next character address
  68. chr = *str; // get character from address
  69. }
  70. return retval;
  71. }
  72. /*
  73. Copies string from src to dest
  74. Returns number of characters copied
  75. */
  76. word strcpy(char* dest, char* src)
  77. {
  78. // write to buffer
  79. word i = 0;
  80. while (src[i] != 0)
  81. {
  82. dest[i] = src[i];
  83. i++;
  84. }
  85. // terminate
  86. dest[i] = 0;
  87. return i;
  88. }
  89. /*
  90. Appends string from src to dest
  91. Returns number of characters appended
  92. */
  93. word strcat(char* dest, char* src)
  94. {
  95. // move to end of destination
  96. word endOfDest = 0;
  97. while (dest[endOfDest] != 0)
  98. endOfDest++;
  99. // copy to end of destination
  100. return strcpy(dest+endOfDest, src);
  101. }
  102. /*
  103. Appends string from src to dest up to n characters
  104. Returns pointer to resulting dest string
  105. */
  106. char *strncat(char *dest, char *src, word n)
  107. {
  108. word len1 = strlen(dest);
  109. word len2 = strlen(src);
  110. if (len2 < n)
  111. {
  112. strcpy(&dest[len1], src);
  113. }
  114. else
  115. {
  116. strncpy(&dest[len1], src, n);
  117. dest[len1 + n] = '\0';
  118. }
  119. return dest;
  120. }
  121. /*
  122. Compares two strings a and b
  123. Returns 0 if similar
  124. otherwise returns the difference in the first non-matching character
  125. */
  126. word strcmp(char* s1, char* s2)
  127. {
  128. while(*s1 && (*s1 == *s2))
  129. {
  130. s1++;
  131. s2++;
  132. }
  133. return *s1 - *s2;
  134. }
  135. /*
  136. Returns a pointer to the first occurrence of the character c in the string s, or 0 if the character is not found.
  137. */
  138. char* strchr (const char *s, char c)
  139. {
  140. do {
  141. if (*s == c)
  142. {
  143. return (char*)s;
  144. }
  145. } while (*s++);
  146. return 0;
  147. }
  148. /*
  149. Returns a pointer to the last occurance of a character, or 0 if the character is not found.
  150. */
  151. char* strrchr (const char *s, int c)
  152. {
  153. char *rtnval = 0;
  154. do {
  155. if (*s == c)
  156. rtnval = (char*) s;
  157. } while (*s++);
  158. return (rtnval);
  159. }
  160. char * strtok_old_str;
  161. /*
  162. Parse str into tokens separated by characters in delim.
  163. If S is NULL, the last string strtok() was called with is used.
  164. Note that strtok() modifies the input string.
  165. For example:
  166. char s[] = "-abc-=-def";
  167. x = strtok(s, "-"); // x = "abc"
  168. x = strtok(NULL, "-="); // x = "def"
  169. x = strtok(NULL, "="); // x = NULL
  170. // s = "abc\0=-def\0"
  171. */
  172. char* strtok(char* str, const char* delim)
  173. {
  174. if (str != (word*)-1)
  175. strtok_old_str = str;
  176. if (strtok_old_str == (word*)-1)
  177. return (word*)-1;
  178. // Return reached end of string
  179. if (*strtok_old_str == 0)
  180. {
  181. return (word*)-1;
  182. }
  183. // Skip leading delimiters
  184. while (strchr(delim, *strtok_old_str) != 0)
  185. strtok_old_str++;
  186. // Find end of token
  187. char* start = strtok_old_str;
  188. while (*strtok_old_str != 0 && strchr(delim, *strtok_old_str) == 0)
  189. strtok_old_str++;
  190. if (*strtok_old_str == 0)
  191. {
  192. strtok_old_str = (word*)-1;
  193. return start;
  194. }
  195. *strtok_old_str = 0;
  196. strtok_old_str++;
  197. return start;
  198. }
  199. /*
  200. Compress a string made of one char per word, into a string made of one char per byte.
  201. */
  202. void strcompress(word* dest, char* src)
  203. {
  204. word i_src = 0;
  205. word i_dst = 0;
  206. word byte_offset = 0;
  207. word c = src[i_src];
  208. while (c != 0)
  209. {
  210. dest[i_dst] |= (c << byte_offset);
  211. if (byte_offset == 24)
  212. {
  213. byte_offset = 0;
  214. i_dst++;
  215. dest[i_dst] = 0;
  216. }
  217. else
  218. {
  219. byte_offset += 8;
  220. }
  221. i_src++;
  222. c = src[i_src];
  223. }
  224. }
  225. /*
  226. Decompress a string made of one char per byte, into a string made of one char per word.
  227. */
  228. void strdecompress(char* dest, word* src)
  229. {
  230. word i_src = 0;
  231. word i_dst = 0;
  232. word byte_offset = 0;
  233. while (1)
  234. {
  235. word c = (src[i_src] >> byte_offset) & 0xFF;
  236. if (c == 0)
  237. break;
  238. dest[i_dst] = c;
  239. i_dst++;
  240. if (byte_offset == 24)
  241. {
  242. byte_offset = 0;
  243. i_src++;
  244. }
  245. else
  246. {
  247. byte_offset += 8;
  248. }
  249. }
  250. // Terminate
  251. dest[i_dst] = 0;
  252. }
  253. /**
  254. * Return the basename of a path
  255. * path: full path
  256. */
  257. char* basename(char *path)
  258. {
  259. char *base = strrchr(path, '/');
  260. return base ? base + 1 : path;
  261. }
  262. /**
  263. * Return the dirname of a path
  264. * output: buffer to store the dirname
  265. * path: full path
  266. */
  267. char* dirname(char* output, char *path)
  268. {
  269. strcpy(output, path);
  270. char *last_slash = strrchr(output, '/');
  271. if (last_slash != 0)
  272. {
  273. *last_slash = 0;
  274. // If the last slash is the first character, return "/"
  275. if (last_slash == output)
  276. {
  277. strcpy(output, "/");
  278. }
  279. } else
  280. {
  281. // No slash found, return "."
  282. strcpy(output, ".");
  283. }
  284. return output;
  285. }
  286. /*
  287. Recursive helper function for itoa
  288. Eventually returns the number of digits in n
  289. s is the output buffer
  290. */
  291. word itoar(word n, char *s)
  292. {
  293. word digit = MATH_modU(n, 10);
  294. word i = 0;
  295. n = MATH_divU(n,10);
  296. if ((unsigned int) n > 0)
  297. i += itoar(n, s);
  298. s[i++] = digit + '0';
  299. return i;
  300. }
  301. /*
  302. Converts integer n to characters.
  303. The characters are placed in the buffer s.
  304. The buffer is terminated with a 0 value.
  305. Uses recursion, division and mod to compute.
  306. */
  307. void itoa(word n, char *s)
  308. {
  309. // compute and fill the buffer
  310. word i = itoar(n, s);
  311. // end with terminator
  312. s[i] = 0;
  313. }
  314. /*
  315. Recursive helper function for itoa
  316. Eventually returns the number of digits in n
  317. s is the output buffer
  318. */
  319. word itoahr(word n, char *s)
  320. {
  321. word digit = MATH_modU(n, 16);
  322. word i = 0;
  323. n = MATH_divU(n,16);
  324. if ((unsigned int) n > 0)
  325. i += itoahr(n, s);
  326. char c;
  327. if (digit > 9)
  328. {
  329. c = digit + 'A' - 10;
  330. }
  331. else
  332. {
  333. c = digit + '0';
  334. }
  335. s[i++] = c;
  336. return i;
  337. }
  338. /*
  339. Converts integer n to hex string characters.
  340. The characters are placed in the buffer s.
  341. A prefix of 0x is added.
  342. The buffer is terminated with a 0 value.
  343. Uses recursion, division and mod to compute.
  344. */
  345. void itoah(word n, char *s)
  346. {
  347. // add prefix
  348. s[0] = '0';
  349. s[1] = 'x';
  350. s+=2;
  351. // compute and fill the buffer
  352. word i = itoahr(n, s);
  353. // end with terminator
  354. s[i] = 0;
  355. }
  356. // isalpha
  357. word isalpha(char c)
  358. {
  359. if (c >= 'A' && c <= 'Z')
  360. return 2;
  361. if (c >= 'a' && c <= 'z')
  362. return 1;
  363. return 0;
  364. }
  365. // isdigit
  366. word isdigit(char c)
  367. {
  368. if (c >= '0' && c <= '9')
  369. return 1;
  370. return 0;
  371. }
  372. // isalnum
  373. word isalnum(char c)
  374. {
  375. if (isdigit(c) || isalpha(c))
  376. return 1;
  377. return 0;
  378. }
  379. /*
  380. Converts string into int.
  381. Assumes the string is valid.
  382. */
  383. word strToInt(char* str)
  384. {
  385. word retval = 0;
  386. word multiplier = 1;
  387. word i = 0;
  388. while (str[i] != 0)
  389. {
  390. i++;
  391. }
  392. if (i == 0)
  393. return 0;
  394. i--;
  395. while (i > 0)
  396. {
  397. // Return 0 if not a digit
  398. if (str[i] < '0' || str[i] > '9')
  399. return 0;
  400. word currentDigit = str[i] - '0';
  401. word toAdd = multiplier * currentDigit;
  402. retval += toAdd;
  403. multiplier = multiplier * 10;
  404. i--;
  405. }
  406. // Check for negative
  407. if (str[i] == '-')
  408. {
  409. retval *= -1;
  410. }
  411. else
  412. {
  413. word currentDigit = str[i] - '0';
  414. word toAdd = multiplier * currentDigit;
  415. retval += toAdd;
  416. }
  417. return retval;
  418. }
  419. /*
  420. Prints a single char c by writing it to UART_TX_ADDR
  421. */
  422. void uprintc(char c)
  423. {
  424. word *p = (word *)UART_TX_ADDR; // address of UART TX
  425. *p = (word)c; // write char over UART
  426. }
  427. /*
  428. Sends each character from str over UART
  429. by writing them to UART_TX_ADDR
  430. until a 0 value is found.
  431. Does not send a newline afterwards.
  432. */
  433. void uprint(char* str)
  434. {
  435. word *p = (word *)UART_TX_ADDR; // address of UART TX
  436. char chr = *str; // first character of str
  437. while (chr != 0) // continue until null value
  438. {
  439. *p = (word)chr; // write char over UART
  440. str++; // go to next character address
  441. chr = *str; // get character from address
  442. }
  443. }
  444. /*
  445. Same as uprint(char* str),
  446. except it sends a newline afterwards.
  447. */
  448. void uprintln(char* str)
  449. {
  450. uprint(str);
  451. uprintc('\n');
  452. }
  453. /*
  454. Prints decimal integer over UART
  455. */
  456. void uprintDec(word i)
  457. {
  458. char buffer[20];
  459. itoa(i, buffer);
  460. uprint(buffer);
  461. }
  462. /*
  463. Prints hex integer over UART
  464. */
  465. void uprintHex(word i)
  466. {
  467. char buffer[16];
  468. itoah(i, buffer);
  469. uprint(buffer);
  470. }
  471. /*
  472. Prints decimal integer over UART, with newline
  473. */
  474. void uprintlnDec(word i)
  475. {
  476. char buffer[20];
  477. itoa(i, buffer);
  478. uprint(buffer);
  479. uprintc('\n');
  480. }
  481. /*
  482. Prints hex integer over UART, with newline
  483. */
  484. void uprintlnHex(word i)
  485. {
  486. char buffer[16];
  487. itoah(i, buffer);
  488. uprint(buffer);
  489. uprintc('\n');
  490. }
  491. // sleeps ms using timer1.
  492. // blocking.
  493. // requires int1() to set timer1Value to 1:
  494. /*
  495. timer1Value = 1; // notify ending of timer1
  496. */
  497. void delay(word ms)
  498. {
  499. // clear result
  500. timer1Value = 0;
  501. // set timer
  502. word *p = (word *) TIMER1_VAL;
  503. *p = ms;
  504. // start timer
  505. word *q = (word *) TIMER1_CTRL;
  506. *q = 1;
  507. // wait until timer done
  508. while (timer1Value == 0);
  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. }