startup_close_seek_tb.v 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. `timescale 1ns/1ps
  2. // 3-axis startup close-seek with accelerated ticks and reduced step budgets.
  3. module startup_close_seek_tb;
  4. localparam integer AXIS_COUNT = 3;
  5. localparam integer CLOSE_STEPS = 12;
  6. localparam integer BACKOFF_STEPS = 4;
  7. localparam integer DIR_SETUP = 1;
  8. reg clk = 1'b0;
  9. reg rst_n = 1'b0;
  10. reg tick_500us = 1'b0;
  11. reg [AXIS_COUNT-1:0] limit_ls = {AXIS_COUNT{1'b0}};
  12. reg [AXIS_COUNT-1:0] limit_hs = {AXIS_COUNT{1'b0}};
  13. reg limit_sync_valid = 1'b0;
  14. wire startup_done;
  15. wire motor_start;
  16. wire [AXIS_COUNT-1:0] motor_active_mask;
  17. wire [AXIS_COUNT-1:0] motor_dir_mask;
  18. wire [AXIS_COUNT-1:0] motor_stop_mask;
  19. wire [12:0] motor_step_count;
  20. wire motor_busy;
  21. wire motor_done;
  22. wire [AXIS_COUNT-1:0] step;
  23. wire [AXIS_COUNT-1:0] dir;
  24. integer start_count;
  25. always #1 clk = ~clk;
  26. startup_close_seek #(
  27. .CLOSE_STEPS(CLOSE_STEPS),
  28. .BACKOFF_STEPS(BACKOFF_STEPS),
  29. .AXIS_COUNT(AXIS_COUNT),
  30. .CLOSE_DIR_MASK({AXIS_COUNT{1'b1}})
  31. ) dut (
  32. .clk(clk), .rst_n(rst_n),
  33. .limit_sync_valid(limit_sync_valid),
  34. .close_limit(limit_ls), .open_limit(limit_hs),
  35. .startup_done(startup_done), .motor_start(motor_start),
  36. .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
  37. .motor_step_count(motor_step_count), .motor_stop_mask(motor_stop_mask),
  38. .motor_done(motor_done)
  39. );
  40. door_motor_engine #(
  41. .AXIS_COUNT(AXIS_COUNT),
  42. .MAX_STEPS(CLOSE_STEPS),
  43. .DIR_SETUP_TICKS(DIR_SETUP)
  44. ) u_motor (
  45. .clk(clk), .rst_n(rst_n), .start(motor_start),
  46. .active_mask(motor_active_mask), .dir_mask(motor_dir_mask),
  47. .step_count(motor_step_count[12:0]),
  48. .tick_500us(tick_500us), .abort(1'b0),
  49. .stop_mask(motor_stop_mask),
  50. .busy(motor_busy), .done(motor_done), .step(step), .dir(dir)
  51. );
  52. task pulse_tick;
  53. begin
  54. @(negedge clk);
  55. tick_500us = 1'b1;
  56. @(negedge clk);
  57. tick_500us = 1'b0;
  58. end
  59. endtask
  60. task wait_sync_valid;
  61. begin
  62. repeat (3) @(posedge clk);
  63. limit_sync_valid = 1'b1;
  64. end
  65. endtask
  66. task wait_startup_done;
  67. begin
  68. while (!startup_done) pulse_tick();
  69. @(posedge clk);
  70. end
  71. endtask
  72. initial begin
  73. #10000000 $fatal(1, "startup_close_seek_tb timeout");
  74. end
  75. always @(posedge clk) begin
  76. if (motor_start) start_count <= start_count + 1;
  77. if (!limit_sync_valid && motor_start)
  78. $fatal(1, "motor_start asserted before limit_sync_valid");
  79. end
  80. initial begin
  81. start_count = 0;
  82. // Scenario 1: all axes already at LS -> immediate done, no close start.
  83. limit_ls = 3'b111;
  84. limit_hs = 3'b000;
  85. limit_sync_valid = 1'b0;
  86. start_count = 0;
  87. repeat (3) @(posedge clk);
  88. rst_n = 1'b1;
  89. wait_sync_valid();
  90. wait_startup_done();
  91. if (start_count !== 0)
  92. $fatal(1, "expected 0 close starts when all LS high, got %0d", start_count);
  93. // Scenario 2: X hits LS during close; Y/Z finish close budget then backoff.
  94. rst_n = 1'b0;
  95. limit_ls = 3'b000;
  96. limit_hs = 3'b000;
  97. limit_sync_valid = 1'b0;
  98. start_count = 0;
  99. @(negedge clk);
  100. repeat (3) @(posedge clk);
  101. rst_n = 1'b1;
  102. wait_sync_valid();
  103. while (!startup_done) begin
  104. if (step[0] && limit_ls[0] == 1'b0) limit_ls[0] <= 1'b1;
  105. pulse_tick();
  106. end
  107. if (limit_ls[0] !== 1'b1) $fatal(1, "X should have reached LS");
  108. if (!startup_done) $fatal(1, "startup_done not set after close/backoff");
  109. // Scenario 3: post-done sticky; limit toggles must not restart.
  110. start_count = 0;
  111. limit_ls = 3'b000;
  112. limit_hs = 3'b111;
  113. repeat (20) pulse_tick();
  114. if (!startup_done) $fatal(1, "startup_done cleared after post-done limit toggle");
  115. if (start_count !== 0) $fatal(1, "post-done limit toggle restarted motion");
  116. $display("startup_close_seek_tb PASS");
  117. $finish;
  118. end
  119. endmodule