【verilog】【Modelsim仿真】“XXX“already declared in this scope

问题:

作业要求写一个求3个n比特数的中间数的verilog代码,写完在modelsim仿真中遇到了如下问题:

【verilog】【Modelsim仿真】“XXX“already declared in this scope_第1张图片

代码段如下:

module mid #(parameter N = 4) (
    input [N - 1:0] a,
    input [N - 1:0] b,
    input [N - 1:0] c,
    output[N - 1:0] out
);
    wire [N - 1:0] l1, l2, l3, g1;
   
    MagCompL l1(a, b, l1);
    MagCompL l2(b, c, l2);
    MagCompL l3(a, c, l3);
    MagCompG g1(l1, l2, g1);
    MagCompG g2(g1, l3, out);  //

endmodule

解决过程:

知道原因是信号被重复定义了,百度了之后发现大家犯的错在定义某信号前在其它模块接口信号中调用了这个信号,可通过把信号定义挪到调用模块前面解决问题

但上面的代码段中:

wire [N - 1:0] l1, l2, l3, g1;

信号定义是在模块例化之前的,不存在该问题。

卡了好久才发现,好像实例名字和信号名字重复了……

解决方法:

修改实例名,不要与变量名重复,否则会重复定义


module mid #(parameter N = 4) (
    input [N - 1:0] a,
    input [N - 1:0] b,
    input [N - 1:0] c,
    output[N - 1:0] out
);
    wire [N - 1:0] l1, l2, l3, g1;

    MagCompL min1(a, b, l1);
    MagCompL min2(b, c, l2);
    MagCompL min3(a, c, l3);
    MagCompG max1(l1, l2, g1);
    MagCompG max2(g1, l3, out);  //

endmodule

总结与反思:

1.模块实例化不能放在always块里,(实例化只需要一次,与always语义不符)

2.verilog的执行机制中,信号定义与模块实例化有先后顺序?

你可能感兴趣的:(Verilog学习,fpga开发)