`timescale 1ns/1ps module counter_12_tb; reg clk, rst_n, valid_count; wire [3:0] out; counter_12 dut ( .rst_n(rst_n), .clk(clk), .valid_count(valid_count), .out(out) ); always #5 clk = ~clk; integer i = 0; integer error = 0; integer hexp; task hard_chk(input [3:0] exp, input [8*28-1:0] what); begin if (out !== exp) begin error = error + 1; $display("HARD %0t: out=%0d, expected %0d (%0s)", $time, out, exp, what); end end endtask initial begin clk = 0; rst_n = 0; valid_count = 0; #20 rst_n = 1; // testcase1: validation of valid_count repeat(15) begin error = (out == 0) ?error:error+1; #10; end // testcase2: counter #100 valid_count = 1; repeat(11) begin error = (out == i) ?error:error+1; i = i+1; #10; end // testcase3: the count is paused if valid_count is invalid valid_count = 0; repeat(5) begin error = (out == 11) ?error:error+1; #10; end // additional check (t=430; clk rises at x5, out sampled at x0 as above): wrap 11 -> 0, pause mid-count, mid-run reset. valid_count = 1; hexp = 11; repeat (27) begin // 11 -> 0,1,..,11,0,..,11,0,1,2 #10; hexp = (hexp == 11) ? 0 : hexp + 1; hard_chk(hexp, "counting (wrap after 11)"); end valid_count = 0; repeat (3) begin #10; hard_chk(hexp, "paused"); end valid_count = 1; repeat (12) begin // through 11 -> 0 again #10; hexp = (hexp == 11) ? 0 : hexp + 1; hard_chk(hexp, "counting (wrap after 11)"); end #2 rst_n = 0; // x2, valid_count=1, held low across two rising edges #18 hard_chk(0, "rst_n low"); // x0, after two edges #7 rst_n = 1; // x7, released between edges #3 hard_chk(0, "after reset"); // x0, no rising edge since the release hexp = 0; repeat (13) begin #10; hexp = (hexp == 11) ? 0 : hexp + 1; hard_chk(hexp, "counting after reset"); end if (error == 0) begin $display("===========Your Design Passed==========="); end else begin $display("===========Failed===========", error); end $finish; end endmodule