如何在 VHDL 93 中制作完全通用的 MUX

how to make a fully generic MUX in VHDL 93

我想用两个泛型合成一个通用 MUX:

根据这些非常相似的问题,这似乎相当容易: and 。问题是:我必须使用 vhdl 93,并且两个答案都假设 vhdl 2008 and/or 只有一个泛型是给定的。

entity Mux is
    generic (
        addressSize : integer := 2;
        wordSize    : integer := 32
    );
    port (
        …
    );
end entity;

architecture RTL of Mux is
begin
…
end architecture;

我卡住了,确实,在 vhdl 93 中,我可能无法在数组中使用不受约束的 std_logic_vector:

package common is
    type WORD_ARRAY_type is array (integer range <>) of std_logic_vector;
end package;

这会让我同时使用这两种泛型:

port(
   input : WORD_ARRAY_type(0 to addressSize)(wordSize - 1 downto 0);
   …
);

当然,我可能不会在泛型和端口之间定义类型。问题是,我肯定不是第一个遇到这个特定问题的人,所以我徘徊:“如何在 VHDL 93 中完成这个非常经典的功能?”

我明白您的想法,但是,对于像这样的小型组合逻辑块,我会改用一个函数,然后使用重载为您需要的每个 Mux 大小定义一个 Mux 函数:

function Mux (
    sel  : std_logic ;
    A1   : std_logic_vector ;
    A2   : std_logic_vector 
) return std_logic_vector is 
  variable Y : std_logic_vector(A1'range) ; 
begin
  case sel is 
    when '0' => Y := A1 ; 
    when '1' => Y := A2 ;
    when others => Y := (others => 'X') ; 
  end case ; 
  return Y ; 
end function Mux ; 

是的,开发包是蛮力的,但是一旦完成,使用起来非常简单。引用包并进行函数调用。由于它是一个函数,您可以将输出链接在一起。

MuxOut <= Mux(Sel, A, B) and Mux(Sel, C, D) ; 

如果您想在 IEEE 标准中看到这样的内容(这是个好主意),请联系我,我可以帮助您开始参与。

在上面,我通过使用变量强制大小相同。 VHDL-2019 给了我们更好的选择。事实上,它为我们提供了允许 A1, ... 的类型比 std_logic_vector.

更灵活的方法

添加 @Tricky 方法,但将其应用于子程序:

function Mux (
    Width : integer ; 
    sel   : integer ;
    A     : std_logic_vector 
) return std_logic_vector is 
  alias normalizedA : std_logic_vector(0 to A'length-1) is A ; 
  constant WordStart : integer := width * sel ; 
begin
  return normalizedA(WordStart to WordStart + Width - 1) ; 
end function Mux ;