iverilog为什么抱怨此表达式/端口宽度?

时间:2019-03-05 05:18:33

标签: verilog iverilog

当我尝试使用STRUCTURAL代码制作5位2x1 MUX时,出现了一个令人困惑的Verilog错误,我似乎找不到任何有关为什么我的代码显示错误的信息。错误是:

error: Expression width 5 does not match width 1 of logic gate array port 1.

我知道它正在谈论我的输入之一,但是我不知道哪个输入在端口1中。但是基于其余代码的布局方式,我很确定所有5位宽输入与5位导线和5位输出相匹配。任何帮助将不胜感激!

作为参考,这里是我的.v和tb.v

.v: {module mux_2x1_5bit(in1,in2,gate,out);

input [4:0] in1, in2;
input gate;
output [4:0] out;

wire [4:0] a, b;

and #4 and1(a, {5{gate}}, in1);
and #5 and2(b, {5{~gate}}, in2);
or #4 or1(out, a, b);

endmodule

tb.v:

module mux_2x1_5bitTEST();

wire [4:0] out;
reg [4:0] in1;
reg [4:0] in2;
reg gate;

mux_2x1_5bit DUT(in1, in2, gate, out);

initial
begin
    in1 = 5'b00000;
    in2 = 5'b00010;
    gate = 0;

    #20 in1 = 5'b00001;
    #20 gate = 1;
    #20 in2 = 5'b10101;
    #20 in1 = 5'b01101;
    #20 gate = 0;

end

always @(in1 or in2 or gate)
    #1 $display("| gate = %b | in1 = %b | in2 = %b | out = %b |",  gate,   in1,   in2,   out);

endmodule

1 个答案:

答案 0 :(得分:0)

问题出在这里:

and #4 and1(a, {5{gate}}, in1);
and #5 and2(a, {5{~gate}}, in2);
or #4 or1(out, a, b);

and门的输入和输出只有宽度为1,但是在这里您的输入为5。要解决这个问题,您可以这样操作:

assign a = {5{gate}} & in1;
assign b = {5{~gate}} & in2;
assign out = a | b;

或者,如果您想像这样使用and,则可以将输入分隔1个宽度:

and and_a_1(a[0],gate,in2[0]);
and and_a_2(a[1],gate,in2[1]);
...
and and_a_i(a[i],gate,in2[i];