【问题标题】:Why is iverilog complaining about this expression/port width?为什么iverilog 抱怨这个表达式/端口宽度?
【发布时间】:2019-03-05 05:18:33
【问题描述】:

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

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

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

这里是我的 .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

【问题讨论】:

    标签: verilog iverilog


    【解决方案1】:

    问题出在这里:

    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];
    

    【讨论】:

    • 我正在为一个作业写结构,所以我坚持使用 and 版本。我必须为a和b都这样做吗?所以要效仿你的例子,and_a_i & and_b_i?
    • 自己找到了答案。是的,我愿意。感谢您的帮助!
    • 另一种选择是排列实例。例如:and #4 and1[4:0](a, {5{gate}}, in1);
    猜你喜欢
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2015-06-09
    • 2020-08-12
    相关资源
    最近更新 更多