stdlib.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. word currentDigit = str[i] - '0';
  147. word toAdd = multiplier * currentDigit;
  148. retval += toAdd;
  149. return retval;
  150. }
  151. /*
  152. Converts dec string into int.
  153. Assumes the string is valid.
  154. Can be signed.
  155. */
  156. word decToInt(char* dec)
  157. {
  158. if (dec[0] == '-')
  159. {
  160. // signed
  161. return -strToInt((dec+1));
  162. }
  163. else
  164. {
  165. return strToInt(dec);
  166. }
  167. return 0;
  168. }
  169. /*
  170. Converts hex string into int.
  171. Assumes the string is valid.
  172. */
  173. word hexToInt(char *hex) {
  174. word val = 0;
  175. hex += 2; // skip the 0x
  176. while (*hex)
  177. {
  178. // get current character then increment
  179. char byte = *hex++;
  180. // transform hex character to the 4bit equivalent number, using the ascii table indexes
  181. if (byte >= '0' && byte <= '9') byte = byte - '0';
  182. else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
  183. else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;
  184. // shift 4 to make space for new digit, and add the 4 bits of the new digit
  185. val = (val << 4) | (byte & 0xF);
  186. }
  187. return val;
  188. }
  189. // 0b1100101
  190. /*
  191. Converts binary string into int.
  192. Assumes the string is valid.
  193. */
  194. word binToInt(char *binStr) {
  195. binStr += 2; // skip the 0b
  196. word retval = 0;
  197. word binLength = strlen(binStr);
  198. word i;
  199. for (i = 0; i < binLength; i++)
  200. {
  201. char c = binStr[(binLength - 1) - i];
  202. if (c == '1')
  203. {
  204. retval += 1 << i;
  205. }
  206. else if (c != '0')
  207. {
  208. BDOS_PrintConsole("Invalid binary number\n");
  209. exit(1);
  210. }
  211. }
  212. return retval;
  213. }
  214. // Converts char c to uppercase if possible
  215. char toUpper(char c)
  216. {
  217. if (c>96 && c<123)
  218. c = c ^ 0x20;
  219. return c;
  220. }
  221. // Converts string str to uppercase if possible
  222. void strToUpper(char* str)
  223. {
  224. char chr = *str; // first character of str
  225. while (chr != 0) // continue until null value
  226. {
  227. *str = toUpper(chr); // uppercase char
  228. str++; // go to next character address
  229. chr = *str; // get character from address
  230. }
  231. }