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