Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
43852fe
Improve savetype state
networkfusion Oct 23, 2023
55268cf
Improve writeback
networkfusion Oct 23, 2023
63292ba
Fix rom size
networkfusion Oct 28, 2023
047e74c
Use switch rather than if
networkfusion Oct 28, 2023
41d05d2
Improve some comments
networkfusion Oct 28, 2023
b3cdc6b
Add device variant
networkfusion Oct 28, 2023
db4c73c
Move save on init to a seperate function.
networkfusion Oct 28, 2023
05675e8
Start adding device variants
networkfusion Oct 28, 2023
c88e69c
Update ed64_ll.c
networkfusion Oct 28, 2023
404f977
Work on EEPROM get and set
networkfusion Oct 28, 2023
b06e8f2
Fix eeprom get-set
networkfusion Oct 30, 2023
a487811
Further EEPROM improvements
networkfusion Oct 30, 2023
9a217af
Dont reset save types
networkfusion Oct 30, 2023
95edc70
Merge remote-tracking branch 'upstream/main' into ed64p-refactor
networkfusion Nov 1, 2023
0592068
Merge remote-tracking branch 'upstream/main' into ed64p-refactor
networkfusion Nov 10, 2023
bb6b6e2
Update submodules
networkfusion Nov 10, 2023
ea8d8c4
Revert "Update submodules"
networkfusion Nov 10, 2023
f89d7a5
Revert, main was correct!
networkfusion Nov 10, 2023
8c5d919
Merge branch 'ed64-basic' into ed64p-refactor
networkfusion Nov 10, 2023
5c15273
Revert "Revert, main was correct!"
networkfusion Nov 10, 2023
98b5837
Improve savetype state
networkfusion Oct 23, 2023
6d383ab
Improve writeback
networkfusion Oct 23, 2023
6836226
Fix rom size
networkfusion Oct 28, 2023
5e7163d
Use switch rather than if
networkfusion Oct 28, 2023
2c4c7e1
Improve some comments
networkfusion Oct 28, 2023
e689bab
Add device variant
networkfusion Oct 28, 2023
f92df19
Move save on init to a seperate function.
networkfusion Oct 28, 2023
13c8b02
Start adding device variants
networkfusion Oct 28, 2023
54ee1d6
Update ed64_ll.c
networkfusion Oct 28, 2023
d66301e
Work on EEPROM get and set
networkfusion Oct 28, 2023
8995818
Fix eeprom get-set
networkfusion Oct 30, 2023
4946cc4
Further EEPROM improvements
networkfusion Oct 30, 2023
8988f59
Dont reset save types
networkfusion Oct 30, 2023
89eb0ea
Update submodules
networkfusion Nov 10, 2023
6d28a80
Revert "Update submodules"
networkfusion Nov 10, 2023
2ff9458
Revert, main was correct!
networkfusion Nov 10, 2023
c6aa993
Revert "Revert, main was correct!"
networkfusion Nov 10, 2023
70ebf8e
Part re-align from merge
networkfusion Nov 14, 2023
003c366
Merge branch 'ed64p-refactor' of https://github.com/networkfusion/N64…
networkfusion Nov 14, 2023
68c32b2
Update ed64_ll.c
networkfusion Nov 14, 2023
8cb9701
Work on SRAM
networkfusion Nov 15, 2023
4ca2742
Attempted SRAM improvements
networkfusion Nov 15, 2023
9f77027
Remove pi_initalize
networkfusion Nov 15, 2023
81cdd46
minor comment improvements
networkfusion Nov 15, 2023
7a9f7ce
space fix
networkfusion Nov 15, 2023
c23a66c
Ensure load on NMI reset only
networkfusion Nov 16, 2023
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
156 changes: 87 additions & 69 deletions src/flashcart/ed64/ed64.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@
#include "ed64.h"
#include "ed64_state.h"


/**
* The ED64's variant, read from the CPLD
*/
// static ed64_device_variant_t device_variant = DEVICE_VARIANT_UNKNOWN;

/**
* The ED64's current state
*/
static ed64_pseudo_writeback_t current_state;

extern int ed_exit (void);
static flashcart_err_t ed64_pesudo_set_save_writeback (void);

static flashcart_err_t ed64_init (void) {

Expand All @@ -29,58 +37,17 @@ 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.

// load the ed64 state file
ed64_state_load(&current_state);

// Cold restarts are only available with battery backup.
// FIXME: add hot restarts with: ` || RTC == true`

if (current_state.is_expecting_save_writeback == true) {

// make sure next boot doesnt trigger the check changing its state.
current_state.is_expecting_save_writeback = false;
ed64_state_save(&current_state);

// Now save the content back to the SD card!
FIL fil;
UINT bw;
uint8_t cartsave_data[KiB(128)];

// find the path to last save
if (file_exists(strip_sd_prefix(current_state.last_save_path))) {

int save_size = file_get_size(current_state.last_save_path);

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;
}

// everdrive doesn't care about the save type other than flash sram and eeprom
// so minus flashram we can just check the size
if (current_state.is_fram_save_type == true) { // flashram is bugged atm
ed64_ll_get_fram(cartsave_data, save_size);
// deletes flag
current_state.is_fram_save_type = false;
ed64_state_save(&current_state);
}
else if (save_size > KiB(2)) { // sram
ed64_ll_get_sram(cartsave_data, save_size);
}
else { // eeprom
ed64_ll_get_eeprom(cartsave_data, save_size);
}

if (f_write(&fil, cartsave_data, save_size, &bw) != FR_OK) {
return FLASHCART_ERR_LOAD;
}

if (f_close(&fil) != FR_OK) {
return FLASHCART_ERR_LOAD;
}
}
else {
current_state.is_expecting_save_writeback = false;
current_state.is_fram_save_type = false;
current_state.last_save_path = "";
ed64_state_save(&current_state);
}
return ed64_pesudo_set_save_writeback();
}

