summaryrefslogtreecommitdiffstats
path: root/src/PrbsGenerator.cpp
blob: 64baf893193195cfce7706cc71455cc00b5f5882 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty
   the Queen in Right of Canada (Communications Research Center Canada)
 */
/*
   This file is part of ODR-DabMod.

   ODR-DabMod is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   ODR-DabMod is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with ODR-DabMod.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "PrbsGenerator.h"
#include "PcDebug.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdexcept>


PrbsGenerator::PrbsGenerator(size_t framesize, uint32_t polynomial,
        uint32_t accum, size_t init) :
    ModCodec(ModFormat(0), ModFormat(framesize)),
    d_framesize(framesize),
    d_polynomial(polynomial),
    d_accum(accum),
    d_accum_init(accum),
    d_init(init)
{
    PDEBUG("PrbsGenerator::PrbsGenerator(%zu, %u, %u, %zu) @ %p\n",
            framesize, polynomial, accum, init, this);

    gen_prbs_table();
    gen_weight_table();
}


PrbsGenerator::~PrbsGenerator()
{
    PDEBUG("PrbsGenerator::~PrbsGenerator() @ %p\n", this);

}


/*
 * Generate a table of matrix products to update a 32-bit PRBS generator.
 */
void PrbsGenerator::gen_prbs_table()
{
    int i;
    for (i = 0;  i < 4;  ++i) {
        int j;
        for (j = 0;  j < 256;  ++j) {
            uint32_t prbs_accum = ((uint32_t)j << (i * 8));
            int k;
            for (k = 0;  k < 8;  ++k) {
                prbs_accum = (prbs_accum << 1)
                                ^ parity_check(prbs_accum & d_polynomial);
            }
            d_prbs_table[i][j] = (prbs_accum & 0xff);
        }
    }
}


/*
 * Generate the weight table.
 */
void PrbsGenerator::gen_weight_table()
{
    int i;
    for (i = 0;  i < 256;  ++i) {
        unsigned char mask=1U, ones_count = 0U;
        int j;
        for (j = 0;  j < 8;  ++j) {
            ones_count += ((i & mask) != 0U);
            mask = mask << 1;
        }
        d_weight[i] = ones_count;
    }
}


/*
 * Generate a parity check for a 32-bit word.
 */
uint32_t PrbsGenerator::parity_check(uint32_t prbs_accum)
{
    uint32_t mask=1UL, parity=0UL;
    int i;
    for (i = 0;  i < 32;  ++i) {
        parity ^= ((prbs_accum & mask) != 0UL);
        mask <<= 1;
    }
    return parity;
}


/*
 * Update a 32-bit PRBS generator eight bits at a time.
 */
uint32_t PrbsGenerator::update_prbs()
{
    unsigned char acc_lsb = 0;
    int i;
    for (i = 0; i < 4; ++i) {
//        PDEBUG("0x%x = 0x%x ^ 0x%x\n",
//            acc_lsb ^ d_prbs_table [i][(d_accum >> (i * 8)) & 0xff],
//            acc_lsb, d_prbs_table [i][(d_accum >> (i * 8)) & 0xff]);
        acc_lsb ^= d_prbs_table[i][(d_accum >> (i * 8)) & 0xff];
    }
    return (d_accum << 8) ^ ((uint32_t)acc_lsb);
}


int PrbsGenerator::process(Buffer* const dataIn, Buffer* dataOut)
{
    PDEBUG("PrbsGenerator::process(dataIn: %p, dataOut: %p)\n",
            dataIn, dataOut);
    dataOut->setLength(d_framesize);
    unsigned char* out = reinterpret_cast<unsigned char*>(dataOut->getData());
    
    // Initialization
    if (d_accum_init) {
        d_accum = d_accum_init;
    } else {
        d_accum = 0;
        while (d_accum < d_polynomial) {
            d_accum <<= 1;
            d_accum |= 1;
        }
    }
    //PDEBUG("Polynomial: 0x%x\n", d_polynomial);
    //PDEBUG("Init accum: 0x%x\n", d_accum);
    size_t i = 0;
    while (i < d_init) {
        out[i++] = 0xff;
    }

    for (; i < d_framesize; ++i) {
        // Writting data
        d_accum = update_prbs();
        if ((d_accum_init == 0xa9) && (i % 188 == 0)) { // DVB energy dispersal
            out[i] = 0;
        } else {
            out[i] = (unsigned char)(d_accum & 0xff);
        }
        //PDEBUG("accum: 0x%x\n", d_accum);
    }

    if ((dataIn != NULL) && dataIn->getLength()) {
        PDEBUG(" mixing input\n");
        const unsigned char* in = reinterpret_cast<const unsigned char*>(dataIn->getData());
        if (dataIn->getLength() != dataOut->getLength()) {
            PDEBUG("%zu != %zu\n", dataIn->getLength(), dataOut->getLength());
            throw std::runtime_error("PrbsGenerator::process "
                    "input size is not equal to output size!\n");
        }
        for (size_t i = 0; i < dataOut->getLength(); ++i) {
            out[i] ^= in[i];
        }
    }
    
    return dataOut->getLength();
}