prod2u_fan_tach_capture8_tb.v 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. `timescale 1ns/1ps
  2. module prod2u_fan_tach_capture8_tb;
  3. reg clk;
  4. reg rst_n;
  5. reg [7:0] tach_in;
  6. wire [8*16-1:0] sample_edges;
  7. wire [7:0] sample_overflow;
  8. wire [31:0] sample_seq;
  9. wire [31:0] sample_ticks;
  10. prod2u_fan_tach_capture8 #(
  11. .WINDOW_TICKS(128),
  12. .WINDOW_COUNTER_WIDTH(7),
  13. .EDGE_COUNTER_WIDTH(4)
  14. ) dut (
  15. .clk(clk),
  16. .rst_n(rst_n),
  17. .tach_in(tach_in),
  18. .sample_edges(sample_edges),
  19. .sample_overflow(sample_overflow),
  20. .sample_seq(sample_seq),
  21. .sample_ticks(sample_ticks)
  22. );
  23. initial begin
  24. clk = 1'b0;
  25. forever #5 clk = ~clk;
  26. end
  27. task wait_window_tick;
  28. input [6:0] value;
  29. begin
  30. while (dut.window_tick !== value) begin
  31. @(negedge clk);
  32. end
  33. end
  34. endtask
  35. initial begin
  36. rst_n = 1'b0;
  37. tach_in = 8'd0;
  38. repeat (3) @(negedge clk);
  39. rst_n = 1'b1;
  40. // Three synchronizer stages from pin observation to edge detection put
  41. // this rising edge exactly on the window-closing clock.
  42. wait_window_tick(7'd125);
  43. tach_in[0] = 1'b1;
  44. @(negedge clk);
  45. tach_in[0] = 1'b0;
  46. @(negedge clk);
  47. @(negedge clk);
  48. if (sample_seq !== 32'd1 || sample_ticks !== 32'd128 ||
  49. sample_edges[15:0] !== 16'd1 || sample_overflow[0]) begin
  50. $display("tach sample must atomically include the closing-boundary edge");
  51. $fatal;
  52. end
  53. repeat (128) @(negedge clk);
  54. if (sample_seq !== 32'd2 || sample_edges[15:0] !== 16'd0 ||
  55. sample_overflow[0]) begin
  56. $display("tach sample must clear only after publishing the completed window");
  57. $fatal;
  58. end
  59. // Sixteen real rising edges overflow a four-bit test counter at 15.
  60. repeat (16) begin
  61. tach_in[1] = 1'b1;
  62. repeat (2) @(negedge clk);
  63. tach_in[1] = 1'b0;
  64. repeat (2) @(negedge clk);
  65. end
  66. while (sample_seq < 32'd3) @(negedge clk);
  67. if (sample_edges[(1*16) +: 16] !== 16'h000f || !sample_overflow[1]) begin
  68. $display("tach sample must saturate at 16 bits and report overflow");
  69. $fatal;
  70. end
  71. $display("PASS: prod2u_fan_tach_capture8_tb");
  72. $finish;
  73. end
  74. endmodule