1
0

stdlib.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. word timer2Value = 0;
  17. word timer3Value = 0;
  18. /*
  19. * TODO:
  20. * - Convert most of these functions to assembly
  21. */
  22. // isalpha
  23. word isalpha(word argument)
  24. {
  25. if (argument >= 'A' && argument <= 'Z')
  26. return 2;
  27. if (argument >= 'a' && argument <= 'z')
  28. return 1;
  29. return 0;
  30. }
  31. // isdigit
  32. word isdigit(word argument)
  33. {
  34. if (argument >= '0' && argument <= '9')
  35. return 1;
  36. return 0;
  37. }
  38. // isalnum
  39. word isalnum(word argument)
  40. {
  41. if (isdigit(argument) || isalpha(argument))
  42. return 1;
  43. return 0;
  44. }
  45. void* memcpy(void *dest, const void *src, size_t len)
  46. {
  47. // Typecast src and dest addresses to (char *)
  48. char *csrc = (char *)src;
  49. char *cdest = (char *)dest;
  50. // Copy contents of src[] to dest[]
  51. word i;
  52. for (i=0; i<len; i++)
  53. cdest[i] = csrc[i];
  54. }
  55. void* memmove(void* dest, const void* src, size_t n)
  56. {
  57. unsigned char* from = (unsigned char*) src;
  58. unsigned char* to = (unsigned char*) dest;
  59. if (from == to || n == 0)
  60. return dest;
  61. if (to > from && to-from < (word)n) {
  62. /* to overlaps with from */
  63. /* <from......> */
  64. /* <to........> */
  65. /* copy in reverse, to avoid overwriting from */
  66. word i;
  67. for(i=n-1; i>=0; i--)
  68. to[i] = from[i];
  69. return dest;
  70. }
  71. if (from > to && from-to < (word)n) {
  72. /* to overlaps with from */
  73. /* <from......> */
  74. /* <to........> */
  75. /* copy forwards, to avoid overwriting from */
  76. size_t i;
  77. for(i=0; i<n; i++)
  78. to[i] = from[i];
  79. return dest;
  80. }
  81. memcpy(dest, src, n);
  82. return dest;
  83. }
  84. // Function to implement `strcpy()` function
  85. char* strcpy(char* destination, const char* source)
  86. {
  87. // return if no memory is allocated to the destination
  88. if (destination == NULL) {
  89. return NULL;
  90. }
  91. // take a pointer pointing to the beginning of the destination string
  92. char *ptr = destination;
  93. // copy the C-string pointed by source into the array
  94. // pointed by destination
  95. while (*source != '\0')
  96. {
  97. *destination = *source;
  98. destination++;
  99. source++;
  100. }
  101. // include the terminating null character
  102. *destination = '\0';
  103. // the destination is returned by standard `strcpy()`
  104. return ptr;
  105. }
  106. size_t strlen(const char *str)
  107. {
  108. const char *s;
  109. for (s = str; *s; ++s);
  110. return (s - str);
  111. }
  112. char* strcat (char *dest, const char *src)
  113. {
  114. strcpy (dest + strlen (dest), src);
  115. return dest;
  116. }
  117. char* strchr (const char *s, word c)
  118. {
  119. do {
  120. if (*s == c)
  121. {
  122. return (char*)s;
  123. }
  124. } while (*s++);
  125. return (0);
  126. }
  127. /*
  128. Returns a pointer to the last occurance of a character, or 0 if the character is not found.
  129. */
  130. char* strrchr (const char *s, int c)
  131. {
  132. char *rtnval = 0;
  133. do {
  134. if (*s == c)
  135. rtnval = (char*) s;
  136. } while (*s++);
  137. return (rtnval);
  138. }
  139. char * strtok_old_str;
  140. /*
  141. Parse str into tokens separated by characters in delim.
  142. If S is NULL, the last string strtok() was called with is used.
  143. Note that strtok() modifies the input string.
  144. For example:
  145. char s[] = "-abc-=-def";
  146. x = strtok(s, "-"); // x = "abc"
  147. x = strtok(NULL, "-="); // x = "def"
  148. x = strtok(NULL, "="); // x = NULL
  149. // s = "abc\0=-def\0"
  150. */
  151. char* strtok(char* str, const char* delim)
  152. {
  153. if (str != (word*)-1)
  154. strtok_old_str = str;
  155. if (strtok_old_str == (word*)-1)
  156. return (word*)-1;
  157. // Return reached end of string
  158. if (*strtok_old_str == 0)
  159. {
  160. return (word*)-1;
  161. }
  162. // Skip leading delimiters
  163. while (strchr(delim, *strtok_old_str) != 0)
  164. strtok_old_str++;
  165. // Find end of token
  166. char* start = strtok_old_str;
  167. while (*strtok_old_str != 0 && strchr(delim, *strtok_old_str) == 0)
  168. strtok_old_str++;
  169. if (*strtok_old_str == 0)
  170. {
  171. strtok_old_str = (word*)-1;
  172. return start;
  173. }
  174. *strtok_old_str = 0;
  175. strtok_old_str++;
  176. return start;
  177. }
  178. word strcmp(const char* s1, const char* s2)
  179. {
  180. while(*s1 && (*s1 == *s2))
  181. {
  182. s1++;
  183. s2++;
  184. }
  185. return *(unsigned char*)s1 - *(unsigned char*)s2;
  186. }
  187. word strncmp(const char * s1, const char * s2, size_t n )
  188. {
  189. while ( n && *s1 && ( *s1 == *s2 ) )
  190. {
  191. ++s1;
  192. ++s2;
  193. --n;
  194. }
  195. if ( n == 0 )
  196. {
  197. return 0;
  198. }
  199. else
  200. {
  201. return ( *(unsigned char *)s1 - *(unsigned char *)s2 );
  202. }
  203. }
  204. /**
  205. * Return the basename of a path
  206. * path: full path
  207. */
  208. char* basename(char *path)
  209. {
  210. char *base = strrchr(path, '/');
  211. return base ? base + 1 : path;
  212. }
  213. /**
  214. * Return the dirname of a path
  215. * output: buffer to store the dirname
  216. * path: full path
  217. */
  218. char* dirname(char* output, char *path)
  219. {
  220. strcpy(output, path);
  221. char *last_slash = strrchr(output, '/');
  222. if (last_slash != 0)
  223. {
  224. *last_slash = 0;
  225. // If the last slash is the first character, return "/"
  226. if (last_slash == output)
  227. {
  228. strcpy(output, "/");
  229. }
  230. } else
  231. {
  232. // No slash found, return "."
  233. strcpy(output, ".");
  234. }
  235. return output;
  236. }
  237. /*
  238. Recursive helper function for itoa
  239. Eventually returns the number of digits in n
  240. s is the output buffer
  241. */
  242. word itoar(word n, char *s)
  243. {
  244. word digit = MATH_modU(n, 10);
  245. word i = 0;
  246. n = MATH_divU(n,10);
  247. if ((unsigned int) n > 0)
  248. i += itoar(n, s);
  249. s[i++] = digit + '0';
  250. return i;
  251. }
  252. /*
  253. Converts integer n to characters.
  254. The characters are placed in the buffer s.
  255. The buffer is terminated with a 0 value.
  256. Uses recursion, division and mod to compute.
  257. */
  258. void itoa(word n, char *s)
  259. {
  260. // compute and fill the buffer
  261. word i = itoar(n, s);
  262. // end with terminator
  263. s[i] = 0;
  264. }
  265. /*
  266. Prints a single char c by writing it to UART_TX_ADDR
  267. */
  268. void uprintc(char c)
  269. {
  270. word *p = (word *)UART_TX_ADDR; // address of UART TX
  271. *p = (word)c; // write char over UART
  272. }
  273. /*
  274. Sends each character from str over UART
  275. by writing them to UART_TX_ADDR
  276. until a 0 value is found.
  277. Does not send a newline afterwards.
  278. */
  279. void uprint(char* str)
  280. {
  281. word *p = (word *)UART_TX_ADDR; // address of UART TX
  282. char chr = *str; // first character of str
  283. while (chr != 0) // continue until null value
  284. {
  285. *p = (word)chr; // write char over UART
  286. str++; // go to next character address
  287. chr = *str; // get character from address
  288. }
  289. }
  290. /*
  291. Same as uprint(char* str),
  292. except it sends a newline afterwards.
  293. */
  294. void uprintln(char* str)
  295. {
  296. uprint(str);
  297. uprintc('\n');
  298. }
  299. // Converts char c to uppercase if possible
  300. char toUpper(char c)
  301. {
  302. if (c>96 && c<123)
  303. c = c ^ 0x20;
  304. return c;
  305. }
  306. // Converts string str to uppercase if possible
  307. void strToUpper(char* str)
  308. {
  309. char chr = *str; // first character of str
  310. while (chr != 0) // continue until null value
  311. {
  312. *str = toUpper(chr); // uppercase char
  313. str++; // go to next character address
  314. chr = *str; // get character from address
  315. }
  316. }