From c86294b1f07624eee3c3749fb877aff96413f17b Mon Sep 17 00:00:00 2001 From: Jon Nordby Date: Wed, 14 May 2025 19:55:05 +0200 Subject: bindings: Actually build cic3_pdm code --- bindings/Makefile | 3 +-- bindings/galearn_pdm.cpp | 16 +++++----------- bindings/setup.py | 2 +- bindings/sim_cic3_pdm.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 bindings/sim_cic3_pdm.cc (limited to 'bindings') diff --git a/bindings/Makefile b/bindings/Makefile index c76d3a0..c1f19f0 100644 --- a/bindings/Makefile +++ b/bindings/Makefile @@ -11,7 +11,7 @@ clean: .PHONY: all check clean -${SHARED_LIB}: ../verilator_lib/mul.v ../verilator_lib/sim.cc +${SHARED_LIB}: ../cocotb_try/cic3_pdm.v ./sim_cic3_pdm.cc verilator --cc --lib-create sim --build -j 0 -Wall $^ check: ${PY_MODULE} @@ -20,4 +20,3 @@ check: ${PY_MODULE} ${PY_MODULE}: ${SHARED_LIB} python setup.py build_ext --inplace - diff --git a/bindings/galearn_pdm.cpp b/bindings/galearn_pdm.cpp index 5b7e4f5..541ba1a 100644 --- a/bindings/galearn_pdm.cpp +++ b/bindings/galearn_pdm.cpp @@ -1,11 +1,12 @@ #include #include -int sim(void); +int pdm2pcm_cic3(const uint8_t *pdm, int pdm_length, int16_t *pcm, int pcm_length); + namespace py = pybind11; -void process(py::array_t arr1, py::array_t arr2) { +int process(py::array_t arr1, py::array_t arr2) { // Check shapes or sizes if needed auto buf1 = arr1.request(); auto buf2 = arr2.request(); @@ -18,19 +19,12 @@ void process(py::array_t arr1, py::array_t arr2) { throw std::runtime_error("Input 1 must be same or larger than input 2"); } -#if 0 - // Example: access data uint8_t* in = static_cast(buf1.ptr); int16_t* out = static_cast(buf2.ptr); - for (int i=0; i +#include + +#include "Vcic3_pdm.h" +#include "verilated.h" + + +int +pdm2pcm_cic3(const uint8_t *pdm, int pdm_length, int16_t *pcm, int pcm_length) +{ + + // FIXME: verify that output buffer is large enough + + VerilatedContext *cp = new VerilatedContext; + + Vcic3_pdm *top = new Vcic3_pdm{cp}; + + // Start clock off + top->clk = 0; + + // Go through all the input data + int pcm_sample = 0; + + for (int i = 0; i < pdm_length; i++) { + + top->pdm_in = bool(pdm[i]); + top->clk = 1; + top->eval(); + + if (top->pcm_valid) { + pcm[pcm_sample++] = top->pcm_out; + } + + top->clk = 0; + top->eval(); + } + + return pcm_sample; +} -- cgit v1.2.3