#include "sdmmc.h" #include "memory_map.h" #include #include #include #include #include #include #include #define SDHCI_PRESENT_STATE 0x24 #define SDHCI_CMD_REG 0x0E #define SDHCI_TRANSFER_MODE 0x0C #define SDHCI_BLOCK_SIZE 0x04 #define SDHCI_BLOCK_COUNT 0x06 #define SDHCI_ARGUMENT 0x08 #define SDHCI_RESPONSE 0x10 #define SDHCI_INT_STATUS 0x30 #define SDHCI_ADMA_ADDR 0x58 #define SDHCI_HOST_CONTROL 0x28 #define SDHCI_INT_RESPONSE 1 #define SDHCI_INT_DATA_AVAIL (1 << 1) #define SDHCI_INT_DMA (1 << 3) #define SDHCI_INT_XFER_DONE (1 << 1) // In norintsts, bit 1 is Transfer Complete // SDHCI Status Bits #define SDHCI_CMD_INHIBIT 1 #define SDHCI_DATA_INHIBIT 2 #define SDHCI_CARD_PRESENT (1 << 16) #define SDHCI_DATA_0_LVL (1 << 20) void sdmmc_init_storage(EmuState *state) { if (state->sd_fd == -1) { state->sd_fd = open("sd_card.bin", O_RDWR); if (state->sd_fd == -1) { // Create a small empty file if it doesn't exist state->sd_fd = open("sd_card.bin", O_RDWR | O_CREAT, 0644); if (state->sd_fd != -1) { if (ftruncate(state->sd_fd, 64 * 1024 * 1024) != 0) perror("ftruncate sd_card.bin"); } } } if (state->emmc_boot0_fd == -1) { state->emmc_boot0_fd = open("emmc_boot0.bin", O_RDWR | O_CREAT, 0644); if (state->emmc_boot0_fd != -1) { if (ftruncate(state->emmc_boot0_fd, 4 * 1024 * 1024) != 0) perror("ftruncate emmc_boot0.bin"); } } } static void handle_adma2(uc_engine *uc, EmuState *state, int id) { uint64_t adma_addr = (id == 1) ? state->sdmmc_adma_addr : state->sdmmc4_adma_addr; int fd = (id == 1) ? state->sd_fd : state->emmc_boot0_fd; // Simple mock: always boot0 for now uint32_t arg = (id == 1) ? state->sdmmc_arg : state->sdmmc4_arg; uint16_t trn = (id == 1) ? state->sdmmc_trnmod : state->sdmmc4_trnmod; // trn & 0x10 == 0x10 means READ if (!(trn & 0x10)) return; // ADMA2 Descriptor Table struct { uint16_t attr; uint16_t len; uint32_t addr; } desc; uint64_t sector = arg; if (id == 4) sector = arg; // Adjust if needed while (true) { if (uc_mem_read(uc, adma_addr, &desc, sizeof(desc)) != UC_ERR_OK) break; // Attr bits: 0=Valid, 1=End, 2=Int, 4=Act1, 5=Act2 if (!(desc.attr & 1)) break; // Not valid uint32_t act = (desc.attr >> 4) & 3; if (act == 2) { // Transfer data uint32_t len = desc.len ? desc.len : 65536; std::vector buffer(len); lseek(fd, (off_t)sector * 512, SEEK_SET); if (read(fd, buffer.data(), len) != (ssize_t)len) { // Handle short read or error if necessary } uc_mem_write(uc, desc.addr, buffer.data(), len); sector += (len / 512); } if (desc.attr & 2) break; // End bit adma_addr += 8; } // Set Transfer Complete interrupt if (id == 1) state->sdmmc_norintsts |= 0x01; else state->sdmmc4_norintsts |= 0x01; } static void handle_sd_command(uc_engine *uc, EmuState *state, int id, uint32_t cmd) { uint32_t *arg = (id == 1) ? &state->sdmmc_arg : &state->sdmmc4_arg; uint32_t *rsp = (id == 1) ? state->sdmmc_rsp : state->sdmmc4_rsp; uint32_t *norintsts = (id == 1) ? &state->sdmmc_norintsts : &state->sdmmc4_norintsts; bool *last_55 = (id == 1) ? &state->last_cmd_was_55 : &state->last_cmd4_was_55; printf("[sdmmc%d] CMD%d Arg: 0x%08X\n", id, cmd, *arg); *norintsts |= 0x01; // Command Complete interrupt switch (cmd) { case 0: // GO_IDLE_STATE break; case 8: // SEND_IF_COND rsp[0] = *arg; // Echo check pattern break; case 55: // APP_CMD *last_55 = true; break; case 41: // ACMD41: SD_SEND_OP_COND if (*last_55) { rsp[0] = 0xC0FF8000; // Ready, High Capacity, Voltage range *last_55 = false; } break; case 1: // SEND_OP_COND (eMMC) rsp[0] = 0x80FF8080; // Ready, High Capacity break; case 2: // ALL_SEND_CID rsp[0] = 0x12345678; rsp[1] = 0xABCDEF01; rsp[2] = 0x23456789; rsp[3] = 0x88; break; case 3: // SEND_RELATIVE_ADDR rsp[0] = 0x00010000; // RCA = 1 break; case 9: // SEND_CSD rsp[0] = 0x400E0032; rsp[1] = 0x5B590000; rsp[2] = 0x00007F3F; rsp[3] = 0x0A400000; break; case 7: // SELECT_CARD break; case 17: // READ_SINGLE_BLOCK case 18: // READ_MULTIPLE_BLOCK handle_adma2(uc, state, id); break; default: break; } } uint32_t sdmmc1_read(EmuState *state, uint64_t addr) { uint32_t offset = (uint32_t)(addr - SDMMC1_BASE); switch (offset) { case SDHCI_PRESENT_STATE: return SDHCI_CARD_PRESENT | SDHCI_DATA_0_LVL; case SDHCI_INT_STATUS: return state->sdmmc_norintsts | (state->sdmmc_errintsts << 16); case SDHCI_RESPONSE + 0: return state->sdmmc_rsp[0]; case SDHCI_RESPONSE + 4: return state->sdmmc_rsp[1]; case SDHCI_RESPONSE + 8: return state->sdmmc_rsp[2]; case SDHCI_RESPONSE + 12: return state->sdmmc_rsp[3]; case 0x1FC: return 0x0303; // Host Controller Version default: return 0; } } void sdmmc1_write(uc_engine *uc, EmuState *state, uint64_t addr, uint32_t val) { uint32_t offset = (uint32_t)(addr - SDMMC1_BASE); switch (offset) { case SDHCI_ARGUMENT: state->sdmmc_arg = val; break; case SDHCI_CMD_REG: handle_sd_command(uc, state, 1, (val >> 8) & 0x3F); break; case SDHCI_TRANSFER_MODE: state->sdmmc_trnmod = val; break; case SDHCI_ADMA_ADDR: state->sdmmc_adma_addr = (state->sdmmc_adma_addr & 0xFFFFFFFF00000000ULL) | val; break; case SDHCI_ADMA_ADDR + 4: state->sdmmc_adma_addr = (state->sdmmc_adma_addr & 0xFFFFFFFFULL) | ((uint64_t)val << 32); break; case SDHCI_INT_STATUS: state->sdmmc_norintsts &= ~(val & 0xFFFF); state->sdmmc_errintsts &= ~(val >> 16); break; case 0x2C: // CLKCON - poll for stable // Reads of CLKCON return SDHCI_CLOCK_INT_STABLE; writes are dropped here // since this module's matching read path returns the stable bit unconditionally. (void)addr; (void)val; (void)state; break; case 0x2F: // SWRST break; } } uint32_t sdmmc4_read(EmuState *state, uint64_t addr) { uint32_t offset = (uint32_t)(addr - SDMMC4_BASE); switch (offset) { case SDHCI_PRESENT_STATE: return SDHCI_CARD_PRESENT | SDHCI_DATA_0_LVL; case SDHCI_INT_STATUS: return state->sdmmc4_norintsts | (state->sdmmc4_errintsts << 16); case SDHCI_RESPONSE + 0: return state->sdmmc4_rsp[0]; case SDHCI_RESPONSE + 4: return state->sdmmc4_rsp[1]; case SDHCI_RESPONSE + 8: return state->sdmmc4_rsp[2]; case SDHCI_RESPONSE + 12: return state->sdmmc4_rsp[3]; default: return 0; } } void sdmmc4_write(uc_engine *uc, EmuState *state, uint64_t addr, uint32_t val) { uint32_t offset = (uint32_t)(addr - SDMMC4_BASE); switch (offset) { case SDHCI_ARGUMENT: state->sdmmc4_arg = val; break; case SDHCI_CMD_REG: handle_sd_command(uc, state, 4, (val >> 8) & 0x3F); break; case SDHCI_TRANSFER_MODE: state->sdmmc4_trnmod = val; break; case SDHCI_ADMA_ADDR: state->sdmmc4_adma_addr = (state->sdmmc4_adma_addr & 0xFFFFFFFF00000000ULL) | val; break; case SDHCI_ADMA_ADDR + 4: state->sdmmc4_adma_addr = (state->sdmmc4_adma_addr & 0xFFFFFFFFULL) | ((uint64_t)val << 32); break; case SDHCI_INT_STATUS: state->sdmmc4_norintsts &= ~(val & 0xFFFF); state->sdmmc4_errintsts &= ~(val >> 16); break; } }