Verilog用于模块的测试

Verilog用于模块的测试

Verilog可以用来描述变化的测试信号,描述测试信号的变化和测试过程的模块也称为testbench。在这里,我写一个示例,大家能明白该怎么写了。

首先要写功能模块——二选一多路选择器。

代码如下:

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2017/03/24 11:02:14
// Design Name: 
// Module Name: muxtwo
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module muxtwo(out, a, b, s1
    );
    input a, b, s1;
    output out;
    reg out;
        always @(s1 or a or b)
            if(!s1) out = a;
                else out = b;
endmodule

模块muxtwo表示的是二选一选择器,输出跟控制信号s1有关。

那么接下来我们需要对模块muxtwo进行测试。

代码如下:

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2017/03/24 11:10:53
// Design Name: 
// Module Name: testBench
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module testBench(
    );
    reg ain, bin, select;
    reg clock;
    wire outw;
    
    initial
    begin
        ain = 0;
        bin = 0;
        select = 0;
        clock = 0;
     end
       always #50 clock <= ~clock;
       
       always @(posedge clock)
        begin
            #1ain = {$random}%2;
            #3bin = {$random}%2;
        end
        
        always #10  select = !select;
        //..........
        muxtwo m(.out(outw), .a(ain), .b(bin), .s1(select));
endmodule

我们可以认为,testbench就是给模块加一个激励,从而根据仿真图来确定是否实现我们 想要的功能。testbench并不难写,主要是你要了解你要测试的模块,并给予正确的激烈。

仿真图:

Verilog用于模块的测试_第1张图片











你可能感兴趣的:(计算机体系结构,Verilog)