Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_check_lfsr_32k_s.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 2, 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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_check_lfsr_32k_s.h>
28 #include <gr_io_signature.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 gr_check_lfsr_32k_s_sptr
33 gr_make_check_lfsr_32k_s ()
34 {
35   return gr_check_lfsr_32k_s_sptr (new gr_check_lfsr_32k_s ());
36 }
37
38 gr_check_lfsr_32k_s::gr_check_lfsr_32k_s ()
39   : gr_sync_block ("gr_check_lfsr_32k",
40                    gr_make_io_signature (1, 1, sizeof (short)),
41                    gr_make_io_signature (0, 0, 0)),
42     d_state(SEARCHING), d_history (0), d_ntotal (0), d_nright (0),
43     d_runlength (0), d_index(0)
44 {
45   gri_lfsr_32k  lfsr;
46   
47   for (int i = 0; i < BUFSIZE; i++)
48     d_buffer[i] = lfsr.next_short ();
49
50   enter_SEARCHING ();
51 }
52
53 int
54 gr_check_lfsr_32k_s::work (int noutput_items,
55                            gr_vector_const_void_star &input_items,
56                            gr_vector_void_star &output_items)
57 {
58   unsigned short *in = (unsigned short *) input_items[0];
59
60   for (int i = 0; i < noutput_items; i++){
61     unsigned short      x = in[i];
62     unsigned short      expected;
63     
64     switch (d_state){
65
66     case MATCH0:
67       if (x == d_buffer[0])
68         enter_MATCH1 ();
69       break;
70       
71     case MATCH1:
72       if (x == d_buffer[1])
73         enter_MATCH2 ();
74       else
75         enter_MATCH0 ();
76       break;
77       
78     case MATCH2:
79       if (x == d_buffer[2])
80         enter_LOCKED ();
81       else
82         enter_MATCH0 ();
83       break;
84
85     case LOCKED:
86       expected = d_buffer[d_index];
87       d_index = d_index + 1;
88       if (d_index >= BUFSIZE)
89         d_index = 0;
90
91       if (x == expected)
92         right ();
93       else {
94         wrong ();
95         log_error (expected, x);
96         if (wrong_three_times ())
97           enter_SEARCHING ();
98       }
99       break;
100
101     default:
102       abort ();
103     }
104
105     d_ntotal++;
106   }
107
108   return noutput_items;
109 }
110
111 void
112 gr_check_lfsr_32k_s::enter_SEARCHING ()
113 {
114   d_state = SEARCHING;
115   wrong ();                     // reset history
116   wrong ();
117   wrong ();
118
119   d_runlength = 0;
120   d_index = 0;                  // reset LFSR to beginning
121
122   if (0)
123     fprintf (stdout, "gr_check_lfsr_32k: enter_SEARCHING at offset %8ld (0x%08lx)\n",
124              d_ntotal, d_ntotal);
125
126   enter_MATCH0 ();
127 }
128
129 void
130 gr_check_lfsr_32k_s::enter_MATCH0 ()
131 {
132   d_state = MATCH0;
133 }
134
135 void
136 gr_check_lfsr_32k_s::enter_MATCH1 ()
137 {
138   d_state = MATCH1;
139 }
140
141 void
142 gr_check_lfsr_32k_s::enter_MATCH2 ()
143 {
144   d_state = MATCH2;
145 }
146
147 void
148 gr_check_lfsr_32k_s::enter_LOCKED ()
149 {
150   d_state = LOCKED;
151   right ();                     // setup history
152   right ();
153   right ();
154
155   d_index = 3;                  // already matched first 3 items
156
157   if (0)
158     fprintf (stdout, "gr_check_lfsr_32k: enter_LOCKED at offset %8ld (0x%08lx)\n",
159              d_ntotal, d_ntotal);
160 }
161
162 void
163 gr_check_lfsr_32k_s::log_error (unsigned short expected, unsigned short actual)
164 {
165   if (0)
166     fprintf (stdout, 
167      "gr_check_lfsr_32k: expected %5d (0x%04x) got %5d (0x%04x) offset %8ld (0x%08lx)\n",
168              expected, expected, actual, actual, d_ntotal, d_ntotal);
169 }