Bez popisu

debug.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. /* === STRUCTURES === */
  39. struct assert_memory
  40. {
  41. char* file;
  42. int line;
  43. char* expression;
  44. };
  45. /* === STATICS === */
  46. struct assert_memory* assert_global = (struct assert_memory*)0x0203FEC4;
  47. static struct print_engine* print_memory = (struct print_engine*)(0x0203FFF0);
  48. /* === PROTOTYPES === */
  49. /**
  50. * @brief convert int to char
  51. * @param i integer
  52. * @param ref buffer to output to
  53. */
  54. void debug_int_to_char(u32 i, char* ref);
  55. /**
  56. * @get length of integer
  57. * @param i integer
  58. * @return length
  59. */
  60. u32 debug_dec_len(u32 i);
  61. /**
  62. * @brief update debug environment
  63. */
  64. void debug_update();
  65. /**
  66. * @brief reset scrolling (from overworld e.g.)
  67. */
  68. void debug_reset_scrolling();
  69. /**
  70. * @brief start a unit test function
  71. */
  72. void debug_init_unit_test();
  73. /**
  74. * @brief handle for the debug scene
  75. */
  76. void debug_scene();
  77. /**
  78. * @brief convert character to byte
  79. * @param character character
  80. * @return byte from character
  81. */
  82. u8 char_to_byte(char character);
  83. /**
  84. * @brief print a character on the debug environment
  85. * @param line line to print to
  86. * @param row row to print to
  87. * @param character character to print
  88. * @param color color to print in
  89. */
  90. void debug_print_char(u16 line, u16 row, char character, u8 color);
  91. /**
  92. * @brief print a string to the debug environment
  93. * @param line line to print to
  94. * @param row row to start
  95. * @param color color to print in
  96. * @param pBuf string buffer to print (null terminated)
  97. */
  98. void debug_print_string(u16 line, u16 row, u8 color, char* pBuf);
  99. /**
  100. * @brief build power
  101. * @param n integer to power
  102. * @param power exponent
  103. * @return n^power
  104. */
  105. u32 debug_power(u32 n, u32 power);
  106. void debug_init_stage_one();
  107. void debug_init_stage_two();
  108. /**
  109. * @brief set bg color of debug environment
  110. * @param color color to set to
  111. */
  112. void debug_set_bg(u16 color) {
  113. u16* bgc = (u16*) 0x020375f8;
  114. *bgc = color;
  115. return;
  116. }
  117. /* === STATIC STRUCTURES === */
  118. static struct bg_config debug_bg_config[4] = {
  119. {0, 0, 0x19, 0, 0, 0},
  120. {1, 1, 0x1A, 0, 0, 1},
  121. {2, 2, 0x1B, 0, 0, 2},
  122. {3, 3, 0x1C, 0, 0, 3}
  123. };
  124. /* === IMPLEMENTATIONS === */
  125. void as_assert(char* expression, char* file, int line)
  126. {
  127. assert_global->file = file;
  128. assert_global->line = line;
  129. assert_global->expression = expression;
  130. set_callback2(debug_assert_scene);
  131. vblank_handler_set(debug_update);
  132. superstate.multi_purpose_state_tracker = 0;
  133. }
  134. void debug_scene() {
  135. if (superstate.multi_purpose_state_tracker == 0) {
  136. debug_init_stage_one();
  137. superstate.multi_purpose_state_tracker++;
  138. } else if (superstate.multi_purpose_state_tracker == 1) {
  139. debug_init_stage_two();
  140. superstate.multi_purpose_state_tracker++;
  141. } else if (superstate.multi_purpose_state_tracker == 2) {
  142. debug_init_unit_test();
  143. superstate.multi_purpose_state_tracker++;
  144. }
  145. return;
  146. }
  147. void debug_some_test() {
  148. set_callback2(debug_scene);
  149. vblank_handler_set(debug_update);
  150. superstate.multi_purpose_state_tracker = 0;
  151. return;
  152. }
  153. void debug_reset_scrolling() {
  154. lcd_io_set_func(0x12, 0x0);
  155. lcd_io_set_func(0x14, 0x0);
  156. lcd_io_set_func(0x16, 0x0);
  157. lcd_io_set_func(0x18, 0x0);
  158. lcd_io_set_func(0x1A, 0x0);
  159. lcd_io_set_func(0x1C, 0x0);
  160. lcd_io_set_func(0x1E, 0x0);
  161. return;
  162. }
  163. void debug_init_unit_test() {
  164. test_speed();
  165. }
  166. void debug_print_string(u16 line, u16 row, u8 color, char* pBuf) {
  167. while (*pBuf) {
  168. debug_print_char(line, row++, *pBuf++, color);
  169. }
  170. return;
  171. }
  172. void debug_print(char* str) {
  173. while (*str) {
  174. if(print_memory->row > 29)
  175. {
  176. print_memory->line++;
  177. print_memory->row = 0;
  178. }
  179. if (*str == '\n') {
  180. print_memory->line++;
  181. print_memory->row = 0;
  182. } if (*str == '\xFE') {
  183. str++;
  184. u8 c = *str;
  185. if (c > 2)
  186. c = 0;
  187. print_memory->color = c;
  188. } else {
  189. debug_print_char(print_memory->line, print_memory->row, *str, print_memory->color);
  190. print_memory->row++;
  191. }
  192. str++;
  193. }
  194. return;
  195. }
  196. void debug_printf(char* str, int arg) {
  197. while (*str) {
  198. if(print_memory->row > 29)
  199. {
  200. print_memory->line++;
  201. print_memory->row = 0;
  202. }
  203. if (*str == '\n') {
  204. print_memory->line++;
  205. print_memory->row = 0;
  206. } if (*str == '\xFE') {
  207. str++;
  208. u8 c = *str;
  209. if (c > 2)
  210. c = 0;
  211. print_memory->color = c;
  212. } else if (*str == '%') {
  213. str++;
  214. if (*str == '%') {
  215. debug_print_char(print_memory->line, print_memory->row, *str, print_memory->color);
  216. print_memory->row++;
  217. } else if (*str == 'd') {
  218. u32 len = debug_dec_len(arg);
  219. char temp[debug_dec_len(len + 1)];
  220. temp[len] = 0;
  221. debug_int_to_char(arg, temp);
  222. debug_print(temp);
  223. } else if (*str == 'c') {
  224. char print_char = (char) (arg);
  225. debug_print_char(print_memory->line, print_memory->row, print_char, print_memory->color);
  226. print_memory->row++;
  227. }
  228. } else {
  229. debug_print_char(print_memory->line, print_memory->row, *str, print_memory->color);
  230. print_memory->row++;
  231. }
  232. str++;
  233. }
  234. }
  235. void debug_clean() {
  236. memset((void*) 0x0600C800, 0, 0x800);
  237. print_memory->row = 0;
  238. print_memory->line = 0;
  239. print_memory->color = 0;
  240. return;
  241. }
  242. void debug_wait_for_btn(u16 field) {
  243. volatile u16* control_io = (volatile u16*) (0x04000130);
  244. while (*control_io & field) {
  245. }
  246. return;
  247. }
  248. void debug_init_stage_one()
  249. {
  250. print_memory->row = 0;
  251. print_memory->line = 0;
  252. print_memory->color = 0;
  253. gpu_tile_bg_drop_all_sets(0);
  254. gpu_tile_bg_drop_all_sets(1);
  255. gpu_tile_bg_drop_all_sets(2);
  256. gpu_tile_bg_drop_all_sets(3);
  257. gpu_bg_vram_setup(0, debug_bg_config, 4);
  258. gpu_bg_show(0);
  259. gpu_bg_show(1);
  260. gpu_bg_show(2);
  261. gpu_bg_show(3);
  262. gpu_sync_bg_visibility_and_mode();
  263. debug_reset_scrolling();
  264. obj_delete_all();
  265. memset((void*) 0x06000000, 0, 0x17fe0);
  266. memset((void*) 0x020375F8, 0, 0x400);
  267. }
  268. void debug_init_stage_two()
  269. {
  270. vram_decompress((void*) asciiTiles, (void*) 0x06000000);
  271. memcpy((void*) 0x020375F8, (void*) asciiPal, 0x60);
  272. debug_set_bg(0x0000);
  273. }
  274. void debug_assert_scene() {
  275. if (superstate.multi_purpose_state_tracker == 0) {
  276. debug_init_stage_one();
  277. superstate.multi_purpose_state_tracker++;
  278. } else if (superstate.multi_purpose_state_tracker == 1) {
  279. debug_init_stage_two();
  280. superstate.multi_purpose_state_tracker++;
  281. } else if (superstate.multi_purpose_state_tracker == 2) {
  282. debug_print("Assertion Failed: ");
  283. debug_print(assert_global->expression);
  284. debug_print("\n\nFile: ");
  285. debug_print(assert_global->file);
  286. debug_printf("\n\nLine: %d", assert_global->line);
  287. superstate.multi_purpose_state_tracker++;
  288. }
  289. /* Loop Endlessly in this stage */
  290. return;
  291. }
  292. void debug_print_char(u16 line, u16 row, char character, u8 color) {
  293. if (color > 2)
  294. color = 0;
  295. u16 position = (32 * line) + row;
  296. union t_map_entry map_entry;
  297. map_entry.entry.tile = char_to_byte(character);
  298. map_entry.entry.pal = color;
  299. u16* ptr = (u16*) (0x0600c800 + (position * 2));
  300. *ptr = map_entry.short_map;
  301. return;
  302. }
  303. u8 char_to_byte(char character) {
  304. if (character >= 0x20 && character <= 0x7E)
  305. return character - 0x20;
  306. else
  307. return 3;
  308. }
  309. void debug_update() {
  310. fade_update();
  311. task_exec();
  312. objc_exec();
  313. obj_sync();
  314. gpu_pal_upload();
  315. obj_gpu_sprites_upload();
  316. }
  317. void debug_int_to_char(u32 i, char* ref) {
  318. if (i == 0) {
  319. ref[0] = '0';
  320. return;
  321. }
  322. u32 len = debug_dec_len(i);
  323. while (i > 0) {
  324. ref[len - 1] = '0' + (__aeabi_uidivmod(i, 10));
  325. i /= 10;
  326. len--;
  327. }
  328. return;
  329. }
  330. u32 debug_power(u32 n, u32 power) {
  331. u32 out = 1;
  332. for (int i = 0; i < power; ++i) {
  333. out = out * n;
  334. }
  335. return out;
  336. }
  337. u32 debug_dec_len(u32 i) {
  338. u32 len = 1;
  339. while ((i /= 10) > 0) {
  340. len++;
  341. }
  342. return len;
  343. }