Verilog中的ALU:“无法绑定导线/寄存器/内存”

时间:2019-04-02 03:47:13

标签: verilog modelsim alu

我试图制作一个带有溢出标志的简单32位ALU,然后将ALU的输入和结果输出到屏幕上,但是在连接测试平台的元件时遇到了一些问题。我收到此错误:

  

test_32bALU.v:15:错误:端口数量错误。期望4,得到5。   test_32bALU.v:33:错误:无法绑定导线/ reg /内存   test_unit.overflow' in alu_test'

     精心制作过程中出现

2个错误。

我只是从Verilog开始,我对语法有基本的了解。我知道我不应该问调试问题,但这是我唯一的希望。我的教授或助教不会回复我的帮助请求。如果有人可以帮助我指出我的错误,我将不胜感激。

这是我的32bALU.v文件:

module alu(
    input signed[31:0] a,b,
    input[3:0] opcode;
    output signed[31:0] c;
    output overflow;
    );

reg signed[31:0] result;
assign c = result;

reg tmp;

parameter
add = 4'b0000,
sub = 4'b0110,
sla = 4'b0001,
srai = 4'b0011;

always @(a,b,opcode)
begin

    case(opcode)
        add:
        begin
            c = a + b;
        end 

  endcase
end

always @(c)
begin

    if (c[32:31] == (2'b11 | 2'b10)) // Overflow
    begin
        tmp = 1'b1;
        assign overflow = tmp;
    end
    else begin
        tmp = 1'b0;
        assign overflow = tmp;
    end 
end

assign result = c[31:0];

endmodule

test_32bALU.v

`timescale 1ns/1ps

module alu_test;

// Inputs
reg[31:0] a,b;
reg[2:0] opcode;

// Outputs
wire[31:0] c;
//wire [1:0] zero;
wire [1:0] overflow;
//wire [1:0] neg;

alu test_unit(
    a,b, // Inputs
    opcode,
    c,
    overflow
);

parameter
add = 4'b0000,
sub = 4'b0110,
sla = 4'b0001,
srai = 4'b0011;

initial begin

$display("op: a      : b      : c      : reg_A  : reg_B  : reg_C");
$monitor(" %h:%h:%h:%h:%h:%h:%h",
opcode, a, b, c, test_unit.a, test_unit.b, test_unit.c);
$monitor("%h", test_unit.overflow);

//// add
#10 a=32'b0000_0000_0000_0000_0000_0000_0000_0001;
#10 b=32'b0000_0000_0000_0000_0000_0000_0000_0001;
opcode= add;//3'b000

#10 $finish;

end
endmodule

我对为什么说“端口数错误”感到困惑?我假设这是module alualu test_unit中参数的数量?它们具有相同数量的参数(a,b,c,操作码和溢出),那么我到底缺少什么?我如何确切地获得overflow的值?在添加overflow参数之前,它工作正常,所以我认为我做错了吗?

对于第二个错误,我在这里读到某个地方,可能是由于缺少声明所致,但是我已经声明了所有错误...所以我不确定是什么使它出错了。

2 个答案:

答案 0 :(得分:2)

我不确定这是否是问题,但是您的模块定义不正确。应该如下:

module alu(
    input signed[31:0] a,b,
    input[3:0] opcode,
    output signed[31:0] c,
    output overflow
    );

也许这可以帮助您解决问题。

答案 1 :(得分:2)

在模块声明中用逗号分隔输入和输出。

对于称为A的模块,永远不要依赖于参数的顺序,并且总是尝试使用它;

module A(output wire c, 
         input  wire a, 
         input  wire b);
...
endmodule // A

使用以下示例:

A yourAname(.c(Bar), 
            .a(Foo1), 
            .b(Foo2));

因此,如果模块的I / O的定义和顺序发生更改,则该实例化将跟踪这些更改和/或在模拟/合成时给出适当的错误。

命名时,遵循源代码中的一些简单规则可能会很有用;

inputs  are denoted by  i_yourinputname
outputs are denoted by  o_youroutputname
inout   are denoted by io_yourinputoutputname
wire    are denoted by  w_yourwirename
reg     are denoted by  r_yourregname

因为这样可以避免混乱,并且是开始学习Verilog时应尽快养成的好习惯。