No Description

debug.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <bpre.h>
  2. #include <debug.h>
  3. #include <assets/ascii.h>
  4. #include <battle_test.h>
  5. void set_debug_background(u16 color) {
  6. u16* bgc = (u16*) 0x020375f8;
  7. *bgc = color;
  8. return;
  9. }
  10. void debug_scene() {
  11. if (superstate.multi_purpose_state_tracker == 0) {
  12. print_memory->row = 0;
  13. print_memory->line = 0;
  14. print_memory->color = 0;
  15. gpu_tile_bg_drop_all_sets(0);
  16. gpu_tile_bg_drop_all_sets(1);
  17. gpu_tile_bg_drop_all_sets(2);
  18. gpu_tile_bg_drop_all_sets(3);
  19. bg_vram_setup(0, standard_bg, 4);
  20. gpu_bg_show(0);
  21. gpu_bg_show(1);
  22. gpu_bg_show(2);
  23. gpu_bg_show(3);
  24. gpu_sync_bg_visibility_and_mode();
  25. reset_scrolling_debug();
  26. obj_delete_all();
  27. memset((void*) 0x06000000, 0, 0x17fe0);
  28. memset((void*) 0x020375F8, 0, 0x400);
  29. superstate.multi_purpose_state_tracker++;
  30. } else if (superstate.multi_purpose_state_tracker == 1) {
  31. vram_decompress((void*) asciiTiles, (void*) 0x06000000);
  32. memcpy((void*) 0x020375F8, (void*) asciiPal, 0x60);
  33. set_debug_background(0x0000);
  34. superstate.multi_purpose_state_tracker++;
  35. } else if (superstate.multi_purpose_state_tracker == 2) {
  36. init_unit_test();
  37. superstate.multi_purpose_state_tracker++;
  38. }
  39. return;
  40. }
  41. u8 some_test() {
  42. set_callback2(debug_scene);
  43. vblank_handler_set(debug_update);
  44. superstate.multi_purpose_state_tracker = 0;
  45. return 0;
  46. }
  47. void reset_scrolling_debug() {
  48. lcd_io_set_func(0x12, 0x0);
  49. lcd_io_set_func(0x14, 0x0);
  50. lcd_io_set_func(0x16, 0x0);
  51. lcd_io_set_func(0x18, 0x0);
  52. lcd_io_set_func(0x1A, 0x0);
  53. lcd_io_set_func(0x1C, 0x0);
  54. lcd_io_set_func(0x1E, 0x0);
  55. return;
  56. }
  57. void init_unit_test() {
  58. test_speed();
  59. }
  60. void print_string(u16 line, u16 row, u8 color, char* pBuf) {
  61. while (*pBuf) {
  62. print_character(line, row++, *pBuf++, color);
  63. }
  64. return;
  65. }
  66. void print(char* str) {
  67. while (*str) {
  68. if (*str == '\n') {
  69. print_memory->line++;
  70. print_memory->row = 0;
  71. } else if (*str == '\xFE') {
  72. str++;
  73. u8 c = *str;
  74. if (c > 2)
  75. c = 0;
  76. print_memory->color = c;
  77. } else {
  78. print_character(print_memory->line, print_memory->row, *str, print_memory->color);
  79. print_memory->row++;
  80. }
  81. str++;
  82. }
  83. return;
  84. }
  85. void printf(char* str, int arg) {
  86. while (*str) {
  87. if (*str == '\n') {
  88. print_memory->line++;
  89. print_memory->row = 0;
  90. } else if (*str == '\xFE') {
  91. str++;
  92. u8 c = *str;
  93. if (c > 2)
  94. c = 0;
  95. print_memory->color = c;
  96. } else if (*str == '%') {
  97. str++;
  98. if (*str == '%') {
  99. print_character(print_memory->line, print_memory->row, *str, print_memory->color);
  100. print_memory->row++;
  101. } else if (*str == 'd') {
  102. u32 len = dec_len(arg);
  103. char temp[dec_len(len + 1)];
  104. temp[len] = 0;
  105. int_to_char(arg, temp);
  106. print(temp);
  107. } else if (*str == 'c') {
  108. char print_char = (char) (arg);
  109. print_character(print_memory->line, print_memory->row, print_char, print_memory->color);
  110. print_memory->row++;
  111. }
  112. } else {
  113. print_character(print_memory->line, print_memory->row, *str, print_memory->color);
  114. print_memory->row++;
  115. }
  116. str++;
  117. }
  118. }
  119. void debug_clean() {
  120. memset((void*) 0x0600C800, 0, 0x800);
  121. print_memory->row = 0;
  122. print_memory->line = 0;
  123. print_memory->color = 0;
  124. return;
  125. }
  126. void wait_for_btn(u16 field) {
  127. volatile u16* control_io = (volatile u16*) (0x04000130);
  128. while (*control_io & field) {
  129. }
  130. return;
  131. }
  132. void print_character(u16 line, u16 row, char character, u8 color) {
  133. if (color > 2)
  134. color = 0;
  135. u16 position = (32 * line) + row;
  136. union t_map_entry map_entry;
  137. map_entry.entry.tile = char_to_byte(character);
  138. map_entry.entry.pal = color;
  139. u16* ptr = (u16*) (0x0600c800 + (position * 2));
  140. *ptr = map_entry.short_map;
  141. return;
  142. }
  143. u8 char_to_byte(char character) {
  144. if (character >= 0x20 && character <= 0x7E)
  145. return character - 0x20;
  146. else
  147. return 3;
  148. }
  149. void debug_update() {
  150. fade_update();
  151. task_exec();
  152. objc_exec();
  153. obj_sync();
  154. gpu_pal_upload();
  155. gpu_sprites_upload();
  156. }
  157. void int_to_char(u32 i, char* ref) {
  158. if (i == 0) {
  159. ref[0] = '0';
  160. return;
  161. }
  162. u32 len = dec_len(i);
  163. while (i > 0) {
  164. ref[len - 1] = '0' + (__aeabi_uidivmod(i, 10));
  165. i /= 10;
  166. len--;
  167. }
  168. return;
  169. }
  170. u32 power(u32 n, u32 power) {
  171. u32 out = 1;
  172. for (int i = 0; i < power; ++i) {
  173. out = out * n;
  174. }
  175. return out;
  176. }
  177. u32 dec_len(u32 i) {
  178. u32 len = 1;
  179. while ((i /= 10) > 0) {
  180. len++;
  181. }
  182. return len;
  183. }