blob: 92353271825f486e91ccf71431ff965cc7e41a5a [file] [log] [blame]
Alexander Chemeris082bbbf2017-04-02 12:45:36 +02001/*
2 * Copyright (C) 2017 Alexander Chemeris <Alexander.Chemeris@fairwaves.co>
3 *
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004 * SPDX-License-Identifier: LGPL-2.1+
5 *
Alexander Chemeris082bbbf2017-04-02 12:45:36 +02006 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef PRBS_H
22#define PRBS_H
23
24#include <stdint.h>
25#include <assert.h>
26
27/** Pseudo-random binary sequence (PRBS) generator (a Galois LFSR implementation). */
28class PRBS {
29public:
30
31 PRBS(unsigned wLen, uint64_t wCoeff, uint64_t wState = 0x01)
32 : mCoeff(wCoeff), mStartState(wState), mState(wState), mLen(wLen)
33 { assert(wLen<=64); }
34
35 /**@name Accessors */
36 //@{
37 uint64_t coeff() const { return mCoeff; }
38 uint64_t state() const { return mState; }
39 void state(uint64_t state) { mState = state & mask(); }
40 unsigned size() const { return mLen; }
41 //@}
42
43 /**
44 Calculate one bit of a PRBS
45 */
46 unsigned generateBit()
47 {
48 const unsigned result = mState & 0x01;
49 processBit(result);
50 return result;
51 }
52
53 /**
54 Update the generator state by one bit.
55 If you want to synchronize your PRBS to a known state, call this function
56 size() times passing your PRBS to it bit by bit.
57 */
58 void processBit(unsigned inBit)
59 {
60 mState >>= 1;
61 if (inBit) mState ^= mCoeff;
62 }
63
64 /** Return true when PRBS is wrapping through initial state */
65 bool isFinished() const { return mStartState == mState; }
66
67protected:
68
69 uint64_t mCoeff; ///< polynomial coefficients. LSB is zero exponent.
70 uint64_t mStartState; ///< initial shift register state.
71 uint64_t mState; ///< shift register state.
72 unsigned mLen; ///< number of bits used in shift register
73
74 /** Return mask for the state register */
75 uint64_t mask() const { return (mLen==64)?0xFFFFFFFFFFFFFFFFUL:((1<<mLen)-1); }
76
77};
78
79/**
80 A standard 9-bit based pseudorandom binary sequence (PRBS) generator.
81 Polynomial: x^9 + x^5 + 1
82*/
83class PRBS9 : public PRBS {
84 public:
85 PRBS9(uint64_t wState = 0x01)
86 : PRBS(9, 0x0110, wState)
87 {}
88};
89
90/**
91 A standard 15-bit based pseudorandom binary sequence (PRBS) generator.
92 Polynomial: x^15 + x^14 + 1
93*/
94class PRBS15 : public PRBS {
95public:
96 PRBS15(uint64_t wState = 0x01)
97 : PRBS(15, 0x6000, wState)
98 {}
99};
100
101/**
102 A standard 64-bit based pseudorandom binary sequence (PRBS) generator.
103 Polynomial: x^64 + x^63 + x^61 + x^60 + 1
104*/
105class PRBS64 : public PRBS {
106public:
107 PRBS64(uint64_t wState = 0x01)
108 : PRBS(64, 0xD800000000000000ULL, wState)
109 {}
110};
111
112#endif // PRBS_H