// tb_pulse_sync.sv // Self-checking testbench for pulse_sync. Two unrelated clocks (10 ns and 14.3 ns). // Sends NUM_PULSES pulses with random spacing that respects the 3-destination-cycle // rule, then checks that the destination saw the same number of one-cycle pulses. `timescale 1ns/1ps module tb_pulse_sync; localparam int NUM_PULSES = 200; localparam int MIN_GAP = 4; // extra source cycles after each pulse (minimum) logic clk_src, clk_dst; logic rst_src, rst_dst; logic pulse_src; logic pulse_dst; logic pulse_dst_d; integer seed; // fixed seed: the run is repeatable int sent, received, width_errors; pulse_sync dut ( .clk_src(clk_src), .rst_src(rst_src), .pulse_src(pulse_src), .clk_dst(clk_dst), .rst_dst(rst_dst), .pulse_dst(pulse_dst) ); initial begin clk_src = 1'b0; forever #5.0 clk_src = ~clk_src; // 100 MHz end initial begin clk_dst = 1'b0; forever #7.15 clk_dst = ~clk_dst; // about 69.9 MHz, unrelated to clk_src end // Destination-side monitor: count pulses and check each lasts one cycle. initial begin received = 0; width_errors = 0; pulse_dst_d = 1'b0; forever begin @(posedge clk_dst); if (pulse_dst) begin received = received + 1; if (pulse_dst_d) begin $display("ERROR: pulse_dst high for more than one cycle at %0t", $time); width_errors = width_errors + 1; end end pulse_dst_d = pulse_dst; end end // Stimulus and final check. initial begin seed = 1; sent = 0; rst_src = 1'b1; rst_dst = 1'b1; pulse_src = 1'b0; repeat (4) @(posedge clk_dst); rst_dst <= 1'b0; @(posedge clk_src); rst_src <= 1'b0; repeat (4) @(posedge clk_src); for (int i = 0; i < NUM_PULSES; i++) begin pulse_src <= 1'b1; @(posedge clk_src); pulse_src <= 1'b0; sent++; // Three destination periods are about 43 ns, i.e. about 5 source cycles. // Wait MIN_GAP+1 to MIN_GAP+9 source cycles before the next pulse. repeat (MIN_GAP + 1 + ($unsigned($random(seed)) % 9)) @(posedge clk_src); end repeat (10) @(posedge clk_dst); if (received == sent && width_errors == 0) begin $display("PASS: %0d pulses sent, %0d received", sent, received); end else begin $display("FAIL: sent %0d, received %0d, width errors %0d", sent, received, width_errors); end $finish; end endmodule