1
0

stdlib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. // Returns length of string
  49. word strlen(char* str)
  50. {
  51. word retval = 0;
  52. char chr = *str; // first character of str
  53. while (chr != 0) // continue until null value
  54. {
  55. retval += 1;
  56. str++; // go to next character address
  57. chr = *str; // get character from address
  58. }
  59. return retval;
  60. }
  61. /*
  62. Copies string from src to dest
  63. Returns number of characters copied
  64. */
  65. word strcpy(char* dest, char* src)
  66. {
  67. // write to buffer
  68. word i = 0;
  69. while (src[i] != 0)
  70. {
  71. dest[i] = src[i];
  72. i++;
  73. }
  74. // terminate
  75. dest[i] = 0;
  76. return i;
  77. }
  78. /*
  79. Appends string from src to dest
  80. Returns number of characters appended
  81. */
  82. word strcat(char* dest, char* src)
  83. {
  84. // move to end of destination
  85. word endOfDest = 0;
  86. while (dest[endOfDest] != 0)
  87. endOfDest++;
  88. // copy to end of destination
  89. return strcpy(dest+endOfDest, src);
  90. }
  91. /*
  92. Compares two strings a and b
  93. Returns 0 if similar
  94. otherwise returns the difference in the first non-matching character
  95. */
  96. word strcmp(char* s1, char* s2)
  97. {
  98. while(*s1 && (*s1 == *s2))
  99. {
  100. s1++;
  101. s2++;
  102. }
  103. return *s1 - *s2;
  104. }
  105. /*
  106. Recursive helper function for itoa
  107. Eventually returns the number of digits in n
  108. s is the output buffer
  109. */
  110. word itoar(word n, char *s)
  111. {
  112. word digit = MATH_modU(n, 10);
  113. word i = 0;
  114. n = MATH_divU(n,10);
  115. if ((unsigned int) n > 0)
  116. i += itoar(n, s);
  117. s[i++] = digit + '0';
  118. return i;
  119. }
  120. /*
  121. Converts integer n to characters.
  122. The characters are placed in the buffer s.
  123. The buffer is terminated with a 0 value.
  124. Uses recursion, division and mod to compute.
  125. */
  126. void itoa(word n, char *s)
  127. {
  128. // compute and fill the buffer
  129. word i = itoar(n, s);
  130. // end with terminator
  131. s[i] = 0;
  132. }
  133. /*
  134. Recursive helper function for itoa
  135. Eventually returns the number of digits in n
  136. s is the output buffer
  137. */
  138. word itoahr(word n, char *s)
  139. {
  140. word digit = MATH_modU(n, 16);
  141. word i = 0;
  142. n = MATH_divU(n,16);
  143. if ((unsigned int) n > 0)
  144. i += itoahr(n, s);
  145. char c;
  146. if (digit > 9)
  147. {
  148. c = digit + 'A' - 10;
  149. }
  150. else
  151. {
  152. c = digit + '0';
  153. }
  154. s[i++] = c;
  155. return i;
  156. }
  157. /*
  158. Converts integer n to hex string characters.
  159. The characters are placed in the buffer s.
  160. A prefix of 0x is added.
  161. The buffer is terminated with a 0 value.
  162. Uses recursion, division and mod to compute.
  163. */
  164. void itoah(word n, char *s)
  165. {
  166. // add prefix
  167. s[0] = '0';
  168. s[1] = 'x';
  169. s+=2;
  170. // compute and fill the buffer
  171. word i = itoahr(n, s);
  172. // end with terminator
  173. s[i] = 0;
  174. }
  175. /*
  176. Converts string into int.
  177. Assumes the string is valid.
  178. */
  179. word strToInt(char* str)
  180. {
  181. word retval = 0;
  182. word multiplier = 1;
  183. word i = 0;
  184. while (str[i] != 0)
  185. {
  186. i++;
  187. }
  188. if (i == 0)
  189. return 0;
  190. i--;
  191. while (i > 0)
  192. {
  193. // Return 0 if not a digit
  194. if (str[i] < '0' || str[i] > '9')
  195. return 0;
  196. word currentDigit = str[i] - '0';
  197. word toAdd = multiplier * currentDigit;
  198. retval += toAdd;
  199. multiplier = multiplier * 10;
  200. i--;
  201. }
  202. // Check for negative
  203. if (str[i] == '-')
  204. {
  205. retval *= -1;
  206. }
  207. else
  208. {
  209. word currentDigit = str[i] - '0';
  210. word toAdd = multiplier * currentDigit;
  211. retval += toAdd;
  212. }
  213. return retval;
  214. }
  215. /*
  216. Prints a single char c by writing it to UART_TX_ADDR
  217. */
  218. void uprintc(char c)
  219. {
  220. word *p = (word *)UART_TX_ADDR; // address of UART TX
  221. *p = (word)c; // write char over UART
  222. }
  223. /*
  224. Sends each character from str over UART
  225. by writing them to UART_TX_ADDR
  226. until a 0 value is found.
  227. Does not send a newline afterwards.
  228. */
  229. void uprint(char* str)
  230. {
  231. word *p = (word *)UART_TX_ADDR; // address of UART TX
  232. char chr = *str; // first character of str
  233. while (chr != 0) // continue until null value
  234. {
  235. *p = (word)chr; // write char over UART
  236. str++; // go to next character address
  237. chr = *str; // get character from address
  238. }
  239. }
  240. /*
  241. Same as uprint(char* str),
  242. except it sends a newline afterwards.
  243. */
  244. void uprintln(char* str)
  245. {
  246. uprint(str);
  247. uprintc('\n');
  248. }
  249. /*
  250. Prints decimal integer over UART
  251. */
  252. void uprintDec(word i)
  253. {
  254. char buffer[20];
  255. itoa(i, buffer);
  256. uprint(buffer);
  257. uprintc('\n');
  258. }
  259. /*
  260. Prints hex integer over UART
  261. */
  262. void uprintHex(word i)
  263. {
  264. char buffer[16];
  265. itoah(i, buffer);
  266. uprint(buffer);
  267. uprintc('\n');
  268. }
  269. /*
  270. Prints decimal integer over UART, with newline
  271. */
  272. void uprintlnDec(word i)
  273. {
  274. char buffer[20];
  275. itoa(i, buffer);
  276. uprint(buffer);
  277. uprintc('\n');
  278. }
  279. /*
  280. Prints hex integer over UART, with newline
  281. */
  282. void uprintlnHex(word i)
  283. {
  284. char buffer[16];
  285. itoah(i, buffer);
  286. uprint(buffer);
  287. uprintc('\n');
  288. }
  289. // sleeps ms using timer1.
  290. // blocking.
  291. // requires int1() to set timer1Value to 1:
  292. /*
  293. timer1Value = 1; // notify ending of timer1
  294. */
  295. void delay(word ms)
  296. {
  297. // clear result
  298. timer1Value = 0;
  299. // set timer
  300. word *p = (word *) TIMER1_VAL;
  301. *p = ms;
  302. // start timer
  303. word *q = (word *) TIMER1_CTRL;
  304. *q = 1;
  305. // wait until timer done
  306. while (timer1Value == 0);
  307. }
  308. // Returns interrupt ID by using the readintid asm instruction
  309. word getIntID()
  310. {
  311. word retval = 0;
  312. asm(
  313. "readintid r2 ;reads interrupt id to r2\n"
  314. "write -4 r14 r2 ;write to stack to return\n"
  315. );
  316. return retval;
  317. }
  318. // Converts char c to uppercase if possible
  319. char toUpper(char c)
  320. {
  321. if (c>96 && c<123)
  322. c = c ^ 0x20;
  323. return c;
  324. }
  325. // Converts string str to uppercase if possible
  326. void strToUpper(char* str)
  327. {
  328. char chr = *str; // first character of str
  329. while (chr != 0) // continue until null value
  330. {
  331. *str = toUpper(chr); // uppercase char
  332. str++; // go to next character address
  333. chr = *str; // get character from address
  334. }
  335. }
  336. /*
  337. For debugging
  338. Prints a hex dump of size 'len' for each word starting from 'addr'
  339. Values are printed over UART
  340. */
  341. void hexdump(char* addr, word len)
  342. {
  343. char buf[16];
  344. word i;
  345. for (i = 0; i < len; i++)
  346. {
  347. // newline every 8 words
  348. if (i != 0 && MATH_modU(i, 8) == 0)
  349. uprintc('\n');
  350. itoah(addr[i], buf);
  351. uprint(buf);
  352. uprintc(' ');
  353. }
  354. }