From 17132dd3e65f8ec5d1c85b5f41ff3c7a7f83b70a Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Tue, 10 Oct 2023 20:49:29 +0000 Subject: [PATCH 01/38] Fix saves for real --- src/flashcart/ed64/ed64.c | 3 --- src/flashcart/ed64/ed64_ll.h | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 15824248..4869b912 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -12,7 +12,6 @@ #include "ed64_ll.h" #include "ed64.h" -#define EEPROM_ADDRESS (0x1FFE2000) extern int ed_exit(void); @@ -125,8 +124,6 @@ static flashcart_error_t ed64_load_save (char *save_path) { switch (type) { case SAVE_TYPE_EEPROM_4K: case SAVE_TYPE_EEPROM_16K: - address = (void *) (EEPROM_ADDRESS); - break; case SAVE_TYPE_SRAM: case SAVE_TYPE_SRAM_128K: case SAVE_TYPE_FLASHRAM: diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 0faa635d..1e8f3cf0 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -30,7 +30,7 @@ typedef enum { } ed64_save_type_t; -#define SRAM_ADDRESS (0xA8000000) +#define SRAM_ADDRESS (0x1FFE2000) #define ROM_ADDRESS (0xB0000000) /* Save functions */ From 37f32bd2e61a82f8ab914ded9402cfea5d117f0f Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Fri, 13 Oct 2023 13:53:00 -0400 Subject: [PATCH 02/38] really rough prototype code --- src/flashcart/ed64/ed64.c | 61 +++++------ src/flashcart/ed64/ed64_ll.c | 197 +++++++++++++++++++++++++++++++++++ src/flashcart/ed64/ed64_ll.h | 149 +++++++++++++++++++++++++- src/flashcart/ed64/types.h | 36 +++++++ 4 files changed, 409 insertions(+), 34 deletions(-) create mode 100644 src/flashcart/ed64/types.h diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 313722ab..c8d7cce7 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -18,17 +18,23 @@ extern int ed_exit(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 + FIL fil, rsfil; + UINT br; + if (f_open(&rsfil, strip_sd_prefix("/menu/RESET"), FA_READ | FA_OPEN_EXISTING) == FR_OK) { + f_close(&rsfil); + f_unlink(strip_sd_prefix("/menu/RESET")); + int size = KiB(128); + uint8_t cartsave_data[size]; + f_open(&fil, strip_sd_prefix("test.sav"), FA_WRITE | FA_CREATE_NEW); + getSRAM(cartsave_data, size); + f_write(&fil, cartsave_data, size, &br); + f_close(&fil); + } - // FIXME: Update firmware if needed. - // FIXME: Enable RTC if available. - - // FIXME: retrive a config file from (probably SRAM) that might have been set. - // This should include the location of the ROM and its save type. - // Then, if it is valid, perform a save. - return FLASHCART_OK; } + static flashcart_err_t ed64_deinit (void) { // For the moment, just use libCart exit. @@ -134,43 +140,32 @@ static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uin } static flashcart_err_t ed64_load_save (char *save_path) { - void *address = NULL; - ed64_save_type_t type = ed64_ll_get_save_type(); - - switch (type) { - case SAVE_TYPE_EEPROM_4K: - case SAVE_TYPE_EEPROM_16K: - case SAVE_TYPE_SRAM: - case SAVE_TYPE_SRAM_128K: - case SAVE_TYPE_FLASHRAM: - address = (void *) (SRAM_ADDRESS); - break; - case SAVE_TYPE_NONE: - default: - return FLASHCART_ERR_ARGS; - } - + //ed64_save_type_t type = ed64_ll_get_save_type(); FIL fil; UINT br; - if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) { - return FLASHCART_ERR_LOAD; - } + if (f_open(&fil, strip_sd_prefix("test.sav"), FA_READ) == FR_OK) { - size_t save_size = f_size(&fil); + size_t save_size = KiB(128); + uint8_t cartsave_data[save_size]; - if (f_read(&fil, address, save_size, &br) != FR_OK) { - f_close(&fil); - return FLASHCART_ERR_LOAD; + if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) { + f_close(&fil); + return FLASHCART_ERR_LOAD; } if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } - if (br != save_size) { - return FLASHCART_ERR_LOAD; - } + setSRAM(cartsave_data, save_size); +} + FIL rsfil; + UINT rsbr; + TCHAR byte[1]; + f_open(&rsfil, strip_sd_prefix("/menu/RESET"), FA_WRITE | FA_CREATE_ALWAYS); + f_write(&rsfil, (void *)byte, 1, &rsbr); + f_close(&rsfil); return FLASHCART_OK; } diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 3eaa70b1..fa9b6328 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -114,3 +114,200 @@ void ed64_ll_set_sram_bank(uint8_t bank) { ed64_ll_sram_bank = bank == 0 ? 0 : 1; } + + +#include +#include +#include +#include "types.h" + + +void PI_Init(void) { + PI_DMAWait(); + IO_WRITE(PI_STATUS_REG, 0x03); +} + +// Inits PI for sram transfer +void PI_Init_SRAM(void) { + + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); + +} + +void PI_DMAWait(void) { + + while (IO_READ(PI_STATUS_REG) & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)); +} + + +void PI_DMAFromSRAM(void *dest, u32 offset, u32 size) { + + + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); + asm volatile ("" : : : "memory"); + IO_WRITE(PI_WR_LEN_REG, (size - 1)); + asm volatile ("" : : : "memory"); + +} + + +void PI_DMAToSRAM(void *src, u32 offset, u32 size) { //void* + PI_DMAWait(); + + IO_WRITE(PI_STATUS_REG, 2); + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); + IO_WRITE(PI_RD_LEN_REG, (size - 1)); +} + +void PI_DMAFromCart(void* dest, void* src, u32 size) { + PI_DMAWait(); + + IO_WRITE(PI_STATUS_REG, 0x03); + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(src)); + IO_WRITE(PI_WR_LEN_REG, (size - 1)); +} + + +void PI_DMAToCart(void* dest, void* src, u32 size) { + PI_DMAWait(); + + IO_WRITE(PI_STATUS_REG, 0x02); + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); + IO_WRITE(PI_RD_LEN_REG, (size - 1)); +} + + +// Wrapper to support unaligned access to memory +void PI_SafeDMAFromCart(void *dest, void *src, u32 size) { + if (!dest || !src || !size) return; + + u32 unalignedSrc = ((u32)src) % 2; + u32 unalignedDest = ((u32)dest) % 8; + + //FIXME: Do i really need to check if size is 16bit aligned? + if (!unalignedDest && !unalignedSrc && !(size % 2)) { + PI_DMAFromCart(dest, src, size); + PI_DMAWait(); + + return; + } + + void* newSrc = (void*)(((u32)src) - unalignedSrc); + u32 newSize = (size + unalignedSrc) + ((size + unalignedSrc) % 2); + + u8 *buffer = memalign(8, newSize); + PI_DMAFromCart(buffer, newSrc, newSize); + PI_DMAWait(); + + memcpy(dest, (buffer + unalignedSrc), size); + + free(buffer); +} + +#include "types.h" + +int getSRAM( uint8_t *buffer, int size){ + while (dma_busy()) ; + + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); + + while (dma_busy()) ; + + PI_Init(); + + while (dma_busy()) ; + + PI_DMAFromSRAM(buffer, 0, size) ; + + while (dma_busy()) ; + + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); + + return 1; +} + +int getEeprom( uint8_t *buffer){ + int blocks=(16*1024)/8; + for( int b = 0; b < blocks; b++ ) { + eeprom_read( b, &buffer[b * 8] ); + } + + return 1; +} + + +/* +sram upload +*/ +int setSRAM( uint8_t *buffer,int size){ + //half working + PI_DMAWait(); + //Timing + PI_Init_SRAM(); + + //Readmode + PI_Init(); + + data_cache_hit_writeback_invalidate(buffer,size); + while (dma_busy()); + PI_DMAToSRAM(buffer, 0, size); + data_cache_hit_writeback_invalidate(buffer,size); + + //Wait + PI_DMAWait(); + //Restore evd Timing + setSDTiming(); + + return 1; +} + + +int setEeprom(uint8_t *buffer){ + int blocks=(16*1024)/8; + for( int b = 0; b < blocks; b++ ) { + eeprom_write( b, &buffer[b * 8] ); + } + + return 1; +} + +void setSDTiming(void){ + + // PI_DMAWait(); + IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); + + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); +} + + +void restoreTiming(void) { + //n64 timing restore :> + IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); + + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); +} diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 1e8f3cf0..30bc032d 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -7,10 +7,11 @@ #ifndef FLASHCART_ED64_LL_H__ #define FLASHCART_ED64_LL_H__ - +#include #include #include #include +#include "types.h" /** * @addtogroup ed64 @@ -40,4 +41,150 @@ void ed64_ll_set_save_type(ed64_save_type_t type); /** @} */ /* ed64 */ + +//sram.h + +void data_cache_hit_writeback_invalidate(volatile void *, unsigned long); +unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len); +void dma_write_sram(void* src, u32 offset, u32 size); +void dma_read_sram(void *dest, u32 offset, u32 size); +void dma_write_s(void * ram_address, unsigned long pi_address, unsigned long len); +void dma_read_s(void * ram_address, unsigned long pi_address, unsigned long len); +int writeSram(void* src, u32 size); +void setSDTiming(void); + + +void PI_Init(void); +void PI_Init_SRAM(void); +void PI_DMAWait(void); +void PI_DMAFromCart(void* dest, void* src, u32 size); +void PI_DMAToCart(void* dest, void* src, u32 size); +void PI_DMAFromSRAM(void *dest, u32 offset, u32 size); +void PI_DMAToSRAM(void* src, u32 offset, u32 size); +void PI_SafeDMAFromCart(void *dest, void *src, u32 size); + +//memory +/*** MEMORY ***/ +void *safe_memalign(size_t boundary, size_t size); +void *safe_calloc(size_t nmemb, size_t size); +void *safe_malloc(size_t size); +void safe_free(void *ptr); +void *safe_memset(void *s, int c, size_t n); +void *safe_memcpy(void *dest, const void *src, size_t n); + + +#define DP_BASE_REG 0x04100000 +#define VI_BASE_REG 0x04400000 +#define PI_BASE_REG 0x04600000 +#define PIF_RAM_START 0x1FC007C0 + + +/* + * PI status register has 3 bits active when read from (PI_STATUS_REG - read) + * Bit 0: DMA busy - set when DMA is in progress + * Bit 1: IO busy - set when IO is in progress + * Bit 2: Error - set when CPU issues IO request while DMA is busy + */ + +#define PI_STATUS_REG (PI_BASE_REG+0x10) + +/* PI DRAM address (R/W): starting RDRAM address */ +#define PI_DRAM_ADDR_REG (PI_BASE_REG+0x00) /* DRAM address */ + +/* PI pbus (cartridge) address (R/W): starting AD16 address */ +#define PI_CART_ADDR_REG (PI_BASE_REG+0x04) + +/* PI read length (R/W): read data length */ +#define PI_RD_LEN_REG (PI_BASE_REG+0x08) + +/* PI write length (R/W): write data length */ +#define PI_WR_LEN_REG (PI_BASE_REG+0x0C) + +/* + * PI status (R): [0] DMA busy, [1] IO busy, [2], error + * (W): [0] reset controller (and abort current op), [1] clear intr + */ + +#define PI_BSD_DOM1_LAT_REG (PI_BASE_REG+0x14) + +/* PI dom1 pulse width (R/W): [7:0] domain 1 device R/W strobe pulse width */ +#define PI_BSD_DOM1_PWD_REG (PI_BASE_REG+0x18) + +/* PI dom1 page size (R/W): [3:0] domain 1 device page size */ +#define PI_BSD_DOM1_PGS_REG (PI_BASE_REG+0x1C) /* page size */ + +/* PI dom1 release (R/W): [1:0] domain 1 device R/W release duration */ +#define PI_BSD_DOM1_RLS_REG (PI_BASE_REG+0x20) +/* PI dom2 latency (R/W): [7:0] domain 2 device latency */ +#define PI_BSD_DOM2_LAT_REG (PI_BASE_REG+0x24) /* Domain 2 latency */ + +/* PI dom2 pulse width (R/W): [7:0] domain 2 device R/W strobe pulse width */ +#define PI_BSD_DOM2_PWD_REG (PI_BASE_REG+0x28) /* pulse width */ + +/* PI dom2 page size (R/W): [3:0] domain 2 device page size */ +#define PI_BSD_DOM2_PGS_REG (PI_BASE_REG+0x2C) /* page size */ + +/* PI dom2 release (R/W): [1:0] domain 2 device R/W release duration */ +#define PI_BSD_DOM2_RLS_REG (PI_BASE_REG+0x30) /* release duration */ + + +#define PI_DOMAIN1_REG PI_BSD_DOM1_LAT_REG +#define PI_DOMAIN2_REG PI_BSD_DOM2_LAT_REG + + +#define PI_STATUS_ERROR 0x04 +#define PI_STATUS_IO_BUSY 0x02 +#define PI_STATUS_DMA_BUSY 0x01 + +#define DPC_START (DP_BASE_REG + 0x00) +#define DPC_END (DP_BASE_REG + 0x04) +#define DPC_CURRENT (DP_BASE_REG + 0x08) +#define DPC_STATUS (DP_BASE_REG + 0x0C) +#define DPC_CLOCK (DP_BASE_REG + 0x10) +#define DPC_BUFBUSY (DP_BASE_REG + 0x14) +#define DPC_PIPEBUSY (DP_BASE_REG + 0x18) +#define DPC_TMEM (DP_BASE_REG + 0x1C) + +#define VI_CONTROL (VI_BASE_REG + 0x00) +#define VI_FRAMEBUFFER (VI_BASE_REG + 0x04) +#define VI_WIDTH (VI_BASE_REG + 0x08) +#define VI_V_INT (VI_BASE_REG + 0x0C) +#define VI_CUR_LINE (VI_BASE_REG + 0x10) +#define VI_TIMING (VI_BASE_REG + 0x14) +#define VI_V_SYNC (VI_BASE_REG + 0x18) +#define VI_H_SYNC (VI_BASE_REG + 0x1C) +#define VI_H_SYNC2 (VI_BASE_REG + 0x20) +#define VI_H_LIMITS (VI_BASE_REG + 0x24) +#define VI_COLOR_BURST (VI_BASE_REG + 0x28) +#define VI_H_SCALE (VI_BASE_REG + 0x2C) +#define VI_VSCALE (VI_BASE_REG + 0x30) + +#define PHYS_TO_K0(x) ((u32)(x)|0x80000000) /* physical to kseg0 */ +#define K0_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg0 to physical */ +#define PHYS_TO_K1(x) ((u32)(x)|0xA0000000) /* physical to kseg1 */ +#define K1_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg1 to physical */ + +#define IO_READ(addr) (*(volatile u32*)PHYS_TO_K1(addr)) +#define IO_WRITE(addr,data) (*(volatile u32*)PHYS_TO_K1(addr)=(u32)(data)) + +#define FRAM_EXECUTE_CMD 0xD2000000 +#define FRAM_STATUS_MODE_CMD 0xE1000000 +#define FRAM_ERASE_OFFSET_CMD 0x4B000000 +#define FRAM_WRITE_OFFSET_CMD 0xA5000000 +#define FRAM_ERASE_MODE_CMD 0x78000000 +#define FRAM_WRITE_MODE_CMD 0xB4000000 +#define FRAM_READ_MODE_CMD 0xF0000000 + +#define FRAM_STATUS_REG 0xA8000000 +#define FRAM_COMMAND_REG 0xA8010000 + +int getSRAM( uint8_t *buffer, int size); +int getEeprom( uint8_t *buffer); + +int setSRAM( uint8_t *buffer, int size); +int setEeprom( uint8_t *buffer); + + + + #endif diff --git a/src/flashcart/ed64/types.h b/src/flashcart/ed64/types.h new file mode 100644 index 00000000..625c877b --- /dev/null +++ b/src/flashcart/ed64/types.h @@ -0,0 +1,36 @@ +/* + * File: types.h + * Author: KRIK + * + * Created on 16 ΐοπελό 2011 γ., 2:24 + */ + +#include + +#ifndef _TYPES_H +#define _TYPES_H + +#define u8 unsigned char +#define u16 unsigned short +#define u32 unsigned long +#define u64 unsigned long long + +#define vu8 volatile unsigned char +#define vu16 volatile unsigned short +#define vu32 volatile unsigned long +#define vu64 volatile unsigned long long + +#define s8 signed char +#define s16 short +#define s32 long +#define s64 long long + + +typedef volatile uint64_t sim_vu64; +typedef volatile uint64_t sim_vu64; +typedef unsigned int sim_u32; +typedef uint64_t sim_u64; + + +#endif /* _TYPES_H */ + From ef99dcef606a63987af777073e4c14af9ff016e8 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 14 Oct 2023 21:44:28 -0400 Subject: [PATCH 03/38] bump --- src/flashcart/ed64/ed64.c | 119 ++++++++++++++++++++++++++++------- src/flashcart/ed64/ed64_ll.c | 11 ++-- src/flashcart/ed64/ed64_ll.h | 11 +++- 3 files changed, 111 insertions(+), 30 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index c8d7cce7..261499ef 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -16,21 +16,41 @@ extern int ed_exit(void); static flashcart_err_t ed64_init (void) { + if (file_exists(strip_sd_prefix("/menu/RESET"))) { + f_unlink(strip_sd_prefix("/menu/RESET")); + + FIL fil, lsfil, savfil; + UINT br, lsbr, savbr; + // Older everdrives cant save during gameplay so we need to the reset method. - // TODO: partly already done, see https://github.com/DragonMinded/libdragon/blob/4ec469d26b6dc4e308caf3d5b86c2b340b708bbd/src/libcart/cart.c#L1064 - FIL fil, rsfil; - UINT br; - if (f_open(&rsfil, strip_sd_prefix("/menu/RESET"), FA_READ | FA_OPEN_EXISTING) == FR_OK) { - f_close(&rsfil); - f_unlink(strip_sd_prefix("/menu/RESET")); - int size = KiB(128); - uint8_t cartsave_data[size]; - f_open(&fil, strip_sd_prefix("test.sav"), FA_WRITE | FA_CREATE_NEW); - getSRAM(cartsave_data, size); - f_write(&fil, cartsave_data, size, &br); + // find the path to last save + static char path[50]; + f_open(&lsfil, strip_sd_prefix("/menu/LASTROM"), FA_READ); + f_lseek(&lsfil , SEEK_END); + int path_size = f_tell(&lsfil); + f_lseek((&lsfil), 0); + f_read(&lsfil, (void *)path, path_size, &lsbr); + f_close(&lsfil); + // find the size of the save file + char text_size[50]; + f_open(&savfil, strip_sd_prefix("/menu/LASTSAVE"), FA_READ); + f_lseek(&savfil , SEEK_END); + int save_length = f_tell(&savfil); + f_lseek((&savfil), 0); + f_read(&savfil, (void *)text_size, save_length, &savbr); + int save_size = atoi(text_size); + f_close(&savfil); + uint8_t cartsave_data[save_size]; + f_open(&fil, strip_sd_prefix(path), FA_WRITE | FA_CREATE_ALWAYS); + if (save_size == (512 || KiB(2))){ + getEeprom(cartsave_data, save_size); + f_write(&fil, (void *)cartsave_data, save_size, &br); + } else if (save_size > KiB(2)){ + getSRAM(cartsave_data, save_size); + f_write(&fil, (void *)cartsave_data, save_size, &br); + } f_close(&fil); } - return FLASHCART_OK; } @@ -140,14 +160,30 @@ static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uin } static flashcart_err_t ed64_load_save (char *save_path) { - //ed64_save_type_t type = ed64_ll_get_save_type(); - FIL fil; - UINT br; + ed64_save_type_t type = ed64_ll_get_save_type(); + ed64_save_transfer_mode mode; + switch (type) { + case SAVE_TYPE_EEPROM_4K: + case SAVE_TYPE_EEPROM_16K: + mode = EEPROM_MODE; + break; + case SAVE_TYPE_SRAM: + case SAVE_TYPE_SRAM_128K: + case SAVE_TYPE_FLASHRAM: + mode = SRAM_MODE; + break; + case SAVE_TYPE_NONE: + mode = MEMPAK_MODE; + default: + return FLASHCART_ERR_ARGS; + } - if (f_open(&fil, strip_sd_prefix("test.sav"), FA_READ) == FR_OK) { - - size_t save_size = KiB(128); + if (mode == SRAM_MODE) { + FIL fil, srmfil; + UINT br, srmbr; + size_t save_size = f_size(&fil); uint8_t cartsave_data[save_size]; + if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) == FR_OK) { if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) { f_close(&fil); @@ -159,13 +195,52 @@ static flashcart_err_t ed64_load_save (char *save_path) { } setSRAM(cartsave_data, save_size); + + char sram_buffer[50]; + f_open(&srmfil, strip_sd_prefix("/menu/LASTSAVE"), FA_WRITE | FA_CREATE_ALWAYS); + sprintf(sram_buffer, "%d", save_size); + f_write(&srmfil, (void *)sram_buffer, f_size(&srmfil), &srmbr); + f_close(&srmfil); + } +} else if (mode == EEPROM_MODE){ + FIL epfil, eepfil; + UINT epbr, eepbr; + + if (f_open(&epfil, strip_sd_prefix(save_path), FA_READ) == FR_OK) { + + size_t save_size = f_size(&epfil); + uint8_t cartsave_data[save_size]; + + if (f_read(&epfil, cartsave_data, save_size, &epbr) != FR_OK) { + f_close(&epfil); + return FLASHCART_ERR_LOAD; + } + + if (f_close(&epfil) != FR_OK) { + return FLASHCART_ERR_LOAD; + } + + setEeprom(cartsave_data, save_size); + char eeprom_buffer[50]; + f_open(&eepfil, strip_sd_prefix("/menu/LASTSAVE"), FA_WRITE | FA_CREATE_ALWAYS); + sprintf(eeprom_buffer, "%d", save_size); + f_write(&eepfil, (void *)eeprom_buffer, f_size(&eepfil), &eepbr); + f_close(&eepfil); + } } - FIL rsfil; - UINT rsbr; - TCHAR byte[1]; + FIL rsfil, lrfil; + UINT rsbr, lrbr; + TCHAR reset_byte[1]; f_open(&rsfil, strip_sd_prefix("/menu/RESET"), FA_WRITE | FA_CREATE_ALWAYS); - f_write(&rsfil, (void *)byte, 1, &rsbr); + f_write(&rsfil, (void *)reset_byte, 1, &rsbr); f_close(&rsfil); + + + char tmp[50]; + f_open(&lrfil, strip_sd_prefix("/menu/LASTROM"), FA_WRITE | FA_CREATE_ALWAYS); + strcpy(tmp, strip_sd_prefix(save_path)); + f_write(&lrfil, (void *)tmp, f_size(&lrfil), &lrbr); + f_close(&lrfil); return FLASHCART_OK; } diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index fa9b6328..a897299b 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -3,6 +3,7 @@ #include "ed64_ll.h" + /* ED64 configuration registers base address */ #define ED64_CONFIG_REGS_BASE (0xA8040000) @@ -239,8 +240,8 @@ int getSRAM( uint8_t *buffer, int size){ return 1; } -int getEeprom( uint8_t *buffer){ - int blocks=(16*1024)/8; +int getEeprom( uint8_t *buffer, int size){ + int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_read( b, &buffer[b * 8] ); } @@ -252,7 +253,7 @@ int getEeprom( uint8_t *buffer){ /* sram upload */ -int setSRAM( uint8_t *buffer,int size){ +int setSRAM( uint8_t *buffer, int size){ //half working PI_DMAWait(); //Timing @@ -275,8 +276,8 @@ int setSRAM( uint8_t *buffer,int size){ } -int setEeprom(uint8_t *buffer){ - int blocks=(16*1024)/8; +int setEeprom(uint8_t *buffer, int size){ + int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_write( b, &buffer[b * 8] ); } diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 30bc032d..ffa17736 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -30,8 +30,13 @@ typedef enum { SAVE_TYPE_DD64 = 16, } ed64_save_type_t; +typedef enum { + MEMPAK_MODE = 0, + EEPROM_MODE = 1, + SRAM_MODE = 2, +} ed64_save_transfer_mode; + -#define SRAM_ADDRESS (0x1FFE2000) #define ROM_ADDRESS (0xB0000000) /* Save functions */ @@ -179,10 +184,10 @@ void *safe_memcpy(void *dest, const void *src, size_t n); #define FRAM_COMMAND_REG 0xA8010000 int getSRAM( uint8_t *buffer, int size); -int getEeprom( uint8_t *buffer); +int getEeprom( uint8_t *buffer, int size); int setSRAM( uint8_t *buffer, int size); -int setEeprom( uint8_t *buffer); +int setEeprom( uint8_t *buffer, int size); From 303651309448add61cf54d4aba4b5c87277451e3 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sun, 15 Oct 2023 09:29:39 -0400 Subject: [PATCH 04/38] fix warnings --- src/flashcart/ed64/ed64.c | 4 +--- src/flashcart/ed64/ed64_ll.c | 10 +++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index aaff0260..4e235b5d 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -37,13 +37,11 @@ static flashcart_err_t ed64_init (void) { FIL lrp_fil; UINT lrp_br; TCHAR lrp_path[1024]; - lrp_path[0] = '\0'; - if (f_open(&lrp_fil, LAST_SAVE_FILE_PATH, FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } - if (f_read(&lrp_fil, lrp_path, 1024, &lrp_br) != FR_OK) { + if (f_read(&lrp_fil, lrp_path, sizeof(&lrp_fil) + 1, &lrp_br) != FR_OK) { f_close(&lrp_fil); return FLASHCART_ERR_LOAD; } diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index a897299b..b8c27780 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -215,22 +215,22 @@ void PI_SafeDMAFromCart(void *dest, void *src, u32 size) { #include "types.h" int getSRAM( uint8_t *buffer, int size){ - while (dma_busy()) ; + dma_wait(); IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); - while (dma_busy()) ; + dma_wait(); PI_Init(); - while (dma_busy()) ; + dma_wait(); PI_DMAFromSRAM(buffer, 0, size) ; - while (dma_busy()) ; + dma_wait(); IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); @@ -263,7 +263,7 @@ int setSRAM( uint8_t *buffer, int size){ PI_Init(); data_cache_hit_writeback_invalidate(buffer,size); - while (dma_busy()); + dma_wait(); PI_DMAToSRAM(buffer, 0, size); data_cache_hit_writeback_invalidate(buffer,size); From a2b300360587ddd073e763a5bc5083986ad90622 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sun, 15 Oct 2023 09:52:56 -0400 Subject: [PATCH 05/38] account for games that dont save --- src/flashcart/ed64/ed64.c | 73 +++++++++++++++------------------------ 1 file changed, 28 insertions(+), 45 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 4e235b5d..55249e92 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -29,10 +29,9 @@ int read_line(FILE *in, char *buffer, size_t max) static flashcart_err_t ed64_init (void) { - if (file_exists(strip_sd_prefix("/menu/RESET"))) { + if (file_exists(strip_sd_prefix("/menu/RESET"))) { // checks if reset f_unlink(strip_sd_prefix("/menu/RESET")); - if (file_exists(LAST_SAVE_FILE_PATH)) { FIL lrp_fil; UINT lrp_br; @@ -59,7 +58,7 @@ static flashcart_err_t ed64_init (void) { // find the path to last save uint8_t cartsave_data[KiB(128)]; - + if (file_exists(strip_sd_prefix(lrp_path))){ if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -67,9 +66,12 @@ static flashcart_err_t ed64_init (void) { f_lseek(&fil , SEEK_END); int save_size = f_tell(&fil); f_lseek(&fil, 0); + + // everdrive doesnt care about the save type other than eeprom + // so we can just check the size if (save_size >= KiB(32)) { getSRAM(cartsave_data, save_size); - } else { + } else if (save_size >= 512){ getEeprom(cartsave_data, save_size); } @@ -80,7 +82,7 @@ static flashcart_err_t ed64_init (void) { if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } -} + } } return FLASHCART_OK; } @@ -207,39 +209,7 @@ static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uin } static flashcart_err_t ed64_load_save (char *save_path) { - FIL lsp_fil; - UINT lsp_bw ; - if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - 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; - } - if (f_close(&lsp_fil) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - - ed64_save_type_t type = ed64_ll_get_save_type(); - ed64_save_transfer_mode mode; - switch (type) { - case SAVE_TYPE_EEPROM_4K: - case SAVE_TYPE_EEPROM_16K: - mode = EEPROM_MODE; - break; - case SAVE_TYPE_SRAM: - case SAVE_TYPE_SRAM_128K: - case SAVE_TYPE_FLASHRAM: - mode = SRAM_MODE; - break; - case SAVE_TYPE_NONE: - mode = MEMPAK_MODE; - break; - default: - return FLASHCART_ERR_ARGS; - break; - } - +if (file_exists(strip_sd_prefix(save_path))){ // if file doesnt exist do nothing FIL fil; UINT br; if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) { @@ -257,12 +227,25 @@ static flashcart_err_t ed64_load_save (char *save_path) { if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } - -if (mode == SRAM_MODE) { - setSRAM(cartsave_data, save_size); -} else if (mode == EEPROM_MODE){ - setEeprom(cartsave_data, save_size); -} + // everdrive doesnt care about the save type other than eeprom + // so we can just check the size + if (save_size >= KiB(32)) { //sram and flash + setSRAM(cartsave_data, save_size); + } else if (save_size >= 512){ // eeprom + setEeprom(cartsave_data, save_size); + } + FIL lsp_fil; + UINT lsp_bw ; + if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { + return FLASHCART_ERR_LOAD; + } + 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; + } + if (f_close(&lsp_fil) != FR_OK) { + return FLASHCART_ERR_LOAD; + } FIL rsfil; UINT rsbr; TCHAR reset_byte[1]; @@ -270,7 +253,7 @@ if (mode == SRAM_MODE) { f_open(&rsfil, "/menu/RESET",FA_CREATE_ALWAYS); f_write(&rsfil, (void *)reset_byte, 1, &rsbr); f_close(&rsfil); - +} return FLASHCART_OK; } From 1505c1ebdf432ca52144c03b05c736c1b3fa3f6b Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sun, 15 Oct 2023 17:54:17 -0400 Subject: [PATCH 06/38] working but hardcodded --- src/flashcart/ed64/ed64.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 55249e92..4935a981 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -22,11 +22,6 @@ extern int ed_exit(void); -int read_line(FILE *in, char *buffer, size_t max) -{ - return fgets(buffer, max, in) == buffer; -} - static flashcart_err_t ed64_init (void) { if (file_exists(strip_sd_prefix("/menu/RESET"))) { // checks if reset @@ -39,12 +34,12 @@ static flashcart_err_t ed64_init (void) { if (f_open(&lrp_fil, LAST_SAVE_FILE_PATH, FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } - - if (f_read(&lrp_fil, lrp_path, sizeof(&lrp_fil) + 1, &lrp_br) != FR_OK) { + int test = f_size(&lrp_fil); + if (f_read(&lrp_fil, lrp_path, test, &lrp_br) != FR_OK) { f_close(&lrp_fil); return FLASHCART_ERR_LOAD; } - + if (f_close(&lrp_fil) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -57,15 +52,12 @@ static flashcart_err_t ed64_init (void) { // find the path to last save - uint8_t cartsave_data[KiB(128)]; - if (file_exists(strip_sd_prefix(lrp_path))){ if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) { return FLASHCART_ERR_LOAD; } - f_lseek(&fil , SEEK_END); - int save_size = f_tell(&fil); - f_lseek(&fil, 0); + int save_size = KiB(32); + uint8_t cartsave_data[save_size]; // everdrive doesnt care about the save type other than eeprom // so we can just check the size @@ -83,7 +75,6 @@ static flashcart_err_t ed64_init (void) { return FLASHCART_ERR_LOAD; } } -} return FLASHCART_OK; } @@ -209,7 +200,6 @@ static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uin } static flashcart_err_t ed64_load_save (char *save_path) { -if (file_exists(strip_sd_prefix(save_path))){ // if file doesnt exist do nothing FIL fil; UINT br; if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) { @@ -232,10 +222,11 @@ if (file_exists(strip_sd_prefix(save_path))){ // if file doesnt exist do nothing if (save_size >= KiB(32)) { //sram and flash setSRAM(cartsave_data, save_size); } else if (save_size >= 512){ // eeprom - setEeprom(cartsave_data, save_size); + setEeprom(cartsave_data, save_size); } FIL lsp_fil; UINT lsp_bw ; + f_unlink(LAST_SAVE_FILE_PATH); if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -253,7 +244,6 @@ if (file_exists(strip_sd_prefix(save_path))){ // if file doesnt exist do nothing f_open(&rsfil, "/menu/RESET",FA_CREATE_ALWAYS); f_write(&rsfil, (void *)reset_byte, 1, &rsbr); f_close(&rsfil); -} return FLASHCART_OK; } From df3baa31101ebb7e555a7f8400516b7eb8312594 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sun, 15 Oct 2023 18:13:04 -0400 Subject: [PATCH 07/38] working but needs testing --- src/flashcart/ed64/ed64.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 4935a981..05228ad3 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -31,11 +31,14 @@ static flashcart_err_t ed64_init (void) { FIL lrp_fil; UINT lrp_br; TCHAR lrp_path[1024]; + // FIXME: use max supported length- if (f_open(&lrp_fil, LAST_SAVE_FILE_PATH, FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } - int test = f_size(&lrp_fil); - if (f_read(&lrp_fil, lrp_path, test, &lrp_br) != FR_OK) { + + int lrp_size = f_size(&lrp_fil); + + if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) { f_close(&lrp_fil); return FLASHCART_ERR_LOAD; } @@ -45,25 +48,25 @@ static flashcart_err_t ed64_init (void) { } // Now save the content back to the SD! - + FIL fil; 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 - if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) { + 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; } - int save_size = KiB(32); - uint8_t cartsave_data[save_size]; - // everdrive doesnt care about the save type other than eeprom // so we can just check the size - if (save_size >= KiB(32)) { + if (save_size > KiB(2)) { getSRAM(cartsave_data, save_size); - } else if (save_size >= 512){ + } else if (save_size > 0){ getEeprom(cartsave_data, save_size); } From 01b802ea4105ffe715c6b3767d7fc64ff7ca758d Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sun, 15 Oct 2023 19:25:51 -0400 Subject: [PATCH 08/38] fully working --- src/flashcart/ed64/ed64.c | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 05228ad3..10150b93 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -30,8 +30,10 @@ static flashcart_err_t ed64_init (void) { FIL lrp_fil; UINT lrp_br; - TCHAR lrp_path[1024]; - // FIXME: use max supported length- + TCHAR lrp_path[255*32]; + + // FIXME: use max supported length + if (f_open(&lrp_fil, LAST_SAVE_FILE_PATH, FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -55,7 +57,8 @@ static flashcart_err_t ed64_init (void) { // Older everdrives cant save during gameplay so we need to the reset method. // 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) { @@ -66,7 +69,7 @@ static flashcart_err_t ed64_init (void) { // so we can just check the size if (save_size > KiB(2)) { getSRAM(cartsave_data, save_size); - } else if (save_size > 0){ + } else { getEeprom(cartsave_data, save_size); } @@ -78,6 +81,7 @@ static flashcart_err_t ed64_init (void) { return FLASHCART_ERR_LOAD; } } +} return FLASHCART_OK; } @@ -139,26 +143,6 @@ static flashcart_err_t ed64_load_rom (char *rom_path, flashcart_progress_callbac return FLASHCART_ERR_LOAD; } - - // Set the required actions for retriving the save file later. - // Given there is no good place in RAM... - // This would involve creating some content to a file on the SD card that includes: - // the ROM name and location and possibly its save type. This information will be used on init to perform a "save writeback". - // Actually, we should be using the settings API, so this is just a trial hack. - // FIL lrp_fil; - // UINT lrp_bw; - - // if (f_open(&lrp_fil, LAST_ROM_FILE_PATH, FA_CREATE_ALWAYS) != FR_OK) { - // return FLASHCART_ERR_LOAD; - // } - // if (f_write(&lrp_fil, rom_path, strlen(rom_path) + 1, &lrp_bw) != FR_OK) { - // f_close(&lrp_fil); - // return FLASHCART_ERR_LOAD; - // } - // if (f_close(&lrp_fil) != FR_OK) { - // return FLASHCART_ERR_LOAD; - // } - return FLASHCART_OK; } From 346371afa556a47f09951d70992e459bc1900291 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sun, 15 Oct 2023 20:48:30 -0400 Subject: [PATCH 09/38] cleaned up code slightly --- src/flashcart/ed64/ed64.c | 47 ++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 10150b93..9b1bafa9 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -67,6 +67,7 @@ static flashcart_err_t ed64_init (void) { // everdrive doesnt care about the save type other than eeprom // so we can just check the size + if (save_size > KiB(2)) { getSRAM(cartsave_data, save_size); } else { @@ -79,8 +80,8 @@ static flashcart_err_t ed64_init (void) { if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; + } } - } } return FLASHCART_OK; } @@ -160,6 +161,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. + if (file_size > (MiB(64) - rom_offset)) { f_close(&fil); return FLASHCART_ERR_ARGS; @@ -189,10 +191,12 @@ static flashcart_err_t ed64_load_file (char *file_path, uint32_t rom_offset, uin static flashcart_err_t ed64_load_save (char *save_path) { FIL fil; UINT br; + if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) { f_close(&fil); return FLASHCART_ERR_LOAD; } + size_t save_size = f_size(&fil); uint8_t cartsave_data[save_size]; @@ -204,33 +208,54 @@ static flashcart_err_t ed64_load_save (char *save_path) { if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } + // everdrive doesnt care about the save type other than eeprom // so we can just check the size + if (save_size >= KiB(32)) { //sram and flash setSRAM(cartsave_data, save_size); } else if (save_size >= 512){ // eeprom setEeprom(cartsave_data, save_size); } + FIL lsp_fil; - UINT lsp_bw ; + UINT lsp_bw; + + // probably not nessacery f_unlink(LAST_SAVE_FILE_PATH); - if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { + + if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { 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; - } - if (f_close(&lsp_fil) != FR_OK) { + } + + if (f_close(&lsp_fil) != FR_OK) { return FLASHCART_ERR_LOAD; - } + } + FIL rsfil; UINT rsbr; TCHAR reset_byte[1]; - f_open(&rsfil, "/menu/RESET",FA_CREATE_ALWAYS); - f_write(&rsfil, (void *)reset_byte, 1, &rsbr); - f_close(&rsfil); + // this wries a 1 byte file as it only needs to exist to detect a reset + + if (f_open(&rsfil, "/menu/RESET",FA_CREATE_ALWAYS) != FR_OK) { + f_close(&rsfil); + return FLASHCART_ERR_LOAD; + } + + if (f_write(&rsfil, (void *)reset_byte, 1, &rsbr) != FR_OK) { + f_close(&rsfil); + return FLASHCART_ERR_LOAD; + } + + if (f_close(&rsfil) != FR_OK) { + return FLASHCART_OK; + } + return FLASHCART_OK; } From 64762bbcf7fafbe24431f8eee7bca53f87f4e463 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Mon, 16 Oct 2023 20:50:09 -0400 Subject: [PATCH 10/38] remove types.h --- src/flashcart/ed64/ed64_ll.c | 23 ++++++++++------------- src/flashcart/ed64/ed64_ll.h | 29 ++++++++++++++--------------- src/flashcart/ed64/types.h | 36 ------------------------------------ 3 files changed, 24 insertions(+), 64 deletions(-) delete mode 100644 src/flashcart/ed64/types.h diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index b8c27780..19e42e75 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -120,8 +120,6 @@ void ed64_ll_set_sram_bank(uint8_t bank) { #include #include #include -#include "types.h" - void PI_Init(void) { PI_DMAWait(); @@ -144,7 +142,7 @@ void PI_DMAWait(void) { } -void PI_DMAFromSRAM(void *dest, u32 offset, u32 size) { +void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); @@ -156,7 +154,7 @@ void PI_DMAFromSRAM(void *dest, u32 offset, u32 size) { } -void PI_DMAToSRAM(void *src, u32 offset, u32 size) { //void* +void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* PI_DMAWait(); IO_WRITE(PI_STATUS_REG, 2); @@ -165,7 +163,7 @@ void PI_DMAToSRAM(void *src, u32 offset, u32 size) { //void* IO_WRITE(PI_RD_LEN_REG, (size - 1)); } -void PI_DMAFromCart(void* dest, void* src, u32 size) { +void PI_DMAFromCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); IO_WRITE(PI_STATUS_REG, 0x03); @@ -175,7 +173,7 @@ void PI_DMAFromCart(void* dest, void* src, u32 size) { } -void PI_DMAToCart(void* dest, void* src, u32 size) { +void PI_DMAToCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); IO_WRITE(PI_STATUS_REG, 0x02); @@ -186,11 +184,11 @@ void PI_DMAToCart(void* dest, void* src, u32 size) { // Wrapper to support unaligned access to memory -void PI_SafeDMAFromCart(void *dest, void *src, u32 size) { +void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { if (!dest || !src || !size) return; - u32 unalignedSrc = ((u32)src) % 2; - u32 unalignedDest = ((u32)dest) % 8; + unsigned long unalignedSrc = ((unsigned long)src) % 2; + unsigned long unalignedDest = ((unsigned long)dest) % 8; //FIXME: Do i really need to check if size is 16bit aligned? if (!unalignedDest && !unalignedSrc && !(size % 2)) { @@ -200,10 +198,10 @@ void PI_SafeDMAFromCart(void *dest, void *src, u32 size) { return; } - void* newSrc = (void*)(((u32)src) - unalignedSrc); - u32 newSize = (size + unalignedSrc) + ((size + unalignedSrc) % 2); + void* newSrc = (void*)(((unsigned long)src) - unalignedSrc); + unsigned long newSize = (size + unalignedSrc) + ((size + unalignedSrc) % 2); - u8 *buffer = memalign(8, newSize); + unsigned char *buffer = memalign(8, newSize); PI_DMAFromCart(buffer, newSrc, newSize); PI_DMAWait(); @@ -212,7 +210,6 @@ void PI_SafeDMAFromCart(void *dest, void *src, u32 size) { free(buffer); } -#include "types.h" int getSRAM( uint8_t *buffer, int size){ dma_wait(); diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index ffa17736..f975630c 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -11,7 +11,6 @@ #include #include #include -#include "types.h" /** * @addtogroup ed64 @@ -51,22 +50,22 @@ void ed64_ll_set_save_type(ed64_save_type_t type); void data_cache_hit_writeback_invalidate(volatile void *, unsigned long); unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len); -void dma_write_sram(void* src, u32 offset, u32 size); -void dma_read_sram(void *dest, u32 offset, u32 size); +void dma_write_sram(void* src, unsigned long offset, unsigned long size); +void dma_read_sram(void *dest, unsigned long offset, unsigned long size); void dma_write_s(void * ram_address, unsigned long pi_address, unsigned long len); void dma_read_s(void * ram_address, unsigned long pi_address, unsigned long len); -int writeSram(void* src, u32 size); +int writeSram(void* src, unsigned long size); void setSDTiming(void); void PI_Init(void); void PI_Init_SRAM(void); void PI_DMAWait(void); -void PI_DMAFromCart(void* dest, void* src, u32 size); -void PI_DMAToCart(void* dest, void* src, u32 size); -void PI_DMAFromSRAM(void *dest, u32 offset, u32 size); -void PI_DMAToSRAM(void* src, u32 offset, u32 size); -void PI_SafeDMAFromCart(void *dest, void *src, u32 size); +void PI_DMAFromCart(void* dest, void* src, unsigned long size); +void PI_DMAToCart(void* dest, void* src, unsigned long size); +void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size); +void PI_DMAToSRAM(void* src, unsigned long offset, unsigned long size); +void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size); //memory /*** MEMORY ***/ @@ -164,13 +163,13 @@ void *safe_memcpy(void *dest, const void *src, size_t n); #define VI_H_SCALE (VI_BASE_REG + 0x2C) #define VI_VSCALE (VI_BASE_REG + 0x30) -#define PHYS_TO_K0(x) ((u32)(x)|0x80000000) /* physical to kseg0 */ -#define K0_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg0 to physical */ -#define PHYS_TO_K1(x) ((u32)(x)|0xA0000000) /* physical to kseg1 */ -#define K1_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg1 to physical */ +#define PHYS_TO_K0(x) ((unsigned long)(x)|0x80000000) /* physical to kseg0 */ +#define K0_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg0 to physical */ +#define PHYS_TO_K1(x) ((unsigned long)(x)|0xA0000000) /* physical to kseg1 */ +#define K1_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg1 to physical */ -#define IO_READ(addr) (*(volatile u32*)PHYS_TO_K1(addr)) -#define IO_WRITE(addr,data) (*(volatile u32*)PHYS_TO_K1(addr)=(u32)(data)) +#define IO_READ(addr) (*(volatile unsigned long*)PHYS_TO_K1(addr)) +#define IO_WRITE(addr,data) (*(volatile unsigned long*)PHYS_TO_K1(addr)=(unsigned long)(data)) #define FRAM_EXECUTE_CMD 0xD2000000 #define FRAM_STATUS_MODE_CMD 0xE1000000 diff --git a/src/flashcart/ed64/types.h b/src/flashcart/ed64/types.h deleted file mode 100644 index 625c877b..00000000 --- a/src/flashcart/ed64/types.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * File: types.h - * Author: KRIK - * - * Created on 16 ΐοπελό 2011 γ., 2:24 - */ - -#include - -#ifndef _TYPES_H -#define _TYPES_H - -#define u8 unsigned char -#define u16 unsigned short -#define u32 unsigned long -#define u64 unsigned long long - -#define vu8 volatile unsigned char -#define vu16 volatile unsigned short -#define vu32 volatile unsigned long -#define vu64 volatile unsigned long long - -#define s8 signed char -#define s16 short -#define s32 long -#define s64 long long - - -typedef volatile uint64_t sim_vu64; -typedef volatile uint64_t sim_vu64; -typedef unsigned int sim_u32; -typedef uint64_t sim_u64; - - -#endif /* _TYPES_H */ - From dfc0a920133ef3c8098781cfd50fc36c6510fb50 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Mon, 16 Oct 2023 22:16:00 -0400 Subject: [PATCH 11/38] remove bloat --- src/flashcart/ed64/ed64_ll.c | 96 +++++++++++----------- src/flashcart/ed64/ed64_ll.h | 155 ++++++----------------------------- 2 files changed, 72 insertions(+), 179 deletions(-) diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 19e42e75..7badaf5e 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -1,5 +1,5 @@ +#include #include - #include "ed64_ll.h" @@ -117,38 +117,34 @@ void ed64_ll_set_sram_bank(uint8_t bank) { } -#include -#include -#include - void PI_Init(void) { PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 0x03); + io_write(PI_STATUS_REG, 0x03); } // Inits PI for sram transfer void PI_Init_SRAM(void) { - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); + io_write(PI_BSD_DOM2_LAT_REG, 0x05); + io_write(PI_BSD_DOM2_PWD_REG, 0x0C); + io_write(PI_BSD_DOM2_PGS_REG, 0x0D); + io_write(PI_BSD_DOM2_RLS_REG, 0x02); } void PI_DMAWait(void) { - while (IO_READ(PI_STATUS_REG) & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)); + while (io_read(PI_STATUS_REG) & (SP_STATUS_IO_BUSY | SP_STATUS_DMA_BUSY)); } void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); - IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); asm volatile ("" : : : "memory"); - IO_WRITE(PI_WR_LEN_REG, (size - 1)); + io_write(PI_WR_LEN_REG, (size - 1)); asm volatile ("" : : : "memory"); } @@ -157,29 +153,29 @@ void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 2); - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); - IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); - IO_WRITE(PI_RD_LEN_REG, (size - 1)); + io_write(PI_STATUS_REG, 2); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); + io_write(PI_RD_LEN_REG, (size - 1)); } void PI_DMAFromCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 0x03); - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); - IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(src)); - IO_WRITE(PI_WR_LEN_REG, (size - 1)); + io_write(PI_STATUS_REG, 0x03); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + io_write(PI_CART_ADDR_REG, K0_TO_PHYS(src)); + io_write(PI_WR_LEN_REG, (size - 1)); } void PI_DMAToCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 0x02); - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); - IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); - IO_WRITE(PI_RD_LEN_REG, (size - 1)); + io_write(PI_STATUS_REG, 0x02); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + io_write(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); + io_write(PI_RD_LEN_REG, (size - 1)); } @@ -214,10 +210,10 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { int getSRAM( uint8_t *buffer, int size){ dma_wait(); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); + io_write(PI_BSD_DOM2_LAT_REG, 0x05); + io_write(PI_BSD_DOM2_PWD_REG, 0x0C); + io_write(PI_BSD_DOM2_PGS_REG, 0x0D); + io_write(PI_BSD_DOM2_RLS_REG, 0x02); dma_wait(); @@ -229,10 +225,10 @@ int getSRAM( uint8_t *buffer, int size){ dma_wait(); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); return 1; } @@ -285,27 +281,27 @@ int setEeprom(uint8_t *buffer, int size){ void setSDTiming(void){ // PI_DMAWait(); - IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); + io_write(PI_BSD_DOM1_LAT_REG, 0x40); + io_write(PI_BSD_DOM1_PWD_REG, 0x12); + io_write(PI_BSD_DOM1_PGS_REG, 0x07); + io_write(PI_BSD_DOM1_RLS_REG, 0x03); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); } void restoreTiming(void) { //n64 timing restore :> - IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); + io_write(PI_BSD_DOM1_LAT_REG, 0x40); + io_write(PI_BSD_DOM1_PWD_REG, 0x12); + io_write(PI_BSD_DOM1_PGS_REG, 0x07); + io_write(PI_BSD_DOM1_RLS_REG, 0x03); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); } diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index f975630c..9b9289f0 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -12,6 +12,32 @@ #include #include +// FIXME: redefined because its in a .c instead of a .h +#define PI_BASE_REG 0x04600000 + +#define PIF_RAM_START 0x1FC007C0 + +#define PI_STATUS_REG (PI_BASE_REG+0x10) +#define PI_DRAM_ADDR_REG (PI_BASE_REG+0x00) /* DRAM address */ +#define PI_CART_ADDR_REG (PI_BASE_REG+0x04) +#define PI_RD_LEN_REG (PI_BASE_REG+0x08) +#define PI_WR_LEN_REG (PI_BASE_REG+0x0C) + +// FIXME: redefined because its in a .c instead of a .h +#define PI_BSD_DOM1_LAT_REG (PI_BASE_REG+0x14) +#define PI_BSD_DOM1_PWD_REG (PI_BASE_REG+0x18) +#define PI_BSD_DOM1_PGS_REG (PI_BASE_REG+0x1C) +#define PI_BSD_DOM1_RLS_REG (PI_BASE_REG+0x20) +#define PI_BSD_DOM2_LAT_REG (PI_BASE_REG+0x24) +#define PI_BSD_DOM2_PWD_REG (PI_BASE_REG+0x28) +#define PI_BSD_DOM2_PGS_REG (PI_BASE_REG+0x2C) +#define PI_BSD_DOM2_RLS_REG (PI_BASE_REG+0x30) + +#define K0_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg0 to physical */ +#define K1_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg1 to physical */ + + + /** * @addtogroup ed64 * @{ @@ -29,13 +55,6 @@ typedef enum { SAVE_TYPE_DD64 = 16, } ed64_save_type_t; -typedef enum { - MEMPAK_MODE = 0, - EEPROM_MODE = 1, - SRAM_MODE = 2, -} ed64_save_transfer_mode; - - #define ROM_ADDRESS (0xB0000000) /* Save functions */ @@ -45,9 +64,6 @@ void ed64_ll_set_save_type(ed64_save_type_t type); /** @} */ /* ed64 */ - -//sram.h - void data_cache_hit_writeback_invalidate(volatile void *, unsigned long); unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len); void dma_write_sram(void* src, unsigned long offset, unsigned long size); @@ -66,129 +82,10 @@ void PI_DMAToCart(void* dest, void* src, unsigned long size); void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size); void PI_DMAToSRAM(void* src, unsigned long offset, unsigned long size); void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size); - -//memory -/*** MEMORY ***/ -void *safe_memalign(size_t boundary, size_t size); -void *safe_calloc(size_t nmemb, size_t size); -void *safe_malloc(size_t size); -void safe_free(void *ptr); -void *safe_memset(void *s, int c, size_t n); -void *safe_memcpy(void *dest, const void *src, size_t n); - - -#define DP_BASE_REG 0x04100000 -#define VI_BASE_REG 0x04400000 -#define PI_BASE_REG 0x04600000 -#define PIF_RAM_START 0x1FC007C0 - - -/* - * PI status register has 3 bits active when read from (PI_STATUS_REG - read) - * Bit 0: DMA busy - set when DMA is in progress - * Bit 1: IO busy - set when IO is in progress - * Bit 2: Error - set when CPU issues IO request while DMA is busy - */ - -#define PI_STATUS_REG (PI_BASE_REG+0x10) - -/* PI DRAM address (R/W): starting RDRAM address */ -#define PI_DRAM_ADDR_REG (PI_BASE_REG+0x00) /* DRAM address */ - -/* PI pbus (cartridge) address (R/W): starting AD16 address */ -#define PI_CART_ADDR_REG (PI_BASE_REG+0x04) - -/* PI read length (R/W): read data length */ -#define PI_RD_LEN_REG (PI_BASE_REG+0x08) - -/* PI write length (R/W): write data length */ -#define PI_WR_LEN_REG (PI_BASE_REG+0x0C) - -/* - * PI status (R): [0] DMA busy, [1] IO busy, [2], error - * (W): [0] reset controller (and abort current op), [1] clear intr - */ - -#define PI_BSD_DOM1_LAT_REG (PI_BASE_REG+0x14) - -/* PI dom1 pulse width (R/W): [7:0] domain 1 device R/W strobe pulse width */ -#define PI_BSD_DOM1_PWD_REG (PI_BASE_REG+0x18) - -/* PI dom1 page size (R/W): [3:0] domain 1 device page size */ -#define PI_BSD_DOM1_PGS_REG (PI_BASE_REG+0x1C) /* page size */ - -/* PI dom1 release (R/W): [1:0] domain 1 device R/W release duration */ -#define PI_BSD_DOM1_RLS_REG (PI_BASE_REG+0x20) -/* PI dom2 latency (R/W): [7:0] domain 2 device latency */ -#define PI_BSD_DOM2_LAT_REG (PI_BASE_REG+0x24) /* Domain 2 latency */ - -/* PI dom2 pulse width (R/W): [7:0] domain 2 device R/W strobe pulse width */ -#define PI_BSD_DOM2_PWD_REG (PI_BASE_REG+0x28) /* pulse width */ - -/* PI dom2 page size (R/W): [3:0] domain 2 device page size */ -#define PI_BSD_DOM2_PGS_REG (PI_BASE_REG+0x2C) /* page size */ - -/* PI dom2 release (R/W): [1:0] domain 2 device R/W release duration */ -#define PI_BSD_DOM2_RLS_REG (PI_BASE_REG+0x30) /* release duration */ - - -#define PI_DOMAIN1_REG PI_BSD_DOM1_LAT_REG -#define PI_DOMAIN2_REG PI_BSD_DOM2_LAT_REG - - -#define PI_STATUS_ERROR 0x04 -#define PI_STATUS_IO_BUSY 0x02 -#define PI_STATUS_DMA_BUSY 0x01 - -#define DPC_START (DP_BASE_REG + 0x00) -#define DPC_END (DP_BASE_REG + 0x04) -#define DPC_CURRENT (DP_BASE_REG + 0x08) -#define DPC_STATUS (DP_BASE_REG + 0x0C) -#define DPC_CLOCK (DP_BASE_REG + 0x10) -#define DPC_BUFBUSY (DP_BASE_REG + 0x14) -#define DPC_PIPEBUSY (DP_BASE_REG + 0x18) -#define DPC_TMEM (DP_BASE_REG + 0x1C) - -#define VI_CONTROL (VI_BASE_REG + 0x00) -#define VI_FRAMEBUFFER (VI_BASE_REG + 0x04) -#define VI_WIDTH (VI_BASE_REG + 0x08) -#define VI_V_INT (VI_BASE_REG + 0x0C) -#define VI_CUR_LINE (VI_BASE_REG + 0x10) -#define VI_TIMING (VI_BASE_REG + 0x14) -#define VI_V_SYNC (VI_BASE_REG + 0x18) -#define VI_H_SYNC (VI_BASE_REG + 0x1C) -#define VI_H_SYNC2 (VI_BASE_REG + 0x20) -#define VI_H_LIMITS (VI_BASE_REG + 0x24) -#define VI_COLOR_BURST (VI_BASE_REG + 0x28) -#define VI_H_SCALE (VI_BASE_REG + 0x2C) -#define VI_VSCALE (VI_BASE_REG + 0x30) - -#define PHYS_TO_K0(x) ((unsigned long)(x)|0x80000000) /* physical to kseg0 */ -#define K0_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg0 to physical */ -#define PHYS_TO_K1(x) ((unsigned long)(x)|0xA0000000) /* physical to kseg1 */ -#define K1_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg1 to physical */ - -#define IO_READ(addr) (*(volatile unsigned long*)PHYS_TO_K1(addr)) -#define IO_WRITE(addr,data) (*(volatile unsigned long*)PHYS_TO_K1(addr)=(unsigned long)(data)) - -#define FRAM_EXECUTE_CMD 0xD2000000 -#define FRAM_STATUS_MODE_CMD 0xE1000000 -#define FRAM_ERASE_OFFSET_CMD 0x4B000000 -#define FRAM_WRITE_OFFSET_CMD 0xA5000000 -#define FRAM_ERASE_MODE_CMD 0x78000000 -#define FRAM_WRITE_MODE_CMD 0xB4000000 -#define FRAM_READ_MODE_CMD 0xF0000000 - -#define FRAM_STATUS_REG 0xA8000000 -#define FRAM_COMMAND_REG 0xA8010000 - int getSRAM( uint8_t *buffer, int size); int getEeprom( uint8_t *buffer, int size); - int setSRAM( uint8_t *buffer, int size); int setEeprom( uint8_t *buffer, int size); - - #endif From 075a204a8281ef7a7f14b1412004fb8ef88fe872 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Mon, 16 Oct 2023 23:09:48 -0400 Subject: [PATCH 12/38] fix --- src/flashcart/ed64/ed64.c | 22 +++------ src/flashcart/ed64/ed64_ll.c | 91 ++++++++++++++++++------------------ src/flashcart/ed64/ed64_ll.h | 8 ++++ 3 files changed, 61 insertions(+), 60 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 9b1bafa9..14d9c8b8 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -65,7 +65,7 @@ static flashcart_err_t ed64_init (void) { return FLASHCART_ERR_LOAD; } - // everdrive doesnt care about the save type other than eeprom + // everdrive doesn't care about the save type other than eeprom // so we can just check the size if (save_size > KiB(2)) { @@ -197,7 +197,7 @@ static flashcart_err_t ed64_load_save (char *save_path) { return FLASHCART_ERR_LOAD; } - size_t save_size = f_size(&fil); + size_t save_size = file_get_size(strip_sd_prefix(save_path)); uint8_t cartsave_data[save_size]; if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) { @@ -214,16 +214,15 @@ static flashcart_err_t ed64_load_save (char *save_path) { if (save_size >= KiB(32)) { //sram and flash setSRAM(cartsave_data, save_size); - } else if (save_size >= 512){ // eeprom - setEeprom(cartsave_data, save_size); + } + + else if (save_size >= 512){ // eeprom + setEeprom(cartsave_data, save_size); } FIL lsp_fil; UINT lsp_bw; - // probably not nessacery - f_unlink(LAST_SAVE_FILE_PATH); - if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -237,20 +236,13 @@ static flashcart_err_t ed64_load_save (char *save_path) { } FIL rsfil; - UINT rsbr; - TCHAR reset_byte[1]; // this wries a 1 byte 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, "/menu/RESET", FA_CREATE_ALWAYS ) != FR_OK) { f_close(&rsfil); return FLASHCART_ERR_LOAD; } - - if (f_write(&rsfil, (void *)reset_byte, 1, &rsbr) != FR_OK) { - f_close(&rsfil); - return FLASHCART_ERR_LOAD; - } if (f_close(&rsfil) != FR_OK) { return FLASHCART_OK; diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 7badaf5e..0fba27ae 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -119,32 +119,33 @@ void ed64_ll_set_sram_bank(uint8_t bank) { void PI_Init(void) { PI_DMAWait(); - io_write(PI_STATUS_REG, 0x03); + IO_WRITE(PI_STATUS_REG, 0x03); } // Inits PI for sram transfer void PI_Init_SRAM(void) { - io_write(PI_BSD_DOM2_LAT_REG, 0x05); - io_write(PI_BSD_DOM2_PWD_REG, 0x0C); - io_write(PI_BSD_DOM2_PGS_REG, 0x0D); - io_write(PI_BSD_DOM2_RLS_REG, 0x02); + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); } void PI_DMAWait(void) { - while (io_read(PI_STATUS_REG) & (SP_STATUS_IO_BUSY | SP_STATUS_DMA_BUSY)); + while (IO_READ(PI_STATUS_REG) & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)); + } void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { - io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); - io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); asm volatile ("" : : : "memory"); - io_write(PI_WR_LEN_REG, (size - 1)); + IO_WRITE(PI_WR_LEN_REG, (size - 1)); asm volatile ("" : : : "memory"); } @@ -153,29 +154,29 @@ void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* PI_DMAWait(); - io_write(PI_STATUS_REG, 2); - io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); - io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); - io_write(PI_RD_LEN_REG, (size - 1)); + IO_WRITE(PI_STATUS_REG, 2); + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); + IO_WRITE(PI_RD_LEN_REG, (size - 1)); } void PI_DMAFromCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); - io_write(PI_STATUS_REG, 0x03); - io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); - io_write(PI_CART_ADDR_REG, K0_TO_PHYS(src)); - io_write(PI_WR_LEN_REG, (size - 1)); + IO_WRITE(PI_STATUS_REG, 0x03); + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(src)); + IO_WRITE(PI_WR_LEN_REG, (size - 1)); } void PI_DMAToCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); - io_write(PI_STATUS_REG, 0x02); - io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); - io_write(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); - io_write(PI_RD_LEN_REG, (size - 1)); + IO_WRITE(PI_STATUS_REG, 0x02); + IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); + IO_WRITE(PI_RD_LEN_REG, (size - 1)); } @@ -210,10 +211,10 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { int getSRAM( uint8_t *buffer, int size){ dma_wait(); - io_write(PI_BSD_DOM2_LAT_REG, 0x05); - io_write(PI_BSD_DOM2_PWD_REG, 0x0C); - io_write(PI_BSD_DOM2_PGS_REG, 0x0D); - io_write(PI_BSD_DOM2_RLS_REG, 0x02); + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); dma_wait(); @@ -225,10 +226,10 @@ int getSRAM( uint8_t *buffer, int size){ dma_wait(); - io_write(PI_BSD_DOM2_LAT_REG, 0x40); - io_write(PI_BSD_DOM2_PWD_REG, 0x12); - io_write(PI_BSD_DOM2_PGS_REG, 0x07); - io_write(PI_BSD_DOM2_RLS_REG, 0x03); + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); return 1; } @@ -281,27 +282,27 @@ int setEeprom(uint8_t *buffer, int size){ void setSDTiming(void){ // PI_DMAWait(); - io_write(PI_BSD_DOM1_LAT_REG, 0x40); - io_write(PI_BSD_DOM1_PWD_REG, 0x12); - io_write(PI_BSD_DOM1_PGS_REG, 0x07); - io_write(PI_BSD_DOM1_RLS_REG, 0x03); + IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); - io_write(PI_BSD_DOM2_LAT_REG, 0x40); - io_write(PI_BSD_DOM2_PWD_REG, 0x12); - io_write(PI_BSD_DOM2_PGS_REG, 0x07); - io_write(PI_BSD_DOM2_RLS_REG, 0x03); + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); } void restoreTiming(void) { //n64 timing restore :> - io_write(PI_BSD_DOM1_LAT_REG, 0x40); - io_write(PI_BSD_DOM1_PWD_REG, 0x12); - io_write(PI_BSD_DOM1_PGS_REG, 0x07); - io_write(PI_BSD_DOM1_RLS_REG, 0x03); + IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); - io_write(PI_BSD_DOM2_LAT_REG, 0x40); - io_write(PI_BSD_DOM2_PWD_REG, 0x12); - io_write(PI_BSD_DOM2_PGS_REG, 0x07); - io_write(PI_BSD_DOM2_RLS_REG, 0x03); + IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); + IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); + IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); + IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); } diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 9b9289f0..2561b489 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -14,8 +14,14 @@ // FIXME: redefined because its in a .c instead of a .h #define PI_BASE_REG 0x04600000 +#define IO_READ(addr) (*(volatile uint32_t *)PHYS_TO_K1(addr)) +#define IO_WRITE(addr,data) \ + (*(volatile uint32_t *)PHYS_TO_K1(addr) = (uint32_t)(data)) #define PIF_RAM_START 0x1FC007C0 +#define PI_STATUS_ERROR 0x04 +#define PI_STATUS_IO_BUSY 0x02 +#define PI_STATUS_DMA_BUSY 0x01 #define PI_STATUS_REG (PI_BASE_REG+0x10) #define PI_DRAM_ADDR_REG (PI_BASE_REG+0x00) /* DRAM address */ @@ -33,7 +39,9 @@ #define PI_BSD_DOM2_PGS_REG (PI_BASE_REG+0x2C) #define PI_BSD_DOM2_RLS_REG (PI_BASE_REG+0x30) +#define PHYS_TO_K0(x) ((unsigned long)(x)|0x80000000) /* physical to kseg0 */ #define K0_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg0 to physical */ +#define PHYS_TO_K1(x) ((unsigned long)(x)|0xA0000000) /* physical to kseg1 */ #define K1_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg1 to physical */ From 35e4727c161d1707fec73b16ca66ea3ea0614d33 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Tue, 17 Oct 2023 07:32:48 -0400 Subject: [PATCH 13/38] flash ram fixed --- src/flashcart/ed64/ed64.c | 11 +++- src/flashcart/ed64/ed64_ll.c | 117 +++++++++++++++++++++-------------- src/flashcart/ed64/ed64_ll.h | 2 + src/menu/cart_load.c | 3 +- 4 files changed, 84 insertions(+), 49 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 14d9c8b8..caf0a0cb 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -67,8 +67,9 @@ static flashcart_err_t ed64_init (void) { // everdrive doesn't care about the save type other than eeprom // so we can just check the size - - if (save_size > KiB(2)) { + if (save_size == KiB(128)) { + getFlashRAM(cartsave_data, save_size); + } else if (save_size > KiB(2)) { getSRAM(cartsave_data, save_size); } else { getEeprom(cartsave_data, save_size); @@ -212,7 +213,11 @@ static flashcart_err_t ed64_load_save (char *save_path) { // everdrive doesnt care about the save type other than eeprom // so we can just check the size - if (save_size >= KiB(32)) { //sram and flash + if (save_size == KiB(128)) { //sram 128 and flash + setFlashRAM(cartsave_data, save_size); + } + + else if (save_size > KiB(2)) { //sram setSRAM(cartsave_data, save_size); } diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 0fba27ae..64a4f09c 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -119,22 +119,22 @@ void ed64_ll_set_sram_bank(uint8_t bank) { void PI_Init(void) { PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 0x03); + io_write(PI_STATUS_REG, 0x03); } // Inits PI for sram transfer void PI_Init_SRAM(void) { - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); + io_write(PI_BSD_DOM2_LAT_REG, 0x05); + io_write(PI_BSD_DOM2_PWD_REG, 0x0C); + io_write(PI_BSD_DOM2_PGS_REG, 0x0D); + io_write(PI_BSD_DOM2_RLS_REG, 0x02); } void PI_DMAWait(void) { - while (IO_READ(PI_STATUS_REG) & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)); + while (io_read(PI_STATUS_REG) & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)); } @@ -142,10 +142,10 @@ void PI_DMAWait(void) { void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); - IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); asm volatile ("" : : : "memory"); - IO_WRITE(PI_WR_LEN_REG, (size - 1)); + io_write(PI_WR_LEN_REG, (size - 1)); asm volatile ("" : : : "memory"); } @@ -154,29 +154,29 @@ void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 2); - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); - IO_WRITE(PI_CART_ADDR_REG, (0xA8000000 + offset)); - IO_WRITE(PI_RD_LEN_REG, (size - 1)); + io_write(PI_STATUS_REG, 2); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); + io_write(PI_RD_LEN_REG, (size - 1)); } void PI_DMAFromCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 0x03); - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); - IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(src)); - IO_WRITE(PI_WR_LEN_REG, (size - 1)); + io_write(PI_STATUS_REG, 0x03); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); + io_write(PI_CART_ADDR_REG, K0_TO_PHYS(src)); + io_write(PI_WR_LEN_REG, (size - 1)); } void PI_DMAToCart(void* dest, void* src, unsigned long size) { PI_DMAWait(); - IO_WRITE(PI_STATUS_REG, 0x02); - IO_WRITE(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); - IO_WRITE(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); - IO_WRITE(PI_RD_LEN_REG, (size - 1)); + io_write(PI_STATUS_REG, 0x02); + io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); + io_write(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); + io_write(PI_RD_LEN_REG, (size - 1)); } @@ -211,10 +211,10 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { int getSRAM( uint8_t *buffer, int size){ dma_wait(); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02); + io_write(PI_BSD_DOM2_LAT_REG, 0x05); + io_write(PI_BSD_DOM2_PWD_REG, 0x0C); + io_write(PI_BSD_DOM2_PGS_REG, 0x0D); + io_write(PI_BSD_DOM2_RLS_REG, 0x02); dma_wait(); @@ -226,10 +226,10 @@ int getSRAM( uint8_t *buffer, int size){ dma_wait(); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); return 1; } @@ -244,6 +244,19 @@ int getEeprom( uint8_t *buffer, int size){ } +int getFlashRAM( uint8_t *buffer, int size){ + ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 + PI_DMAWait(); + + getSRAM(buffer, size); + data_cache_hit_writeback_invalidate(buffer, size); + + PI_DMAWait(); + ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); + + return 1; +} + /* sram upload */ @@ -279,30 +292,44 @@ int setEeprom(uint8_t *buffer, int size){ return 1; } +int setFlashRAM(uint8_t *buffer, int size){ + ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 + PI_DMAWait(); + + setSRAM(buffer, size); + data_cache_hit_writeback_invalidate(buffer, size); + + PI_DMAWait(); + ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); + + return 1; +} + + void setSDTiming(void){ // PI_DMAWait(); - IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); + io_write(PI_BSD_DOM1_LAT_REG, 0x40); + io_write(PI_BSD_DOM1_PWD_REG, 0x12); + io_write(PI_BSD_DOM1_PGS_REG, 0x07); + io_write(PI_BSD_DOM1_RLS_REG, 0x03); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); } void restoreTiming(void) { //n64 timing restore :> - IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03); + io_write(PI_BSD_DOM1_LAT_REG, 0x40); + io_write(PI_BSD_DOM1_PWD_REG, 0x12); + io_write(PI_BSD_DOM1_PGS_REG, 0x07); + io_write(PI_BSD_DOM1_RLS_REG, 0x03); - IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40); - IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12); - IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07); - IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03); + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); } diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 2561b489..42156faf 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -92,8 +92,10 @@ void PI_DMAToSRAM(void* src, unsigned long offset, unsigned long size); void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size); int getSRAM( uint8_t *buffer, int size); int getEeprom( uint8_t *buffer, int size); +int getFlashRAM( uint8_t *buffer, int size); int setSRAM( uint8_t *buffer, int size); int setEeprom( uint8_t *buffer, int size); +int setFlashRAM( uint8_t *buffer, int size); #endif diff --git a/src/menu/cart_load.c b/src/menu/cart_load.c index 2986d9a2..6f24d07a 100644 --- a/src/menu/cart_load.c +++ b/src/menu/cart_load.c @@ -1,5 +1,6 @@ #include +#include #include #include "cart_load.h" @@ -174,7 +175,7 @@ cart_load_err_t cart_load_emulator (menu_t *menu, cart_load_emu_type_t emu_type, save_type = FLASHCART_SAVE_TYPE_FLASHRAM; break; case CART_LOAD_EMU_TYPE_GAMEBOY_COLOR: - path_push(path, "gbc.v64"); + path_push(path, "gbc.v64"); save_type = FLASHCART_SAVE_TYPE_FLASHRAM; break; case CART_LOAD_EMU_TYPE_SEGA_GENERIC_8BIT: From 3d61ed89f19ec50689aa5d374e5fa5724eeb57c2 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Tue, 17 Oct 2023 09:10:17 -0400 Subject: [PATCH 14/38] use libdragon io --- src/flashcart/ed64/ed64_ll.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 42156faf..676cd78e 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -14,9 +14,7 @@ // FIXME: redefined because its in a .c instead of a .h #define PI_BASE_REG 0x04600000 -#define IO_READ(addr) (*(volatile uint32_t *)PHYS_TO_K1(addr)) -#define IO_WRITE(addr,data) \ - (*(volatile uint32_t *)PHYS_TO_K1(addr) = (uint32_t)(data)) +/////////////////////////////////////////////////////// #define PIF_RAM_START 0x1FC007C0 #define PI_STATUS_ERROR 0x04 @@ -38,6 +36,7 @@ #define PI_BSD_DOM2_PWD_REG (PI_BASE_REG+0x28) #define PI_BSD_DOM2_PGS_REG (PI_BASE_REG+0x2C) #define PI_BSD_DOM2_RLS_REG (PI_BASE_REG+0x30) +/////////////////////////////////////////////////////// #define PHYS_TO_K0(x) ((unsigned long)(x)|0x80000000) /* physical to kseg0 */ #define K0_TO_PHYS(x) ((unsigned long)(x)&0x1FFFFFFF) /* kseg0 to physical */ From 66668aa2212f61362830aed614c042d79ccad637 Mon Sep 17 00:00:00 2001 From: ariahiro64 <45049787+ariahiro64@users.noreply.github.com> Date: Thu, 19 Oct 2023 02:45:55 -0400 Subject: [PATCH 15/38] Update src/menu/cart_load.c remove whitespace Co-authored-by: Robin Jones --- src/menu/cart_load.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/menu/cart_load.c b/src/menu/cart_load.c index 6f24d07a..eb7979f5 100644 --- a/src/menu/cart_load.c +++ b/src/menu/cart_load.c @@ -175,7 +175,7 @@ cart_load_err_t cart_load_emulator (menu_t *menu, cart_load_emu_type_t emu_type, save_type = FLASHCART_SAVE_TYPE_FLASHRAM; break; case CART_LOAD_EMU_TYPE_GAMEBOY_COLOR: - path_push(path, "gbc.v64"); + path_push(path, "gbc.v64"); save_type = FLASHCART_SAVE_TYPE_FLASHRAM; break; case CART_LOAD_EMU_TYPE_SEGA_GENERIC_8BIT: From e76e11db7ebd0e5a44fb7b52dbe5f06c9a78bb4f Mon Sep 17 00:00:00 2001 From: ariahiro64 <45049787+ariahiro64@users.noreply.github.com> Date: Thu, 19 Oct 2023 02:56:50 -0400 Subject: [PATCH 16/38] Update src/flashcart/ed64/ed64.c Co-authored-by: Robin Jones --- src/flashcart/ed64/ed64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index caf0a0cb..48fe501a 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -17,7 +17,7 @@ // This is a trial hack before using the settings API. #ifndef LAST_SAVE_FILE_PATH -#define LAST_SAVE_FILE_PATH strip_sd_prefix("/menu/last_rom.txt") +#define LAST_SAVE_FILE_PATH "/menu/last_rom.txt" #endif extern int ed_exit(void); From 8f8b9ad31b9a69c6c58deac71540340e864f6aa4 Mon Sep 17 00:00:00 2001 From: ariahiro64 <45049787+ariahiro64@users.noreply.github.com> Date: Thu, 19 Oct 2023 02:58:08 -0400 Subject: [PATCH 17/38] Update src/flashcart/ed64/ed64.c Co-authored-by: Robin Jones --- src/flashcart/ed64/ed64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 48fe501a..c22fbc7a 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -203,7 +203,7 @@ static flashcart_err_t ed64_load_save (char *save_path) { if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) { f_close(&fil); - return FLASHCART_ERR_LOAD; + return FLASHCART_ERR_LOAD; } if (f_close(&fil) != FR_OK) { From c30fc873b02deb4834f46ce2a11141f4fb8502dc Mon Sep 17 00:00:00 2001 From: ariahiro64 <45049787+ariahiro64@users.noreply.github.com> Date: Thu, 19 Oct 2023 02:59:43 -0400 Subject: [PATCH 18/38] Update src/flashcart/ed64/ed64.c Co-authored-by: Robin Jones --- src/flashcart/ed64/ed64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index c22fbc7a..a1fdd90c 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -202,7 +202,7 @@ static flashcart_err_t ed64_load_save (char *save_path) { uint8_t cartsave_data[save_size]; if (f_read(&fil, cartsave_data, save_size, &br) != FR_OK) { - f_close(&fil); + f_close(&fil); return FLASHCART_ERR_LOAD; } From f49f7a47f34cc5e9bc72fada0cf7307951a4138d Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Thu, 19 Oct 2023 08:13:06 -0400 Subject: [PATCH 19/38] fixed sram 128 and added suggested changes --- src/flashcart/ed64/ed64.c | 336 +++++++++++++++++++++++--------------- 1 file changed, 205 insertions(+), 131 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index a1fdd90c..2e7ccdc6 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -12,83 +12,110 @@ #include "ed64_ll.h" #include "ed64.h" - // #include "../menu/settings.h" // This is a trial hack before using the settings API. #ifndef LAST_SAVE_FILE_PATH #define LAST_SAVE_FILE_PATH "/menu/last_rom.txt" #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); -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 - - f_unlink(strip_sd_prefix("/menu/RESET")); - - FIL lrp_fil; - UINT lrp_br; - TCHAR lrp_path[255*32]; + // FIXME: Update firmware if needed. + // FIXME: Enable RTC if available. - // FIXME: use max supported length - - if (f_open(&lrp_fil, LAST_SAVE_FILE_PATH, FA_READ) != FR_OK) { - return FLASHCART_ERR_LOAD; - } + // older everdrives cant save during gameplay so we need to the reset method. + // works by checking if a file exists. - 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) { - f_close(&lrp_fil); - return FLASHCART_ERR_LOAD; - } + // make sure next boot doesnt trigger the check by deleting the reset file + f_unlink(strip_sd_prefix(RESET_CHECK_FILE_PATH)); - if (f_close(&lrp_fil) != FR_OK) { - return FLASHCART_ERR_LOAD; - } + // finds the last save location + FIL lrp_fil; + UINT lrp_br; - // Now save the content back to the SD! - - FIL fil; - UINT br; - uint8_t cartsave_data[KiB(128)]; - // Older everdrives cant save during gameplay so we need to the reset method. + if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK) + { + return FLASHCART_ERR_LOAD; + } - // find the path to last save - if (file_exists(strip_sd_prefix(lrp_path))) { + int lrp_size = f_size(&lrp_fil); - 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) { - return FLASHCART_ERR_LOAD; - } + if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) + { + f_close(&lrp_fil); + return FLASHCART_ERR_LOAD; + } - // everdrive doesn't care about the save type other than eeprom - // so we can just check the size - if (save_size == KiB(128)) { - 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(&lrp_fil) != FR_OK) + { + return FLASHCART_ERR_LOAD; + } - if (f_close(&fil) != FR_OK) { - return FLASHCART_ERR_LOAD; + // Now save the content back to the SD! + 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; } - -static flashcart_err_t ed64_deinit (void) { +static flashcart_err_t ed64_deinit(void) +{ // For the moment, just use libCart exit. ed_exit(); @@ -96,18 +123,24 @@ static flashcart_err_t ed64_deinit (void) { return FLASHCART_OK; } -static bool ed64_has_feature (flashcart_features_t feature) { - switch (feature) { - case FLASHCART_FEATURE_64DD: return false; - default: return false; +static bool ed64_has_feature(flashcart_features_t feature) +{ + switch (feature) + { + 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; 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; } @@ -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. // 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); 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 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); - 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); return FLASHCART_ERR_LOAD; } - if (progress) { - progress(f_tell(&fil) / (float) (f_size(&fil))); + if (progress) + { + progress(f_tell(&fil) / (float)(f_size(&fil))); } } - if (f_tell(&fil) != sdram_size) { + if (f_tell(&fil) != sdram_size) + { f_close(&fil); return FLASHCART_ERR_LOAD; } - - if (f_close(&fil) != FR_OK) { + if (f_close(&fil) != FR_OK) + { return FLASHCART_ERR_LOAD; } 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; 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; } @@ -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. // 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); return FLASHCART_ERR_ARGS; } - if (f_lseek(&fil, file_offset) != FR_OK) { + 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) { + 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) { + if (br != file_size) + { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) { + if (f_close(&fil) != FR_OK) + { return FLASHCART_ERR_LOAD; } 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; UINT br; - if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) { - f_close(&fil); - return FLASHCART_ERR_LOAD; + if (f_open(&fil, strip_sd_prefix(save_path), FA_READ) != FR_OK) + { + f_close(&fil); + return FLASHCART_ERR_LOAD; } size_t save_size = file_get_size(strip_sd_prefix(save_path)); 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); return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) { + if (f_close(&fil) != FR_OK) + { return FLASHCART_ERR_LOAD; } - // everdrive doesnt care about the save type other than eeprom - // so we can just check the size - - if (save_size == KiB(128)) { //sram 128 and flash - setFlashRAM(cartsave_data, save_size); - } - - else if (save_size > KiB(2)) { //sram - setSRAM(cartsave_data, save_size); - } - - else if (save_size >= 512){ // eeprom + ed64_save_type_t type = ed64_ll_get_save_type(); + switch (type) + { + case SAVE_TYPE_EEPROM_4K: + case SAVE_TYPE_EEPROM_16K: 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; UINT lsp_bw; - if (f_open(&lsp_fil, LAST_SAVE_FILE_PATH, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { - return FLASHCART_ERR_LOAD; + if (f_open(&lsp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) + { + return FLASHCART_ERR_LOAD; } - 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; + 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; } - if (f_close(&lsp_fil) != FR_OK) { - return FLASHCART_ERR_LOAD; + if (f_close(&lsp_fil) != FR_OK) + { + return FLASHCART_ERR_LOAD; } 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) { - f_close(&rsfil); - return FLASHCART_ERR_LOAD; + if (f_open(&rsfil, strip_sd_prefix(RESET_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) + { + 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; } -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; - switch (save_type) { - case FLASHCART_SAVE_TYPE_NONE: - type = SAVE_TYPE_NONE; - break; - case FLASHCART_SAVE_TYPE_EEPROM_4K: - type = SAVE_TYPE_EEPROM_4K; - break; - case FLASHCART_SAVE_TYPE_EEPROM_16K: - type = SAVE_TYPE_EEPROM_16K; - break; - case FLASHCART_SAVE_TYPE_SRAM: - type = SAVE_TYPE_SRAM; - break; - case FLASHCART_SAVE_TYPE_SRAM_BANKED: - case FLASHCART_SAVE_TYPE_SRAM_128K: - type = SAVE_TYPE_SRAM_128K; - break; - case FLASHCART_SAVE_TYPE_FLASHRAM_PKST2: - case FLASHCART_SAVE_TYPE_FLASHRAM: - type = SAVE_TYPE_FLASHRAM; - break; - default: - return FLASHCART_ERR_ARGS; + switch (save_type) + { + case FLASHCART_SAVE_TYPE_NONE: + type = SAVE_TYPE_NONE; + break; + case FLASHCART_SAVE_TYPE_EEPROM_4K: + type = SAVE_TYPE_EEPROM_4K; + break; + case FLASHCART_SAVE_TYPE_EEPROM_16K: + type = SAVE_TYPE_EEPROM_16K; + break; + case FLASHCART_SAVE_TYPE_SRAM: + type = SAVE_TYPE_SRAM; + break; + case FLASHCART_SAVE_TYPE_SRAM_BANKED: + case FLASHCART_SAVE_TYPE_SRAM_128K: + type = SAVE_TYPE_SRAM_128K; + break; + case FLASHCART_SAVE_TYPE_FLASHRAM_PKST2: + case FLASHCART_SAVE_TYPE_FLASHRAM: + type = SAVE_TYPE_FLASHRAM; + break; + default: + return FLASHCART_ERR_ARGS; } 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; } - static flashcart_t flashcart_ed64 = { .init = ed64_init, .deinit = ed64_deinit, @@ -303,7 +377,7 @@ static flashcart_t flashcart_ed64 = { .set_save_writeback = NULL, }; - -flashcart_t *ed64_get_flashcart (void) { +flashcart_t *ed64_get_flashcart(void) +{ return &flashcart_ed64; } From ef8d86b7e49dd981a315cca74975e1f93f801502 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Fri, 20 Oct 2023 04:32:26 -0400 Subject: [PATCH 20/38] dinosaur planet working --- src/flashcart/ed64/ed64.c | 7 ++--- src/flashcart/ed64/ed64_ll.c | 50 ++++++++++-------------------------- src/flashcart/ed64/ed64_ll.h | 1 - 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 2e7ccdc6..37dcdbc4 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -4,6 +4,7 @@ #include #include +#include #include "utils/fs.h" #include "utils/utils.h" @@ -146,7 +147,7 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback fix_file_size(&fil); - size_t rom_size = f_size(&fil); + size_t rom_size = f_size(&fil) - KiB(128); // 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. @@ -173,11 +174,11 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback progress(f_tell(&fil) / (float)(f_size(&fil))); } } - if (f_tell(&fil) != sdram_size) + /*if (f_tell(&fil) != sdram_size) { f_close(&fil); return FLASHCART_ERR_LOAD; - } + }*/ if (f_close(&fil) != FR_OK) { diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 64a4f09c..d5858ced 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -98,7 +98,6 @@ void ed64_ll_set_save_type(ed64_save_type_t type) { break; } - save_cfg = 0; if (eeprom_on)save_cfg |= SAV_EEP_ON; if (sram_on)save_cfg |= SAV_SRM_ON; if (eeprom_size)save_cfg |= SAV_EEP_SIZE; @@ -118,7 +117,7 @@ void ed64_ll_set_sram_bank(uint8_t bank) { void PI_Init(void) { - PI_DMAWait(); + dma_wait(); io_write(PI_STATUS_REG, 0x03); } @@ -132,13 +131,6 @@ void PI_Init_SRAM(void) { } -void PI_DMAWait(void) { - - while (io_read(PI_STATUS_REG) & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)); - -} - - void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { @@ -152,7 +144,7 @@ void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* - PI_DMAWait(); + dma_wait(); io_write(PI_STATUS_REG, 2); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); @@ -161,7 +153,7 @@ void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* } void PI_DMAFromCart(void* dest, void* src, unsigned long size) { - PI_DMAWait(); + dma_wait(); io_write(PI_STATUS_REG, 0x03); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); @@ -171,7 +163,7 @@ void PI_DMAFromCart(void* dest, void* src, unsigned long size) { void PI_DMAToCart(void* dest, void* src, unsigned long size) { - PI_DMAWait(); + dma_wait(); io_write(PI_STATUS_REG, 0x02); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); @@ -190,7 +182,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { //FIXME: Do i really need to check if size is 16bit aligned? if (!unalignedDest && !unalignedSrc && !(size % 2)) { PI_DMAFromCart(dest, src, size); - PI_DMAWait(); + dma_wait(); return; } @@ -200,7 +192,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { unsigned char *buffer = memalign(8, newSize); PI_DMAFromCart(buffer, newSrc, newSize); - PI_DMAWait(); + dma_wait(); memcpy(dest, (buffer + unalignedSrc), size); @@ -246,12 +238,12 @@ int getEeprom( uint8_t *buffer, int size){ int getFlashRAM( uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 - PI_DMAWait(); + dma_wait(); getSRAM(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); - PI_DMAWait(); + dma_wait(); ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); return 1; @@ -262,7 +254,7 @@ sram upload */ int setSRAM( uint8_t *buffer, int size){ //half working - PI_DMAWait(); + dma_wait(); //Timing PI_Init_SRAM(); @@ -271,11 +263,12 @@ int setSRAM( uint8_t *buffer, int size){ data_cache_hit_writeback_invalidate(buffer,size); dma_wait(); + PI_DMAToSRAM(buffer, 0, size); data_cache_hit_writeback_invalidate(buffer,size); //Wait - PI_DMAWait(); + dma_wait(); //Restore evd Timing setSDTiming(); @@ -293,13 +286,13 @@ int setEeprom(uint8_t *buffer, int size){ } int setFlashRAM(uint8_t *buffer, int size){ - ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 - PI_DMAWait(); + ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); + dma_wait(); setSRAM(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); - PI_DMAWait(); + dma_wait(); ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); return 1; @@ -308,21 +301,6 @@ int setFlashRAM(uint8_t *buffer, int size){ void setSDTiming(void){ - // PI_DMAWait(); - io_write(PI_BSD_DOM1_LAT_REG, 0x40); - io_write(PI_BSD_DOM1_PWD_REG, 0x12); - io_write(PI_BSD_DOM1_PGS_REG, 0x07); - io_write(PI_BSD_DOM1_RLS_REG, 0x03); - - io_write(PI_BSD_DOM2_LAT_REG, 0x40); - io_write(PI_BSD_DOM2_PWD_REG, 0x12); - io_write(PI_BSD_DOM2_PGS_REG, 0x07); - io_write(PI_BSD_DOM2_RLS_REG, 0x03); -} - - -void restoreTiming(void) { - //n64 timing restore :> io_write(PI_BSD_DOM1_LAT_REG, 0x40); io_write(PI_BSD_DOM1_PWD_REG, 0x12); io_write(PI_BSD_DOM1_PGS_REG, 0x07); diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 676cd78e..9230d7b7 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -83,7 +83,6 @@ void setSDTiming(void); void PI_Init(void); void PI_Init_SRAM(void); -void PI_DMAWait(void); void PI_DMAFromCart(void* dest, void* src, unsigned long size); void PI_DMAToCart(void* dest, void* src, unsigned long size); void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size); From 9ef2f2d8a4b7eb3e7284fd1b78132cbc8d6a50ca Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Fri, 20 Oct 2023 04:49:34 -0400 Subject: [PATCH 21/38] hotfix --- src/flashcart/ed64/ed64.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 37dcdbc4..e9e75f36 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -147,7 +147,7 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback fix_file_size(&fil); - size_t rom_size = f_size(&fil) - KiB(128); + size_t rom_size = f_size(&fil); // 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. @@ -158,6 +158,11 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback return FLASHCART_ERR_LOAD; } + if (rom_size == MiB(64)) + { + rom_size -= KiB(128); + } + size_t sdram_size = rom_size; size_t chunk_size = MiB(1); From 11d90fdc1a73d6d97c0176ef81dd69adade7fdd9 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 21:07:35 +0100 Subject: [PATCH 22/38] Improve code style Function names. file names. things included in header. --- src/flashcart/ed64/ed64.c | 22 +++++++------- src/flashcart/ed64/ed64_ll.c | 56 ++++++++++++++++++++---------------- src/flashcart/ed64/ed64_ll.h | 36 +++++++---------------- 3 files changed, 53 insertions(+), 61 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index e9e75f36..12988d26 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -17,13 +17,13 @@ // This is a trial hack before using the settings API. #ifndef LAST_SAVE_FILE_PATH -#define LAST_SAVE_FILE_PATH "/menu/last_rom.txt" +#define LAST_SAVE_FILE_PATH "/menu/last_rom.tmp" #endif #ifndef RESET_CHECK_FILE_PATH -#define RESET_CHECK_FILE_PATH "/menu/RESET" +#define RESET_CHECK_FILE_PATH "/menu/reset.tmp" #endif #ifndef FLASHRAM_CHECK_FILE_PATH -#define FLASHRAM_CHECK_FILE_PATH "/menu/FLASHRAM" +#define FLASHRAM_CHECK_FILE_PATH "/menu/flashram.tmp" #endif extern int ed_exit(void); @@ -35,7 +35,7 @@ static flashcart_err_t ed64_init(void) // FIXME: Update firmware if needed. // FIXME: Enable RTC if available. - // older everdrives cant save during gameplay so we need to the reset method. + // older everdrives cannot save during gameplay so we need to the reset method. // works by checking if a file exists. if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) @@ -68,7 +68,7 @@ static flashcart_err_t ed64_init(void) return FLASHCART_ERR_LOAD; } - // Now save the content back to the SD! + // Now save the content back to the SD card! FIL fil; UINT br; uint8_t cartsave_data[KiB(128)]; @@ -88,17 +88,17 @@ static flashcart_err_t ed64_init(void) // 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); + ed64_ll_get_fram(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); + ed64_ll_get_sram(cartsave_data, save_size); } else { // eeprom - getEeprom(cartsave_data, save_size); + ed64_ll_get_eeprom(cartsave_data, save_size); } if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) @@ -272,14 +272,14 @@ static flashcart_err_t ed64_load_save(char *save_path) { case SAVE_TYPE_EEPROM_4K: case SAVE_TYPE_EEPROM_16K: - setEeprom(cartsave_data, save_size); + ed64_ll_set_eeprom(cartsave_data, save_size); break; case SAVE_TYPE_SRAM: case SAVE_TYPE_SRAM_128K: - setSRAM(cartsave_data, save_size); + ed64_ll_set_sram(cartsave_data, save_size); break; case SAVE_TYPE_FLASHRAM: - setFlashRAM(cartsave_data, save_size); + 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 FIL flashfil; diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index d5858ced..a08bc0d9 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -24,6 +24,14 @@ typedef enum { } ed64_registers_t; +void pi_initialize(void); +void pi_initialize_sram(void); +void pi_dma_from_cart(void* dest, void* src, unsigned long size); +void pi_dma_to_cart(void* dest, void* src, unsigned long size); +void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size); +void pi_dma_to_sram(void* src, unsigned long offset, unsigned long size); +void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size); + #define SAV_EEP_ON 1 #define SAV_SRM_ON 2 @@ -116,13 +124,13 @@ void ed64_ll_set_sram_bank(uint8_t bank) { } -void PI_Init(void) { +void pi_initialize(void) { dma_wait(); io_write(PI_STATUS_REG, 0x03); } // Inits PI for sram transfer -void PI_Init_SRAM(void) { +void pi_initialize_sram(void) { io_write(PI_BSD_DOM2_LAT_REG, 0x05); io_write(PI_BSD_DOM2_PWD_REG, 0x0C); @@ -131,7 +139,7 @@ void PI_Init_SRAM(void) { } -void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { +void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size) { io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); @@ -143,7 +151,7 @@ void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size) { } -void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* +void pi_dma_to_sram(void *src, unsigned long offset, unsigned long size) { //void* dma_wait(); io_write(PI_STATUS_REG, 2); @@ -152,7 +160,7 @@ void PI_DMAToSRAM(void *src, unsigned long offset, unsigned long size) { //void* io_write(PI_RD_LEN_REG, (size - 1)); } -void PI_DMAFromCart(void* dest, void* src, unsigned long size) { +void pi_dma_from_cart(void* dest, void* src, unsigned long size) { dma_wait(); io_write(PI_STATUS_REG, 0x03); @@ -162,7 +170,7 @@ void PI_DMAFromCart(void* dest, void* src, unsigned long size) { } -void PI_DMAToCart(void* dest, void* src, unsigned long size) { +void pi_dma_to_cart(void* dest, void* src, unsigned long size) { dma_wait(); io_write(PI_STATUS_REG, 0x02); @@ -173,7 +181,7 @@ void PI_DMAToCart(void* dest, void* src, unsigned long size) { // Wrapper to support unaligned access to memory -void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { +void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size) { if (!dest || !src || !size) return; unsigned long unalignedSrc = ((unsigned long)src) % 2; @@ -181,7 +189,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { //FIXME: Do i really need to check if size is 16bit aligned? if (!unalignedDest && !unalignedSrc && !(size % 2)) { - PI_DMAFromCart(dest, src, size); + pi_dma_from_cart(dest, src, size); dma_wait(); return; @@ -191,7 +199,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { unsigned long newSize = (size + unalignedSrc) + ((size + unalignedSrc) % 2); unsigned char *buffer = memalign(8, newSize); - PI_DMAFromCart(buffer, newSrc, newSize); + pi_dma_from_cart(buffer, newSrc, newSize); dma_wait(); memcpy(dest, (buffer + unalignedSrc), size); @@ -200,7 +208,7 @@ void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size) { } -int getSRAM( uint8_t *buffer, int size){ +int ed64_ll_get_sram( uint8_t *buffer, int size){ dma_wait(); io_write(PI_BSD_DOM2_LAT_REG, 0x05); @@ -210,11 +218,11 @@ int getSRAM( uint8_t *buffer, int size){ dma_wait(); - PI_Init(); + pi_initialize(); dma_wait(); - PI_DMAFromSRAM(buffer, 0, size) ; + pi_dma_from_sram(buffer, 0, size) ; dma_wait(); @@ -226,7 +234,7 @@ int getSRAM( uint8_t *buffer, int size){ return 1; } -int getEeprom( uint8_t *buffer, int size){ +int ed64_ll_get_eeprom( uint8_t *buffer, int size){ int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_read( b, &buffer[b * 8] ); @@ -236,11 +244,11 @@ int getEeprom( uint8_t *buffer, int size){ } -int getFlashRAM( uint8_t *buffer, int size){ +int ed64_ll_get_fram( uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 dma_wait(); - getSRAM(buffer, size); + ed64_ll_get_sram(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); @@ -252,31 +260,31 @@ int getFlashRAM( uint8_t *buffer, int size){ /* sram upload */ -int setSRAM( uint8_t *buffer, int size){ +int ed64_ll_set_sram( uint8_t *buffer, int size){ //half working dma_wait(); //Timing - PI_Init_SRAM(); + pi_initialize_sram(); //Readmode - PI_Init(); + pi_initialize(); data_cache_hit_writeback_invalidate(buffer,size); dma_wait(); - PI_DMAToSRAM(buffer, 0, size); + pi_dma_to_sram(buffer, 0, size); data_cache_hit_writeback_invalidate(buffer,size); //Wait dma_wait(); //Restore evd Timing - setSDTiming(); + ed64_ll_set_sdcard_timing(); return 1; } -int setEeprom(uint8_t *buffer, int size){ +int ed64_ll_set_eeprom(uint8_t *buffer, int size){ int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_write( b, &buffer[b * 8] ); @@ -285,11 +293,11 @@ int setEeprom(uint8_t *buffer, int size){ return 1; } -int setFlashRAM(uint8_t *buffer, int size){ +int ed64_ll_set_fram(uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); dma_wait(); - setSRAM(buffer, size); + ed64_ll_set_sram(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); @@ -299,7 +307,7 @@ int setFlashRAM(uint8_t *buffer, int size){ } -void setSDTiming(void){ +void ed64_ll_set_sdcard_timing(void){ io_write(PI_BSD_DOM1_LAT_REG, 0x40); io_write(PI_BSD_DOM1_PWD_REG, 0x12); diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 9230d7b7..fc009105 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -69,31 +69,15 @@ void ed64_ll_set_ram_bank(uint8_t bank); ed64_save_type_t ed64_ll_get_save_type(); void ed64_ll_set_save_type(ed64_save_type_t type); +void ed64_ll_set_sdcard_timing(void); + +int ed64_ll_get_sram( uint8_t *buffer, int size); +int ed64_ll_get_eeprom( uint8_t *buffer, int size); +int ed64_ll_get_fram( uint8_t *buffer, int size); + +int ed64_ll_set_sram( uint8_t *buffer, int size); +int ed64_ll_set_eeprom( uint8_t *buffer, int size); +int ed64_ll_set_fram( uint8_t *buffer, int size); + /** @} */ /* ed64 */ - -void data_cache_hit_writeback_invalidate(volatile void *, unsigned long); -unsigned int CRC_Calculate(unsigned int crc, unsigned char* buf, unsigned int len); -void dma_write_sram(void* src, unsigned long offset, unsigned long size); -void dma_read_sram(void *dest, unsigned long offset, unsigned long size); -void dma_write_s(void * ram_address, unsigned long pi_address, unsigned long len); -void dma_read_s(void * ram_address, unsigned long pi_address, unsigned long len); -int writeSram(void* src, unsigned long size); -void setSDTiming(void); - - -void PI_Init(void); -void PI_Init_SRAM(void); -void PI_DMAFromCart(void* dest, void* src, unsigned long size); -void PI_DMAToCart(void* dest, void* src, unsigned long size); -void PI_DMAFromSRAM(void *dest, unsigned long offset, unsigned long size); -void PI_DMAToSRAM(void* src, unsigned long offset, unsigned long size); -void PI_SafeDMAFromCart(void *dest, void *src, unsigned long size); -int getSRAM( uint8_t *buffer, int size); -int getEeprom( uint8_t *buffer, int size); -int getFlashRAM( uint8_t *buffer, int size); -int setSRAM( uint8_t *buffer, int size); -int setEeprom( uint8_t *buffer, int size); -int setFlashRAM( uint8_t *buffer, int size); - - #endif From f75a26ad126be5a1d28658c541cfe154758df7cd Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 21:23:13 +0100 Subject: [PATCH 23/38] Move sd timings --- src/flashcart/ed64/ed64_ll.c | 4 ++++ src/flashcart/ed64/ed64_ll.h | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index a08bc0d9..137c6995 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -32,6 +32,10 @@ void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size); void pi_dma_to_sram(void* src, unsigned long offset, unsigned long size); void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size); +void ed64_ll_set_sdcard_timing(void); + +void ed64_ll_set_sdcard_timing(void); + #define SAV_EEP_ON 1 #define SAV_SRM_ON 2 diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index fc009105..5fd38ad4 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -69,8 +69,6 @@ void ed64_ll_set_ram_bank(uint8_t bank); ed64_save_type_t ed64_ll_get_save_type(); void ed64_ll_set_save_type(ed64_save_type_t type); -void ed64_ll_set_sdcard_timing(void); - int ed64_ll_get_sram( uint8_t *buffer, int size); int ed64_ll_get_eeprom( uint8_t *buffer, int size); int ed64_ll_get_fram( uint8_t *buffer, int size); From d0875e4a36607065451453f6240b573ab988b9f0 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 21:29:47 +0100 Subject: [PATCH 24/38] Remove un-necessary header --- src/menu/cart_load.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/menu/cart_load.c b/src/menu/cart_load.c index eb7979f5..2986d9a2 100644 --- a/src/menu/cart_load.c +++ b/src/menu/cart_load.c @@ -1,6 +1,5 @@ #include -#include #include #include "cart_load.h" From 0f17c610e04779fc0cab450da4278f4f14f7e935 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 22:08:53 +0100 Subject: [PATCH 25/38] Code style Also fixes ed64_ll_set_ram_bank --- src/flashcart/ed64/ed64.c | 138 ++++++++++++----------------------- src/flashcart/ed64/ed64_ll.c | 94 +++++++++++++++--------- src/flashcart/ed64/ed64_ll.h | 18 ++--- 3 files changed, 115 insertions(+), 135 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 12988d26..11a9c309 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -26,10 +26,10 @@ #define FLASHRAM_CHECK_FILE_PATH "/menu/flashram.tmp" #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 // FIXME: Update firmware if needed. @@ -38,8 +38,7 @@ 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. - if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) - { + if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) { // make sure next boot doesnt trigger the check by deleting the reset file f_unlink(strip_sd_prefix(RESET_CHECK_FILE_PATH)); @@ -48,8 +47,7 @@ static flashcart_err_t ed64_init(void) FIL lrp_fil; UINT lrp_br; - if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK) - { + if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -57,14 +55,12 @@ static flashcart_err_t ed64_init(void) TCHAR lrp_path[lrp_size++]; - if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) - { + if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) { f_close(&lrp_fil); return FLASHCART_ERR_LOAD; } - if (f_close(&lrp_fil) != FR_OK) - { + if (f_close(&lrp_fil) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -74,40 +70,33 @@ static flashcart_err_t ed64_init(void) uint8_t cartsave_data[KiB(128)]; // find the path to last save - if (file_exists(strip_sd_prefix(lrp_path))) - { + 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) - { + 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 + if (file_exists(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH))) { // flashram is bugged atm ed64_ll_get_fram(cartsave_data, save_size); // deletes flag f_unlink(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH)); } - else if (save_size > KiB(2)) - { // sram + else if (save_size > KiB(2)) { // sram ed64_ll_get_sram(cartsave_data, save_size); } - else - { // eeprom + else { // eeprom ed64_ll_get_eeprom(cartsave_data, save_size); } - if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) - { + if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) { return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } } @@ -115,8 +104,7 @@ static flashcart_err_t ed64_init(void) return FLASHCART_OK; } -static flashcart_err_t ed64_deinit(void) -{ +static flashcart_err_t ed64_deinit (void) { // For the moment, just use libCart exit. ed_exit(); @@ -124,10 +112,8 @@ static flashcart_err_t ed64_deinit(void) return FLASHCART_OK; } -static bool ed64_has_feature(flashcart_features_t feature) -{ - switch (feature) - { +static bool ed64_has_feature (flashcart_features_t feature) { + switch (feature) { case FLASHCART_FEATURE_64DD: return false; default: @@ -135,13 +121,12 @@ static bool ed64_has_feature(flashcart_features_t feature) } } -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; 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; } @@ -152,14 +137,12 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback // 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. - if (rom_size > MiB(64)) - { + if (rom_size > MiB(64)) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (rom_size == MiB(64)) - { + if (rom_size == MiB(64)) { rom_size -= KiB(128); } @@ -169,37 +152,32 @@ static flashcart_err_t ed64_load_rom(char *rom_path, flashcart_progress_callback 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) - { + if (f_read(&fil, (void *)(ROM_ADDRESS + offset), block_size, &br) != FR_OK) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (progress) - { + if (progress) { progress(f_tell(&fil) / (float)(f_size(&fil))); } } - /*if (f_tell(&fil) != sdram_size) - { + /*if (f_tell(&fil) != sdram_size) { f_close(&fil); return FLASHCART_ERR_LOAD; }*/ - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } 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; 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; } @@ -210,45 +188,38 @@ static flashcart_err_t ed64_load_file(char *file_path, uint32_t rom_offset, uint // 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. - if (file_size > (MiB(64) - rom_offset)) - { + if (file_size > (MiB(64) - rom_offset)) { f_close(&fil); return FLASHCART_ERR_ARGS; } - if (f_lseek(&fil, file_offset) != FR_OK) - { + 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) - { + 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) - { + if (br != file_size) { f_close(&fil); return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } 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; 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; } @@ -256,20 +227,17 @@ static flashcart_err_t ed64_load_save(char *save_path) size_t save_size = file_get_size(strip_sd_prefix(save_path)); 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); return FLASHCART_ERR_LOAD; } - if (f_close(&fil) != FR_OK) - { + if (f_close(&fil) != FR_OK) { return FLASHCART_ERR_LOAD; } ed64_save_type_t type = ed64_ll_get_save_type(); - switch (type) - { + switch (type) { case SAVE_TYPE_EEPROM_4K: case SAVE_TYPE_EEPROM_16K: ed64_ll_set_eeprom(cartsave_data, save_size); @@ -283,14 +251,12 @@ static flashcart_err_t ed64_load_save(char *save_path) // 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) - { + 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) - { + if (f_close(&flashfil) != FR_OK) { return FLASHCART_OK; } break; @@ -302,18 +268,15 @@ static flashcart_err_t ed64_load_save(char *save_path) FIL lsp_fil; UINT lsp_bw; - if (f_open(&lsp_fil, strip_sd_prefix(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; } - 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; } - if (f_close(&lsp_fil) != FR_OK) - { + if (f_close(&lsp_fil) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -321,26 +284,22 @@ static flashcart_err_t ed64_load_save(char *save_path) // simulate a unix touch command to create a file as it only needs to exist to detect a reset - if (f_open(&rsfil, strip_sd_prefix(RESET_CHECK_FILE_PATH), 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; } - if (f_close(&rsfil) != FR_OK) - { + if (f_close(&rsfil) != FR_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; - switch (save_type) - { + switch (save_type) { case FLASHCART_SAVE_TYPE_NONE: type = SAVE_TYPE_NONE; break; @@ -383,7 +342,6 @@ static flashcart_t flashcart_ed64 = { .set_save_writeback = NULL, }; -flashcart_t *ed64_get_flashcart(void) -{ +flashcart_t *ed64_get_flashcart (void) { return &flashcart_ed64; } diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 137c6995..5b04eed5 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -24,17 +24,15 @@ typedef enum { } ed64_registers_t; -void pi_initialize(void); -void pi_initialize_sram(void); -void pi_dma_from_cart(void* dest, void* src, unsigned long size); -void pi_dma_to_cart(void* dest, void* src, unsigned long size); -void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size); -void pi_dma_to_sram(void* src, unsigned long offset, unsigned long size); -void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size); +void pi_initialize (void); +void pi_initialize_sram (void); +void pi_dma_from_cart (void* dest, void* src, unsigned long size); +void pi_dma_to_cart (void* dest, void* src, unsigned long size); +void pi_dma_from_sram (void *dest, unsigned long offset, unsigned long size); +void pi_dma_to_sram (void* src, unsigned long offset, unsigned long size); +void pi_dma_from_cart_safe (void *dest, void *src, unsigned long size); -void ed64_ll_set_sdcard_timing(void); - -void ed64_ll_set_sdcard_timing(void); +void ed64_ll_set_sdcard_timing (void); #define SAV_EEP_ON 1 @@ -45,20 +43,21 @@ void ed64_ll_set_sdcard_timing(void); #define SAV_RAM_BANK 128 #define SAV_RAM_BANK_APPLY 32768 -uint32_t ed64_ll_reg_read(uint32_t reg); -void ed64_ll_reg_write(uint32_t reg, uint32_t data); +uint32_t ed64_ll_reg_read (uint32_t reg); +void ed64_ll_reg_write (uint32_t reg, uint32_t data); uint8_t ed64_ll_sram_bank; ed64_save_type_t ed64_ll_save_type; -uint32_t ed64_ll_reg_read(uint32_t reg) { +uint32_t ed64_ll_reg_read (uint32_t reg) { *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE); return *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE + reg * 4); + } -void ed64_ll_reg_write(uint32_t reg, uint32_t data) { +void ed64_ll_reg_write (uint32_t reg, uint32_t data) { *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE); *(volatile uint32_t *) (ED64_CONFIG_REGS_BASE + reg * 4) = data; @@ -67,12 +66,13 @@ void ed64_ll_reg_write(uint32_t reg, uint32_t data) { } -ed64_save_type_t ed64_ll_get_save_type() { +ed64_save_type_t ed64_ll_get_save_type (void) { return ed64_ll_save_type; + } -void ed64_ll_set_save_type(ed64_save_type_t type) { +void ed64_ll_set_save_type (ed64_save_type_t type) { uint16_t save_cfg; uint8_t eeprom_on, sram_on, eeprom_size, sram_size, ram_bank; @@ -121,71 +121,79 @@ void ed64_ll_set_save_type(ed64_save_type_t type) { } -void ed64_ll_set_sram_bank(uint8_t bank) { +void ed64_ll_set_sram_bank (uint8_t bank) { ed64_ll_sram_bank = bank == 0 ? 0 : 1; } -void pi_initialize(void) { +void pi_initialize (void) { + dma_wait(); io_write(PI_STATUS_REG, 0x03); + } // Inits PI for sram transfer -void pi_initialize_sram(void) { - +void pi_initialize_sram (void) { + io_write(PI_BSD_DOM2_LAT_REG, 0x05); io_write(PI_BSD_DOM2_PWD_REG, 0x0C); io_write(PI_BSD_DOM2_PGS_REG, 0x0D); io_write(PI_BSD_DOM2_RLS_REG, 0x02); - + } -void pi_dma_from_sram(void *dest, unsigned long offset, unsigned long size) { - +void pi_dma_from_sram (void *dest, unsigned long offset, unsigned long size) { io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); asm volatile ("" : : : "memory"); io_write(PI_WR_LEN_REG, (size - 1)); asm volatile ("" : : : "memory"); - + } -void pi_dma_to_sram(void *src, unsigned long offset, unsigned long size) { //void* +void pi_dma_to_sram (void *src, unsigned long offset, unsigned long size) { + dma_wait(); io_write(PI_STATUS_REG, 2); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); io_write(PI_CART_ADDR_REG, (0xA8000000 + offset)); io_write(PI_RD_LEN_REG, (size - 1)); + } -void pi_dma_from_cart(void* dest, void* src, unsigned long size) { +void pi_dma_from_cart (void* dest, void* src, unsigned long size) { + dma_wait(); io_write(PI_STATUS_REG, 0x03); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(dest)); io_write(PI_CART_ADDR_REG, K0_TO_PHYS(src)); io_write(PI_WR_LEN_REG, (size - 1)); + } -void pi_dma_to_cart(void* dest, void* src, unsigned long size) { +void pi_dma_to_cart (void* dest, void* src, unsigned long size) { + dma_wait(); io_write(PI_STATUS_REG, 0x02); io_write(PI_DRAM_ADDR_REG, K1_TO_PHYS(src)); io_write(PI_CART_ADDR_REG, K0_TO_PHYS(dest)); io_write(PI_RD_LEN_REG, (size - 1)); + } // Wrapper to support unaligned access to memory -void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size) { +void pi_dma_from_cart_safe (void *dest, void *src, unsigned long size) { + if (!dest || !src || !size) return; unsigned long unalignedSrc = ((unsigned long)src) % 2; @@ -209,10 +217,12 @@ void pi_dma_from_cart_safe(void *dest, void *src, unsigned long size) { memcpy(dest, (buffer + unalignedSrc), size); free(buffer); + } -int ed64_ll_get_sram( uint8_t *buffer, int size){ +int ed64_ll_get_sram (uint8_t *buffer, int size) { + dma_wait(); io_write(PI_BSD_DOM2_LAT_REG, 0x05); @@ -236,19 +246,23 @@ int ed64_ll_get_sram( uint8_t *buffer, int size){ io_write(PI_BSD_DOM2_RLS_REG, 0x03); return 1; + } -int ed64_ll_get_eeprom( uint8_t *buffer, int size){ +int ed64_ll_get_eeprom (uint8_t *buffer, int size) { + int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_read( b, &buffer[b * 8] ); } return 1; + } -int ed64_ll_get_fram( uint8_t *buffer, int size){ +int ed64_ll_get_fram (uint8_t *buffer, int size) { + ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 dma_wait(); @@ -259,13 +273,15 @@ int ed64_ll_get_fram( uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); return 1; + } /* sram upload */ -int ed64_ll_set_sram( uint8_t *buffer, int size){ - //half working +int ed64_ll_set_sram (uint8_t *buffer, int size) { + + //half working dma_wait(); //Timing pi_initialize_sram(); @@ -285,19 +301,23 @@ int ed64_ll_set_sram( uint8_t *buffer, int size){ ed64_ll_set_sdcard_timing(); return 1; + } -int ed64_ll_set_eeprom(uint8_t *buffer, int size){ +int ed64_ll_set_eeprom (uint8_t *buffer, int size) { + int blocks=size/8; for( int b = 0; b < blocks; b++ ) { eeprom_write( b, &buffer[b * 8] ); } return 1; + } -int ed64_ll_set_fram(uint8_t *buffer, int size){ +int ed64_ll_set_fram (uint8_t *buffer, int size) { + ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); dma_wait(); @@ -308,10 +328,11 @@ int ed64_ll_set_fram(uint8_t *buffer, int size){ ed64_ll_set_save_type(SAVE_TYPE_FLASHRAM); return 1; + } -void ed64_ll_set_sdcard_timing(void){ +void ed64_ll_set_sdcard_timing (void) { io_write(PI_BSD_DOM1_LAT_REG, 0x40); io_write(PI_BSD_DOM1_PWD_REG, 0x12); @@ -322,4 +343,5 @@ void ed64_ll_set_sdcard_timing(void){ io_write(PI_BSD_DOM2_PWD_REG, 0x12); io_write(PI_BSD_DOM2_PGS_REG, 0x07); io_write(PI_BSD_DOM2_RLS_REG, 0x03); + } diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 5fd38ad4..2d1c84e8 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -65,17 +65,17 @@ typedef enum { #define ROM_ADDRESS (0xB0000000) /* Save functions */ -void ed64_ll_set_ram_bank(uint8_t bank); -ed64_save_type_t ed64_ll_get_save_type(); -void ed64_ll_set_save_type(ed64_save_type_t type); +void ed64_ll_set_sram_bank (uint8_t bank); +ed64_save_type_t ed64_ll_get_save_type (); +void ed64_ll_set_save_type (ed64_save_type_t type); -int ed64_ll_get_sram( uint8_t *buffer, int size); -int ed64_ll_get_eeprom( uint8_t *buffer, int size); -int ed64_ll_get_fram( uint8_t *buffer, int size); +int ed64_ll_get_sram (uint8_t *buffer, int size); +int ed64_ll_get_eeprom (uint8_t *buffer, int size); +int ed64_ll_get_fram (uint8_t *buffer, int size); -int ed64_ll_set_sram( uint8_t *buffer, int size); -int ed64_ll_set_eeprom( uint8_t *buffer, int size); -int ed64_ll_set_fram( uint8_t *buffer, int size); +int ed64_ll_set_sram (uint8_t *buffer, int size); +int ed64_ll_set_eeprom (uint8_t *buffer, int size); +int ed64_ll_set_fram (uint8_t *buffer, int size); /** @} */ /* ed64 */ #endif From 7b4a675193dfb4d3b375319709cc68e0f0b9d889 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 23:52:27 +0100 Subject: [PATCH 26/38] Add state ini (not used yet) prefix temp files with ed_ for the moment. --- src/flashcart/ed64/ed64.c | 10 ++++---- src/flashcart/ed64/ed64_state.c | 42 +++++++++++++++++++++++++++++++++ src/flashcart/ed64/ed64_state.h | 26 ++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/flashcart/ed64/ed64_state.c create mode 100644 src/flashcart/ed64/ed64_state.h diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 11a9c309..dc8f801e 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -12,18 +12,18 @@ #include "../flashcart_utils.h" #include "ed64_ll.h" #include "ed64.h" +#include "ed64_state.h" -// #include "../menu/settings.h" -// This is a trial hack before using the settings API. +// FIXME: Use one file using the ed64_pseudo_writeback_t struct. #ifndef LAST_SAVE_FILE_PATH -#define LAST_SAVE_FILE_PATH "/menu/last_rom.tmp" +#define LAST_SAVE_FILE_PATH "/menu/ed_last_rom.tmp" #endif #ifndef RESET_CHECK_FILE_PATH -#define RESET_CHECK_FILE_PATH "/menu/reset.tmp" +#define RESET_CHECK_FILE_PATH "/menu/ed_reset.tmp" #endif #ifndef FLASHRAM_CHECK_FILE_PATH -#define FLASHRAM_CHECK_FILE_PATH "/menu/flashram.tmp" +#define FLASHRAM_CHECK_FILE_PATH "/menu/ed_flashram.tmp" #endif extern int ed_exit (void); diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c new file mode 100644 index 00000000..57a5f5ab --- /dev/null +++ b/src/flashcart/ed64/ed64_state.c @@ -0,0 +1,42 @@ +#include +#include + +#include "ed64_state.h" +#include "utils/fs.h" + +#ifndef ED64_STATE_FILE_PATH +#define ED64_STATE_FILE_PATH "sd:/menu/ed64_state.ini" +#endif + +static ed64_pseudo_writeback_t init = { + .is_warm_start = false, + .last_rom_save_type = false, + .last_rom_path = "" +}; + + +void ed64_state_load (ed64_pseudo_writeback_t *state) { + if (!file_exists(ED64_STATE_FILE_PATH)) { + ed64_state_save(&init); + } + + mini_t *ini = mini_try_load(ED64_STATE_FILE_PATH); + + state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); + state->last_rom_save_type = mini_get_bool(ini, "ed64", "last_rom_save_type", init.last_rom_save_type); + state->last_rom_path = strdup(mini_get_string(ini, "ed64", "last_rom_path", init.last_rom_path)); + + mini_free(ini); +} + +void ed64_state_save (ed64_pseudo_writeback_t *state) { + mini_t *ini = mini_create(ED64_STATE_FILE_PATH); + + mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); + mini_set_bool(ini, "ed64", "last_rom_save_type", state->last_rom_save_type); + mini_set_string(ini, "ed64", "last_rom_path", state->last_rom_path); + + mini_save(ini); + + mini_free(ini); +} diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h new file mode 100644 index 00000000..7cd6d768 --- /dev/null +++ b/src/flashcart/ed64/ed64_state.h @@ -0,0 +1,26 @@ +/** + * @file settings.h + * @brief Menu Settings + * @ingroup menu + */ + +#ifndef FLASHCART_ED64_STATE_H__ +#define LASHCART_ED64_STATE_H__ + +#include + +/** @brief ed64 pseudo Writeback Structure */ +typedef struct { +/** @brief The reset button was used */ + bool is_warm_start; +/** @brief The last save type used */ + bool last_rom_save_type; // FIXME: for the moment only a bool for flashram +/** @brief The path to the last loaded ROM */ + char *last_rom_path; +} ed64_pseudo_writeback_t; + +void ed64_state_load (ed64_pseudo_writeback_t *state); +void ed64_state_save (ed64_pseudo_writeback_t *state); + + +#endif \ No newline at end of file From ff218daa09cafa92593fe1cc59b23476a3340ee2 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 00:40:02 +0100 Subject: [PATCH 27/38] Move to struct for reset and fram state. --- Makefile | 1 + src/flashcart/ed64/ed64.c | 47 +++++++++++---------------------- src/flashcart/ed64/ed64_state.c | 6 ++--- src/flashcart/ed64/ed64_state.h | 8 +++--- 4 files changed, 23 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index b8a84b61..daad90af 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ SRCS = \ flashcart/sc64/sc64_ll.c \ flashcart/sc64/sc64.c \ flashcart/ed64/ed64_ll.c \ + flashcart/ed64/ed64_state.c \ flashcart/ed64/ed64.c \ libs/libspng/spng/spng.c \ libs/mini.c/src/mini.c \ diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index dc8f801e..e9022a31 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -15,16 +15,12 @@ #include "ed64_state.h" +static ed64_pseudo_writeback_t current_state; + // FIXME: Use one file using the ed64_pseudo_writeback_t struct. #ifndef LAST_SAVE_FILE_PATH #define LAST_SAVE_FILE_PATH "/menu/ed_last_rom.tmp" #endif -#ifndef RESET_CHECK_FILE_PATH -#define RESET_CHECK_FILE_PATH "/menu/ed_reset.tmp" -#endif -#ifndef FLASHRAM_CHECK_FILE_PATH -#define FLASHRAM_CHECK_FILE_PATH "/menu/ed_flashram.tmp" -#endif extern int ed_exit (void); @@ -38,10 +34,13 @@ 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. - if (file_exists(strip_sd_prefix(RESET_CHECK_FILE_PATH))) { + ed64_state_load(¤t_state); - // make sure next boot doesnt trigger the check by deleting the reset file - f_unlink(strip_sd_prefix(RESET_CHECK_FILE_PATH)); + if (current_state.is_warm_start == true) { + + // make sure next boot doesnt trigger the check changing its state. + current_state.is_warm_start = false; + ed64_state_save(¤t_state); // finds the last save location FIL lrp_fil; @@ -80,10 +79,11 @@ static flashcart_err_t ed64_init (void) { // 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 + if (current_state.is_fram_save_type == true) { // flashram is bugged atm ed64_ll_get_fram(cartsave_data, save_size); // deletes flag - f_unlink(strip_sd_prefix(FLASHRAM_CHECK_FILE_PATH)); + current_state.is_fram_save_type = false; + ed64_state_save(¤t_state); } else if (save_size > KiB(2)) { // sram ed64_ll_get_sram(cartsave_data, save_size); @@ -250,15 +250,8 @@ static flashcart_err_t ed64_load_save (char *save_path) { 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 - 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; - } + current_state.is_fram_save_type = true; + ed64_state_save(¤t_state); break; default: break; @@ -280,18 +273,8 @@ static flashcart_err_t ed64_load_save (char *save_path) { return FLASHCART_ERR_LOAD; } - FIL rsfil; - - // simulate a unix touch command to create a file as it only needs to exist to detect a reset - - if (f_open(&rsfil, strip_sd_prefix(RESET_CHECK_FILE_PATH), FA_CREATE_ALWAYS) != FR_OK) { - f_close(&rsfil); - return FLASHCART_ERR_LOAD; - } - - if (f_close(&rsfil) != FR_OK) { - return FLASHCART_OK; - } + current_state.is_warm_start = true; + ed64_state_save(¤t_state); return FLASHCART_OK; } diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c index 57a5f5ab..392f677f 100644 --- a/src/flashcart/ed64/ed64_state.c +++ b/src/flashcart/ed64/ed64_state.c @@ -10,7 +10,7 @@ static ed64_pseudo_writeback_t init = { .is_warm_start = false, - .last_rom_save_type = false, + .is_fram_save_type = false, .last_rom_path = "" }; @@ -23,7 +23,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_try_load(ED64_STATE_FILE_PATH); state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); - state->last_rom_save_type = mini_get_bool(ini, "ed64", "last_rom_save_type", init.last_rom_save_type); + state->is_fram_save_type = mini_get_bool(ini, "ed64", "is_fram_save_type", init.is_fram_save_type); state->last_rom_path = strdup(mini_get_string(ini, "ed64", "last_rom_path", init.last_rom_path)); mini_free(ini); @@ -33,7 +33,7 @@ void ed64_state_save (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_create(ED64_STATE_FILE_PATH); mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); - mini_set_bool(ini, "ed64", "last_rom_save_type", state->last_rom_save_type); + mini_set_bool(ini, "ed64", "is_fram_save_type", state->is_fram_save_type); mini_set_string(ini, "ed64", "last_rom_path", state->last_rom_path); mini_save(ini); diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index 7cd6d768..5adbd149 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -5,16 +5,16 @@ */ #ifndef FLASHCART_ED64_STATE_H__ -#define LASHCART_ED64_STATE_H__ +#define FLASHCART_ED64_STATE_H__ #include -/** @brief ed64 pseudo Writeback Structure */ +/** @brief ED64 Pseudo Writeback Structure */ typedef struct { /** @brief The reset button was used */ bool is_warm_start; -/** @brief The last save type used */ - bool last_rom_save_type; // FIXME: for the moment only a bool for flashram +/** @brief The last save type was flash ram */ + bool is_fram_save_type; /** @brief The path to the last loaded ROM */ char *last_rom_path; } ed64_pseudo_writeback_t; From 1daa664fe0dab99060a2f169f115d57715e50dab Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 01:11:37 +0100 Subject: [PATCH 28/38] Minor comment improvements --- src/flashcart/ed64/ed64_state.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index 5adbd149..2ea719e0 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -1,14 +1,12 @@ /** - * @file settings.h - * @brief Menu Settings - * @ingroup menu + * @file e664_state.h + * @brief ED64 state + * @ingroup flashcart */ #ifndef FLASHCART_ED64_STATE_H__ #define FLASHCART_ED64_STATE_H__ -#include - /** @brief ED64 Pseudo Writeback Structure */ typedef struct { /** @brief The reset button was used */ From d677712b51e6d8c5d95d7d115aa1c114b6931777 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 01:57:50 +0100 Subject: [PATCH 29/38] Finish state conversion for last save path (hopefully not broken). --- src/flashcart/ed64/ed64.c | 56 ++++++--------------------------- src/flashcart/ed64/ed64_state.c | 6 ++-- src/flashcart/ed64/ed64_state.h | 2 +- 3 files changed, 14 insertions(+), 50 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index e9022a31..89df5e71 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -17,11 +17,6 @@ static ed64_pseudo_writeback_t current_state; -// FIXME: Use one file using the ed64_pseudo_writeback_t struct. -#ifndef LAST_SAVE_FILE_PATH -#define LAST_SAVE_FILE_PATH "/menu/ed_last_rom.tmp" -#endif - extern int ed_exit (void); static flashcart_err_t ed64_init (void) { @@ -42,38 +37,17 @@ static flashcart_err_t ed64_init (void) { current_state.is_warm_start = false; ed64_state_save(¤t_state); - // finds the last save location - FIL lrp_fil; - UINT lrp_br; - - if (f_open(&lrp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_READ) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - - int lrp_size = f_size(&lrp_fil); - - TCHAR lrp_path[lrp_size++]; - - if (f_read(&lrp_fil, lrp_path, lrp_size, &lrp_br) != FR_OK) { - f_close(&lrp_fil); - return FLASHCART_ERR_LOAD; - } - - if (f_close(&lrp_fil) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - // Now save the content back to the SD card! FIL fil; - UINT br; + UINT bw; uint8_t cartsave_data[KiB(128)]; // find the path to last save - if (file_exists(strip_sd_prefix(lrp_path))) { + if (file_exists(strip_sd_prefix(current_state.last_save_path))) { - int save_size = file_get_size(strip_sd_prefix(lrp_path)); + int save_size = file_get_size(strip_sd_prefix(current_state.last_save_path)); - if ((f_open(&fil, strip_sd_prefix(lrp_path), FA_CREATE_ALWAYS | FA_READ | FA_WRITE)) != FR_OK) { + 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; } @@ -92,7 +66,7 @@ static flashcart_err_t ed64_init (void) { ed64_ll_get_eeprom(cartsave_data, save_size); } - if (f_write(&fil, cartsave_data, save_size, &br) != FR_OK) { + if (f_write(&fil, cartsave_data, save_size, &bw) != FR_OK) { return FLASHCART_ERR_LOAD; } @@ -100,6 +74,10 @@ static flashcart_err_t ed64_init (void) { return FLASHCART_ERR_LOAD; } } + else { + current_state.last_save_path = ""; + ed64_state_save(¤t_state); + } } return FLASHCART_OK; } @@ -258,21 +236,7 @@ static flashcart_err_t ed64_load_save (char *save_path) { } - FIL lsp_fil; - UINT lsp_bw; - - if (f_open(&lsp_fil, strip_sd_prefix(LAST_SAVE_FILE_PATH), FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - 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; - } - - if (f_close(&lsp_fil) != FR_OK) { - return FLASHCART_ERR_LOAD; - } - + current_state.last_save_path = save_path; current_state.is_warm_start = true; ed64_state_save(¤t_state); diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c index 392f677f..39dcc640 100644 --- a/src/flashcart/ed64/ed64_state.c +++ b/src/flashcart/ed64/ed64_state.c @@ -11,7 +11,7 @@ static ed64_pseudo_writeback_t init = { .is_warm_start = false, .is_fram_save_type = false, - .last_rom_path = "" + .last_save_path = "" }; @@ -24,7 +24,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); state->is_fram_save_type = mini_get_bool(ini, "ed64", "is_fram_save_type", init.is_fram_save_type); - state->last_rom_path = strdup(mini_get_string(ini, "ed64", "last_rom_path", init.last_rom_path)); + state->last_save_path = strdup(mini_get_string(ini, "ed64", "last_save_path", init.last_save_path)); mini_free(ini); } @@ -34,7 +34,7 @@ void ed64_state_save (ed64_pseudo_writeback_t *state) { mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); mini_set_bool(ini, "ed64", "is_fram_save_type", state->is_fram_save_type); - mini_set_string(ini, "ed64", "last_rom_path", state->last_rom_path); + mini_set_string(ini, "ed64", "last_save_path", state->last_save_path); mini_save(ini); diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index 2ea719e0..ee4b20bd 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -14,7 +14,7 @@ typedef struct { /** @brief The last save type was flash ram */ bool is_fram_save_type; /** @brief The path to the last loaded ROM */ - char *last_rom_path; + char *last_save_path; } ed64_pseudo_writeback_t; void ed64_state_load (ed64_pseudo_writeback_t *state); From a5d9f7d069e8f3e15bbfc7a8a005a09f25b24f0a Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 21 Oct 2023 02:11:13 +0100 Subject: [PATCH 30/38] Improve error conditions --- src/flashcart/ed64/ed64.c | 10 +++++++--- src/flashcart/ed64/ed64_state.c | 6 +++--- src/flashcart/ed64/ed64_state.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 89df5e71..b3de5823 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -31,10 +31,10 @@ static flashcart_err_t ed64_init (void) { ed64_state_load(¤t_state); - if (current_state.is_warm_start == true) { + if (current_state.is_expecting_save_writeback == true) { // make sure next boot doesnt trigger the check changing its state. - current_state.is_warm_start = false; + current_state.is_expecting_save_writeback = false; ed64_state_save(¤t_state); // Now save the content back to the SD card! @@ -75,6 +75,8 @@ static flashcart_err_t ed64_init (void) { } } else { + current_state.is_expecting_save_writeback = false; + current_state.is_fram_save_type = false; current_state.last_save_path = ""; ed64_state_save(¤t_state); } @@ -214,6 +216,8 @@ static flashcart_err_t ed64_load_save (char *save_path) { return FLASHCART_ERR_LOAD; } + current_state.is_fram_save_type = false; + ed64_save_type_t type = ed64_ll_get_save_type(); switch (type) { case SAVE_TYPE_EEPROM_4K: @@ -237,7 +241,7 @@ static flashcart_err_t ed64_load_save (char *save_path) { current_state.last_save_path = save_path; - current_state.is_warm_start = true; + current_state.is_expecting_save_writeback = true; ed64_state_save(¤t_state); return FLASHCART_OK; diff --git a/src/flashcart/ed64/ed64_state.c b/src/flashcart/ed64/ed64_state.c index 39dcc640..ee29bab7 100644 --- a/src/flashcart/ed64/ed64_state.c +++ b/src/flashcart/ed64/ed64_state.c @@ -9,7 +9,7 @@ #endif static ed64_pseudo_writeback_t init = { - .is_warm_start = false, + .is_expecting_save_writeback = false, .is_fram_save_type = false, .last_save_path = "" }; @@ -22,7 +22,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_try_load(ED64_STATE_FILE_PATH); - state->is_warm_start = mini_get_bool(ini, "ed64", "is_warm_start", init.is_warm_start); + state->is_expecting_save_writeback = mini_get_bool(ini, "ed64", "is_expecting_save_writeback", init.is_expecting_save_writeback); state->is_fram_save_type = mini_get_bool(ini, "ed64", "is_fram_save_type", init.is_fram_save_type); state->last_save_path = strdup(mini_get_string(ini, "ed64", "last_save_path", init.last_save_path)); @@ -32,7 +32,7 @@ void ed64_state_load (ed64_pseudo_writeback_t *state) { void ed64_state_save (ed64_pseudo_writeback_t *state) { mini_t *ini = mini_create(ED64_STATE_FILE_PATH); - mini_set_bool(ini, "ed64", "is_warm_start", state->is_warm_start); + mini_set_bool(ini, "ed64", "is_expecting_save_writeback", state->is_expecting_save_writeback); mini_set_bool(ini, "ed64", "is_fram_save_type", state->is_fram_save_type); mini_set_string(ini, "ed64", "last_save_path", state->last_save_path); diff --git a/src/flashcart/ed64/ed64_state.h b/src/flashcart/ed64/ed64_state.h index ee4b20bd..3f53a148 100644 --- a/src/flashcart/ed64/ed64_state.h +++ b/src/flashcart/ed64/ed64_state.h @@ -10,7 +10,7 @@ /** @brief ED64 Pseudo Writeback Structure */ typedef struct { /** @brief The reset button was used */ - bool is_warm_start; + bool is_expecting_save_writeback; /** @brief The last save type was flash ram */ bool is_fram_save_type; /** @brief The path to the last loaded ROM */ From 3c2daf7277c56373327be4b62c0395d7a258f739 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 21 Oct 2023 02:28:26 -0400 Subject: [PATCH 31/38] fix large saves --- src/flashcart/ed64/ed64.c | 2 ++ src/flashcart/ed64/ed64_ll.c | 58 ++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index b3de5823..94006a02 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -80,6 +80,8 @@ static flashcart_err_t ed64_init (void) { current_state.last_save_path = ""; ed64_state_save(¤t_state); } + //clears rom/save sdram area + memset((void *)(ROM_ADDRESS), 0xFF, MiB(64) + KiB(128)); } return FLASHCART_OK; } diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 5b04eed5..973fca9d 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -1,5 +1,6 @@ #include #include +#include "utils/utils.h" #include "ed64_ll.h" @@ -220,6 +221,33 @@ void pi_dma_from_cart_safe (void *dest, void *src, unsigned long size) { } +int ed64_ll_get_sram128 (uint8_t *buffer, int size) { + + dma_wait(); + + io_write(PI_BSD_DOM2_LAT_REG, 0x05); + io_write(PI_BSD_DOM2_PWD_REG, 0x0C); + io_write(PI_BSD_DOM2_PGS_REG, 0x0D); + io_write(PI_BSD_DOM2_RLS_REG, 0x02); + + dma_wait(); + + pi_initialize(); + + dma_wait(); + + pi_dma_from_sram(buffer, 0 - (KiB(128) - KiB(32)), size) ; + + dma_wait(); + + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); + + return 1; + +} int ed64_ll_get_sram (uint8_t *buffer, int size) { @@ -266,7 +294,7 @@ int ed64_ll_get_fram (uint8_t *buffer, int size) { ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 dma_wait(); - ed64_ll_get_sram(buffer, size); + ed64_ll_get_sram128(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); @@ -279,6 +307,32 @@ int ed64_ll_get_fram (uint8_t *buffer, int size) { /* sram upload */ + +int ed64_ll_set_sram128 (uint8_t *buffer, int size) { + + //half working + dma_wait(); + //Timing + pi_initialize_sram(); + + //Readmode + pi_initialize(); + + data_cache_hit_writeback_invalidate(buffer,size); + dma_wait(); + + pi_dma_to_sram(buffer, 0 - (KiB(128) - KiB(32)), size); + data_cache_hit_writeback_invalidate(buffer,size); + + //Wait + dma_wait(); + //Restore evd Timing + ed64_ll_set_sdcard_timing(); + + return 1; + +} + int ed64_ll_set_sram (uint8_t *buffer, int size) { //half working @@ -321,7 +375,7 @@ int ed64_ll_set_fram (uint8_t *buffer, int size) { ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); dma_wait(); - ed64_ll_set_sram(buffer, size); + ed64_ll_set_sram128(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); From eb9fecb1154901861b4f270b196bf572d9ebaf3d Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 21 Oct 2023 02:46:54 -0400 Subject: [PATCH 32/38] calculate fram size without seperate function --- src/flashcart/ed64/ed64_ll.c | 59 +++--------------------------------- 1 file changed, 4 insertions(+), 55 deletions(-) diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 973fca9d..d565e8af 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -221,33 +221,6 @@ void pi_dma_from_cart_safe (void *dest, void *src, unsigned long size) { } -int ed64_ll_get_sram128 (uint8_t *buffer, int size) { - - dma_wait(); - - io_write(PI_BSD_DOM2_LAT_REG, 0x05); - io_write(PI_BSD_DOM2_PWD_REG, 0x0C); - io_write(PI_BSD_DOM2_PGS_REG, 0x0D); - io_write(PI_BSD_DOM2_RLS_REG, 0x02); - - dma_wait(); - - pi_initialize(); - - dma_wait(); - - pi_dma_from_sram(buffer, 0 - (KiB(128) - KiB(32)), size) ; - - dma_wait(); - - io_write(PI_BSD_DOM2_LAT_REG, 0x40); - io_write(PI_BSD_DOM2_PWD_REG, 0x12); - io_write(PI_BSD_DOM2_PGS_REG, 0x07); - io_write(PI_BSD_DOM2_RLS_REG, 0x03); - - return 1; - -} int ed64_ll_get_sram (uint8_t *buffer, int size) { @@ -264,7 +237,7 @@ int ed64_ll_get_sram (uint8_t *buffer, int size) { dma_wait(); - pi_dma_from_sram(buffer, 0, size) ; + pi_dma_from_sram(buffer, 0 - (KiB(32) - size), size) ; dma_wait(); @@ -294,7 +267,7 @@ int ed64_ll_get_fram (uint8_t *buffer, int size) { ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 dma_wait(); - ed64_ll_get_sram128(buffer, size); + ed64_ll_get_sram(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); @@ -308,30 +281,6 @@ int ed64_ll_get_fram (uint8_t *buffer, int size) { sram upload */ -int ed64_ll_set_sram128 (uint8_t *buffer, int size) { - - //half working - dma_wait(); - //Timing - pi_initialize_sram(); - - //Readmode - pi_initialize(); - - data_cache_hit_writeback_invalidate(buffer,size); - dma_wait(); - - pi_dma_to_sram(buffer, 0 - (KiB(128) - KiB(32)), size); - data_cache_hit_writeback_invalidate(buffer,size); - - //Wait - dma_wait(); - //Restore evd Timing - ed64_ll_set_sdcard_timing(); - - return 1; - -} int ed64_ll_set_sram (uint8_t *buffer, int size) { @@ -346,7 +295,7 @@ int ed64_ll_set_sram (uint8_t *buffer, int size) { data_cache_hit_writeback_invalidate(buffer,size); dma_wait(); - pi_dma_to_sram(buffer, 0, size); + pi_dma_to_sram(buffer, 0 - (KiB(32) - size), size); data_cache_hit_writeback_invalidate(buffer,size); //Wait @@ -375,7 +324,7 @@ int ed64_ll_set_fram (uint8_t *buffer, int size) { ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); dma_wait(); - ed64_ll_set_sram128(buffer, size); + ed64_ll_set_sram(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); From ea1c7dbead79f569eaae38ea17218b609716ff32 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 21 Oct 2023 03:19:49 -0400 Subject: [PATCH 33/38] fix other saves --- src/flashcart/ed64/ed64.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 94006a02..7338786b 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -125,7 +125,17 @@ static flashcart_err_t ed64_load_rom (char *rom_path, flashcart_progress_callbac } if (rom_size == MiB(64)) { - rom_size -= KiB(128); + ed64_save_type_t type = ed64_ll_get_save_type(); + switch (type) { + case SAVE_TYPE_SRAM: + rom_size -= KiB(32); + case SAVE_TYPE_SRAM_128K: + case SAVE_TYPE_FLASHRAM: + rom_size -= KiB(128); + break; + default: + break; + } } size_t sdram_size = rom_size; From ff9b961cd3a4f7dee647b156362daed9735b21bc Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 21 Oct 2023 04:13:26 -0400 Subject: [PATCH 34/38] Dinosaur planet 100% working --- src/flashcart/ed64/ed64_ll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index d565e8af..8ab3fe28 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -237,7 +237,7 @@ int ed64_ll_get_sram (uint8_t *buffer, int size) { dma_wait(); - pi_dma_from_sram(buffer, 0 - (KiB(32) - size), size) ; + pi_dma_from_sram(buffer, 0 - (KiB(64) - size), size) ; dma_wait(); @@ -295,7 +295,7 @@ int ed64_ll_set_sram (uint8_t *buffer, int size) { data_cache_hit_writeback_invalidate(buffer,size); dma_wait(); - pi_dma_to_sram(buffer, 0 - (KiB(32) - size), size); + pi_dma_to_sram(buffer, 0 - (KiB(64) - size), size); data_cache_hit_writeback_invalidate(buffer,size); //Wait From 811c9e3a3284265544a46a31720595e12c7ca97e Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 21 Oct 2023 04:32:38 -0400 Subject: [PATCH 35/38] fix other saves 2 --- src/flashcart/ed64/ed64.c | 6 +++- src/flashcart/ed64/ed64_ll.c | 63 +++++++++++++++++++++++++++++++++--- src/flashcart/ed64/ed64_ll.h | 2 ++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 7338786b..049058eb 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -59,6 +59,9 @@ static flashcart_err_t ed64_init (void) { current_state.is_fram_save_type = false; ed64_state_save(¤t_state); } + else if (save_size > KiB(32)) { // sram 128 + ed64_ll_get_sram(cartsave_data, save_size); + } else if (save_size > KiB(2)) { // sram ed64_ll_get_sram(cartsave_data, save_size); } @@ -237,8 +240,9 @@ static flashcart_err_t ed64_load_save (char *save_path) { ed64_ll_set_eeprom(cartsave_data, save_size); break; case SAVE_TYPE_SRAM: - case SAVE_TYPE_SRAM_128K: ed64_ll_set_sram(cartsave_data, save_size); + case SAVE_TYPE_SRAM_128K: + ed64_ll_set_sram_128(cartsave_data, save_size); break; case SAVE_TYPE_FLASHRAM: ed64_ll_set_fram(cartsave_data, save_size); diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 8ab3fe28..2c00d731 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -222,7 +222,7 @@ void pi_dma_from_cart_safe (void *dest, void *src, unsigned long size) { } -int ed64_ll_get_sram (uint8_t *buffer, int size) { +int ed64_ll_get_sram_128 (uint8_t *buffer, int size) { dma_wait(); @@ -250,6 +250,35 @@ int ed64_ll_get_sram (uint8_t *buffer, int size) { } + +int ed64_ll_get_sram (uint8_t *buffer, int size) { + + dma_wait(); + + io_write(PI_BSD_DOM2_LAT_REG, 0x05); + io_write(PI_BSD_DOM2_PWD_REG, 0x0C); + io_write(PI_BSD_DOM2_PGS_REG, 0x0D); + io_write(PI_BSD_DOM2_RLS_REG, 0x02); + + dma_wait(); + + pi_initialize(); + + dma_wait(); + + pi_dma_from_sram(buffer, 0, size) ; + + dma_wait(); + + io_write(PI_BSD_DOM2_LAT_REG, 0x40); + io_write(PI_BSD_DOM2_PWD_REG, 0x12); + io_write(PI_BSD_DOM2_PGS_REG, 0x07); + io_write(PI_BSD_DOM2_RLS_REG, 0x03); + + return 1; + +} + int ed64_ll_get_eeprom (uint8_t *buffer, int size) { int blocks=size/8; @@ -267,7 +296,7 @@ int ed64_ll_get_fram (uint8_t *buffer, int size) { ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); //2 dma_wait(); - ed64_ll_get_sram(buffer, size); + ed64_ll_get_sram_128(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); @@ -282,6 +311,32 @@ sram upload */ +int ed64_ll_set_sram_128 (uint8_t *buffer, int size) { + + //half working + dma_wait(); + //Timing + pi_initialize_sram(); + + //Readmode + pi_initialize(); + + data_cache_hit_writeback_invalidate(buffer,size); + dma_wait(); + + pi_dma_to_sram(buffer, 0 - (KiB(64) - size), size); + data_cache_hit_writeback_invalidate(buffer,size); + + //Wait + dma_wait(); + //Restore evd Timing + ed64_ll_set_sdcard_timing(); + + return 1; + +} + + int ed64_ll_set_sram (uint8_t *buffer, int size) { //half working @@ -295,7 +350,7 @@ int ed64_ll_set_sram (uint8_t *buffer, int size) { data_cache_hit_writeback_invalidate(buffer,size); dma_wait(); - pi_dma_to_sram(buffer, 0 - (KiB(64) - size), size); + pi_dma_to_sram(buffer, 0, size); data_cache_hit_writeback_invalidate(buffer,size); //Wait @@ -324,7 +379,7 @@ int ed64_ll_set_fram (uint8_t *buffer, int size) { ed64_ll_set_save_type(SAVE_TYPE_SRAM_128K); dma_wait(); - ed64_ll_set_sram(buffer, size); + ed64_ll_set_sram_128(buffer, size); data_cache_hit_writeback_invalidate(buffer, size); dma_wait(); diff --git a/src/flashcart/ed64/ed64_ll.h b/src/flashcart/ed64/ed64_ll.h index 2d1c84e8..0f36618d 100644 --- a/src/flashcart/ed64/ed64_ll.h +++ b/src/flashcart/ed64/ed64_ll.h @@ -69,10 +69,12 @@ void ed64_ll_set_sram_bank (uint8_t bank); ed64_save_type_t ed64_ll_get_save_type (); void ed64_ll_set_save_type (ed64_save_type_t type); +int ed64_ll_get_sram_128 (uint8_t *buffer, int size); int ed64_ll_get_sram (uint8_t *buffer, int size); int ed64_ll_get_eeprom (uint8_t *buffer, int size); int ed64_ll_get_fram (uint8_t *buffer, int size); +int ed64_ll_set_sram_128 (uint8_t *buffer, int size); int ed64_ll_set_sram (uint8_t *buffer, int size); int ed64_ll_set_eeprom (uint8_t *buffer, int size); int ed64_ll_set_fram (uint8_t *buffer, int size); From a88e1f0b7e7101d9512d3c54c60d43962ae9f616 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 21 Oct 2023 04:58:09 -0400 Subject: [PATCH 36/38] get rid of unneded function --- src/flashcart/ed64/ed64.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 049058eb..4dabae7c 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -83,8 +83,6 @@ static flashcart_err_t ed64_init (void) { current_state.last_save_path = ""; ed64_state_save(¤t_state); } - //clears rom/save sdram area - memset((void *)(ROM_ADDRESS), 0xFF, MiB(64) + KiB(128)); } return FLASHCART_OK; } From 620a9e9ea1dcb1c7d13755edd9c72e51f6147f78 Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sat, 21 Oct 2023 07:20:25 -0400 Subject: [PATCH 37/38] make sure sram128 and fram are 128kb --- src/flashcart/ed64/ed64.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 4dabae7c..25936578 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -240,10 +240,10 @@ static flashcart_err_t ed64_load_save (char *save_path) { case SAVE_TYPE_SRAM: ed64_ll_set_sram(cartsave_data, save_size); case SAVE_TYPE_SRAM_128K: - ed64_ll_set_sram_128(cartsave_data, save_size); + ed64_ll_set_sram_128(cartsave_data, KiB(128)); break; case SAVE_TYPE_FLASHRAM: - ed64_ll_set_fram(cartsave_data, save_size); + ed64_ll_set_fram(cartsave_data, KiB(128)); // 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; From 7163cf7c2e48ad4889c192f06f5f58f5d9512ebc Mon Sep 17 00:00:00 2001 From: ariahiro64 Date: Sun, 22 Oct 2023 02:12:48 -0400 Subject: [PATCH 38/38] dino fix (cubavore still crashes) --- src/flashcart/ed64/ed64.c | 4 ++-- src/flashcart/ed64/ed64_ll.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/flashcart/ed64/ed64.c b/src/flashcart/ed64/ed64.c index 25936578..8d3abf89 100644 --- a/src/flashcart/ed64/ed64.c +++ b/src/flashcart/ed64/ed64.c @@ -129,10 +129,10 @@ static flashcart_err_t ed64_load_rom (char *rom_path, flashcart_progress_callbac ed64_save_type_t type = ed64_ll_get_save_type(); switch (type) { case SAVE_TYPE_SRAM: - rom_size -= KiB(32); + rom_size -= KiB(32) - KiB(16); case SAVE_TYPE_SRAM_128K: case SAVE_TYPE_FLASHRAM: - rom_size -= KiB(128); + rom_size -= KiB(128) - KiB(16); break; default: break; diff --git a/src/flashcart/ed64/ed64_ll.c b/src/flashcart/ed64/ed64_ll.c index 2c00d731..1dd5d7b9 100644 --- a/src/flashcart/ed64/ed64_ll.c +++ b/src/flashcart/ed64/ed64_ll.c @@ -237,7 +237,7 @@ int ed64_ll_get_sram_128 (uint8_t *buffer, int size) { dma_wait(); - pi_dma_from_sram(buffer, 0 - (KiB(64) - size), size) ; + pi_dma_from_sram(buffer, -(size - KiB(16)), size) ; dma_wait(); @@ -324,7 +324,7 @@ int ed64_ll_set_sram_128 (uint8_t *buffer, int size) { data_cache_hit_writeback_invalidate(buffer,size); dma_wait(); - pi_dma_to_sram(buffer, 0 - (KiB(64) - size), size); + pi_dma_to_sram(buffer, -(size - KiB(16)), size); data_cache_hit_writeback_invalidate(buffer,size); //Wait