1

我正在使用赛灵思 IDE。我的 ALU 模块如下:

module ALU(in1,in2,operation,clk,out     
);
     input [15:0] in1;
     input [15:0] in2;
     input [3:0] operation;
     input clk;
     output[15:0] out;
reg [15:0] out;
always@(posedge clk)
begin
    case(operation)
                4'b0010:
                    out <= in1+in2;
                4'b0011:
                    out <= in1-in2;
                4'b0100:
                   out <= !in1;
                4'b0101:
                    out <= in1<<in2;
                4'b0110:
                   out <= in1>>in2;
                4'b0111:
                    out <= in1&in2;
                4'b1000:
                    out <= in1|in2;     
                //4'b1001:
                //   out = in1>=in2?16'd0:16'd1;
                default: out <= 16'hFFFF;
    endcase
end
endmodule

我的测试台如下

module test_projectALU;
reg [15:0] in1;
reg [15:0] in2;
reg [3:0] operation;
reg [15:0] out;
reg clk;

ALU PA(in1,in2,operation,out);
initial
begin
operation=4'b0000;
in1=4'b0000;
in2=4'b0000;
clk = 0;
end
always
begin
    #2 operation=4'b0010; in1=4'b0011; in2=4'b0000;
    #2 operation=4'b0011; in1=4'b0001; in2=4'b0011;
    #2 operation=4'b0000; in1=4'b1100; in2=4'b1101;
    #2 operation=4'b0011; in1=4'b1100; in2=4'b1101;
end
always
begin
    #5 clk=~clk; 
    end

initial $monitor($time,"f=%b, a=%b, b=%b,c=%b",operation,in1,in2,out);
//initial #10 $stop;
endmodule

在此处输入图像描述

我的模拟输出作为图像附加。

为什么未定义输出(X 状态)?我究竟做错了什么?

4

2 回答 2

3

out在您的测试台中是 X,因为它从未被赋值。您错误地将其连接到模块实例的clk端口。ALU我的模拟器给了我一个警告:

ALU PA(in1,in2,operation,out);
     |
ncelab: *W,CUVWSP (./tb.v,41|5): 1 output port was not connected: out

改变:

ALU PA(in1,in2,operation,out);

到:

ALU PA(in1,in2,operation,clk,out);

使用按名称连接而不是按位置连接可以帮助避免此类常见错误:

ALU PA (
        // Inputs:
    .clk        (clk),
    .in1        (in1),
    .in2        (in2),
    .operation  (operation),
        // Outputs:
    .out        (out)
);
于 2016-04-29T16:56:00.613 回答
0

ALU 是一个输出端口,因此在您的测试台输出之前将reg转换为wire 。由于您采用了具有相同命名写入模块声明的变量: ALU PA(in1, in2, operation, clk, out);

于 2019-10-30T19:12:53.860 回答