HDLBits答案(25)_编写Testbench

Verification:Writing Testbenches

HDLBits链接


前言

今天更新HDLBits最后一章的习题:编写Testbench。


题库

Clock

提供了如下描述的模块:

module dut ( input clk ) ;

要求传入频率为10ps的时钟,初始为0,如下图所示。

pic1.png

Solution

module top_module ();

    reg clk;
    initial begin
        clk = 1'b0;
    end
    always #5 clk = ~clk;
    
    dut u0(clk);
    
endmodule

Testbench 1

产生如下图所示的A,B激励。

pic2.png

Solution

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

    // generate input patterns here
    initial begin
		A = 1'b0;
        B = 1'b0;
        #10 A = 1'b1;
        #5 B = 1'b1;
        #5 A = 1'b0;
        #20 B = 1'b0;
    end

endmodule

AND gate

写测试激励测试and模块,波形图如下图所示。

pic3.png

提供的AND模块声明如下:

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

Solution

module top_module();

    reg in_0,in_1;
    reg out;
    
    initial begin
        in_0 = 1'b0;
        in_1 = 1'b0;
        #10 in_0 = 1'b1;
        #10 
        in_0 = 1'b0;
        in_1 = 1'b1;
        #10 in_0 = 1'b1;
    end
    
    andgate u0(.in({in_1,in_0}),.out(out));
    
endmodule

Testbench 2

产生如下图波形图所示的激励信号,激励模块q7;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gJoCkqof-1613628105042)(http://ww1.sinaimg.cn/large/006AXXmQgy1gnrn2vzxtrj311q06f0st.jpg)]

模块q7的描述如下:

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

Solution

module top_module();

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

T flip-flop

该题作者给出了一个T触发器的module,我们仅需将其复位,然后切回到状态1就可以了。

Solution

module top_module ();
    reg clk,reset,t;
    wire q;
    
    tff u0(
        .clk	(clk    ),
        .reset	(reset  ),
        .t      (t      ),
        .q      (q      )
    );

    initial begin
        clk = 1'b0;
        reset = 1'b0;
        #3;
        reset = 1'b1;
        #10;
        reset = 1'b0;   
    end
    
    always #5 clk = ~clk;
    
    always@(posedge clk)begin
        if(reset)begin
            t <= 1'b0;
        end
        else begin
            t <= 1'b1;
        end
    end
 
endmodule

结语

HDLBits系列总算是更新结束了,非常感谢该网站的作者!自己立的flag总算完成了,在寒假收假前刷完了。如果代码有错误的地方欢迎大家指正。

  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日拱一卒_未来可期

若复习顺利望有闲钱的同学支持下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值