stdlib.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. Recursive helper function for itoa
  120. Eventually returns the number of digits in n
  121. s is the output buffer
  122. */
  123. word itoahr(word n, char *s)
  124. {
  125. word digit = MATH_modU(n, 16);
  126. word i = 0;
  127. n = MATH_divU(n,16);
  128. if ((unsigned int) n > 0)
  129. i += itoahr(n, s);
  130. char c;
  131. if (digit > 9)
  132. {
  133. c = digit + 'A' - 10;
  134. }
  135. else
  136. {
  137. c = digit + '0';
  138. }
  139. s[i++] = c;
  140. return i;
  141. }
  142. /*
  143. Converts integer n to hex string characters.
  144. The characters are placed in the buffer s.
  145. A prefix of 0x is added.
  146. The buffer is terminated with a 0 value.
  147. Uses recursion, division and mod to compute.
  148. */
  149. void itoah(word n, char *s)
  150. {
  151. // add prefix
  152. s[0] = '0';
  153. s[1] = 'x';
  154. s+=2;
  155. // compute and fill the buffer
  156. word i = itoahr(n, s);
  157. // end with terminator
  158. s[i] = 0;
  159. }
  160. /*
  161. Converts string into int.
  162. Assumes the string is valid.
  163. Unsigned only!
  164. */
  165. word strToInt(char* str)
  166. {
  167. word retval = 0;
  168. word multiplier = 1;
  169. word i = 0;
  170. while (str[i] != 0)
  171. {
  172. i++;
  173. }
  174. if (i == 0)
  175. return 0;
  176. i--;
  177. while (i > 0)
  178. {
  179. // Return 0 if not a digit
  180. if (str[i] < '0' || str[i] > '9')
  181. return 0;
  182. word currentDigit = str[i] - '0';
  183. word toAdd = multiplier * currentDigit;
  184. retval += toAdd;
  185. multiplier = multiplier * 10;
  186. i--;
  187. }
  188. // Check for negative
  189. if (str[i] == '-')
  190. {
  191. retval *= -1;
  192. }
  193. else
  194. {
  195. word currentDigit = str[i] - '0';
  196. word toAdd = multiplier * currentDigit;
  197. retval += toAdd;
  198. }
  199. return retval;
  200. }
  201. /*
  202. Converts dec string into int.
  203. Assumes the string is valid.
  204. Can be signed.
  205. */
  206. word decToInt(char* dec)
  207. {
  208. if (dec[0] == '-')
  209. {
  210. // signed
  211. return -strToInt((dec+1));
  212. }
  213. else
  214. {
  215. return strToInt(dec);
  216. }
  217. return 0;
  218. }
  219. /*
  220. Converts hex string into int.
  221. Assumes the string is valid.
  222. */
  223. word hexToInt(char *hex) {
  224. word val = 0;
  225. hex += 2; // skip the 0x
  226. while (*hex)
  227. {
  228. // get current character then increment
  229. char byte = *hex++;
  230. // transform hex character to the 4bit equivalent number, using the ascii table indexes
  231. if (byte >= '0' && byte <= '9') byte = byte - '0';
  232. else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
  233. else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;
  234. // shift 4 to make space for new digit, and add the 4 bits of the new digit
  235. val = (val << 4) | (byte & 0xF);
  236. }
  237. return val;
  238. }
  239. // 0b1100101
  240. /*
  241. Converts binary string into int.
  242. Assumes the string is valid.
  243. */
  244. word binToInt(char *binStr) {
  245. binStr += 2; // skip the 0b
  246. word retval = 0;
  247. word binLength = strlen(binStr);
  248. word i;
  249. for (i = 0; i < binLength; i++)
  250. {
  251. char c = binStr[(binLength - 1) - i];
  252. if (c == '1')
  253. {
  254. retval += 1 << i;
  255. }
  256. else if (c != '0')
  257. {
  258. bdos_print("Invalid binary number\n");
  259. exit(1);
  260. }
  261. }
  262. return retval;
  263. }
  264. // Converts char c to uppercase if possible
  265. char toUpper(char c)
  266. {
  267. if (c>96 && c<123)
  268. c = c ^ 0x20;
  269. return c;
  270. }
  271. // Converts string str to uppercase if possible
  272. void strToUpper(char* str)
  273. {
  274. char chr = *str; // first character of str
  275. while (chr != 0) // continue until null value
  276. {
  277. *str = toUpper(chr); // uppercase char
  278. str++; // go to next character address
  279. chr = *str; // get character from address
  280. }
  281. }
  282. /*
  283. Prints a single char c by writing it to UART_TX_ADDR
  284. */
  285. void uprintc(char c)
  286. {
  287. word *p = (word *)UART_TX_ADDR; // address of UART TX
  288. *p = (word)c; // write char over UART
  289. }
  290. /*
  291. Sends each character from str over UART
  292. by writing them to UART_TX_ADDR
  293. until a 0 value is found.
  294. Does not send a newline afterwards.
  295. */
  296. void uprint(char* str)
  297. {
  298. word *p = (word *)UART_TX_ADDR; // address of UART TX
  299. char chr = *str; // first character of str
  300. while (chr != 0) // continue until null value
  301. {
  302. *p = (word)chr; // write char over UART
  303. str++; // go to next character address
  304. chr = *str; // get character from address
  305. }
  306. }
  307. /*
  308. Same as uprint(char* str),
  309. except it sends a newline afterwards.
  310. */
  311. void uprintln(char* str)
  312. {
  313. uprint(str);
  314. uprintc('\n');
  315. }
  316. /*
  317. Prints decimal integer over UART
  318. */
  319. void uprintDec(word i)
  320. {
  321. char buffer[11];
  322. itoa(i, buffer);
  323. uprint(buffer);
  324. }
  325. /*
  326. Prints hex integer over UART
  327. */
  328. void uprintHex(word i)
  329. {
  330. char buffer[11];
  331. itoah(i, buffer);
  332. uprint(buffer);
  333. }
  334. /*
  335. Prints decimal integer over UART, with newline
  336. */
  337. void uprintlnDec(word i)
  338. {
  339. char buffer[11];
  340. itoa(i, buffer);
  341. uprint(buffer);
  342. uprintc('\n');
  343. }
  344. /*
  345. Prints hex integer over UART, with newline
  346. */
  347. void uprintlnHex(word i)
  348. {
  349. char buffer[11];
  350. itoah(i, buffer);
  351. uprint(buffer);
  352. uprintc('\n');
  353. }