Aucune description

debug.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /****************************************************************************
  2. * Copyright (C) 2015-2016 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 debug.c
  24. * @author Sturmvogel
  25. * @date 15 dec 2016
  26. * @brief Operate with the sots debug engine, very temporary
  27. */
  28. /* === INCLUDE === */
  29. #include <types.h>
  30. #include <callback.h>
  31. #include <lcd.h>
  32. #include <debug.h>
  33. #include <memory.h>
  34. #include <assets/ascii.h>
  35. #include <battle_test.h>
  36. #include <math.h>
  37. #include <fade.h>
  38. /* === STATICS === */
  39. static struct print_engine* print_memory = (struct print_engine*)(0x0203FFF0);
  40. /* === PROTOTYPES === */
  41. /**
  42. * @brief set bg color
  43. * @param color color to set bg to
  44. */
  45. void debug_set_bg(u16 color);
  46. /**
  47. * @brief clean debug screen from any text and reset cursor
  48. */
  49. void debug_clean();
  50. /**
  51. * @brief wait for button press
  52. * @param field bitfield to specify buttom combination
  53. */
  54. void debug_wait_for_btn(u16 field);
  55. /**
  56. * @brief convert int to char
  57. * @param i integer
  58. * @param ref buffer to output to
  59. */
  60. void debug_int_to_char(u32 i, char* ref);
  61. /**
  62. * @get length of integer
  63. * @param i integer
  64. * @return length
  65. */
  66. u32 debug_dec_len(u32 i);
  67. /**
  68. * @brief update debug environment
  69. */
  70. void debug_update();
  71. /**
  72. * @brief reset scrolling (from overworld e.g.)
  73. */
  74. void debug_reset_scrolling();
  75. /**
  76. * @brief start a unit test function
  77. */
  78. void debug_init_unit_test();
  79. /**
  80. * @brief handle for the debug scene
  81. */
  82. void debug_scene();
  83. /**
  84. * @brief convert character to byte
  85. * @param character character
  86. * @return byte from character
  87. */
  88. u8 char_to_byte(char character);
  89. /**
  90. * @brief print a character on the debug environment
  91. * @param line line to print to
  92. * @param row row to print to
  93. * @param character character to print
  94. * @param color color to print in
  95. */
  96. void debug_print_char(u16 line, u16 row, char character, u8 color);
  97. /**
  98. * @brief print a string to the debug environment
  99. * @param line line to print to
  100. * @param row row to start
  101. * @param color color to print in
  102. * @param pBuf string buffer to print (null terminated)
  103. */
  104. void debug_print_string(u16 line, u16 row, u8 color, char* pBuf);
  105. /**
  106. * @brief print string onto debug environment at current position with parsing
  107. * @param str string to print
  108. */
  109. void debug_print(char* str);
  110. /**
  111. * @brief build power
  112. * @param n integer to power
  113. * @param power exponent
  114. * @return n^power
  115. */
  116. u32 debug_power(u32 n, u32 power);
  117. /**
  118. * @brief print in format to debug environment
  119. * @param str string with format
  120. * @param arg argument to print (only one, not an array)
  121. */
  122. void debug_printf(char* str, int arg);
  123. /**
  124. * @brief set bg color of debug environment
  125. * @param color color to set to
  126. */
  127. void debug_set_bg(u16 color) {
  128. u16* bgc = (u16*) 0x020375f8;
  129. *bgc = color;
  130. return;
  131. }
  132. /* === IMPLEMENTATIONS === */
  133. void debug_scene() {
  134. if (superstate.multi_purpose_state_tracker == 0) {
  135. print_memory->row = 0;
  136. print_memory->line = 0;
  137. print_memory->color = 0;
  138. gpu_tile_bg_drop_all_sets(0);
  139. gpu_tile_bg_drop_all_sets(1);
  140. gpu_tile_bg_drop_all_sets(2);
  141. gpu_tile_bg_drop_all_sets(3);
  142. gpu_bg_vram_setup(0, standard_bg, 4);
  143. gpu_bg_show(0);
  144. gpu_bg_show(1);
  145. gpu_bg_show(2);
  146. gpu_bg_show(3);
  147. gpu_sync_bg_visibility_and_mode();
  148. debug_reset_scrolling();
  149. obj_delete_all();
  150. memset((void*) 0x06000000, 0, 0x17fe0);
  151. memset((void*) 0x020375F8, 0, 0x400);
  152. superstate.multi_purpose_state_tracker++;
  153. } else if (superstate.multi_purpose_state_tracker == 1) {
  154. vram_decompress((void*) asciiTiles, (void*) 0x06000000);
  155. memcpy((void*) 0x020375F8, (void*) asciiPal, 0x60);
  156. debug_set_bg(0x0000);
  157. superstate.multi_purpose_state_tracker++;
  158. } else if (superstate.multi_purpose_state_tracker == 2) {
  159. debug_init_unit_test();
  160. superstate.multi_purpose_state_tracker++;
  161. }
  162. return;
  163. }
  164. u8 debug_some_test() {
  165. set_callback2(debug_scene);
  166. vblank_handler_set(debug_update);
  167. superstate.multi_purpose_state_tracker = 0;
  168. return 0;
  169. }
  170. void debug_reset_scrolling() {
  171. lcd_io_set_func(0x12, 0x0);
  172. lcd_io_set_func(0x14, 0x0);
  173. lcd_io_set_func(0x16, 0x0);
  174. lcd_io_set_func(0x18, 0x0);
  175. lcd_io_set_func(0x1A, 0x0);
  176. lcd_io_set_func(0x1C, 0x0);
  177. lcd_io_set_func(0x1E, 0x0);
  178. return;
  179. }
  180. void debug_init_unit_test() {
  181. test_speed();
  182. }
  183. void debug_print_string(u16 line, u16 row, u8 color, char* pBuf) {
  184. while (*pBuf) {
  185. debug_print_char(line, row++, *pBuf++, color);
  186. }
  187. return;
  188. }
  189. void debug_print(char* str) {
  190. while (*str) {
  191. if (*str == '\n') {
  192. print_memory->line++;
  193. print_memory->row = 0;
  194. } else if (*str == '\xFE') {
  195. str++;
  196. u8 c = *str;
  197. if (c > 2)
  198. c = 0;
  199. print_memory->color = c;
  200. } else {
  201. debug_print_char(print_memory->line, print_memory->row, *str, print_memory->color);
  202. print_memory->row++;
  203. }
  204. str++;
  205. }
  206. return;
  207. }
  208. void debug_printf(char* str, int arg) {
  209. while (*str) {
  210. if (*str == '\n') {
  211. print_memory->line++;
  212. print_memory->row = 0;
  213. } else if (*str == '\xFE') {
  214. str++;
  215. u8 c = *str;
  216. if (c > 2)
  217. c = 0;
  218. print_memory->color = c;
  219. } else if (*str == '%') {
  220. str++;
  221. if (*str == '%') {
  222. debug_print_char(print_memory->line, print_memory->row, *str, print_memory->color);
  223. print_memory->row++;
  224. } else if (*str == 'd') {
  225. u32 len = debug_dec_len(arg);
  226. char temp[debug_dec_len(len + 1)];
  227. temp[len] = 0;
  228. debug_int_to_char(arg, temp);
  229. debug_print(temp);
  230. } else if (*str == 'c') {
  231. char print_char = (char) (arg);
  232. debug_print_char(print_memory->line, print_memory->row, print_char, print_memory->color);
  233. print_memory->row++;
  234. }
  235. } else {
  236. debug_print_char(print_memory->line, print_memory->row, *str, print_memory->color);
  237. print_memory->row++;
  238. }
  239. str++;
  240. }
  241. }
  242. void debug_clean() {
  243. memset((void*) 0x0600C800, 0, 0x800);
  244. print_memory->row = 0;
  245. print_memory->line = 0;
  246. print_memory->color = 0;
  247. return;
  248. }
  249. void debug_wait_for_btn(u16 field) {
  250. volatile u16* control_io = (volatile u16*) (0x04000130);
  251. while (*control_io & field) {
  252. }
  253. return;
  254. }
  255. void debug_print_char(u16 line, u16 row, char character, u8 color) {
  256. if (color > 2)
  257. color = 0;
  258. u16 position = (32 * line) + row;
  259. union t_map_entry map_entry;
  260. map_entry.entry.tile = char_to_byte(character);
  261. map_entry.entry.pal = color;
  262. u16* ptr = (u16*) (0x0600c800 + (position * 2));
  263. *ptr = map_entry.short_map;
  264. return;
  265. }
  266. u8 char_to_byte(char character) {
  267. if (character >= 0x20 && character <= 0x7E)
  268. return character - 0x20;
  269. else
  270. return 3;
  271. }
  272. void debug_update() {
  273. fade_update();
  274. task_exec();
  275. objc_exec();
  276. obj_sync();
  277. gpu_pal_upload();
  278. obj_gpu_sprites_upload();
  279. }
  280. void debug_int_to_char(u32 i, char* ref) {
  281. if (i == 0) {
  282. ref[0] = '0';
  283. return;
  284. }
  285. u32 len = debug_dec_len(i);
  286. while (i > 0) {
  287. ref[len - 1] = '0' + (__aeabi_uidivmod(i, 10));
  288. i /= 10;
  289. len--;
  290. }
  291. return;
  292. }
  293. u32 debug_power(u32 n, u32 power) {
  294. u32 out = 1;
  295. for (int i = 0; i < power; ++i) {
  296. out = out * n;
  297. }
  298. return out;
  299. }
  300. u32 debug_dec_len(u32 i) {
  301. u32 len = 1;
  302. while ((i /= 10) > 0) {
  303. len++;
  304. }
  305. return len;
  306. }