module count4_rst_en_load(clk,rst,en,load,data,cnt); input clk,rst,en,load; input [3:0]data; output reg[3:0]cnt; always @(posedge clk or negedge rst) begin if(!rst) cnt<=0; else if(en==1) begin if(load) cnt<=data; else cnt<=cnt+1; end end endmodule 测试程序: `timescale 1 ns/10 ps module test_count4_rst_en_load(); reg clk=0; reg rst,en,load; reg [3:0]data; wire [3:0]cnt; count4_rst_en_load i1(.clk(clk),.rst(rst),.en(en), .load(load),.data(data),.cnt(cnt)); always #10 clk=~clk; initial begin rst=0;en=0;load=0;data=4'd6; #20 en=1; #20 rst=1; #20 load=1; #20 rst=0; #20 rst=1; #20 load=0; end initial begin $monitor($time,,,"clk=%d rst=%d en=%d load=%d cnt=%d", clk,rst,en,load,data,cnt); #800 $stop; end endmodule 仿真波形: |