shared_500us_tick_tb.v 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. `timescale 1ns/1ps
  2. module shared_500us_tick_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. wire tick;
  6. integer tick_count;
  7. integer cycle_count;
  8. always #1 clk = ~clk;
  9. shared_500us_tick #(.CLOCK_HZ(10), .TICK_US(1000000)) dut (
  10. .clk(clk), .rst_n(rst_n), .tick(tick)
  11. );
  12. always @(negedge clk) begin
  13. if (rst_n && tick) tick_count = tick_count + 1;
  14. end
  15. initial begin
  16. tick_count = 0;
  17. repeat (2) @(posedge clk);
  18. if (dut.counter !== 0 || tick !== 1'b0) $fatal(1, "reset did not clear divider");
  19. @(posedge clk);
  20. #0.1;
  21. rst_n = 1'b1;
  22. for (cycle_count = 0; cycle_count < 10; cycle_count = cycle_count + 1) begin
  23. @(posedge clk);
  24. @(negedge clk);
  25. #0.1;
  26. if (cycle_count < 9) begin
  27. if (dut.counter !== cycle_count + 1 || tick !== 1'b0)
  28. $fatal(1, "divider boundary mismatch cycle=%0d counter=%0d tick=%b", cycle_count, dut.counter, tick);
  29. end else if (dut.counter !== 0 || tick !== 1'b1) begin
  30. $fatal(1, "tick did not occur on divider terminal count counter=%0d tick=%b", dut.counter, tick);
  31. end
  32. end
  33. if (tick_count !== 1) $fatal(1, "tick count mismatch after one period: %0d", tick_count);
  34. for (cycle_count = 0; cycle_count < 10; cycle_count = cycle_count + 1) begin
  35. @(posedge clk);
  36. @(negedge clk);
  37. #0.1;
  38. end
  39. if (tick_count !== 2) $fatal(1, "tick count mismatch after two periods: %0d", tick_count);
  40. $display("shared_500us_tick_tb PASS");
  41. $finish;
  42. end
  43. endmodule