stdlib.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Standard library
  3. * Contains basic functions, including timer and memory functions
  4. * Modified version for BCC
  5. */
  6. // uses math.c
  7. #define UART_TX_ADDR 0xC02723
  8. // Timer I/O Addresses
  9. #define TIMER1_VAL 0xC02739
  10. #define TIMER1_CTRL 0xC0273A
  11. #define TIMER2_VAL 0xC0273B
  12. #define TIMER2_CTRL 0xC0273C
  13. #define TIMER3_VAL 0xC0273D
  14. #define TIMER3_CTRL 0xC0273E
  15. word timer1Value = 0;
  16. void* memcpy(void *dest, const void *src, word len)
  17. {
  18. // Typecast src and dest addresses to (char *)
  19. char *csrc = (char *)src;
  20. char *cdest = (char *)dest;
  21. // Copy contents of src[] to dest[]
  22. word i;
  23. for (i=0; i<len; i++)
  24. cdest[i] = csrc[i];
  25. }
  26. /*
  27. Compares n words between a and b
  28. Returns 1 if similar, 0 otherwise
  29. */
  30. word memcmp(word* a, word* b, word n)
  31. {
  32. word i;
  33. for (i = 0; i < n; i++)
  34. {
  35. if (a[i] != b[i])
  36. {
  37. return 0;
  38. }
  39. }
  40. return 1;
  41. }
  42. // Function to implement `strcpy()` function
  43. char* strcpy(char* destination, const char* source)
  44. {
  45. // take a pointer pointing to the beginning of the destination string
  46. char *ptr = destination;
  47. // copy the C-string pointed by source into the array
  48. // pointed by destination
  49. while (*source != '\0')
  50. {
  51. *destination = *source;
  52. destination++;
  53. source++;
  54. }
  55. // include the terminating null character
  56. *destination = '\0';
  57. // the destination is returned by standard `strcpy()`
  58. return ptr;
  59. }
  60. word strlen(const char *str)
  61. {
  62. const char *s;
  63. for (s = str; *s; ++s);
  64. return (s - str);
  65. }
  66. char* strcat (char *dest, const char *src)
  67. {
  68. strcpy (dest + strlen (dest), src);
  69. return dest;
  70. }
  71. word strcmp(const char* s1, const char* s2)
  72. {
  73. while(*s1 && (*s1 == *s2))
  74. {
  75. s1++;
  76. s2++;
  77. }
  78. return *(unsigned char*)s1 - *(unsigned char*)s2;
  79. }
  80. /*
  81. Recursive helper function for itoa
  82. Eventually returns the number of digits in n
  83. s is the output buffer
  84. */
  85. word itoar(word n, char *s)
  86. {
  87. word digit = MATH_modU(n, 10);
  88. word i = 0;
  89. n = MATH_divU(n,10);
  90. if ((unsigned int) n > 0)
  91. i += itoar(n, s);
  92. s[i++] = digit + '0';
  93. return i;
  94. }
  95. /*
  96. Converts integer n to characters.
  97. The characters are placed in the buffer s.
  98. The buffer is terminated with a 0 value.
  99. Uses recursion, division and mod to compute.
  100. */
  101. void itoa(word n, char *s)
  102. {
  103. // compute and fill the buffer
  104. word i = itoar(n, s);
  105. // end with terminator
  106. s[i] = 0;
  107. }
  108. // Returns interrupt ID by using the readintid asm instruction
  109. word getIntID()
  110. {
  111. word retval = 0;
  112. asm(
  113. "readintid r2 ;reads interrupt id to r2\n"
  114. "write -4 r14 r2 ;write to stack to return\n"
  115. );
  116. return retval;
  117. }
  118. /*
  119. Converts string into int.
  120. Assumes the string is valid.
  121. Unsigned only!
  122. */
  123. word strToInt(char* str)
  124. {
  125. word retval = 0;
  126. word multiplier = 1;
  127. word i = 0;
  128. while (str[i] != 0)
  129. {
  130. i++;
  131. }
  132. if (i == 0)
  133. return 0;
  134. i--;
  135. while (i > 0)
  136. {
  137. // Return 0 if not a digit
  138. if (str[i] < '0' || str[i] > '9')
  139. return 0;
  140. word currentDigit = str[i] - '0';
  141. word toAdd = multiplier * currentDigit;
  142. retval += toAdd;
  143. multiplier = multiplier * 10;
  144. i--;
  145. }
  146. // Check for negative
  147. if (str[i] == '-')
  148. {
  149. retval *= -1;
  150. }
  151. else
  152. {
  153. word currentDigit = str[i] - '0';
  154. word toAdd = multiplier * currentDigit;
  155. retval += toAdd;
  156. }
  157. return retval;
  158. }
  159. /*
  160. Converts dec string into int.
  161. Assumes the string is valid.
  162. Can be signed.
  163. */
  164. word decToInt(char* dec)
  165. {
  166. if (dec[0] == '-')
  167. {
  168. // signed
  169. return -strToInt((dec+1));
  170. }
  171. else
  172. {
  173. return strToInt(dec);
  174. }
  175. return 0;
  176. }
  177. /*
  178. Converts hex string into int.
  179. Assumes the string is valid.
  180. */
  181. word hexToInt(char *hex) {
  182. word val = 0;
  183. hex += 2; // skip the 0x
  184. while (*hex)
  185. {
  186. // get current character then increment
  187. char byte = *hex++;
  188. // transform hex character to the 4bit equivalent number, using the ascii table indexes
  189. if (byte >= '0' && byte <= '9') byte = byte - '0';
  190. else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
  191. else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;
  192. // shift 4 to make space for new digit, and add the 4 bits of the new digit
  193. val = (val << 4) | (byte & 0xF);
  194. }
  195. return val;
  196. }
  197. // 0b1100101
  198. /*
  199. Converts binary string into int.
  200. Assumes the string is valid.
  201. */
  202. word binToInt(char *binStr) {
  203. binStr += 2; // skip the 0b
  204. word retval = 0;
  205. word binLength = strlen(binStr);
  206. word i;
  207. for (i = 0; i < binLength; i++)
  208. {
  209. char c = binStr[(binLength - 1) - i];
  210. if (c == '1')
  211. {
  212. retval += 1 << i;
  213. }
  214. else if (c != '0')
  215. {
  216. BDOS_PrintConsole("Invalid binary number\n");
  217. exit(1);
  218. }
  219. }
  220. return retval;
  221. }
  222. // Converts char c to uppercase if possible
  223. char toUpper(char c)
  224. {
  225. if (c>96 && c<123)
  226. c = c ^ 0x20;
  227. return c;
  228. }
  229. // Converts string str to uppercase if possible
  230. void strToUpper(char* str)
  231. {
  232. char chr = *str; // first character of str
  233. while (chr != 0) // continue until null value
  234. {
  235. *str = toUpper(chr); // uppercase char
  236. str++; // go to next character address
  237. chr = *str; // get character from address
  238. }
  239. }