// tb_led_blinker.sv // Runs led_blinker with a scaled-down clock (CLK_HZ = 1000, BLINK_HZ = 50), // so the LED must toggle every 1000 / 100 = 10 clock cycles. // Run: iverilog -g2012 -o sim tb_led_blinker.sv led_blinker.sv && vvp sim module tb_led_blinker; localparam int CLK_HZ = 1000; localparam int BLINK_HZ = 50; localparam int HALF = CLK_HZ / (2 * BLINK_HZ); // cycles per LED state logic clk = 1'b0; logic rst = 1'b1; logic led; int errors = 0; int cycle = 0; int last_toggle = -1; always #5 clk = ~clk; led_blinker #(.CLK_HZ(CLK_HZ), .BLINK_HZ(BLINK_HZ)) dut ( .clk(clk), .rst(rst), .led(led) ); // Measure the number of clock cycles between LED toggles. logic led_d = 1'b0; int toggles = 0; always @(posedge clk) begin cycle++; led_d <= led; if (!rst && led !== led_d) begin if (last_toggle >= 0 && cycle - last_toggle != HALF) begin $display("FAIL: toggle after %0d cycles, expected %0d", cycle - last_toggle, HALF); errors++; end last_toggle = cycle; toggles++; end end initial begin repeat (3) @(posedge clk); @(negedge clk) rst = 1'b0; repeat (20 * HALF) @(posedge clk); if (toggles < 19) begin $display("FAIL: only %0d toggles", toggles); errors++; end if (errors == 0) $display("PASS: %0d toggles, %0d cycles apart", toggles, HALF); else $display("FAILED with %0d errors", errors); $finish; end endmodule