From 11d90fdc1a73d6d97c0176ef81dd69adade7fdd9 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 21:07:35 +0100 Subject: [PATCH 1/9] Improve code style Function names. file names. things included in header. --- src/flashcart/ed64/ed64.c | 22 +++++++------- src/flashcart/ed64/ed64_ll.c | 56 ++++++++++++++++++++---------------- src/flashcart/ed64/ed64_ll.h | 36 +++++++---------------- 3 files changed, 53 insertions(+), 61 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index e9e75f36..12988d26 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -17,13 +17,13 @@ // This is a trial hack before using the settings API. #ifndef LAST_SAVE_FILE_PATH -#define LAST_SAVE_FILE_PATH "/menu/last_rom.txt" +#define LAST_SAVE_FILE_PATH "/menu/last_rom.tmp" #endif #ifndef RESET_CHECK_FILE_PATH -#define RESET_CHECK_FILE_PATH "/menu/RESET" +#define RESET_CHECK_FILE_PATH "/menu/reset.tmp" #endif #ifndef FLASHRAM_CHECK_FILE_PATH -#define FLASHRAM_CHECK_FILE_PATH "/menu/FLASHRAM" +#define FLASHRAM_CHECK_FILE_PATH "/menu/flashram.tmp" #endif extern int ed_exit(void); @@ -35,7 +35,7 @@ static flashcart_err_t ed64_init(void) // FIXME: Update firmware if needed. // FIXME: Enable RTC if available. - // older everdrives cant save during gameplay so we need to the reset method. + // older everdrives cannot save during gameplay so we need to the reset method. // works by checking if a file exists. if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) @@ -68,7 +68,7 @@ static flashcart_err_t ed64_init(void) return FLASHCART_ERR_LOAD; } - // Now save the content back to the SD! + // Now save the content back to the SD card! FIL fil; UINT br; uint8_t cartsave_data[KiB(128)]; @@ -88,17 +88,17 @@ static flashcart_err_t ed64_init(void) // so minus flashram we can just check the size if (file_exists(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH))) { // flashram is bugged atm - getFlashRAM(cartsave_data, save_size); + ed64_ll_get_fram(cartsave_data, save_size); // deletes flag f_unlink(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH)); } else if (save_size > KiB(2)) { // sram - getSRAM(cartsave_data, save_size); + ed64_ll_get_sram(cartsave_data, save_size); } else { // eeprom - getEeprom(cartsave_data, save_size); + ed64_ll_get_eeprom(cartsave_data, save_size); } if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) @@ -272,14 +272,14 @@ static flashcart_err_t ed64_load_save(char *save_path) { case SAVE_TYPE_EEPROM_4K: case SAVE_TYPE_EEPROM_16K: - setEeprom(cartsave_data, save_size); + ed64_ll_set_eeprom(cartsave_data, save_size); break; case SAVE_TYPE_SRAM: case SAVE_TYPE_SRAM_128K: - setSRAM(cartsave_data, save_size); + ed64_ll_set_sram(cartsave_data, save_size); break; case SAVE_TYPE_FLASHRAM: - setFlashRAM(cartsave_data, save_size); + ed64_ll_set_fram(cartsave_data, save_size); // a cold and warm boot has no way of seeing save types and most types can be determined by size // this tells the cart to use flash instead of sram 128 since they are the same size FIL flashfil; diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index d5858ced..a08bc0d9 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -24,6 +24,14 @@ typedef enum { } ed64_registers_t; +void pi_initialize(void); +void pi_initialize_sram(void); +void pi_dma_from_cart(void* dest, void* src, unsigned long size); +void pi_dma_to_cart(void* dest, void* src, unsigned long size); +void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size); +void pi_dma_to_sram(void* src, unsigned long offset, unsigned long size); +void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size); + #define SAV_EEP_ON 1 #define SAV_SRM_ON 2 @@ -116,13 +124,13 @@ void ed64_ll_set_sram_bank(uint8_t bank) { } -void PI_Init(void) { +void pi_initialize(void) { dma_wait(); io_write(PI_STATUS_REG, 0x03); } // Inits PI for sram transfer -void PI_Init_SRAM(void) { +void pi_initialize_sram(void) { io_write(PI_BSD_DOM2_LAT_REG, 0x05); io_write(PI_BSD_DOM2_PWD_REG, 0x0C); @@ -131,7 +139,7 @@ void PI_Init_SRAM(void) { } -void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { +void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size) { io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); @@ -143,7 +151,7 @@ void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { } -void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* +void pi_dma_to_sram(void *src, unsigned long offset, unsigned long size) { //void* dma_wait(); io_write(PI_STATUS_REG, 2); @@ -152,7 +160,7 @@ void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* io_write(PI_RD_LEN_REG, (size - 1)); } -void PI_DMAFromCart(void* dest, void* src, unsigned long size) { +void pi_dma_from_cart(void* dest, void* src, unsigned long size) { dma_wait(); io_write(PI_STATUS_REG, 0x03); @@ -162,7 +170,7 @@ void PI_DMAFromCart(void* dest, void* src, unsigned long size) { } -void PI_DMAToCart(void* dest, void* src, unsigned long size) { +void pi_dma_to_cart(void* dest, void* src, unsigned long size) { dma_wait(); io_write(PI_STATUS_REG, 0x02); @@ -173,7 +181,7 @@ void PI_DMAToCart(void* dest, void* src, unsigned long size) { // Wrapper to support unaligned access to memory -void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { +void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size) { if (!dest || !src || !size) return; unsigned long unalignedSrc = ((unsigned long)src) % 2; @@ -181,7 +189,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { //FIXME: Do i really need to check if size is 16bit aligned? if (!unalignedDest && !unalignedSrc && !(size % 2)) { - PI_DMAFromCart(dest, src, size); + pi_dma_from_cart(dest, src, size); dma_wait(); return; @@ -191,7 +199,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { unsigned long newSize = (size + unalignedSrc) + ((size + unalignedSrc) % 2); unsigned char *buffer = memalign(8, newSize); - PI_DMAFromCart(buffer, newSrc, newSize); + pi_dma_from_cart(buffer, newSrc, newSize); dma_wait(); memcpy(dest, (buffer + unalignedSrc), size); @@ -200,7 +208,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { } -int getSRAM( uint8_t *buffer, int size){ +int ed64_ll_get_sram( uint8_t *buffer, int size){ dma_wait(); io_write(PI_BSD_DOM2_LAT_REG, 0x05); @@ -210,11 +218,11 @@ int getSRAM( uint8_t *buffer, int size){ dma_wait(); - PI_Init(); + pi_initialize(); dma_wait(); - PI_DMAFromSRAM(buffer, 0, size) ; + pi_dma_from_sram(buffer, 0, size) ; dma_wait(); @@ -226,7 +234,7 @@ int getSRAM( uint8_t *buffer, int size){ return 1; } -int getEeprom( uint8_t *buffer, int size){ +int ed64_ll_get_eeprom( uint8_t *buffer, int size){ int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_read( b, &buffer[b * 8] ); @@ -236,11 +244,11 @@ int getEeprom( uint8_t *buffer, int size){ } -int getFlashRAM( uint8_t *buffer, int size){ +int ed64_ll_get_fram( uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 dma_wait(); - getSRAM(buffer, size); + ed64_ll_get_sram(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); @@ -252,31 +260,31 @@ int getFlashRAM( uint8_t *buffer, int size){ /* sram upload */ -int setSRAM( uint8_t *buffer, int size){ +int ed64_ll_set_sram( uint8_t *buffer, int size){ //half working dma_wait(); //Timing - PI_Init_SRAM(); + pi_initialize_sram(); //Readmode - PI_Init(); + pi_initialize(); data_cache_hit_writeback_invalidate(buffer,size); dma_wait(); - PI_DMAToSRAM(buffer, 0, size); + pi_dma_to_sram(buffer, 0, size); data_cache_hit_writeback_invalidate(buffer,size); //Wait dma_wait(); //Restore evd Timing - setSDTiming(); + ed64_ll_set_sdcard_timing(); return 1; } -int setEeprom(uint8_t *buffer, int size){ +int ed64_ll_set_eeprom(uint8_t *buffer, int size){ int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_write( b, &buffer[b * 8] ); @@ -285,11 +293,11 @@ int setEeprom(uint8_t *buffer, int size){ return 1; } -int setFlashRAM(uint8_t *buffer, int size){ +int ed64_ll_set_fram(uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); dma_wait(); - setSRAM(buffer, size); + ed64_ll_set_sram(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); @@ -299,7 +307,7 @@ int setFlashRAM(uint8_t *buffer, int size){ } -void setSDTiming(void){ +void ed64_ll_set_sdcard_timing(void){ io_write(PI_BSD_DOM1_LAT_REG, 0x40); io_write(PI_BSD_DOM1_PWD_REG, 0x12); diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 9230d7b7..fc009105 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -69,31 +69,15 @@ void ed64_ll_set_ram_bank(uint8_t bank); ed64_save_type_t ed64_ll_get_save_type(); void ed64_ll_set_save_type(ed64_save_type_t type); +void ed64_ll_set_sdcard_timing(void); + +int ed64_ll_get_sram( uint8_t *buffer, int size); +int ed64_ll_get_eeprom( uint8_t *buffer, int size); +int ed64_ll_get_fram( uint8_t *buffer, int size); + +int ed64_ll_set_sram( uint8_t *buffer, int size); +int ed64_ll_set_eeprom( uint8_t *buffer, int size); +int ed64_ll_set_fram( uint8_t *buffer, int size); + /** @} */ /* ed64 */ - -void data_cache_hit_writeback_invalidate(volatile void *, unsigned long); -unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len); -void dma_write_sram(void* src, unsigned long offset, unsigned long size); -void dma_read_sram(void *dest, unsigned long offset, unsigned long size); -void dma_write_s(void * ram_address, unsigned long pi_address, unsigned long len); -void dma_read_s(void * ram_address, unsigned long pi_address, unsigned long len); -int writeSram(void* src, unsigned long size); -void setSDTiming(void); - - -void PI_Init(void); -void PI_Init_SRAM(void); -void PI_DMAFromCart(void* dest, void* src, unsigned long size); -void PI_DMAToCart(void* dest, void* src, unsigned long size); -void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size); -void PI_DMAToSRAM(void* src, unsigned long offset, unsigned long size); -void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size); -int getSRAM( uint8_t *buffer, int size); -int getEeprom( uint8_t *buffer, int size); -int getFlashRAM( uint8_t *buffer, int size); -int setSRAM( uint8_t *buffer, int size); -int setEeprom( uint8_t *buffer, int size); -int setFlashRAM( uint8_t *buffer, int size); - - #endif From f75a26ad126be5a1d28658c541cfe154758df7cd Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 21:23:13 +0100 Subject: [PATCH 2/9] Move sd timings --- src/flashcart/ed64/ed64_ll.c | 4 ++++ src/flashcart/ed64/ed64_ll.h | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index a08bc0d9..137c6995 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -32,6 +32,10 @@ void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size); void pi_dma_to_sram(void* src, unsigned long offset, unsigned long size); void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size); +void ed64_ll_set_sdcard_timing(void); + +void ed64_ll_set_sdcard_timing(void); + #define SAV_EEP_ON 1 #define SAV_SRM_ON 2 diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index fc009105..5fd38ad4 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -69,8 +69,6 @@ void ed64_ll_set_ram_bank(uint8_t bank); ed64_save_type_t ed64_ll_get_save_type(); void ed64_ll_set_save_type(ed64_save_type_t type); -void ed64_ll_set_sdcard_timing(void); - int ed64_ll_get_sram( uint8_t *buffer, int size); int ed64_ll_get_eeprom( uint8_t *buffer, int size); int ed64_ll_get_fram( uint8_t *buffer, int size); From d0875e4a36607065451453f6240b573ab988b9f0 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 21:29:47 +0100 Subject: [PATCH 3/9] Remove un-necessary header --- src/menu/cart_load.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/menu/cart_load.c b/src/menu/cart_load.c index eb7979f5..2986d9a2 100644 --- a/src/menu/cart_load.c +++ b/src/menu/cart_load.c @@ -1,6 +1,5 @@ #include -#include #include #include "cart_load.h" From 0f17c610e04779fc0cab450da4278f4f14f7e935 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 22:08:53 +0100 Subject: [PATCH 4/9] Code style Also fixes ed64_ll_set_ram_bank --- src/flashcart/ed64/ed64.c | 138 ++++++++++++----------------------- src/flashcart/ed64/ed64_ll.c | 94 +++++++++++++++--------- src/flashcart/ed64/ed64_ll.h | 18 ++--- 3 files changed, 115 insertions(+), 135 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 12988d26..11a9c309 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -26,10 +26,10 @@ #define FLASHRAM_CHECK_FILE_PATH "/menu/flashram.tmp" #endif -extern int ed_exit(void); +extern int ed_exit (void); + +static flashcart_err_t ed64_init (void) { -static flashcart_err_t ed64_init(void) -{ // TODO: partly already done, see https://github.com/DragonMinded/libdragon/blob/4ec469d26b6dc4e308caf3d5b86c2b340b708bbd/src/libcart/cart.c#L1064 // FIXME: Update firmware if needed. @@ -38,8 +38,7 @@ static flashcart_err_t ed64_init(void) // older everdrives cannot save during gameplay so we need to the reset method. // works by checking if a file exists. - if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) - { + if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) { // make sure next boot doesnt trigger the check by deleting the reset file f_unlink(strip_sd_prefix(RESET_CHECK_FILE_PATH)); @@ -48,8 +47,7 @@ static flashcart_err_t ed64_init(void) FIL lrp_fil; UINT lrp_br; - if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK) - { + if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -57,14 +55,12 @@ static flashcart_err_t ed64_init(void) TCHAR lrp_path[lrp_size++]; - if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) - { + if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) { f_close(&lrp_fil); return FLASHCART_ERR_LOAD; } - if (f_close(&lrp_fil) != FR_OK) - { + if (f_close(&lrp_fil) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -74,40 +70,33 @@ static flashcart_err_t ed64_init(void) uint8_t cartsave_data[KiB(128)]; // find the path to last save - if (file_exists(strip_sd_prefix(lrp_path))) - { + if (file_exists(strip_sd_prefix(lrp_path))) { int save_size = file_get_size(strip_sd_prefix(lrp_path)); - if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_READ | FA_WRITE)) != FR_OK) - { + if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_READ | FA_WRITE)) != FR_OK) { return FLASHCART_ERR_LOAD; } // everdrive doesn't care about the save type other than flash sram and eeprom // so minus flashram we can just check the size - if (file_exists(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH))) - { // flashram is bugged atm + if (file_exists(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH))) { // flashram is bugged atm ed64_ll_get_fram(cartsave_data, save_size); // deletes flag f_unlink(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH)); } - else if (save_size > KiB(2)) - { // sram + else if (save_size > KiB(2)) { // sram ed64_ll_get_sram(cartsave_data, save_size); } - else - { // eeprom + else { // eeprom ed64_ll_get_eeprom(cartsave_data, save_size); } - if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) - { + if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) { return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } } @@ -115,8 +104,7 @@ static flashcart_err_t ed64_init(void) return FLASHCART_OK; } -static flashcart_err_t ed64_deinit(void) -{ +static flashcart_err_t ed64_deinit (void) { // For the moment, just use libCart exit. ed_exit(); @@ -124,10 +112,8 @@ static flashcart_err_t ed64_deinit(void) return FLASHCART_OK; } -static bool ed64_has_feature(flashcart_features_t feature) -{ - switch (feature) - { +static bool ed64_has_feature (flashcart_features_t feature) { + switch (feature) { case FLASHCART_FEATURE_64DD: return false; default: @@ -135,13 +121,12 @@ static bool ed64_has_feature(flashcart_features_t feature) } } -static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback_t *progress) -{ +static flashcart_err_t ed64_load_rom (char *rom_path, flashcart_progress_callback_t *progress) { + FIL fil; UINT br; - if (f_open(&fil, strip_sd_prefix(rom_path), FA_READ) != FR_OK) - { + if (f_open(&fil, strip_sd_prefix(rom_path), FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -152,14 +137,12 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback // FIXME: if the cart is not V3 or X5 or X7, we need probably need to - 128KiB for save compatibility. // Or somehow warn that certain ROM's will have corruption due to the address space being used for saves. // Conker's Bad Fur Day doesn't have this issue because eeprom data is at a fixed address in pif ram. - if (rom_size > MiB(64)) - { + if (rom_size > MiB(64)) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (rom_size == MiB(64)) - { + if (rom_size == MiB(64)) { rom_size -= KiB(128); } @@ -169,37 +152,32 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback for (int offset = 0; offset < sdram_size; offset += chunk_size) { size_t block_size = MIN(sdram_size - offset, chunk_size); - if (f_read(&fil, (void *)(ROM_ADDRESS + offset), block_size, &br) != FR_OK) - { + if (f_read(&fil, (void *)(ROM_ADDRESS + offset), block_size, &br) != FR_OK) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (progress) - { + if (progress) { progress(f_tell(&fil) / (float)(f_size(&fil))); } } - /*if (f_tell(&fil) != sdram_size) - { + /*if (f_tell(&fil) != sdram_size) { f_close(&fil); return FLASHCART_ERR_LOAD; }*/ - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } return FLASHCART_OK; } -static flashcart_err_t ed64_load_file(char *file_path, uint32_t rom_offset, uint32_t file_offset) +static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uint32_t file_offset) { FIL fil; UINT br; - if (f_open(&fil, strip_sd_prefix(file_path), FA_READ) != FR_OK) - { + if (f_open(&fil, strip_sd_prefix(file_path), FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -210,45 +188,38 @@ static flashcart_err_t ed64_load_file(char *file_path, uint32_t rom_offset, uint // FIXME: if the cart is not V3 or X5 or X7, we need probably need to - 128KiB for save compatibility. // Or somehow warn that certain ROM's will have corruption due to the address space being used for saves. - if (file_size > (MiB(64) - rom_offset)) - { + if (file_size > (MiB(64) - rom_offset)) { f_close(&fil); return FLASHCART_ERR_ARGS; } - if (f_lseek(&fil, file_offset) != FR_OK) - { + if (f_lseek(&fil, file_offset) != FR_OK) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (f_read(&fil, (void *)(ROM_ADDRESS + rom_offset), file_size, &br) != FR_OK) - { + if (f_read(&fil, (void *)(ROM_ADDRESS + rom_offset), file_size, &br) != FR_OK) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (br != file_size) - { + if (br != file_size) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } return FLASHCART_OK; } -static flashcart_err_t ed64_load_save(char *save_path) -{ +static flashcart_err_t ed64_load_save (char *save_path) { FIL fil; UINT br; - if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) - { + if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) { f_close(&fil); return FLASHCART_ERR_LOAD; } @@ -256,20 +227,17 @@ static flashcart_err_t ed64_load_save(char *save_path) size_t save_size = file_get_size(strip_sd_prefix(save_path)); uint8_t cartsave_data[save_size]; - if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) - { + if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } ed64_save_type_t type = ed64_ll_get_save_type(); - switch (type) - { + switch (type) { case SAVE_TYPE_EEPROM_4K: case SAVE_TYPE_EEPROM_16K: ed64_ll_set_eeprom(cartsave_data, save_size); @@ -283,14 +251,12 @@ static flashcart_err_t ed64_load_save(char *save_path) // a cold and warm boot has no way of seeing save types and most types can be determined by size // this tells the cart to use flash instead of sram 128 since they are the same size FIL flashfil; - if (f_open(&flashfil, strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) - { + if (f_open(&flashfil, strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) { f_close(&flashfil); return FLASHCART_ERR_LOAD; } - if (f_close(&flashfil) != FR_OK) - { + if (f_close(&flashfil) != FR_OK) { return FLASHCART_OK; } break; @@ -302,18 +268,15 @@ static flashcart_err_t ed64_load_save(char *save_path) FIL lsp_fil; UINT lsp_bw; - if (f_open(&lsp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) - { + if (f_open(&lsp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { return FLASHCART_ERR_LOAD; } - if (f_write(&lsp_fil, strip_sd_prefix(save_path), strlen(save_path), &lsp_bw) != FR_OK) - { + if (f_write(&lsp_fil, strip_sd_prefix(save_path), strlen(save_path), &lsp_bw) != FR_OK) { f_close(&lsp_fil); return FLASHCART_ERR_LOAD; } - if (f_close(&lsp_fil) != FR_OK) - { + if (f_close(&lsp_fil) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -321,26 +284,22 @@ static flashcart_err_t ed64_load_save(char *save_path) // simulate a unix touch command to create a file as it only needs to exist to detect a reset - if (f_open(&rsfil, strip_sd_prefix(RESET_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) - { + if (f_open(&rsfil, strip_sd_prefix(RESET_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) { f_close(&rsfil); return FLASHCART_ERR_LOAD; } - if (f_close(&rsfil) != FR_OK) - { + if (f_close(&rsfil) != FR_OK) { return FLASHCART_OK; } return FLASHCART_OK; } -static flashcart_err_t ed64_set_save_type(flashcart_save_type_t save_type) -{ +static flashcart_err_t ed64_set_save_type (flashcart_save_type_t save_type) { ed64_save_type_t type; - switch (save_type) - { + switch (save_type) { case FLASHCART_SAVE_TYPE_NONE: type = SAVE_TYPE_NONE; break; @@ -383,7 +342,6 @@ static flashcart_t flashcart_ed64 = { .set_save_writeback = NULL, }; -flashcart_t *ed64_get_flashcart(void) -{ +flashcart_t *ed64_get_flashcart (void) { return &flashcart_ed64; } diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 137c6995..5b04eed5 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -24,17 +24,15 @@ typedef enum { } ed64_registers_t; -void pi_initialize(void); -void pi_initialize_sram(void); -void pi_dma_from_cart(void* dest, void* src, unsigned long size); -void pi_dma_to_cart(void* dest, void* src, unsigned long size); -void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size); -void pi_dma_to_sram(void* src, unsigned long offset, unsigned long size); -void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size); +void pi_initialize (void); +void pi_initialize_sram (void); +void pi_dma_from_cart (void* dest, void* src, unsigned long size); +void pi_dma_to_cart (void* dest, void* src, unsigned long size); +void pi_dma_from_sram (void *dest, unsigned long offset, unsigned long size); +void pi_dma_to_sram (void* src, unsigned long offset, unsigned long size); +void pi_dma_from_cart_safe (void *dest, void *src, unsigned long size); -void ed64_ll_set_sdcard_timing(void); - -void ed64_ll_set_sdcard_timing(void); +void ed64_ll_set_sdcard_timing (void); #define SAV_EEP_ON 1 @@ -45,20 +43,21 @@ void ed64_ll_set_sdcard_timing(void); #define SAV_RAM_BANK 128 #define SAV_RAM_BANK_APPLY 32768 -uint32_t ed64_ll_reg_read(uint32_t reg); -void ed64_ll_reg_write(uint32_t reg, uint32_t data); +uint32_t ed64_ll_reg_read (uint32_t reg); +void ed64_ll_reg_write (uint32_t reg, uint32_t data); uint8_t ed64_ll_sram_bank; ed64_save_type_t ed64_ll_save_type; -uint32_t ed64_ll_reg_read(uint32_t reg) { +uint32_t ed64_ll_reg_read (uint32_t reg) { *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE); return *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE + reg * 4); + } -void ed64_ll_reg_write(uint32_t reg, uint32_t data) { +void ed64_ll_reg_write (uint32_t reg, uint32_t data) { *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE); *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE + reg * 4) = data; @@ -67,12 +66,13 @@ void ed64_ll_reg_write(uint32_t reg, uint32_t data) { } -ed64_save_type_t ed64_ll_get_save_type() { +ed64_save_type_t ed64_ll_get_save_type (void) { return ed64_ll_save_type; + } -void ed64_ll_set_save_type(ed64_save_type_t type) { +void ed64_ll_set_save_type (ed64_save_type_t type) { uint16_t save_cfg; uint8_t eeprom_on, sram_on, eeprom_size, sram_size, ram_bank; @@ -121,71 +121,79 @@ void ed64_ll_set_save_type(ed64_save_type_t type) { } -void ed64_ll_set_sram_bank(uint8_t bank) { +void ed64_ll_set_sram_bank (uint8_t bank) { ed64_ll_sram_bank = bank == 0 ? 0 : 1; } -void pi_initialize(void) { +void pi_initialize (void) { + dma_wait(); io_write(PI_STATUS_REG, 0x03); + } // Inits PI for sram transfer -void pi_initialize_sram(void) { - +void pi_initialize_sram (void) { + 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); - + } -void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size) { - +void pi_dma_from_sram (void *dest, unsigned long offset, unsigned long size) { io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); asm volatile ("" : : : "memory"); io_write(PI_WR_LEN_REG, (size - 1)); asm volatile ("" : : : "memory"); - + } -void pi_dma_to_sram(void *src, unsigned long offset, unsigned long size) { //void* +void pi_dma_to_sram (void *src, unsigned long offset, unsigned long size) { + dma_wait(); io_write(PI_STATUS_REG, 2); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); io_write(PI_RD_LEN_REG, (size - 1)); + } -void pi_dma_from_cart(void* dest, void* src, unsigned long size) { +void pi_dma_from_cart (void* dest, void* src, unsigned long size) { + dma_wait(); io_write(PI_STATUS_REG, 0x03); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); io_write(PI_CART_ADDR_REG, K0_TO_PHYS(src)); io_write(PI_WR_LEN_REG, (size - 1)); + } -void pi_dma_to_cart(void* dest, void* src, unsigned long size) { +void pi_dma_to_cart (void* dest, void* src, unsigned long size) { + dma_wait(); io_write(PI_STATUS_REG, 0x02); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); io_write(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); io_write(PI_RD_LEN_REG, (size - 1)); + } // Wrapper to support unaligned access to memory -void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size) { +void pi_dma_from_cart_safe (void *dest, void *src, unsigned long size) { + if (!dest || !src || !size) return; unsigned long unalignedSrc = ((unsigned long)src) % 2; @@ -209,10 +217,12 @@ void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size) { memcpy(dest, (buffer + unalignedSrc), size); free(buffer); + } -int ed64_ll_get_sram( uint8_t *buffer, int size){ +int ed64_ll_get_sram (uint8_t *buffer, int size) { + dma_wait(); io_write(PI_BSD_DOM2_LAT_REG, 0x05); @@ -236,19 +246,23 @@ int ed64_ll_get_sram( uint8_t *buffer, int size){ io_write(PI_BSD_DOM2_RLS_REG, 0x03); return 1; + } -int ed64_ll_get_eeprom( uint8_t *buffer, int size){ +int ed64_ll_get_eeprom (uint8_t *buffer, int size) { + int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_read( b, &buffer[b * 8] ); } return 1; + } -int ed64_ll_get_fram( uint8_t *buffer, int size){ +int ed64_ll_get_fram (uint8_t *buffer, int size) { + ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 dma_wait(); @@ -259,13 +273,15 @@ int ed64_ll_get_fram( uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); return 1; + } /* sram upload */ -int ed64_ll_set_sram( uint8_t *buffer, int size){ - //half working +int ed64_ll_set_sram (uint8_t *buffer, int size) { + + //half working dma_wait(); //Timing pi_initialize_sram(); @@ -285,19 +301,23 @@ int ed64_ll_set_sram( uint8_t *buffer, int size){ ed64_ll_set_sdcard_timing(); return 1; + } -int ed64_ll_set_eeprom(uint8_t *buffer, int size){ +int ed64_ll_set_eeprom (uint8_t *buffer, int size) { + int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_write( b, &buffer[b * 8] ); } return 1; + } -int ed64_ll_set_fram(uint8_t *buffer, int size){ +int ed64_ll_set_fram (uint8_t *buffer, int size) { + ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); dma_wait(); @@ -308,10 +328,11 @@ int ed64_ll_set_fram(uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); return 1; + } -void ed64_ll_set_sdcard_timing(void){ +void ed64_ll_set_sdcard_timing (void) { io_write(PI_BSD_DOM1_LAT_REG, 0x40); io_write(PI_BSD_DOM1_PWD_REG, 0x12); @@ -322,4 +343,5 @@ void ed64_ll_set_sdcard_timing(void){ io_write(PI_BSD_DOM2_PWD_REG, 0x12); io_write(PI_BSD_DOM2_PGS_REG, 0x07); io_write(PI_BSD_DOM2_RLS_REG, 0x03); + } diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 5fd38ad4..2d1c84e8 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -65,17 +65,17 @@ typedef enum { #define ROM_ADDRESS (0xB0000000) /* Save functions */ -void ed64_ll_set_ram_bank(uint8_t bank); -ed64_save_type_t ed64_ll_get_save_type(); -void ed64_ll_set_save_type(ed64_save_type_t type); +void ed64_ll_set_sram_bank (uint8_t bank); +ed64_save_type_t ed64_ll_get_save_type (); +void ed64_ll_set_save_type (ed64_save_type_t type); -int ed64_ll_get_sram( uint8_t *buffer, int size); -int ed64_ll_get_eeprom( uint8_t *buffer, int size); -int ed64_ll_get_fram( uint8_t *buffer, int size); +int ed64_ll_get_sram (uint8_t *buffer, int size); +int ed64_ll_get_eeprom (uint8_t *buffer, int size); +int ed64_ll_get_fram (uint8_t *buffer, int size); -int ed64_ll_set_sram( uint8_t *buffer, int size); -int ed64_ll_set_eeprom( uint8_t *buffer, int size); -int ed64_ll_set_fram( uint8_t *buffer, int size); +int ed64_ll_set_sram (uint8_t *buffer, int size); +int ed64_ll_set_eeprom (uint8_t *buffer, int size); +int ed64_ll_set_fram (uint8_t *buffer, int size); /** @} */ /* ed64 */ #endif From 7b4a675193dfb4d3b375319709cc68e0f0b9d889 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 23:52:27 +0100 Subject: [PATCH 5/9] Add state ini (not used yet) prefix temp files with ed_ for the moment. --- src/flashcart/ed64/ed64.c | 10 ++++---- src/flashcart/ed64/ed64_state.c | 42 +++++++++++++++++++++++++++++++++ src/flashcart/ed64/ed64_state.h | 26 ++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/flashcart/ed64/ed64_state.c create mode 100644 src/flashcart/ed64/ed64_state.h diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 11a9c309..dc8f801e 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -12,18 +12,18 @@ #include "../flashcart_utils.h" #include "ed64_ll.h" #include "ed64.h" +#include "ed64_state.h" -// #include "../menu/settings.h" -// This is a trial hack before using the settings API. +// FIXME: Use one file using the ed64_pseudo_writeback_t struct. #ifndef LAST_SAVE_FILE_PATH -#define LAST_SAVE_FILE_PATH "/menu/last_rom.tmp" +#define LAST_SAVE_FILE_PATH "/menu/ed_last_rom.tmp" #endif #ifndef RESET_CHECK_FILE_PATH -#define RESET_CHECK_FILE_PATH "/menu/reset.tmp" +#define RESET_CHECK_FILE_PATH "/menu/ed_reset.tmp" #endif #ifndef FLASHRAM_CHECK_FILE_PATH -#define FLASHRAM_CHECK_FILE_PATH "/menu/flashram.tmp" +#define FLASHRAM_CHECK_FILE_PATH "/menu/ed_flashram.tmp" #endif extern int ed_exit (void); diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c new file mode 100644 index 00000000..57a5f5ab --- /dev/null +++ b/src/flashcart/ed64/ed64_state.c @@ -0,0 +1,42 @@ +#include +#include + +#include "ed64_state.h" +#include "utils/fs.h" + +#ifndef ED64_STATE_FILE_PATH +#define ED64_STATE_FILE_PATH "sd:/menu/ed64_state.ini" +#endif + +static ed64_pseudo_writeback_t init = { + .is_warm_start = false, + .last_rom_save_type = false, + .last_rom_path = "" +}; + + +void ed64_state_load (ed64_pseudo_writeback_t *state) { + if (!file_exists(ED64_STATE_FILE_PATH)) { + ed64_state_save(&init); + } + + mini_t *ini = mini_try_load(ED64_STATE_FILE_PATH); + + state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); + state->last_rom_save_type = mini_get_bool(ini, "ed64", "last_rom_save_type", init.last_rom_save_type); + state->last_rom_path = strdup(mini_get_string(ini, "ed64", "last_rom_path", init.last_rom_path)); + + mini_free(ini); +} + +void ed64_state_save (ed64_pseudo_writeback_t *state) { + mini_t *ini = mini_create(ED64_STATE_FILE_PATH); + + mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); + mini_set_bool(ini, "ed64", "last_rom_save_type", state->last_rom_save_type); + mini_set_string(ini, "ed64", "last_rom_path", state->last_rom_path); + + mini_save(ini); + + mini_free(ini); +} diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h new file mode 100644 index 00000000..7cd6d768 --- /dev/null +++ b/src/flashcart/ed64/ed64_state.h @@ -0,0 +1,26 @@ +/** + * @file settings.h + * @brief Menu Settings + * @ingroup menu + */ + +#ifndef FLASHCART_ED64_STATE_H__ +#define LASHCART_ED64_STATE_H__ + +#include + +/** @brief ed64 pseudo Writeback Structure */ +typedef struct { +/** @brief The reset button was used */ + bool is_warm_start; +/** @brief The last save type used */ + bool last_rom_save_type; // FIXME: for the moment only a bool for flashram +/** @brief The path to the last loaded ROM */ + char *last_rom_path; +} ed64_pseudo_writeback_t; + +void ed64_state_load (ed64_pseudo_writeback_t *state); +void ed64_state_save (ed64_pseudo_writeback_t *state); + + +#endif \ No newline at end of file From ff218daa09cafa92593fe1cc59b23476a3340ee2 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 00:40:02 +0100 Subject: [PATCH 6/9] Move to struct for reset and fram state. --- Makefile | 1 + src/flashcart/ed64/ed64.c | 47 +++++++++++---------------------- src/flashcart/ed64/ed64_state.c | 6 ++--- src/flashcart/ed64/ed64_state.h | 8 +++--- 4 files changed, 23 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index b8a84b61..daad90af 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ SRCS = \ flashcart/sc64/sc64_ll.c \ flashcart/sc64/sc64.c \ flashcart/ed64/ed64_ll.c \ + flashcart/ed64/ed64_state.c \ flashcart/ed64/ed64.c \ libs/libspng/spng/spng.c \ libs/mini.c/src/mini.c \ diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index dc8f801e..e9022a31 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -15,16 +15,12 @@ #include "ed64_state.h" +static ed64_pseudo_writeback_t current_state; + // FIXME: Use one file using the ed64_pseudo_writeback_t struct. #ifndef LAST_SAVE_FILE_PATH #define LAST_SAVE_FILE_PATH "/menu/ed_last_rom.tmp" #endif -#ifndef RESET_CHECK_FILE_PATH -#define RESET_CHECK_FILE_PATH "/menu/ed_reset.tmp" -#endif -#ifndef FLASHRAM_CHECK_FILE_PATH -#define FLASHRAM_CHECK_FILE_PATH "/menu/ed_flashram.tmp" -#endif extern int ed_exit (void); @@ -38,10 +34,13 @@ static flashcart_err_t ed64_init (void) { // older everdrives cannot save during gameplay so we need to the reset method. // works by checking if a file exists. - if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) { + ed64_state_load(¤t_state); - // make sure next boot doesnt trigger the check by deleting the reset file - f_unlink(strip_sd_prefix(RESET_CHECK_FILE_PATH)); + if (current_state.is_warm_start == true) { + + // make sure next boot doesnt trigger the check changing its state. + current_state.is_warm_start = false; + ed64_state_save(¤t_state); // finds the last save location FIL lrp_fil; @@ -80,10 +79,11 @@ static flashcart_err_t ed64_init (void) { // everdrive doesn't care about the save type other than flash sram and eeprom // so minus flashram we can just check the size - if (file_exists(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH))) { // flashram is bugged atm + if (current_state.is_fram_save_type == true) { // flashram is bugged atm ed64_ll_get_fram(cartsave_data, save_size); // deletes flag - f_unlink(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH)); + current_state.is_fram_save_type = false; + ed64_state_save(¤t_state); } else if (save_size > KiB(2)) { // sram ed64_ll_get_sram(cartsave_data, save_size); @@ -250,15 +250,8 @@ static flashcart_err_t ed64_load_save (char *save_path) { ed64_ll_set_fram(cartsave_data, save_size); // a cold and warm boot has no way of seeing save types and most types can be determined by size // this tells the cart to use flash instead of sram 128 since they are the same size - FIL flashfil; - if (f_open(&flashfil, strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) { - f_close(&flashfil); - return FLASHCART_ERR_LOAD; - } - - if (f_close(&flashfil) != FR_OK) { - return FLASHCART_OK; - } + current_state.is_fram_save_type = true; + ed64_state_save(¤t_state); break; default: break; @@ -280,18 +273,8 @@ static flashcart_err_t ed64_load_save (char *save_path) { return FLASHCART_ERR_LOAD; } - FIL rsfil; - - // simulate a unix touch command to create a file as it only needs to exist to detect a reset - - if (f_open(&rsfil, strip_sd_prefix(RESET_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) { - f_close(&rsfil); - return FLASHCART_ERR_LOAD; - } - - if (f_close(&rsfil) != FR_OK) { - return FLASHCART_OK; - } + current_state.is_warm_start = true; + ed64_state_save(¤t_state); return FLASHCART_OK; } diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c index 57a5f5ab..392f677f 100644 --- a/src/flashcart/ed64/ed64_state.c +++ b/src/flashcart/ed64/ed64_state.c @@ -10,7 +10,7 @@ static ed64_pseudo_writeback_t init = { .is_warm_start = false, - .last_rom_save_type = false, + .is_fram_save_type = false, .last_rom_path = "" }; @@ -23,7 +23,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_try_load(ED64_STATE_FILE_PATH); state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); - state->last_rom_save_type = mini_get_bool(ini, "ed64", "last_rom_save_type", init.last_rom_save_type); + state->is_fram_save_type = mini_get_bool(ini, "ed64", "is_fram_save_type", init.is_fram_save_type); state->last_rom_path = strdup(mini_get_string(ini, "ed64", "last_rom_path", init.last_rom_path)); mini_free(ini); @@ -33,7 +33,7 @@ void ed64_state_save (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_create(ED64_STATE_FILE_PATH); mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); - mini_set_bool(ini, "ed64", "last_rom_save_type", state->last_rom_save_type); + mini_set_bool(ini, "ed64", "is_fram_save_type", state->is_fram_save_type); mini_set_string(ini, "ed64", "last_rom_path", state->last_rom_path); mini_save(ini); diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index 7cd6d768..5adbd149 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -5,16 +5,16 @@ */ #ifndef FLASHCART_ED64_STATE_H__ -#define LASHCART_ED64_STATE_H__ +#define FLASHCART_ED64_STATE_H__ #include -/** @brief ed64 pseudo Writeback Structure */ +/** @brief ED64 Pseudo Writeback Structure */ typedef struct { /** @brief The reset button was used */ bool is_warm_start; -/** @brief The last save type used */ - bool last_rom_save_type; // FIXME: for the moment only a bool for flashram +/** @brief The last save type was flash ram */ + bool is_fram_save_type; /** @brief The path to the last loaded ROM */ char *last_rom_path; } ed64_pseudo_writeback_t; From 1daa664fe0dab99060a2f169f115d57715e50dab Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 01:11:37 +0100 Subject: [PATCH 7/9] Minor comment improvements --- src/flashcart/ed64/ed64_state.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index 5adbd149..2ea719e0 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -1,14 +1,12 @@ /** - * @file settings.h - * @brief Menu Settings - * @ingroup menu + * @file e664_state.h + * @brief ED64 state + * @ingroup flashcart */ #ifndef FLASHCART_ED64_STATE_H__ #define FLASHCART_ED64_STATE_H__ -#include - /** @brief ED64 Pseudo Writeback Structure */ typedef struct { /** @brief The reset button was used */ From d677712b51e6d8c5d95d7d115aa1c114b6931777 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 01:57:50 +0100 Subject: [PATCH 8/9] Finish state conversion for last save path (hopefully not broken). --- src/flashcart/ed64/ed64.c | 56 ++++++--------------------------- src/flashcart/ed64/ed64_state.c | 6 ++-- src/flashcart/ed64/ed64_state.h | 2 +- 3 files changed, 14 insertions(+), 50 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index e9022a31..89df5e71 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -17,11 +17,6 @@ static ed64_pseudo_writeback_t current_state; -// FIXME: Use one file using the ed64_pseudo_writeback_t struct. -#ifndef LAST_SAVE_FILE_PATH -#define LAST_SAVE_FILE_PATH "/menu/ed_last_rom.tmp" -#endif - extern int ed_exit (void); static flashcart_err_t ed64_init (void) { @@ -42,38 +37,17 @@ static flashcart_err_t ed64_init (void) { current_state.is_warm_start = false; ed64_state_save(¤t_state); - // finds the last save location - FIL lrp_fil; - UINT lrp_br; - - if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - - int lrp_size = f_size(&lrp_fil); - - TCHAR lrp_path[lrp_size++]; - - if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) { - f_close(&lrp_fil); - return FLASHCART_ERR_LOAD; - } - - if (f_close(&lrp_fil) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - // Now save the content back to the SD card! FIL fil; - UINT br; + UINT bw; uint8_t cartsave_data[KiB(128)]; // find the path to last save - if (file_exists(strip_sd_prefix(lrp_path))) { + if (file_exists(strip_sd_prefix(current_state.last_save_path))) { - int save_size = file_get_size(strip_sd_prefix(lrp_path)); + int save_size = file_get_size(strip_sd_prefix(current_state.last_save_path)); - if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_READ | FA_WRITE)) != FR_OK) { + if ((f_open(&fil, strip_sd_prefix(current_state.last_save_path), FA_CREATE_ALWAYS | FA_READ | FA_WRITE)) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -92,7 +66,7 @@ static flashcart_err_t ed64_init (void) { ed64_ll_get_eeprom(cartsave_data, save_size); } - if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) { + if (f_write(&fil, cartsave_data, save_size, &bw) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -100,6 +74,10 @@ static flashcart_err_t ed64_init (void) { return FLASHCART_ERR_LOAD; } } + else { + current_state.last_save_path = ""; + ed64_state_save(¤t_state); + } } return FLASHCART_OK; } @@ -258,21 +236,7 @@ static flashcart_err_t ed64_load_save (char *save_path) { } - FIL lsp_fil; - UINT lsp_bw; - - if (f_open(&lsp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - if (f_write(&lsp_fil, strip_sd_prefix(save_path), strlen(save_path), &lsp_bw) != FR_OK) { - f_close(&lsp_fil); - return FLASHCART_ERR_LOAD; - } - - if (f_close(&lsp_fil) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - + current_state.last_save_path = save_path; current_state.is_warm_start = true; ed64_state_save(¤t_state); diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c index 392f677f..39dcc640 100644 --- a/src/flashcart/ed64/ed64_state.c +++ b/src/flashcart/ed64/ed64_state.c @@ -11,7 +11,7 @@ static ed64_pseudo_writeback_t init = { .is_warm_start = false, .is_fram_save_type = false, - .last_rom_path = "" + .last_save_path = "" }; @@ -24,7 +24,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); state->is_fram_save_type = mini_get_bool(ini, "ed64", "is_fram_save_type", init.is_fram_save_type); - state->last_rom_path = strdup(mini_get_string(ini, "ed64", "last_rom_path", init.last_rom_path)); + state->last_save_path = strdup(mini_get_string(ini, "ed64", "last_save_path", init.last_save_path)); mini_free(ini); } @@ -34,7 +34,7 @@ void ed64_state_save (ed64_pseudo_writeback_t *state) { mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); mini_set_bool(ini, "ed64", "is_fram_save_type", state->is_fram_save_type); - mini_set_string(ini, "ed64", "last_rom_path", state->last_rom_path); + mini_set_string(ini, "ed64", "last_save_path", state->last_save_path); mini_save(ini); diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index 2ea719e0..ee4b20bd 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -14,7 +14,7 @@ typedef struct { /** @brief The last save type was flash ram */ bool is_fram_save_type; /** @brief The path to the last loaded ROM */ - char *last_rom_path; + char *last_save_path; } ed64_pseudo_writeback_t; void ed64_state_load (ed64_pseudo_writeback_t *state); From a5d9f7d069e8f3e15bbfc7a8a005a09f25b24f0a Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 02:11:13 +0100 Subject: [PATCH 9/9] Improve error conditions --- src/flashcart/ed64/ed64.c | 10 +++++++--- src/flashcart/ed64/ed64_state.c | 6 +++--- src/flashcart/ed64/ed64_state.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 89df5e71..b3de5823 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -31,10 +31,10 @@ static flashcart_err_t ed64_init (void) { ed64_state_load(¤t_state); - if (current_state.is_warm_start == true) { + if (current_state.is_expecting_save_writeback == true) { // make sure next boot doesnt trigger the check changing its state. - current_state.is_warm_start = false; + current_state.is_expecting_save_writeback = false; ed64_state_save(¤t_state); // Now save the content back to the SD card! @@ -75,6 +75,8 @@ static flashcart_err_t ed64_init (void) { } } else { + current_state.is_expecting_save_writeback = false; + current_state.is_fram_save_type = false; current_state.last_save_path = ""; ed64_state_save(¤t_state); } @@ -214,6 +216,8 @@ static flashcart_err_t ed64_load_save (char *save_path) { return FLASHCART_ERR_LOAD; } + current_state.is_fram_save_type = false; + ed64_save_type_t type = ed64_ll_get_save_type(); switch (type) { case SAVE_TYPE_EEPROM_4K: @@ -237,7 +241,7 @@ static flashcart_err_t ed64_load_save (char *save_path) { current_state.last_save_path = save_path; - current_state.is_warm_start = true; + current_state.is_expecting_save_writeback = true; ed64_state_save(¤t_state); return FLASHCART_OK; diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c index 39dcc640..ee29bab7 100644 --- a/src/flashcart/ed64/ed64_state.c +++ b/src/flashcart/ed64/ed64_state.c @@ -9,7 +9,7 @@ #endif static ed64_pseudo_writeback_t init = { - .is_warm_start = false, + .is_expecting_save_writeback = false, .is_fram_save_type = false, .last_save_path = "" }; @@ -22,7 +22,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_try_load(ED64_STATE_FILE_PATH); - state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); + state->is_expecting_save_writeback = mini_get_bool(ini, "ed64", "is_expecting_save_writeback", init.is_expecting_save_writeback); state->is_fram_save_type = mini_get_bool(ini, "ed64", "is_fram_save_type", init.is_fram_save_type); state->last_save_path = strdup(mini_get_string(ini, "ed64", "last_save_path", init.last_save_path)); @@ -32,7 +32,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { void ed64_state_save (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_create(ED64_STATE_FILE_PATH); - mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); + mini_set_bool(ini, "ed64", "is_expecting_save_writeback", state->is_expecting_save_writeback); mini_set_bool(ini, "ed64", "is_fram_save_type", state->is_fram_save_type); mini_set_string(ini, "ed64", "last_save_path", state->last_save_path); diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index ee4b20bd..3f53a148 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -10,7 +10,7 @@ /** @brief ED64 Pseudo Writeback Structure */ typedef struct { /** @brief The reset button was used */ - bool is_warm_start; + bool is_expecting_save_writeback; /** @brief The last save type was flash ram */ bool is_fram_save_type; /** @brief The path to the last loaded ROM */