多位门

Multiple bits gates

我想知道如何将 AND、OR 和其他门用于多位输入。我正在尝试构建 2:1 MUX,但出现此错误:

Expression width 2 does not match width 1 of logic gate array port 1.

这是代码

module mux_21 #(parameter width = 1) (
    input [width-1:0] d0, d1,
    input s,
    output [width-1:0] y
);
    wire [width-1:0] ns, y1, y2;
    not  g1 (ns, s);
    and  g2 (y1, d0, ns);
    and  g3 (y2, d1, s);
    or   g4 (y, y1, y2);
endmodule

原始门总是单个位。您需要为每个位组合创建实例。

最常见的方法是使用生成 for 循环。生成逻辑在编译的细化阶段通过静态展开。

genvar gidx;
wire ns;
wire [width-1:0] y1, y2;
not  g1 (ns, s);
generate
  for(gidx=0; gidx<width; gidx=gidx+1) begin : mux
    and  g2 (y1[gidx], d0[gidx], ns );
    and  g3 (y2[gidx], d1[gidx], s );
    or   g4 (y[gidx], y1[gidx], y2[gidx] );
  end
endgenerate

您还可以创建一系列实例。这是一个比生成结构存在时间更长但不太常用的功能,我注意到一些合成器不支持它(例如 EDAplayground 上的 Yosys 0.9)。

wire ns;
wire [width-1:0] y1, y2;
not  g1 (ns, s);
and  g2 [width-1:0] (y1, d0, ns);
and  g3 [width-1:0] (y2, d1, s);
or   g4 [width-1:0] (y, y1, y2);