summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/check_lfsr_32k_s_impl.cc
blob: fac30223f7ea681a68726a85022ed93d273d4c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- c++ -*- */
/*
 * Copyright 2004,2010,2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "check_lfsr_32k_s_impl.h"
#include <gnuradio/io_signature.h>
#include <stdlib.h>
#include <stdio.h>

namespace gr {
  namespace blocks {

    check_lfsr_32k_s::sptr
    check_lfsr_32k_s::make()
    {
      return gnuradio::get_initial_sptr
        (new check_lfsr_32k_s_impl());
    }

    check_lfsr_32k_s_impl::check_lfsr_32k_s_impl()
      : sync_block("check_lfsr_32k",
                       io_signature::make(1, 1, sizeof(short)),
                      io_signature::make(0, 0, 0)),
        d_state(SEARCHING), d_history(0), d_ntotal(0), d_nright(0),
        d_runlength(0), d_index(0)
    {
      lfsr_32k lfsr;

      for(int i = 0; i < BUFSIZE; i++)
        d_buffer[i] = lfsr.next_short();

      enter_SEARCHING();
    }

    check_lfsr_32k_s_impl::~check_lfsr_32k_s_impl()
    {
    }

    int
    check_lfsr_32k_s_impl::work(int noutput_items,
                                gr_vector_const_void_star &input_items,
                                gr_vector_void_star &output_items)
    {
      unsigned short *in = (unsigned short*)input_items[0];

      for(int i = 0; i < noutput_items; i++) {
        unsigned short x = in[i];
        unsigned short expected;

        switch(d_state) {
        case MATCH0:
          if(x == d_buffer[0])
            enter_MATCH1();
          break;

        case MATCH1:
          if(x == d_buffer[1])
            enter_MATCH2();
          else
            enter_MATCH0();
          break;

        case MATCH2:
          if(x == d_buffer[2])
            enter_LOCKED();
          else
            enter_MATCH0();
          break;

        case LOCKED:
          expected = d_buffer[d_index];
          d_index = d_index + 1;
          if(d_index >= BUFSIZE)
            d_index = 0;

          if(x == expected)
            right();
          else {
            wrong();
            log_error(expected, x);
            if(wrong_three_times())
              enter_SEARCHING();
          }
          break;

        default:
          abort();
        }

        d_ntotal++;
      }

      return noutput_items;
    }

    void
    check_lfsr_32k_s_impl::enter_SEARCHING()
    {
      d_state = SEARCHING;
      wrong();  // reset history
      wrong();
      wrong();

      d_runlength = 0;
      d_index = 0;      // reset LFSR to beginning

      if(0)
        fprintf(stdout, "check_lfsr_32k: enter_SEARCHING at offset %8ld (0x%08lx)\n",
                d_ntotal, d_ntotal);

      enter_MATCH0();
    }

    void
    check_lfsr_32k_s_impl::enter_MATCH0()
    {
      d_state = MATCH0;
    }

    void
    check_lfsr_32k_s_impl::enter_MATCH1()
    {
      d_state = MATCH1;
    }

    void
    check_lfsr_32k_s_impl::enter_MATCH2()
    {
      d_state = MATCH2;
    }

    void
    check_lfsr_32k_s_impl::enter_LOCKED()
    {
      d_state = LOCKED;
      right();          // setup history
      right();
      right();

      d_index = 3;      // already matched first 3 items

      if(0)
        fprintf(stdout, "check_lfsr_32k: enter_LOCKED at offset %8ld (0x%08lx)\n",
                d_ntotal, d_ntotal);
    }

    void
    check_lfsr_32k_s_impl::log_error(unsigned short expected, unsigned short actual)
    {
      if(0)
        fprintf(stdout,
                "check_lfsr_32k: expected %5d (0x%04x) got %5d (0x%04x) offset %8ld (0x%08lx)\n",
                expected, expected, actual, actual, d_ntotal, d_ntotal);
    }

  } /* namespace blocks */
} /* namespace gr */