startup_motor_stop_tb.v 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. `timescale 1ns/1ps
  2. // Startup + motor engine: LS hard-stop during close truncates axis before step budget.
  3. module startup_motor_stop_tb;
  4. localparam integer AXIS_COUNT = 3;
  5. localparam integer CLOSE_STEPS = 16;
  6. localparam integer BACKOFF_STEPS = 4;
  7. reg clk = 1'b0;
  8. reg rst_n = 1'b0;
  9. reg tick_500us = 1'b0;
  10. reg [AXIS_COUNT-1:0] limit_ls = {AXIS_COUNT{1'b0}};
  11. reg [AXIS_COUNT-1:0] limit_hs = {AXIS_COUNT{1'b0}};
  12. reg limit_valid = 1'b0;
  13. wire startup_done;
  14. wire motor_start;
  15. wire [AXIS_COUNT-1:0] motor_active_mask;
  16. wire [AXIS_COUNT-1:0] motor_dir_mask;
  17. wire [AXIS_COUNT-1:0] motor_stop_mask;
  18. wire [4:0] motor_step_count;
  19. wire motor_busy;
  20. wire motor_done;
  21. wire [AXIS_COUNT-1:0] step;
  22. integer x_rises;
  23. reg prev_x;
  24. always #1 clk = ~clk;
  25. always @(posedge clk) begin
  26. prev_x <= step[0];
  27. if (!prev_x && step[0]) x_rises <= x_rises + 1;
  28. end
  29. startup_close_seek #(
  30. .CLOSE_STEPS(CLOSE_STEPS),
  31. .BACKOFF_STEPS(BACKOFF_STEPS),
  32. .AXIS_COUNT(AXIS_COUNT),
  33. .CLOSE_DIR_MASK({AXIS_COUNT{1'b1}})
  34. ) u_startup (
  35. .clk(clk), .rst_n(rst_n),
  36. .limit_sync_valid(limit_valid),
  37. .close_limit(limit_ls), .open_limit(limit_hs),
  38. .startup_done(startup_done), .motor_start(motor_start),
  39. .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
  40. .motor_step_count(motor_step_count), .motor_stop_mask(motor_stop_mask),
  41. .motor_done(motor_done)
  42. );
  43. door_motor_engine #(
  44. .AXIS_COUNT(AXIS_COUNT),
  45. .MAX_STEPS(CLOSE_STEPS),
  46. .DIR_SETUP_TICKS(1)
  47. ) u_motor (
  48. .clk(clk), .rst_n(rst_n), .start(motor_start),
  49. .active_mask(motor_active_mask), .dir_mask(motor_dir_mask),
  50. .step_count(motor_step_count),
  51. .tick_500us(tick_500us), .abort(1'b0),
  52. .stop_mask(motor_stop_mask),
  53. .busy(motor_busy), .done(motor_done), .step(step), .dir()
  54. );
  55. task pulse_tick;
  56. begin
  57. @(negedge clk);
  58. tick_500us = 1'b1;
  59. @(negedge clk);
  60. tick_500us = 1'b0;
  61. end
  62. endtask
  63. task reset_dut;
  64. begin
  65. rst_n = 1'b0;
  66. limit_valid = 1'b0;
  67. x_rises = 0;
  68. prev_x = 1'b0;
  69. repeat (4) @(posedge clk);
  70. rst_n = 1'b1;
  71. repeat (4) @(posedge clk);
  72. limit_valid = 1'b1;
  73. @(posedge clk);
  74. end
  75. endtask
  76. initial begin
  77. #2000000 $fatal(1, "startup_motor_stop_tb timeout");
  78. end
  79. initial begin
  80. // All LS high -> no steps.
  81. limit_ls = 3'b111;
  82. limit_hs = 3'b000;
  83. reset_dut();
  84. repeat (32) pulse_tick();
  85. if (!startup_done) $fatal(1, "expected startup_done when all LS high");
  86. if (x_rises !== 0) $fatal(1, "unexpected X steps with all LS high");
  87. // X hits LS during close before consuming full budget.
  88. limit_ls = 3'b000;
  89. limit_hs = 3'b000;
  90. reset_dut();
  91. while (x_rises < 1) pulse_tick();
  92. limit_ls[0] = 1'b1;
  93. while (!startup_done) pulse_tick();
  94. if (x_rises >= CLOSE_STEPS)
  95. $fatal(1, "X reached full close budget after LS stop, x=%0d", x_rises);
  96. $display("startup_motor_stop_tb PASS");
  97. $finish;
  98. end
  99. endmodule