| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- `timescale 1ns/1ps
- module prod2u_fan_tach_capture8_tb;
- reg clk;
- reg rst_n;
- reg [7:0] tach_in;
- wire [8*16-1:0] sample_edges;
- wire [7:0] sample_overflow;
- wire [31:0] sample_seq;
- wire [31:0] sample_ticks;
- prod2u_fan_tach_capture8 #(
- .WINDOW_TICKS(128),
- .WINDOW_COUNTER_WIDTH(7),
- .EDGE_COUNTER_WIDTH(4)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .tach_in(tach_in),
- .sample_edges(sample_edges),
- .sample_overflow(sample_overflow),
- .sample_seq(sample_seq),
- .sample_ticks(sample_ticks)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- task wait_window_tick;
- input [6:0] value;
- begin
- while (dut.window_tick !== value) begin
- @(negedge clk);
- end
- end
- endtask
- initial begin
- rst_n = 1'b0;
- tach_in = 8'd0;
- repeat (3) @(negedge clk);
- rst_n = 1'b1;
- // Three synchronizer stages from pin observation to edge detection put
- // this rising edge exactly on the window-closing clock.
- wait_window_tick(7'd125);
- tach_in[0] = 1'b1;
- @(negedge clk);
- tach_in[0] = 1'b0;
- @(negedge clk);
- @(negedge clk);
- if (sample_seq !== 32'd1 || sample_ticks !== 32'd128 ||
- sample_edges[15:0] !== 16'd1 || sample_overflow[0]) begin
- $display("tach sample must atomically include the closing-boundary edge");
- $fatal;
- end
- repeat (128) @(negedge clk);
- if (sample_seq !== 32'd2 || sample_edges[15:0] !== 16'd0 ||
- sample_overflow[0]) begin
- $display("tach sample must clear only after publishing the completed window");
- $fatal;
- end
- // Sixteen real rising edges overflow a four-bit test counter at 15.
- repeat (16) begin
- tach_in[1] = 1'b1;
- repeat (2) @(negedge clk);
- tach_in[1] = 1'b0;
- repeat (2) @(negedge clk);
- end
- while (sample_seq < 32'd3) @(negedge clk);
- if (sample_edges[(1*16) +: 16] !== 16'h000f || !sample_overflow[1]) begin
- $display("tach sample must saturate at 16 bits and report overflow");
- $fatal;
- end
- $display("PASS: prod2u_fan_tach_capture8_tb");
- $finish;
- end
- endmodule
|