1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2012,2016 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include <gnuradio/digital/glfsr.h>
24 #include <stdexcept>
25 
26 namespace gr {
27 namespace digital {
28 
29 static uint32_t s_polynomial_masks[] = {
30     0x00000000,
31     0x00000001, // x^1 + 1
32     0x00000003, // x^2 + x^1 + 1
33     0x00000005, // x^3 + x^1 + 1
34     0x00000009, // x^4 + x^1 + 1
35     0x00000012, // x^5 + x^2 + 1
36     0x00000021, // x^6 + x^1 + 1
37     0x00000041, // x^7 + x^1 + 1
38     0x0000008E, // x^8 + x^4 + x^3 + x^2 + 1
39     0x00000108, // x^9 + x^4 + 1
40     0x00000204, // x^10 + x^4 + 1
41     0x00000402, // x^11 + x^2 + 1
42     0x00000829, // x^12 + x^6 + x^4 + x^1 + 1
43     0x0000100D, // x^13 + x^4 + x^3 + x^1 + 1
44     0x00002015, // x^14 + x^5 + x^3 + x^1 + 1
45     0x00004001, // x^15 + x^1 + 1
46     0x00008016, // x^16 + x^5 + x^3 + x^2 + 1
47     0x00010004, // x^17 + x^3 + 1
48     0x00020013, // x^18 + x^5 + x^2 + x^1 + 1
49     0x00040013, // x^19 + x^5 + x^2 + x^1 + 1
50     0x00080004, // x^20 + x^3 + 1
51     0x00100002, // x^21 + x^2 + 1
52     0x00200001, // x^22 + x^1 + 1
53     0x00400010, // x^23 + x^5 + 1
54     0x0080000D, // x^24 + x^4 + x^3 + x^1 + 1
55     0x01000004, // x^25 + x^3 + 1
56     0x02000023, // x^26 + x^6 + x^2 + x^1 + 1
57     0x04000013, // x^27 + x^5 + x^2 + x^1 + 1
58     0x08000004, // x^28 + x^3 + 1
59     0x10000002, // x^29 + x^2 + 1
60     0x20000029, // x^30 + x^4 + x^1 + 1
61     0x40000004, // x^31 + x^3 + 1
62     0x80000057  // x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + 1
63 };
64 
~glfsr()65 glfsr::~glfsr() {}
66 
glfsr_mask(unsigned int degree)67 uint32_t glfsr::glfsr_mask(unsigned int degree)
68 {
69     if (degree < 1 || degree > 32)
70         throw std::runtime_error(
71             "glfsr::glfsr_mask(): degree must be between 1 and 32 inclusive");
72     return s_polynomial_masks[degree];
73 }
74 
next_bit()75 uint8_t glfsr::next_bit()
76 {
77     unsigned char bit = d_shift_register & 0x1;
78     d_shift_register >>= 1;
79     if (bit)
80         d_shift_register ^= d_mask;
81     return bit;
82 }
83 
84 } /* namespace digital */
85 } /* namespace gr */
86