如何访问记录元素并为其赋值?

How to access record elements and assign value to them?

我已经在 VHDL 中创建了一条记录

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

而且我有一个函数应该 return 这条记录的左右赋值分别为 0 或 1。

那部分函数看起来像这样:

begin

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

return direction;
end;

但我不断收到错误消息,提示它不能用作所选名称的前缀。我从来没有在函数中使用过记录,所以我觉得在访问记录的元素时我做错了什么。

感谢您的帮助!

你定义的是一个type

类型是项目的描述符,通常是 signalvariable

VHDL中常见的类型有std_logicstd_logic_vectorinteger等...您有权创建自己的类型。您描述的类型方向没问题。

要使用它,您必须在函数中声明一个使用此类型定义的变量:

variable my_direction : direction;

然后你可以在你的函数中使用变量my_direction

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;