// tb_blinky.sv: run blinky with a 4-bit counter and check that the LED // toggles every 2^(WIDTH-1) = 8 clock cycles. `timescale 1ns/1ps module tb_blinky; localparam int unsigned WIDTH = 4; localparam int unsigned HALF_PERIOD = 2 ** (WIDTH - 1); // cycles between toggles logic clk; logic led; int unsigned errors = 0; blinky #(.WIDTH(WIDTH)) dut (.clk(clk), .led(led)); initial clk = 1'b0; always #5 clk = ~clk; // 100 MHz initial begin // After power-up the counter is 0, so the LED starts off. #1; if (led !== 1'b0) begin $display("FAIL: LED should start at 0"); errors++; end for (int toggle = 1; toggle <= 6; toggle++) begin repeat (HALF_PERIOD) @(posedge clk); #1; if (led !== toggle[0]) begin $display("FAIL: after %0d toggles LED=%b", toggle, led); errors++; end end if (errors == 0) $display("PASS: LED toggles every %0d cycles", HALF_PERIOD); else $display("FAILED with %0d errors", errors); $finish; end endmodule