模型中的verilog仿真

时间:2014-01-16 13:55:30

标签: verilog

我的设计块代码和testbench编译,但是当我在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

以下是testbench块的代码:

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试修改默认语句,如下所示:

  default : begin
  alu_out=5'bxxxxx;

  $display("Invalid Operation!");

end    

这是因为函数alu_out的返回值是5位大小,而在默认情况下,它是9位大小的返回值。这可以让你摆脱错误Error loading design

答案 2 :(得分:0)

感谢您的回答。 我使用ISE进行模拟,ISE没有出现任何错误。

相关问题