Imported Upstream version 3.2.2
[debian/gnuradio] / usrp / fpga / sdr_lib / cic_decim.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_decim
24   ( clock,reset,enable,rate,strobe_in,strobe_out,signal_in,signal_out);
25    parameter bw = 16;
26    parameter N = 4;
27    parameter log2_of_max_rate = 7;
28    parameter maxbitgain = N * log2_of_max_rate;
29    
30    input clock;
31    input reset;
32    input enable;
33    input [7:0] rate;
34    input strobe_in,strobe_out;  
35    input [bw-1:0] signal_in;
36    output [bw-1:0] signal_out;
37    reg [bw-1:0] signal_out;
38    wire [bw-1:0] signal_out_unreg;
39    
40    wire [bw+maxbitgain-1:0] signal_in_ext;
41    reg [bw+maxbitgain-1:0]  integrator [0:N-1];
42    reg [bw+maxbitgain-1:0] differentiator [0:N-1];
43    reg [bw+maxbitgain-1:0] pipeline [0:N-1];
44    reg [bw+maxbitgain-1:0] sampler;
45    
46    integer i;
47    
48    sign_extend #(bw,bw+maxbitgain) 
49       ext_input (.in(signal_in),.out(signal_in_ext));
50    
51    always @(posedge clock)
52      if(reset)
53        for(i=0;i<N;i=i+1)
54          integrator[i] <= #1 0;
55      else if (enable && strobe_in)
56        begin
57           integrator[0] <= #1 integrator[0] + signal_in_ext;
58           for(i=1;i<N;i=i+1)
59             integrator[i] <= #1 integrator[i] + integrator[i-1];
60        end      
61    
62    always @(posedge clock)
63      if(reset)
64        begin
65           sampler <= #1 0;
66           for(i=0;i<N;i=i+1)
67             begin
68                pipeline[i] <= #1 0;
69                differentiator[i] <= #1 0;
70             end
71        end
72      else if (enable && strobe_out)
73        begin
74           sampler <= #1 integrator[N-1];
75           differentiator[0] <= #1 sampler;
76           pipeline[0] <= #1 sampler - differentiator[0];
77           for(i=1;i<N;i=i+1)
78             begin
79                differentiator[i] <= #1 pipeline[i-1];
80                pipeline[i] <= #1 pipeline[i-1] - differentiator[i];
81             end
82        end // if (enable && strobe_out)
83       
84    wire [bw+maxbitgain-1:0] signal_out_unnorm = pipeline[N-1];
85
86    cic_dec_shifter #(bw)
87         cic_dec_shifter(rate,signal_out_unnorm,signal_out_unreg);
88
89    always @(posedge clock)
90      signal_out <= #1 signal_out_unreg;
91    
92 endmodule // cic_decim
93