首页 文章

在VHDL中对函数中的原始向量进行索引

提问于
浏览
0

我想在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) .

我不认为这是有道理的,有人可以为我证明这一点 .

Edit: 如果用以下内容替换声明行:

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

然后它按照我的预期工作 . 我想向量 c 给它's indexing from the vector it' s .

我想看一个函数,它接受一个向量的任意切片并返回该切片的最高位 .

2 回答

  • 2

    您正在使用 'length 属性,您可以使用 'high . 我想这会做你想要的 .

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

  • 1

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

    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 ,则'll get bit 7. Is this the top bit? If you want bit 4 to be the top bit, you'll需要使用 'low .

相关问题