Verilog HDLBits 第二十四期:5.1 Verification :Writing Testbvenches(5.1.1-5.1.5)

目录

前言

5.1.1 Clock(Tb/clock)

Solution:

5.1.2 Testbench1(Tb/tb1)

Solution:

 5.1.3 And gate(Tb/and)

Solution:

 5.1.4 Testbench2(Tb/tb2)

Solution:

5.1.5 T flip-flop(Tb/tff)

Solution:


前言

HDLbits网站如下

Problem sets - HDLBits (01xz.net)

从本期开始我们继续HDLbits第五章Circuits的学习,本期的内容是5.1 Verification :Writing Testbvenches(5.1.1-5.1.5)


5.1.1 Clock(Tb/clock)

我们为您提供了一个包含以下声明的模块:

module dut ( input clk ) ;

编写一个testbench,创建模块dut的一个实例(任何实例名称都可以),并创建一个时钟信号来驱动模块的clk输入。时钟的周期为10 ps。第一次转换为0到1时钟应初始化为零。

​​

Solution:

module top_module ( );
    reg clk;
    dut uut(.clk(clk));
    
    initial begin
        clk=0;
    end
    always begin
        #5 clk=~clk;
    end

endmodule

5.1.2 Testbench1(Tb/tb1)

创建一个Verilog测试台,为输出a和B生成以下波形:

Solution:

module top_module ( output reg A, output reg B );//

    // generate input patterns here
    initial begin
        A=0;
        #10 A=1;
        #10 A=0;
    end
    initial begin
        B=0;
        #15 B=1;
        #25 B=0;
    end

endmodule

 5.1.3 And gate(Tb/and)

您将获得以下希望测试的AND门:

module andgate (
    input [1:0] in,
    output out
);

编写一个测试台,通过生成以下时序图来实例化该与门,并测试所有4种输入组合:

Solution:

module top_module();
    reg[1:0]in;
    wire out;
    andgate uut(
        .in(in),
        .out(out)
    );
    
    initial begin
        in=2'b00;
        #10 in=2'b01;
        #10 in=2'b10;
        #10 in=2'b11;
    end

endmodule

 5.1.4 Testbench2(Tb/tb2)

以下波形设置clk、in和s:

 模块q7有以下声明:

module q7 (
    input clk,
    input in,
    input [2:0] s,
    output out
);

编写一个测试台,实例化模块q7,并生成上述波形所示的输入信号。

Solution:

module top_module();
    reg clk;
    reg in;
    reg [2:0]s;
    wire out;
    q7 myq7(
        .clk(clk),
        .in(in),
        .s(s),
        .out(out)
    );
    initial begin
        clk=0;
    end
    always begin
        #5 clk=~clk;
    end
    initial begin
        in=0;
        #20 in=1;
        #10 in=0;
        #10 in=1;
        #30 in=0;
    end
    initial begin
        s=3'd2;
        #10 s=3'd6;
        #10 s=3'd2;
        #10 s=3'd7;
        #10 s=3'd0;
    end
        
endmodule

5.1.5 T flip-flop(Tb/tff)

您将获得一个带有以下声明的T触发器模块:

module tff (
    input clk,
    input reset,   // active-high synchronous reset
    input t,       // toggle
    output q
);

编写一个测试台,实例化一个tff,复位T触发器,然后将其切换到“1”状态。

Solution:

module top_module ();
    reg clk;
    reg reset;
    reg t;
    wire q;
    tff my_tff(
        .clk(clk),
        .reset(reset),
        .t(t),
        .q(q)
    );
    
    always@(posedge clk) begin
        if(reset)
            t<=1'b0;
    	else
            t<=1'b1;
    end
    initial begin
        reset=1'b0;
        #10 reset=1'b1;
        #10 reset=1'b0;
    end
    
    initial begin
        clk=0;
    end
    
    always begin
        #5 clk=~clk;
    end
    
endmodule

终于做完了,其实一路上很磕磕绊绊,有些也没来得及复习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值