Debian Codegraph @

hdl/fpga/platforms/bladerf-micro/vhdl/tb/fx3_gpif_meta_8bit_tb.vhd

raw
1 -- Copyright (c) 2022 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
25 library rtl_work;
26
27 library altera_lnsim;
28 use altera_lnsim.altera_pll;
29
30 library nuand;
31 use nuand.util.all ;
32 use nuand.fifo_readwrite_p.all;
33 use nuand.common_dcfifo_p.all;
34 use nuand.bladerf_p.all;
35 use nuand.fx3_gpif_p.all;
36
37 entity fx3_gpif_meta_8bit_tb is
38 generic (
39 -- For bladeRF2 (2x2 MIMO):
40 NUM_MIMO_STREAMS : natural := 2;
41 FIFO_READER_READ_THROTTLE : natural := 0;
42
43 ENABLE_CHANNEL_0 : std_logic := '1';
44 ENABLE_CHANNEL_1 : std_logic := '1';
45 EIGHT_BIT_MODE_EN : std_logic := '1'
46 );
47 end entity;
48
49 architecture arch of fx3_gpif_meta_8bit_tb is
50
51 -- bladerf-hosted uses ad9361.clock (125 MHz) for rx_clock and tx_clock
52 constant SYSCLK_HALF_PERIOD : time := 1 sec * (1.0/125.0e6/2.0);
53
54 -- For reasons unknown, simulation of this system in ModelSim requires that
55 -- wrreq and data be asserted shortly before the rising clock edge. Sigh...
56 constant FIFO_WORKAROUND : time := 1 ps;
57
58 type fx3_control_t is record
59 usb_speed : std_logic;
60 tx_enable : std_logic;
61 rx_enable : std_logic;
62 meta_enable : std_logic;
63 packet : std_logic;
64 end record;
65
66 type fx3_gpif_t is record
67 gpif_in : std_logic_vector(31 downto 0);
68 gpif_out : std_logic_vector(31 downto 0);
69 gpif_oe : std_logic;
70 ctl_in : std_logic_vector(12 downto 0);
71 ctl_out : std_logic_vector(12 downto 0);
72 ctl_oe : std_logic_vector(12 downto 0);
73 end record;
74
75 signal sys_rst : std_logic := '1';
76 signal done : boolean := false;
77
78 signal fx3_pclk : std_logic := '1';
79 signal fx3_pclk_pll : std_logic := '1';
80 signal sys_clk : std_logic := '1';
81
82 signal pll_reset : std_logic := '0';
83 signal pll_locked : std_logic := '0';
84 signal fx3_control : fx3_control_t;
85 signal fx3_gpif : fx3_gpif_t;
86 signal fx3_gpif_i : std_logic_vector(31 downto 0);
87 signal fx3_ctl_i : std_logic_vector(12 downto 0);
88
89 -- Control mapping
90 alias dma0_rx_ack is fx3_ctl_i( 0);
91 alias dma1_rx_ack is fx3_ctl_i( 1);
92 alias dma2_tx_ack is fx3_ctl_i( 2);
93 alias dma3_tx_ack is fx3_ctl_i( 3);
94 alias dma_rx_enable is fx3_ctl_i( 4);
95 alias dma_tx_enable is fx3_ctl_i( 5);
96 alias dma_idle is fx3_ctl_i( 6);
97 alias system_reset is fx3_ctl_i( 7);
98 alias dma0_rx_reqx is fx3_ctl_i( 8);
99 alias dma1_rx_reqx is fx3_ctl_i(12); -- due to 9 being connected to dclk
100 alias dma2_tx_reqx is fx3_ctl_i(10);
101 alias dma3_tx_reqx is fx3_ctl_i(11);
102
103 signal loopback_i : signed(15 downto 0) := (others =>'0');
104 signal loopback_q : signed(15 downto 0) := (others =>'0');
105 signal loopback_valid : std_logic := '0';
106 signal loopback_enabled : std_logic := '0';
107
108 signal tx_clock : std_logic := '1' ;
109 signal rx_clock : std_logic := '1' ;
110
111 signal rx_sample_fifo : rx_fifo_t := RX_FIFO_T_DEFAULT;
112 signal tx_sample_fifo : tx_fifo_t := TX_FIFO_T_DEFAULT;
113 signal loopback_fifo : loopback_fifo_t := LOOPBACK_FIFO_T_DEFAULT;
114 signal rx_meta_fifo : meta_fifo_rx_t := META_FIFO_RX_T_DEFAULT;
115 signal tx_meta_fifo : meta_fifo_tx_t := META_FIFO_TX_T_DEFAULT;
116
117 signal rx_mux_i : signed(15 downto 0);
118 signal rx_mux_q : signed(15 downto 0);
119 signal rx_mux_valid : std_logic;
120
121 signal tx_timestamp : unsigned(63 downto 0) := ( others => '0' );
122 signal rx_timestamp : unsigned(63 downto 0) := ( others => '0' );
123
124 constant FX3_HALF_PERIOD : time := 1.0/(100.0e6)/2.0*1 sec ;
125 constant TX_HALF_PERIOD : time := 1.0/(9.0e6)/2.0*1 sec ;
126 constant RX_HALF_PERIOD : time := 1.0/(9.0e6)/2.0*1 sec ;
127
128 signal dac_controls : sample_controls_t(0 to NUM_MIMO_STREAMS-1) := (others => SAMPLE_CONTROL_DISABLE);
129 signal dac_streams : sample_streams_t(dac_controls'range) := (others => ZERO_SAMPLE);
130 signal adc_controls : sample_controls_t(0 to NUM_MIMO_STREAMS-1) := (others => SAMPLE_CONTROL_DISABLE);
131 signal adc_streams : sample_streams_t(adc_controls'range) := (others => ZERO_SAMPLE);
132
133 signal tx_reset : std_logic := '0';
134 signal tx_ts_reset : std_logic := '0';
135 signal rx_reset : std_logic := '0';
136 signal rx_ts_reset : std_logic := '0';
137
138 signal meta_en_tx : std_logic := '0';
139 signal meta_en_rx : std_logic := '0';
140
141 signal rx_packet_control : packet_control_t := PACKET_CONTROL_DEFAULT;
142 signal tx_packet_control : packet_control_t;
143
144 signal trigger_signal_sync_tb : std_logic;
145 signal adc_stream_val_at_rx_enable : signed(15 downto 0) := ( others => '0' );
146
147 function data_gen (count : natural) return std_logic_vector is
148 variable msw, lsw : std_logic_vector(15 downto 0);
149 begin
150 msw := std_logic_vector(to_signed(count, 16));
151 lsw := std_logic_vector(to_signed(-count, 16));
152
153 return (msw & lsw);
154 end function data_gen;
155
156 signal rx_packet_ready : std_logic;
157
158 begin
159
160 -- ========================================================================
161 -- Brief summary of data flow
162 -- ========================================================================
163 --
164 -- Simulated samples flow from fx3_model's TX side to its RX side. The
165 -- validity of the data is checked there.
166 --
167 -- entity U_fx3_model FX3 model, TX process
168 -- signal fx3_gpif_i fx3_gpif.gpif_oe is '0'
169 -- process register_gpif
170 -- signal fx3_gpif.gpif_in Demuxed GPIF interface (FX3 -> FPGA)
171 -- entity U_fx3_gpif GPIF implementation, write side
172 -- signal tx_sample_fifo.w* TX fifo, write side
173 -- entity U_tx_sample_fifo
174 -- signal tx_sample_fifo.r* TX fifo, read side
175 -- process tx_mux
176 -- signal loopback_fifo.w* Loopback fifo, write side
177 -- entity U_rx_loopback_fifo
178 -- signal loopback_fifo.r* Loopback fifo, read side
179 -- process loopback_fifo_control
180 -- signal loopback_{i,q,valid}
181 -- process rx_mux
182 -- signal rx_mux_{i,q,valid}
183 -- entity U_fifo_writer
184 -- signal rx_sample_fifo.w* RX fifo, write side
185 -- entity U_rx_sample_fifo
186 -- signal rx_sample_fifo.r* RX fifo, read side
187 -- entity U_fx3_gpif GPIF implementation, read side
188 -- signal fx3_gpif.gpif_out Demuxed GPIF interface (FPGA -> FX3)
189 -- process register_gpif
190 -- signal fx3_gpif_i fx3_gpif.gpif_oe is '1'
191 -- entity U_fx3_model FX3 model, RX process
192
193
194 -- ========================================================================
195 -- Instantiations
196 -- ========================================================================
197
198 -- Unit under test
199 -- Clock domain: fx3_pclk_pll
200 U_fx3_gpif : entity nuand.fx3_gpif
201 port map (
202 pclk => fx3_pclk_pll,
203 reset => sys_rst,
204 usb_speed => fx3_control.usb_speed,
205 gpif_in => fx3_gpif.gpif_in,
206 gpif_out => fx3_gpif.gpif_out,
207 gpif_oe => fx3_gpif.gpif_oe,
208 ctl_in => fx3_gpif.ctl_in,
209 ctl_out => fx3_gpif.ctl_out,
210 ctl_oe => fx3_gpif.ctl_oe,
211 tx_enable => fx3_control.tx_enable,
212 rx_enable => fx3_control.rx_enable,
213 meta_enable => fx3_control.meta_enable,
214 packet_enable => fx3_control.packet,
215 tx_fifo_write => tx_sample_fifo.wreq,
216 tx_fifo_full => tx_sample_fifo.wfull,
217 tx_fifo_empty => tx_sample_fifo.wempty,
218 tx_fifo_usedw => tx_sample_fifo.wused,
219 tx_fifo_data => tx_sample_fifo.wdata,
220 tx_timestamp => tx_timestamp,
221 tx_meta_fifo_write => tx_meta_fifo.wreq,
222 tx_meta_fifo_full => tx_meta_fifo.wfull,
223 tx_meta_fifo_empty => tx_meta_fifo.wempty,
224 tx_meta_fifo_usedw => tx_meta_fifo.wused,
225 tx_meta_fifo_data => tx_meta_fifo.wdata,
226 rx_fifo_read => rx_sample_fifo.rreq,
227 rx_fifo_full => rx_sample_fifo.rfull,
228 rx_fifo_empty => rx_sample_fifo.rempty,
229 rx_fifo_usedw => rx_sample_fifo.rused,
230 rx_fifo_data => rx_sample_fifo.rdata,
231 rx_meta_fifo_read => rx_meta_fifo.rreq,
232 rx_meta_fifo_full => rx_meta_fifo.rfull,
233 rx_meta_fifo_empty => rx_meta_fifo.rempty,
234 rx_meta_fifo_usedr => rx_meta_fifo.rused,
235 rx_meta_fifo_data => rx_meta_fifo.rdata
236 );
237
238 -- Discrete control signals for fx3_gpif
239 fx3_control.usb_speed <= '0';
240 fx3_control.meta_enable <= '1';
241 fx3_control.packet <= '0';
242
243 U_pkt_gen : entity nuand.rx_packet_generator
244 port map(
245 rx_clock => rx_clock,
246 rx_reset => rx_reset,
247
248 rx_enable => fx3_control.rx_enable,
249 rx_packet_enable => fx3_control.packet,
250
251 rx_packet_ready => rx_packet_ready,
252
253 rx_packet_control => rx_packet_control
254
255 );
256
257
258 meta_en_tx <= fx3_control.meta_enable;
259 meta_en_rx <= fx3_control.meta_enable;
260
261 increment_tx_ts : process(tx_clock)
262 variable ping : boolean := true ;
263 begin
264 if( rising_edge(tx_clock) ) then
265 ping := not ping ;
266 if( ping = true ) then
267 tx_timestamp <= tx_timestamp + 1 ;
268 end if ;
269 end if ;
270 end process ;
271
272 increment_rx_ts : process(rx_clock)
273 variable ping : boolean := true ;
274 begin
275 if( rising_edge(rx_clock) ) then
276 ping := not ping ;
277 if( ping = true ) then
278 rx_timestamp <= rx_timestamp + 1 ;
279 end if ;
280 end if ;
281 end process ;
282
283 -- Model of FX3's GPIF interface
284 -- Clock domain: fx3_pclk (source)
285 U_fx3_model : entity nuand.fx3_model(micro_dma)
286 port map (
287 fx3_pclk => fx3_pclk,
288 fx3_gpif => fx3_gpif_i,
289 fx3_ctl => fx3_ctl_i,
290 fx3_uart_rxd => '0',
291 fx3_uart_txd => open,
292 fx3_uart_cts => open,
293 fx3_rx_en => '1',
294 fx3_rx_meta_en => meta_en_rx,
295 fx3_tx_en => '1',
296 fx3_tx_meta_en => meta_en_tx,
297 EIGHT_BIT_MODE_EN => EIGHT_BIT_MODE_EN,
298 done => done
299 );
300
301 -- Generate phase-shifted PLL clock
302 U_fx3_pll : entity rtl_work.fx3_pll
303 port map (
304 inclk0 => fx3_pclk,
305 areset => pll_reset,
306 c0 => fx3_pclk_pll,
307 locked => pll_locked
308 );
309
310 tx_clock <= not tx_clock after TX_HALF_PERIOD ;
311 rx_clock <= not rx_clock after RX_HALF_PERIOD ;
312
313
314 -- Reset handler
315 sys_rst <= '0' after 100 ns;
316
317 -- Generate system clock
318 sys_clk <= not sys_clk after SYSCLK_HALF_PERIOD when not done else '0';
319
320
321 tx_reset <= sys_rst;
322 tx_ts_reset <= sys_rst;
323 rx_reset <= sys_rst;
324 rx_ts_reset <= sys_rst;
325
326 meta_en_tx <= fx3_control.meta_enable;
327 meta_en_rx <= fx3_control.meta_enable;
328
329 -- TX Submodule
330 U_tx : entity work.tx
331 generic map (
332 NUM_STREAMS => dac_controls'length
333 )
334 port map (
335 tx_reset => tx_reset,
336 tx_clock => tx_clock,
337 tx_enable => fx3_control.tx_enable,
338
339 meta_en => meta_en_tx,
340 timestamp_reset => tx_ts_reset,
341 usb_speed => fx3_control.usb_speed,
342 tx_underflow_led => open,
343 tx_timestamp => tx_timestamp,
344
345 -- Triggering
346 trigger_arm => '0',
347 trigger_fire => '0',
348 trigger_master => '0',
349 trigger_line => open,
350
351 -- Packet FIFO
352 packet_en => fx3_control.packet,
353 packet_empty => open,
354 packet_control => tx_packet_control,
355 packet_ready => '1',
356
357 -- 8-bit mode
358 EIGHT_BIT_MODE_EN => EIGHT_BIT_MODE_EN,
359
360 -- Samples from host via FX3
361 sample_fifo_wclock => fx3_pclk_pll,
362 sample_fifo_wreq => tx_sample_fifo.wreq,
363 sample_fifo_wdata => tx_sample_fifo.wdata,
364 sample_fifo_wempty => tx_sample_fifo.wempty,
365 sample_fifo_wfull => tx_sample_fifo.wfull,
366 sample_fifo_wused => tx_sample_fifo.wused,
367
368 -- Metadata from host via FX3
369 meta_fifo_wclock => fx3_pclk_pll,
370 meta_fifo_wreq => tx_meta_fifo.wreq,
371 meta_fifo_wdata => tx_meta_fifo.wdata,
372 meta_fifo_wempty => tx_meta_fifo.wempty,
373 meta_fifo_wfull => tx_meta_fifo.wfull,
374 meta_fifo_wused => tx_meta_fifo.wused,
375
376 -- Digital Loopback Interface
377 loopback_enabled => '0',
378 loopback_fifo_wdata => loopback_fifo.wdata,
379 loopback_fifo_wreq => loopback_fifo.wreq,
380 loopback_fifo_wfull => loopback_fifo.wfull,
381 loopback_fifo_wused => loopback_fifo.wused,
382
383 -- RFFE Interface
384 dac_controls => dac_controls,
385 dac_streams => dac_streams
386 );
387
388 -- RX Submodule
389 U_rx : entity work.rx
390 generic map (
391 NUM_STREAMS => adc_controls'length
392 )
393 port map (
394 rx_reset => rx_reset,
395 rx_clock => rx_clock,
396 rx_enable => fx3_control.rx_enable,
397
398 meta_en => meta_en_rx,
399 timestamp_reset => rx_ts_reset,
400 usb_speed => fx3_control.usb_speed,
401 rx_mux_sel => to_unsigned(0, 3),
402 rx_overflow_led => open,
403 rx_timestamp => rx_timestamp,
404
405 -- Triggering
406 trigger_arm => '0',
407 trigger_fire => '0',
408 trigger_master => '0',
409 trigger_line => open,
410 trigger_signal_sync_tb => trigger_signal_sync_tb,
411
412 -- Packet FIFO
413 packet_en => fx3_control.packet,
414 packet_control => rx_packet_control,
415 packet_ready => rx_packet_ready,
416
417 -- 8-bit mode
418 EIGHT_BIT_MODE_EN => EIGHT_BIT_MODE_EN,
419
420 -- Samples to host via FX3
421 sample_fifo_rclock => fx3_pclk_pll,
422 sample_fifo_raclr => not fx3_control.rx_enable,
423 sample_fifo_rreq => rx_sample_fifo.rreq,
424 sample_fifo_rdata => rx_sample_fifo.rdata,
425 sample_fifo_rempty => rx_sample_fifo.rempty,
426 sample_fifo_rfull => rx_sample_fifo.rfull,
427 sample_fifo_rused => rx_sample_fifo.rused,
428
429 -- Mini expansion signals
430 mini_exp => "00",
431
432 -- Metadata to host via FX3
433 meta_fifo_rclock => fx3_pclk_pll,
434 meta_fifo_raclr => not fx3_control.rx_enable,
435 meta_fifo_rreq => rx_meta_fifo.rreq,
436 meta_fifo_rdata => rx_meta_fifo.rdata,
437 meta_fifo_rempty => rx_meta_fifo.rempty,
438 meta_fifo_rfull => rx_meta_fifo.rfull,
439 meta_fifo_rused => rx_meta_fifo.rused,
440
441 -- Digital Loopback Interface
442 loopback_fifo_wenabled => open,
443 loopback_fifo_wreset => tx_reset,
444 loopback_fifo_wclock => tx_clock,
445 loopback_fifo_wdata => loopback_fifo.wdata,
446 loopback_fifo_wreq => loopback_fifo.wreq,
447 loopback_fifo_wfull => loopback_fifo.wfull,
448 loopback_fifo_wused => loopback_fifo.wused,
449
450 -- RFFE Interface
451 adc_controls => adc_controls,
452 adc_streams => adc_streams
453 );
454
455 -- ========================================================================
456 -- Data converters
457 -- ========================================================================
458
459 gen_dac_controls : if( NUM_MIMO_STREAMS > 1 ) generate
460 -- The TX side of the AD9361 HDL is a FIFO pull interface
461 -- that expects a readahead FIFO. It toggles the data request
462 -- signal every other cycle. This behavior is mimicked here.
463 process( tx_clock, sys_rst )
464 begin
465 if( sys_rst = '1' ) then
466 dac_controls <= (
467 0 => (enable => ENABLE_CHANNEL_0, data_req => '1'),
468 1 => (enable => ENABLE_CHANNEL_1, data_req => '1')
469 );
470 elsif( rising_edge(tx_clock) ) then
471 for i in dac_controls'range loop
472 dac_controls(i) <= (
473 enable => dac_controls(i).enable,
474 data_req => not dac_controls(i).data_req );
475 end loop;
476 end if;
477 end process;
478 else generate
479 dac_controls <= (others => SAMPLE_CONTROL_ENABLE);
480 end generate;
481
482 gen_adc_controls : if( NUM_MIMO_STREAMS > 1 ) generate
483 process( rx_clock, sys_rst )
484 constant SIGMA_DELTA_BITS : signed (3 downto 0) := "0000";
485 constant COUNT_RESET : integer := 0;
486 variable count : integer range -2047 to 2047 := COUNT_RESET;
487 begin
488 if( sys_rst = '1' ) then
489 count := COUNT_RESET;
490 adc_controls <= (
491 0 => (enable => ENABLE_CHANNEL_0, data_req => '1'),
492 1 => (enable => ENABLE_CHANNEL_1, data_req => '1')
493 );
494 elsif( rising_edge(rx_clock) ) then
495 for i in adc_controls'range loop
496 adc_controls(i) <= (
497 enable => adc_controls(i).enable,
498 data_req => not adc_controls(i).data_req );
499 if( adc_controls(i).enable = '1') then
500 if( adc_streams(i).data_v = '1' ) then
501 if( EIGHT_BIT_MODE_EN = '1' ) then
502 adc_streams(i).data_i <= "0000" & to_signed(count, 8) & SIGMA_DELTA_BITS;
503 adc_streams(i).data_q <= "0000" & to_signed(-count, 8) & SIGMA_DELTA_BITS;
504 count := (count + 1) mod 128;
505 else
506 adc_streams(i).data_i <= to_signed(count, 16);
507 adc_streams(i).data_q <= to_signed(-count, 16);
508 count := (count + 1) mod 2048;
509 end if;
510 end if;
511 end if;
512 adc_streams(i).data_v <= not adc_streams(i).data_v;
513 end loop;
514 end if;
515 end process;
516 else generate
517 adc_controls <= (others => SAMPLE_CONTROL_ENABLE);
518 end generate;
519
520 -- ========================================================================
521 -- Processes
522 -- ========================================================================
523
524 -- FX3 GPIF bidirectional signal control
525 -- Adapted from same process in bladerf-hosted.vhd
526 register_gpif : process(sys_rst, fx3_pclk_pll)
527 begin
528 if( sys_rst = '1' ) then
529 fx3_gpif_i <= (others => 'Z');
530 fx3_gpif.gpif_in <= (others => 'Z');
531 elsif( rising_edge(fx3_pclk_pll) ) then
532 fx3_gpif.gpif_in <= fx3_gpif_i;
533
534 if( fx3_gpif.gpif_oe = '1' ) then
535 fx3_gpif_i <= fx3_gpif.gpif_out;
536 else
537 fx3_gpif_i <= (others =>'Z');
538 end if;
539 end if;
540 end process;
541
542 -- FX3 CTL bidirectional signals
543 -- Adapted from same generator in bladerf-hosted.vhd
544 generate_ctl : for i in fx3_ctl_i'range generate
545 fx3_ctl_i(i) <= fx3_gpif.ctl_out(i) when fx3_gpif.ctl_oe(i) = '1' else 'Z';
546 end generate;
547
548 fx3_gpif.ctl_in <= fx3_ctl_i;
549
550 -- Controller for loopback FIFO
551 -- Stripped-down version of same process from rx.vhd
552 loopback_fifo_control : process( sys_rst, loopback_fifo.rclock )
553 begin
554 if( sys_rst = '1' ) then
555 loopback_enabled <= '0';
556 loopback_fifo.rreq <= '0';
557 loopback_i <= (others => '0');
558 loopback_q <= (others => '0');
559 loopback_valid <= '0';
560 elsif( rising_edge(loopback_fifo.rclock) ) then
561 loopback_enabled <= '0';
562 loopback_fifo.rreq <= '0';
563 loopback_i <= loopback_i;
564 loopback_q <= loopback_q;
565 loopback_valid <= '0';
566
567 -- Is loopback enabled?
568 loopback_enabled <= fx3_control.rx_enable;
569
570 -- Do the loopback
571 loopback_i <= resize(signed(loopback_fifo.rdata(15 downto 0)), loopback_i'length);
572 loopback_q <= resize(signed(loopback_fifo.rdata(31 downto 16)), loopback_q'length);
573 loopback_valid <= loopback_fifo.rreq and not loopback_fifo.rempty;
574
575 -- Read from the FIFO if req'd
576 loopback_fifo.rreq <= loopback_enabled and (not loopback_fifo.rempty);
577 end if;
578 end process;
579
580 -- RX multiplexer
581 -- Stripped-down version of same process from rx.vhd
582 rx_mux : process(sys_rst, sys_clk)
583 begin
584 if( sys_rst = '1' ) then
585 rx_mux_i <= (others =>'0');
586 rx_mux_q <= (others =>'0');
587 rx_mux_valid <= '0';
588 elsif( rising_edge(sys_clk) ) then
589 rx_mux_i <= loopback_i;
590 rx_mux_q <= loopback_q;
591 rx_mux_valid <= loopback_valid;
592 end if;
593 end process;
594
595 -- TX multiplexer
596 -- Stripped-down version of mimo_channel_sel_mux from tx.vhd
597 --tx_mux : process( sys_clk )
598 --begin
599 -- if( rising_edge(sys_clk) ) then
600 -- tx_sample_fifo.rreq <= not tx_sample_fifo.rempty;
601
602 -- loopback_fifo.wdata <= tx_sample_fifo.rdata;
603 -- loopback_fifo.wreq <= tx_sample_fifo.rreq and not tx_sample_fifo.rempty;
604 -- end if;
605 --end process;
606
607
608 -- ========================================================================
609 -- Verification
610 -- ========================================================================
611
612 --
613 -- Check for metavalues
614 -- We should never see metavalues written to the FIFOs
615 --
616 check_fifo_write : process(fx3_pclk_pll) is
617 begin
618 if( rising_edge(fx3_pclk_pll) ) then
619 if( tx_sample_fifo.wreq = '1' and fx3_control.meta_enable = '0' ) then
620 for i in tx_sample_fifo.wdata'range loop
621 assert tx_sample_fifo.wdata(i) = '0' or tx_sample_fifo.wdata(i) = '1'
622 severity failure;
623 end loop;
624 end if;
625 end if;
626 end process;
627
628 check_fifo_read : process(fx3_pclk_pll) is
629 begin
630 if( rising_edge(fx3_pclk_pll) ) then
631 if( rx_sample_fifo.rreq = '1' ) then
632 for i in rx_sample_fifo.rdata'range loop
633 assert rx_sample_fifo.rdata(i) = '0' or rx_sample_fifo.rdata(i) = '1'
634 severity failure;
635 end loop;
636 end if;
637 end if;
638 end process check_fifo_read;
639
640 --
641 -- Check for fullness on FIFOs
642 --
643 assert(rx_sample_fifo.wfull = '0') report "rx_sample_fifo full (write)" severity warning;
644 assert(tx_sample_fifo.wfull = '0') report "tx_sample_fifo full (write)" severity warning;
645 assert(loopback_fifo.wfull = '0') report "loopback_fifo full (write)" severity warning;
646
647 --
648 -- Tx Sample Check
649 --
650 tx_sample_start_check: process
651 type time_state_t is (
652 WAIT_FOR_HEADER_TIMESTAMP,
653 WAIT_FOR_SAMPLE
654 );
655 variable state : time_state_t := WAIT_FOR_HEADER_TIMESTAMP;
656 variable timestamp_in_header : unsigned (31 downto 0) := (others => '0');
657 begin
658 case state is
659 when WAIT_FOR_HEADER_TIMESTAMP =>
660 wait until rising_edge(dma3_tx_reqx);
661 if( fx3_control.meta_enable = '1' ) then
662 wait until rising_edge(fx3_pclk);
663 wait until rising_edge(fx3_pclk);
664 report "Timestamp: 0x" & to_hstring(fx3_gpif_i);
665 timestamp_in_header := unsigned(fx3_gpif_i);
666 report "Timestamp Saved: " & to_string(to_integer(timestamp_in_header));
667 end if;
668 state := WAIT_FOR_SAMPLE;
669
670 when WAIT_FOR_SAMPLE =>
671 while( timestamp_in_header /= tx_timestamp ) loop
672 assert(tx_timestamp < timestamp_in_header)
673 report "Header timestamp is in the past. Samples " &
674 "usually ignored but considered a failure in tb."
675 severity failure;
676 for i in dac_controls'range loop
677 assert(dac_streams(i).data_v = '0')
678 report "Dac_stream("&to_string(i)&") data being transmitted before expected tx_timestamp."
679 severity failure;
680 end loop;
681 wait until rising_edge(tx_clock);
682 end loop;
683 report "TX TIMESTAMP = META TIMESTAMP";
684
685 wait until rising_edge(tx_clock);
686 for i in dac_controls'range loop
687 if( dac_controls(i).enable = '1') then
688 assert(dac_streams(i).data_v = '1')
689 report "Dac_stream("&to_string(i)&") sample not seen at expected tx_timestamp."
690 severity failure;
691 end if;
692 end loop;
693 state := WAIT_FOR_HEADER_TIMESTAMP;
694 end case;
695 end process tx_sample_start_check;
696
697 look_for_dropped_tx_samples : process(tx_clock) is
698 constant MESSAGES_PER_ITERATION : natural := 3; --Provided in fx3_model
699 constant SIGMA_DELTA_BITS : signed (3 downto 0) := "0000";
700 constant HEADER_LEN : natural := 4; -- In clk cycles
701 variable valid_sample_count : natural := 0;
702 variable current_message_count : natural := 1;
703 variable iq_value : signed (15 downto 0) := x"0000";
704 variable expected_sample_i : signed (15 downto 0);
705 variable expected_sample_q : signed (15 downto 0);
706 variable failure_count : natural := 0;
707 begin
708 for i in dac_controls'range loop
709 if( rising_edge(tx_clock)
710 and dac_controls(i).enable = '1'
711 and dac_streams(i).data_v = '1'
712 and fx3_control.tx_enable = '1' )
713 then
714 valid_sample_count := valid_sample_count + 1;
715 if( EIGHT_BIT_MODE_EN = '1' ) then
716 expected_sample_i := iq_value (11 downto 0) & SIGMA_DELTA_BITS;
717 expected_sample_q := iq_value (11 downto 0) + 1 & SIGMA_DELTA_BITS;
718
719 assert ( dac_streams(i).data_i = expected_sample_i )
720 report "dac_streams("&to_string(i)&").data_i: " & to_hstring(dac_streams(i).data_i) & " | " &
721 "expected_sample_i: " & to_hstring(expected_sample_i)
722 severity warning;
723 assert ( dac_streams(i).data_q = expected_sample_q )
724 report "dac_streams("&to_string(i)&").data_q: " & to_hstring(dac_streams(i).data_q) & " | " &
725 "expected_sample_q: " & to_hstring(expected_sample_q)
726 severity warning;
727
728 if ( dac_streams(i).data_i /= expected_sample_i or dac_streams(i).data_q /= expected_sample_q ) then
729 failure_count := failure_count + 1;
730 if ( failure_count >= 6 ) then
731 report "tx fail count tolerance exceeded" severity failure;
732 end if;
733 end if;
734
735 if( valid_sample_count mod (2*GPIF_BUF_SIZE_SS - 8) = 0 ) then
736 iq_value := x"0000";
737 if( current_message_count < MESSAGES_PER_ITERATION ) then
738 current_message_count := current_message_count + 1;
739 else
740 current_message_count := 1;
741 end if;
742 else
743 iq_value := (iq_value + 2) mod 128;
744 end if;
745 else
746 expected_sample_i := iq_value;
747 expected_sample_q := to_signed(current_message_count, 8) & (iq_value(7 downto 0) + 1);
748
749 assert(dac_streams(i).data_i = expected_sample_i)
750 report "dac_streams("&to_string(i)&").data_i: " & to_hstring(dac_streams(i).data_i) & " | " &
751 "expected_sample_i: " & to_hstring(expected_sample_i)
752 severity failure;
753 assert(dac_streams(i).data_q = expected_sample_q)
754 report "dac_streams("&to_string(i)&").data_q: " & to_hstring(dac_streams(i).data_q) & " | " &
755 "expected_sample_q: " & to_hstring(expected_sample_q)
756 severity failure;
757
758 -- Set iq values based on blocks received
759 -- See tx_sample_stream's encoding scheme in fx3_model.vhd
760 if( valid_sample_count mod (GPIF_BUF_SIZE_SS - HEADER_LEN) = 0 ) then
761 iq_value := x"0000";
762 if( current_message_count < MESSAGES_PER_ITERATION ) then
763 current_message_count := current_message_count + 1;
764 else
765 current_message_count := 1;
766 end if;
767 else
768 iq_value := (iq_value + 2) mod 2048;
769 end if;
770 end if;
771 end if;
772 end loop;
773
774 end process look_for_dropped_tx_samples;
775
776 --
777 -- Rx Sample Check
778 --
779 first_rx_val_at_mux: process
780 -- Catches and saves the first sample of
781 -- each rx enabled burst seen by the mux
782 begin
783 for i in adc_controls'range loop
784 if( adc_controls(i).enable = '1') then
785 if( ENABLE_CHANNEL_0 = '1' and ENABLE_CHANNEL_1 = '1' ) then
786 adc_stream_val_at_rx_enable <= adc_streams(i).data_i - 1;
787 else
788 adc_stream_val_at_rx_enable <= adc_streams(i).data_i;
789 end if;
790 end if;
791 end loop;
792 wait until rising_edge(trigger_signal_sync_tb);
793 end process first_rx_val_at_mux;
794
795 look_for_dropped_rx_samples: process(fx3_pclk_pll)
796 constant HEADER_LEN : integer := 4; -- In clk cycles
797 variable header_downcount : integer := HEADER_LEN;
798 variable rx_val : integer := 0;
799 variable q_expected : std_logic_vector (15 downto 0);
800 variable i_expected : std_logic_vector (15 downto 0);
801 variable q_out : std_logic_vector (15 downto 0);
802 variable i_out : std_logic_vector (15 downto 0);
803
804 variable q0_expected_eight_bit_mode : std_logic_vector (7 downto 0);
805 variable i0_expected_eight_bit_mode : std_logic_vector (7 downto 0);
806 variable q1_expected_eight_bit_mode : std_logic_vector (7 downto 0);
807 variable i1_expected_eight_bit_mode : std_logic_vector (7 downto 0);
808 variable q0_out_eight_bit_mode : std_logic_vector (7 downto 0);
809 variable i0_out_eight_bit_mode : std_logic_vector (7 downto 0);
810 variable q1_out_eight_bit_mode : std_logic_vector (7 downto 0);
811 variable i1_out_eight_bit_mode : std_logic_vector (7 downto 0);
812
813 variable previous_adc_stream_val_at_rx_enable : signed (15 downto 0) := ( others => '0');
814 variable fail_count: natural := 0;
815 variable both_channels_en : std_logic;
816 begin
817 if( previous_adc_stream_val_at_rx_enable /= adc_stream_val_at_rx_enable ) then
818 rx_val := to_integer(adc_stream_val_at_rx_enable (15 downto 4));
819 previous_adc_stream_val_at_rx_enable := adc_stream_val_at_rx_enable;
820 end if;
821
822 if( rising_edge(fx3_pclk_pll)
823 and fx3_gpif.gpif_oe = '1'
824 and fx3_control.rx_enable = '1' ) then
825
826 if( fx3_control.meta_enable = '1' and header_downcount /= 0 ) then
827 header_downcount := header_downcount - 1;
828 else
829 if( EIGHT_BIT_MODE_EN = '1' ) then
830 -- The +-1 ch1 offset in SISO mode will not reach 128
831 both_channels_en := ENABLE_CHANNEL_0 and ENABLE_CHANNEL_1;
832 q1_expected_eight_bit_mode := std_logic_vector(to_signed(-rx_val - 1, 8)) when both_channels_en else
833 std_logic_vector(to_signed(-((rx_val + 1) mod 128), 8));
834 i1_expected_eight_bit_mode := std_logic_vector(to_signed(rx_val + 1, 8)) when both_channels_en else
835 std_logic_vector(to_signed((rx_val + 1) mod 128, 8));
836 q0_expected_eight_bit_mode := std_logic_vector(to_signed(-rx_val, 8));
837 i0_expected_eight_bit_mode := std_logic_vector(to_signed(rx_val, 8));
838
839 q1_out_eight_bit_mode := fx3_gpif.gpif_out(31 downto 24);
840 i1_out_eight_bit_mode := fx3_gpif.gpif_out(23 downto 16);
841 q0_out_eight_bit_mode := fx3_gpif.gpif_out(15 downto 8);
842 i0_out_eight_bit_mode := fx3_gpif.gpif_out(7 downto 0);
843
844 if( q1_out_eight_bit_mode /= q1_expected_eight_bit_mode or
845 i1_out_eight_bit_mode /= i1_expected_eight_bit_mode or
846 q0_out_eight_bit_mode /= q0_expected_eight_bit_mode or
847 i0_out_eight_bit_mode /= i0_expected_eight_bit_mode) then
848 fail_count := fail_count + 1;
849 report "-----------------------------";
850 if( fail_count > 3 ) then
851 report "rx fail count tolerance exceeded" severity failure;
852 end if;
853 end if;
854
855 assert ( q1_out_eight_bit_mode = q1_expected_eight_bit_mode )
856 report "data_q1: " & to_hstring(q1_out_eight_bit_mode) & " | " &
857 "expected: " & to_hstring(q1_expected_eight_bit_mode)
858 severity warning;
859 assert ( i1_out_eight_bit_mode = i1_expected_eight_bit_mode )
860 report "data_i1: " & to_hstring(i1_out_eight_bit_mode) & " | " &
861 "expected: " & to_hstring(i1_expected_eight_bit_mode)
862 severity warning;
863 assert ( q0_out_eight_bit_mode = q0_expected_eight_bit_mode )
864 report "data_q0: " & to_hstring(q0_out_eight_bit_mode) & " | " &
865 "expected: " & to_hstring(q0_expected_eight_bit_mode)
866 severity warning;
867 assert ( i0_out_eight_bit_mode = i0_expected_eight_bit_mode )
868 report "data_i0: " & to_hstring(i0_out_eight_bit_mode) & " | " &
869 "expected: " & to_hstring(i0_expected_eight_bit_mode)
870 severity warning;
871
872 -- 128 limit set by gen_adc_controls above
873 rx_val := (rx_val + 2) mod 128;
874 else
875 q_expected := std_logic_vector(to_signed(-rx_val, 16));
876 i_expected := std_logic_vector(to_signed(rx_val, 16));
877 q_out := fx3_gpif.gpif_out(31 downto 16);
878 i_out := fx3_gpif.gpif_out(15 downto 0);
879
880 assert(q_out = q_expected)
881 report "data_q: " & to_hstring(q_out) & " | " &
882 "expected: " & to_hstring(q_expected)
883 severity failure;
884 assert(i_out = i_expected)
885 report "data_i: " & to_hstring(i_out) & " | " &
886 "expected: " & to_hstring(i_expected)
887 severity failure;
888
889 -- 2047 limit set by gen_adc_controls above
890 rx_val := (rx_val + 1) mod 2048;
891 end if;
892 end if;
893
894 header_downcount := HEADER_LEN;
895 end if;
896 end process look_for_dropped_rx_samples;
897
898 meta_spacing: process
899 procedure wait_for_gpif_ts( signal clk: in std_logic; signal en: in std_logic ) is
900 begin
901 wait until rising_edge(en);
902 wait until rising_edge(clk);
903 wait until rising_edge(clk);
904 end;
905
906 variable ts_increment : integer := 0;
907 variable last_ts : unsigned (31 downto 0) := 32x"BB"; -- First timestamp
908 variable iteration : natural := 1;
909 begin
910
911 wait_for_gpif_ts(fx3_pclk, fx3_gpif.gpif_oe);
912
913 assert fx3_gpif.gpif_out = std_logic_vector(last_ts + ts_increment)
914 report LF&
915 "Unexpected ts: " & to_hstring(fx3_gpif.gpif_out) &LF&
916 "Expected ts: " & to_hstring(last_ts + ts_increment)
917 severity failure;
918
919 last_ts := unsigned(fx3_gpif.gpif_out);
920
921 -- Allows a 0 increment for the first ts assertion
922 if( ENABLE_CHANNEL_0 = '1' and ENABLE_CHANNEL_1 = '1') then
923 ts_increment := (GPIF_BUF_SIZE_SS-4)/2 when EIGHT_BIT_MODE_EN = '0'
924 else (GPIF_BUF_SIZE_SS-4);
925 else
926 ts_increment := (GPIF_BUF_SIZE_SS -4) when EIGHT_BIT_MODE_EN = '0'
927 else 2*(GPIF_BUF_SIZE_SS -4);
928 end if;
929
930 iteration := iteration + 1;
931 if (iteration > 3) then
932 wait_for_gpif_ts(fx3_pclk, fx3_gpif.gpif_oe);
933 last_ts := unsigned(fx3_gpif.gpif_out);
934 iteration := 2;
935 end if;
936
937 end process meta_spacing;
938
939 end architecture;