From 02010f1cbb0556e55e1d5bf55c23c275336ef8ab Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Tue, 11 Jul 2023 10:58:49 +0200 Subject: [PATCH] SC64 libcart PI setting fix --- src/flashcart/flashcart.c | 28 +++++++++++++++++----------- src/flashcart/flashcart.h | 2 +- src/flashcart/sc64/sc64.c | 7 +++++++ src/menu/views/credits.c | 2 +- src/menu/views/error.c | 2 +- src/menu/views/file_info.c | 6 ++---- src/menu/views/load.c | 5 ++--- src/menu/views/player.c | 3 +-- src/menu/views/system_info.c | 3 ++- 9 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/flashcart/flashcart.c b/src/flashcart/flashcart.c index 10781fd4..a8635059 100644 --- a/src/flashcart/flashcart.c +++ b/src/flashcart/flashcart.c @@ -38,18 +38,12 @@ static flashcart_t *flashcart = &((flashcart_t) { flashcart_error_t flashcart_init (void) { + flashcart_error_t error; + if (usb_initialize() == CART_NONE) { return FLASHCART_ERROR_NOT_DETECTED; } -#ifndef NDEBUG - assertf(debug_init_usblog(), "Couldn't initialize USB debugging"); -#endif - - if (!debug_init_sdfs("sd:/", -1)) { - return FLASHCART_ERROR_SD_CARD_ERROR; - } - switch (usb_getcart()) { case CART_64DRIVE: case CART_EVERDRIVE: @@ -63,7 +57,19 @@ flashcart_error_t flashcart_init (void) { return FLASHCART_ERROR_UNSUPPORTED; } - return flashcart->init(); + if ((error = flashcart->init()) != FLASHCART_OK) { + return error; + } + + if (!debug_init_sdfs("sd:/", -1)) { + return FLASHCART_ERROR_SD_CARD_ERROR; + } + +#ifndef NDEBUG + assertf(debug_init_usblog(), "Couldn't initialize USB debugging"); +#endif + + return FLASHCART_OK; } flashcart_error_t flashcart_deinit (void) { @@ -80,7 +86,7 @@ flashcart_error_t flashcart_load_rom (char *rom_path, bool byte_swap) { return flashcart->load_rom(rom_path, byte_swap); } -flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type, bool save_writeback) { +flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type) { flashcart_error_t error; uint32_t sectors[WRITEBACK_MAX_SECTORS] __attribute__((aligned(8))); @@ -110,7 +116,7 @@ flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t sa return error; } - if (save_writeback) { + if (flashcart->set_save_writeback) { if (file_get_sectors(save_path, sectors, WRITEBACK_MAX_SECTORS)) { return FLASHCART_ERROR_LOAD; } diff --git a/src/flashcart/flashcart.h b/src/flashcart/flashcart.h index 8cb68804..79a85489 100644 --- a/src/flashcart/flashcart.h +++ b/src/flashcart/flashcart.h @@ -41,7 +41,7 @@ typedef struct { flashcart_error_t flashcart_init (void); flashcart_error_t flashcart_deinit (void); flashcart_error_t flashcart_load_rom (char *rom_path, bool byte_swap); -flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type, bool save_writeback); +flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type); #endif diff --git a/src/flashcart/sc64/sc64.c b/src/flashcart/sc64/sc64.c index 291567d0..bfe28467 100644 --- a/src/flashcart/sc64/sc64.c +++ b/src/flashcart/sc64/sc64.c @@ -61,6 +61,12 @@ static flashcart_error_t sc64_init (void) { uint16_t major; uint16_t minor; + // HACK: Because libcart reads PI config from address 0x10000000 when initializing + // we need to write safe value before running any libcart function. + // Data in SDRAM can be undefined at this point and result in setting incorrect PI config. + extern uint32_t cart_dom1; + cart_dom1 = 0x80371240; + sc64_unlock(); if (!sc64_check_presence()) { @@ -87,6 +93,7 @@ static flashcart_error_t sc64_init (void) { } static flashcart_error_t sc64_deinit (void) { + sc64_set_config(CFG_ROM_WRITE_ENABLE, false); sc64_lock(); return FLASHCART_OK; } diff --git a/src/menu/views/credits.c b/src/menu/views/credits.c index efdc208a..a81b24d8 100644 --- a/src/menu/views/credits.c +++ b/src/menu/views/credits.c @@ -1,8 +1,8 @@ #include +#include "../menu_res_setup.h" #include "views.h" -#include "../menu_res_setup.h" #ifndef MENU_VERSION #define MENU_VERSION "0.0.0.4" diff --git a/src/menu/views/error.c b/src/menu/views/error.c index 26408fa1..d096bcb5 100644 --- a/src/menu/views/error.c +++ b/src/menu/views/error.c @@ -1,7 +1,7 @@ #include -#include "views.h" #include "../menu_res_setup.h" +#include "views.h" static void process (menu_t *menu) { diff --git a/src/menu/views/file_info.c b/src/menu/views/file_info.c index 947bc12b..4287db9d 100644 --- a/src/menu/views/file_info.c +++ b/src/menu/views/file_info.c @@ -1,13 +1,11 @@ #include #include -#include "views.h" - #include "../menu_res_setup.h" -#include "../../utils/str_utils.h" #include "../rom_database.h" - #include "fragments/fragments.h" +#include "utils/str_utils.h" +#include "views.h" static FILINFO info; diff --git a/src/menu/views/load.c b/src/menu/views/load.c index 87dd23cf..4dccc0bc 100644 --- a/src/menu/views/load.c +++ b/src/menu/views/load.c @@ -1,10 +1,9 @@ #include +#include "../rom_database.h" #include "flashcart/flashcart.h" - #include "fragments/fragments.h" #include "views.h" -#include "../rom_database.h" static bool load_pending; @@ -101,7 +100,7 @@ static void load (menu_t *menu) { uint8_t save_type = rom_db_match_save_type(rom_header); path_ext_replace(path, "sav"); - menu->flashcart_error = flashcart_load_save(path_get(path), convert_save_type(save_type), true); + menu->flashcart_error = flashcart_load_save(path_get(path), convert_save_type(save_type)); if (menu->flashcart_error != FLASHCART_OK) { menu->next_mode = MENU_MODE_FAULT; path_free(path); diff --git a/src/menu/views/player.c b/src/menu/views/player.c index 5f3f7c20..4ac13564 100644 --- a/src/menu/views/player.c +++ b/src/menu/views/player.c @@ -1,10 +1,9 @@ #include +#include "../mp3player.h" #include "fragments/fragments.h" #include "views.h" -#include "../mp3player.h" - #define SEEK_SECONDS 10 #define SEEK_UNMUTE_TIMEOUT 17 diff --git a/src/menu/views/system_info.c b/src/menu/views/system_info.c index 13b0a8be..115aa9c8 100644 --- a/src/menu/views/system_info.c +++ b/src/menu/views/system_info.c @@ -1,8 +1,9 @@ #include + #include -#include "views.h" #include "../menu_res_setup.h" +#include "views.h" char *accessory_type_s( int accessory )