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.
133 lines
4.5 KiB
C++
133 lines
4.5 KiB
C++
/**
|
|
* @file harness.h
|
|
* @brief Minimal headless test harness for the T-Deck emulator shims.
|
|
*
|
|
* Must be included BEFORE the sketch (it only depends on the shims, never on
|
|
* sketch internals). Sketch-level helpers (stepSim, units, ...) live in the
|
|
* test suites themselves, which are included AFTER the sketch.
|
|
*
|
|
* Framework: TEST(name) registers a case; EXPECT/EXPECT_EQ assert; the runner
|
|
* in test_main.cpp / e2e_tests.cpp reports failures and exits non-zero.
|
|
*/
|
|
#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>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace test {
|
|
|
|
struct Case {
|
|
const char *name;
|
|
void (*fn)();
|
|
};
|
|
inline std::vector<Case> &cases()
|
|
{
|
|
static std::vector<Case> c;
|
|
return c;
|
|
}
|
|
struct Registrar {
|
|
Registrar(const char *name, void (*fn)()) { cases().push_back({name, fn}); }
|
|
};
|
|
|
|
inline int g_failures = 0;
|
|
inline std::string g_current; // full case name, e.g. "unit/map_queries"
|
|
inline std::string g_group; // prefix before the first '/'
|
|
|
|
inline void fail(const char *file, int line, const std::string &what)
|
|
{
|
|
std::printf(" FAIL [%s/%s] %s:%d %s\n", g_group.c_str(), g_current.c_str(),
|
|
file, line, what.c_str());
|
|
g_failures++;
|
|
}
|
|
|
|
// --- serial transcript helpers (Serial.sink is set by captureSerial()) ---
|
|
inline std::string serialJoined()
|
|
{
|
|
std::string all;
|
|
if (Serial.sink)
|
|
for (const auto &l : *Serial.sink) all += l;
|
|
return all;
|
|
}
|
|
inline bool serialHas(const std::string &pat)
|
|
{
|
|
return serialJoined().find(pat) != std::string::npos;
|
|
}
|
|
inline void serialClear()
|
|
{
|
|
if (Serial.sink) Serial.sink->clear();
|
|
}
|
|
inline void captureSerial()
|
|
{
|
|
static std::deque<std::string> ring;
|
|
Serial.sink = ˚
|
|
}
|
|
|
|
// --- input pipeline helpers (the same state the SDL host writes) ---
|
|
inline void resetInput()
|
|
{
|
|
for (auto &p : emu::pinLevels) p = 1; // everything pulled up
|
|
while (!emu::keyboard.empty()) emu::keyboard.pop_front();
|
|
emu::touchDown = false;
|
|
emu::touchX = emu::touchY = 0;
|
|
}
|
|
// 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)
|
|
{
|
|
if (x < 0 || y < 0 || x >= emuGfx::W || y >= emuGfx::H) return 0;
|
|
return emuGfx::fb[y * emuGfx::W + x];
|
|
}
|
|
inline bool pixelIs565(int x, int y, uint16_t c565)
|
|
{
|
|
return pixel(x, y) == rgb565to888(c565);
|
|
}
|
|
|
|
} // namespace test
|
|
|
|
// The display name may contain '/' (e.g. "unit/map_queries") — identifiers
|
|
// are generated from __LINE__, so the name string itself is never pasted.
|
|
#define TEST_CAT2(a, b) a##b
|
|
#define TEST_CAT(a, b) TEST_CAT2(a, b)
|
|
#define TEST(name) \
|
|
static void TEST_CAT(test_case_, __LINE__)(); \
|
|
static test::Registrar TEST_CAT(reg_case_, __LINE__)( \
|
|
#name, TEST_CAT(test_case_, __LINE__)); \
|
|
static void TEST_CAT(test_case_, __LINE__)()
|
|
|
|
#define EXPECT(cond) \
|
|
do { \
|
|
if (!(cond)) test::fail(__FILE__, __LINE__, "expected " #cond); \
|
|
} while (0)
|
|
|
|
#define EXPECT_EQ(a, b) \
|
|
do { \
|
|
if (!((a) == (b))) \
|
|
test::fail(__FILE__, __LINE__, \
|
|
std::string(#a " == " #b) + " (got: " + \
|
|
std::to_string((long long)(a)) + " vs " + \
|
|
std::to_string((long long)(b)) + ")"); \
|
|
} while (0)
|