2014-01-16 70 views
-1

我的代码为设计模块和测试台编译,但是当我在modelsim中模拟它时,出现此错误:“加载设计时出错。 任何人都可以告诉我我的代码中哪里出错了吗?模拟verilog modelsim

下面是设计模块::

module alu(c,carry,zero,a,b,cin,opr);  
input [3:0] a,b;   // port A,B  
input cin ;    // carry input from carry flag register  
output [3:0] c;  // the result 
output carry; // carry output  
output zero ; // zero output 
input [3:0] opr ;  // functionality control for ALU  
wire [4:0] result;  // ALU result  
assign result = alu_out(a,b,cin,opr);  
assign c = result[3:0];  
assign carry = result[4] ;  
function [4:0] alu_out;  
input [3:0] a,b ;  
input  cin ;  
input [3:0] opr ;  
case (opr)   
4'b0001: alu_out=a+4'b0001 ;   // increment data on port A 

4'b0010: alu_out=a-4'b0001 ;   // decrement data on port A 

4'b0011: alu_out=a+b+cin;   // ADD with CARRY 

4'b0100: alu_out=a-b ;    // SUB without BORROW 

4'b0101: alu_out=a-b+(~cin);   // SUB with BORROW 

4'b0110: alu_out=a*b; 

4'b0111: alu_out=a/b; 

4'b1000: AND(alu_out, a, b); 

4'b1001: OR(alu_out, a, b); 

4'b1010: NAND(alu_out, a, b);  // NAND 

4'b1011: NOR(alu_out, a, b); 

4'b1100: XNOR(alu_out, a, b);   

4'b1101: alu_out=a^b;    // EXOR 

4'b1110: alu_out={b[3:0],1'b0};  // Shift Left 

4'b1111: alu_out={b[0],1'b0,b[3:1]}; // Shift Right 


default : begin 

alu_out=9'bxxxxxxxxx; 

$display("Invalid Operation!"); 

end  

endcase 

endfunction 

endmodule 

的代码,这里是测试平台的代码块:

module tb_alu(); 
    reg [3:0] _a, _b, _opr; 
    reg _cin; 
    wire [3:0] _carry, _zero, _c; 

    initial begin 
    _a=4'b0001; 
    _b=4'b0010; 
    _cin=0; 
    _opr=4'b0001; 
    end 
    alu al(_c, _carry, _zero,_a, _b, _cin, _opr); 
endmodule 
+0

你会得到什么输出?它与你期望的有什么不同? – toolic

+0

为什么您的信号名称以下划线开头?那对我来说看起来很奇怪...... – Russell

+0

得分不成问题 – user3197818

回答

0

感谢您的回答。 我使用ISE进行仿真,ISE没有得到任何错误。

0

稍作修改运行对我来说和输出:

Invalid Operation! 
Invalid Operation! 

我删除了原始门和隐含的按位运算:虽然在我的XNOR上不确定,但您需要检查它。如下所示

module test(c,carry,zero,a,b,cin,opr);  
input [3:0] a,b;   // port A,B  
input cin ;    // carry input from carry flag register  
output [3:0] c;  // the result 
output carry; // carry output  
output zero ; // zero output 
input [3:0] opr ;  // functionality control for ALU  
wire [4:0] result;  // ALU result  
assign result = alu_out(a,b,cin,opr);  
assign c = result[3:0];  
assign carry = result[4] ; 

function [4:0] alu_out;  
    input [3:0] a,b ;  
    input  cin ;  
    input [3:0] opr ; 
    begin  
    case (opr)   
    4'b0001: alu_out = a+4'b0001 ;   // increment data on port A 
    4'b0010: alu_out = a-4'b0001 ;   // decrement data on port A 
    4'b0011: alu_out = a+b+cin;   // ADD with CARRY 
    4'b0100: alu_out = a-b ;    // SUB without BORROW 
    4'b0101: alu_out = a-b+(~cin);   // SUB with BORROW 
    4'b0110: alu_out = a*b; 
    4'b0111: alu_out = a/b; 
    4'b1000: alu_out = a&b; // AND(alu_out, a, b); 
    4'b1001: alu_out = a|b; //OR(alu_out, a, b); 
    4'b1010: alu_out = ~(a&b); //NAND(alu_out, a, b);  // NAND 
    4'b1011: alu_out = ~(a|b); //NOR(alu_out, a, b); 
    4'b1100: alu_out = a~^b ; //XNOR(alu_out, a, b);   
    4'b1101: alu_out = a^b;    // EXOR 
    4'b1110: alu_out = {b[3:0],1'b0};  // Shift Left 
    4'b1111: alu_out = {b[0],1'b0,b[3:1]}; // Shift Right 

    default : begin 
     alu_out= 'x; 
     $display("Invalid Operation!"); 
    end  

    endcase 
end 

endfunction 

endmodule 
+0

不!我用你的代码,但我没有看到任何错误的变化。 – user3197818

+0

@ user3197818,我已将begin语句添加到函数中。 modelsim可能需要这种语法。让我们知道更新示例如何工作。 – Morgan

+0

没有这些更改没有解决我的问题! – user3197818

0

尝试修改默认语句:

default : begin 
    alu_out=5'bxxxxx; 

    $display("Invalid Operation!"); 

end  

这是因为函数alu_out的返回值是5位的大小的,而在缺省情况下,它是一个9位大小返回值。这应该让你摆脱错误Error loading design