summaryrefslogtreecommitdiff
path: root/bindings/sim_cic3_pdm.cc
blob: 03682170969baa7926b4ce95d2d0c464d05d48bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#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;
}