module testbench; reg clk; reg reset; reg up_down; wire [15:0] count; // Instantiate the up_down_counter module up_down_counter uut ( .clk(clk), .reset(reset), .up_down(up_down), .count(count) ); integer error = 0; reg [15:0] hexp; task hard_chk(input [8*24-1:0] what); begin if (count !== hexp) begin error = error + 1; $display("HARD %0t: count=%0d, expected %0d (%0s)", $time, count, hexp, what); end end endtask always begin #5 clk <= ~clk; end // Initial initial begin clk = 0; reset = 1; up_down = 1; #10 reset = 0; end // Monitor for displaying count // always @(posedge clk) begin // $display("Count = %d", count); // end // Test counting up initial begin #20 up_down = 1; #100 up_down = 0; // Switch to counting down // $display("Count = %d", count); error = (count==11)?error : error+1; #200 up_down = 1; // Switch back to counting up // $display("Count = %d", count); error = (count==65527)?error : error+1; #300 ; // $display("Count = %d", count); error = (count==21)?error : error+1; // additional check (t=620; clk rises at x5, count sampled at x0 as above): every-cycle +1/-1 with 16-bit wrap // in both directions, and reset in the middle of a run. hexp = 21; repeat (5) begin #10; hexp = hexp + 1; hard_chk("up"); end up_down = 0; repeat (30) begin #10; hexp = hexp - 1; hard_chk("down (wraps 0 -> 65535)"); end up_down = 1; repeat (8) begin #10; hexp = hexp + 1; hard_chk("up (65535 -> 0)"); end #2 reset = 1; // x2, held high across two rising edges (up_down=1) #18 hexp = 0; hard_chk("reset high"); up_down = 0; #10 hard_chk("reset high, up_down=0"); #7 reset = 0; // x7, released between edges #3 hard_chk("after reset"); repeat (3) begin #10; hexp = hexp - 1; hard_chk("down after reset"); end up_down = 1; repeat (6) begin #10; hexp = hexp + 1; hard_chk("up after reset"); end if (error == 0) begin $display("=========== Your Design Passed ==========="); end else begin $display("=========== Test completed with %d failures ===========", error); end $finish; end endmodule