1
0

STDLIB.C 8.9 KB

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