prod2u_runtime_step_limit_gate_reopen_tb.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. `timescale 1ns/1ps
  2. // Round-12: one engine_step high phase must produce at most one gated_step rise even if
  3. // selected_limit pulses high briefly (truncate then must not re-open until engine low).
  4. module prod2u_runtime_step_limit_gate_reopen_tb;
  5. reg clk = 1'b0;
  6. reg rst_n = 1'b0;
  7. reg engine_step = 1'b0;
  8. reg engine_dir = 1'b0;
  9. reg close_limit = 1'b0;
  10. reg open_limit = 1'b0;
  11. wire selected_limit;
  12. wire gated_step;
  13. reg gated_prev = 1'b0;
  14. integer rise_count;
  15. integer cycle;
  16. integer i;
  17. always #5 clk = ~clk;
  18. prod2u_runtime_step_limit_gate #(.AXIS_COUNT(1)) dut (
  19. .clk(clk),
  20. .rst_n(rst_n),
  21. .engine_step(engine_step),
  22. .engine_dir(engine_dir),
  23. .close_limit(close_limit),
  24. .open_limit(open_limit),
  25. .selected_limit(selected_limit),
  26. .gated_step(gated_step)
  27. );
  28. task sample_rise;
  29. begin
  30. if (gated_step && !gated_prev)
  31. rise_count = rise_count + 1;
  32. gated_prev = gated_step;
  33. end
  34. endtask
  35. task count_rises_during_engine_high;
  36. input integer limit_pulse_cycles;
  37. begin
  38. rise_count = 0;
  39. gated_prev = gated_step;
  40. engine_dir = 1'b1;
  41. close_limit = 1'b0;
  42. open_limit = 1'b0;
  43. engine_step = 1'b0;
  44. @(negedge clk);
  45. sample_rise();
  46. engine_step = 1'b1;
  47. for (cycle = 0; cycle < 24; cycle = cycle + 1) begin
  48. if (cycle == 2)
  49. close_limit = 1'b1;
  50. else if (cycle == (2 + limit_pulse_cycles))
  51. close_limit = 1'b0;
  52. @(negedge clk);
  53. sample_rise();
  54. end
  55. if (rise_count !== 1)
  56. $fatal(1,
  57. "engine high + %0d-cycle limit pulse: %0d gated rises (want 1)",
  58. limit_pulse_cycles, rise_count);
  59. engine_step = 1'b0;
  60. for (i = 0; i < 4; i = i + 1) begin
  61. @(negedge clk);
  62. sample_rise();
  63. end
  64. end
  65. endtask
  66. initial begin
  67. repeat (50_000) @(negedge clk);
  68. $fatal(1, "prod2u_runtime_step_limit_gate_reopen_tb timeout");
  69. end
  70. initial begin
  71. rst_n = 1'b0;
  72. repeat (4) @(negedge clk);
  73. rst_n = 1'b1;
  74. repeat (2) @(negedge clk);
  75. count_rises_during_engine_high(1);
  76. count_rises_during_engine_high(2);
  77. rise_count = 0;
  78. gated_prev = gated_step;
  79. engine_dir = 1'b0;
  80. close_limit = 1'b0;
  81. open_limit = 1'b0;
  82. engine_step = 1'b1;
  83. for (cycle = 0; cycle < 20; cycle = cycle + 1) begin
  84. if (cycle == 1)
  85. open_limit = 1'b1;
  86. else if (cycle == 2)
  87. open_limit = 1'b0;
  88. @(negedge clk);
  89. sample_rise();
  90. end
  91. if (rise_count !== 1)
  92. $fatal(1, "open-dir limit pulse: %0d rises (want 1)", rise_count);
  93. engine_step = 1'b0;
  94. repeat (4) @(negedge clk);
  95. rise_count = 0;
  96. gated_prev = gated_step;
  97. engine_step = 1'b1;
  98. @(negedge clk);
  99. sample_rise();
  100. if (rise_count !== 1)
  101. $fatal(1, "expected fresh rise after engine went low, got %0d", rise_count);
  102. engine_step = 1'b0;
  103. $display("prod2u_runtime_step_limit_gate_reopen_tb PASS");
  104. $finish;
  105. end
  106. endmodule