blob: 8d08fa319fad3006017c14190b12c2f193ca16bb [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.
Alexander Chemeris082bbbf2017-04-02 12:45:36 +020015 */
16
17#ifndef PRBS_H
18#define PRBS_H
19
20#include <stdint.h>
21#include <assert.h>
22
23/** Pseudo-random binary sequence (PRBS) generator (a Galois LFSR implementation). */
24class PRBS {
25public:
26
27 PRBS(unsigned wLen, uint64_t wCoeff, uint64_t wState = 0x01)
28 : mCoeff(wCoeff), mStartState(wState), mState(wState), mLen(wLen)
29 { assert(wLen<=64); }
30
31 /**@name Accessors */
32 //@{
33 uint64_t coeff() const { return mCoeff; }
34 uint64_t state() const { return mState; }
35 void state(uint64_t state) { mState = state & mask(); }
36 unsigned size() const { return mLen; }
37 //@}
38
39 /**
40 Calculate one bit of a PRBS
41 */
42 unsigned generateBit()
43 {
44 const unsigned result = mState & 0x01;
45 processBit(result);
46 return result;
47 }
48
49 /**
50 Update the generator state by one bit.
51 If you want to synchronize your PRBS to a known state, call this function
52 size() times passing your PRBS to it bit by bit.
53 */
54 void processBit(unsigned inBit)
55 {
56 mState >>= 1;
57 if (inBit) mState ^= mCoeff;
58 }
59
60 /** Return true when PRBS is wrapping through initial state */
61 bool isFinished() const { return mStartState == mState; }
62
63protected:
64
65 uint64_t mCoeff; ///< polynomial coefficients. LSB is zero exponent.
66 uint64_t mStartState; ///< initial shift register state.
67 uint64_t mState; ///< shift register state.
68 unsigned mLen; ///< number of bits used in shift register
69
70 /** Return mask for the state register */
71 uint64_t mask() const { return (mLen==64)?0xFFFFFFFFFFFFFFFFUL:((1<<mLen)-1); }
72
73};
74
75/**
76 A standard 9-bit based pseudorandom binary sequence (PRBS) generator.
77 Polynomial: x^9 + x^5 + 1
78*/
79class PRBS9 : public PRBS {
80 public:
81 PRBS9(uint64_t wState = 0x01)
82 : PRBS(9, 0x0110, wState)
83 {}
84};
85
86/**
87 A standard 15-bit based pseudorandom binary sequence (PRBS) generator.
88 Polynomial: x^15 + x^14 + 1
89*/
90class PRBS15 : public PRBS {
91public:
92 PRBS15(uint64_t wState = 0x01)
93 : PRBS(15, 0x6000, wState)
94 {}
95};
96
97/**
98 A standard 64-bit based pseudorandom binary sequence (PRBS) generator.
99 Polynomial: x^64 + x^63 + x^61 + x^60 + 1
100*/
101class PRBS64 : public PRBS {
102public:
103 PRBS64(uint64_t wState = 0x01)
104 : PRBS(64, 0xD800000000000000ULL, wState)
105 {}
106};
107
108#endif // PRBS_H