【HDLBits习题详解 4】Verification: Writing Testbenches

1. Tb/clock(Clock)

module top_module ();
    // input
	reg clk;
    
    // ---------- Initial ----------
    initial clk = 0;
    
    always #5 clk = ~clk;
    
    // ---------- Instantiation ----------
    dut uut ( 
        .clk	(clk)
    );
endmodule

2. Tb/tb1(Testbench1)

module top_module ( 
    output reg A, 
    output reg B
);
    // ---------- Initial ----------
    initial begin
		A = 0;
        B = 0;
        
        #10 A = 1;
        #5	B = 1;
        #5	A = 0;
        #20 B = 0;
    end
endmodule

3. Tb/and(AND gate)

module top_module();
    // output
    wire			out;
    // input
    reg		[1:0]	in;
    
    // ---------- Initial ----------
    initial begin
        in = 2'd0;
        
        #10	in[0] 	= 1;
        #10	in		= 2'b10;
        #10	in[0]	= 1;
    end
    
    // ---------- Instantiation ----------
    andgate uut (
        .out	(out),
        .in		(in)
    );
endmodule

4. Tb/tb2(Testbench2)

module top_module();
	// output
    wire			out;
    // input
    reg				clk;
    reg				in;
    reg		[2:0]	s;
    
    // ---------- Initial ----------
    // 输入值clk
    initial 	clk = 0;
    always 	#5 	clk = ~clk;
    
    // 输入值in
    initial begin
        in = 0;
        
        #20	in = 1;
        #10	in = 0;
        #10	in = 1;
        #30	in = 0;
    end
    
    // 输入值s
    initial begin
    	s = 3'd2;
        
        #10	s = 3'd6;
        #10	s = 3'd2;
        #10	s = 3'd7;
        #10	s = 3'd0;
    end
    
    // ---------- Instantiaion ----------
    q7 uut (
        .out	(out),
        .clk	(clk),
        .in		(in),
        .s		(s)
    );
endmodule

5. Tb/tff(T flip-flop)

module top_module ();
	// output
    wire	q;
    // input
    reg		clk;
    reg		reset;
    reg		t;
    
    // Initial
    // 输入值clk
    initial 	clk = 0;
    always 	#5 	clk = ~clk;
    
    // 输入值reset
    initial begin
        reset = 0;
        
        #10	reset = 1;
        #20	reset = 0;
    end
    
    // 输入值t
    initial	begin
        t = 0;
        
        #40	t = 1;
    end
    
    // Instantiation
    tff uut (
        .q		(q),
        .clk	(clk),
        .reset	(reset),
        .t		(t)
    );
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值