Debian Codegraph @

hdl/fpga/ip/nuand/simulation/sample_stream_tb.vhd

raw
1 -- Copyright (c) 2013-2017 Nuand LLC
2 --
3 -- Permission is hereby granted, free of charge, to any person obtaining a copy
4 -- of this software and associated documentation files (the "Software"), to deal
5 -- in the Software without restriction, including without limitation the rights
6 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 -- copies of the Software, and to permit persons to whom the Software is
8 -- furnished to do so, subject to the following conditions:
9 --
10 -- The above copyright notice and this permission notice shall be included in
11 -- all copies or substantial portions of the Software.
12 --
13 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 -- THE SOFTWARE.
20
21 library ieee ;
22 use ieee.std_logic_1164.all ;
23 use ieee.numeric_std.all ;
24 use ieee.math_real.all ;
25
26 library nuand ;
27 use nuand.util.all ;
28 use nuand.fifo_readwrite_p.all;
29 use nuand.common_dcfifo_p.all;
30 use nuand.bladerf_p.all;
31 use nuand.fx3_gpif_p.all;
32
33 library std ;
34 use std.env.all ;
35 use std.textio.all ;
36
37 entity sample_stream_tb is
38 generic (
39 -- For bladeRF (SISO):
40 NUM_MIMO_STREAMS : natural := 1;
41 FIFO_READER_READ_THROTTLE : natural := 1;
42
43 -- For bladeRF2 (2x2 MIMO):
44 --NUM_MIMO_STREAMS : natural := 2;
45 --FIFO_READER_READ_THROTTLE : natural := 0;
46
47 ENABLE_CHANNEL_0 : std_logic := '1';
48 ENABLE_CHANNEL_1 : std_logic := '1'
49 );
50 end entity ;
51
52 architecture arch of sample_stream_tb is
53
54 -- Checking constants
55 constant CHECK_OVERFLOW : boolean := false ;
56 constant CHECK_UNDERFLOW : boolean := false ;
57
58 -- Clock half periods
59 constant FX3_HALF_PERIOD : time := 1.0/(100.0e6)/2.0*1 sec ;
60 constant TX_HALF_PERIOD : time := 1.0/(9.0e6)/2.0*1 sec ;
61 constant RX_HALF_PERIOD : time := 1.0/(9.0e6)/2.0*1 sec ;
62
63 signal dac_controls : sample_controls_t(0 to NUM_MIMO_STREAMS-1) := (others => SAMPLE_CONTROL_DISABLE);
64 signal dac_streams : sample_streams_t(dac_controls'range) := (others => ZERO_SAMPLE);
65 signal adc_controls : sample_controls_t(0 to NUM_MIMO_STREAMS-1) := (others => SAMPLE_CONTROL_DISABLE);
66 signal adc_streams : sample_streams_t(adc_controls'range) := (others => ZERO_SAMPLE);
67
68 -- Clocks
69 signal fx3_clock : std_logic := '1' ;
70 signal tx_clock : std_logic := '1' ;
71 signal rx_clock : std_logic := '1' ;
72
73 signal reset : std_logic := '1' ;
74
75 -- Configuration
76 signal usb_speed : std_logic ;
77 signal meta_en : std_logic ;
78 signal rx_timestamp : unsigned(63 downto 0) := (others =>'0') ;
79 signal tx_timestamp : unsigned(63 downto 0) := (others =>'0') ;
80
81 -- TX Signalling
82 signal tx_enable : std_logic ;
83 signal tx_native_i : signed(11 downto 0) ;
84 signal tx_native_q : signed(11 downto 0) ;
85 signal tx_sample_i : signed(15 downto 0) ;
86 signal tx_sample_q : signed(15 downto 0) ;
87 signal tx_sample_valid : std_logic ;
88
89 signal tx_samples : sample_streams_t(0 to NUM_MIMO_STREAMS-1) := (others => ZERO_SAMPLE);
90
91 -- RX Signalling
92 signal rx_enable : std_logic ;
93 signal rx_native_i : signed(11 downto 0) ;
94 signal rx_native_q : signed(11 downto 0) ;
95 signal rx_sample_i : signed(15 downto 0) ;
96 signal rx_sample_q : signed(15 downto 0) ;
97 signal rx_sample_valid : std_logic ;
98
99 signal rx_samples : sample_streams_t(0 to NUM_MIMO_STREAMS-1) := (others => ZERO_SAMPLE);
100
101 -- Underflow
102 signal underflow_led : std_logic ;
103 signal underflow_count : unsigned(63 downto 0) ;
104 signal underflow_duration : unsigned(15 downto 0) := to_unsigned(20, 16) ;
105
106 -- Overflow
107 signal overflow_led : std_logic ;
108 signal overflow_count : unsigned(63 downto 0) ;
109 signal overflow_duration : unsigned(15 downto 0) := to_unsigned(20, 16) ;
110
111 -- FIFO type
112 type fifo_t is record
113 aclr : std_logic;
114
115 wclock : std_logic;
116 wdata : std_logic_vector;
117 wreq : std_logic;
118 wempty : std_logic;
119 wfull : std_logic;
120 wused : std_logic_vector;
121
122 rclock : std_logic;
123 rdata : std_logic_vector;
124 rreq : std_logic;
125 rempty : std_logic;
126 rfull : std_logic;
127 rused : std_logic_vector;
128 end record;
129
130 -- FIFOs
131 signal tx_sample_fifo : fifo_t(
132 wdata( 31 downto 0), -- GPIF side is always 32 bits
133 rdata(((NUM_MIMO_STREAMS*32)-1) downto 0),
134 rused(compute_rdusedw_high(4096,32,(NUM_MIMO_STREAMS*32),"NO") downto 0),
135 wused(TX_FIFO_T_DEFAULT.wused'range)
136 );
137
138 signal rx_sample_fifo : fifo_t(
139 wdata(((NUM_MIMO_STREAMS*32)-1) downto 0),
140 rdata( 31 downto 0), -- GPIF side is always 32 bits
141 rused(RX_FIFO_T_DEFAULT.rused'range),
142 wused(compute_wrusedw_high(4096,"NO") downto 0)
143 );
144
145 signal tx_meta_fifo : fifo_t(
146 wdata( 31 downto 0),
147 rdata(127 downto 0),
148 rused(META_FIFO_TX_T_DEFAULT.rused'range),
149 wused(META_FIFO_TX_T_DEFAULT.wused'range)
150 );
151
152 signal rx_meta_fifo : fifo_t(
153 wdata(127 downto 0),
154 rdata( 31 downto 0),
155 rused(META_FIFO_RX_T_DEFAULT.rused'range),
156 wused(META_FIFO_RX_T_DEFAULT.wused'range)
157 );
158
159 signal tx_loopback_fifo : loopback_fifo_t;
160
161 -- Loopback controls
162 signal tx_loopback_enabled : std_logic := '0';
163
164 alias rx_reset : std_logic is reset;
165 alias tx_reset : std_logic is reset;
166 alias meta_en_rx : std_logic is meta_en;
167 alias meta_en_tx : std_logic is meta_en;
168 alias rx_ts_reset : std_logic is rx_reset;
169 alias tx_ts_reset : std_logic is tx_reset;
170
171 signal rx_packet_control : packet_control_t;
172 signal tx_packet_control : packet_control_t := PACKET_CONTROL_DEFAULT;
173
174 signal rx_packet_ready : std_logic;
175 signal tx_packet_ready : std_logic;
176 begin
177
178 usb_speed <= '0' ;
179 meta_en <= '1' ;
180
181 increment_tx_ts : process(tx_clock)
182 variable ping : boolean := true ;
183 begin
184 if( rising_edge(tx_clock) ) then
185 ping := not ping ;
186 if( ping = true ) then
187 tx_timestamp <= tx_timestamp + 1 ;
188 end if ;
189 end if ;
190 end process ;
191
192 increment_rx_ts : process(rx_clock)
193 variable ping : boolean := true ;
194 begin
195 if( rising_edge(rx_clock) ) then
196 ping := not ping ;
197 if( ping = true ) then
198 rx_timestamp <= rx_timestamp + 1 ;
199 end if ;
200 end if ;
201 end process ;
202
203 -- Clock creation
204 fx3_clock <= not fx3_clock after FX3_HALF_PERIOD ;
205 tx_clock <= not tx_clock after TX_HALF_PERIOD ;
206 rx_clock <= not rx_clock after RX_HALF_PERIOD ;
207
208 -- TX Submodule
209 U_tx : entity work.tx
210 generic map (
211 NUM_STREAMS => dac_controls'length
212 )
213 port map (
214 tx_reset => tx_reset,
215 tx_clock => tx_clock,
216 tx_enable => tx_enable,
217
218 meta_en => meta_en_tx,
219 timestamp_reset => tx_ts_reset,
220 usb_speed => usb_speed,
221 tx_underflow_led => underflow_led,
222 tx_timestamp => tx_timestamp,
223
224 -- Triggering
225 trigger_arm => '0',
226 trigger_fire => '0',
227 trigger_master => '0',
228 trigger_line => open,
229
230 -- Packet FIFO
231 packet_en => '0',
232 packet_control => tx_packet_control,
233 packet_ready => tx_packet_ready,
234
235 -- Samples from host via FX3
236 sample_fifo_wclock => fx3_clock,
237 sample_fifo_wreq => tx_sample_fifo.wreq,
238 sample_fifo_wdata => tx_sample_fifo.wdata,
239 sample_fifo_wempty => tx_sample_fifo.wempty,
240 sample_fifo_wfull => tx_sample_fifo.wfull,
241 sample_fifo_wused => tx_sample_fifo.wused,
242
243 -- Metadata from host via FX3
244 meta_fifo_wclock => fx3_clock,
245 meta_fifo_wreq => tx_meta_fifo.wreq,
246 meta_fifo_wdata => tx_meta_fifo.wdata,
247 meta_fifo_wempty => tx_meta_fifo.wempty,
248 meta_fifo_wfull => tx_meta_fifo.wfull,
249 meta_fifo_wused => tx_meta_fifo.wused,
250
251 -- Digital Loopback Interface
252 loopback_enabled => tx_loopback_enabled,
253 loopback_fifo_wdata => tx_loopback_fifo.wdata,
254 loopback_fifo_wreq => tx_loopback_fifo.wreq,
255 loopback_fifo_wfull => tx_loopback_fifo.wfull,
256 loopback_fifo_wused => tx_loopback_fifo.wused,
257
258 -- RFFE Interface
259 dac_controls => dac_controls,
260 dac_streams => dac_streams
261 );
262
263 -- RX Submodule
264 U_rx : entity work.rx
265 generic map (
266 NUM_STREAMS => adc_controls'length
267 )
268 port map (
269 rx_reset => rx_reset,
270 rx_clock => rx_clock,
271 rx_enable => rx_enable,
272
273 meta_en => meta_en_rx,
274 timestamp_reset => rx_ts_reset,
275 usb_speed => usb_speed,
276 rx_mux_sel => to_unsigned(4, 3), -- digital loopback
277 rx_overflow_led => overflow_led,
278 rx_timestamp => rx_timestamp,
279
280 -- Triggering
281 trigger_arm => '0',
282 trigger_fire => '0',
283 trigger_master => '0',
284 trigger_line => open,
285
286 -- Packet FIFO
287 packet_en => '0',
288 packet_control => rx_packet_control,
289 packet_ready => rx_packet_ready,
290
291 -- Samples to host via FX3
292 sample_fifo_rclock => fx3_clock,
293 sample_fifo_raclr => not rx_enable,
294 sample_fifo_rreq => rx_sample_fifo.rreq,
295 sample_fifo_rdata => rx_sample_fifo.rdata,
296 sample_fifo_rempty => rx_sample_fifo.rempty,
297 sample_fifo_rfull => rx_sample_fifo.rfull,
298 sample_fifo_rused => rx_sample_fifo.rused,
299
300 -- Mini expansion signals
301 mini_exp => "00",
302
303 -- Metadata to host via FX3
304 meta_fifo_rclock => fx3_clock,
305 meta_fifo_raclr => not rx_enable,
306 meta_fifo_rreq => rx_meta_fifo.rreq,
307 meta_fifo_rdata => rx_meta_fifo.rdata,
308 meta_fifo_rempty => rx_meta_fifo.rempty,
309 meta_fifo_rfull => rx_meta_fifo.rfull,
310 meta_fifo_rused => rx_meta_fifo.rused,
311
312 -- Digital Loopback Interface
313 loopback_fifo_wenabled => tx_loopback_enabled,
314 loopback_fifo_wreset => tx_reset,
315 loopback_fifo_wclock => tx_clock,
316 loopback_fifo_wdata => tx_loopback_fifo.wdata,
317 loopback_fifo_wreq => tx_loopback_fifo.wreq,
318 loopback_fifo_wfull => tx_loopback_fifo.wfull,
319 loopback_fifo_wused => tx_loopback_fifo.wused,
320
321 -- RFFE Interface
322 adc_controls => adc_controls,
323 adc_streams => adc_streams
324 );
325
326 gen_dac_controls : if( NUM_MIMO_STREAMS > 1 ) generate
327 -- The TX side of the AD9361 HDL is a FIFO pull interface
328 -- that expects a readahead FIFO. It toggles the data request
329 -- signal every other cycle. This behavior is mimicked here.
330 process( tx_clock, reset )
331 begin
332 if( reset = '1' ) then
333 dac_controls <= (
334 0 => (enable => ENABLE_CHANNEL_0, data_req => '1'),
335 1 => (enable => ENABLE_CHANNEL_1, data_req => '1')
336 );
337 elsif( rising_edge(tx_clock) ) then
338 for i in dac_controls'range loop
339 dac_controls(i) <= (
340 enable => dac_controls(i).enable,
341 data_req => not dac_controls(i).data_req );
342 end loop;
343 end if;
344 end process;
345 else generate
346 -- The LMS6 HDL is not nearly as complicated (or featureful)
347 -- as the AD9361. For LMS6, we just push samples directly to
348 -- the device, so the data request line can stay asserted.
349 dac_controls <= (others => SAMPLE_CONTROL_ENABLE);
350 end generate;
351
352 gen_adc_controls : if( NUM_MIMO_STREAMS > 1 ) generate
353 process( rx_clock, reset )
354 begin
355 if( reset = '1' ) then
356 adc_controls <= (
357 0 => (enable => ENABLE_CHANNEL_0, data_req => '1'),
358 1 => (enable => ENABLE_CHANNEL_1, data_req => '1')
359 );
360 elsif( rising_edge(rx_clock) ) then
361 for i in adc_controls'range loop
362 adc_controls(i) <= (
363 enable => adc_controls(i).enable,
364 data_req => not adc_controls(i).data_req );
365 end loop;
366 end if;
367 end process;
368 else generate
369 adc_controls <= (others => SAMPLE_CONTROL_ENABLE);
370 end generate;
371
372 -- TX FIFO Filler
373 tx_filler : process
374 variable ang : real := 0.0 ;
375 variable dang : real := MATH_PI/100.0 ;
376 variable sample_i : signed(15 downto 0) ;
377 variable sample_q : signed(15 downto 0) ;
378 variable ts : integer ;
379 begin
380 if( reset = '1' ) then
381 tx_sample_fifo.wdata <= (others =>'0') ;
382 tx_sample_fifo.wreq <= '0' ;
383 wait until reset = '0' ;
384 end if ;
385 for i in 1 to 100 loop
386 wait until rising_edge(fx3_clock) ;
387 end loop ;
388 for j in 1 to 5 loop
389 ts := 16#1000# + (j-1)*10000 ;
390 for i in 1 to 5 loop
391 tx_meta_fifo.wdata <= (others =>'0') ;
392 wait until rising_edge(fx3_clock) and unsigned(tx_sample_fifo.wused) < 1024 ;
393 tx_meta_fifo.wdata <= x"12345678" ;
394 tx_meta_fifo.wreq <= '1' ;
395 wait until rising_edge(fx3_clock) ;
396 tx_meta_fifo.wdata <= std_logic_vector(to_unsigned(ts,tx_meta_fifo.wdata'length)) ;
397 ts := ts + 1020 ;
398 wait until rising_edge(fx3_clock) ;
399 tx_meta_fifo.wdata <= x"00000000" ;
400 wait until rising_edge(fx3_clock) ;
401 tx_meta_fifo.wdata <= x"00000000" ;
402 wait until rising_edge(fx3_clock) ;
403 tx_meta_fifo.wreq <= '0' ;
404 tx_meta_fifo.wdata <= (others =>'0') ;
405 for r in 1 to 1020 loop
406 if( r = 1 or r = 1020 ) then
407 sample_i := (others =>'0') ;
408 sample_q := (others =>'0') ;
409 else
410 sample_i := to_signed(2047, sample_i'length) ;
411 sample_q := to_signed(2047, sample_q'length) ;
412 end if ;
413 sample_i := to_signed(integer(2047.0*cos(ang)),sample_i'length);
414 sample_q := to_signed(integer(2047.0*sin(ang)),sample_q'length);
415 tx_sample_fifo.wdata <= std_logic_vector(sample_q & sample_i) after 0.1 ns ;
416 tx_sample_fifo.wreq <= '1' after 0.1 ns ;
417 nop( fx3_clock, 1 );
418 tx_sample_fifo.wreq <= '0' after 0.1 ns ;
419 ang := (ang + dang) mod MATH_2_PI ;
420 end loop ;
421 if( CHECK_UNDERFLOW ) then
422 nop( fx3_clock, 3000 ) ;
423 end if ;
424 tx_sample_fifo.wdata <= (others =>'X') after 0.1 ns ;
425 end loop ;
426 nop(fx3_clock, 100000) ;
427 end loop ;
428 wait ;
429 end process ;
430
431 -- RX FIFO Reader
432 rx_reader : process
433 begin
434 if( reset = '1' ) then
435 rx_sample_fifo.rreq <= '0' ;
436 wait until reset = '0' ;
437 end if ;
438 while true loop
439 wait until rising_edge(fx3_clock) and unsigned(rx_sample_fifo.rused) > GPIF_BUF_SIZE_SS;
440 for i in 1 to GPIF_BUF_SIZE_SS loop
441 rx_sample_fifo.rreq <= '1' ;
442 nop( fx3_clock, 1 ) ;
443 if( CHECK_OVERFLOW ) then
444 rx_sample_fifo.rreq <= '0' ;
445 nop( fx3_clock, 5 ) ;
446 end if ;
447 end loop ;
448 rx_sample_fifo.rreq <= '0' ;
449 end loop ;
450 end process ;
451
452 -- Testbench
453 tb : process
454 begin
455 -- Initializing
456 reset <= '1' ;
457 tx_enable <= '0' ;
458 rx_enable <= '0' ;
459 nop( fx3_clock, 10 ) ;
460 reset <= '0' ;
461 nop( fx3_clock, 10 ) ;
462 rx_enable <= '1' ;
463 tx_enable <= '1' ;
464 nop( fx3_clock, 2000000 ) ;
465 report "-- End of Simulation --" ;
466 stop(2) ;
467 wait ;
468 end process ;
469
470 end architecture ;