diff options
author | Jon Nordby <jononor@gmail.com> | 2025-05-14 19:55:05 +0200 |
---|---|---|
committer | Jon Nordby <jononor@gmail.com> | 2025-05-14 19:55:05 +0200 |
commit | c86294b1f07624eee3c3749fb877aff96413f17b (patch) | |
tree | 938838ca4a52685f772768af79f501d0aea429d5 | |
parent | 8b2563ef4764f93901d65fffc786ae1a9ae9774d (diff) |
bindings: Actually build cic3_pdm code
-rw-r--r-- | bindings/Makefile | 3 | ||||
-rw-r--r-- | bindings/galearn_pdm.cpp | 16 | ||||
-rw-r--r-- | bindings/setup.py | 2 | ||||
-rw-r--r-- | bindings/sim_cic3_pdm.cc | 40 | ||||
-rw-r--r-- | cocotb_try/cic3_pdm.v | 2 |
5 files changed, 48 insertions, 15 deletions
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 <pybind11/pybind11.h> #include <pybind11/numpy.h> -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<uint8_t> arr1, py::array_t<int16_t> arr2) { +int process(py::array_t<uint8_t> arr1, py::array_t<int16_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<uint8_t> arr1, py::array_t<int16_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<uint8_t*>(buf1.ptr); int16_t* out = static_cast<int16_t*>(buf2.ptr); - for (int i=0; i<buf2.size; i++) { - out[i] = in[i] + 1; - } -#else - - sim(); -#endif + int samples = pdm2pcm_cic3(in, arr1.size(), out, arr2.size()); + return samples; } PYBIND11_MODULE(galearn_pdm, m) { diff --git a/bindings/setup.py b/bindings/setup.py index c2f93f7..0b18130 100644 --- a/bindings/setup.py +++ b/bindings/setup.py @@ -3,7 +3,7 @@ import pybind11 import numpy import os.path -verilated_build_dir = '../verilator_lib/obj_dir/' +verilated_build_dir = './obj_dir/' ext_modules = [ Extension( diff --git a/bindings/sim_cic3_pdm.cc b/bindings/sim_cic3_pdm.cc new file mode 100644 index 0000000..e21010b --- /dev/null +++ b/bindings/sim_cic3_pdm.cc @@ -0,0 +1,40 @@ + +#include <cstdint> +#include <cstdio> + +#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; +} diff --git a/cocotb_try/cic3_pdm.v b/cocotb_try/cic3_pdm.v index 2826715..a8cc715 100644 --- a/cocotb_try/cic3_pdm.v +++ b/cocotb_try/cic3_pdm.v @@ -9,7 +9,7 @@ module cic3_pdm ( output wire pcm_valid ); - parameter DECIMATION = 64; // Decimation factor + //parameter DECIMATION = 64; // Decimation factor parameter OUTPUT_SHIFT = 8; // Can tune this // Internal registers |