fixed sram 128 and added suggested changes

This commit is contained in:
ariahiro64 2023-10-19 08:13:06 -04:00
parent c30fc873b0
commit f49f7a47f3

View File

@ -12,83 +12,110 @@
#include "ed64_ll.h" #include "ed64_ll.h"
#include "ed64.h" #include "ed64.h"
// #include "../menu/settings.h" // #include "../menu/settings.h"
// This is a trial hack before using the settings API. // This is a trial hack before using the settings API.
#ifndef LAST_SAVE_FILE_PATH #ifndef LAST_SAVE_FILE_PATH
#define LAST_SAVE_FILE_PATH "/menu/last_rom.txt" #define LAST_SAVE_FILE_PATH "/menu/last_rom.txt"
#endif #endif
#ifndef RESET_CHECK_FILE_PATH
#define RESET_CHECK_FILE_PATH "/menu/RESET"
#endif
#ifndef FLASHRAM_CHECK_FILE_PATH
#define FLASHRAM_CHECK_FILE_PATH "/menu/FLASHRAM"
#endif
extern int ed_exit(void); extern int ed_exit(void);
static flashcart_err_t ed64_init (void) { static flashcart_err_t ed64_init(void)
{
// TODO: partly already done, see https://github.com/DragonMinded/libdragon/blob/4ec469d26b6dc4e308caf3d5b86c2b340b708bbd/src/libcart/cart.c#L1064
if (file_exists(strip_sd_prefix("/menu/RESET"))) { // checks if reset // FIXME: Update firmware if needed.
// FIXME: Enable RTC if available.
f_unlink(strip_sd_prefix("/menu/RESET"));
FIL lrp_fil;
UINT lrp_br;
TCHAR lrp_path[255*32];
// FIXME: use max supported length // older everdrives cant save during gameplay so we need to the reset method.
// works by checking if a file exists.
if (f_open(&lrp_fil, LAST_SAVE_FILE_PATH, FA_READ) != FR_OK) {
return FLASHCART_ERR_LOAD;
}
int lrp_size = f_size(&lrp_fil); if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH)))
{
if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) { // make sure next boot doesnt trigger the check by deleting the reset file
f_close(&lrp_fil); f_unlink(strip_sd_prefix(RESET_CHECK_FILE_PATH));
return FLASHCART_ERR_LOAD;
}
if (f_close(&lrp_fil) != FR_OK) { // finds the last save location
return FLASHCART_ERR_LOAD; FIL lrp_fil;
} UINT lrp_br;
// Now save the content back to the SD! if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK)
{
FIL fil; return FLASHCART_ERR_LOAD;
UINT br; }
uint8_t cartsave_data[KiB(128)];
// Older everdrives cant save during gameplay so we need to the reset method.
// find the path to last save int lrp_size = f_size(&lrp_fil);
if (file_exists(strip_sd_prefix(lrp_path))) {
int save_size = file_get_size(strip_sd_prefix(lrp_path)); TCHAR lrp_path[lrp_size++];
if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_READ | FA_WRITE)) != FR_OK) { if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK)
return FLASHCART_ERR_LOAD; {
} f_close(&lrp_fil);
return FLASHCART_ERR_LOAD;
}
// everdrive doesn't care about the save type other than eeprom if (f_close(&lrp_fil) != FR_OK)
// so we can just check the size {
if (save_size == KiB(128)) { return FLASHCART_ERR_LOAD;
getFlashRAM(cartsave_data, save_size); }
} else if (save_size > KiB(2)) {
getSRAM(cartsave_data, save_size);
} else {
getEeprom(cartsave_data, save_size);
}
if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) {
return FLASHCART_ERR_LOAD;
}
if (f_close(&fil) != FR_OK) { // Now save the content back to the SD!
return FLASHCART_ERR_LOAD; FIL fil;
UINT br;
uint8_t cartsave_data[KiB(128)];
// find the path to last save
if (file_exists(strip_sd_prefix(lrp_path)))
{
int save_size = file_get_size(strip_sd_prefix(lrp_path));
if ((f_open(&fil, strip_sd_prefix(lrp_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 (file_exists(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH)))
{ // flashram is bugged atm
getFlashRAM(cartsave_data, save_size);
// deletes flag
f_unlink(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH));
}
else if (save_size > KiB(2))
{ // sram
getSRAM(cartsave_data, save_size);
}
else
{ // eeprom
getEeprom(cartsave_data, save_size);
}
if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK)
{
return FLASHCART_ERR_LOAD;
}
if (f_close(&fil) != FR_OK)
{
return FLASHCART_ERR_LOAD;
}
} }
} }
}
return FLASHCART_OK; return FLASHCART_OK;
} }
static flashcart_err_t ed64_deinit(void)
static flashcart_err_t ed64_deinit (void) { {
// For the moment, just use libCart exit. // For the moment, just use libCart exit.
ed_exit(); ed_exit();
@ -96,18 +123,24 @@ static flashcart_err_t ed64_deinit (void) {
return FLASHCART_OK; return FLASHCART_OK;
} }
static bool ed64_has_feature (flashcart_features_t feature) { static bool ed64_has_feature(flashcart_features_t feature)
switch (feature) { {
case FLASHCART_FEATURE_64DD: return false; switch (feature)
default: return false; {
case FLASHCART_FEATURE_64DD:
return false;
default:
return false;
} }
} }
static flashcart_err_t ed64_load_rom (char *rom_path, flashcart_progress_callback_t *progress) { static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback_t *progress)
{
FIL fil; FIL fil;
UINT br; UINT br;
if (f_open(&fil, strip_sd_prefix(rom_path), FA_READ) != FR_OK) { if (f_open(&fil, strip_sd_prefix(rom_path), FA_READ) != FR_OK)
{
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
@ -117,7 +150,9 @@ 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. // 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. // Or somehow warn that certain ROM's will have corruption due to the address space being used for saves.
if (rom_size > MiB(64)) { // Conker's Bad Fur Day doesn't have this issue because eeprom data is at a fixed address in pif ram.
if (rom_size > MiB(64))
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
@ -125,34 +160,40 @@ static flashcart_err_t ed64_load_rom (char *rom_path, flashcart_progress_callbac
size_t sdram_size = rom_size; size_t sdram_size = rom_size;
size_t chunk_size = MiB(1); size_t chunk_size = MiB(1);
for (int offset = 0; offset < sdram_size; offset += chunk_size) { for (int offset = 0; offset < sdram_size; offset += chunk_size)
{
size_t block_size = MIN(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) { if (f_read(&fil, (void *)(ROM_ADDRESS + offset), block_size, &br) != FR_OK)
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
if (progress) { if (progress)
progress(f_tell(&fil) / (float) (f_size(&fil))); {
progress(f_tell(&fil) / (float)(f_size(&fil)));
} }
} }
if (f_tell(&fil) != sdram_size) { if (f_tell(&fil) != sdram_size)
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
if (f_close(&fil) != FR_OK)
if (f_close(&fil) != FR_OK) { {
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
return FLASHCART_OK; return FLASHCART_OK;
} }
static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uint32_t file_offset) { static flashcart_err_t ed64_load_file(char *file_path, uint32_t rom_offset, uint32_t file_offset)
{
FIL fil; FIL fil;
UINT br; UINT br;
if (f_open(&fil, strip_sd_prefix(file_path), FA_READ) != FR_OK) { if (f_open(&fil, strip_sd_prefix(file_path), FA_READ) != FR_OK)
{
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
@ -163,125 +204,159 @@ 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. // 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. // Or somehow warn that certain ROM's will have corruption due to the address space being used for saves.
if (file_size > (MiB(64) - rom_offset)) { if (file_size > (MiB(64) - rom_offset))
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_ARGS; return FLASHCART_ERR_ARGS;
} }
if (f_lseek(&fil, file_offset) != FR_OK) { if (f_lseek(&fil, file_offset) != FR_OK)
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
if (f_read(&fil, (void *) (ROM_ADDRESS + rom_offset), file_size, &br) != FR_OK) { if (f_read(&fil, (void *)(ROM_ADDRESS + rom_offset), file_size, &br) != FR_OK)
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
if (br != file_size) { if (br != file_size)
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
if (f_close(&fil) != FR_OK) { if (f_close(&fil) != FR_OK)
{
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
return FLASHCART_OK; return FLASHCART_OK;
} }
static flashcart_err_t ed64_load_save (char *save_path) { static flashcart_err_t ed64_load_save(char *save_path)
{
FIL fil; FIL fil;
UINT br; UINT br;
if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) { if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK)
f_close(&fil); {
return FLASHCART_ERR_LOAD; f_close(&fil);
return FLASHCART_ERR_LOAD;
} }
size_t save_size = file_get_size(strip_sd_prefix(save_path)); size_t save_size = file_get_size(strip_sd_prefix(save_path));
uint8_t cartsave_data[save_size]; uint8_t cartsave_data[save_size];
if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) { if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK)
{
f_close(&fil); f_close(&fil);
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
if (f_close(&fil) != FR_OK) { if (f_close(&fil) != FR_OK)
{
return FLASHCART_ERR_LOAD; return FLASHCART_ERR_LOAD;
} }
// everdrive doesnt care about the save type other than eeprom ed64_save_type_t type = ed64_ll_get_save_type();
// so we can just check the size switch (type)
{
if (save_size == KiB(128)) { //sram 128 and flash case SAVE_TYPE_EEPROM_4K:
setFlashRAM(cartsave_data, save_size); case SAVE_TYPE_EEPROM_16K:
}
else if (save_size > KiB(2)) { //sram
setSRAM(cartsave_data, save_size);
}
else if (save_size >= 512){ // eeprom
setEeprom(cartsave_data, save_size); setEeprom(cartsave_data, save_size);
break;
case SAVE_TYPE_SRAM:
case SAVE_TYPE_SRAM_128K:
setSRAM(cartsave_data, save_size);
break;
case SAVE_TYPE_FLASHRAM:
setFlashRAM(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
FIL flashfil;
if (f_open(&flashfil, strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK)
{
f_close(&flashfil);
return FLASHCART_ERR_LOAD;
}
if (f_close(&flashfil) != FR_OK)
{
return FLASHCART_OK;
}
break;
default:
break;
} }
FIL lsp_fil; FIL lsp_fil;
UINT lsp_bw; UINT lsp_bw;
if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { if (f_open(&lsp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)
return FLASHCART_ERR_LOAD; {
return FLASHCART_ERR_LOAD;
} }
if (f_write(&lsp_fil, strip_sd_prefix(save_path) , strlen(save_path), &lsp_bw) != FR_OK) { if (f_write(&lsp_fil, strip_sd_prefix(save_path), strlen(save_path), &lsp_bw) != FR_OK)
f_close(&lsp_fil); {
return FLASHCART_ERR_LOAD; f_close(&lsp_fil);
return FLASHCART_ERR_LOAD;
} }
if (f_close(&lsp_fil) != FR_OK) { if (f_close(&lsp_fil) != FR_OK)
return FLASHCART_ERR_LOAD; {
return FLASHCART_ERR_LOAD;
} }
FIL rsfil; FIL rsfil;
// this wries a 1 byte file as it only needs to exist to detect a reset // simulate a unix touch command to create a file as it only needs to exist to detect a reset
if (f_open(&rsfil, "/menu/RESET", FA_CREATE_ALWAYS ) != FR_OK) { if (f_open(&rsfil, strip_sd_prefix(RESET_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK)
f_close(&rsfil); {
return FLASHCART_ERR_LOAD; f_close(&rsfil);
return FLASHCART_ERR_LOAD;
} }
if (f_close(&rsfil) != FR_OK) { if (f_close(&rsfil) != FR_OK)
{
return FLASHCART_OK; return FLASHCART_OK;
} }
return FLASHCART_OK; return FLASHCART_OK;
} }
static flashcart_err_t ed64_set_save_type (flashcart_save_type_t save_type) { static flashcart_err_t ed64_set_save_type(flashcart_save_type_t save_type)
{
ed64_save_type_t type; ed64_save_type_t type;
switch (save_type) { switch (save_type)
case FLASHCART_SAVE_TYPE_NONE: {
type = SAVE_TYPE_NONE; case FLASHCART_SAVE_TYPE_NONE:
break; type = SAVE_TYPE_NONE;
case FLASHCART_SAVE_TYPE_EEPROM_4K: break;
type = SAVE_TYPE_EEPROM_4K; case FLASHCART_SAVE_TYPE_EEPROM_4K:
break; type = SAVE_TYPE_EEPROM_4K;
case FLASHCART_SAVE_TYPE_EEPROM_16K: break;
type = SAVE_TYPE_EEPROM_16K; case FLASHCART_SAVE_TYPE_EEPROM_16K:
break; type = SAVE_TYPE_EEPROM_16K;
case FLASHCART_SAVE_TYPE_SRAM: break;
type = SAVE_TYPE_SRAM; case FLASHCART_SAVE_TYPE_SRAM:
break; type = SAVE_TYPE_SRAM;
case FLASHCART_SAVE_TYPE_SRAM_BANKED: break;
case FLASHCART_SAVE_TYPE_SRAM_128K: case FLASHCART_SAVE_TYPE_SRAM_BANKED:
type = SAVE_TYPE_SRAM_128K; case FLASHCART_SAVE_TYPE_SRAM_128K:
break; type = SAVE_TYPE_SRAM_128K;
case FLASHCART_SAVE_TYPE_FLASHRAM_PKST2: break;
case FLASHCART_SAVE_TYPE_FLASHRAM: case FLASHCART_SAVE_TYPE_FLASHRAM_PKST2:
type = SAVE_TYPE_FLASHRAM; case FLASHCART_SAVE_TYPE_FLASHRAM:
break; type = SAVE_TYPE_FLASHRAM;
default: break;
return FLASHCART_ERR_ARGS; default:
return FLASHCART_ERR_ARGS;
} }
ed64_ll_set_save_type(type); ed64_ll_set_save_type(type);
@ -289,7 +364,6 @@ static flashcart_err_t ed64_set_save_type (flashcart_save_type_t save_type) {
return FLASHCART_OK; return FLASHCART_OK;
} }
static flashcart_t flashcart_ed64 = { static flashcart_t flashcart_ed64 = {
.init = ed64_init, .init = ed64_init,
.deinit = ed64_deinit, .deinit = ed64_deinit,
@ -303,7 +377,7 @@ static flashcart_t flashcart_ed64 = {
.set_save_writeback = NULL, .set_save_writeback = NULL,
}; };
flashcart_t *ed64_get_flashcart(void)
flashcart_t *ed64_get_flashcart (void) { {
return &flashcart_ed64; return &flashcart_ed64;
} }