From 035a0e6db617c0d44f7b0ab30fad0f97d0d11a69 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 19 Jul 2021 09:22:43 +0100 Subject: [PATCH] is_debug (#322) --- include/macros.h | 16 ++- src/1060_len_310.c | 88 --------------- src/1370_len_7d0.c | 2 +- src/is_debug.c | 100 ++++++++++++++++++ src/jp/1060.c | 2 + src/npc.c | 10 +- .../EC2240/func_80240158_EC2358.s | 2 +- .../ED46D0/func_802404B0_ED46D0.s | 4 +- .../ED46D0/func_80240B10_ED4D30.s | 4 +- .../ED8E20/func_802400F0_ED8F10.s | 2 +- .../func_80025F44.s | 2 +- ver/us/splat.yaml | 4 +- ver/us/symbol_addrs.txt | 10 +- 13 files changed, 134 insertions(+), 112 deletions(-) delete mode 100644 src/1060_len_310.c create mode 100644 src/is_debug.c rename ver/us/asm/nonmatchings/{1060_len_310 => is_debug}/func_80025F44.s (92%) diff --git a/include/macros.h b/include/macros.h index c7fc975ee9..a25e219ccd 100644 --- a/include/macros.h +++ b/include/macros.h @@ -16,9 +16,19 @@ #define PHYSICAL_TO_VIRTUAL(addr) (void*)((u32)(addr) + 0x80000000) #define VIRTUAL_TO_PHYSICAL(addr) (u32)((u8*)(addr) - 0x80000000) -#define ASSERT(condition) if (!(condition)) { while (1) {} } -#define PANIC() ASSERT(0) -#define STATIC_ASSERT(condition) enum { static_assert_fail = 1/(!!(condition)) } // Causes division by zero ("not integer constant") if false +#ifdef DEBUG +#define ASSERT(condition) \ + if (!(condition)) { \ + func_80025F44("Assertion failed: " #condition, __FILE__, __LINE__); \ + while (TRUE) {} \ + } +#define PANIC() \ + func_80025F44("Panic!", __FILE__, __LINE__); \ + while (TRUE) {} +#else +#define ASSERT(condition) if (!(condition)) { while (TRUE) {} } +#define PANIC() while (TRUE) {} +#endif #define BADGE_MENU_PAGE(index) (&gBadgeMenuPages[index]) #define ITEM_MENU_PAGE(index) (&gItemMenuPages[index]) diff --git a/src/1060_len_310.c b/src/1060_len_310.c deleted file mode 100644 index 3e882fb404..0000000000 --- a/src/1060_len_310.c +++ /dev/null @@ -1,88 +0,0 @@ -#include "common.h" -#include "stdlib/stdarg.h" - -u32 func_80025D74(void* arg0, const unsigned char* str, s32 count); - -void func_80025C60(void) { - osEPiWriteIo(carthandle, 0xB3FF0014, 0); - osEPiWriteIo(carthandle, 0xB3FF0004, 0); - osEPiWriteIo(carthandle, 0xB3FF0000, ASCII_TO_U32('I', 'S', '6', '4')); -} - -void func_80025CC0(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - - _Printf(func_80025D74, NULL, fmt, args); -} - -void func_80025CFC(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - - _Printf(func_80025D74, NULL, fmt, args); -} - -void func_80025D38(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - - _Printf(func_80025D74, NULL, fmt, args); -} - -u32 func_80025D74(void* arg0, const unsigned char* str, s32 count) { - u32 data; - s32 pos; - s32 start; - s32 end; - - osEPiReadIo(carthandle, 0xB3FF0000, &data); - if (data != ASCII_TO_U32('I', 'S', '6', '4')) { - return 1; - } - osEPiReadIo(carthandle, 0xB3FF0004, &data); - pos = data; - osEPiReadIo(carthandle, 0xB3FF0014, &data); - start = data; - end = start + count; - if (end >= 0xffe0) { - end -= 0xffe0; - if (pos < end || start < pos) { - return 1; - } - } else { - if (start < pos && pos < end) { - return 1; - } - } - while (count) { - if (*str) { - u32 addr = 0xB3FF0020 + (start & 0xffffffc); - s32 shift = ((3 - (start & 3)) * 8); - - osEPiReadIo(carthandle, addr, &data); - osEPiWriteIo(carthandle, addr, (data & ~(0xff << shift)) | (*str << shift)); - - start++; - if (start >= 0xffe0) { - start -= 0xffe0; - } - } - count--; - str++; - } - osEPiWriteIo(carthandle, 0xB3FF0014, start); - return 1; -} - -// Nop issue with the rodata string -#ifdef NON_MATCHING -void func_80025F44(char* arg0, char* file, s32 line) { - func_80025CFC("File:%s Line:%d %s \n", file, line, arg0); - PANIC(); -} -#else -INCLUDE_ASM(void, "1060_len_310", func_80025F44, char* arg0, char* file, s32 line); -#endif - - diff --git a/src/1370_len_7d0.c b/src/1370_len_7d0.c index 2471cd86d7..24cda1959c 100644 --- a/src/1370_len_7d0.c +++ b/src/1370_len_7d0.c @@ -31,7 +31,7 @@ void boot_main(void) { nuGfxDisplayOff(); crash_create_monitor(); - func_80025C60(); + is_debug_init(); nuGfxInit(); gGameStatusPtr->contBitPattern = nuContInit(); func_8002D160(); diff --git a/src/is_debug.c b/src/is_debug.c new file mode 100644 index 0000000000..1012ecaab7 --- /dev/null +++ b/src/is_debug.c @@ -0,0 +1,100 @@ +#include "common.h" +#include "stdlib/stdarg.h" + +typedef struct { + /* 0x00 */ u32 magic; + /* 0x04 */ u32 get; + /* 0x08 */ char unk_08[0x14-0x08]; + /* 0x14 */ u32 put; + /* 0x18 */ char unk_18[0x20-0x18]; + /* 0x20 */ char data[0x10000-0x20]; +} ISVDbg; + +#define gISVDbgPrnAdrs ((ISVDbg*) 0xb3ff0000) + +u32 is_debug_print(void* arg0, const unsigned char* str, s32 count); + +void is_debug_init(void) { + osEPiWriteIo(carthandle, (u32) &gISVDbgPrnAdrs->put, 0); + osEPiWriteIo(carthandle, (u32) &gISVDbgPrnAdrs->get, 0); + osEPiWriteIo(carthandle, (u32) &gISVDbgPrnAdrs->magic, ASCII_TO_U32('I', 'S', '6', '4')); +} + +void printf(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + _Printf(is_debug_print, NULL, fmt, args); +} + +void osSyncPrintf(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + _Printf(is_debug_print, NULL, fmt, args); +} + +void rmonPrintf(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + _Printf(is_debug_print, NULL, fmt, args); +} + +u32 is_debug_print(void* arg0, const unsigned char* str, s32 count) { + u32 data; + s32 pos; + s32 start; + s32 end; + + osEPiReadIo(carthandle, (u32) &gISVDbgPrnAdrs->magic, &data); + if (data != ASCII_TO_U32('I', 'S', '6', '4')) { + return 1; + } + osEPiReadIo(carthandle, (u32) &gISVDbgPrnAdrs->get, &data); + pos = data; + osEPiReadIo(carthandle, (u32) &gISVDbgPrnAdrs->put, &data); + start = data; + end = start + count; + if (end >= 0xffe0) { + end -= 0xffe0; + if (pos < end || start < pos) { + return 1; + } + } else { + if (start < pos && pos < end) { + return 1; + } + } + while (count) { + if (*str) { + u32 addr = (u32) &gISVDbgPrnAdrs->data + (start & 0xffffffc); + s32 shift = ((3 - (start & 3)) * 8); + + osEPiReadIo(carthandle, addr, &data); + osEPiWriteIo(carthandle, addr, (data & ~(0xff << shift)) | (*str << shift)); + + start++; + if (start >= 0xffe0) { + start -= 0xffe0; + } + } + count--; + str++; + } + osEPiWriteIo(carthandle, (u32)&gISVDbgPrnAdrs->put, start); + return 1; +} + +// Nop issue with the rodata string +#ifdef NON_MATCHING +void func_80025F44(const char* message, char* file, s32 line) { + osSyncPrintf("File:%s Line:%d %s \n", file, line, message); + + while (TRUE) { + ; + } +} +#else +INCLUDE_ASM(void, "is_debug", func_80025F44, char* arg0, char* file, s32 line); +#endif diff --git a/src/jp/1060.c b/src/jp/1060.c index 80d2b53128..520f827f75 100644 --- a/src/jp/1060.c +++ b/src/jp/1060.c @@ -1,6 +1,8 @@ #include "common.h" #include "stdlib/stdarg.h" +// TODO: use src/is_debug.c instead as this file is identical between versions + u32 func_80025D74(void* arg0, const unsigned char* str, s32 count); void func_80025C60(void) { diff --git a/src/npc.c b/src/npc.c index 31d1436fd9..1a732bed99 100644 --- a/src/npc.c +++ b/src/npc.c @@ -194,10 +194,9 @@ void free_npc_by_index(s32 listIndex) { } if (!(npc->flags & NPC_FLAG_NO_ANIMS_LOADED)) { - if (!(npc->flags & NPC_FLAG_1000000) && spr_free_sprite(npc->spriteInstanceID)) { - PANIC(); - } + ASSERT((npc->flags & NPC_FLAG_1000000) || !spr_free_sprite(npc->spriteInstanceID)); } + delete_shadow(npc->shadowIndex); for (i = 0; i < 2; i++) { @@ -224,10 +223,9 @@ void free_npc(Npc* npc) { } if (!(npc->flags & NPC_FLAG_NO_ANIMS_LOADED)) { - if (!(npc->flags & NPC_FLAG_1000000) && spr_free_sprite(npc->spriteInstanceID) != 0) { - PANIC(); - } + ASSERT((npc->flags & NPC_FLAG_1000000) || !spr_free_sprite(npc->spriteInstanceID)); } + delete_shadow(npc->shadowIndex); for (i = 0; i < 2; i++) { diff --git a/ver/us/asm/nonmatchings/EC2240/func_80240158_EC2358.s b/ver/us/asm/nonmatchings/EC2240/func_80240158_EC2358.s index e686f58f40..b0a2ebfa73 100644 --- a/ver/us/asm/nonmatchings/EC2240/func_80240158_EC2358.s +++ b/ver/us/asm/nonmatchings/EC2240/func_80240158_EC2358.s @@ -138,7 +138,7 @@ glabel func_80240158_EC2358 /* EC2524 80240324 00000000 */ nop .L80240328: /* EC2528 80240328 3C048024 */ lui $a0, %hi(D_80244F88_EC7188) -/* EC252C 8024032C 0C00973F */ jal func_80025CFC +/* EC252C 8024032C 0C00973F */ jal osSyncPrintf /* EC2530 80240330 24844F88 */ addiu $a0, $a0, %lo(D_80244F88_EC7188) .L80240334: /* EC2534 80240334 C60C0038 */ lwc1 $f12, 0x38($s0) diff --git a/ver/us/asm/nonmatchings/ED46D0/func_802404B0_ED46D0.s b/ver/us/asm/nonmatchings/ED46D0/func_802404B0_ED46D0.s index 9ee22dcc77..fdb40138b9 100644 --- a/ver/us/asm/nonmatchings/ED46D0/func_802404B0_ED46D0.s +++ b/ver/us/asm/nonmatchings/ED46D0/func_802404B0_ED46D0.s @@ -144,7 +144,7 @@ glabel func_802404B0_ED46D0 /* ED48CC 802406AC 3C058024 */ lui $a1, %hi(D_80244BEC_ED8E0C) /* ED48D0 802406B0 24A54BEC */ addiu $a1, $a1, %lo(D_80244BEC_ED8E0C) /* ED48D4 802406B4 24060053 */ addiu $a2, $zero, 0x53 -/* ED48D8 802406B8 0C00973F */ jal func_80025CFC +/* ED48D8 802406B8 0C00973F */ jal osSyncPrintf /* ED48DC 802406BC 0040382D */ daddu $a3, $v0, $zero /* ED48E0 802406C0 3C02FD10 */ lui $v0, 0xfd10 .L802406C4: @@ -308,7 +308,7 @@ glabel func_802404B0_ED46D0 /* ED4B4C 8024092C 3C058024 */ lui $a1, %hi(D_80244BEC_ED8E0C) /* ED4B50 80240930 24A54BEC */ addiu $a1, $a1, %lo(D_80244BEC_ED8E0C) /* ED4B54 80240934 2406006B */ addiu $a2, $zero, 0x6b -/* ED4B58 80240938 0C00973F */ jal func_80025CFC +/* ED4B58 80240938 0C00973F */ jal osSyncPrintf /* ED4B5C 8024093C 0040382D */ daddu $a3, $v0, $zero /* ED4B60 80240940 3C02FD10 */ lui $v0, 0xfd10 .L80240944: diff --git a/ver/us/asm/nonmatchings/ED46D0/func_80240B10_ED4D30.s b/ver/us/asm/nonmatchings/ED46D0/func_80240B10_ED4D30.s index 2d993b41e8..1e02fc52f1 100644 --- a/ver/us/asm/nonmatchings/ED46D0/func_80240B10_ED4D30.s +++ b/ver/us/asm/nonmatchings/ED46D0/func_80240B10_ED4D30.s @@ -358,7 +358,7 @@ glabel func_80240B10_ED4D30 /* ED5270 80241050 3C058024 */ lui $a1, %hi(D_80244BEC_ED8E0C) /* ED5274 80241054 24A54BEC */ addiu $a1, $a1, %lo(D_80244BEC_ED8E0C) /* ED5278 80241058 240600AF */ addiu $a2, $zero, 0xaf -/* ED527C 8024105C 0C00973F */ jal func_80025CFC +/* ED527C 8024105C 0C00973F */ jal osSyncPrintf /* ED5280 80241060 0040382D */ daddu $a3, $v0, $zero .L80241064: /* ED5284 80241064 36B5013F */ ori $s5, $s5, 0x13f @@ -417,7 +417,7 @@ glabel func_80240B10_ED4D30 /* ED5358 80241138 3C058024 */ lui $a1, %hi(D_80244BEC_ED8E0C) /* ED535C 8024113C 24A54BEC */ addiu $a1, $a1, %lo(D_80244BEC_ED8E0C) /* ED5360 80241140 240600BA */ addiu $a2, $zero, 0xba -/* ED5364 80241144 0C00973F */ jal func_80025CFC +/* ED5364 80241144 0C00973F */ jal osSyncPrintf /* ED5368 80241148 0040382D */ daddu $a3, $v0, $zero .L8024114C: /* ED536C 8024114C 8E840000 */ lw $a0, ($s4) diff --git a/ver/us/asm/nonmatchings/ED8E20/func_802400F0_ED8F10.s b/ver/us/asm/nonmatchings/ED8E20/func_802400F0_ED8F10.s index 4008b8908c..dce9fa4741 100644 --- a/ver/us/asm/nonmatchings/ED8E20/func_802400F0_ED8F10.s +++ b/ver/us/asm/nonmatchings/ED8E20/func_802400F0_ED8F10.s @@ -70,7 +70,7 @@ glabel func_802400F0_ED8F10 /* ED8FF8 802401D8 8E060084 */ lw $a2, 0x84($s0) /* ED8FFC 802401DC 8E070088 */ lw $a3, 0x88($s0) /* ED9000 802401E0 3C048024 */ lui $a0, %hi(D_80243170_EDBF90) -/* ED9004 802401E4 0C00973F */ jal func_80025CFC +/* ED9004 802401E4 0C00973F */ jal osSyncPrintf /* ED9008 802401E8 24843170 */ addiu $a0, $a0, %lo(D_80243170_EDBF90) /* ED900C 802401EC 8FBF002C */ lw $ra, 0x2c($sp) /* ED9010 802401F0 8FB20028 */ lw $s2, 0x28($sp) diff --git a/ver/us/asm/nonmatchings/1060_len_310/func_80025F44.s b/ver/us/asm/nonmatchings/is_debug/func_80025F44.s similarity index 92% rename from ver/us/asm/nonmatchings/1060_len_310/func_80025F44.s rename to ver/us/asm/nonmatchings/is_debug/func_80025F44.s index 6eb63ff317..382f96f650 100644 --- a/ver/us/asm/nonmatchings/1060_len_310/func_80025F44.s +++ b/ver/us/asm/nonmatchings/is_debug/func_80025F44.s @@ -14,7 +14,7 @@ glabel func_80025F44 /* 134C 80025F4C 0080382D */ daddu $a3, $a0, $zero /* 1350 80025F50 3C048009 */ lui $a0, %hi(D_80097D30) /* 1354 80025F54 24847D30 */ addiu $a0, $a0, %lo(D_80097D30) -/* 1358 80025F58 0C00973F */ jal func_80025CFC +/* 1358 80025F58 0C00973F */ jal osSyncPrintf /* 135C 80025F5C 00000000 */ nop .L80025F60: /* 1360 80025F60 080097D8 */ j .L80025F60 diff --git a/ver/us/splat.yaml b/ver/us/splat.yaml index 71b6bcf84e..f655a9b494 100644 --- a/ver/us/splat.yaml +++ b/ver/us/splat.yaml @@ -40,7 +40,7 @@ segments: vram: 0x80025C00 subsegments: - [0x1000, hasm, entry_point] - - [0x1060, c, 1060_len_310] + - [0x1060, c, is_debug] - [0x1370, c, 1370_len_7d0] - [0x1b40, c, main_loop] - [0x3bf0, c, input] @@ -290,7 +290,7 @@ segments: - [0x6FA70, data] # has scripts? - [0x70E30, .data, battle/battle] - [0x71430, data] - - [0x73130, .rodata, 1060_len_310] + - [0x73130, .rodata, is_debug] - [0x73148, .rodata, main_loop] - [0x73160, .rodata, 43F0] - [0x73200, .rodata, curtains] diff --git a/ver/us/symbol_addrs.txt b/ver/us/symbol_addrs.txt index 39b70f2c32..189339e06c 100644 --- a/ver/us/symbol_addrs.txt +++ b/ver/us/symbol_addrs.txt @@ -1,9 +1,9 @@ entry_point = 0x80025C00; // type:func rom:0x1000 -func_80025C60 = 0x80025C60; // type:func rom:0x1060 -func_80025CC0 = 0x80025CC0; // type:func rom:0x10C0 -func_80025CFC = 0x80025CFC; // type:func rom:0x10FC -func_80025D38 = 0x80025D38; // type:func rom:0x1138 -func_80025D74 = 0x80025D74; // type:func rom:0x1174 +is_debug_init = 0x80025C60; // type:func rom:0x1060 +printf = 0x80025CC0; // type:func rom:0x10C0 +osSyncPrintf = 0x80025CFC; // type:func rom:0x10FC +rmonPrintf = 0x80025D38; // type:func rom:0x1138 +is_debug_print = 0x80025D74; // type:func rom:0x1174 func_80025F44 = 0x80025F44; // type:func rom:0x1344 boot_main = 0x80025F70; // type:func rom:0x1370 gfxRetrace_Callback = 0x8002605C; // type:func rom:0x145C