Added firmware support for adc_mux to handle swapping I/Q, etc.
[debian/gnuradio] / usrp2 / fpga / sdr_lib / dsp_core_rx.v
1
2 `define DSP_CORE_RX_BASE 160
3 module dsp_core_rx
4   (input clk, input rst,
5    input set_stb, input [7:0] set_addr, input [31:0] set_data,
6
7    input [13:0] adc_a, input adc_ovf_a,
8    input [13:0] adc_b, input adc_ovf_b,
9    
10    output [31:0] sample,
11    input run,
12    output strobe,
13    output [31:0] debug
14    );
15
16    wire [15:0] scale_i, scale_q;
17    wire [13:0] adc_a_ofs, adc_b_ofs;
18    reg [13:0] adc_i, adc_q;
19    wire [31:0] phase_inc;
20    reg [31:0]  phase;
21
22    wire [35:0] prod_i, prod_q;
23    wire [23:0] i_cordic, q_cordic;
24    wire [23:0] i_cic, q_cic;
25    wire [17:0] i_cic_scaled, q_cic_scaled;
26    wire [17:0] i_hb1, q_hb1;
27    wire [17:0] i_hb2, q_hb2;
28    wire [15:0] i_out, q_out;
29
30    wire        strobe_cic, strobe_hb1, strobe_hb2;
31    wire        enable_hb1, enable_hb2;
32    wire [7:0]  cic_decim_rate;
33    
34    setting_reg #(.my_addr(`DSP_CORE_RX_BASE+0)) sr_0
35      (.clk(clk),.rst(rst),.strobe(set_stb),.addr(set_addr),
36       .in(set_data),.out(phase_inc),.changed());
37    
38    setting_reg #(.my_addr(`DSP_CORE_RX_BASE+1)) sr_1
39      (.clk(clk),.rst(rst),.strobe(set_stb),.addr(set_addr),
40       .in(set_data),.out({scale_i,scale_q}),.changed());
41    
42    setting_reg #(.my_addr(`DSP_CORE_RX_BASE+2)) sr_2
43      (.clk(clk),.rst(rst),.strobe(set_stb),.addr(set_addr),
44       .in(set_data),.out({enable_hb1, enable_hb2, cic_decim_rate}),.changed());
45
46    rx_dcoffset #(.WIDTH(14),.ADDR(`DSP_CORE_RX_BASE+6)) rx_dcoffset_a
47      (.clk(clk),.rst(rst),.set_stb(set_stb),.set_addr(set_addr),.set_data(set_data),
48       .adc_in(adc_a),.adc_out(adc_a_ofs));
49    
50    rx_dcoffset #(.WIDTH(14),.ADDR(`DSP_CORE_RX_BASE+7)) rx_dcoffset_b
51      (.clk(clk),.rst(rst),.set_stb(set_stb),.set_addr(set_addr),.set_data(set_data),
52       .adc_in(adc_b),.adc_out(adc_b_ofs));
53
54    wire [3:0]  muxctrl;
55    setting_reg #(.my_addr(`DSP_CORE_RX_BASE+8)) sr_8
56      (.clk(clk),.rst(rst),.strobe(set_stb),.addr(set_addr),
57       .in(set_data),.out(muxctrl),.changed());
58
59    // The TVRX connects to what is called adc_b, thus A and B are
60    // swapped throughout the design.
61    //
62    // In the interest of expediency and keeping the s/w sane, we just remap them here.
63    // The I & Q fields are mapped the same:
64    // 0 -> "the real A" (as determined by the TVRX)
65    // 1 -> "the real B"
66    // 2 -> const zero
67    
68    always @(posedge clk)
69      case(muxctrl[1:0])         // The I mapping
70        0: adc_i <= adc_b_ofs;   // "the real A"
71        1: adc_i <= adc_a_ofs;
72        2: adc_i <= 0;
73        default: adc_i <= 0;
74      endcase // case(muxctrl[1:0])
75           
76    always @(posedge clk)
77      case(muxctrl[3:2])         // The Q mapping
78        0: adc_q <= adc_b_ofs;   // "the real A"
79        1: adc_q <= adc_a_ofs;
80        2: adc_q <= 0;
81        default: adc_q <= 0;
82      endcase // case(muxctrl[3:2])
83        
84    always @(posedge clk)
85      if(rst)
86        phase <= 0;
87      else if(run)
88        phase <= phase + phase_inc;
89
90    MULT18X18S mult_i
91      (.P(prod_i),    // 36-bit multiplier output
92       .A({{4{adc_i[13]}},adc_i} ),    // 18-bit multiplier input
93       .B({{2{scale_i[15]}},scale_i}),    // 18-bit multiplier input
94       .C(clk),    // Clock input
95       .CE(1),  // Clock enable input
96       .R(rst)     // Synchronous reset input
97       );
98
99    MULT18X18S mult_q
100      (.P(prod_q),    // 36-bit multiplier output
101       .A({{4{adc_q[13]}},adc_q} ),    // 18-bit multiplier input
102       .B({{2{scale_q[15]}},scale_q}),    // 18-bit multiplier input
103       .C(clk),    // Clock input
104       .CE(1),  // Clock enable input
105       .R(rst)     // Synchronous reset input
106       ); 
107
108    cordic #(.bitwidth(24))
109      cordic(.clock(clk), .reset(rst), .enable(run),
110             .xi(prod_i[23:0]),. yi(prod_q[23:0]), .zi(phase[31:16]),
111             .xo(i_cordic),.yo(q_cordic),.zo() );
112
113    cic_strober cic_strober(.clock(clk),.reset(rst),.enable(run),.rate(cic_decim_rate),
114                            .strobe_fast(1),.strobe_slow(strobe_cic) );
115
116    cic_decim #(.bw(24))
117      decim_i (.clock(clk),.reset(rst),.enable(run),
118               .rate(cic_decim_rate),.strobe_in(1'b1),.strobe_out(strobe_cic),
119               .signal_in(i_cordic),.signal_out(i_cic));
120    
121    cic_decim #(.bw(24))
122      decim_q (.clock(clk),.reset(rst),.enable(run),
123               .rate(cic_decim_rate),.strobe_in(1'b1),.strobe_out(strobe_cic),
124               .signal_in(q_cordic),.signal_out(q_cic));
125
126    round_reg #(.bits_in(24),.bits_out(18)) round_icic (.clk(clk),.in(i_cic),.out(i_cic_scaled));
127    round_reg #(.bits_in(24),.bits_out(18)) round_qcic (.clk(clk),.in(q_cic),.out(q_cic_scaled));
128    reg         strobe_cic_d1;
129    always @(posedge clk) strobe_cic_d1 <= strobe_cic;
130    
131    small_hb_dec #(.WIDTH(18)) small_hb_i
132      (.clk(clk),.rst(rst),.bypass(~enable_hb1),
133       .stb_in(strobe_cic_d1),.data_in(i_cic_scaled),.stb_out(strobe_hb1),.data_out(i_hb1));
134    
135    small_hb_dec #(.WIDTH(18)) small_hb_q
136      (.clk(clk),.rst(rst),.bypass(~enable_hb1),
137       .stb_in(strobe_cic_d1),.data_in(q_cic_scaled),.stb_out(),.data_out(q_hb1));
138
139    wire [8:0]  cpi_hb = enable_hb1 ? {cic_decim_rate,1'b0} : {1'b0,cic_decim_rate};
140    hb_dec #(.IWIDTH(18), .OWIDTH(18), .CWIDTH(18), .ACCWIDTH(24)) hb_i
141      (.clk(clk),.rst(rst),.bypass(~enable_hb2),.cpi(cpi_hb),
142       .stb_in(strobe_hb1),.data_in(i_hb1),.stb_out(strobe_hb2),.data_out(i_hb2));
143
144    hb_dec #(.IWIDTH(18), .OWIDTH(18), .CWIDTH(18), .ACCWIDTH(24)) hb_q
145      (.clk(clk),.rst(rst),.bypass(~enable_hb2),.cpi(cpi_hb),
146       .stb_in(strobe_hb1),.data_in(q_hb1),.stb_out(),.data_out(q_hb2));
147
148    round #(.bits_in(18),.bits_out(16)) round_iout (.in(i_hb2),.out(i_out));
149    round #(.bits_in(18),.bits_out(16)) round_qout (.in(q_hb2),.out(q_out));
150
151    assign      sample = {i_out,q_out};
152    assign      strobe = strobe_hb2;
153    assign      debug = {enable_hb1, enable_hb2, run, strobe, strobe_cic, strobe_cic_d1, strobe_hb1, strobe_hb2};
154    
155 endmodule // dsp_core_rx