1
0

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. if (byte_offset == 24)
  240. {
  241. byte_offset = 0;
  242. i_src++;
  243. }
  244. else
  245. {
  246. byte_offset += 8;
  247. }
  248. }
  249. // Terminate
  250. dest[i_dst] = 0;
  251. }
  252. /**
  253. * Return the basename of a path
  254. * path: full path
  255. */
  256. char* basename(char *path)
  257. {
  258. char *base = strrchr(path, '/');
  259. return base ? base + 1 : path;
  260. }
  261. /**
  262. * Return the dirname of a path
  263. * output: buffer to store the dirname
  264. * path: full path
  265. */
  266. char* dirname(char* output, char *path)
  267. {
  268. strcpy(output, path);
  269. char *last_slash = strrchr(output, '/');
  270. if (last_slash != 0)
  271. {
  272. *last_slash = 0;
  273. // If the last slash is the first character, return "/"
  274. if (last_slash == output)
  275. {
  276. strcpy(output, "/");
  277. }
  278. } else
  279. {
  280. // No slash found, return "."
  281. strcpy(output, ".");
  282. }
  283. return output;
  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 itoar(word n, char *s)
  291. {
  292. word digit = MATH_modU(n, 10);
  293. word i = 0;
  294. n = MATH_divU(n,10);
  295. if ((unsigned int) n > 0)
  296. i += itoar(n, s);
  297. s[i++] = digit + '0';
  298. return i;
  299. }
  300. /*
  301. Converts integer n to characters.
  302. The characters are placed in the buffer s.
  303. The buffer is terminated with a 0 value.
  304. Uses recursion, division and mod to compute.
  305. */
  306. void itoa(word n, char *s)
  307. {
  308. // compute and fill the buffer
  309. word i = itoar(n, s);
  310. // end with terminator
  311. s[i] = 0;
  312. }
  313. /*
  314. Recursive helper function for itoa
  315. Eventually returns the number of digits in n
  316. s is the output buffer
  317. */
  318. word itoahr(word n, char *s)
  319. {
  320. word digit = MATH_modU(n, 16);
  321. word i = 0;
  322. n = MATH_divU(n,16);
  323. if ((unsigned int) n > 0)
  324. i += itoahr(n, s);
  325. char c;
  326. if (digit > 9)
  327. {
  328. c = digit + 'A' - 10;
  329. }
  330. else
  331. {
  332. c = digit + '0';
  333. }
  334. s[i++] = c;
  335. return i;
  336. }
  337. /*
  338. Converts integer n to hex string characters.
  339. The characters are placed in the buffer s.
  340. A prefix of 0x is added.
  341. The buffer is terminated with a 0 value.
  342. Uses recursion, division and mod to compute.
  343. */
  344. void itoah(word n, char *s)
  345. {
  346. // add prefix
  347. s[0] = '0';
  348. s[1] = 'x';
  349. s+=2;
  350. // compute and fill the buffer
  351. word i = itoahr(n, s);
  352. // end with terminator
  353. s[i] = 0;
  354. }
  355. // isalpha
  356. word isalpha(char c)
  357. {
  358. if (c >= 'A' && c <= 'Z')
  359. return 2;
  360. if (c >= 'a' && c <= 'z')
  361. return 1;
  362. return 0;
  363. }
  364. // isdigit
  365. word isdigit(char c)
  366. {
  367. if (c >= '0' && c <= '9')
  368. return 1;
  369. return 0;
  370. }
  371. // isalnum
  372. word isalnum(char c)
  373. {
  374. if (isdigit(c) || isalpha(c))
  375. return 1;
  376. return 0;
  377. }
  378. /*
  379. Converts string into int.
  380. Assumes the string is valid.
  381. */
  382. word strToInt(char* str)
  383. {
  384. word retval = 0;
  385. word multiplier = 1;
  386. word i = 0;
  387. while (str[i] != 0)
  388. {
  389. i++;
  390. }
  391. if (i == 0)
  392. return 0;
  393. i--;
  394. while (i > 0)
  395. {
  396. // Return 0 if not a digit
  397. if (str[i] < '0' || str[i] > '9')
  398. return 0;
  399. word currentDigit = str[i] - '0';
  400. word toAdd = multiplier * currentDigit;
  401. retval += toAdd;
  402. multiplier = multiplier * 10;
  403. i--;
  404. }
  405. // Check for negative
  406. if (str[i] == '-')
  407. {
  408. retval *= -1;
  409. }
  410. else
  411. {
  412. word currentDigit = str[i] - '0';
  413. word toAdd = multiplier * currentDigit;
  414. retval += toAdd;
  415. }
  416. return retval;
  417. }
  418. /*
  419. Prints a single char c by writing it to UART_TX_ADDR
  420. */
  421. void uprintc(char c)
  422. {
  423. word *p = (word *)UART_TX_ADDR; // address of UART TX
  424. *p = (word)c; // write char over UART
  425. }
  426. /*
  427. Sends each character from str over UART
  428. by writing them to UART_TX_ADDR
  429. until a 0 value is found.
  430. Does not send a newline afterwards.
  431. */
  432. void uprint(char* str)
  433. {
  434. word *p = (word *)UART_TX_ADDR; // address of UART TX
  435. char chr = *str; // first character of str
  436. while (chr != 0) // continue until null value
  437. {
  438. *p = (word)chr; // write char over UART
  439. str++; // go to next character address
  440. chr = *str; // get character from address
  441. }
  442. }
  443. /*
  444. Same as uprint(char* str),
  445. except it sends a newline afterwards.
  446. */
  447. void uprintln(char* str)
  448. {
  449. uprint(str);
  450. uprintc('\n');
  451. }
  452. /*
  453. Prints decimal integer over UART
  454. */
  455. void uprintDec(word i)
  456. {
  457. char buffer[20];
  458. itoa(i, buffer);
  459. uprint(buffer);
  460. }
  461. /*
  462. Prints hex integer over UART
  463. */
  464. void uprintHex(word i)
  465. {
  466. char buffer[16];
  467. itoah(i, buffer);
  468. uprint(buffer);
  469. }
  470. /*
  471. Prints decimal integer over UART, with newline
  472. */
  473. void uprintlnDec(word i)
  474. {
  475. char buffer[20];
  476. itoa(i, buffer);
  477. uprint(buffer);
  478. uprintc('\n');
  479. }
  480. /*
  481. Prints hex integer over UART, with newline
  482. */
  483. void uprintlnHex(word i)
  484. {
  485. char buffer[16];
  486. itoah(i, buffer);
  487. uprint(buffer);
  488. uprintc('\n');
  489. }
  490. // sleeps ms using timer1.
  491. // blocking.
  492. // requires int1() to set timer1Value to 1:
  493. /*
  494. timer1Value = 1; // notify ending of timer1
  495. */
  496. void delay(word ms)
  497. {
  498. // clear result
  499. timer1Value = 0;
  500. // set timer
  501. word *p = (word *) TIMER1_VAL;
  502. *p = ms;
  503. // start timer
  504. word *q = (word *) TIMER1_CTRL;
  505. *q = 1;
  506. // wait until timer done
  507. while (timer1Value == 0);
  508. }
  509. // Returns interrupt ID by using the readintid asm instruction
  510. word getIntID()
  511. {
  512. word retval = 0;
  513. asm(
  514. "readintid r2 ;reads interrupt id to r2\n"
  515. "write -4 r14 r2 ;write to stack to return\n"
  516. );
  517. return retval;
  518. }
  519. // Converts char c to uppercase if possible
  520. char toUpper(char c)
  521. {
  522. if (c>96 && c<123)
  523. c = c ^ 0x20;
  524. return c;
  525. }
  526. // Converts string str to uppercase if possible
  527. void strToUpper(char* str)
  528. {
  529. char chr = *str; // first character of str
  530. while (chr != 0) // continue until null value
  531. {
  532. *str = toUpper(chr); // uppercase char
  533. str++; // go to next character address
  534. chr = *str; // get character from address
  535. }
  536. }
  537. /*
  538. For debugging
  539. Prints a hex dump of size 'len' for each word starting from 'addr'
  540. Values are printed over UART
  541. */
  542. void hexdump(char* addr, word len)
  543. {
  544. char buf[16];
  545. word i;
  546. for (i = 0; i < len; i++)
  547. {
  548. // newline every 8 words
  549. if (i != 0 && MATH_modU(i, 8) == 0)
  550. uprintc('\n');
  551. itoah(addr[i], buf);
  552. uprint(buf);
  553. uprintc(' ');
  554. }
  555. }