// tb_fir_hls.cpp // C++ testbench for fir_hls.cpp. This is the testbench an HLS tool runs in // C simulation before synthesis; here it is compiled with clang++. // // tb_fir_hls self-checks, prints PASS, exit status 0 // tb_fir_hls in.txt out.txt filters the integers in in.txt (one per line) // in blocks of varying length, writes out.txt #include #include #include "fir_hls.hpp" namespace { // Independent direct-form reference: whole signal at once, no static state. std::vector reference(const std::vector& x, const std::vector& h) { std::vector y(x.size()); for (std::size_t n = 0; n < x.size(); n++) { acc_t acc = 0; for (int k = 0; k < NTAPS && static_cast(k) <= n; k++) { acc += acc_t(x[n - k]) * h[k]; } acc_t r = (acc + (acc_t(1) << (FRAC - 1))) >> FRAC; r = r > 32767 ? 32767 : (r < -32768 ? -32768 : r); y[n] = static_cast(r); } return y; } // Runs the kernel over x in blocks whose lengths cycle through `blocks`. std::vector run_blocks(const std::vector& x, const std::vector& blocks) { std::vector y(x.size()); std::vector in(MAX_LEN), out(MAX_LEN); std::size_t pos = 0; int first = 1; for (std::size_t b = 0; pos < x.size(); b++) { std::size_t len = static_cast(blocks[b % blocks.size()]); if (len > x.size() - pos) len = x.size() - pos; for (std::size_t i = 0; i < len; i++) in[i] = x[pos + i]; fir_hls(in.data(), out.data(), static_cast(len), first); for (std::size_t i = 0; i < len; i++) y[pos + i] = out[i]; pos += len; first = 0; } return y; } int fails = 0; void expect(bool ok, const char* what) { if (!ok) { std::printf("FAIL: %s\n", what); fails++; } } } // namespace int main(int argc, char** argv) { // Recover the coefficients from the kernel itself: the response to a // unit impulse of 1.0 - 2^-15 (32767) is round(32767 * h / 32768). std::vector imp(64, 0); imp[0] = 32767; std::vector y_imp = run_blocks(imp, {64}); const std::vector h = {-39, -67, -68, 0, 156, 324, 327, 0, -621, -1189, -1139, 0, 2249, 5022, 7322, 8216, 7322, 5022, 2249, 0, -1139, -1189, -621, 0, 327, 324, 156, 0, -68, -67, -39}; if (argc == 3) { std::FILE* fi = std::fopen(argv[1], "r"); std::FILE* fo = std::fopen(argv[2], "w"); if (!fi || !fo) return 2; std::vector x; int v; while (std::fscanf(fi, "%d", &v) == 1) x.push_back(static_cast(v)); std::vector y = run_blocks(x, {1, 7, 256, 1024, 31, 500}); for (sample_t s : y) std::fprintf(fo, "%d\n", s); std::fclose(fi); std::fclose(fo); return 0; } // 1. Impulse response matches the coefficient table. std::vector y_ref = reference(imp, h); expect(y_imp == y_ref, "impulse response"); expect(y_imp[15] == 8216 && y_imp[31] == 0, "impulse response centre tap and tail"); // 2. Pseudo-random input, sent in blocks of awkward lengths. std::vector x(5000); unsigned s = 12345u; for (auto& e : x) { s = s * 1103515245u + 12345u; // simple LCG, fixed seed e = static_cast(static_cast((s >> 8) & 0xFFFF) - 32768); } expect(run_blocks(x, {1, 3, 1024, 17, 600}) == reference(x, h), "random input in blocks"); // 3. clear really clears: a second run from the same start gives the same output. expect(run_blocks(x, {1024}) == reference(x, h), "clear between runs"); // 4. Saturation: the DC gain is 32770/32768, so full-scale DC overflows. std::vector dc_pos(100, 32767), dc_neg(100, -32768); expect(run_blocks(dc_pos, {100}).back() == 32767, "positive saturation"); expect(run_blocks(dc_neg, {100}).back() == -32768, "negative saturation"); // 5. len larger than MAX_LEN is clamped, not an out-of-bounds access. std::vector in(MAX_LEN, 100), out(MAX_LEN, 0); fir_hls(in.data(), out.data(), MAX_LEN + 5, 1); expect(out[MAX_LEN - 1] == reference(in, h)[MAX_LEN - 1], "len clamp"); if (fails == 0) { std::printf("PASS: fir_hls C simulation\n"); return 0; } return 1; }