stdlib.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. word currentDigit = str[i] - '0';
  203. word toAdd = multiplier * currentDigit;
  204. retval += toAdd;
  205. return retval;
  206. }
  207. /*
  208. Prints a single char c by writing it to UART_TX_ADDR
  209. */
  210. void uprintc(char c)
  211. {
  212. word *p = (word *)UART_TX_ADDR; // address of UART TX
  213. *p = (word)c; // write char over UART
  214. }
  215. /*
  216. Sends each character from str over UART
  217. by writing them to UART_TX_ADDR
  218. until a 0 value is found.
  219. Does not send a newline afterwards.
  220. */
  221. void uprint(char* str)
  222. {
  223. word *p = (word *)UART_TX_ADDR; // address of UART TX
  224. char chr = *str; // first character of str
  225. while (chr != 0) // continue until null value
  226. {
  227. *p = (word)chr; // write char over UART
  228. str++; // go to next character address
  229. chr = *str; // get character from address
  230. }
  231. }
  232. /*
  233. Same as uprint(char* str),
  234. except it sends a newline afterwards.
  235. */
  236. void uprintln(char* str)
  237. {
  238. uprint(str);
  239. uprintc('\n');
  240. }
  241. /*
  242. Prints decimal integer over UART
  243. */
  244. void uprintDec(word i)
  245. {
  246. char buffer[20];
  247. itoa(i, buffer);
  248. uprint(buffer);
  249. uprintc('\n');
  250. }
  251. /*
  252. Prints hex integer over UART
  253. */
  254. void uprintHex(word i)
  255. {
  256. char buffer[16];
  257. itoah(i, buffer);
  258. uprint(buffer);
  259. uprintc('\n');
  260. }
  261. /*
  262. Prints decimal integer over UART, with newline
  263. */
  264. void uprintlnDec(word i)
  265. {
  266. char buffer[20];
  267. itoa(i, buffer);
  268. uprint(buffer);
  269. uprintc('\n');
  270. }
  271. /*
  272. Prints hex integer over UART, with newline
  273. */
  274. void uprintlnHex(word i)
  275. {
  276. char buffer[16];
  277. itoah(i, buffer);
  278. uprint(buffer);
  279. uprintc('\n');
  280. }
  281. // sleeps ms using timer1.
  282. // blocking.
  283. // requires int1() to set timer1Value to 1:
  284. /*
  285. timer1Value = 1; // notify ending of timer1
  286. */
  287. void delay(word ms)
  288. {
  289. // clear result
  290. timer1Value = 0;
  291. // set timer
  292. word *p = (word *) TIMER1_VAL;
  293. *p = ms;
  294. // start timer
  295. word *q = (word *) TIMER1_CTRL;
  296. *q = 1;
  297. // wait until timer done
  298. while (timer1Value == 0);
  299. }
  300. // Returns interrupt ID by using the readintid asm instruction
  301. word getIntID()
  302. {
  303. word retval = 0;
  304. asm(
  305. "readintid r2 ;reads interrupt id to r2\n"
  306. "write -4 r14 r2 ;write to stack to return\n"
  307. );
  308. return retval;
  309. }
  310. // Converts char c to uppercase if possible
  311. char toUpper(char c)
  312. {
  313. if (c>96 && c<123)
  314. c = c ^ 0x20;
  315. return c;
  316. }
  317. // Converts string str to uppercase if possible
  318. void strToUpper(char* str)
  319. {
  320. char chr = *str; // first character of str
  321. while (chr != 0) // continue until null value
  322. {
  323. *str = toUpper(chr); // uppercase char
  324. str++; // go to next character address
  325. chr = *str; // get character from address
  326. }
  327. }
  328. /*
  329. For debugging
  330. Prints a hex dump of size 'len' for each word starting from 'addr'
  331. Values are printed over UART
  332. */
  333. void hexdump(char* addr, word len)
  334. {
  335. char buf[16];
  336. word i;
  337. for (i = 0; i < len; i++)
  338. {
  339. // newline every 8 words
  340. if (i != 0 && MATH_modU(i, 8) == 0)
  341. uprintc('\n');
  342. itoah(addr[i], buf);
  343. uprint(buf);
  344. uprintc(' ');
  345. }
  346. }