2
Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28
//---------------------------------------------------------------------------
31
// Pseudo random binary sequence generator using polynomial
32
// division in GF (2).
34
// There are two ways to built such a generator. Both use some
35
// form of shift register.
37
// 1. The first type feeds back the parity (XOR) of the taps corresponding to
38
// the non-zero elements of the polynomial into the input of the register.
39
// This is the most efficient way to do it in hardware.
41
// 2. In the seond form, when the bit shifted out is 1, the contents of the
42
// register are XORed with a bit pattern representing the polynomial.
43
// This is the best way to do it in software.
45
// Mutatis mutandis the two forms are equivalent. Any sequence that can be
46
// generated by one of the realisations can also be produced by the other.
47
// This software obviously uses the second form. It can use any polynomial
48
// up to (and including) a degree of 32.
53
// Defines the polynomial to be used. The value of p is found from the
54
// sequence of coefficients (0 or 1) of the polynomial starting with the
55
// constant term, and dropping the highest one
58
// Example: P = x^7 + x^6 + 1 --> 1 0 0 0 0 0 1 1 --> 1000001 --> 0x41
60
// To emulate the first form described above, start with the highest
61
// exponent and drop the constant term.
64
// Example: P = x^7 + x^6 + 1 --> 1 1 0 0 0 0 0 1 --> 1100000 --> 0x60
66
// Also sets the state to all ones.
71
// Sets the initial state to x.
76
// Returns the next pseudo random bit.
81
// This sets the generator in a state as if the last N (= degree) bits
82
// were those defined by x (the LSB of x represents the oldest bit).
83
// This can be used to synchronise a BER counter to a received bit stream,
84
// or to set the initial state when emulating a generator of the first form
85
// when the output is taken from the feedback.
90
// This sets the generator in a state so that the first N (= degree) output
91
// bits will be those defined by x (the LSB of x will be the first output bit).
92
// This can be used to set the initial state when emulating a generator of
93
// the first form when the output is taken from the shifted out bit.
96
//---------------------------------------------------------------------------==
105
// Some polynomials for maximum length seqeunces.
119
void set_poly (uint32_t poly);
120
void set_stat (uint32_t stat);
121
void sync_forw (uint32_t bits);
122
void sync_back (uint32_t bits);
127
uint32_t stat (void) const;
128
uint32_t poly (void) const;
129
uint32_t mask (void) const;
130
uint32_t hbit (void) const;
131
int degr (void) const;
146
inline Prbsgen::Prbsgen (void)
147
: _stat (0), _poly (0), _mask (0), _degr (0)
152
inline Prbsgen::~Prbsgen (void)
157
inline void Prbsgen::set_poly (uint32_t poly)
165
while (_mask < _poly)
167
_mask = (_mask << 1) | 1;
171
_hbit = (_mask >> 1) + 1;
175
inline void Prbsgen::set_stat (uint32_t stat)
179
_stat = stat & _mask;
185
inline int Prbsgen::step (void)
193
if (bit) _stat ^= _poly;
199
inline void Prbsgen::sync_forw (uint32_t bits)
203
for (int i = 0; i < _degr; i++)
206
if (bits & 1) _stat ^= _poly;
212
inline void Prbsgen::sync_back (uint32_t bits)
217
for (int h = _hbit; h; h >>= 1)
219
if (bits & h) _stat ^= _poly;
227
inline void Prbsgen::crc_in (int b)
233
bit = (_stat & 1) ^ b;
235
if (bit) _stat ^= _poly;
239
inline int Prbsgen::crc_out (void)
251
inline uint32_t Prbsgen::stat (void) const { return _stat; }
253
inline uint32_t Prbsgen::poly (void) const { return _poly; }
255
inline uint32_t Prbsgen::mask (void) const { return _mask; }
257
inline uint32_t Prbsgen::hbit (void) const { return _hbit; }
259
inline int Prbsgen::degr (void) const { return _degr; }