debug.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <string.h>
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include "debug.h"
  6. #include "data.h"
  7. #include "pad.h"
  8. #include "PPU.h"
  9. #include "ressource.h"
  10. #define DEBUG_BUFFER_SIZE 128
  11. word debugMap[0x400];
  12. char debug_buffer[DEBUG_BUFFER_SIZE];
  13. char screen_buffer[DEBUG_BUFFER_SIZE];
  14. void debug_init(void)
  15. {
  16. word i;
  17. for (i = 0; i < 0x400; i++) {
  18. debugMap[i] = 0x00;
  19. }
  20. memset(debug_buffer, 0, DEBUG_BUFFER_SIZE);
  21. memset(screen_buffer, 0,DEBUG_BUFFER_SIZE);
  22. }
  23. void debug_enable(void)
  24. {
  25. VRAMLoad((word) debugFont_pic, 0x5000, 2048);
  26. VRAMLoad((word) debugMap, 0x4000, 0x0800);
  27. setTileMapLocation(0x4000, (byte) 0x00, (byte) 0);
  28. setCharacterLocation(0x5000, (byte) 0);
  29. *(byte *) 0x2100 = 0x0f; // enable background
  30. // Font Color
  31. // hex(24 << 10 | 24 << 5 | 24 ) = '0x6318'
  32. *(byte *) 0x2121 = 0x02;
  33. *(byte *) 0x2122 = 0xff;
  34. *(byte *) 0x2122 = 0x7f;
  35. // Font Border Color
  36. *(byte *) 0x2121 = 0x00;
  37. *(byte *) 0x2122 = 0x00;
  38. *(byte *) 0x2122 = 0x00;
  39. // Background Color
  40. *(byte *) 0x2121 = 0x01;
  41. *(byte *) 0x2122 = 0x05;
  42. *(byte *) 0x2122 = 0x29;
  43. }
  44. void clears(void)
  45. {
  46. word i, y;
  47. for (y = 0; y < 20; y++) {
  48. waitForVBlank();
  49. for (i = 0; i < 32; i++) {
  50. *(byte *) 0x2115 = 0x80;
  51. *(word *) 0x2116 = 0x4000 + i + (y * 0x20);
  52. *(byte *) 0x2118 = 0;
  53. }
  54. }
  55. }
  56. void _print_char(word y, word x, char c)
  57. {
  58. waitForVBlank();
  59. VRAMByteWrite((byte) (c - 32), (word) (0x4000 + x + (y * 0x20)));
  60. }
  61. void _print_screen(word y, char *buffer)
  62. {
  63. char l;
  64. unsigned int x;
  65. x = y * 0x20;
  66. l = strlen(buffer);
  67. waitForVBlank();
  68. while (*buffer) {
  69. if (*buffer == '\n') {
  70. while (x++ < 32) {
  71. *(byte *) 0x2115 = 0x80;
  72. *(word *) 0x2116 = 0x4000 + x + (y * 0x20);
  73. *(byte *) 0x2118 = 0;
  74. }
  75. x = 0;
  76. y += 0x20;
  77. buffer++;
  78. waitForVBlank();
  79. continue;
  80. }
  81. *(byte *) 0x2115 = 0x80;
  82. *(word *) 0x2116 = 0x4000 + x;
  83. *(byte *) 0x2118 = *buffer - 32;
  84. x++;
  85. buffer++;
  86. #if 1
  87. waitForVBlank();
  88. #endif
  89. }
  90. }
  91. void _print_console(const char *buffer)
  92. {
  93. while (*buffer)
  94. *(byte *) 0x3000 = *buffer++;
  95. }
  96. void printfc(const char *fmt, ...)
  97. {
  98. va_list ap;
  99. va_start(ap, fmt);
  100. vsprintf(debug_buffer, fmt, ap);
  101. va_end(ap);
  102. _print_console(debug_buffer);
  103. //memset(debug_buffer,0,DEBUG_BUFFER_SIZE);
  104. }
  105. void printfs(word y, const char *fmt, ...)
  106. {
  107. va_list ap;
  108. va_start(ap, fmt);
  109. vsprintf(screen_buffer, fmt, ap);
  110. va_end(ap);
  111. _print_screen(y, screen_buffer);
  112. //memset(screen_buffer, 0, DEBUG_BUFFER_SIZE);
  113. }
  114. void printc_packet(unsigned long addr, unsigned int len, byte * packet)
  115. {
  116. unsigned int i, j;
  117. unsigned int sum = 0;
  118. unsigned int last_sum = 0;
  119. unsigned int clear = 0;
  120. for (i = 0; i < len; i += 16) {
  121. sum = 0;
  122. for (j = 0; j < 16; j++) {
  123. sum += packet[i + j];
  124. }
  125. if (!sum) {
  126. clear = 1;
  127. continue;
  128. }
  129. if (last_sum == sum) {
  130. clear = 1;
  131. continue;
  132. }
  133. if (clear) {
  134. printfc("*\n");
  135. clear = 0;
  136. }
  137. printfc("%06lX:", addr + i);
  138. for (j = 0; j < 16; j++) {
  139. printfc(" %02x", packet[i + j]);
  140. }
  141. printfc(" |");
  142. for (j = 0; j < 16; j++) {
  143. if (packet[i + j] >= 33 && packet[i + j] <= 126)
  144. printfc("%c", packet[i + j]);
  145. else
  146. printfc(".");
  147. }
  148. printfc("|\n");
  149. last_sum = sum;
  150. }
  151. }
  152. /*
  153. * keep the linker happy
  154. */
  155. int open(const char *_name, int _mode)
  156. {
  157. _print_console("open called\n");
  158. return -1;
  159. }
  160. int close(int fd)
  161. {
  162. _print_console("close called\n");
  163. return -1;
  164. }
  165. size_t read(int fd, void *buff, size_t len)
  166. {
  167. _print_console("read called\n");
  168. return 0;
  169. }
  170. size_t write(int fd, void *buffer, size_t len)
  171. {
  172. _print_console("write called\n");
  173. return 0;
  174. }
  175. long lseek(int fd, long off, int count)
  176. {
  177. _print_console("lseek called\n");
  178. return 0;
  179. }
  180. int unlink(const char *name)
  181. {
  182. _print_console("unlink called\n");
  183. return -1;
  184. }
  185. int isatty()
  186. {
  187. _print_console("isatty called\n");
  188. return 1;
  189. }