`timescale 1ns/1ps

module tb_ultrasonic_fpga;

    reg clk = 0;
    reg start = 0;
    wire sig;
    wire [15:0] distance;

    // Clock 27MHz => periode = 37ns
    always #18 clk = ~clk;

    parameter CLK_FREQ = 27_000_000; // 27 MHz

    ultrasonic_fpga uut (
        .clk(clk),
        .start(start),
        .sig(sig),
        .distance(distance)
    );

    ultrasonic_sensor sensor (
        .clk(clk),
        .signal(sig)
    );

    initial begin
        $dumpfile("ultrasonic.vcd");
        $dumpvars(0, tb_ultrasonic_fpga);

        // Start
        #100;
        start = 1;
        #40;
        start = 0;

        // Attendre que la distance soit mesurée
        // wait (distance > 0);
        #600000; // petite marge pour stabiliser
        $display("Distance mesurée: %d cm", distance);


        // Affiche la distance
        if (distance > 0) begin
            $display("Distance measured: %d cm", distance);
        end else begin
            $display("No distance measured.");
        end

        $finish;
    end

endmodule
