stdlib.c 7.3 KB

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