diff --git a/Makefile b/Makefile index ae6e6961..58bf0055 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ SRCS = \ flashcart/64drive/64drive.c \ flashcart/flashcart_utils.c \ flashcart/flashcart.c \ + flashcart/ed64x/ed64x.c \ flashcart/sc64/sc64_ll.c \ flashcart/sc64/sc64.c \ libs/libspng/spng/spng.c \ diff --git a/src/flashcart/ed64x/ed64x.c b/src/flashcart/ed64x/ed64x.c new file mode 100644 index 00000000..8254577a --- /dev/null +++ b/src/flashcart/ed64x/ed64x.c @@ -0,0 +1,160 @@ +#include +#include +#include + +#include +#include + +#include "utils/fs.h" +#include "utils/utils.h" + +#include "../flashcart_utils.h" +#include "ed64x_ll.h" +#include "ed64x.h" + +typedef enum { + ED64_X5 = 5, + ED64_X7 = 7, + ED64_UKNOWN = 0, +} ed64x_device_variant_t; + +/* ED64 save location base address */ +#define SRAM_ADDRESS (0xA8000000) +/* ED64 ROM location base address */ +#define ROM_ADDRESS (0xB0000000) + +static flashcart_err_t ed64x_init (void) { + + return FLASHCART_OK; +} + +static flashcart_err_t ed64x_deinit (void) { + + return FLASHCART_OK; +} + +static ed64x_device_variant_t get_cart_model() { + // Currently either X5 or X7 + return true; // FIXME: check cart model. +} + +static bool ed64x_has_feature (flashcart_features_t feature) { + bool is_model_x7 = get_cart_model(); + switch (feature) { + case FLASHCART_FEATURE_RTC: return is_model_x7 ? true : false; + case FLASHCART_FEATURE_USB: return is_model_x7 ? true : false; + case FLASHCART_FEATURE_AUTO_CIC: return true; + case FLASHCART_FEATURE_64DD: return false; + default: return false; + } +} + +static flashcart_err_t ed64x_load_rom (char *rom_path, flashcart_progress_callback_t *progress) { + FIL fil; + UINT br; + + if (f_open(&fil, strip_fs_prefix(rom_path), FA_READ) != FR_OK) { + return FLASHCART_ERR_LOAD; + } + + fatfs_fix_file_size(&fil); + + size_t rom_size = f_size(&fil); + + if (rom_size > MiB(64)) { + f_close(&fil); + return FLASHCART_ERR_LOAD; + } + + size_t sdram_size = MiB(64); + + size_t chunk_size = KiB(128); + 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) { + f_close(&fil); + return FLASHCART_ERR_LOAD; + } + if (progress) { + progress(f_tell(&fil) / (float) (f_size(&fil))); + } + } + if (f_tell(&fil) != rom_size) { + f_close(&fil); + return FLASHCART_ERR_LOAD; + } + + if (f_close(&fil) != FR_OK) { + return FLASHCART_ERR_LOAD; + } + + return FLASHCART_OK; +} + +static flashcart_err_t ed64x_load_file (char *file_path, uint32_t rom_offset, uint32_t file_offset) { + FIL fil; + UINT br; + + if (f_open(&fil, strip_fs_prefix(file_path), FA_READ) != FR_OK) { + return FLASHCART_ERR_LOAD; + } + + fatfs_fix_file_size(&fil); + + size_t file_size = f_size(&fil) - file_offset; + + if (file_size > (MiB(64) - rom_offset)) { + f_close(&fil); + return FLASHCART_ERR_ARGS; + } + + 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) { + f_close(&fil); + return FLASHCART_ERR_LOAD; + } + if (br != file_size) { + f_close(&fil); + return FLASHCART_ERR_LOAD; + } + + if (f_close(&fil) != FR_OK) { + return FLASHCART_ERR_LOAD; + } + + return FLASHCART_OK; +} + +static flashcart_err_t ed64x_load_save (char *save_path) { + + + return FLASHCART_OK; +} + +static flashcart_err_t ed64x_set_save_type (flashcart_save_type_t save_type) { + + + return FLASHCART_OK; +} + +static flashcart_t flashcart_ed64x = { + .init = ed64x_init, + .deinit = ed64x_deinit, + .has_feature = ed64x_has_feature, + .load_rom = ed64x_load_rom, + .load_file = ed64x_load_file, + .load_save = ed64x_load_save, + .load_64dd_ipl = NULL, + .load_64dd_disk = NULL, + .set_save_type = ed64x_set_save_type, + .set_save_writeback = NULL, +}; + + +flashcart_t *ed64x_get_flashcart (void) { + return &flashcart_ed64x; +} diff --git a/src/flashcart/ed64x/ed64x.h b/src/flashcart/ed64x/ed64x.h new file mode 100644 index 00000000..f96bd69f --- /dev/null +++ b/src/flashcart/ed64x/ed64x.h @@ -0,0 +1,24 @@ +/** + * @file ed64x.h + * @brief ED64 Xseries flashcart support + * @ingroup flashcart + */ + +#ifndef FLASHCART_ED64X_H__ +#define FLASHCART_ED64X_H__ + + +#include "../flashcart.h" + + +/** + * @addtogroup ED64_Xseries + * @{ + */ + +flashcart_t *ed64x_get_flashcart (void); + +/** @} */ /* ed64x */ + + +#endif \ No newline at end of file diff --git a/src/flashcart/ed64x/ed64x_ll.h b/src/flashcart/ed64x/ed64x_ll.h new file mode 100644 index 00000000..89652ee5 --- /dev/null +++ b/src/flashcart/ed64x/ed64x_ll.h @@ -0,0 +1,14 @@ +/** + * @file ed64x_ll.h + * @brief ed64x flashcart low level access + * @ingroup flashcart + */ + +#ifndef FLASHCART_ED64X_LL_H__ +#define FLASHCART_ED64X_LL_H__ + + +/** @} */ /* ed64x */ + + +#endif diff --git a/src/flashcart/flashcart.c b/src/flashcart/flashcart.c index 0cb55e84..1d6fbec3 100644 --- a/src/flashcart/flashcart.c +++ b/src/flashcart/flashcart.c @@ -11,6 +11,7 @@ #include "flashcart_utils.h" #include "64drive/64drive.h" +#include "ed64x/ed64x.h" #include "sc64/sc64.h" @@ -108,10 +109,11 @@ flashcart_err_t flashcart_init (const char **storage_prefix) { flashcart = d64_get_flashcart(); break; - case CART_EDX: // Series X EverDrive-64 + case CART_EDX: // Official EverDrive 64 Series X + flashcart = ed64x_get_flashcart(); break; - case CART_ED: // Original EverDrive-64 + case CART_ED: // Series V EverDrive-64 or clone break; case CART_SC: // SummerCart64