Files
RCM-Emulator/t210/sdmmc.cpp
T
Baptiste MOINE a71ccdf682 Take a dropped payload, and stop drowning the console
Double-clicking the emulator printed a usage line to a console nobody sees
and exited. It now opens a window that accepts a dropped .bin, with O for the
system file dialog - GetOpenFileName on Windows, zenity or kdialog on Linux,
since there is no such thing there without dragging in a toolkit. The window
draws a dashed frame and an upload glyph from rectangles and one filled
triangle; no text, because a font would have to be vendored for a screen that
lives a few seconds and the instruction is on the title bar already. It is
created and destroyed before the emulator's own display comes up, so it
cannot interfere with the framebuffer window. A headless or toolkit-less
machine falls back to the old usage message, and dragging a payload onto the
executable still skips all of it - that arrives as argv[1].

Dropping a payload was then unusably slow, and the reason had nothing to do
with the picker. A full hwtest sweep printed 52,452 lines, of which about
51,900 were per-register-access traces: 29,673 [gpio], 16,707 [mmio], 5,430
[sdmmc]. Redirected to a file that is merely wasteful, which is why it went
unnoticed - every scripted run here redirects. On a Windows console it is
not: each line goes through conhost one write at a time. Those traces now
need --trace or RCM_EMU_TRACE=1, which drops default output to 578 lines -
the payload's own UART plus the handful of [emu], [display], [pcie] and
[audio] lines worth seeing. The flag brings all 48,354 back when a
register-level question needs answering.

Also adds RCM_EMU_PICK, which runs the picker's window lifecycle and returns
a path as if it had been dropped, so that path can be tested by a script
instead of only by a human dragging a file.
2026-08-20 10:29:28 +02:00

241 lines
7.9 KiB
C++

#include "sdmmc.h"
#include "trace.h"
#include "memory_map.h"
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <vector>
#include <sys/types.h>
#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<uint8_t> 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;
TRACE("[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;
}
}