blob: 9b2b18e7291df88513f5daaf0c5b840d0cd11809 (
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include "Vcic3_pdm.h"
#include "verilated.h"
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
VerilatedContext *cp = new VerilatedContext;
Vcic3_pdm *top = new Vcic3_pdm{cp};
// TODO: bring back high pass filter and adjustable shift
//top->dc_alpha = hpf_alpha;
//top->scale_shift = scale_shift;
// Reset on start
top->rst = 0;
top->eval();
top->rst = 1;
top->eval();
top->rst = 0;
top->eval();
// Start clock off
top->clk = 0;
// Go through all the input data
int pcm_sample = 0;
bool last_valid = top->pcm_valid;
for (int i = 0; i < pdm_length; i++) {
top->pdm_in = bool(pdm[i]);
top->clk = 1;
top->eval();
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;
}
|