OpenCores
URL https://opencores.org/ocsvn/spdif_transmitter/spdif_transmitter/trunk

Subversion Repositories spdif_transmitter

[/] [spdif_transmitter/] [trunk/] [testbench/] [spdif_decoder.cpp] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 ultra_embe
#include "spdif_decoder.h"
2
 
3
#define PREAMBLE_Z         0x17
4
#define PREAMBLE_Y         0x27
5
#define PREAMBLE_X         0x47
6
 
7
#define SPDIF_PREAMBLE_L   0
8
#define SPDIF_PREAMBLE_H   3
9
#define SPDIF_PREAMBLE_W   4
10
 
11
#define SPDIF_SAMPLE_L     4
12
#define SPDIF_SAMPLE_H     27
13
#define SPDIF_SAMPLE_W     24
14
 
15
#define SPDIF_NVALID_L     28
16
#define SPDIF_NVALID_H     28
17
#define SPDIF_NVALID_W     1
18
 
19
#define SPDIF_PARITY_L     31
20
#define SPDIF_PARITY_H     31
21
#define SPDIF_PARITY_W     1
22
 
23
#define SPDIF_FIELD(v,n)   v.range(SPDIF_##n##_H, SPDIF_##n##_L)
24
 
25
//-----------------------------------------------------------------
26
// input: Handle rx data
27
//-----------------------------------------------------------------
28
void spdif_decoder::input(void)
29
{
30
    sc_uint <64> bmc_data = 0;
31
    sc_uint <32> data     = 0;
32
 
33
    do
34
    {
35
        wait();
36
    }
37
    while (rst_i.read());
38
 
39
    bool last_rx  = false;
40
    int  subframe = 0;
41
    bool rx       = false;
42
    while (true)
43
    {
44
        rx = rx_i.read();
45
 
46
        // Detect transition in preamble
47
        if (rx == false && last_rx == true)
48
        {
49
            // Wait for half a bit time to sample mid bit
50
            wait(m_divisor/2);
51
 
52
            // Preamble leading bits were 0111
53
            bmc_data = 0x7;
54
 
55
            // Capture remaining timeslots
56
            for (int i=4;i<64;i++)
57
            {
58
                wait(m_divisor);
59
                rx = rx_i.read();
60
                bmc_data[i] = rx;
61
            }
62
 
63
            // Check preamble type is expected
64
            if (subframe == 0)
65
                sc_assert(bmc_data.range(7,0) == PREAMBLE_Z);
66
            else if (subframe & 1)
67
                sc_assert(bmc_data.range(7,0) == PREAMBLE_Y);
68
            else
69
                sc_assert(bmc_data.range(7,0) == PREAMBLE_X);
70
 
71
            if (++subframe == 384)
72
                subframe = 0;
73
 
74
            // Decode BMC data
75
            for (int i=0;i<64;i+=2)
76
            {
77
                if (bmc_data[i+0] != bmc_data[i+1])
78
                    data[i/2] = 1;
79
                else
80
                    data[i/2] = 0;
81
            }
82
 
83
            // Check parity
84
            int ones_count = 0;
85
            for (int i=SPDIF_PREAMBLE_H+1;i<SPDIF_PARITY_L;i++)
86
                if (data[i])
87
                    ones_count += 1;
88
 
89
            if (ones_count & 1)
90
                sc_assert(SPDIF_FIELD(data, PARITY));
91
            else
92
                sc_assert(!SPDIF_FIELD(data, PARITY));
93
 
94
            sc_uint <SPDIF_SAMPLE_W> sample = SPDIF_FIELD(data, SAMPLE);
95
 
96
            // Valid sample
97
            if (!SPDIF_FIELD(data, NVALID))
98
            {
99
                sc_assert(m_rx_fifo.num_free() > 0);
100
 
101
                if (m_bits == 16)
102
                    m_rx_fifo.write(sample >> (SPDIF_SAMPLE_W - 16));
103
                else if (m_bits == 20)
104
                    m_rx_fifo.write(sample >> (SPDIF_SAMPLE_W - 20));
105
                else if (m_bits == 24)
106
                    m_rx_fifo.write(sample >> (SPDIF_SAMPLE_W - 24));
107
                else
108
                    sc_assert(!"Unsupported bit width");
109
            }
110
        }
111
        else
112
            wait();
113
 
114
        last_rx = rx;
115
    }
116
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.