VHDL 函数中原始向量的索引

Indexing of original vector in a function in VHDL

我想在 VHDL 中编写一个函数,该函数被赋予 std_logic_vector 的前几位并对它们进行处理,但似乎我的函数的索引仍然从底部开始计数整个向量。

我可以通过先将我的矢量分配给一个临时信号并使用它来解决这个问题,但我担心我不明白这里发生了什么。

有人可以解释为什么 ab 在下面没有得到相同的输出吗?

architecture rtl of inds is
  function top_bit (c : std_logic_vector) return std_logic is
  begin
    return c(c'length-1);
  end top_bit;
  signal temp : std_logic_vector(2 downto 0);
begin
  temp <= input(3 downto 1);
  a <= top_bit(temp);
  b <= top_bit(input(3 downto 1));
end rtl;

如果你给他们输入 "0100",你会得到 a='0', b='1'

如果你给他们输入 "1000",你会得到 a='1', b='0'

所以 a=temp(2)=input(3)b=input(2)input("length of c" -1).

我不认为这是有道理的,谁能为我证明一下。

编辑: 如果将声明行替换为:

function top_bit (c : std_logic_vector(2 downto 0)) return std_logic is

然后它就如我所料的那样工作了。 我想向量 c 从给定的向量中获取它的索引。

我希望看到一个函数,它采用向量的任意切片和 returns 该切片的最高位。

您正在使用 'length 属性,而您本可以使用 'high。我认为这会满足您的要求。

我在 table 的墙上打印了一张 http://www.csee.umbc.edu/portal/help/VHDL/attribute.html 作为可用属性的参考。

问题是 c'length returns 向量的长度不一定是有效索引。例如,假设我声明了以下信号:

signal temp : std_logic_vector(7 downto 4);

这会导致调用 top_bit 的范围错误。正如您在对 scary_jeff 的回答的评论中指出的那样,并非所有向量都是 x downto 0。他们可能是 x downto y。或者他们甚至可以 0 to xx to y。假设 c'length-1 是最高位仅当 c 声明为 std_logic_vector(N-1 downto 0)(您在答案中发现)时才成立。

作为澄清。 scary_jeff的回答是正确的方法。但是,您需要解决"top_bit" 的含义。如果给你一个 to 向量怎么办,例如:

signal temp : std_logic_vector(4 to 7)

什么是最高位?第 4 位还是第 7 位?如果你使用 'high,你会得到第 7 位。这是最高位吗?如果你想让第 4 位成为最高位,你需要使用 'low.