summaryrefslogtreecommitdiff
path: root/bindings/sim_cic3_pdm.cc
diff options
context:
space:
mode:
authorJon Nordby <jononor@gmail.com>2025-08-12 23:16:41 +0200
committerJon Nordby <jononor@gmail.com>2025-08-12 23:16:41 +0200
commit45f5b98c480bba9736c2adbd65e46cabfb435959 (patch)
tree4145c89a345535c1a27ac8ae2a52b692b6ff911c /bindings/sim_cic3_pdm.cc
parenta8733312ed2e67f8033d7dbe45008b2ed2e595be (diff)
pdm: Fix error handling around PDM to PCM with verilator
Diffstat (limited to 'bindings/sim_cic3_pdm.cc')
-rw-r--r--bindings/sim_cic3_pdm.cc20
1 files changed, 19 insertions, 1 deletions
diff --git a/bindings/sim_cic3_pdm.cc b/bindings/sim_cic3_pdm.cc
index 323b2dd..9b2b18e 100644
--- a/bindings/sim_cic3_pdm.cc
+++ b/bindings/sim_cic3_pdm.cc
@@ -1,6 +1,7 @@
#include <cstdint>
#include <cstdio>
+#include <stdexcept>
#include "Vcic3_pdm.h"
#include "verilated.h"
@@ -8,6 +9,12 @@
int
pdm2pcm_cic3(const uint8_t *pdm, int64_t pdm_length, int16_t *pcm, int32_t pcm_length, uint8_t hpf_alpha, uint8_t scale_shift)
{
+ const int DECIMATION = 64;
+
+ const int expect_length = pdm_length / DECIMATION;
+ if (pcm_length < expect_length) {
+ throw std::runtime_error("PCM buffer too small");
+ }
// FIXME: verify that output buffer is large enough
@@ -32,6 +39,7 @@ pdm2pcm_cic3(const uint8_t *pdm, int64_t pdm_length, int16_t *pcm, int32_t pcm_l
// Go through all the input data
int pcm_sample = 0;
+ bool last_valid = top->pcm_valid;
for (int i = 0; i < pdm_length; i++) {
@@ -39,13 +47,23 @@ pdm2pcm_cic3(const uint8_t *pdm, int64_t pdm_length, int16_t *pcm, int32_t pcm_l
top->clk = 1;
top->eval();
- if (top->pcm_valid) {
+ if (pcm_sample >= pcm_length) {
+ throw std::runtime_error("PCM buffer overrun: pcm_sample=" + std::to_string(pcm_sample) + " pdm_sample="+std::to_string(i));
+ }
+ const bool valid = top->pcm_valid;
+ // && (!last_valid);
+ if (valid) {
pcm[pcm_sample++] = top->pcm_out;
}
+ last_valid = top->pcm_valid;
top->clk = 0;
top->eval();
}
+ if (pcm_sample != expect_length) {
+ throw std::runtime_error("Wrong length. expected="+std::to_string(expect_length) + std::string(" got=")+std::to_string(pcm_sample));
+ }
+
return pcm_sample;
}