~ubuntu-branches/ubuntu/intrepid/aeolus/intrepid

« back to all changes in this revision

Viewing changes to prbsgen.h

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2007-05-14 22:18:54 UTC
  • Revision ID: james.westby@ubuntu.com-20070514221854-274rj6fqs5tegu7q
Tags: upstream-0.6.6+2
ImportĀ upstreamĀ versionĀ 0.6.6+2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2003 Fons Adriaensen <fons.adriaensen@skynet.be>
 
3
    
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
17
*/
 
18
 
 
19
 
 
20
#ifndef __PRBSGEN_H
 
21
#define __PRBSGEN_H
 
22
 
 
23
 
 
24
#include <assert.h>
 
25
#include "itypes.h"
 
26
 
 
27
 
 
28
//---------------------------------------------------------------------------
 
29
//
 
30
// Class Synopsis: class Prbs_gen
 
31
//
 
32
// Description:
 
33
//   Pseudo random binary sequence generator using polynomial
 
34
//   division in GF (2).
 
35
//
 
36
// There are two ways to built such a generator. Both use some
 
37
// form of shift register.
 
38
//
 
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. 
 
42
//
 
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.
 
46
//
 
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.
 
51
//  
 
52
//
 
53
//  set_poly (p)
 
54
//
 
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
 
58
//
 
59
//                                   0 1 2 3 4 5 6 7
 
60
//    Example: P = x^7 + x^6 + 1 --> 1 0 0 0 0 0 1 1 --> 1000001 --> 0x41
 
61
//
 
62
//    To emulate the first form described above, start with the highest
 
63
//    exponent and drop the constant term.
 
64
//
 
65
//                                   7 6 5 4 3 2 1 0
 
66
//    Example: P = x^7 + x^6 + 1 --> 1 1 0 0 0 0 0 1 --> 1100000 --> 0x60
 
67
//
 
68
//    Also sets the state to all ones.
 
69
//
 
70
//   
 
71
//  set_state (x)
 
72
//   
 
73
//    Sets the initial state to x.
 
74
// 
 
75
//
 
76
//  step ()
 
77
//
 
78
//     Returns the next pseudo random bit.
 
79
//
 
80
//
 
81
//  sync_forw (x)
 
82
// 
 
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.
 
88
//
 
89
//
 
90
//  sync_back (x)
 
91
//
 
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.
 
96
//
 
97
//
 
98
//---------------------------------------------------------------------------==
 
99
 
 
100
 
 
101
class Prbsgen
 
102
{
 
103
public:
 
104
 
 
105
  enum
 
106
  {
 
107
    // Some polynomials for maximum length seqeunces.
 
108
 
 
109
    G7  = 0x00000041,
 
110
    G8  = 0x0000008E,
 
111
    G15 = 0x00004001,
 
112
    G16 = 0x00008016,
 
113
    G23 = 0x00400010,
 
114
    G24 = 0x0080000D,
 
115
    G31 = 0x40000004,
 
116
    G32 = 0x80000057,
 
117
  };
 
118
 
 
119
  Prbsgen (void);
 
120
 
 
121
  void set_poly (U32 poly);
 
122
  void set_stat (U32 stat);
 
123
  void sync_forw (U32 bits);
 
124
  void sync_back (U32 bits);
 
125
  int  step (void);
 
126
  void crc_in (int b);
 
127
  int  crc_out (void);
 
128
 
 
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;
 
134
 
 
135
  ~Prbsgen (void);
 
136
 
 
137
private:
 
138
  
 
139
  U32 _stat;
 
140
  U32 _poly;
 
141
  U32 _mask;
 
142
  U32 _hbit;
 
143
  int _degr;
 
144
 
 
145
};
 
146
 
 
147
 
 
148
inline Prbsgen::Prbsgen (void)
 
149
  : _stat (0), _poly (0), _mask (0), _degr (0)
 
150
{
 
151
}
 
152
 
 
153
 
 
154
inline Prbsgen::~Prbsgen (void)
 
155
{
 
156
}
 
157
 
 
158
 
 
159
inline void Prbsgen::set_poly (U32 poly)
 
160
{
 
161
  assert (poly != 0);
 
162
 
 
163
  _poly = poly;
 
164
  _mask = 0;
 
165
  _degr = 0;
 
166
  
 
167
  while (_mask < _poly)
 
168
  {
 
169
    _mask = (_mask << 1) | 1;
 
170
    _degr += 1;
 
171
  }    
 
172
  _stat = _mask;
 
173
  _hbit = (_mask >> 1) + 1;
 
174
}
 
175
 
 
176
 
 
177
inline void Prbsgen::set_stat (U32 stat)
 
178
{
 
179
  assert (_poly != 0);
 
180
 
 
181
  _stat = stat & _mask;
 
182
 
 
183
  assert (_stat != 0);
 
184
}
 
185
 
 
186
 
 
187
inline int Prbsgen::step (void)
 
188
{
 
189
  int bit;
 
190
 
 
191
  assert (_poly != 0);
 
192
 
 
193
  bit = _stat & 1;
 
194
  _stat >>= 1;
 
195
  if (bit) _stat ^= _poly;
 
196
 
 
197
  return bit;
 
198
}
 
199
 
 
200
 
 
201
inline void Prbsgen::sync_forw (U32 bits)
 
202
{
 
203
  assert (_poly != 0);
 
204
 
 
205
  for (int i = 0; i < _degr; i++)
 
206
  {
 
207
    _stat >>= 1;
 
208
    if (bits & 1) _stat ^= _poly;
 
209
    bits >>= 1;
 
210
  }
 
211
}
 
212
 
 
213
 
 
214
inline void Prbsgen::sync_back (U32 bits)
 
215
{
 
216
  assert (_poly != 0);
 
217
 
 
218
  _stat = 0;
 
219
  for (int h = _hbit; h; h >>= 1)
 
220
  {
 
221
    if (bits & h) _stat ^= _poly;
 
222
    _stat <<= 1;
 
223
  }
 
224
  _stat ^= bits;
 
225
  _stat &= _mask;
 
226
}
 
227
 
 
228
 
 
229
inline void Prbsgen::crc_in (int b)
 
230
{
 
231
  int bit;
 
232
 
 
233
  assert (_poly != 0);
 
234
 
 
235
  bit = (_stat & 1) ^ b;
 
236
  _stat >>= 1;
 
237
  if (bit) _stat ^= _poly;
 
238
}
 
239
 
 
240
 
 
241
inline int Prbsgen::crc_out (void)
 
242
{
 
243
  int bit;
 
244
 
 
245
  assert (_poly != 0);
 
246
 
 
247
  bit = (_stat & 1);
 
248
  _stat >>= 1;
 
249
  return bit;
 
250
}
 
251
 
 
252
 
 
253
inline U32 Prbsgen::stat (void) const { return _stat; }
 
254
 
 
255
inline U32 Prbsgen::poly (void) const { return _poly; }
 
256
 
 
257
inline U32 Prbsgen::mask (void) const { return _mask; }
 
258
 
 
259
inline U32 Prbsgen::hbit (void) const { return _hbit; }
 
260
 
 
261
inline int Prbsgen::degr (void) const { return _degr; }
 
262
 
 
263
 
 
264
#endif