VHDL coding tips and tricks: VHDL: Arrays and Records

Thursday, February 25, 2010

VHDL: Arrays and Records

In many situations you may have to use a 2-D array in your design. A 2-D array can be declared in two ways in VHDL. Let me show some examples:

1)Using the keyword "array".  

--first example
type array_type1 is array (0 to 3) of integer; --first define the type of array.
signal array_name1 : array_type1;  --array_name1 is a 4 element array of integers.

--second example
--first define the type of array.
type array_type2 is array (0 to 3) of std_logic_vector(11 downto 0); 
--array_name2 is a 4 element array of 12-bit vectors.
signal array_name2 : array_type2; 

2)Array of different kind of elements.

        Using array ,you can easily create an  array of similar types. But what will you do if you want an array of different type of elements, like the structures in C programming. For handling such data types there is another keyword available in VHDL - record.

--third example
type record_name is
  record
     a : std_logic_vector(11 downto 0);
     b: std_logic_vector(2 downto 0);
     c : std_logic;
  end record;
type array_type3 is array (0 to 3) of record_name; --first define the type of array.
signal actual_name : array_type3;

After going through the above examples you must have got an idea about array and record declarations. Now we will see how to read or write these new types.

If the array name is "var_name" then the individual elements can be accessed by the following notation : var_name(0),var_name(1) etc....

Keep in mind the types declared above to understand the following examples.

signal test1 : std_logic_vector(11 downto 0);  --12 bit vector.
test1 <= array_name2(0);

signal test2 : integer;
test2 <= array_name1(2);

--accessing the record.
a1 : std_logic_vector(11 downto 0);
b1: std_logic_vector(2 downto 0);
c1 : std_logic;

--reading from a record.
a1 <= actual_name(1).a;
b1 <= actual_name(1).b;
c1 <= actual_name(1).c;

--writing to a record
actual_name(2).a <= "100011100011";
actual_name(1) <= (a =>  "100011100011", b => "101", c => '1');
actual_name(0) <= ("100011100011","101",'1');

Initialization:

Sometimes you may need to initialize a large array with zeros. If the array is very large then it is tedious to initialize it using the above methods. A keyword called others is used in such cases.

--an example illustrating the usage of "others".
signal test4 : std_logic_vector(127 downto 0) :=  (others =>'0');

--test5 ="00000010";
signal test5 : std_logic_vector(7 downto 0) := (1 =>'1', (others =>'0') );

--initializing a 4 element array of 12 bit elements to zero.
array_name2 <= (others=> (others=>'0'));

--Example for 2-d array declaration
type array_type2 is array (0 to 3) of std_logic_vector(11 downto 0);
type array_type4 is array (0 to 2) of array_type2;
signal array_name4 : array_type4;  --array_name4 is a 3*4 two dimensional array.

--initialization to zeros.
array_name4<=((others=>(others=>'0')),(others=>(others=>'0')),(others=>(others=>'0')));


I have written a new post on Arrays and Records here. I have answered several questions received through comments in this post. Hopefully it will let you understand this topic a bit more.

