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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __PRBSGEN_H
#define __PRBSGEN_H
#include <assert.h>
#include <stdint.h>
//---------------------------------------------------------------------------
//
// Description:
// Pseudo random binary sequence generator using polynomial
// division in GF (2).
//
// There are two ways to built such a generator. Both use some
// form of shift register.
//
// 1. The first type feeds back the parity (XOR) of the taps corresponding to
// the non-zero elements of the polynomial into the input of the register.
// This is the most efficient way to do it in hardware.
//
// 2. In the seond form, when the bit shifted out is 1, the contents of the
// register are XORed with a bit pattern representing the polynomial.
// This is the best way to do it in software.
//
// Mutatis mutandis the two forms are equivalent. Any sequence that can be
// generated by one of the realisations can also be produced by the other.
// This software obviously uses the second form. It can use any polynomial
// up to (and including) a degree of 32.
//
//
// set_poly (p)
//
// Defines the polynomial to be used. The value of p is found from the
// sequence of coefficients (0 or 1) of the polynomial starting with the
// constant term, and dropping the highest one
//
// 0 1 2 3 4 5 6 7
// Example: P = x^7 + x^6 + 1 --> 1 0 0 0 0 0 1 1 --> 1000001 --> 0x41
//
// To emulate the first form described above, start with the highest
// exponent and drop the constant term.
//
// 7 6 5 4 3 2 1 0
// Example: P = x^7 + x^6 + 1 --> 1 1 0 0 0 0 0 1 --> 1100000 --> 0x60
//
// Also sets the state to all ones.
//
//
// set_state (x)
//
// Sets the initial state to x.
//
//
// step ()
//
// Returns the next pseudo random bit.
//
//
// sync_forw (x)
//
// This sets the generator in a state as if the last N (= degree) bits
// were those defined by x (the LSB of x represents the oldest bit).
// This can be used to synchronise a BER counter to a received bit stream,
// or to set the initial state when emulating a generator of the first form
// when the output is taken from the feedback.
//
//
// sync_back (x)
//
// This sets the generator in a state so that the first N (= degree) output
// bits will be those defined by x (the LSB of x will be the first output bit).
// This can be used to set the initial state when emulating a generator of
// the first form when the output is taken from the shifted out bit.
//
//
//---------------------------------------------------------------------------==
class Prbsgen
{
public:
enum
{
// Some polynomials for maximum length seqeunces.
G7 = 0x00000041,
G8 = 0x0000008E,
G15 = 0x00004001,
G16 = 0x00008016,
G23 = 0x00400010,
G24 = 0x0080000D,
G31 = 0x40000004,
G32 = 0x80000057,
};
Prbsgen (void);
void set_poly (uint32_t poly);
void set_stat (uint32_t stat);
void sync_forw (uint32_t bits);
void sync_back (uint32_t bits);
int step (void);
void crc_in (int b);
int crc_out (void);
uint32_t stat (void) const;
uint32_t poly (void) const;
uint32_t mask (void) const;
uint32_t hbit (void) const;
int degr (void) const;
~Prbsgen (void);
private:
uint32_t _stat;
uint32_t _poly;
uint32_t _mask;
uint32_t _hbit;
int _degr;
};
inline Prbsgen::Prbsgen (void)
: _stat (0), _poly (0), _mask (0), _degr (0)
{
}
inline Prbsgen::~Prbsgen (void)
{
}
inline void Prbsgen::set_poly (uint32_t poly)
{
assert (poly != 0);
_poly = poly;
_mask = 0;
_degr = 0;
while (_mask < _poly)
{
_mask = (_mask << 1) | 1;
_degr += 1;
}
_stat = _mask;
_hbit = (_mask >> 1) + 1;
}
inline void Prbsgen::set_stat (uint32_t stat)
{
assert (_poly != 0);
_stat = stat & _mask;
assert (_stat != 0);
}
inline int Prbsgen::step (void)
{
int bit;
assert (_poly != 0);
bit = _stat & 1;
_stat >>= 1;
if (bit) _stat ^= _poly;
return bit;
}
inline void Prbsgen::sync_forw (uint32_t bits)
{
assert (_poly != 0);
for (int i = 0; i < _degr; i++)
{
_stat >>= 1;
if (bits & 1) _stat ^= _poly;
bits >>= 1;
}
}
inline void Prbsgen::sync_back (uint32_t bits)
{
assert (_poly != 0);
_stat = 0;
for (int h = _hbit; h; h >>= 1)
{
if (bits & h) _stat ^= _poly;
_stat <<= 1;
}
_stat ^= bits;
_stat &= _mask;
}
inline void Prbsgen::crc_in (int b)
{
int bit;
assert (_poly != 0);
bit = (_stat & 1) ^ b;
_stat >>= 1;
if (bit) _stat ^= _poly;
}
inline int Prbsgen::crc_out (void)
{
int bit;
assert (_poly != 0);
bit = (_stat & 1);
_stat >>= 1;
return bit;
}
inline uint32_t Prbsgen::stat (void) const { return _stat; }
inline uint32_t Prbsgen::poly (void) const { return _poly; }
inline uint32_t Prbsgen::mask (void) const { return _mask; }
inline uint32_t Prbsgen::hbit (void) const { return _hbit; }
inline int Prbsgen::degr (void) const { return _degr; }
#endif
|