Email: bknpk@hotmail.com Phone: +972-54-7649119


V

 

ASIC/FPGA Design and Verification Out Source Services

A VHDL example that reads data from file and stores it in a list.



  1. The purpose of this code is to read data from a file, parse it, store the data in a list at simulation time 0. Later on the data is retrieved in a FIFO style.

  2. The read is done based on Very simple, VHDL standalone bench, to demonstrate text file read and lists in VHDL.

  3. The requirements are to read lines of hex data bytes separated by spaces and store it in a list. Drive the data to DUT. This is done in a single VHDL process.
    The input file is of the following format:

    ff ff ff 22 22 22 00 0c 29 b4 41 b0 08 00 45 00
    00 23 00 00 40 00 40 11 75 68 c0 a8 00 b4 01 02
    03 04 00 00 00 35 00 0f d2 3b 55 44 50 44 61 74

    The generation of the network stimuli data is explained at:
    multi protocol packets cpp packet generator.
    and simple TCP cpp packet generator.

  4. The read and drive process runs continuously. There is no wait statement, once the file content is read to the list. Instead a boolean is used to instruct the process to wait for clock once read file into a list ends.

  5. For more information, please send me a mail and put in the subject: read and drive vhdl list.

    1. library IEEE;
    2. use IEEE.STD_LOGIC_1164.ALL;
    3. use IEEE.NUMERIC_STD.ALL;
    4. use work.axi.all;
    5. use work.ipv4_types.all;
    6. use work.arp_types.all;
    7. use STD.textio.all;
    8. use IEEE.std_logic_textio.all;
    9. ENTITY UDP_complete_nomac_tb IS
    10. END UDP_complete_nomac_tb;
    11. ARCHITECTURE behavior OF UDP_complete_nomac_tb IS
    12. type fifo_item; -- the item
    13. type fifo_item_ptr is access fifo_item; -- pointer to item
    14. type fifo_item is record -- full definition of item
    15. data : std_logic_vector(7 downto 0);
    16. indx : integer;
    17. next_rec : fifo_item_ptr;
    18. end record;
    19. ...
    20. BEGIN
    21. --read a packet stimuli file
    22. process
    23. file fp : text;
    24. variable new_pkt : fifo_item_ptr;
    25. variable tmp_ptr : fifo_item_ptr;
    26. variable fifo_ptr: fifo_item_ptr := null;
    27. variable fifo_first : boolean;
    28. variable read_first : boolean := false;
    29. variable fifo_add : boolean;
    30. variable pkt_cnt : integer;
    31. variable file_read : boolean := false;
    32. variable line_content : string(1 to 47);
    33. variable line_num : line;
    34. variable j : integer := 0;
    35. variable p : integer := 1;
    36. variable d : std_logic_vector(7 downto 0);-- data
    37. variable char : character:='0';
    38. --debug
    39. variable my_line : line;
    40. variable my_cnt : integer;
    41. begin
    42.   if(file_read) then
    43.     wait until clk_int'event and clk_int = '1';
    44.     if(not read_first) then
    45.       tmp_ptr := fifo_ptr;
    46.       read_first := true;
    47.     end if;
    48.     if(fifo_read = '1') then
    49.       if(tmp_ptr.next_rec /= null) then
    50.         fifo_data <= tmp_ptr.data;
    51.         
    52.         --write(my_line, string'("dbg "));
    53.         --hwrite(my_line, tmp_ptr.data);
    54.         --write(my_line, string'(" "));
    55.         --write(my_line, now);
    56.         --writeline(output, my_line);
    57.         
    58.         tmp_ptr := tmp_ptr.next_rec;
    59.       else
    60.         fifo_data <= (others => 'X');
    61.       end if;
    62.     end if;
    63.   else
    64.     fifo_first := true;
    65.     pkt_cnt := 0;
    66.     my_cnt := 0;
    67.     file_open(fp,"vhdl_stim.txt", READ_MODE);
    68.     while not endfile(fp) loop
    69.       readline (fp, line_num);
    70.       READ (line_num, line_content);
    71.       --debug string length
    72.       --write(my_line, string'("dbg "));
    73.       --write(my_line, line_content'length);
    74.       --writeline(output, my_line);
    75.       for j in 1 to 47 loop
    76.         char := line_content(j);
    77.         
    78.         --write(my_line, string'("dbg "));
    79.         --write(my_line, my_cnt);
    80.         --write(my_line, string'(" "));
    81.         --write(my_line, char);
    82.         --writeline(output, my_line);
    83.         --my_cnt := my_cnt + 1;
    84.         
    85.         fifo_add := false;
    86.         case char is
    87.           when '0' =>
    88.           if(p = 1) then d := "00000000"; p := 16;
    89.             else d := d or "00000000"; p := 1; fifo_add := true;
    90.           end if;
    91.           when '1' =>
    92.           if(p = 1) then d := "00010000"; p := 16;
    93.             else d := d or "00000001"; p := 1; fifo_add := true;
    94.           end if;
    95. ...
    96.           when 'e' | 'E' =>
    97.           if(p = 1) then d := "11100000"; p := 16;
    98.             else d := d or "00001110"; p := 1; fifo_add := true;
    99.           end if;
    100.           when 'f' | 'F' =>
    101.           if(p = 1) then d := "11110000"; p := 16;
    102.             else d := d or "00001111"; p := 1; fifo_add := true;
    103.           end if;
    104.           when others =>
    105.           fifo_add := false;
    106.         end case;
    107.         if(fifo_add) then
    108.           new_pkt := new fifo_item;
    109.           new_pkt.indx := pkt_cnt;
    110.           new_pkt.data := d;
    111.           new_pkt.next_rec := null;
    112.           if(fifo_first) then
    113.             fifo_first := false;
    114.             fifo_ptr := new_pkt;
    115.           else
    116.             tmp_ptr := fifo_ptr;
    117.             while(tmp_ptr.next_rec /= null) loop --find last
    118.               tmp_ptr := tmp_ptr.next_rec;
    119.             end loop; --while(tmp_ptr.next_rec /= null)
    120.             tmp_ptr.next_rec := new_pkt;
    121.           end if;
    122.           --write(my_line, string'("dbg "));
    123.           --hwrite(my_line, new_pkt.data);
    124.           --writeline(output, my_line);
    125.         else
    126.           if(char = 'p') then
    127.             pkt_cnt := pkt_cnt + 1;
    128.           end if;
    129.         end if;
    130.       end loop; --for j in 1 to 47
    131.     end loop; --while not endfile(fp)
    132.     file_close(fp); --after reading all the lines close the file.
    133.     file_read := true;
    134.   end if;
    135.   
    136.   --stop the process from re-read
    137.   --wait;
    138. end process;
    139. --debug
    140. p_1 : process(clk_int)
    141. variable cnt : integer := 0;
    142. begin
    143.   if clk_int'event and clk_int = '1' then
    144.     cnt := cnt + 1;
    145.     if(cnt = 15) then fifo_read <= '1'; end if;
    146.     if(cnt = 16) then cnt := 1; fifo_read <= '0'; end if;
    147.   end if;
    148. end process;


Search This Site


Feedback This Site




new pages on this site