和VHDL中的n位数组的所有元素

5 vhdl digital-design

假设我有一个n位数组.我想要和数组中的所有元素.类似于将每个元件连接到n位AND门.

我如何在VHDL中实现这一目标?

注意:我正在尝试使用可重用的VHDL代码,所以我想避免硬编码

    result <= array(0) and array(1) and array(2)....and array(n); 
Run Code Online (Sandbox Code Playgroud)

谢谢Oshara

小智 9

解决方案1:使用一元运算符

VHDL-2008定义了一元运算符,如下所示:

outp <= and "11011";
outp <= xor "11011";
outp <= and inp; --this would be your case
Run Code Online (Sandbox Code Playgroud)

但是,编译器可能尚不支持它们.

解决方案2:使用纯组合(和传统)代码

因为在并发代码中,您不能多次为信号赋值,您可以创建具有"额外"维度的临时信号.在您的情况下,输出是一位,因此临时信号应该是一维数组,如下所示.

-------------------------------------------
entity unary_AND IS
    generic (N: positive := 8); --array size
    port (
        inp: in bit_vector(N-1 downto 0);
        outp: out bit);
end entity;
-------------------------------------------
architecture unary_AND of unary_AND is
    signal temp: bit_vector(N-1 downto 0);
begin
    temp(0) <= inp(0);
    gen: for i in 1 to N-1 generate
        temp(i) <= temp(i-1) and inp(i);
    end generate; 
    outp <= temp(N-1); 
end architecture;
-------------------------------------------
Run Code Online (Sandbox Code Playgroud)

推断电路如下图所示. 在此输入图像描述

解决方案3:使用顺序代码

这比解决方案2简单,尽管您现在使用顺序代码来解决纯粹的组合问题(但硬件将是相同的).您可以编写类似于解决方案2中的代码,但使用流程循环(后者代替生成)或使用函数.因为在顺序代码中允许多次为信号赋值,所以此处不需要解2的临时信号.


Mor*_*mer 9

如果你有VHDL-2008可用,那么and就像David Koontz和Pedroni所解释的那样,缩小是建立在语言之上的.

如果您只有VHDL-2003和之前的可用,那么您可以使用如下函数:

function and_reduct(slv : in std_logic_vector) return std_logic is
  variable res_v : std_logic := '1';  -- Null slv vector will also return '1'
begin
  for i in slv'range loop
    res_v := res_v and slv(i);
  end loop;
  return res_v;
end function;
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用内部和外部函数的函数:

signal arg : std_logic_vector(7 downto 0);
signal res : std_logic;
...
res <= and_reduct(arg);
Run Code Online (Sandbox Code Playgroud)


Jim*_*wis 5

我最喜欢的非 VHDL-2008 解决方案是:

use ieee.std_logic_unsigned.all ;  -- assuming not VHDL-2008
. . . 
result <= '1' when not MyArray = 0 else '0' ; 
Run Code Online (Sandbox Code Playgroud)

对于VHDL-2008,我建议您使用内置的“and”缩减(参见Pedroni 的帖子)并使用IEEE 标准包“ieee.numeric_std_unsigned.all”而不是共享软件包“std_logic_unsigned”。