door_motion_three_axis_overlap_tb.v 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. `timescale 1ns/1ps
  2. // Scheme A (R12 per-axis kernels): mid-percent overlap with >=2 gated rises/axis.
  3. module door_motion_three_axis_overlap_tb;
  4. localparam integer CAL_TRAVEL = 32;
  5. localparam integer MID_PCT = 50;
  6. reg clk = 1'b0;
  7. reg rst_n = 1'b0;
  8. reg tick_500us = 1'b0;
  9. reg limit_sync_valid = 1'b0;
  10. reg [2:0] open_limit = 3'b000;
  11. reg [2:0] close_limit = 3'b111;
  12. reg [2:0] gated_step = 3'b000;
  13. reg [2:0] gated_step_rise = 3'b000;
  14. reg motor_request_valid = 1'b0;
  15. reg [7:0] percent_x = 8'd0, percent_y = 8'd0, percent_z = 8'd0;
  16. wire [2:0] step, dir_close;
  17. wire startup_complete;
  18. wire [2:0] calibrated_mask, fault_mask;
  19. wire x_moving = dut.u_axis_x.moving;
  20. wire y_moving = dut.u_axis_y.moving;
  21. wire z_moving = dut.u_axis_z.moving;
  22. integer overlap_cycles, overlap_rise_x, overlap_rise_y, overlap_rise_z;
  23. integer sustained_overlap, overlap_done, ax, poll, k;
  24. always #1 clk = ~clk;
  25. door_motion_controller #(
  26. .MAX_CAL_STEPS(64), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(8),
  27. .DIR_SETUP_TICKS(1), .PERCENT_MAX(100)
  28. ) dut (
  29. .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
  30. .limit_sync_valid(limit_sync_valid),
  31. .open_limit(open_limit), .close_limit(close_limit),
  32. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  33. .motor_request_valid(motor_request_valid),
  34. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  35. .watchdog_stop(1'b0), .watchdog_tripped(1'b0),
  36. .step(step), .dir_close(dir_close),
  37. .startup_complete(startup_complete),
  38. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  39. .status_capture(1'b0), .status_rd_addr(8'd0), .status_rd_data()
  40. );
  41. task pulse_tick;
  42. begin
  43. @(negedge clk); tick_500us = 1'b1;
  44. @(negedge clk); tick_500us = 1'b0;
  45. end
  46. endtask
  47. reg [2:0] step_d;
  48. always @(posedge clk or negedge rst_n) begin
  49. if (!rst_n) step_d <= 3'b0; else step_d <= step;
  50. end
  51. wire [2:0] step_rise = step & ~step_d;
  52. initial begin
  53. #8000000 $fatal(1, "timeout");
  54. end
  55. initial begin
  56. overlap_cycles = 0; overlap_rise_x = 0; overlap_rise_y = 0; overlap_rise_z = 0;
  57. sustained_overlap = 0; overlap_done = 0;
  58. repeat (2) @(posedge clk); rst_n = 1'b1;
  59. @(negedge clk); limit_sync_valid = 1'b1;
  60. // Concurrent cal: start with close_limit high → measure-open; count travel; hit open.
  61. repeat (8) pulse_tick();
  62. for (k = 0; k < CAL_TRAVEL; k = k + 1) begin
  63. @(negedge clk);
  64. gated_step = 3'b111; gated_step_rise = 3'b111;
  65. @(negedge clk);
  66. gated_step = 3'b000; gated_step_rise = 3'b000;
  67. repeat (2) pulse_tick();
  68. end
  69. @(negedge clk);
  70. open_limit = 3'b111; close_limit = 3'b000;
  71. @(negedge clk);
  72. open_limit = 3'b000; close_limit = 3'b000;
  73. repeat (40) pulse_tick();
  74. if (!startup_complete) $fatal(1, "no startup_complete cal=%b fault=%b", calibrated_mask, fault_mask);
  75. if (calibrated_mask !== 3'b111) $fatal(1, "cal mask %b fault %b", calibrated_mask, fault_mask);
  76. percent_x = MID_PCT; percent_y = MID_PCT; percent_z = MID_PCT;
  77. @(negedge clk); motor_request_valid = 1'b1;
  78. @(negedge clk); motor_request_valid = 1'b0;
  79. for (poll = 0; poll < 200000 && !overlap_done; poll = poll + 1) begin
  80. @(posedge clk);
  81. tick_500us = (poll % 4) == 0;
  82. if (x_moving && y_moving && z_moving) begin
  83. overlap_cycles = overlap_cycles + 1;
  84. if (overlap_cycles >= 8) sustained_overlap = 1;
  85. end
  86. for (ax = 0; ax < 3; ax = ax + 1) begin
  87. if (step_rise[ax]) begin
  88. gated_step[ax] = 1'b1; gated_step_rise[ax] = 1'b1;
  89. if (x_moving && y_moving && z_moving) begin
  90. if (ax == 0) overlap_rise_x = overlap_rise_x + 1;
  91. else if (ax == 1) overlap_rise_y = overlap_rise_y + 1;
  92. else overlap_rise_z = overlap_rise_z + 1;
  93. end
  94. end else begin
  95. gated_step[ax] = 1'b0; gated_step_rise[ax] = 1'b0;
  96. end
  97. end
  98. if (sustained_overlap && overlap_rise_x >= 2 && overlap_rise_y >= 2 && overlap_rise_z >= 2)
  99. overlap_done = 1;
  100. end
  101. if (!sustained_overlap) $fatal(1, "no overlap mov=%b%b%b", x_moving, y_moving, z_moving);
  102. if (overlap_rise_x < 2 || overlap_rise_y < 2 || overlap_rise_z < 2)
  103. $fatal(1, "rises %0d/%0d/%0d", overlap_rise_x, overlap_rise_y, overlap_rise_z);
  104. $display("door_motion_three_axis_overlap_tb PASS schemeA overlap_cycles=%0d rises=%0d/%0d/%0d",
  105. overlap_cycles, overlap_rise_x, overlap_rise_y, overlap_rise_z);
  106. $finish;
  107. end
  108. endmodule