counter_tb.v 文件
`timescale 1ns/1ns `define clock_period 20 module counter_tb ; reg cin ; //进位输入( +1) reg clk; //计数基准时钟 wire cout ; //进位输出 wire [3:0]q ; //计数器输出 counter counter0 ( .cin(cin), .clock(clk), .cout(cout), .q(q) ); initial clk =1 ; always #(`clock_period/2) clk =~clk ; initial begin repeat (25) begin // repeat : 这里表示重复5次 cin = 0; //先初始化 cin =0 ; 低电平 #(`clock_period * 5) cin = 1 ; //隔5个时钟周期后,cin =1 变为高电平 #(`clock_period) cin = 0 ; //隔1个时钟周期后,cin =0 变为低电平 end #(`clock_period * 200) ; $stop ; end endmodulecounter_top.v文件
module counter_top( cin , clk , cout , q ); input cin ; input clk ; output cout; output [7:0]q ; wire cout_0 ; counter counter_0( .cin(cin), .clock(clk), .cout(cout_0), .q(q[3:0]) ); counter counter_1( .cin(cout_0), .clock(clk), .cout(cout), .q(q[7:4]) ); endmodulecounter_top_tb .v 文件
`timescale 1ns/1ns `define clock_period 20 module counter_top_tb ; reg cin ; //进位输入( +1) reg clk; //计数基准时钟 wire cout ; //进位输出 wire [7:0]q ; //计数器输出 counter_top counter_top_0( .cin(cin) , .clk(clk) , .cout(cout) , .q(q) ); initial clk =1 ; always #(`clock_period/2) clk =~clk ; initial begin repeat (320) begin // repeat : 这里表示重复5次 cin = 0; //先初始化 cin =0 ; 低电平 #(`clock_period * 5) cin = 1 ; //隔5个时钟周期后,cin =1 变为高电平 #(`clock_period) cin = 0 ; //隔1个时钟周期后,cin =0 变为低电平 end #(`clock_period * 200) ; $stop ; end endmodule