debug.c 4.1 KB

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