Added SC64 API version check

This commit is contained in:
Mateusz Faderewski 2023-02-15 00:20:17 +01:00
parent 992fcf4923
commit 22656b150c
12 changed files with 589 additions and 548 deletions

View File

@ -25,21 +25,21 @@ static const size_t SAVE_SIZE[__FLASHCART_SAVE_TYPE_END] = {
static flashcart_t *flashcart;
bool flashcart_init (void) {
flashcart_error_t flashcart_init (void) {
switch (usb_getcart()) {
case CART_SC64:
flashcart = sc64_get_flashcart();
break;
default:
return false;
return FLASHCART_ERROR_UNSUPPORTED;
}
return flashcart->init();
}
void flashcart_deinit (void) {
flashcart->deinit();
flashcart_error_t flashcart_deinit (void) {
return flashcart->deinit();
}
flashcart_error_t flashcart_load_rom (char *rom_path) {

View File

@ -8,6 +8,8 @@
typedef enum {
FLASHCART_OK,
FLASHCART_ERROR_UNSUPPORTED,
FLASHCART_ERROR_OUTDATED,
FLASHCART_ERROR_ARGS,
FLASHCART_ERROR_LOAD,
FLASHCART_ERROR_INT,
@ -25,8 +27,8 @@ typedef enum {
} flashcart_save_type_t;
typedef struct {
bool (*init) (void);
void (*deinit) (void);
flashcart_error_t (*init) (void);
flashcart_error_t (*deinit) (void);
flashcart_error_t (*load_rom) (char *rom_path);
flashcart_error_t (*load_save) (char *save_path);
flashcart_error_t (*set_save_type) (flashcart_save_type_t save_type);
@ -34,8 +36,8 @@ typedef struct {
} flashcart_t;
bool flashcart_init (void);
void flashcart_deinit (void);
flashcart_error_t flashcart_init (void);
flashcart_error_t flashcart_deinit (void);
flashcart_error_t flashcart_load_rom (char *rom_path);
flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type, bool save_writeback);

View File

@ -18,6 +18,8 @@
#define SHADOW_ADDRESS (0x1FFC0000)
#define EEPROM_ADDRESS (0x1FFE2000)
#define MIN_SUPPORTED_API_VERSION (1)
static flashcart_error_t load_to_flash (FIL *fil, void *address, size_t size, UINT *br) {
size_t erase_block_size;
@ -49,13 +51,29 @@ static flashcart_error_t load_to_flash (FIL *fil, void *address, size_t size, UI
}
static bool sc64_init (void) {
static flashcart_error_t sc64_init (void) {
uint32_t api_version;
sc64_unlock();
return sc64_check_presence();
if (!sc64_check_presence()) {
return FLASHCART_ERROR_UNSUPPORTED;
}
static void sc64_deinit (void) {
if (sc64_get_api_version(&api_version) != SC64_OK) {
return FLASHCART_ERROR_OUTDATED;
}
if (api_version < MIN_SUPPORTED_API_VERSION) {
return FLASHCART_ERROR_OUTDATED;
}
return FLASHCART_OK;
}
static flashcart_error_t sc64_deinit (void) {
sc64_lock();
return FLASHCART_OK;
}
static flashcart_error_t sc64_load_rom (char *rom_path) {

View File

@ -26,6 +26,7 @@ typedef struct {
typedef enum {
CMD_ID_API_VERSION_GET = 'V',
CMD_ID_CONFIG_GET = 'c',
CMD_ID_CONFIG_SET = 'C',
CMD_ID_WRITEBACK_SD_INFO = 'W',
@ -89,6 +90,13 @@ void sc64_write_data (void *src, void *dst, size_t length) {
dma_wait();
}
sc64_error_t sc64_get_api_version (uint32_t *api_version) {
sc64_cmd_t cmd = { .id = CMD_ID_API_VERSION_GET };
sc64_error_t error = sc64_execute_cmd(&cmd);
*api_version = cmd.rsp[0];
return error;
}
sc64_error_t sc64_get_config (sc64_cfg_t id, void *value) {
sc64_cmd_t cmd = { .id = CMD_ID_CONFIG_GET, .arg = { id } };
sc64_error_t error = sc64_execute_cmd(&cmd);

View File

@ -60,6 +60,7 @@ void sc64_lock (void);
bool sc64_check_presence (void);
void sc64_read_data (void *src, void *dst, size_t length);
void sc64_write_data (void *src, void *dst, size_t length);
sc64_error_t sc64_get_api_version (uint32_t *api_version);
sc64_error_t sc64_get_config (sc64_cfg_t cfg, void *value);
sc64_error_t sc64_set_config (sc64_cfg_t cfg, uint32_t value);
sc64_error_t sc64_writeback_enable (void *address);

View File

@ -10,8 +10,20 @@
static void init (void) {
assertf(usb_initialize() != CART_NONE, "No flashcart was detected");
switch (flashcart_init()) {
case FLASHCART_OK:
break;
case FLASHCART_ERROR_OUTDATED:
assertf(false, "Outdated flashcart firmware");
break;
case FLASHCART_ERROR_UNSUPPORTED:
assertf(false, "Unsupported flashcart");
break;
default:
assertf(false, "Unknown error while initializing flashcart");
break;
}
assertf(debug_init_sdfs("sd:/", -1), "Couldn't initialize SD card");
assertf(flashcart_init(), "Couldn't initialize flashcart");
}
static void deinit (void) {