Fix short beeps silent on HW: i2s_zero_dma_buffer erased the unplayed DMA ring

The legacy I2S driver buffers i2s_write() data in a TX DMA ring
(8x64 samples = 64 ms @ 8 kHz) and hands a buffer back to i2s_write()
only after the DMA finished playing it. After the last queued event
the sound task called i2s_zero_dma_buffer(), which memsets the whole
ring instantly — erasing everything not yet played. Any sound <= 64 ms
was written into free ring space without ever blocking, so the zeroing
wiped it before the DMA reached a single sample: completely silent on
hardware. Longer tones force i2s_write() to block on a full ring,
pacing rendering to real-time playback — hence only long beeps were
audible, and the fade-envelope cap never moved the threshold. Every
game beep <= 80 ms (link-active 10, laser 30, sever 40, bleep/ICE 50,
feedback 80) sat in the dead zone. The emulator never showed it: its
i2s_write shim fed SDL directly and had no ring at all.

Fix: replace the instant zeroing with a paced silence purge — once the
queue drains, write one ring + one chunk of zero samples through
i2s_write(), reusing each DMA buffer only after the DMA played it. The
sound plays out fully and the ring then loops silence (still kills the
tail-replay bug the zeroing existed for). Keep the envelope cap at
total/4 so short tones keep a steady-state portion.

Emulator shim gains a TX DMA ring model (write/play cursors + bounded
DAC-emitted log) so the bug class reproduces headlessly: unit/
sound_short_tone_reaches_dac fails on the zero_dma code, passes on the
purge (A/B verified); shim_zero_dma_erases_unplayed pins model fidelity;
sound_long_tone_plays_out_fully guards against truncation. SoundEngine
gains a pumpOne() test seam and begin(spawnTask) for deterministic
driving. Full suite: 26 unit + 7 integration + 4 E2E, 0 failures.
This commit is contained in:
Dejvino
2026-11-15 12:19:21 +01:00
parent 8008b78db0
commit 7df1a2d785
6 changed files with 303 additions and 24 deletions
+17
View File
@@ -11,6 +11,7 @@
*/
#pragma once
#include "Arduino.h"
#include "driver/i2s.h" // emu::emuDmaRing — TX DMA ring model
#include "TFT_eSPI.h" // rgb565to888 for pixel assertions
#include "emu_gfx.h"
#include <cstdio>
@@ -77,6 +78,22 @@ inline void resetInput()
// queue a key on the fake 0x55 keyboard device (consumed by pollKeyboard())
inline void typeKey(char c) { emu::keyboard.push_back((uint8_t)c); }
// --- audio helpers (the TX DMA ring model in the i2s shim) ---
// resetAudio(): empty + zero the ring, clear the "what the DAC emitted" log.
// Keeps the capacity configured by i2s_driver_install(); when no install has
// happened yet it defaults to the sound engine's geometry (8 x 64 = 512).
inline void resetAudio()
{
auto &r = emu::emuDmaRing();
r.reset(r.cap ? r.cap : 512);
}
// samples the DAC actually emitted (may be far fewer than i2s_write() got —
// that gap is exactly the class of hardware bug the ring model exposes)
inline const std::deque<int16_t> &playedAudio()
{
return emu::emuDmaRing().played;
}
// --- framebuffer helpers (E2E pixel assertions) ---
inline uint32_t pixel(int x, int y)
{