STDLIB.C 9.8 KB

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