HDLbits Verfication: Writing Testbenches

这篇博客展示了几个Verilog模块的实现,包括时钟生成、与门、TFF时钟翻转触发器,以及相应的测试平台。在每个模块中,详细说明了测试平台如何生成输入信号和时钟,并通过示例代码解释了测试激励的设置和时序操作。这些实例对于理解和验证数字逻辑设计非常有帮助。

Verfication: Writing Testbenches

clock

`timescale 1ps / 1ps
module top_module ( );
reg clk;
dut u_dut( .clk(clk) ) ;
initial begin
    clk = 1'b0;
    forever begin
        #(5) clk = ~clk;
    end
end
endmodule

tb1

`timescale 1ps / 1ps
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

`timescale 1ps/1ps
module top_module();
reg [1:0] in;
wire out;
andgate u_andgate(
    .in(in),
    .out(out)
);
initial begin
    in = 2'b00;
    #10;
    in[0] = 1'b1;
    #10;
    in[0] = 1'b0;
    in[1] = 1'b1;
    #10;
    in[0] = 1'b1;
end

endmodule

tb2

`timescale 1ps/1ps
module top_module();
reg [2:0] s;
reg clk,in;
wire out;
q7 u_q7(
    .clk(clk),
    .in(in),
    .s(s),
    .out(out)
);
initial begin
    clk = 1'b0;
    forever begin
        #5;
        clk = ~clk;

    end
end
initial begin
    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
endmodule

tff

`timescale 1ps/1ps
module top_module ();
reg clk,reset,t;
wire q;
tff u_tff(
    .clk(clk),
    .reset(reset),
    .t(t),
    .q(q)
);

initial begin
    clk = 1'b0;
    forever begin       
        #5;
        clk = ~clk;
    end
end
initial begin
    reset = 1'b0;
    t = 1'b0;
    #10;
    reset = 1'b1;
    #10;
    reset = 1'b0;
    t = 1'b1;

end
endmodule
//reset the T flip-flop then toggle it to the "1" state.意思是把t置1
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值