Merged features/inband -r4812:5218 into trunk. This group of changes
[debian/gnuradio] / usrp / fpga / toplevel / usrp_inband_usb / usrp_inband_usb.v
1 // -*- verilog -*-
2 //
3 //  USRP - Universal Software Radio Peripheral
4 //
5 //  Copyright (C) 2003,2004 Matt Ettus
6 //  Copyright 2007 Free Software Foundation, Inc.
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 `define IN_BAND
23
24 `include "config.vh"
25 `include "../../../firmware/include/fpga_regs_common.v"
26 `include "../../../firmware/include/fpga_regs_standard.v"
27
28 module usrp_inband_usb
29 (output MYSTERY_SIGNAL,
30  input master_clk,
31  input SCLK,
32  input SDI,
33  inout SDO,
34  input SEN_FPGA,
35
36  input FX2_1,
37  output FX2_2,
38  output FX2_3,
39  
40  input wire [11:0] rx_a_a,
41  input wire [11:0] rx_b_a,
42  input wire [11:0] rx_a_b,
43  input wire [11:0] rx_b_b,
44
45  output wire [13:0] tx_a,
46  output wire [13:0] tx_b,
47
48  output wire TXSYNC_A,
49  output wire TXSYNC_B,
50  
51   // USB interface
52  input usbclk,
53  input wire [2:0] usbctl,
54  output wire [1:0] usbrdy,
55  inout [15:0] usbdata,  // NB Careful, inout
56
57  // These are the general purpose i/o's that go to the daughterboard slots
58  inout wire [15:0] io_tx_a,
59  inout wire [15:0] io_tx_b,
60  inout wire [15:0] io_rx_a,
61  inout wire [15:0] io_rx_b
62  );     
63    wire [15:0] debugdata,debugctrl;
64    assign MYSTERY_SIGNAL = 1'b0;
65    
66    wire clk64,clk128;
67    
68    wire WR = usbctl[0];
69    wire RD = usbctl[1];
70    wire OE = usbctl[2];
71
72    wire have_space, have_pkt_rdy;
73    assign usbrdy[0] = have_space;
74    assign usbrdy[1] = have_pkt_rdy;
75
76    wire   tx_underrun, rx_overrun;    
77    wire   clear_status = FX2_1;
78    assign FX2_2 = rx_overrun;
79    assign FX2_3 = tx_underrun;
80       
81    wire [15:0] usbdata_out;
82    
83    wire [3:0]  dac0mux,dac1mux,dac2mux,dac3mux;
84    
85    wire        tx_realsignals;
86    wire [3:0]  rx_numchan;
87    wire [2:0]  tx_numchan;
88    
89    wire [7:0]  interp_rate, decim_rate;
90    wire [15:0] tx_debugbus, rx_debugbus;
91    
92    wire        enable_tx, enable_rx;
93    wire        tx_dsp_reset, rx_dsp_reset, tx_bus_reset, rx_bus_reset;
94    wire [7:0]  settings;
95    
96    // Tri-state bus macro
97    bustri bustri( .data(usbdata_out),.enabledt(OE),.tridata(usbdata) );
98
99    assign      clk64 = master_clk;
100
101    wire [15:0] ch0tx,ch1tx,ch2tx,ch3tx; //,ch4tx,ch5tx,ch6tx,ch7tx;
102    wire [15:0] ch0rx,ch1rx,ch2rx,ch3rx,ch4rx,ch5rx,ch6rx,ch7rx;
103    
104    // TX
105    wire [15:0] i_out_0,i_out_1,q_out_0,q_out_1;
106    wire [15:0] bb_tx_i0,bb_tx_q0,bb_tx_i1,bb_tx_q1;  // bb_tx_i2,bb_tx_q2,bb_tx_i3,bb_tx_q3;
107    
108    wire        strobe_interp, tx_sample_strobe;
109    wire        tx_empty;
110    
111    wire        serial_strobe;
112    wire [6:0]  serial_addr;
113    wire [31:0] serial_data;
114
115    reg [15:0] debug_counter;
116    reg [15:0] loopback_i_0,loopback_q_0;
117    
118    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
119    // Transmit Side
120 `ifdef TX_ON
121    assign      bb_tx_i0 = ch0tx;
122    assign      bb_tx_q0 = ch1tx;
123    assign      bb_tx_i1 = ch2tx;
124    assign      bb_tx_q1 = ch3tx;
125    
126 `ifdef IN_BAND
127         tx_buffer_inband tx_buffer
128      ( .usbclk(usbclk),.bus_reset(tx_bus_reset),.reset(tx_dsp_reset),
129        .usbdata(usbdata),.WR(WR),.have_space(have_space),.tx_underrun(tx_underrun),
130        .channels({tx_numchan,1'b0}),
131        .tx_i_0(ch0tx),.tx_q_0(ch1tx),
132        .tx_i_1(ch2tx),.tx_q_1(ch3tx),
133        .tx_i_2(),.tx_q_2(),
134        .tx_i_3(),.tx_q_3(),
135        .txclk(clk64),.txstrobe(strobe_interp),
136        .clear_status(clear_status),
137        .tx_empty(tx_empty),
138        .debugbus(tx_debugbus) );
139 `else
140    tx_buffer tx_buffer
141      ( .usbclk(usbclk),.bus_reset(tx_bus_reset),.reset(tx_dsp_reset),
142        .usbdata(usbdata),.WR(WR),.have_space(have_space),.tx_underrun(tx_underrun),
143        .channels({tx_numchan,1'b0}),
144        .tx_i_0(ch0tx),.tx_q_0(ch1tx),
145        .tx_i_1(ch2tx),.tx_q_1(ch3tx),
146        .tx_i_2(),.tx_q_2(),
147        .tx_i_3(),.tx_q_3(),
148        .txclk(clk64),.txstrobe(strobe_interp),
149        .clear_status(clear_status),
150        .tx_empty(tx_empty),
151        .debugbus(tx_debugbus) );
152 `endif
153
154  `ifdef TX_EN_0
155    tx_chain tx_chain_0
156      ( .clock(clk64),.reset(tx_dsp_reset),.enable(enable_tx),
157        .interp_rate(interp_rate),.sample_strobe(tx_sample_strobe),
158        .interpolator_strobe(strobe_interp),.freq(),
159        .i_in(bb_tx_i0),.q_in(bb_tx_q0),.i_out(i_out_0),.q_out(q_out_0) );
160  `else
161    assign      i_out_0=16'd0;
162    assign      q_out_0=16'd0;
163  `endif
164
165  `ifdef TX_EN_1
166    tx_chain tx_chain_1
167      ( .clock(clk64),.reset(tx_dsp_reset),.enable(enable_tx),
168        .interp_rate(interp_rate),.sample_strobe(tx_sample_strobe),
169        .interpolator_strobe(strobe_interp),.freq(),
170        .i_in(bb_tx_i1),.q_in(bb_tx_q1),.i_out(i_out_1),.q_out(q_out_1) );
171  `else
172    assign      i_out_1=16'd0;
173    assign      q_out_1=16'd0;
174  `endif
175
176    setting_reg #(`FR_TX_MUX) 
177      sr_txmux(.clock(clk64),.reset(tx_dsp_reset),.strobe(serial_strobe),.addr(serial_addr),.in(serial_data),
178               .out({dac3mux,dac2mux,dac1mux,dac0mux,tx_realsignals,tx_numchan}));
179    
180    wire [15:0] tx_a_a = dac0mux[3] ? (dac0mux[1] ? (dac0mux[0] ? q_out_1 : i_out_1) : (dac0mux[0] ? q_out_0 : i_out_0)) : 16'b0;
181    wire [15:0] tx_b_a = dac1mux[3] ? (dac1mux[1] ? (dac1mux[0] ? q_out_1 : i_out_1) : (dac1mux[0] ? q_out_0 : i_out_0)) : 16'b0;
182    wire [15:0] tx_a_b = dac2mux[3] ? (dac2mux[1] ? (dac2mux[0] ? q_out_1 : i_out_1) : (dac2mux[0] ? q_out_0 : i_out_0)) : 16'b0;
183    wire [15:0] tx_b_b = dac3mux[3] ? (dac3mux[1] ? (dac3mux[0] ? q_out_1 : i_out_1) : (dac3mux[0] ? q_out_0 : i_out_0)) : 16'b0;
184
185    wire txsync = tx_sample_strobe;
186    assign TXSYNC_A = txsync;
187    assign TXSYNC_B = txsync;
188
189    assign tx_a = txsync ? tx_b_a[15:2] : tx_a_a[15:2];
190    assign tx_b = txsync ? tx_b_b[15:2] : tx_a_b[15:2];
191 `endif //  `ifdef TX_ON
192    
193    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
194    // Receive Side
195 `ifdef RX_ON
196    wire        rx_sample_strobe,strobe_decim,hb_strobe;
197    wire [15:0] bb_rx_i0,bb_rx_q0,bb_rx_i1,bb_rx_q1,
198                bb_rx_i2,bb_rx_q2,bb_rx_i3,bb_rx_q3;
199
200    wire loopback = settings[0];
201    wire counter = settings[1];
202
203    always @(posedge clk64)
204      if(rx_dsp_reset)
205        debug_counter <= #1 16'd0;
206      else if(~enable_rx)
207        debug_counter <= #1 16'd0;
208      else if(hb_strobe)
209        debug_counter <=#1 debug_counter + 16'd2;
210    
211    always @(posedge clk64)
212      if(strobe_interp)
213        begin
214           loopback_i_0 <= #1 ch0tx;
215           loopback_q_0 <= #1 ch1tx;
216        end
217    
218    assign ch0rx = counter ? debug_counter : loopback ? loopback_i_0 : bb_rx_i0;
219    assign ch1rx = counter ? debug_counter + 16'd1 : loopback ? loopback_q_0 : bb_rx_q0;
220    assign ch2rx = bb_rx_i1;
221    assign ch3rx = bb_rx_q1;
222    assign ch4rx = bb_rx_i2;
223    assign ch5rx = bb_rx_q2;
224    assign ch6rx = bb_rx_i3;
225    assign ch7rx = bb_rx_q3;
226
227    wire [15:0] ddc0_in_i,ddc0_in_q,ddc1_in_i,ddc1_in_q,ddc2_in_i,ddc2_in_q,ddc3_in_i,ddc3_in_q;
228    wire [31:0] rssi_0,rssi_1,rssi_2,rssi_3;
229    
230    adc_interface adc_interface(.clock(clk64),.reset(rx_dsp_reset),.enable(1'b1),
231                                .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
232                                .rx_a_a(rx_a_a),.rx_b_a(rx_b_a),.rx_a_b(rx_a_b),.rx_b_b(rx_b_b),
233                                .rssi_0(rssi_0),.rssi_1(rssi_1),.rssi_2(rssi_2),.rssi_3(rssi_3),
234                                .ddc0_in_i(ddc0_in_i),.ddc0_in_q(ddc0_in_q),
235                                .ddc1_in_i(ddc1_in_i),.ddc1_in_q(ddc1_in_q),
236                                .ddc2_in_i(ddc2_in_i),.ddc2_in_q(ddc2_in_q),
237                                .ddc3_in_i(ddc3_in_i),.ddc3_in_q(ddc3_in_q),.rx_numchan(rx_numchan) );
238    
239    rx_buffer rx_buffer
240      ( .usbclk(usbclk),.bus_reset(rx_bus_reset),.reset(rx_dsp_reset),
241        .reset_regs(rx_dsp_reset),
242        .usbdata(usbdata_out),.RD(RD),.have_pkt_rdy(have_pkt_rdy),.rx_overrun(rx_overrun),
243        .channels(rx_numchan),
244        .ch_0(ch0rx),.ch_1(ch1rx),
245        .ch_2(ch2rx),.ch_3(ch3rx),
246        .ch_4(ch4rx),.ch_5(ch5rx),
247        .ch_6(ch6rx),.ch_7(ch7rx),
248        .rxclk(clk64),.rxstrobe(hb_strobe),
249        .clear_status(clear_status),
250        .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
251        .debugbus(rx_debugbus) );
252    
253  `ifdef RX_EN_0
254    rx_chain #(`FR_RX_FREQ_0,`FR_RX_PHASE_0) rx_chain_0
255      ( .clock(clk64),.reset(1'b0),.enable(enable_rx),
256        .decim_rate(decim_rate),.sample_strobe(rx_sample_strobe),.decimator_strobe(strobe_decim),.hb_strobe(hb_strobe),
257        .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
258        .i_in(ddc0_in_i),.q_in(ddc0_in_q),.i_out(bb_rx_i0),.q_out(bb_rx_q0),.debugdata(debugdata),.debugctrl(debugctrl));
259  `else
260    assign      bb_rx_i0=16'd0;
261    assign      bb_rx_q0=16'd0;
262  `endif
263    
264  `ifdef RX_EN_1
265    rx_chain #(`FR_RX_FREQ_1,`FR_RX_PHASE_1) rx_chain_1
266      ( .clock(clk64),.reset(1'b0),.enable(enable_rx),
267        .decim_rate(decim_rate),.sample_strobe(rx_sample_strobe),.decimator_strobe(strobe_decim),.hb_strobe(),
268        .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
269        .i_in(ddc1_in_i),.q_in(ddc1_in_q),.i_out(bb_rx_i1),.q_out(bb_rx_q1));
270  `else
271    assign      bb_rx_i1=16'd0;
272    assign      bb_rx_q1=16'd0;
273  `endif
274    
275  `ifdef RX_EN_2
276    rx_chain #(`FR_RX_FREQ_2,`FR_RX_PHASE_2) rx_chain_2
277      ( .clock(clk64),.reset(1'b0),.enable(enable_rx),
278        .decim_rate(decim_rate),.sample_strobe(rx_sample_strobe),.decimator_strobe(strobe_decim),.hb_strobe(),
279        .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
280        .i_in(ddc2_in_i),.q_in(ddc2_in_q),.i_out(bb_rx_i2),.q_out(bb_rx_q2));
281  `else
282    assign      bb_rx_i2=16'd0;
283    assign      bb_rx_q2=16'd0;
284  `endif
285
286  `ifdef RX_EN_3
287    rx_chain #(`FR_RX_FREQ_3,`FR_RX_PHASE_3) rx_chain_3
288      ( .clock(clk64),.reset(1'b0),.enable(enable_rx),
289        .decim_rate(decim_rate),.sample_strobe(rx_sample_strobe),.decimator_strobe(strobe_decim),.hb_strobe(),
290        .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
291        .i_in(ddc3_in_i),.q_in(ddc3_in_q),.i_out(bb_rx_i3),.q_out(bb_rx_q3));
292  `else
293    assign      bb_rx_i3=16'd0;
294    assign      bb_rx_q3=16'd0;
295  `endif
296
297 `endif //  `ifdef RX_ON
298    
299    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
300    // Control Functions
301
302    wire [31:0] capabilities;
303    assign      capabilities[7] =   `TX_CAP_HB;
304    assign      capabilities[6:4] = `TX_CAP_NCHAN;
305    assign      capabilities[3] =   `RX_CAP_HB;
306    assign      capabilities[2:0] = `RX_CAP_NCHAN;
307
308
309    serial_io serial_io
310      ( .master_clk(clk64),.serial_clock(SCLK),.serial_data_in(SDI),
311        .enable(SEN_FPGA),.reset(1'b0),.serial_data_out(SDO),
312        .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
313        .readback_0({io_rx_a,io_tx_a}),.readback_1({io_rx_b,io_tx_b}),.readback_2(capabilities),.readback_3(32'hf0f0931a),
314        .readback_4(rssi_0),.readback_5(rssi_1),.readback_6(rssi_2),.readback_7(rssi_3)
315        );
316
317    wire [15:0] reg_0,reg_1,reg_2,reg_3;
318    master_control master_control
319      ( .master_clk(clk64),.usbclk(usbclk),
320        .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe),
321        .tx_bus_reset(tx_bus_reset),.rx_bus_reset(rx_bus_reset),
322        .tx_dsp_reset(tx_dsp_reset),.rx_dsp_reset(rx_dsp_reset),
323        .enable_tx(enable_tx),.enable_rx(enable_rx),
324        .interp_rate(interp_rate),.decim_rate(decim_rate),
325        .tx_sample_strobe(tx_sample_strobe),.strobe_interp(strobe_interp),
326        .rx_sample_strobe(rx_sample_strobe),.strobe_decim(strobe_decim),
327        .tx_empty(tx_empty),
328        //.debug_0(rx_a_a),.debug_1(ddc0_in_i),
329        .debug_0(rx_debugbus),.debug_1(ddc0_in_i),
330        .debug_2({rx_sample_strobe,strobe_decim,serial_strobe,serial_addr}),.debug_3({rx_dsp_reset,tx_dsp_reset,rx_bus_reset,tx_bus_reset,enable_rx,tx_underrun,rx_overrun,decim_rate}),
331        .reg_0(reg_0),.reg_1(reg_1),.reg_2(reg_2),.reg_3(reg_3) );
332    
333    io_pins io_pins
334      (.io_0(io_tx_a),.io_1(io_rx_a),.io_2(io_tx_b),.io_3(io_rx_b),
335       .reg_0(reg_0),.reg_1(reg_1),.reg_2(reg_2),.reg_3(reg_3),
336       .clock(clk64),.rx_reset(rx_dsp_reset),.tx_reset(tx_dsp_reset),
337       .serial_addr(serial_addr),.serial_data(serial_data),.serial_strobe(serial_strobe));
338    
339    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
340    // Misc Settings
341    setting_reg #(`FR_MODE) sr_misc(.clock(clk64),.reset(rx_dsp_reset),.strobe(serial_strobe),.addr(serial_addr),.in(serial_data),.out(settings));
342
343 endmodule // usrp_inband_usb