Search code examples
vhdl

In VHDL, what does an unconstrained array's index range default to when passed as an argument to a function/procedure?


I'm using a function which returns a user-defined unconstrained array:

type t_array is array(integer range <>) of std_logic_vector(7 downto 0);

This array is then passed to a procedure. Nowhere in my procedure or function have I explicitly defined the range of the array.

What would the array's index range default to when it's passed as an argument?


Solution

  • It will default to whatever you associate with the argument. So, in this example:

    library IEEE;
    use IEEE.std_logic_1164.all;
    
    entity E is
    end entity ;
    
    architecture A of E is
      type t_array is array(integer range <>) of std_logic_vector(7 downto 0);
      procedure P (constant I : in t_array) is
      begin
        report integer'image(I'left);
        report integer'image(I'right);
      end procedure;
    begin
      process
        variable V : t_array(0 to 9);
      begin
        P(V);
        wait;
      end process;
    end architecture A;
    

    This will be displayed:

    # EXECUTION:: NOTE   : 0
    # EXECUTION:: Time: 0 ps,  Iteration: 0,  Instance: /E,  Process: line__19.
    # EXECUTION:: NOTE   : 9
    # EXECUTION:: Time: 0 ps,  Iteration: 0,  Instance: /E,  Process: line__19.
    

    https://www.edaplayground.com/x/2zNy


    Given that you don't know what the range of the input will be, rather than peppering your code with 'left and 'right attributes and getting in a complete pickle with to and downto, it sometimes helps to normalise the input, eg:

      procedure P (constant I : in t_array) is
        variable normalised_I : t_array(1 to I'length) := I;
      begin
        ...
      end procedure;