1
0

STDLIB.C 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. Recursive helper function for itoa
  143. Eventually returns the number of digits in n
  144. s is the output buffer
  145. */
  146. word itoar(word n, char *s)
  147. {
  148. word digit = MATH_modU(n, 10);
  149. word i = 0;
  150. n = MATH_divU(n,10);
  151. if ((unsigned int) n > 0)
  152. i += itoar(n, s);
  153. s[i++] = digit + '0';
  154. return i;
  155. }
  156. /*
  157. Converts integer n to characters.
  158. The characters are placed in the buffer s.
  159. The buffer is terminated with a 0 value.
  160. Uses recursion, division and mod to compute.
  161. */
  162. void itoa(word n, char *s)
  163. {
  164. // compute and fill the buffer
  165. word i = itoar(n, s);
  166. // end with terminator
  167. s[i] = 0;
  168. }
  169. /*
  170. Recursive helper function for itoa
  171. Eventually returns the number of digits in n
  172. s is the output buffer
  173. */
  174. word itoahr(word n, char *s)
  175. {
  176. word digit = MATH_modU(n, 16);
  177. word i = 0;
  178. n = MATH_divU(n,16);
  179. if ((unsigned int) n > 0)
  180. i += itoahr(n, s);
  181. char c;
  182. if (digit > 9)
  183. {
  184. c = digit + 'A' - 10;
  185. }
  186. else
  187. {
  188. c = digit + '0';
  189. }
  190. s[i++] = c;
  191. return i;
  192. }
  193. /*
  194. Converts integer n to hex string characters.
  195. The characters are placed in the buffer s.
  196. A prefix of 0x is added.
  197. The buffer is terminated with a 0 value.
  198. Uses recursion, division and mod to compute.
  199. */
  200. void itoah(word n, char *s)
  201. {
  202. // add prefix
  203. s[0] = '0';
  204. s[1] = 'x';
  205. s+=2;
  206. // compute and fill the buffer
  207. word i = itoahr(n, s);
  208. // end with terminator
  209. s[i] = 0;
  210. }
  211. // isalpha
  212. word isalpha(char c)
  213. {
  214. if (c >= 'A' && c <= 'Z')
  215. return 2;
  216. if (c >= 'a' && c <= 'z')
  217. return 1;
  218. return 0;
  219. }
  220. // isdigit
  221. word isdigit(char c)
  222. {
  223. if (c >= '0' && c <= '9')
  224. return 1;
  225. return 0;
  226. }
  227. // isalnum
  228. word isalnum(char c)
  229. {
  230. if (isdigit(c) || isalpha(c))
  231. return 1;
  232. return 0;
  233. }
  234. /*
  235. Converts string into int.
  236. Assumes the string is valid.
  237. */
  238. word strToInt(char* str)
  239. {
  240. word retval = 0;
  241. word multiplier = 1;
  242. word i = 0;
  243. while (str[i] != 0)
  244. {
  245. i++;
  246. }
  247. if (i == 0)
  248. return 0;
  249. i--;
  250. while (i > 0)
  251. {
  252. // Return 0 if not a digit
  253. if (str[i] < '0' || str[i] > '9')
  254. return 0;
  255. word currentDigit = str[i] - '0';
  256. word toAdd = multiplier * currentDigit;
  257. retval += toAdd;
  258. multiplier = multiplier * 10;
  259. i--;
  260. }
  261. word currentDigit = str[i] - '0';
  262. word toAdd = multiplier * currentDigit;
  263. retval += toAdd;
  264. return retval;
  265. }
  266. /*
  267. Prints a single char c by writing it to UART_TX_ADDR
  268. */
  269. void uprintc(char c)
  270. {
  271. word *p = (word *)UART_TX_ADDR; // address of UART TX
  272. *p = (word)c; // write char over UART
  273. }
  274. /*
  275. Sends each character from str over UART
  276. by writing them to UART_TX_ADDR
  277. until a 0 value is found.
  278. Does not send a newline afterwards.
  279. */
  280. void uprint(char* str)
  281. {
  282. word *p = (word *)UART_TX_ADDR; // address of UART TX
  283. char chr = *str; // first character of str
  284. while (chr != 0) // continue until null value
  285. {
  286. *p = (word)chr; // write char over UART
  287. str++; // go to next character address
  288. chr = *str; // get character from address
  289. }
  290. }
  291. /*
  292. Same as uprint(char* str),
  293. except it sends a newline afterwards.
  294. */
  295. void uprintln(char* str)
  296. {
  297. uprint(str);
  298. uprintc('\n');
  299. }
  300. /*
  301. Prints decimal integer over UART
  302. */
  303. void uprintDec(word i)
  304. {
  305. char buffer[11];
  306. itoa(i, buffer);
  307. uprint(buffer);
  308. uprintc('\n');
  309. }
  310. /*
  311. Prints hex integer over UART
  312. */
  313. void uprintHex(word i)
  314. {
  315. char buffer[11];
  316. itoah(i, buffer);
  317. uprint(buffer);
  318. uprintc('\n');
  319. }
  320. /*
  321. Prints decimal integer over UART, with newline
  322. */
  323. void uprintlnDec(word i)
  324. {
  325. char buffer[11];
  326. itoa(i, buffer);
  327. uprint(buffer);
  328. uprintc('\n');
  329. }
  330. /*
  331. Prints hex integer over UART, with newline
  332. */
  333. void uprintlnHex(word i)
  334. {
  335. char buffer[11];
  336. itoah(i, buffer);
  337. uprint(buffer);
  338. uprintc('\n');
  339. }
  340. // sleeps ms using timer1.
  341. // blocking.
  342. // requires int1() to set timer1Value to 1:
  343. /*
  344. timer1Value = 1; // notify ending of timer1
  345. */
  346. void delay(word ms)
  347. {
  348. // clear result
  349. timer1Value = 0;
  350. // set timer
  351. word *p = (word *) TIMER1_VAL;
  352. *p = ms;
  353. // start timer
  354. word *q = (word *) TIMER1_CTRL;
  355. *q = 1;
  356. // wait until timer done
  357. while (timer1Value == 0);
  358. }
  359. // Returns interrupt ID by using the readintid asm instruction
  360. word getIntID()
  361. {
  362. word retval = 0;
  363. asm(
  364. "readintid r2 ;reads interrupt id to r2\n"
  365. "write -4 r14 r2 ;write to stack to return\n"
  366. );
  367. return retval;
  368. }
  369. // Converts char c to uppercase if possible
  370. char toUpper(char c)
  371. {
  372. if (c>96 && c<123)
  373. c = c ^ 0x20;
  374. return c;
  375. }
  376. // Converts string str to uppercase if possible
  377. void strToUpper(char* str)
  378. {
  379. char chr = *str; // first character of str
  380. while (chr != 0) // continue until null value
  381. {
  382. *str = toUpper(chr); // uppercase char
  383. str++; // go to next character address
  384. chr = *str; // get character from address
  385. }
  386. }
  387. /*
  388. For debugging
  389. Prints a hex dump of size 'len' for each word starting from 'addr'
  390. Values are printed over UART
  391. */
  392. void hexdump(char* addr, word len)
  393. {
  394. char buf[16];
  395. word i;
  396. for (i = 0; i < len; i++)
  397. {
  398. // newline every 8 words
  399. if (i != 0 && MATH_modU(i, 8) == 0)
  400. uprintc('\n');
  401. itoah(addr[i], buf);
  402. uprint(buf);
  403. uprintc(' ');
  404. }
  405. }