From 77dd0045fa33b13527c258d8221b418bb4a8e8ff Mon Sep 17 00:00:00 2001 From: CrashOveride95 Date: Sun, 12 Sep 2021 19:33:32 -0400 Subject: [PATCH] Fix UNF by moving it's code to engine segment Additionally update UNF code to latest master --- README.md | 2 ++ sm64.ld | 8 ++++---- src/boot/main.c | 7 ++++--- src/boot/memory.c | 4 ---- src/game/game_init.c | 4 ---- src/usb/debug.c | 11 +++++++--- src/usb/usb.c | 48 ++++++++++++++++++++++---------------------- src/usb/usb.h | 2 +- 8 files changed, 43 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 42e6b56c..37499626 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ To build with UNF, run make with ``UNF=1``. Further instructions can be found at the [official repository](https://github.com/buu342/N64-UNFLoader) +**NOTE: Closing the UNFLoader window will result in your game eventually hanging due to lacking a USB device to send messages to, so beware of that** + ## Multi-Save support The repository supports SRAM in addition to EEPROM. The standard save data functions are #ifdef'd to accommedate this. diff --git a/sm64.ld b/sm64.ld index b96f01ec..a6d2a0fb 100755 --- a/sm64.ld +++ b/sm64.ld @@ -132,7 +132,6 @@ SECTIONS BUILD_DIR/src/boot*.o(.text); BUILD_DIR/src/hvqm*.o(.text); - BUILD_DIR/src/usb*.o(.text); BUILD_DIR/src/audio*.o(.text); #if defined(ISVPRINT) || defined(UNF) */libultra_d.a:*.o(.text); @@ -148,7 +147,6 @@ SECTIONS /* data */ BUILD_DIR/src/boot*.o(.*data*); - BUILD_DIR/src/usb*.o(.*data*); BUILD_DIR/src/audio*.o(.*data*); #if defined(ISVPRINT) || defined(UNF) */libultra_d.a:*.o(.*data*); @@ -163,7 +161,6 @@ SECTIONS /* rodata */ BUILD_DIR/src/boot*.o(.rodata*); - BUILD_DIR/src/usb*.o(.rodata*); BUILD_DIR/src/audio*.o(.rodata*); #if defined(ISVPRINT) || defined(UNF) */libultra_d.a:*.o(.*rodata*); @@ -181,7 +178,6 @@ SECTIONS { BUILD_DIR/src/boot*.o(.*bss*); BUILD_DIR/src/hvqm*.o(.*bss*); - BUILD_DIR/src/usb*.o(.*bss*); BUILD_DIR/src/audio*.o(.*bss*); #if defined(ISVPRINT) || defined(UNF) */libultra_d.a:*.o(COMMON); @@ -206,19 +202,23 @@ SECTIONS { BUILD_DIR/src/game*.o(.text); BUILD_DIR/src/engine*.o(.text); + BUILD_DIR/src/usb*.o(.text); /* data */ BUILD_DIR/src/game*.o(.*data*); BUILD_DIR/src/engine*.o(.data*); BUILD_DIR/src/engine*.o(.sdata*); + BUILD_DIR/src/usb*.o(.*data*); /* rodata */ BUILD_DIR/src/game*.o(.rodata*); BUILD_DIR/src/engine*.o(.rodata*); + BUILD_DIR/src/usb*.o(.rodata*); } END_SEG(engine) BEGIN_NOLOAD(engine) { BUILD_DIR/src/game*.o(.*bss*); BUILD_DIR/src/engine*.o(.bss*); + BUILD_DIR/src/usb*.o(.*bss*); . = ALIGN(0x40); } END_NOLOAD(engine) diff --git a/src/boot/main.c b/src/boot/main.c index 73b0cc04..65878bd4 100644 --- a/src/boot/main.c +++ b/src/boot/main.c @@ -319,6 +319,10 @@ void thread3_main(UNUSED void *arg) { crash_screen_init(); #endif +#ifdef UNF + debug_initialize(); +#endif + #ifdef DEBUG osSyncPrintf("Super Mario 64\n"); osSyncPrintf("Built by: %s\n", __username__); @@ -462,9 +466,6 @@ void thread1_idle(UNUSED void *arg) { osViSetSpecialFeatures(OS_VI_DITHER_FILTER_ON); osViSetSpecialFeatures(OS_VI_GAMMA_OFF); osCreatePiManager(OS_PRIORITY_PIMGR, &gPIMesgQueue, gPIMesgBuf, ARRAY_COUNT(gPIMesgBuf)); -#ifdef UNF - debug_initialize(); -#endif create_thread(&gMainThread, 3, thread3_main, NULL, gThread3Stack + 0x2000, 100); osStartThread(&gMainThread); diff --git a/src/boot/memory.c b/src/boot/memory.c index 367104b6..90c36741 100644 --- a/src/boot/memory.c +++ b/src/boot/memory.c @@ -17,10 +17,6 @@ #if defined(RNC1) || defined(RNC2) #include #endif -#ifdef UNF -#include "usb/usb.h" -#include "usb/debug.h" -#endif // round up to the next multiple diff --git a/src/game/game_init.c b/src/game/game_init.c index 4337d051..bc5bc79f 100644 --- a/src/game/game_init.c +++ b/src/game/game_init.c @@ -22,10 +22,6 @@ #ifdef HVQM #include #endif -#ifdef UNF -#include "usb/usb.h" -#include "usb/debug.h" -#endif #ifdef SRAM #include "sram.h" #endif diff --git a/src/usb/debug.c b/src/usb/debug.c index 68e0d94c..e6bd4e94 100644 --- a/src/usb/debug.c +++ b/src/usb/debug.c @@ -449,7 +449,11 @@ https://github.com/buu342/N64-UNFLoader ==============================*/ void _debug_assert(const char* expression, const char* file, int line) - { + { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" + volatile char crash; + // Set the assert data assert_expr = expression; assert_line = line; @@ -460,8 +464,9 @@ https://github.com/buu342/N64-UNFLoader debug_printf("Assertion failed in file '%s', line %d.\n", assert_file, assert_line); #endif - // Intentionally cause a null pointer exception - *((char*)(NULL)) = 0; + // Intentionally cause a TLB exception on load/instruction fetch + crash = *(volatile char *)1; +#pragma GCC diagnostic pop } diff --git a/src/usb/usb.c b/src/usb/usb.c index 43bfdb00..46f56cf9 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -385,20 +385,7 @@ static void usb_findcart() // Check if we have an EverDrive if (buff == ED7_VERSION || buff == ED3_VERSION) - { - // Initialize the PI - IO_WRITE(PI_STATUS_REG, 3); - IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); - IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x04); - IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x0C); - + { // Set the USB mode usb_everdrive_writereg(ED_REG_SYSCFG, 0); usb_everdrive_writereg(ED_REG_USBCFG, ED_USBMODE_RDNOP); @@ -453,7 +440,7 @@ void usb_write(int datatype, const void* data, int size) @return The data header, or 0 ==============================*/ -u32 usb_poll() +unsigned long usb_poll() { // If no debug cart exists, stop if (usb_cart == CART_NONE) @@ -604,7 +591,7 @@ static s8 usb_64drive_wait() #endif // Took too long, abort - if((timeout++) > 1000000) + if((timeout++) > 10000) return -1; } while((ret >> 8) & D64_CI_BUSY); @@ -642,9 +629,10 @@ static void usb_64drive_setwritable(u8 enable) Waits for the 64Drive's USB to be idle ==============================*/ -static void usb_64drive_waitidle() +static int usb_64drive_waitidle() { u32 status __attribute__((aligned(8))); + u32 timeout = 0; do { #ifdef LIBDRAGON @@ -657,8 +645,11 @@ static void usb_64drive_waitidle() #endif #endif status = (status >> 4) & D64_USB_BUSY; + if (timeout++ > 128) + return 0; } while(status != D64_USB_IDLE); + return 1; } @@ -724,7 +715,8 @@ static void usb_64drive_write(int datatype, const void* data, int size) int read = 0; // Spin until the write buffer is free and then set the cartridge to write mode - usb_64drive_waitidle(); + if (!usb_64drive_waitidle()) + return; usb_64drive_setwritable(TRUE); // Write data to SDRAM until we've finished @@ -749,7 +741,11 @@ static void usb_64drive_write(int datatype, const void* data, int size) } // Spin until the write buffer is free - usb_64drive_waitidle(); + if (!usb_64drive_waitidle()) + { + usb_64drive_setwritable(FALSE); + return; + } // Set up DMA transfer between RDRAM and the PI #ifdef LIBDRAGON @@ -1062,7 +1058,8 @@ static void usb_everdrive_writedata(void* buff, u32 pi_address, u32 len) static void usb_everdrive_writereg(u64 reg, u32 value) { - usb_everdrive_writedata(&value, ED_GET_REGADD(reg), sizeof(u32)); + u32 val __attribute__((aligned(8))) = value; + usb_everdrive_writedata(&val, ED_GET_REGADD(reg), sizeof(u32)); } @@ -1073,12 +1070,15 @@ static void usb_everdrive_writereg(u64 reg, u32 value) static void usb_everdrive_usbbusy() { + u32 timeout = 0; u32 val __attribute__((aligned(8))); - do + do { usb_everdrive_readreg(ED_REG_USBCFG, &val); - } - while ((val & ED_USBSTAT_ACT) != 0); + if (timeout++ != 8192) + continue; + usb_everdrive_writereg(ED_REG_USBCFG, ED_USBMODE_RDNOP); + } while ((val & ED_USBSTAT_ACT) != 0); } @@ -1690,4 +1690,4 @@ static void usb_sc64_read(void) // Invalidate cache osInvalDCache(usb_buffer, BUFFER_SIZE); #endif -} \ No newline at end of file +} diff --git a/src/usb/usb.h b/src/usb/usb.h index 3b2ea8ab..813b1b28 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -80,7 +80,7 @@ @return The data header, or 0 ==============================*/ - extern unsigned int usb_poll(); + extern unsigned long usb_poll(); /*==============================