Pace the ADMAIF TX FIFO at the programmed I2S rate

The FIFO_FULL flag read never-full, so a PIO sample loop drained a whole
melody in a few hundred milliseconds of emulated time. Silicon paces that
loop with back-pressure, and a payload that checks it played at the right
rate - hwtest's melody test now measures its own duration against fs -
saw a port that was not clocking. Frames pushed drain at the frame rate
audio_rate_hz() derives from I2S_TIMING, on the emulated clock, and the
flag sets while more than the FIFO's depth is still waiting. A take's
drain clock restarts after a pause longer than its backlog, so the codec
I2C traffic between phrases is not counted against the port. The FIFO
state resets with the take and on a soft reset.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZMuJ4deiS5WBkvnEMEdQD
This commit is contained in:
Claude committed 2026-10-31 23:29:39 +00:00
1 parent 4177e98dfb
commit 47f7279f9a
1 file changed
+43 -8
+43 -8
View File
@@ -4189,21 +4189,45 @@ static void i2s_write(EmuState *state, uint64_t addr, uint32_t val) {
// ADMAIF + AXBAR. Two offsets carry behaviour, the rest is write-back: // ADMAIF + AXBAR. Two offsets carry behaviour, the rest is write-back:
// //
// +0x32C TX channel 0 FIFO_WRITE - the PIO sample port. Accept and drop. // +0x32C TX channel 0 FIFO_WRITE - the PIO sample port. Silent: it is
// This MUST be silent: it is written ~224k times per run. // written ~224k times per run.
// +0x744 TX ACIF FIFO_FULL - the flag the sample loop spins on. Bit 0 // +0x744 TX ACIF FIFO_FULL - the flag the sample loop spins on.
// must stay clear or hwtest burns 200,001 reads per sample before //
// giving up, i.e. ~45 billion reads for one melody. // The FIFO drains at the frame rate the payload programmed into I2S_TIMING
// (audio_rate_hz below), on the emulated clock: a frame pushed is gone fs
// frames per second later, and FIFO_FULL reads set while more than the
// FIFO's depth is still waiting. That back-pressure is what paces a PIO
// sample loop on silicon, and a payload that checks it played at the right
// rate - hwtest's melody test measures its own duration against fs - needs
// it here too. It used to read never-full, which let a run finish ~30x
// early and looked, to such a check, like a port that was not clocking.
// //
// Soft-reset registers read back 0 because the payload polls them until they // Soft-reset registers read back 0 because the payload polls them until they
// self-clear (bdk's usual "write 1, wait for 0" handshake). // self-clear (bdk's usual "write 1, wait for 0" handshake).
static uint32_t audio_rate_hz(void);
static uint64_t audio_fifo_t0_us = 0; // emulated time of the take's first frame
static uint64_t audio_fifo_pushed = 0; // frames pushed since then
static const uint64_t AUDIO_FIFO_DEPTH = 64; // frames held before FIFO_FULL
static void audio_fifo_reset(void) {
audio_fifo_t0_us = 0;
audio_fifo_pushed = 0;
}
static bool audio_fifo_full(EmuState *state) {
if (!audio_fifo_pushed)
return false;
uint64_t drained = (state->emu_usec - audio_fifo_t0_us) * audio_rate_hz() / 1000000u;
return audio_fifo_pushed > drained + AUDIO_FIFO_DEPTH;
}
static uint32_t admaif_read(EmuState *state, uint64_t addr) { static uint32_t admaif_read(EmuState *state, uint64_t addr) {
(void)state;
uint32_t offset = (uint32_t)(addr - ADMAIF_BASE); uint32_t offset = (uint32_t)(addr - ADMAIF_BASE);
switch (offset) { switch (offset) {
case 0x704: // global soft reset - completed case 0x704: // global soft reset - completed
case 0x744: // TX ACIF FIFO_FULL - never full, so PIO never spins
return 0; return 0;
case 0x744: // TX ACIF FIFO_FULL, bit 0
return audio_fifo_full(state) ? 1u : 0u;
default: default:
return mmio_regs.get(addr); return mmio_regs.get(addr);
} }
@@ -4339,12 +4363,22 @@ static void audio_write_wav(void) {
audio_play_take(); // buffer is complete: safe to play it straight audio_play_take(); // buffer is complete: safe to play it straight
audio_pcm.clear(); audio_pcm.clear();
audio_pcm_full = false; audio_pcm_full = false;
audio_fifo_reset();
} }
static void admaif_write(EmuState *state, uint64_t addr, uint32_t val) { static void admaif_write(EmuState *state, uint64_t addr, uint32_t val) {
(void)state;
uint32_t offset = (uint32_t)(addr - ADMAIF_BASE); uint32_t offset = (uint32_t)(addr - ADMAIF_BASE);
if (offset == 0x32C) { if (offset == 0x32C) {
// A new take starts its drain clock at the first frame; a frame that
// arrives after the previous ones have all drained also starts afresh,
// so a pause between phrases (codec I2C traffic) is not counted as
// backlog the port never had.
if (!audio_fifo_pushed ||
(state->emu_usec - audio_fifo_t0_us) * audio_rate_hz() / 1000000u >= audio_fifo_pushed)
audio_fifo_reset();
if (!audio_fifo_pushed)
audio_fifo_t0_us = state->emu_usec;
audio_fifo_pushed++;
// One packed word = one stereo frame. Record it rather than dropping it. // One packed word = one stereo frame. Record it rather than dropping it.
if (audio_pcm.size() / 2 < AUDIO_PCM_MAX_FRAMES) { if (audio_pcm.size() / 2 < AUDIO_PCM_MAX_FRAMES) {
audio_pcm.push_back((int16_t)(val & 0xFFFF)); // left audio_pcm.push_back((int16_t)(val & 0xFFFF)); // left
@@ -5192,6 +5226,7 @@ void mmio_soft_reset(EmuState *state, bool power_cycle) {
actmon_glb_period = 0; actmon_glb_period = 0;
audio_pcm.clear(); audio_pcm.clear();
audio_pcm_full = false; audio_pcm_full = false;
audio_fifo_reset();
// Storage controllers. The images stay open; the eMMC is back in its user // Storage controllers. The images stay open; the eMMC is back in its user
// area, as after CMD0. // area, as after CMD0.