Skip to content

Commit

Permalink
Add basic support for ED64 X series
Browse files Browse the repository at this point in the history
Only loads ROM's
  • Loading branch information
networkfusion committed Jun 8, 2024
1 parent 208527a commit 5031318
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions src/flashcart/64drive/64drive.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static bool d64_has_feature (flashcart_features_t feature) {
case FLASHCART_FEATURE_64DD: return false;
case FLASHCART_FEATURE_RTC: return true;
case FLASHCART_FEATURE_USB: return true;
case FLASHCART_FEATURE_AUTO_CIC: return true;
default: return false;
}
}
Expand Down
160 changes: 160 additions & 0 deletions src/flashcart/ed64x/ed64x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include <fatfs/ff.h>
#include <libdragon.h>

#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;
}
24 changes: 24 additions & 0 deletions src/flashcart/ed64x/ed64x.h
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions src/flashcart/ed64x/ed64x_ll.h
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions src/flashcart/flashcart.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "flashcart_utils.h"

#include "64drive/64drive.h"
#include "ed64x/ed64x.h"
#include "sc64/sc64.h"


Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/flashcart/flashcart.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef enum {
FLASHCART_FEATURE_64DD,
FLASHCART_FEATURE_RTC,
FLASHCART_FEATURE_USB,
FLASHCART_FEATURE_AUTO_CIC,
} flashcart_features_t;

/** @brief Flashcart save type enumeration */
Expand Down
1 change: 1 addition & 0 deletions src/flashcart/sc64/sc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ static bool sc64_has_feature (flashcart_features_t feature) {
case FLASHCART_FEATURE_64DD: return true;
case FLASHCART_FEATURE_RTC: return true;
case FLASHCART_FEATURE_USB: return true;
case FLASHCART_FEATURE_AUTO_CIC: return true;
default: return false;
}
}
Expand Down

0 comments on commit 5031318

Please sign in to comment.