Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[develop] Add basic support for ED64 X series #137

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/ed64/ed64_vseries.c \
flashcart/ed64/ed64_xseries.c \
flashcart/flashcart.c \
flashcart/sc64/sc64_ll.c \
flashcart/sc64/sc64.c \
Expand Down
162 changes: 162 additions & 0 deletions src/flashcart/ed64/ed64_xseries.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#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 "ed64_xseries_ll.h"
#include "ed64_xseries.h"

typedef enum {
// potentially handle if the firmware supports it...
ED64_X5_0 = 550,
ED64_X7_0 = 570,
ED64_UKNOWN = 0,
} ed64_xseries_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 ed64_xseries_init (void) {

return FLASHCART_OK;
}

static flashcart_err_t ed64_xseries_deinit (void) {

return FLASHCART_OK;
}

static ed64_xseries_device_variant_t get_cart_model() {
ed64_xseries_device_variant_t variant = ED64_X7_0; // FIXME: check cart model from ll for better feature handling.
return variant;
}

static bool ed64_xseries_has_feature (flashcart_features_t feature) {
bool is_model_x7 = (get_cart_model() == ED64_X7_0);
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_64DD: return false;
case FLASHCART_FEATURE_AUTO_CIC: return true;
case FLASHCART_FEATURE_AUTO_REGION: return true;
default: return false;
}
}

static flashcart_err_t ed64_xseries_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 ed64_xseries_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 ed64_xseries_load_save (char *save_path) {


return FLASHCART_OK;
}

static flashcart_err_t ed64_xseries_set_save_type (flashcart_save_type_t save_type) {


return FLASHCART_OK;
}

static flashcart_t flashcart_ed64_xseries = {
.init = ed64_xseries_init,
.deinit = ed64_xseries_deinit,
.has_feature = ed64_xseries_has_feature,
.load_rom = ed64_xseries_load_rom,
.load_file = ed64_xseries_load_file,
.load_save = ed64_xseries_load_save,
.load_64dd_ipl = NULL,
.load_64dd_disk = NULL,
.set_save_type = ed64_xseries_set_save_type,
.set_save_writeback = NULL,
};


flashcart_t *ed64_xseries_get_flashcart (void) {
return &flashcart_ed64_xseries;
}
2 changes: 1 addition & 1 deletion src/flashcart/ed64/ed64_xseries.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @{
*/

flashcart_t *ed64xseries_get_flashcart (void);
flashcart_t *ed64_xseries_get_flashcart (void);

/** @} */ /* ED64_Xseries */

Expand Down
14 changes: 14 additions & 0 deletions src/flashcart/ed64/ed64_xseries_ll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file ed64_xseries_ll.h
* @brief ed64x flashcart low level access
* @ingroup flashcart
*/

#ifndef FLASHCART_ED64_XSERIES_LL_H__
#define FLASHCART_ED64_XSERIES_LL_H__


/** @} */ /* ed64_xseries_ll */


#endif
8 changes: 4 additions & 4 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 "ed64/ed64_vseries.h"
#include "ed64/ed64_xseries.h"
#include "64drive/64drive.h"
#include "sc64/sc64.h"

Expand Down Expand Up @@ -109,10 +110,9 @@ flashcart_err_t flashcart_init (const char **storage_prefix) {
flashcart = d64_get_flashcart();
break;

// FIXME: this is commented out awaiting a fix from libcart.
// case CART_EDX: // Series X EverDrive-64
// flashcart = ed64_xseries_get_flashcart();
// break;
case CART_EDX: // Official EverDrive 64 Series X
flashcart = ed64_xseries_get_flashcart();
break;

case CART_ED: // Series V EverDrive-64 or clone
flashcart = ed64_vseries_get_flashcart();
Expand Down