No Description

agb_debug.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. * Copyright (C) 2015-2017 by the SotS Team *
  3. * *
  4. * This file is part of Sovereign of the Skies. *
  5. * *
  6. * Sovereign of the Skies is free software: you can redistribute it *
  7. * and/or modify it *
  8. * under the terms of the GNU Lesser General Public License as published *
  9. * by the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version provided you include a copy of the *
  11. * licence and this header. *
  12. * *
  13. * Sovereign of the Skies is distributed in the hope that it will be *
  14. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU Lesser General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU Lesser General Public *
  19. * License along with Sovereign of the Skies. *
  20. * If not, see <http://www.gnu.org/licenses/>. *
  21. ****************************************************************************/
  22. /*
  23. * File: agb_debug.c
  24. * Author: Sturmvogel
  25. *
  26. * Created on 11. April 2017, 04:55
  27. */
  28. #include <memory.h>
  29. #include <stdarg.h>
  30. #include <math.h>
  31. void dprint(const char * sz)
  32. {
  33. __asm__ __volatile__(
  34. "mov r2, %0\n"
  35. "mov r0, #0xC0\n"
  36. "lsl r0, #0x8\n"
  37. "mov r1, #0xDE\n"
  38. "orr r0, r1\n"
  39. "lsl r0, #0x8\n"
  40. "mov r1, #0xD0\n"
  41. "orr r0, r1\n"
  42. "lsl r0, #8\n"
  43. "mov r1, #0x0D\n"
  44. "orr r0, r1\n"
  45. "mov r1, #0\n"
  46. "and r0, r0, r0\n":
  47. :
  48. "r" (sz):
  49. "r0", "r1", "r2");
  50. }
  51. u32 mini_strlen(const char * s) {
  52. u32 len = 0;
  53. while (s[len] != '\0') len++;
  54. return len;
  55. }
  56. u32 mini_itoa(int value, u32 radix, u32 uppercase, u32 unsig, char * buffer, u32 zero_pad) {
  57. char * pbuffer = buffer;
  58. s32 negative = 0;
  59. u32 i, len;
  60. /* No support for unusual radixes. */
  61. if (radix > 16)
  62. return 0;
  63. if (value < 0 && !unsig) {
  64. negative = 1;
  65. value = -value;
  66. }
  67. /* This builds the string back to front ... */
  68. if (radix == 16) {
  69. do {
  70. u32 digit = value & 0xF;
  71. * (pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
  72. value >>= 4;
  73. } while (value > 0);
  74. } else {
  75. do {
  76. u32 digit = value % radix;
  77. * (pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
  78. value /= radix;
  79. } while (value > 0);
  80. }
  81. for (i = (pbuffer - buffer); i < zero_pad; i++)
  82. * (pbuffer++) = '0';
  83. if (negative)
  84. {
  85. * (pbuffer++) = '-'; * (pbuffer) = '\0';
  86. }
  87. /* ... now we reverse it (could do it recursively but will
  88. * conserve the stack space) */
  89. len = (pbuffer - buffer);
  90. for (i = 0; i < len / 2; i++) {
  91. char j = buffer[i];
  92. buffer[i] = buffer[len - i - 1];
  93. buffer[len - i - 1] = j;
  94. }
  95. return len;
  96. }
  97. int mini_vsnprintf(char * buffer, u32 buffer_len,
  98. const char * fmt, va_list va)
  99. {
  100. char * pbuffer = buffer;
  101. char bf[24];
  102. char ch;
  103. int _putc(char ch)
  104. {
  105. if ((u32)((pbuffer - buffer) + 1) >= buffer_len)
  106. return 0;
  107. * (pbuffer++) = ch;
  108. * (pbuffer) = '\0';
  109. return 1;
  110. }
  111. int _puts(char * s, u32 len)
  112. {
  113. u32 i;
  114. if (buffer_len - (pbuffer - buffer) - 1 < len)
  115. len = buffer_len - (pbuffer - buffer) - 1;
  116. /* Copy to buffer */
  117. for (i = 0; i < len; i++)
  118. * (pbuffer++) = s[i];
  119. * (pbuffer) = '\0';
  120. return len;
  121. }
  122. while ((ch = * (fmt++))) {
  123. if ((u32)((pbuffer - buffer) + 1) >= buffer_len)
  124. break;
  125. if (ch != '%')
  126. _putc(ch);
  127. else {
  128. char zero_pad = 0;
  129. char * ptr;
  130. u32 len;
  131. ch = * (fmt++);
  132. /* Zero padding requested */
  133. if (ch == '0') {
  134. ch = * (fmt++);
  135. if (ch == '\0')
  136. return pbuffer - buffer;
  137. if (ch >= '0' && ch <= '9')
  138. zero_pad = ch - '0';
  139. ch = * (fmt++);
  140. }
  141. switch (ch) {
  142. case 0:
  143. return pbuffer - buffer;
  144. case 'u':
  145. case 'd':
  146. len = mini_itoa(va_arg(va, u32), 10, 0, (ch == 'u'), bf, zero_pad);
  147. _puts(bf, len);
  148. break;
  149. case 'x':
  150. case 'X':
  151. len = mini_itoa(va_arg(va, u32), 16, (ch == 'X'), 1, bf, zero_pad);
  152. _puts(bf, len);
  153. break;
  154. case 'c':
  155. _putc((char)(va_arg(va, int)));
  156. break;
  157. case 's':
  158. ptr = va_arg(va, char * );
  159. _puts(ptr, mini_strlen(ptr));
  160. break;
  161. default:
  162. _putc(ch);
  163. break;
  164. }
  165. }
  166. }
  167. return pbuffer - buffer;
  168. }
  169. void dprintf(const char * str, ...)
  170. {
  171. char __outstr[256];
  172. va_list args;
  173. va_start(args, str);
  174. mini_vsnprintf(__outstr, 256, str, args);
  175. va_end(args);
  176. dprint(__outstr);
  177. }