1
0

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