Advertisement
rommik

testbench.vhd

Dec 14th, 2019
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 5.95 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.std_logic_1164.all;
  3. use IEEE.numeric_std.ALL;
  4. use STD.textio.all;
  5. use IEEE.std_logic_textio.ALL;
  6.  
  7.  
  8. entity testbench is
  9. end entity;
  10.  
  11. architecture test of testbench is
  12.  
  13.     signal EndOfSim       : boolean := false;
  14.     constant half_clk     : time    := 10 ps;
  15.    
  16.     type frame_size_array is array (0 to 1) of integer;
  17.     constant frame_size    : frame_size_array := (0 => 64800, 1 => 16200);
  18.     constant address_space : frame_size_array := (0 => 64800, 1 => 16200);
  19.     constant input_size    : integer          := 1;
  20.  
  21.     signal i_clk           : std_logic := '0';
  22.     signal i_enb           : std_logic := '0';
  23.     signal i_res           : std_logic := '0';
  24.     signal i_frame_select  : std_logic_vector (0 downto 0);
  25.     signal i_QAM_select    : std_logic_vector (2 downto 0);
  26.     signal i_data          : std_logic_vector (input_size - 1 downto 0);
  27.     signal o_data          : std_logic_vector (input_size - 1 downto 0);
  28.     signal expected        : std_logic_vector (input_size - 1 downto 0);
  29.     signal start_r         : std_logic;
  30.     signal correct         : std_logic;
  31.     file testing_data      : text;
  32.  
  33.     component clk_dvb_s2x
  34.         Port(
  35.             i_clk           : in  std_logic;                              
  36.             i_res           : in  std_logic;                              
  37.             i_enb           : in  std_logic;                              
  38.             i_QAM_select    : in  std_logic_vector (2 downto 0);          
  39.             i_frame_select  : in  std_logic_vector (0 downto 0);          
  40.             i_data          : in  std_logic_vector (0 downto 0);                              
  41.             o_data          : out std_logic_vector (0 downto 0));
  42.     end component;
  43.    
  44. begin
  45.     dut: clk_dvb_s2x
  46.     port map(
  47.         i_clk          => i_clk,
  48.         i_enb          => i_enb,
  49.         i_res          => i_res,
  50.         i_data         => i_data,
  51.         o_data         => o_data,
  52.         i_frame_select => i_frame_select,
  53.         i_QAM_select   => i_QAM_select);
  54.        
  55. clock: process is
  56.     begin               -- clock generator, toggle clk every half periode
  57.         if EndOfSim then
  58.         wait;
  59.         end if;
  60.         i_clk <= not i_clk;
  61.         wait for half_clk;
  62. end process;
  63.  
  64.     main: process is
  65.         variable read_line          : line;
  66.         variable frame_count        : integer;
  67.         variable M_frame_select     : std_logic_vector (0 downto 0);
  68.         variable int_frame_select   : integer;
  69.         variable M_APSK_SELECT      : std_logic_vector (2 downto 0);
  70.         variable M_INPUT            : std_logic_vector (0 to frame_size(0) - 1);
  71.         variable M_OUTPUT           : std_logic_vector (0 to frame_size(0) - 1);
  72.         variable frame_counter      : integer;
  73.         variable input_counter      : integer;
  74.         variable input              : std_logic_vector (input_size - 1 downto 0);
  75.         variable output             : std_logic_vector (input_size - 1 downto 0);
  76.         variable lower_bound        : integer;
  77.         variable upper_bound        : integer;
  78.     begin
  79.         file_open(testing_data, "Test_input_interleaver.txt", read_mode);
  80.         readline(testing_data, read_line);
  81.         read(read_line, frame_count);
  82.         read(read_line, M_frame_select);
  83.         int_frame_select := to_integer(unsigned(M_frame_select));
  84.         report "frame_count = " & integer'image(frame_count);
  85.         wait until falling_edge(i_clk);
  86.         i_enb          <= '0';
  87.         i_res          <= '1';
  88.         i_frame_select <= M_frame_select;
  89.         wait until rising_edge(i_clk);
  90.         wait until falling_edge(i_clk);
  91.         i_enb         <= '1';
  92.         i_res         <= '0';
  93.         frame_counter := 0;
  94.         -- FIRST LOOP THAT WRITES WORDS
  95.         while frame_counter < frame_count + 1 loop
  96.             if (frame_counter < frame_count) then
  97.                 readline(testing_data, read_line);
  98.                 read(read_line, M_APSK_SELECT);
  99.                 read(read_line, M_INPUT(0 to frame_size(int_frame_select)-1));
  100.                 report "Frame: " & integer'image(frame_counter) & " M_APSK_SELECT = " & integer'image(to_integer(unsigned(M_APSK_SELECT)));
  101.             else end if;
  102.             -- SECOND LOOP THAT WRITES WORDS
  103.             input_counter := 0;
  104.             while input_counter < (address_space(int_frame_select)) loop
  105.                 lower_bound := input_size * input_counter;
  106.                 upper_bound := input_size * (input_counter + 1) - 1;
  107.                 -- WRITE INPUT(DON'T WRITE AT THE END)
  108.                 if(frame_counter < frame_count) then
  109.                     i_QAM_select <= M_APSK_SELECT;
  110.                     i_data       <= M_INPUT(lower_bound to upper_bound);
  111.                 else
  112.                     i_data       <= (others => 'U');
  113.                 end if;
  114.                 wait until rising_edge(i_clk);
  115.                 -- WRITE EXPECTED OUTPUT(DON'T WRITE AT THE BEGINNING)
  116.                 if (frame_counter > 0) then
  117.                     expected <= M_OUTPUT(lower_bound to upper_bound);
  118.                 else end if;
  119.                 wait until falling_edge(i_clk);
  120.                 input_counter := input_counter + 1;
  121.             end loop;
  122.             if (frame_counter < frame_count) then
  123.                 read (read_line, M_OUTPUT(0 to frame_size(int_frame_select)-1));
  124.             else end if;
  125.             start_r       <= '1';
  126.             frame_counter := frame_counter + 1;
  127.         end loop;
  128.         file_close(testing_data);
  129.         start_r  <= '0';
  130.         i_enb    <= '0';
  131.         wait for 10*half_clk;
  132.         EndOfSim <= true;
  133.         report "Simulation finished succcesfully" severity FAILURE;
  134.         wait;
  135.     end process;
  136.  
  137.     testing: process is
  138.     begin  
  139.         wait until falling_edge(i_clk);
  140.         if(start_r = '1') then
  141.             if(expected = o_data) then
  142.                 correct <= '1';
  143.             else
  144.                 report "ERROR: incorrect";
  145.                 wait for 4*half_clk;
  146.                 report "ERROR: exiting" severity FAILURE;
  147.                 correct <= '0';
  148.             end if;
  149.         end if;
  150.     end process;
  151. end architecture;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement