door_planner_grant_once_tb.v 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. `timescale 1ns/1ps
  2. // Shared planner arbiter: each axis request granted once; X done must not
  3. // steal a second grant before Y.
  4. module door_planner_grant_once_tb;
  5. reg clk = 1'b0;
  6. reg rst_n = 1'b0;
  7. reg tick_500us = 1'b0;
  8. reg limit_sync_valid = 1'b0;
  9. reg [2:0] open_limit = 3'b000;
  10. reg [2:0] close_limit = 3'b111;
  11. reg [2:0] gated_step = 3'b000;
  12. reg [2:0] gated_step_rise = 3'b000;
  13. reg motor_request_valid = 1'b0;
  14. reg [7:0] percent_x = 8'd0;
  15. reg [7:0] percent_y = 8'd0;
  16. reg [7:0] percent_z = 8'd0;
  17. reg watchdog_stop = 1'b0;
  18. wire [2:0] step;
  19. wire [2:0] dir_close;
  20. wire startup_complete;
  21. wire [2:0] calibrated_mask;
  22. wire [2:0] fault_mask;
  23. reg status_capture = 1'b0;
  24. reg [7:0] status_rd_addr = 8'd0;
  25. wire [7:0] status_rd_data;
  26. always #1 clk = ~clk;
  27. door_motion_controller #(
  28. .MAX_CAL_STEPS(32), .MIN_TRAVEL(2),
  29. .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100),
  30. .MOTION_SERVICE_CLKS(4), .PHASE_FACTOR(64), .CAL_RAMP_UNIT(2)
  31. ) dut (
  32. .clk(clk), .rst_n(rst_n),
  33. .tick_500us(tick_500us),
  34. .limit_sync_valid(limit_sync_valid),
  35. .open_limit(open_limit), .close_limit(close_limit),
  36. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  37. .motor_request_valid(motor_request_valid),
  38. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  39. .watchdog_stop(watchdog_stop), .watchdog_tripped(1'b0),
  40. .step(step), .dir_close(dir_close),
  41. .startup_complete(startup_complete),
  42. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  43. .status_capture(status_capture),
  44. .status_rd_addr(status_rd_addr), .status_rd_data(status_rd_data)
  45. );
  46. integer starts_x, starts_y, starts_z;
  47. always @(posedge clk) begin
  48. if (rst_n && dut.grant_start) begin
  49. if (dut.grant_axis == 2'd0) starts_x = starts_x + 1;
  50. else if (dut.grant_axis == 2'd1) starts_y = starts_y + 1;
  51. else if (dut.grant_axis == 2'd2) starts_z = starts_z + 1;
  52. end
  53. end
  54. task pulse_tick;
  55. begin
  56. @(negedge clk); tick_500us = 1'b1;
  57. @(negedge clk); tick_500us = 1'b0;
  58. end
  59. endtask
  60. task force_rise_ax;
  61. input integer ax;
  62. reg [2:0] m;
  63. begin
  64. m = 3'b001 << ax;
  65. @(negedge clk);
  66. gated_step = gated_step | m; gated_step_rise = m;
  67. @(negedge clk);
  68. gated_step_rise = 3'b000; gated_step = gated_step & ~m;
  69. end
  70. endtask
  71. task wait_step_ax;
  72. input integer ax;
  73. input want;
  74. integer t;
  75. begin
  76. for (t = 0; t < 20000; t = t + 1) begin
  77. if (step[ax] === want) disable wait_step_ax;
  78. @(negedge clk);
  79. end
  80. $fatal(1, "timeout step[%0d]==%0d", ax, want);
  81. end
  82. endtask
  83. task cal_axis;
  84. input integer ax;
  85. input integer travel;
  86. integer r;
  87. begin
  88. close_limit[ax] = 1'b1;
  89. open_limit[ax] = 1'b0;
  90. repeat (6) pulse_tick();
  91. for (r = 0; r < travel; r = r + 1) begin
  92. wait_step_ax(ax, 1);
  93. force_rise_ax(ax);
  94. wait_step_ax(ax, 0);
  95. end
  96. @(negedge clk);
  97. open_limit[ax] = 1'b1;
  98. close_limit[ax] = 1'b0;
  99. begin : w
  100. integer t;
  101. for (t = 0; t < 8000; t = t + 1) begin
  102. if (calibrated_mask[ax]) disable w;
  103. @(negedge clk);
  104. end
  105. end
  106. if (!calibrated_mask[ax]) $fatal(1, "cal fail ax=%0d", ax);
  107. end
  108. endtask
  109. integer i;
  110. initial begin
  111. #5_000_000 $fatal(1, "door_planner_grant_once_tb timeout");
  112. end
  113. initial begin
  114. starts_x = 0; starts_y = 0; starts_z = 0;
  115. open_limit = 3'b000;
  116. close_limit = 3'b000;
  117. repeat (2) @(posedge clk);
  118. rst_n = 1'b1;
  119. @(negedge clk);
  120. limit_sync_valid = 1'b1;
  121. cal_axis(0, 4);
  122. cal_axis(1, 4);
  123. cal_axis(2, 4);
  124. begin : ws
  125. integer t;
  126. for (t = 0; t < 8000 && !startup_complete; t = t + 1) @(negedge clk);
  127. end
  128. if (!startup_complete) $fatal(1, "no startup");
  129. open_limit = 3'b000;
  130. starts_x = 0; starts_y = 0; starts_z = 0;
  131. @(negedge clk);
  132. percent_x = 8'd25;
  133. percent_y = 8'd50;
  134. percent_z = 8'd75;
  135. motor_request_valid = 1'b1;
  136. @(negedge clk);
  137. motor_request_valid = 1'b0;
  138. // Planner iterates up to travel*percent — with travel=4, tiny.
  139. for (i = 0; i < 80; i = i + 1)
  140. pulse_tick();
  141. if (starts_x !== 1)
  142. $fatal(1, "X starts=%0d expect 1", starts_x);
  143. if (starts_y !== 1)
  144. $fatal(1, "Y starts=%0d expect 1", starts_y);
  145. if (starts_z !== 1)
  146. $fatal(1, "Z starts=%0d expect 1", starts_z);
  147. // Endpoint must not use planner
  148. starts_x = 0; starts_y = 0; starts_z = 0;
  149. @(negedge clk);
  150. percent_x = 8'd0;
  151. percent_y = 8'd100;
  152. percent_z = 8'd0;
  153. motor_request_valid = 1'b1;
  154. @(negedge clk);
  155. motor_request_valid = 1'b0;
  156. for (i = 0; i < 20; i = i + 1)
  157. pulse_tick();
  158. if ((starts_x + starts_y + starts_z) !== 0)
  159. $fatal(1, "endpoint targets must not grant planner");
  160. $display("door_planner_grant_once_tb PASS");
  161. $finish;
  162. end
  163. endmodule