STDLIB.C 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. char* memmove(char* dest, const char* src, word n)
  33. {
  34. char* from = src;
  35. char* to = dest;
  36. if (from == to || n == 0)
  37. return dest;
  38. if (to > from && to-from < (word)n)
  39. {
  40. /* to overlaps with from */
  41. /* <from......> */
  42. /* <to........> */
  43. /* copy in reverse, to avoid overwriting from */
  44. word i;
  45. for(i=n-1; i>=0; i--)
  46. to[i] = from[i];
  47. return dest;
  48. }
  49. if (from > to && from-to < (word)n)
  50. {
  51. /* to overlaps with from */
  52. /* <from......> */
  53. /* <to........> */
  54. /* copy forwards, to avoid overwriting from */
  55. word i;
  56. for(i=0; i<n; i++)
  57. to[i] = from[i];
  58. return dest;
  59. }
  60. memcpy(dest, src, n);
  61. return dest;
  62. }
  63. /*
  64. Compares n words between a and b
  65. Returns 1 if similar, 0 otherwise
  66. */
  67. word memcmp(word* a, word* b, word n)
  68. {
  69. word i;
  70. for (i = 0; i < n; i++)
  71. {
  72. if (a[i] != b[i])
  73. {
  74. return 0;
  75. }
  76. }
  77. return 1;
  78. }
  79. // Returns length of string
  80. word strlen(char* str)
  81. {
  82. word retval = 0;
  83. char chr = *str; // first character of str
  84. while (chr != 0) // continue until null value
  85. {
  86. retval += 1;
  87. str++; // go to next character address
  88. chr = *str; // get character from address
  89. }
  90. return retval;
  91. }
  92. /*
  93. Copies string from src to dest
  94. Returns number of characters copied
  95. */
  96. word strcpy(char* dest, char* src)
  97. {
  98. // write to buffer
  99. word i = 0;
  100. while (src[i] != 0)
  101. {
  102. dest[i] = src[i];
  103. i++;
  104. }
  105. // terminate
  106. dest[i] = 0;
  107. return i;
  108. }
  109. /*
  110. Appends string from src to dest
  111. Returns number of characters appended
  112. */
  113. word strcat(char* dest, char* src)
  114. {
  115. // move to end of destination
  116. word endOfDest = 0;
  117. while (dest[endOfDest] != 0)
  118. endOfDest++;
  119. // copy to end of destination
  120. return strcpy(dest+endOfDest, src);
  121. }
  122. /*
  123. Compares two strings a and b
  124. Returns 1 if similar, 0 otherwise
  125. */
  126. word strcmp(char* a, char* b)
  127. {
  128. if (strlen(a) != strlen(b))
  129. return 0;
  130. word i = 0;
  131. while (a[i] != 0)
  132. {
  133. if (a[i] != b[i])
  134. {
  135. return 0;
  136. }
  137. i++;
  138. }
  139. return 1;
  140. }
  141. /*
  142. Returns a pointer to the first occurrence of the character c in the string s, or 0 if the character is not found.
  143. */
  144. char* strchr (const char *s, char c)
  145. {
  146. do {
  147. if (*s == c)
  148. {
  149. return (char*)s;
  150. }
  151. } while (*s++);
  152. return 0;
  153. }
  154. /*
  155. Recursive helper function for itoa
  156. Eventually returns the number of digits in n
  157. s is the output buffer
  158. */
  159. word itoar(word n, char *s)
  160. {
  161. word digit = MATH_modU(n, 10);
  162. word i = 0;
  163. n = MATH_divU(n,10);
  164. if ((unsigned int) n > 0)
  165. i += itoar(n, s);
  166. s[i++] = digit + '0';
  167. return i;
  168. }
  169. /*
  170. Converts integer n to characters.
  171. The characters are placed in the buffer s.
  172. The buffer is terminated with a 0 value.
  173. Uses recursion, division and mod to compute.
  174. */
  175. void itoa(word n, char *s)
  176. {
  177. // compute and fill the buffer
  178. word i = itoar(n, s);
  179. // end with terminator
  180. s[i] = 0;
  181. }
  182. /*
  183. Recursive helper function for itoa
  184. Eventually returns the number of digits in n
  185. s is the output buffer
  186. */
  187. word itoahr(word n, char *s)
  188. {
  189. word digit = MATH_modU(n, 16);
  190. word i = 0;
  191. n = MATH_divU(n,16);
  192. if ((unsigned int) n > 0)
  193. i += itoahr(n, s);
  194. char c;
  195. if (digit > 9)
  196. {
  197. c = digit + 'A' - 10;
  198. }
  199. else
  200. {
  201. c = digit + '0';
  202. }
  203. s[i++] = c;
  204. return i;
  205. }
  206. /*
  207. Converts integer n to hex string characters.
  208. The characters are placed in the buffer s.
  209. A prefix of 0x is added.
  210. The buffer is terminated with a 0 value.
  211. Uses recursion, division and mod to compute.
  212. */
  213. void itoah(word n, char *s)
  214. {
  215. // add prefix
  216. s[0] = '0';
  217. s[1] = 'x';
  218. s+=2;
  219. // compute and fill the buffer
  220. word i = itoahr(n, s);
  221. // end with terminator
  222. s[i] = 0;
  223. }
  224. // isalpha
  225. word isalpha(char c)
  226. {
  227. if (c >= 'A' && c <= 'Z')
  228. return 2;
  229. if (c >= 'a' && c <= 'z')
  230. return 1;
  231. return 0;
  232. }
  233. // isdigit
  234. word isdigit(char c)
  235. {
  236. if (c >= '0' && c <= '9')
  237. return 1;
  238. return 0;
  239. }
  240. // isalnum
  241. word isalnum(char c)
  242. {
  243. if (isdigit(c) || isalpha(c))
  244. return 1;
  245. return 0;
  246. }
  247. /*
  248. Converts string into int.
  249. Assumes the string is valid.
  250. */
  251. word strToInt(char* str)
  252. {
  253. word retval = 0;
  254. word multiplier = 1;
  255. word i = 0;
  256. while (str[i] != 0)
  257. {
  258. i++;
  259. }
  260. if (i == 0)
  261. return 0;
  262. i--;
  263. while (i > 0)
  264. {
  265. // Return 0 if not a digit
  266. if (str[i] < '0' || str[i] > '9')
  267. return 0;
  268. word currentDigit = str[i] - '0';
  269. word toAdd = multiplier * currentDigit;
  270. retval += toAdd;
  271. multiplier = multiplier * 10;
  272. i--;
  273. }
  274. // Check for negative
  275. if (str[i] == '-')
  276. {
  277. retval *= -1;
  278. }
  279. else
  280. {
  281. word currentDigit = str[i] - '0';
  282. word toAdd = multiplier * currentDigit;
  283. retval += toAdd;
  284. }
  285. return retval;
  286. }
  287. /*
  288. Speed optimized function to get the number of decimals for a given digit
  289. */
  290. word numberOfDecimals(word n)
  291. {
  292. if (n < 0) n = -n; // Ignore for now the INT_MIN case where this does not work
  293. if (n < 10) return 1;
  294. if (n < 100) return 2;
  295. if (n < 1000) return 3;
  296. if (n < 10000) return 4;
  297. if (n < 100000) return 5;
  298. if (n < 1000000) return 6;
  299. if (n < 10000000) return 7;
  300. if (n < 100000000) return 8;
  301. if (n < 1000000000) return 9;
  302. // Cannot be > 10 for a 32bit integer
  303. return 10;
  304. }
  305. /*
  306. Prints a single char c by writing it to UART_TX_ADDR
  307. */
  308. void uprintc(char c)
  309. {
  310. word *p = (word *)UART_TX_ADDR; // address of UART TX
  311. *p = (word)c; // write char over UART
  312. }
  313. /*
  314. Sends each character from str over UART
  315. by writing them to UART_TX_ADDR
  316. until a 0 value is found.
  317. Does not send a newline afterwards.
  318. */
  319. void uprint(char* str)
  320. {
  321. word *p = (word *)UART_TX_ADDR; // address of UART TX
  322. char chr = *str; // first character of str
  323. while (chr != 0) // continue until null value
  324. {
  325. *p = (word)chr; // write char over UART
  326. str++; // go to next character address
  327. chr = *str; // get character from address
  328. }
  329. }
  330. /*
  331. Same as uprint(char* str),
  332. except it sends a newline afterwards.
  333. */
  334. void uprintln(char* str)
  335. {
  336. uprint(str);
  337. uprintc('\n');
  338. }
  339. /*
  340. Prints decimal integer over UART
  341. */
  342. void uprintDec(word i)
  343. {
  344. char buffer[11];
  345. itoa(i, buffer);
  346. uprint(buffer);
  347. uprintc('\n');
  348. }
  349. /*
  350. Prints hex integer over UART
  351. */
  352. void uprintHex(word i)
  353. {
  354. char buffer[11];
  355. itoah(i, buffer);
  356. uprint(buffer);
  357. uprintc('\n');
  358. }
  359. /*
  360. Prints decimal integer over UART, with newline
  361. */
  362. void uprintlnDec(word i)
  363. {
  364. char buffer[11];
  365. itoa(i, buffer);
  366. uprint(buffer);
  367. uprintc('\n');
  368. }
  369. /*
  370. Prints hex integer over UART, with newline
  371. */
  372. void uprintlnHex(word i)
  373. {
  374. char buffer[11];
  375. itoah(i, buffer);
  376. uprint(buffer);
  377. uprintc('\n');
  378. }
  379. // sleeps ms using timer1.
  380. // blocking.
  381. // requires int1() to set timer1Value to 1:
  382. /*
  383. timer1Value = 1; // notify ending of timer1
  384. */
  385. void delay(word ms)
  386. {
  387. // clear result
  388. timer1Value = 0;
  389. // set timer
  390. word *p = (word *) TIMER1_VAL;
  391. *p = ms;
  392. // start timer
  393. word *q = (word *) TIMER1_CTRL;
  394. *q = 1;
  395. // wait until timer done
  396. while (timer1Value == 0);
  397. }
  398. // Returns interrupt ID by using the readintid asm instruction
  399. word getIntID()
  400. {
  401. word retval = 0;
  402. asm(
  403. "readintid r2 ;reads interrupt id to r2\n"
  404. "write -4 r14 r2 ;write to stack to return\n"
  405. );
  406. return retval;
  407. }
  408. // Converts char c to uppercase if possible
  409. char toUpper(char c)
  410. {
  411. if (c>96 && c<123)
  412. c = c ^ 0x20;
  413. return c;
  414. }
  415. // Converts string str to uppercase if possible
  416. void strToUpper(char* str)
  417. {
  418. char chr = *str; // first character of str
  419. while (chr != 0) // continue until null value
  420. {
  421. *str = toUpper(chr); // uppercase char
  422. str++; // go to next character address
  423. chr = *str; // get character from address
  424. }
  425. }
  426. /*
  427. For debugging
  428. Prints a hex dump of size 'len' for each word starting from 'addr'
  429. Values are printed over UART
  430. */
  431. void hexdump(char* addr, word len)
  432. {
  433. char buf[16];
  434. word i;
  435. for (i = 0; i < len; i++)
  436. {
  437. // newline every 8 words
  438. if (i != 0 && MATH_modU(i, 8) == 0)
  439. uprintc('\n');
  440. itoah(addr[i], buf);
  441. uprint(buf);
  442. uprintc(' ');
  443. }
  444. }