Merged r9433:9527 from features/gr-usrp2 into trunk. Adds usrp2 and gr-usrp2 top...
[debian/gnuradio] / usrp2 / fpga / sdr_lib / cic_interp.v
1 // -*- verilog -*-
2 //
3 //  USRP - Universal Software Radio Peripheral
4 //
5 //  Copyright (C) 2003 Matt Ettus
6 //
7 //  This program 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 of the License, or
10 //  (at your option) any later version.
11 //
12 //  This program 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 this program; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin Street, Boston, MA  02110-1301  USA
20 //
21
22
23 module cic_interp
24   #(parameter bw = 16, parameter N = 4, parameter log2_of_max_rate = 7)
25     (input clock,
26      input reset,
27      input enable,
28      input [7:0] rate,
29      input strobe_in,
30      input strobe_out,
31      input [bw-1:0] signal_in,
32      output reg [bw-1:0] signal_out);
33    
34    integer           i;
35    localparam        maxbitgain = (N-1)*log2_of_max_rate;
36
37    wire [bw+maxbitgain-1:0] signal_in_ext;
38    reg [bw+maxbitgain-1:0]  integrator [0:N-1];
39    reg [bw+maxbitgain-1:0]  differentiator [0:N-1];
40    reg [bw+maxbitgain-1:0]  pipeline [0:N-1];
41    
42    sign_extend #(bw,bw+maxbitgain) 
43      ext_input (.in(signal_in),.out(signal_in_ext));
44    
45    //FIXME Note that this section has pipe and diff reversed
46    // It still works, but is confusing
47    always @(posedge clock)
48      if(reset | ~enable)
49        for(i=0;i<N;i=i+1)
50          integrator[i] <= 0;
51      else if (enable & strobe_out)
52        begin
53           if(strobe_in)
54             integrator[0] <= integrator[0] + pipeline[N-1];
55           for(i=1;i<N;i=i+1)
56             integrator[i] <= integrator[i] + integrator[i-1];
57        end
58    
59    always @(posedge clock)
60      if(reset | ~enable)
61        begin
62           for(i=0;i<N;i=i+1)
63             begin
64                differentiator[i] <= 0;
65                pipeline[i] <= 0;
66             end
67        end
68      else if (enable && strobe_in)
69        begin
70           differentiator[0] <= signal_in_ext;
71           pipeline[0] <= signal_in_ext - differentiator[0];
72           for(i=1;i<N;i=i+1)
73             begin
74                differentiator[i] <= pipeline[i-1];
75                pipeline[i] <= pipeline[i-1] - differentiator[i];
76             end
77        end
78
79    wire [bw-1:0] signal_out_unreg;
80    cic_int_shifter #(bw)
81      cic_int_shifter(rate,integrator[N-1],signal_out_unreg);
82    
83    always @(posedge clock)
84      signal_out <= signal_out_unreg;
85    
86 endmodule // cic_interp
87