338afd55e57f4b24c4f7edbc4275a8ae2e585b18
[debian/gnuradio] / gr-sounder / src / fpga / lib / sounder_rx.v
1 // -*- verilog -*-
2 //
3 //  USRP - Universal Software Radio Peripheral
4 //
5 //  Copyright (C) 2007 Corgan Enterprises LLC
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 module sounder_rx(clk_i,rst_i,ena_i,rx_strobe_i,tx_strobe_i,mask_i,degree_i,len_i,
23                   rx_in_i_i,rx_in_q_i,rx_i_o,rx_q_o,rx_strobe_o,
24                   loop_i);
25    
26    input         clk_i;         // Master clock
27    input         rst_i;         // Subsystem reset
28    input         ena_i;         // Subsystem enable
29    input         rx_strobe_i;   // Strobe every received sample
30    input         tx_strobe_i;   // Strobe every transmitted sample
31     
32    input  [15:0] mask_i;        // PN code LFSR mask
33    input  [4:0]  degree_i;      // PN code LFSR sequency degree
34    input  [15:0] len_i;         // PN code LFSR sequence length
35    input  [15:0] rx_in_i_i;     // I channel on receive
36    input  [15:0] rx_in_q_i;     // Q channel on receive
37
38    output [15:0] rx_i_o;        // I channel of impulse response
39    output [15:0] rx_q_o;        // Q channel of impulse response
40    output        rx_strobe_o;   // Impulse response value ready
41
42    input         loop_i;        // Implement loopback
43    
44    wire strobe_in  = loop_i ? tx_strobe_i : rx_strobe_i;
45    wire [16:0] len = loop_i ? (len_i - 1) : ((len_i << 1) - 2);
46                  
47    strobe #(17) phase_strobe(.clk_i(clk_i),.rst_i(rst_i),.ena_i(ena_i),
48                        .rate_i(len),.strobe_i(strobe_in),.strobe_o(rx_strobe_o),
49                        .count_o());
50
51    wire pn_ref;
52    wire ref_strobe = tx_strobe_i & ~rx_strobe_o; // Retard reference phase once per period
53    lfsr ref_code
54      ( .clk_i(clk_i),.rst_i(rst_i),.ena_i(ena_i),.strobe_i(ref_strobe),.mask_i(mask_i),.pn_o(pn_ref) );
55
56    wire [5:0] offset = (5'd16-degree_i);
57    
58    reg  [31:0] sum_i, sum_q;
59    reg  [31:0] total_i, total_q;
60    wire [31:0] scaled_i = total_i << offset;
61    wire [31:0] scaled_q = total_q << offset;
62    wire [31:0] i_ext, q_ext;
63
64    sign_extend #(16,32) i_extender(rx_in_i_i, i_ext);
65    sign_extend #(16,32) q_extender(rx_in_q_i, q_ext);
66
67    wire [31:0] prod_i = pn_ref ? i_ext : -i_ext;
68    wire [31:0] prod_q = pn_ref ? q_ext : -q_ext;
69    
70    always @(posedge clk_i)
71      if (rst_i | ~ena_i)
72        begin
73           sum_i <= 0;
74           sum_q <= 0;
75           total_i <= 0;
76           total_q <= 0;
77        end
78      else if (rx_strobe_o)
79        begin
80           total_i <= sum_i + prod_i;
81           total_q <= sum_q + prod_q;
82           sum_i <= 0;
83           sum_q <= 0;
84        end
85      else if (strobe_in)
86        begin
87           sum_i = sum_i + prod_i;
88           sum_q = sum_q + prod_q;
89        end
90    
91    assign rx_i_o = scaled_i[31:16];
92    assign rx_q_o = scaled_q[31:16];
93    
94 endmodule // sounder_rx
95