2014-01-16 72 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