debug.c 3.6 KB

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