2016-04-29 130 views
-1

我是verilog的新手。我正在使用Xilinx IDE。我的ALU模块如下:简单的Verilog 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; 
[email protected](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 

enter image description here

我给模拟输出被连接作为图像。

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

+2

在'test_projectALU'中,将''''''从'reg'改成''wire'。 – Greg

+2

注意:'!in1;'是一个布尔值而不是in1。 '〜in1'是in1的按位倒数,可能是你在这里的意思。 – Morgan

回答

2

out在您的测试台中是X,因为它永远不会被分配一个值。您错误地将它连接到ALU模块实例的端口clk。我的模拟器给我一个警告:

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