diff --git a/src/debug.c b/src/debug.c index 6619d1a4ea..31cd66462a 100644 --- a/src/debug.c +++ b/src/debug.c @@ -140,6 +140,7 @@ typedef struct DSTATUS (*disk_initialize)(void); DSTATUS (*disk_status)(void); DRESULT (*disk_read)(BYTE* buff, LBA_t sector, UINT count); + DRESULT (*disk_read_sdram)(BYTE* buff, LBA_t sector, UINT count); DRESULT (*disk_write)(const BYTE* buff, LBA_t sector, UINT count); DRESULT (*disk_ioctl)(BYTE cmd, void* buff); } fat_disk_t; @@ -162,13 +163,19 @@ DSTATUS disk_status(BYTE pdrv) DRESULT disk_read(BYTE pdrv, BYTE* buff, LBA_t sector, UINT count) { - if (fat_disks[pdrv].disk_read) + _Static_assert(FF_MIN_SS == 512, "this function assumes sector size == 512"); + _Static_assert(FF_MAX_SS == 512, "this function assumes sector size == 512"); + if (fat_disks[pdrv].disk_read && PhysicalAddr(buff) < 0x00800000) return fat_disks[pdrv].disk_read(buff, sector, count); + if (fat_disks[pdrv].disk_read_sdram && io_accessible(PhysicalAddr(buff))) + return fat_disks[pdrv].disk_read_sdram(buff, sector, count); return RES_PARERR; } DRESULT disk_write(BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count) { + _Static_assert(FF_MIN_SS == 512, "this function assumes sector size == 512"); + _Static_assert(FF_MAX_SS == 512, "this function assumes sector size == 512"); if (fat_disks[pdrv].disk_write) return fat_disks[pdrv].disk_write(buff, sector, count); return RES_PARERR; @@ -203,6 +210,7 @@ static fat_disk_t fat_disk_everdrive = fat_disk_initialize_everdrive, fat_disk_status_default, fat_disk_read_everdrive, + NULL, fat_disk_write_everdrive, fat_disk_ioctl_default }; @@ -212,6 +220,7 @@ static fat_disk_t fat_disk_64drive = fat_disk_initialize_64drive, fat_disk_status_default, fat_disk_read_64drive, + fat_disk_read_sdram_64drive, fat_disk_write_64drive, fat_disk_ioctl_default }; @@ -221,6 +230,7 @@ static fat_disk_t fat_disk_sc64 = fat_disk_initialize_sc64, fat_disk_status_default, fat_disk_read_sc64, + fat_disk_read_sdram_sc64, fat_disk_write_sc64, fat_disk_ioctl_default }; diff --git a/src/debug_sdfs_64drive.c b/src/debug_sdfs_64drive.c index 8913482887..c22e87f5e7 100644 --- a/src/debug_sdfs_64drive.c +++ b/src/debug_sdfs_64drive.c @@ -4,6 +4,7 @@ #define D64_CIBASE_ADDRESS 0xB8000000 #define D64_BUFFER 0x00000000 +#define D64_REGISTER_SDRAM 0x00000004 #define D64_REGISTER_STATUS 0x00000200 #define D64_REGISTER_COMMAND 0x00000208 #define D64_REGISTER_LBA 0x00000210 @@ -23,11 +24,42 @@ extern int8_t usb_64drive_wait(void); extern void usb_64drive_setwritable(int8_t enable); -static DRESULT fat_disk_read_64drive(BYTE* buff, LBA_t sector, UINT count) +static void sd_abort_64drive(void) { - _Static_assert(FF_MIN_SS == 512, "this function assumes sector size == 512"); - _Static_assert(FF_MAX_SS == 512, "this function assumes sector size == 512"); + // Operation is taking too long. Probably SD was not inserted. + // Send a COMMAND_ABORT and SD_RESET, and return I/O error. + // Note that because of a 64drive firmware bug, this is not + // sufficient to unblock the 64drive. The USB channel will stay + // unresponsive. We don't currently have a workaround for this. + io_write(D64_CIBASE_ADDRESS + D64_REGISTER_COMMAND, D64_COMMAND_ABORT); + usb_64drive_wait(); + io_write(D64_CIBASE_ADDRESS + D64_REGISTER_COMMAND, D64_COMMAND_SD_RESET); + usb_64drive_wait(); +} + +static DRESULT fat_disk_read_sdram_64drive(BYTE* buff, LBA_t sector, UINT count) +{ + usb_64drive_wait(); + io_write(D64_CIBASE_ADDRESS + D64_REGISTER_LBA, sector); + usb_64drive_wait(); + io_write(D64_CIBASE_ADDRESS + D64_REGISTER_LENGTH, count); + usb_64drive_wait(); + io_write(D64_CIBASE_ADDRESS + D64_REGISTER_SDRAM, PhysicalAddr(buff) >> 1); + usb_64drive_wait(); + io_write(D64_CIBASE_ADDRESS + D64_REGISTER_COMMAND, D64_COMMAND_SD_READ); + if (usb_64drive_wait() != 0) + { + debugf("[debug] fat_disk_read_sdram_64drive: wait timeout\n"); + sd_abort_64drive(); + return FR_DISK_ERR; + } + return RES_OK; +} +static DRESULT fat_disk_read_64drive(BYTE* buff, LBA_t sector, UINT count) +{ + usb_64drive_wait(); + io_write(D64_CIBASE_ADDRESS + D64_REGISTER_LENGTH, 1); for (int i=0;i 0) { UINT sectors_to_process = MIN(count, SC64_BUFFER_SIZE/512); @@ -67,10 +65,15 @@ static DRESULT fat_disk_read_sc64(BYTE* buff, LBA_t sector, UINT count) return RES_OK; } +static DRESULT fat_disk_read_sdram_sc64(BYTE* buff, LBA_t sector, UINT count) +{ + if (sc64_sd_read_sectors((uint32_t)buff, sector, count)) + return FR_DISK_ERR; + return RES_OK; +} + static DRESULT fat_disk_write_sc64(const BYTE* buff, LBA_t sector, UINT count) { - _Static_assert(FF_MIN_SS == 512, "this function assumes sector size == 512"); - _Static_assert(FF_MAX_SS == 512, "this function assumes sector size == 512"); while (count > 0) { UINT sectors_to_process = MIN(count, SC64_BUFFER_SIZE/512);