Search code examples
vhdlrecord

How to access record elements and assign value to them?


I've created a record in VHDL

type direction is record
  left : std_logic;
  right : std_logic;
end record direction;

And I have function that should return this record with assigned values to left and right of either 0 or 1.

That part of function looks like this:

begin

if (x = 0) then
    direction.left <= '0';
else
    direction.left <= '1';
end if;
....

return direction;
end;

but I keep getting error that it cannot be used as a prefix in a selected name. I've never worked with a record inside a function, so I feel like I'm doing something wrong while accessing the elements of record.

Thank you for any help!


Solution

  • What you have defined is a type

    A type is a descriptor to an item, usually a signal or a variable.

    Common types in VHDL are std_logic, std_logic_vector, integer, etc... and you have the right to create your own type. The type direction you describe is OK.

    To use it, you have to declare in your function a variable defined with this very type :

    variable my_direction : direction;
    

    Then you can use the variable my_direction in your function.

    type direction is record
      left : std_logic;
      right : std_logic;
    end record direction;
    
    function my_func (x...) return direction is
      variable my_direction : direction;
    begin
    
    if (x = 0) then
        my_direction.left := '0';
    else
        my_direction.left := '1';
    end if;
    ....
    
    return my_direction;
    end;