26 comments:

  1. hi how to initialize an integer array to zero.

    ReplyDelete
  2. type example is array (0 to 2) of integer;
    signal xx : example :=(0,0);
    (OR)
    xx <= (others => 0);

    ReplyDelete
    Replies
    1. This comment has been removed by a blog administrator.

      Delete
  3. This is great. Thanks man. I appreciate it. arrays in vhdl are so confusing . this really helps!!!!

    ReplyDelete
  4. you can also use "array (natural range <>) of" to allow the user to specify a size. I suggest looking at XST's style guide for more examples.

    ReplyDelete
  5. thank u so much for the explanations with example... really useful!!

    ReplyDelete
  6. To initialise the whole array of record to zero is there any easy statement like "others"? else should it be initialized individually?

    ReplyDelete
  7. why did you use 2 others to initialize a 4 element array of 12 bit elements to zero.
    array_name2 <= (others=> (others=>'0'));

    why not array_name2 <= (others=> '0');

    ReplyDelete
  8. @aze: i used two "others" because its a 2 dimensional array.

    ReplyDelete
  9. Hi,

    I dont know how to find the number of rows and colums of a 2d array.

    For example:
    type my_2d is array(4 downto 0,6 downto 0)std_logic_vector(7 downto 0);

    variable eg_2d : my_2d;

    is there a way to find dynamically the number of rows and colums..
    row := eg_2d'ROW..

    Plz suggest.

    ReplyDelete
  10. Hi,

    Could we define a type using some values of the generic? Could we use this type in the ports of the same/other block? Any way around for the second question?

    EXAMPLE (just to show the idea):
    entity stream_TEST is
    generic(
    NBR_STREAM: integer := 16;
    NBIT_STREAM : integer := 10
    );
    port(
    stream1 : in STREAM;
    stream2 : out STREAM
    );

    ARCHITECTURE yy OF zz IS
    Type STREAM is array(NBR_STREAM downto 0) of std_logic_vector(NBIT_STREAM downto 0);
    BEGING

    END ARCHITECTURE;

    ReplyDelete
  11. how to declare and initialize an array of 10 elements each of 32bits in size in VHDL...

    ReplyDelete
  12. @harish

    type type_name is array(9 downto 0) of std_logic_vector(31 downto 0);
    signal sig_name : type_name;

    or

    type WORD is array(31 downto 0) of std_logic;
    type type_name is array(9 downto 0) of WORD;
    signal sig_name :type_name;

    ReplyDelete
  13. can we initialize the array with the elements declared in the entity like, in my case i hv declared elements (arr0,arr1,arr2,arr3,arr4,arr5,arr6,arr7,arr8,arr9) with bit_vector(31 downto 0) in entity..
    ex:-

    type type_name is array(9 downto 0) of std_logic_vector(31 downto 0);
    signal sig_name : type_name:=(arr0,arr1,arr2,arr3,arr4,arr5,arr6,arr7,arr8,arr9);

    ReplyDelete
    Replies
    1. hi i search a solution for this problem,
      I try to connect my entries to the table so that its content varies depending on the input.
      DO you found the solution

      Delete
  14. Why not just use:

    Architecture behavior of testbench is
    signal INPUT : std_logic_vector (19 downto 0) := "00000000000000000000";

    ?

    ReplyDelete
  15. Excuse me,

    The arrays on VHDL can has dimensions beyond 2D?
    For example, I can define a new tipe with a 5D array.

    Thanks a lot.

    ReplyDelete
  16. Hi

    how to read values from an array and send them to a given port when the clock is toggling.

    please reply.

    Thank u

    ReplyDelete
  17. how to make the sum of each column of a table of dimension 2, and put the money into a 1D array ???

    DO you found the solution?

    ReplyDelete
  18. very nice topic to catch up, hey guys can any one tell me how to use an array with the dimensions of n rows and m columns ,, and tell me how and which statement can tell me i am designing the row vector or the column vector? prefer touse the bit_vector type :)

    ReplyDelete
  19. i am define a std logic vector in a function and wand to use that function in other function, but that function has some simple value of std logic.so it gives error in this --ad2 : bit_4adder port map('0','0',w(1 downto 0),s(3 downto 0),t(1 downto 0),p(3 downto 2),c(1)); The error is-- Character '0' is not in type std_logic_vector
    && Indexed name is not a std_logic
    && bit_4adder has only 4 ports
    so plese tell me how to define it ????

    ReplyDelete
    Replies
    1. I dont understand your doubt fully. But check if this is what you are asking about:

      signal test : std_logic_vector(0 downto 0) := "0"';

      Delete
  20. Hi, is it possible to create large arrray of unsigned in separate file, I can store 32 values directly in my main file but if I have 256 values it is not convenient:-(

    ReplyDelete
    Replies
    1. I would use a vhdl package in that case. see this link:
      http://vhdlguru.blogspot.in/2010/03/usage-of-packages-and-functions.html

      Delete
  21. Your example where you got
    actual_name(2).a <= "100011100011";

    How do I assign the 8 bits of it to test5?
    test5<=actual_name(2).a(10 downto 3);
    so that test5 is 00011100
    doesn't work

    ReplyDelete
  22. Hi,

    I'm creating an array of a 2D ROM array.
    What's the correct vhdl coding technique to initialize it without reading from a file?
    I want it to be synthesizeable.

    type rom_block is array (0 to 1023) of std_logic_vector(31 downto 0);
    type rom_type is array (0 to 3) of rom_block;

    constant rom : rom_type := ("11001000000110100010100001010001111111100100001100100110011100111000111111101111011110001001010010101101010101100000010011001100","11001000000110100010100001010001111111100100001100100110011100111000111111101111011110001001010010101101010101100000010011001100","11001000000110100010100001010001111111100100001100100110011100111000111111101111011110001001010010101101010101100000010011001100","11001000000110100010100001010001111111100100001100100110011100111000111111101111011110001001010010101101010101100000010011001100");


    But I'm getting this error from synthesis tool:
    Error : Type mismatch. [VHDLPT-761] [read_hdl]
    : In array aggregate element; type must be rom_block

    What's the proper vhdl coding construct for an array of 2D array (in case for a ROM)?






    ReplyDelete