2
Copyright (C) 2003 Fons Adriaensen <fons.adriaensen@skynet.be>
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
//---------------------------------------------------------------------------
30
// Class Synopsis: class Prbs_gen
33
// Pseudo random binary sequence generator using polynomial
34
// division in GF (2).
36
// There are two ways to built such a generator. Both use some
37
// form of shift register.
39
// 1. The first type feeds back the parity (XOR) of the taps corresponding to
40
// the non-zero elements of the polynomial into the input of the register.
41
// This is the most efficient way to do it in hardware.
43
// 2. In the seond form, when the bit shifted out is 1, the contents of the
44
// register are XORed with a bit pattern representing the polynomial.
45
// This is the best way to do it in software.
47
// Mutatis mutandis the two forms are equivalent. Any sequence that can be
48
// generated by one of the realisations can also be produced by the other.
49
// This software obviously uses the second form. It can use any polynomial
50
// up to (and including) a degree of 32.
55
// Defines the polynomial to be used. The value of p is found from the
56
// sequence of coefficients (0 or 1) of the polynomial starting with the
57
// constant term, and dropping the highest one
60
// Example: P = x^7 + x^6 + 1 --> 1 0 0 0 0 0 1 1 --> 1000001 --> 0x41
62
// To emulate the first form described above, start with the highest
63
// exponent and drop the constant term.
66
// Example: P = x^7 + x^6 + 1 --> 1 1 0 0 0 0 0 1 --> 1100000 --> 0x60
68
// Also sets the state to all ones.
73
// Sets the initial state to x.
78
// Returns the next pseudo random bit.
83
// This sets the generator in a state as if the last N (= degree) bits
84
// were those defined by x (the LSB of x represents the oldest bit).
85
// This can be used to synchronise a BER counter to a received bit stream,
86
// or to set the initial state when emulating a generator of the first form
87
// when the output is taken from the feedback.
92
// This sets the generator in a state so that the first N (= degree) output
93
// bits will be those defined by x (the LSB of x will be the first output bit).
94
// This can be used to set the initial state when emulating a generator of
95
// the first form when the output is taken from the shifted out bit.
98
//---------------------------------------------------------------------------==
107
// Some polynomials for maximum length seqeunces.
121
void set_poly (U32 poly);
122
void set_stat (U32 stat);
123
void sync_forw (U32 bits);
124
void sync_back (U32 bits);
129
U32 stat (void) const;
130
U32 poly (void) const;
131
U32 mask (void) const;
132
U32 hbit (void) const;
133
int degr (void) const;
148
inline Prbsgen::Prbsgen (void)
149
: _stat (0), _poly (0), _mask (0), _degr (0)
154
inline Prbsgen::~Prbsgen (void)
159
inline void Prbsgen::set_poly (U32 poly)
167
while (_mask < _poly)
169
_mask = (_mask << 1) | 1;
173
_hbit = (_mask >> 1) + 1;
177
inline void Prbsgen::set_stat (U32 stat)
181
_stat = stat & _mask;
187
inline int Prbsgen::step (void)
195
if (bit) _stat ^= _poly;
201
inline void Prbsgen::sync_forw (U32 bits)
205
for (int i = 0; i < _degr; i++)
208
if (bits & 1) _stat ^= _poly;
214
inline void Prbsgen::sync_back (U32 bits)
219
for (int h = _hbit; h; h >>= 1)
221
if (bits & h) _stat ^= _poly;
229
inline void Prbsgen::crc_in (int b)
235
bit = (_stat & 1) ^ b;
237
if (bit) _stat ^= _poly;
241
inline int Prbsgen::crc_out (void)
253
inline U32 Prbsgen::stat (void) const { return _stat; }
255
inline U32 Prbsgen::poly (void) const { return _poly; }
257
inline U32 Prbsgen::mask (void) const { return _mask; }
259
inline U32 Prbsgen::hbit (void) const { return _hbit; }
261
inline int Prbsgen::degr (void) const { return _degr; }