Imported Upstream version 3.2.2
[debian/gnuradio] / gr-gpio / src / fpga / lib / integ_shifter.v
1 // -*- verilog -*-
2 //
3 //  USRP - Universal Software Radio Peripheral
4 //
5 //  Copyright (C) 2003 Matt Ettus
6 //  Copyright (C) 2008 Corgan Enterprises LLC
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation; either version 2 of the License, or
11 //  (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 //
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Boston, MA  02110-1301  USA
21 //
22
23
24 // NOTE: This only works for a max decim rate of 256
25 // NOTE: Signal "rate" is ONE LESS THAN the actual rate
26
27 module integ_shifter(rate,signal_in,signal_out);
28     parameter bw = 16;
29     parameter maxbitgain = 8;
30    
31     input [7:0] rate;
32     input       wire [bw+maxbitgain-1:0] signal_in;
33     output      reg [bw-1:0] signal_out;
34
35     reg [3:0] bitgain;
36
37     // Nearest without overflow -- ceil(log2(rate+1))
38     always @*
39       if (rate >= 8'd128)
40         bitgain = 8;
41       else if (rate >= 8'd64)
42         bitgain = 7;
43       else if (rate >= 8'd32)
44         bitgain = 6;
45       else if (rate >= 8'd16)
46         bitgain = 5;
47       else if (rate >= 8'd8)
48         bitgain = 4;
49       else if (rate >= 8'd4)
50         bitgain = 3;
51       else if (rate >= 8'd2)
52         bitgain = 2;
53       else
54         bitgain = 1;
55
56    always @*
57      case(bitgain)
58        5'd1 : signal_out = signal_in[1+bw-1:1];
59        5'd2 : signal_out = signal_in[2+bw-1:2];
60        5'd3 : signal_out = signal_in[3+bw-1:3];
61        5'd4 : signal_out = signal_in[4+bw-1:4];
62        5'd5 : signal_out = signal_in[5+bw-1:5];
63        5'd6 : signal_out = signal_in[6+bw-1:6];
64        5'd7 : signal_out = signal_in[7+bw-1:7];
65        default : signal_out = signal_in[8+bw-1:8];
66      endcase // case(shift)
67
68 endmodule // integ_shifter