return FLASHCART_OK;
}

Expand Down Expand Up @@ -116,15 +83,15 @@ static flashcart_err_t ed64_load_rom (char *rom_path, flashcart_progress_callbac

// 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.
// EEPROM saves should be unaffected.
if (rom_size > MiB(64)) {
f_close(&fil);
return FLASHCART_ERR_LOAD;
}

// FIXME: is this actually needed?
/*
if (rom_size == MiB(64)) {
if (rom_size <= MiB(64)) {
ed64_save_type_t type = ed64_ll_get_save_type();
switch (type) {
case SAVE_TYPE_SRAM:
Expand Down Expand Up @@ -179,6 +146,7 @@ static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uin

// 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.
// EEPROM saves should be unaffected.

if (file_size > (MiB(64) - rom_offset)) {
f_close(&fil);
Expand Down Expand Up @@ -206,7 +174,7 @@ static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uin
return FLASHCART_OK;
}

static flashcart_err_t ed64_load_save (char *save_path) {
static flashcart_err_t ed64_load_save (char *save_path) { // from file

FIL fil;
UINT br;
Expand All @@ -216,42 +184,34 @@ static flashcart_err_t ed64_load_save (char *save_path) {
return FLASHCART_ERR_LOAD;
}

size_t save_size = file_get_size(save_path);
uint8_t cartsave_data[save_size];
size_t save_file_size = file_get_size(strip_sd_prefix(save_path));
uint8_t cartsave_data[save_file_size];

if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) {
if (f_read(&fil, cartsave_data, save_file_size, &br) != FR_OK) {
f_close(&fil);
return FLASHCART_ERR_LOAD;
}

if (br != save_size) {
return FLASHCART_ERR_LOAD;
}

current_state.is_fram_save_type = false;

ed64_save_type_t type = ed64_ll_get_save_type();
ed64_save_type_t type = ed64_ll_get_save_type(); // current_state.is_save_type;
switch (type) {
case SAVE_TYPE_EEPROM_4K:
case SAVE_TYPE_EEPROM_16K:
ed64_ll_set_eeprom(cartsave_data, save_size);
ed64_ll_set_eeprom(cartsave_data, type);
break;
case SAVE_TYPE_SRAM:
case SAVE_TYPE_SRAM_128K:
ed64_ll_set_sram(cartsave_data, save_size);
ed64_ll_set_sram(cartsave_data, 0, save_file_size);
break;
case SAVE_TYPE_FLASHRAM:
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
current_state.is_fram_save_type = true;
ed64_state_save(&current_state);
ed64_ll_set_fram(cartsave_data, save_file_size);
break;
case SAVE_TYPE_DD64_CART_PORT:
break;
default:
break;
}


current_state.is_save_type = type; // SHOULD HAVE BEEN SET FROM ed64_set_save_type()
current_state.last_save_path = save_path;
current_state.is_expecting_save_writeback = true;
ed64_state_save(&current_state);
Expand Down Expand Up @@ -289,6 +249,64 @@ static flashcart_err_t ed64_set_save_type (flashcart_save_type_t save_type) {

ed64_ll_set_save_type(type);

current_state.is_save_type = type;
ed64_state_save(&current_state);

return FLASHCART_OK;
}

static flashcart_err_t ed64_pesudo_set_save_writeback (void) {
// save the content back to the SD card!
FIL fil;
UINT bw;
uint8_t cartsave_data[KiB(128)];

// find the path to last save
if (
file_exists(strip_sd_prefix(current_state.last_save_path))
&& current_state.is_save_type != SAVE_TYPE_NONE
&& sys_reset_type() != RESET_COLD
) {

int save_size = file_get_size(current_state.last_save_path);

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;
}

switch (current_state.is_save_type) {
case SAVE_TYPE_EEPROM_4K:
case SAVE_TYPE_EEPROM_16K:
ed64_ll_get_eeprom(cartsave_data, current_state.is_save_type);
break;
case SAVE_TYPE_SRAM:
case SAVE_TYPE_SRAM_128K:
ed64_ll_get_sram(cartsave_data, 0, save_size);
break;
case SAVE_TYPE_FLASHRAM:
ed64_ll_get_fram(cartsave_data, save_size);
break;
case SAVE_TYPE_DD64_CART_PORT:
break;
default:
break;
}

if (f_write(&fil, cartsave_data, save_size, &bw) != FR_OK) {
return FLASHCART_ERR_LOAD;
}

if (f_close(&fil) != FR_OK) {
return FLASHCART_ERR_LOAD;
}
}

// make sure next boot doesn't trigger the check changing its state.
current_state.is_expecting_save_writeback = false;
current_state.is_save_type = SAVE_TYPE_NONE;
current_state.last_save_path = "";
ed64_state_save(&current_state);

return FLASHCART_OK;
}

Expand Down
Loading