From a186921377df0eb1bd4fe0a0bf36adbfb2357788 Mon Sep 17 00:00:00 2001 From: Yanis002 <35189056+Yanis002@users.noreply.github.com> Date: Thu, 15 Oct 2026 05:00:11 +0200 Subject: [PATCH 1/4] sd gecko support --- include/sd.h | 19 ++ include/sd_gc_internal.h | 14 ++ lib/hb-D43J.txt | 6 + src/homeboy.c | 16 ++ src/init.c | 5 + src/sd_gc.c | 483 ++++++++++++++++++++++++++++++++++++++- src/sd_gc_internal.c | 87 +++++++ 7 files changed, 623 insertions(+), 7 deletions(-) create mode 100644 include/sd_gc_internal.h create mode 100644 src/sd_gc_internal.c diff --git a/include/sd.h b/include/sd.h index a3252e5..08c3a9e 100644 --- a/include/sd.h +++ b/include/sd.h @@ -83,6 +83,23 @@ #define SD_ERROR_NOBUFFER 4 #define SD_ERROR_OTHER 5 +#define SDIO_ERROR_NONE 0 +#define SDIO_ERROR_NO_EXI 1 +#define SDIO_ERROR_IDLE_CLOCKS 2 +#define SDIO_ERROR_CMD0 3 +#define SDIO_ERROR_CMD8 4 +#define SDIO_ERROR_ACMD41 5 +#define SDIO_ERROR_CMD58 6 +#define SDIO_ERROR_SET_BLOCKLEN 7 +#define SDIO_ERROR_READ_CMD 8 +#define SDIO_ERROR_READ_DATA 9 +#define SDIO_ERROR_READ_STOP 10 +#define SDIO_ERROR_WRITE_CMD 11 +#define SDIO_ERROR_WRITE_DATA 12 +#define SDIO_ERROR_WRITE_STOP 13 +#define SDIO_ERROR_WRITE_REJECTED 14 +#define SDIO_ERROR_WRITE_BUSY_TIMEOUT 15 + bool sdio_is_initialized(void); bool sdio_is_inserted(void); bool sdio_is_sdhc(void); @@ -90,5 +107,7 @@ bool sdio_write_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer); bool sdio_read_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer); bool sdio_stop(void); bool sdio_start(void); +uint32_t sdio_get_error(void); +uint32_t sdio_get_error_detail(void); #endif diff --git a/include/sd_gc_internal.h b/include/sd_gc_internal.h new file mode 100644 index 0000000..0e9019d --- /dev/null +++ b/include/sd_gc_internal.h @@ -0,0 +1,14 @@ +#ifndef _SD_GC_INTERNAL_H +#define _SD_GC_INTERNAL_H + +#include "types.h" + +#define SD_GC_EXI_WRITE 1 +#define SD_GC_EXI_READ 2 + +bool sd_gc_exi_present(s32 channel); +bool sd_gc_exi_begin(s32 channel, u32 frequency, bool select_device); +bool sd_gc_exi_end(s32 channel); +bool sd_gc_exi_transfer(s32 channel, u8* data, u32 length, u32 mode); + +#endif \ No newline at end of file diff --git a/lib/hb-D43J.txt b/lib/hb-D43J.txt index eba3e56..65b2b53 100644 --- a/lib/hb-D43J.txt +++ b/lib/hb-D43J.txt @@ -30,3 +30,9 @@ OSGetTick = 0x800A14F8; ganMapGPR = 0x800ED338; gpSystem = 0x8018B894; gnFlagZelda = 0x8018B968; +OSDisableInterrupts = 0x8009E248; +OSRestoreInterrupts = 0x8009E270; +__OSMaskInterrupts = 0x8009E610; +__EXIProbe = 0x800A2170; +EXIGetID = 0x800A2EF8; +EXIProbeEx = 0x800A2364; diff --git a/src/homeboy.c b/src/homeboy.c index ca788b2..b650435 100644 --- a/src/homeboy.c +++ b/src/homeboy.c @@ -46,8 +46,16 @@ static void do_write() { if (sdio_write_sectors(homeboy_obj->write_lba, homeboy_obj->block_cnt, (void*)((char*)n64_dram + homeboy_obj->addr))) { homeboy_obj->error = SD_ERROR_SUCCESS; +#if IS_GC + homeboy_obj->status &= 0x0000FFFF; +#endif } else { +#if IS_GC + homeboy_obj->error = sdio_get_error(); + homeboy_obj->status = (homeboy_obj->status & 0x0000FFFF) | ((sdio_get_error_detail() & 0xFFFF) << 16); +#else homeboy_obj->error = SD_ERROR_INVAL; +#endif } homeboy_obj->ready = 1; @@ -62,7 +70,11 @@ static void do_read() { (void*)((char*)n64_dram + homeboy_obj->addr))) { homeboy_obj->error = SD_ERROR_SUCCESS; } else { +#if IS_GC + homeboy_obj->error = sdio_get_error(); +#else homeboy_obj->error = SD_ERROR_INVAL; +#endif } homeboy_obj->ready = 1; @@ -104,7 +116,11 @@ static void do_status_update() { homeboy_obj->sdhc = sdio_is_sdhc(); homeboy_obj->ready = 1; } else { +#if IS_GC + homeboy_obj->error = sdio_get_error(); +#else homeboy_obj->error = SD_ERROR_INVAL; +#endif } homeboy_obj->busy = 0; diff --git a/src/init.c b/src/init.c index 7e256d7..c9c2f2b 100644 --- a/src/init.c +++ b/src/init.c @@ -48,6 +48,11 @@ INIT bool _start(Ram* pRAM, s32 nSize) { init_hooks(); homeboy_init(); +#if IS_GC + if (homeboy_obj != NULL) { + homeboy_obj->key = 0x1234; + } +#endif #ifdef HB_HEAP homeboy_heap_init(); #endif diff --git a/src/sd_gc.c b/src/sd_gc.c index 29c9443..396c3f9 100644 --- a/src/sd_gc.c +++ b/src/sd_gc.c @@ -1,34 +1,503 @@ +#include +#include + +#include "os.h" #include "sd.h" +#include "sd_gc_internal.h" #include "version.h" #if IS_GC -bool sdio_is_initialized(void) { +#define SD_GC_CHANNEL 1 +#define SD_GC_INIT_FREQUENCY 0 +#define SD_SECTOR_SIZE 512 +#define SD_INIT_TIMEOUT_TICKS 40500000 +#define SD_RESPONSE_TIMEOUT_TICKS 4050000 +#define SD_BUSY_TIMEOUT_TICKS 20250000 + +static const u8 sd_clock_div[64] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x03, 0x05, 0xFF, 0x00, 0x03, 0x05, 0xFF, 0x00, 0x03, 0x05, + 0xFF, 0x00, 0x03, 0x05, 0xFF, 0x01, 0x04, 0x05, 0xFF, 0x01, 0x04, 0x05, 0xFF, 0x01, 0x04, 0x05, + 0xFF, 0x01, 0x05, 0x05, 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x02, 0x05, 0x05, + 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x03, 0x05, 0x05, +}; + +extern OSTick OSGetTick(void); + +static bool sd_initialized; +static bool sd_sdhc; +static u32 sd_last_error; +static u32 sd_last_error_detail; +static u32 sd_frequency = SD_GC_INIT_FREQUENCY; +static u8 sd_scratch[SD_SECTOR_SIZE]; + +static bool sd_begin(bool select_device); + +static bool sd_wait_not_busy_selected(u32 timeout_ticks) { + u8 value; + OSTick start = OSGetTick(); + + do { + value = 0xFF; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { + return false; + } + if (value != 0) { + return true; + } + } while ((u32)(OSGetTick() - start) < timeout_ticks); + return false; } +static u8 sd_crc7(const u8* data, u32 length) { + u8 crc = 0; + + for (u32 i = 0; i < length; i++) { + u8 value = data[i]; + for (u32 bit = 0; bit < 8; bit++) { + crc <<= 1; + if (((value ^ crc) & 0x80) != 0) { + crc ^= 0x09; + } + value <<= 1; + } + } + + return (u8)((crc << 1) | 1); +} + +static u16 sd_crc16(const u8* data, u32 length) { + u16 crc = 0; + + for (u32 i = 0; i < length; i++) { + crc ^= (u16)data[i] << 8; + for (u32 bit = 0; bit < 8; bit++) { + crc = (crc & 0x8000) ? (u16)((crc << 1) ^ 0x1021) : (u16)(crc << 1); + } + } + + return crc; +} + +static bool sd_begin(bool select_device) { + return sd_gc_exi_begin(SD_GC_CHANNEL, sd_frequency, select_device); +} + +static bool sd_send_idle_clocks(void) { + u8 clocks[0x80]; + + memset(clocks, 0xFF, sizeof(clocks)); + if (!sd_gc_exi_begin(SD_GC_CHANNEL, SD_GC_INIT_FREQUENCY, false)) { + return false; + } + + for (u32 i = 0; i < 0x14; i++) { + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, clocks, sizeof(clocks), SD_GC_EXI_WRITE)) { + sd_gc_exi_end(SD_GC_CHANNEL); + return false; + } + } + + return sd_gc_exi_end(SD_GC_CHANNEL); +} + +static bool sd_send_command(u8 command, u32 argument) { + u8 packet[6]; + u8 clocks[10]; + bool result; + + packet[0] = 0x40 | command; + packet[1] = (u8)(argument >> 24); + packet[2] = (u8)(argument >> 16); + packet[3] = (u8)(argument >> 8); + packet[4] = (u8)argument; + packet[5] = sd_crc7(packet, 5); + memset(clocks, 0xFF, sizeof(clocks)); + + if (!sd_begin(true)) { + return false; + } + + result = sd_gc_exi_transfer(SD_GC_CHANNEL, clocks, sizeof(clocks), SD_GC_EXI_WRITE) && + sd_gc_exi_transfer(SD_GC_CHANNEL, packet, 5, SD_GC_EXI_WRITE) && + sd_gc_exi_transfer(SD_GC_CHANNEL, packet + 5, 1, SD_GC_EXI_WRITE); + sd_gc_exi_end(SD_GC_CHANNEL); + return result; +} + +static bool sd_read_response(u8* response, u32 length, bool skip_stuff_byte) { + u8 value = 0xFF; + OSTick start; + bool result = false; + + if (response == NULL || length == 0 || !sd_begin(true)) { + return false; + } + + if (skip_stuff_byte && !sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { + goto done; + } + + start = OSGetTick(); + do { + value = 0xFF; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { + goto done; + } + } while ((value & 0x80) != 0 && (u32)(OSGetTick() - start) < SD_RESPONSE_TIMEOUT_TICKS); + + if ((value & 0x80) != 0) { + goto done; + } + + response[0] = value; + if (length > 1) { + memset(response + 1, 0xFF, length - 1); + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, response + 1, length - 1, SD_GC_EXI_READ)) { + goto done; + } + } + result = true; + +done: + sd_gc_exi_end(SD_GC_CHANNEL); + return result; +} + +static bool sd_command_response(u8 command, u32 argument, u8* response, u32 length) { + return sd_send_command(command, argument) && sd_read_response(response, length, command == 12); +} + +static bool sd_read_data_block(u8* data, u32 length) { + u8 token = 0xFF; + u8 crc_bytes[2]; + OSTick start; + bool result = false; + + if (data == NULL || length == 0 || !sd_begin(true)) { + return false; + } + + start = OSGetTick(); + do { + token = 0xFF; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &token, 1, SD_GC_EXI_READ)) { + goto done; + } + } while (token == 0xFF && (u32)(OSGetTick() - start) < SD_RESPONSE_TIMEOUT_TICKS); + + if (token != 0xFE || !sd_gc_exi_transfer(SD_GC_CHANNEL, data, length, SD_GC_EXI_READ)) { + goto done; + } + + crc_bytes[0] = crc_bytes[1] = 0xFF; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, crc_bytes, sizeof(crc_bytes), SD_GC_EXI_READ)) { + goto done; + } + + result = (((u16)crc_bytes[0] << 8) | crc_bytes[1]) == sd_crc16(data, length); + +done: + sd_gc_exi_end(SD_GC_CHANNEL); + return result; +} + +static bool sd_write_data_block(const u8* data, u32 length, u8 token, u32 block_index) { + u16 crc; + u8 crc_bytes[2]; + u8 response; + u8 busy; + OSTick start; + bool result = false; + + if (data == NULL || length == 0 || !sd_begin(true)) { + return false; + } + + crc = sd_crc16(data, length); + crc_bytes[0] = (u8)(crc >> 8); + crc_bytes[1] = (u8)crc; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &token, 1, SD_GC_EXI_WRITE) || + !sd_gc_exi_transfer(SD_GC_CHANNEL, (u8*)data, length, SD_GC_EXI_WRITE)) { + goto done; + } + + start = OSGetTick(); + while ((u32)(OSGetTick() - start) < 41) {} + + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, crc_bytes, sizeof(crc_bytes), SD_GC_EXI_WRITE)) { + goto done; + } + sd_gc_exi_end(SD_GC_CHANNEL); + + if (!sd_begin(true)) { + sd_last_error = SDIO_ERROR_WRITE_DATA; + return false; + } + + start = OSGetTick(); + do { + response = 0xFF; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &response, 1, SD_GC_EXI_READ)) { + goto done; + } + response &= 0x1F; + if (response == 0x05 || response == 0x0B || response == 0x0D) { + break; + } + } while ((u32)(OSGetTick() - start) < SD_RESPONSE_TIMEOUT_TICKS); + if (response != 0x05 && response != 0x0B && response != 0x0D) { + sd_last_error_detail = ((u32)response << 8) | (block_index & 0xFF); + sd_last_error = SDIO_ERROR_WRITE_DATA; + goto done; + } + sd_last_error_detail = (u32)response << 8; + + start = OSGetTick(); + do { + busy = 0; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &busy, 1, SD_GC_EXI_READ)) { + goto done; + } + } while (busy == 0 && (u32)(OSGetTick() - start) < SD_BUSY_TIMEOUT_TICKS); + sd_last_error_detail |= busy; + + if (busy == 0) { + sd_last_error = SDIO_ERROR_WRITE_BUSY_TIMEOUT; + goto done; + } + if (response != 0x05) { + sd_last_error = SDIO_ERROR_WRITE_REJECTED; + goto done; + } + result = true; + +done: + sd_gc_exi_end(SD_GC_CHANNEL); + return result; +} + +bool sdio_is_initialized(void) { + return sd_initialized && sd_gc_exi_present(SD_GC_CHANNEL); +} + bool sdio_is_inserted(void) { - return false; + return sd_gc_exi_present(SD_GC_CHANNEL); } bool sdio_is_sdhc(void) { - return false; + return sd_initialized && sd_sdhc; +} + +uint32_t sdio_get_error(void) { + return sd_last_error; +} + +uint32_t sdio_get_error_detail(void) { + return sd_last_error_detail; } bool sdio_write_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer) { - return false; + u8 response; + u8* data = (u8*)buffer; + u64 end_sector = (u64)sector + sec_cnt; + + sd_last_error = SDIO_ERROR_NONE; + sd_last_error_detail = 0; + + if (!sdio_is_initialized() || sec_cnt == 0 || end_sector > 0x100000000ULL) { + sd_last_error = SDIO_ERROR_WRITE_CMD; + return false; + } + + if (data == NULL) { + memset(sd_scratch, 0, sizeof(sd_scratch)); + } + + u64 byte_address = sd_sdhc ? sector : (u64)sector * SD_SECTOR_SIZE; + if (byte_address > 0xFFFFFFFFULL || (!sd_sdhc && end_sector * SD_SECTOR_SIZE > 0x100000000ULL)) { + sd_last_error = SDIO_ERROR_WRITE_CMD; + return false; + } + + for (u32 i = 0; i < sec_cnt; i++) { + u64 block_address = sd_sdhc ? (u64)sector + i : ((u64)sector + i) * SD_SECTOR_SIZE; + response = 0xFF; + if (!sd_command_response(24, (u32)block_address, &response, 1) || response != 0) { + sd_last_error = SDIO_ERROR_WRITE_CMD; + sd_last_error_detail = ((u32)response << 8) | (i & 0xFF); + return false; + } + + if (data == NULL) { + memset(sd_scratch, 0, sizeof(sd_scratch)); + if (!sd_write_data_block(sd_scratch, sizeof(sd_scratch), 0xFE, i)) { + if (sd_last_error == SDIO_ERROR_NONE) { + sd_last_error = SDIO_ERROR_WRITE_DATA; + } + return false; + } + } else if (!sd_write_data_block(data + i * SD_SECTOR_SIZE, SD_SECTOR_SIZE, 0xFE, i)) { + if (sd_last_error == SDIO_ERROR_NONE) { + sd_last_error = SDIO_ERROR_WRITE_DATA; + } + return false; + } + } + + sd_last_error = SDIO_ERROR_NONE; + sd_last_error_detail = 0; + return true; } bool sdio_read_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer) { - return false; + u8 response; + u8* data = (u8*)buffer; + u64 end_sector = (u64)sector + sec_cnt; + + if (!sdio_is_initialized() || sec_cnt == 0 || end_sector > 0x100000000ULL) { + sd_last_error = SDIO_ERROR_READ_CMD; + return false; + } + + u64 byte_address = sd_sdhc ? sector : (u64)sector * SD_SECTOR_SIZE; + if (byte_address > 0xFFFFFFFFULL || (!sd_sdhc && end_sector * SD_SECTOR_SIZE > 0x100000000ULL)) { + sd_last_error = SDIO_ERROR_READ_CMD; + return false; + } + + if (!sd_command_response(18, (u32)byte_address, &response, 1) || response != 0) { + sd_last_error = SDIO_ERROR_READ_CMD; + return false; + } + + for (u32 i = 0; i < sec_cnt; i++) { + u8* target = data != NULL ? data + i * SD_SECTOR_SIZE : sd_scratch; + if (!sd_read_data_block(target, SD_SECTOR_SIZE)) { + u8 stop_response; + sd_command_response(12, 0, &stop_response, 1); + sd_last_error = SDIO_ERROR_READ_DATA; + return false; + } + } + + if (!sd_command_response(12, 0, &response, 1)) { + sd_last_error = SDIO_ERROR_READ_STOP; + return false; + } + sd_last_error = SDIO_ERROR_NONE; + return true; } bool sdio_stop(void) { - return false; + if (!sd_initialized) { + return false; + } + + sd_initialized = false; + sd_sdhc = false; + sd_last_error = SDIO_ERROR_NONE; + sd_frequency = SD_GC_INIT_FREQUENCY; + return true; } bool sdio_start(void) { - return false; + u8 response[5]; + u8 csd[16]; + u8 command_response; + u32 ocr; + OSTick start; + bool v2_card; + + if (sd_initialized) { + return true; + } + + sd_sdhc = false; + sd_frequency = SD_GC_INIT_FREQUENCY; + sd_last_error = SDIO_ERROR_NONE; + if (!sdio_is_inserted()) { + sd_last_error = SDIO_ERROR_NO_EXI; + return false; + } + if (!sd_send_idle_clocks()) { + sd_last_error = SDIO_ERROR_IDLE_CLOCKS; + return false; + } + + if (!sd_command_response(0, 0, &command_response, 1) || command_response != 1) { + sd_last_error = SDIO_ERROR_CMD0; + return false; + } + + if (!sd_command_response(8, 0x1AA, response, sizeof(response))) { + sd_last_error = SDIO_ERROR_CMD8; + return false; + } + if (response[0] == 1) { + if (response[3] != 1 || response[4] != 0xAA) { + sd_last_error = SDIO_ERROR_CMD8; + return false; + } + v2_card = true; + } else if ((response[0] & 4) != 0) { + v2_card = false; + } else { + sd_last_error = SDIO_ERROR_CMD8; + return false; + } + + start = OSGetTick(); + do { + if (!sd_command_response(55, 0, &command_response, 1) || (command_response != 0 && command_response != 1)) { + sd_last_error = SDIO_ERROR_ACMD41; + return false; + } + + if (!sd_command_response(41, v2_card ? 0x40300000 : 0x00300000, &command_response, 1)) { + sd_last_error = SDIO_ERROR_ACMD41; + return false; + } + if (command_response == 0) { + break; + } + if (command_response != 1) { + sd_last_error = SDIO_ERROR_ACMD41; + return false; + } + } while ((u32)(OSGetTick() - start) < SD_INIT_TIMEOUT_TICKS); + + if (command_response != 0 || !sd_command_response(58, 0, response, sizeof(response)) || response[0] != 0) { + sd_last_error = SDIO_ERROR_CMD58; + return false; + } + + ocr = ((u32)response[1] << 24) | ((u32)response[2] << 16) | ((u32)response[3] << 8) | response[4]; + sd_sdhc = (ocr & 0x40000000) != 0; + + if (!sd_sdhc) { + if (!sd_command_response(16, SD_SECTOR_SIZE, &command_response, 1) || command_response != 0) { + sd_last_error = SDIO_ERROR_SET_BLOCKLEN; + return false; + } + } + + if (sd_command_response(9, 0, &command_response, 1) && command_response == 0 && + sd_read_data_block(csd, sizeof(csd))) { + u8 transfer_speed = csd[3]; + u8 speed_unit = transfer_speed & 7; + if (speed_unit <= 3) { + u8 frequency = sd_clock_div[(((transfer_speed >> 3) & 0xF) * 4) + speed_unit]; + if (frequency != 0xFF) { + sd_frequency = frequency; + } + } + } + + sd_initialized = true; + sd_last_error = SDIO_ERROR_NONE; + return true; } #endif diff --git a/src/sd_gc_internal.c b/src/sd_gc_internal.c new file mode 100644 index 0000000..31f7551 --- /dev/null +++ b/src/sd_gc_internal.c @@ -0,0 +1,87 @@ +#include + +#include "sd_gc_internal.h" + +#define EXI_REGS ((volatile u32*)0xCC006800) +#define EXI_REG(channel, index) EXI_REGS[(channel) * 5 + (index)] +#define EXI_SPIN_LIMIT 0x10000 + +bool sd_gc_exi_present(s32 channel) { + u32 status; + + if (channel < 0 || channel > 1) { + return false; + } + + status = EXI_REG(channel, 0); + return (status & 0x1000) != 0; +} + +bool sd_gc_exi_begin(s32 channel, u32 frequency, bool select_device) { + u32 status; + + if (channel < 0 || channel > 1 || frequency > 5 || !sd_gc_exi_present(channel)) { + return false; + } + + status = EXI_REG(channel, 0) & 0x405; + status |= frequency << 4; + if (select_device) { + status |= 0x80; + } + EXI_REG(channel, 0) = status; + return true; +} + +bool sd_gc_exi_end(s32 channel) { + if (channel < 0 || channel > 1) { + return false; + } + + EXI_REG(channel, 0) &= ~0x80; + return true; +} + +bool sd_gc_exi_transfer(s32 channel, u8* data, u32 length, u32 mode) { + if (channel < 0 || channel > 1 || data == NULL || (mode != SD_GC_EXI_WRITE && mode != SD_GC_EXI_READ)) { + return false; + } + + while (length > 0) { + u32 chunk = length < 4 ? length : 4; + u32 value = 0; + u32 tries = EXI_SPIN_LIMIT; + + for (u32 i = 0; i < chunk; i++) { + if (mode == SD_GC_EXI_READ) { + data[i] = 0xFF; + } + value |= (u32)data[i] << ((3 - i) * 8); + } + EXI_REG(channel, 4) = value; + + EXI_REG(channel, 3) = (mode << 2) | 1 | ((chunk - 1) << 4); + while (tries > 0 && (EXI_REG(channel, 3) & 1) != 0) { + tries--; + } + if ((EXI_REG(channel, 3) & 1) != 0) { + return false; + } + + if (mode == SD_GC_EXI_READ) { + value = EXI_REG(channel, 4); + for (u32 i = 0; i < chunk; i++) { + data[i] = (u8)(value >> ((3 - i) * 8)); + } + } + + data += chunk; + length -= chunk; + } + + return true; +} + +bool EXISelectSD(long chan, unsigned long dev, unsigned long freq) { + return dev == 0 && sd_gc_exi_begin((s32)chan, (u32)freq, false); +} From a4d4509e7bffc4df9727fd7b483916f77722e60a Mon Sep 17 00:00:00 2001 From: Yanis002 <35189056+Yanis002@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:28:54 +0200 Subject: [PATCH 2/4] cleanup --- include/sd.h | 61 ++----- include/sd_gc_internal.h | 14 -- src/sd_gc.c | 347 ++++++++++++++++++++++++--------------- src/sd_gc_internal.c | 87 ---------- 4 files changed, 230 insertions(+), 279 deletions(-) delete mode 100644 include/sd_gc_internal.h delete mode 100644 src/sd_gc_internal.c diff --git a/include/sd.h b/include/sd.h index 08c3a9e..a1c56f1 100644 --- a/include/sd.h +++ b/include/sd.h @@ -5,11 +5,8 @@ #include "types.h" -#define SDIO_HEAPSIZE (0x5000) - #define PAGE_SIZE512 512 -#define SDIOHCR_RESPONSE 0x10 #define SDIOHCR_HOSTCONTROL 0x28 #define SDIOHCR_POWERCONTROL 0x29 #define SDIOHCR_CLOCKCONTROL 0x2c @@ -22,30 +19,17 @@ #define IOCTL_SDIO_WRITEHCREG 0x01 #define IOCTL_SDIO_READHCREG 0x02 -#define IOCTL_SDIO_READCREG 0x03 #define IOCTL_SDIO_RESETCARD 0x04 -#define IOCTL_SDIO_WRITECREG 0x05 #define IOCTL_SDIO_SETCLK 0x06 #define IOCTL_SDIO_SENDCMD 0x07 -#define IOCTL_SDIO_SETBUSWIDTH 0x08 -#define IOCTL_SDIO_READMCREG 0x09 -#define IOCTL_SDIO_WRITEMCREG 0x0A #define IOCTL_SDIO_GETSTATUS 0x0B -#define IOCTL_SDIO_GETOCR 0x0C -#define IOCTL_SDIO_READDATA 0x0D -#define IOCTL_SDIO_WRITEDATA 0x0E -#define SDIOCMD_TYPE_BC 1 -#define SDIOCMD_TYPE_BCR 2 #define SDIOCMD_TYPE_AC 3 -#define SDIOCMD_TYPE_ADTC 4 -#define SDIO_RESPONSE_NONE 0 #define SDIO_RESPONSE_R1 1 #define SDIO_RESPONSE_R1B 2 #define SDIO_RESPOSNE_R2 3 #define SDIO_RESPONSE_R3 4 -#define SDIO_RESPONSE_R4 5 #define SDIO_RESPONSE_R5 6 #define SDIO_RESPONSE_R6 7 @@ -55,51 +39,40 @@ #define SDIO_CMD_SELECT 0x07 #define SDIO_CMD_DESELECT 0x07 #define SDIO_CMD_SENDIFCOND 0x08 -#define SDIO_CMD_SENDCSD 0x09 -#define SDIO_CMD_SENDCID 0x0A -#define SDIO_CMD_SENDSTATUS 0x0D #define SDIO_CMD_SETBLOCKLEN 0x10 -#define SDIO_CMD_READBLOCK 0x11 #define SDIO_CMD_READMULTIBLOCK 0x12 -#define SDIO_CMD_WRITEBLOCK 0x18 #define SDIO_CMD_WRITEMULTIBLOCK 0x19 #define SDIO_CMD_APPCMD 0x37 #define SDIO_ACMD_SETBUSWIDTH 0x06 -#define SDIO_ACMD_SENDSCR 0x33 #define SDIO_ACMD_SENDOPCOND 0x29 #define SDIO_STATUS_CARD_INSERTED 0x1 #define SDIO_STATUS_CARD_INITIALIZED 0x10000 #define SDIO_STATUS_CARD_SDHC 0x100000 -#define READ_BL_LEN ((u8)(__sd0_csd[5] & 0x0f)) -#define WRITE_BL_LEN ((u8)(((__sd0_csd[12] & 0x03) << 2) | ((__sd0_csd[13] >> 6) & 0x03))) - #define SD_ERROR_SUCCESS 0 #define SD_ERROR_INVAL 1 -#define SD_ERROR_QUEUEFULL 2 -#define SD_ERROR_NOMEM 3 -#define SD_ERROR_NOBUFFER 4 -#define SD_ERROR_OTHER 5 - -#define SDIO_ERROR_NONE 0 -#define SDIO_ERROR_NO_EXI 1 -#define SDIO_ERROR_IDLE_CLOCKS 2 -#define SDIO_ERROR_CMD0 3 -#define SDIO_ERROR_CMD8 4 -#define SDIO_ERROR_ACMD41 5 -#define SDIO_ERROR_CMD58 6 -#define SDIO_ERROR_SET_BLOCKLEN 7 -#define SDIO_ERROR_READ_CMD 8 -#define SDIO_ERROR_READ_DATA 9 -#define SDIO_ERROR_READ_STOP 10 -#define SDIO_ERROR_WRITE_CMD 11 -#define SDIO_ERROR_WRITE_DATA 12 -#define SDIO_ERROR_WRITE_STOP 13 + +#define SDIO_ERROR_NONE 0 +#define SDIO_ERROR_NO_EXI 1 +#define SDIO_ERROR_IDLE_CLOCKS 2 +#define SDIO_ERROR_CMD0 3 +#define SDIO_ERROR_CMD8 4 +#define SDIO_ERROR_ACMD41 5 +#define SDIO_ERROR_CMD58 6 +#define SDIO_ERROR_SET_BLOCKLEN 7 +#define SDIO_ERROR_READ_CMD 8 +#define SDIO_ERROR_READ_DATA 9 +#define SDIO_ERROR_READ_STOP 10 +#define SDIO_ERROR_WRITE_CMD 11 +#define SDIO_ERROR_WRITE_DATA 12 #define SDIO_ERROR_WRITE_REJECTED 14 #define SDIO_ERROR_WRITE_BUSY_TIMEOUT 15 +#define SD_GC_EXI_WRITE 1 +#define SD_GC_EXI_READ 2 + bool sdio_is_initialized(void); bool sdio_is_inserted(void); bool sdio_is_sdhc(void); diff --git a/include/sd_gc_internal.h b/include/sd_gc_internal.h deleted file mode 100644 index 0e9019d..0000000 --- a/include/sd_gc_internal.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _SD_GC_INTERNAL_H -#define _SD_GC_INTERNAL_H - -#include "types.h" - -#define SD_GC_EXI_WRITE 1 -#define SD_GC_EXI_READ 2 - -bool sd_gc_exi_present(s32 channel); -bool sd_gc_exi_begin(s32 channel, u32 frequency, bool select_device); -bool sd_gc_exi_end(s32 channel); -bool sd_gc_exi_transfer(s32 channel, u8* data, u32 length, u32 mode); - -#endif \ No newline at end of file diff --git a/src/sd_gc.c b/src/sd_gc.c index 396c3f9..2865c02 100644 --- a/src/sd_gc.c +++ b/src/sd_gc.c @@ -3,11 +3,16 @@ #include "os.h" #include "sd.h" -#include "sd_gc_internal.h" #include "version.h" #if IS_GC +extern OSTick OSGetTick(void); +extern bool sd_gc_exi_present(s32 channel); +extern bool sd_gc_exi_begin(s32 channel, u32 frequency, bool select_device); +extern bool sd_gc_exi_end(s32 channel); +extern bool sd_gc_exi_transfer(s32 channel, u8* data, u32 length, u32 mode); + #define SD_GC_CHANNEL 1 #define SD_GC_INIT_FREQUENCY 0 #define SD_SECTOR_SIZE 512 @@ -22,32 +27,101 @@ static const u8 sd_clock_div[64] = { 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x02, 0x05, 0x05, 0xFF, 0x03, 0x05, 0x05, }; -extern OSTick OSGetTick(void); - -static bool sd_initialized; -static bool sd_sdhc; -static u32 sd_last_error; -static u32 sd_last_error_detail; +static bool sd_initialized = false; +static bool sd_sdhc = false; +static u32 sd_last_error = SDIO_ERROR_NONE; +static u32 sd_last_error_detail = 0; static u32 sd_frequency = SD_GC_INIT_FREQUENCY; static u8 sd_scratch[SD_SECTOR_SIZE]; -static bool sd_begin(bool select_device); +#define EXI_REGS ((volatile u32*)0xCC006800) +#define EXI_REG(channel, index) EXI_REGS[(channel) * 5 + (index)] +#define EXI_SPIN_LIMIT 0x10000 -static bool sd_wait_not_busy_selected(u32 timeout_ticks) { - u8 value; - OSTick start = OSGetTick(); +bool sd_gc_exi_present(s32 channel) { + u32 status; - do { - value = 0xFF; - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { + if (channel < 0 || channel > 1) { + return false; + } + + status = EXI_REG(channel, 0); + return (status & 0x1000) != 0; +} + +bool sd_gc_exi_begin(s32 channel, u32 frequency, bool select_device) { + u32 status; + + if (channel < 0 || channel > 1 || frequency > 5 || !sd_gc_exi_present(channel)) { + return false; + } + + status = EXI_REG(channel, 0) & 0x405; + status |= frequency << 4; + if (select_device) { + status |= 0x80; + } + + EXI_REG(channel, 0) = status; + return true; +} + +bool sd_gc_exi_end(s32 channel) { + if (channel < 0 || channel > 1) { + return false; + } + + EXI_REG(channel, 0) &= ~0x80; + return true; +} + +bool sd_gc_exi_transfer(s32 channel, u8* data, u32 length, u32 mode) { + if (channel < 0 || channel > 1 || data == NULL || (mode != SD_GC_EXI_WRITE && mode != SD_GC_EXI_READ)) { + return false; + } + + while (length > 0) { + u32 chunk = length < 4 ? length : 4; + u32 value = 0; + u32 tries = EXI_SPIN_LIMIT; + + for (u32 i = 0; i < chunk; i++) { + if (mode == SD_GC_EXI_READ) { + data[i] = 0xFF; + } + value |= (u32)data[i] << ((3 - i) * 8); + } + EXI_REG(channel, 4) = value; + + EXI_REG(channel, 3) = (mode << 2) | 1 | ((chunk - 1) << 4); + while (tries > 0 && (EXI_REG(channel, 3) & 1) != 0) { + tries--; + } + if ((EXI_REG(channel, 3) & 1) != 0) { return false; } - if (value != 0) { - return true; + + if (mode == SD_GC_EXI_READ) { + value = EXI_REG(channel, 4); + for (u32 i = 0; i < chunk; i++) { + data[i] = (u8)(value >> ((3 - i) * 8)); + } } - } while ((u32)(OSGetTick() - start) < timeout_ticks); - return false; + data += chunk; + length -= chunk; + } + + return true; +} + +static inline u32 CheckTime(OSTick start, OSTick end) { + return (u32)(OSGetTick() - start) < end; +} + +static bool DoEXIEnd(bool result) { + sd_gc_exi_end(SD_GC_CHANNEL); + return result; } static u8 sd_crc7(const u8* data, u32 length) { @@ -55,11 +129,14 @@ static u8 sd_crc7(const u8* data, u32 length) { for (u32 i = 0; i < length; i++) { u8 value = data[i]; + for (u32 bit = 0; bit < 8; bit++) { crc <<= 1; + if (((value ^ crc) & 0x80) != 0) { crc ^= 0x09; } + value <<= 1; } } @@ -72,6 +149,7 @@ static u16 sd_crc16(const u8* data, u32 length) { for (u32 i = 0; i < length; i++) { crc ^= (u16)data[i] << 8; + for (u32 bit = 0; bit < 8; bit++) { crc = (crc & 0x8000) ? (u16)((crc << 1) ^ 0x1021) : (u16)(crc << 1); } @@ -80,14 +158,11 @@ static u16 sd_crc16(const u8* data, u32 length) { return crc; } -static bool sd_begin(bool select_device) { - return sd_gc_exi_begin(SD_GC_CHANNEL, sd_frequency, select_device); -} - static bool sd_send_idle_clocks(void) { u8 clocks[0x80]; memset(clocks, 0xFF, sizeof(clocks)); + if (!sd_gc_exi_begin(SD_GC_CHANNEL, SD_GC_INIT_FREQUENCY, false)) { return false; } @@ -115,15 +190,15 @@ static bool sd_send_command(u8 command, u32 argument) { packet[5] = sd_crc7(packet, 5); memset(clocks, 0xFF, sizeof(clocks)); - if (!sd_begin(true)) { + if (!sd_gc_exi_begin(SD_GC_CHANNEL, sd_frequency, true)) { return false; } result = sd_gc_exi_transfer(SD_GC_CHANNEL, clocks, sizeof(clocks), SD_GC_EXI_WRITE) && sd_gc_exi_transfer(SD_GC_CHANNEL, packet, 5, SD_GC_EXI_WRITE) && sd_gc_exi_transfer(SD_GC_CHANNEL, packet + 5, 1, SD_GC_EXI_WRITE); - sd_gc_exi_end(SD_GC_CHANNEL); - return result; + + return DoEXIEnd(result); } static bool sd_read_response(u8* response, u32 length, bool skip_stuff_byte) { @@ -131,38 +206,36 @@ static bool sd_read_response(u8* response, u32 length, bool skip_stuff_byte) { OSTick start; bool result = false; - if (response == NULL || length == 0 || !sd_begin(true)) { + if (response == NULL || length == 0 || !sd_gc_exi_begin(SD_GC_CHANNEL, sd_frequency, true)) { return false; } - if (skip_stuff_byte && !sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { - goto done; - } + if (!skip_stuff_byte || sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { + start = OSGetTick(); + do { + value = 0xFF; - start = OSGetTick(); - do { - value = 0xFF; - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { - goto done; - } - } while ((value & 0x80) != 0 && (u32)(OSGetTick() - start) < SD_RESPONSE_TIMEOUT_TICKS); + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &value, 1, SD_GC_EXI_READ)) { + return DoEXIEnd(result); + } + } while ((value & 0x80) != 0 && CheckTime(start, SD_RESPONSE_TIMEOUT_TICKS)); - if ((value & 0x80) != 0) { - goto done; - } + if (!(value & 0x80)) { + response[0] = value; + + if (length > 1) { + memset(response + 1, 0xFF, length - 1); + + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, response + 1, length - 1, SD_GC_EXI_READ)) { + return DoEXIEnd(result); + } + } - response[0] = value; - if (length > 1) { - memset(response + 1, 0xFF, length - 1); - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, response + 1, length - 1, SD_GC_EXI_READ)) { - goto done; + result = true; } } - result = true; -done: - sd_gc_exi_end(SD_GC_CHANNEL); - return result; + return DoEXIEnd(result); } static bool sd_command_response(u8 command, u32 argument, u8* response, u32 length) { @@ -175,32 +248,28 @@ static bool sd_read_data_block(u8* data, u32 length) { OSTick start; bool result = false; - if (data == NULL || length == 0 || !sd_begin(true)) { + if (data == NULL || length == 0 || !sd_gc_exi_begin(SD_GC_CHANNEL, sd_frequency, true)) { return false; } start = OSGetTick(); do { token = 0xFF; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &token, 1, SD_GC_EXI_READ)) { - goto done; + return DoEXIEnd(result); } - } while (token == 0xFF && (u32)(OSGetTick() - start) < SD_RESPONSE_TIMEOUT_TICKS); + } while (token == 0xFF && CheckTime(start, SD_RESPONSE_TIMEOUT_TICKS)); - if (token != 0xFE || !sd_gc_exi_transfer(SD_GC_CHANNEL, data, length, SD_GC_EXI_READ)) { - goto done; - } + if (token == 0xFE && sd_gc_exi_transfer(SD_GC_CHANNEL, data, length, SD_GC_EXI_READ)) { + crc_bytes[0] = crc_bytes[1] = 0xFF; - crc_bytes[0] = crc_bytes[1] = 0xFF; - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, crc_bytes, sizeof(crc_bytes), SD_GC_EXI_READ)) { - goto done; + if (sd_gc_exi_transfer(SD_GC_CHANNEL, crc_bytes, sizeof(crc_bytes), SD_GC_EXI_READ)) { + result = (((u16)crc_bytes[0] << 8) | crc_bytes[1]) == sd_crc16(data, length); + } } - result = (((u16)crc_bytes[0] << 8) | crc_bytes[1]) == sd_crc16(data, length); - -done: - sd_gc_exi_end(SD_GC_CHANNEL); - return result; + return DoEXIEnd(result); } static bool sd_write_data_block(const u8* data, u32 length, u8 token, u32 block_index) { @@ -211,71 +280,70 @@ static bool sd_write_data_block(const u8* data, u32 length, u8 token, u32 block_ OSTick start; bool result = false; - if (data == NULL || length == 0 || !sd_begin(true)) { + if (data == NULL || length == 0 || !sd_gc_exi_begin(SD_GC_CHANNEL, sd_frequency, true)) { return false; } crc = sd_crc16(data, length); crc_bytes[0] = (u8)(crc >> 8); crc_bytes[1] = (u8)crc; - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &token, 1, SD_GC_EXI_WRITE) || - !sd_gc_exi_transfer(SD_GC_CHANNEL, (u8*)data, length, SD_GC_EXI_WRITE)) { - goto done; - } - start = OSGetTick(); - while ((u32)(OSGetTick() - start) < 41) {} + if (sd_gc_exi_transfer(SD_GC_CHANNEL, &token, 1, SD_GC_EXI_WRITE) && + sd_gc_exi_transfer(SD_GC_CHANNEL, (u8*)data, length, SD_GC_EXI_WRITE)) { - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, crc_bytes, sizeof(crc_bytes), SD_GC_EXI_WRITE)) { - goto done; - } - sd_gc_exi_end(SD_GC_CHANNEL); + start = OSGetTick(); + while (CheckTime(start, 41)) {} - if (!sd_begin(true)) { - sd_last_error = SDIO_ERROR_WRITE_DATA; - return false; - } + if (sd_gc_exi_transfer(SD_GC_CHANNEL, crc_bytes, sizeof(crc_bytes), SD_GC_EXI_WRITE)) { + sd_gc_exi_end(SD_GC_CHANNEL); - start = OSGetTick(); - do { - response = 0xFF; - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &response, 1, SD_GC_EXI_READ)) { - goto done; - } - response &= 0x1F; - if (response == 0x05 || response == 0x0B || response == 0x0D) { - break; - } - } while ((u32)(OSGetTick() - start) < SD_RESPONSE_TIMEOUT_TICKS); - if (response != 0x05 && response != 0x0B && response != 0x0D) { - sd_last_error_detail = ((u32)response << 8) | (block_index & 0xFF); - sd_last_error = SDIO_ERROR_WRITE_DATA; - goto done; - } - sd_last_error_detail = (u32)response << 8; + if (!sd_gc_exi_begin(SD_GC_CHANNEL, sd_frequency, true)) { + sd_last_error = SDIO_ERROR_WRITE_DATA; + return false; + } - start = OSGetTick(); - do { - busy = 0; - if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &busy, 1, SD_GC_EXI_READ)) { - goto done; - } - } while (busy == 0 && (u32)(OSGetTick() - start) < SD_BUSY_TIMEOUT_TICKS); - sd_last_error_detail |= busy; + start = OSGetTick(); + do { + response = 0xFF; + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &response, 1, SD_GC_EXI_READ)) { + return DoEXIEnd(result); + } - if (busy == 0) { - sd_last_error = SDIO_ERROR_WRITE_BUSY_TIMEOUT; - goto done; - } - if (response != 0x05) { - sd_last_error = SDIO_ERROR_WRITE_REJECTED; - goto done; + response &= 0x1F; + if (response == 0x05 || response == 0x0B || response == 0x0D) { + break; + } + } while (CheckTime(start, SD_RESPONSE_TIMEOUT_TICKS)); + + if (response != 0x05 && response != 0x0B && response != 0x0D) { + sd_last_error_detail = ((u32)response << 8) | (block_index & 0xFF); + sd_last_error = SDIO_ERROR_WRITE_DATA; + } else { + sd_last_error_detail = (u32)response << 8; + + start = OSGetTick(); + do { + busy = 0; + + if (!sd_gc_exi_transfer(SD_GC_CHANNEL, &busy, 1, SD_GC_EXI_READ)) { + return DoEXIEnd(result); + } + } while (busy == 0 && CheckTime(start, SD_BUSY_TIMEOUT_TICKS)); + + sd_last_error_detail |= busy; + + if (busy == 0) { + sd_last_error = SDIO_ERROR_WRITE_BUSY_TIMEOUT; + } else if (response != 0x05) { + sd_last_error = SDIO_ERROR_WRITE_REJECTED; + } else { + result = true; + } + } + } } - result = true; -done: - sd_gc_exi_end(SD_GC_CHANNEL); - return result; + return DoEXIEnd(result); } bool sdio_is_initialized(void) { @@ -324,6 +392,7 @@ bool sdio_write_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer) { for (u32 i = 0; i < sec_cnt; i++) { u64 block_address = sd_sdhc ? (u64)sector + i : ((u64)sector + i) * SD_SECTOR_SIZE; response = 0xFF; + if (!sd_command_response(24, (u32)block_address, &response, 1) || response != 0) { sd_last_error = SDIO_ERROR_WRITE_CMD; sd_last_error_detail = ((u32)response << 8) | (i & 0xFF); @@ -332,16 +401,19 @@ bool sdio_write_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer) { if (data == NULL) { memset(sd_scratch, 0, sizeof(sd_scratch)); + if (!sd_write_data_block(sd_scratch, sizeof(sd_scratch), 0xFE, i)) { if (sd_last_error == SDIO_ERROR_NONE) { sd_last_error = SDIO_ERROR_WRITE_DATA; } + return false; } } else if (!sd_write_data_block(data + i * SD_SECTOR_SIZE, SD_SECTOR_SIZE, 0xFE, i)) { if (sd_last_error == SDIO_ERROR_NONE) { sd_last_error = SDIO_ERROR_WRITE_DATA; } + return false; } } @@ -374,6 +446,7 @@ bool sdio_read_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer) { for (u32 i = 0; i < sec_cnt; i++) { u8* target = data != NULL ? data + i * SD_SECTOR_SIZE : sd_scratch; + if (!sd_read_data_block(target, SD_SECTOR_SIZE)) { u8 stop_response; sd_command_response(12, 0, &stop_response, 1); @@ -386,20 +459,21 @@ bool sdio_read_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer) { sd_last_error = SDIO_ERROR_READ_STOP; return false; } + sd_last_error = SDIO_ERROR_NONE; return true; } bool sdio_stop(void) { - if (!sd_initialized) { - return false; + if (sd_initialized) { + sd_initialized = false; + sd_sdhc = false; + sd_last_error = SDIO_ERROR_NONE; + sd_frequency = SD_GC_INIT_FREQUENCY; + return true; } - sd_initialized = false; - sd_sdhc = false; - sd_last_error = SDIO_ERROR_NONE; - sd_frequency = SD_GC_INIT_FREQUENCY; - return true; + return false; } bool sdio_start(void) { @@ -417,10 +491,12 @@ bool sdio_start(void) { sd_sdhc = false; sd_frequency = SD_GC_INIT_FREQUENCY; sd_last_error = SDIO_ERROR_NONE; + if (!sdio_is_inserted()) { sd_last_error = SDIO_ERROR_NO_EXI; return false; } + if (!sd_send_idle_clocks()) { sd_last_error = SDIO_ERROR_IDLE_CLOCKS; return false; @@ -459,14 +535,16 @@ bool sdio_start(void) { sd_last_error = SDIO_ERROR_ACMD41; return false; } + if (command_response == 0) { break; } + if (command_response != 1) { sd_last_error = SDIO_ERROR_ACMD41; return false; } - } while ((u32)(OSGetTick() - start) < SD_INIT_TIMEOUT_TICKS); + } while (CheckTime(start, SD_INIT_TIMEOUT_TICKS)); if (command_response != 0 || !sd_command_response(58, 0, response, sizeof(response)) || response[0] != 0) { sd_last_error = SDIO_ERROR_CMD58; @@ -476,25 +554,26 @@ bool sdio_start(void) { ocr = ((u32)response[1] << 24) | ((u32)response[2] << 16) | ((u32)response[3] << 8) | response[4]; sd_sdhc = (ocr & 0x40000000) != 0; - if (!sd_sdhc) { + if (sd_sdhc) { + if (sd_command_response(9, 0, &command_response, 1) && command_response == 0 && + sd_read_data_block(csd, sizeof(csd))) { + u8 transfer_speed = csd[3]; + u8 speed_unit = transfer_speed & 7; + + if (speed_unit <= 3) { + u8 frequency = sd_clock_div[(((transfer_speed >> 3) & 0xF) * 4) + speed_unit]; + if (frequency != 0xFF) { + sd_frequency = frequency; + } + } + } + } else { if (!sd_command_response(16, SD_SECTOR_SIZE, &command_response, 1) || command_response != 0) { sd_last_error = SDIO_ERROR_SET_BLOCKLEN; return false; } } - if (sd_command_response(9, 0, &command_response, 1) && command_response == 0 && - sd_read_data_block(csd, sizeof(csd))) { - u8 transfer_speed = csd[3]; - u8 speed_unit = transfer_speed & 7; - if (speed_unit <= 3) { - u8 frequency = sd_clock_div[(((transfer_speed >> 3) & 0xF) * 4) + speed_unit]; - if (frequency != 0xFF) { - sd_frequency = frequency; - } - } - } - sd_initialized = true; sd_last_error = SDIO_ERROR_NONE; return true; diff --git a/src/sd_gc_internal.c b/src/sd_gc_internal.c deleted file mode 100644 index 31f7551..0000000 --- a/src/sd_gc_internal.c +++ /dev/null @@ -1,87 +0,0 @@ -#include - -#include "sd_gc_internal.h" - -#define EXI_REGS ((volatile u32*)0xCC006800) -#define EXI_REG(channel, index) EXI_REGS[(channel) * 5 + (index)] -#define EXI_SPIN_LIMIT 0x10000 - -bool sd_gc_exi_present(s32 channel) { - u32 status; - - if (channel < 0 || channel > 1) { - return false; - } - - status = EXI_REG(channel, 0); - return (status & 0x1000) != 0; -} - -bool sd_gc_exi_begin(s32 channel, u32 frequency, bool select_device) { - u32 status; - - if (channel < 0 || channel > 1 || frequency > 5 || !sd_gc_exi_present(channel)) { - return false; - } - - status = EXI_REG(channel, 0) & 0x405; - status |= frequency << 4; - if (select_device) { - status |= 0x80; - } - EXI_REG(channel, 0) = status; - return true; -} - -bool sd_gc_exi_end(s32 channel) { - if (channel < 0 || channel > 1) { - return false; - } - - EXI_REG(channel, 0) &= ~0x80; - return true; -} - -bool sd_gc_exi_transfer(s32 channel, u8* data, u32 length, u32 mode) { - if (channel < 0 || channel > 1 || data == NULL || (mode != SD_GC_EXI_WRITE && mode != SD_GC_EXI_READ)) { - return false; - } - - while (length > 0) { - u32 chunk = length < 4 ? length : 4; - u32 value = 0; - u32 tries = EXI_SPIN_LIMIT; - - for (u32 i = 0; i < chunk; i++) { - if (mode == SD_GC_EXI_READ) { - data[i] = 0xFF; - } - value |= (u32)data[i] << ((3 - i) * 8); - } - EXI_REG(channel, 4) = value; - - EXI_REG(channel, 3) = (mode << 2) | 1 | ((chunk - 1) << 4); - while (tries > 0 && (EXI_REG(channel, 3) & 1) != 0) { - tries--; - } - if ((EXI_REG(channel, 3) & 1) != 0) { - return false; - } - - if (mode == SD_GC_EXI_READ) { - value = EXI_REG(channel, 4); - for (u32 i = 0; i < chunk; i++) { - data[i] = (u8)(value >> ((3 - i) * 8)); - } - } - - data += chunk; - length -= chunk; - } - - return true; -} - -bool EXISelectSD(long chan, unsigned long dev, unsigned long freq) { - return dev == 0 && sd_gc_exi_begin((s32)chan, (u32)freq, false); -} From 747bdde729a77a7c4fc919ab130b8e22e5f9c47a Mon Sep 17 00:00:00 2001 From: Yanis002 <35189056+Yanis002@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:00:57 +0200 Subject: [PATCH 3/4] self-review --- include/sd.h | 8 ++++++-- lib/hb-D43J.txt | 6 ------ src/init.c | 12 ++++++------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/include/sd.h b/include/sd.h index a1c56f1..ef34f10 100644 --- a/include/sd.h +++ b/include/sd.h @@ -4,6 +4,7 @@ #include #include "types.h" +#include "version.h" #define PAGE_SIZE512 512 @@ -54,6 +55,7 @@ #define SD_ERROR_SUCCESS 0 #define SD_ERROR_INVAL 1 +#if IS_GC #define SDIO_ERROR_NONE 0 #define SDIO_ERROR_NO_EXI 1 #define SDIO_ERROR_IDLE_CLOCKS 2 @@ -73,6 +75,10 @@ #define SD_GC_EXI_WRITE 1 #define SD_GC_EXI_READ 2 +uint32_t sdio_get_error(void); +uint32_t sdio_get_error_detail(void); +#endif + bool sdio_is_initialized(void); bool sdio_is_inserted(void); bool sdio_is_sdhc(void); @@ -80,7 +86,5 @@ bool sdio_write_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer); bool sdio_read_sectors(uint32_t sector, uint32_t sec_cnt, void* buffer); bool sdio_stop(void); bool sdio_start(void); -uint32_t sdio_get_error(void); -uint32_t sdio_get_error_detail(void); #endif diff --git a/lib/hb-D43J.txt b/lib/hb-D43J.txt index 65b2b53..eba3e56 100644 --- a/lib/hb-D43J.txt +++ b/lib/hb-D43J.txt @@ -30,9 +30,3 @@ OSGetTick = 0x800A14F8; ganMapGPR = 0x800ED338; gpSystem = 0x8018B894; gnFlagZelda = 0x8018B968; -OSDisableInterrupts = 0x8009E248; -OSRestoreInterrupts = 0x8009E270; -__OSMaskInterrupts = 0x8009E610; -__EXIProbe = 0x800A2170; -EXIGetID = 0x800A2EF8; -EXIProbeEx = 0x800A2364; diff --git a/src/init.c b/src/init.c index c9c2f2b..af0615e 100644 --- a/src/init.c +++ b/src/init.c @@ -48,11 +48,7 @@ INIT bool _start(Ram* pRAM, s32 nSize) { init_hooks(); homeboy_init(); -#if IS_GC - if (homeboy_obj != NULL) { - homeboy_obj->key = 0x1234; - } -#endif + #ifdef HB_HEAP homeboy_heap_init(); #endif @@ -69,7 +65,11 @@ INIT bool _start(Ram* pRAM, s32 nSize) { init_hb_exceptions(); #endif -#if IS_WII +#if IS_GC + if (homeboy_obj != NULL) { + homeboy_obj->key = 0x1234; + } +#elif IS_WII if (hb_hid < 0) { hb_hid = iosCreateHeap((void*)ios_heap_addr, HB_HEAPSIZE); } From 6719fa1f25d481307828317055a4fb26e9dd3a41 Mon Sep 17 00:00:00 2001 From: Yanis002 <35189056+Yanis002@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:09:05 +0200 Subject: [PATCH 4/4] format --- src/sd_gc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sd_gc.c b/src/sd_gc.c index 2865c02..666a602 100644 --- a/src/sd_gc.c +++ b/src/sd_gc.c @@ -559,7 +559,7 @@ bool sdio_start(void) { sd_read_data_block(csd, sizeof(csd))) { u8 transfer_speed = csd[3]; u8 speed_unit = transfer_speed & 7; - + if (speed_unit <= 3) { u8 frequency = sd_clock_div[(((transfer_speed >> 3) & 0xF) * 4) + speed_unit]; if (frequency != 0xFF) {