door_motion_controller_tb.v 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. `timescale 1ns/1ps
  2. // Smoke: serial X→Y→Z calibration + profile ready → startup_complete;
  3. // then mid-percent request on X uses planner. Uses fast motion_service for sim.
  4. module door_motion_controller_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),
  29. .MIN_TRAVEL(2),
  30. .ENDPOINT_GUARD_EXTRA(8),
  31. .DIR_SETUP_TICKS(1),
  32. .PERCENT_MAX(100),
  33. .MOTION_SERVICE_CLKS(4),
  34. .PHASE_FACTOR(64),
  35. .CAL_RAMP_UNIT(2)
  36. ) dut (
  37. .clk(clk), .rst_n(rst_n),
  38. .tick_500us(tick_500us),
  39. .limit_sync_valid(limit_sync_valid),
  40. .open_limit(open_limit), .close_limit(close_limit),
  41. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  42. .motor_request_valid(motor_request_valid),
  43. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  44. .watchdog_stop(watchdog_stop), .watchdog_tripped(watchdog_stop),
  45. .step(step), .dir_close(dir_close),
  46. .startup_complete(startup_complete),
  47. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  48. .status_capture(status_capture),
  49. .status_rd_addr(status_rd_addr), .status_rd_data(status_rd_data)
  50. );
  51. task pulse_tick;
  52. begin
  53. @(negedge clk); tick_500us = 1'b1;
  54. @(negedge clk); tick_500us = 1'b0;
  55. end
  56. endtask
  57. task pump_service;
  58. input integer n;
  59. integer k;
  60. begin
  61. for (k = 0; k < n; k = k + 1)
  62. @(negedge clk);
  63. end
  64. endtask
  65. task force_rise_axis;
  66. input integer ax;
  67. reg [2:0] mask;
  68. begin
  69. mask = 3'b001 << ax;
  70. @(negedge clk);
  71. gated_step = gated_step | mask;
  72. gated_step_rise = mask;
  73. @(negedge clk);
  74. gated_step_rise = 3'b000;
  75. gated_step = gated_step & ~mask;
  76. end
  77. endtask
  78. task calibrate_axis;
  79. input integer ax;
  80. integer r;
  81. begin
  82. // DIR setup via 500us ticks
  83. repeat (4) pulse_tick();
  84. // Produce >=2 measure-open rises (already on close → measure-open)
  85. for (r = 0; r < 3; r = r + 1) begin
  86. // Wait until STEP high for this axis (DDA toggles)
  87. begin : wait_hi
  88. integer t;
  89. for (t = 0; t < 4000; t = t + 1) begin
  90. if (step[ax]) disable wait_hi;
  91. @(negedge clk);
  92. end
  93. $fatal(1, "timeout waiting step high axis=%0d", ax);
  94. end
  95. force_rise_axis(ax);
  96. // Wait STEP low then continue
  97. begin : wait_lo
  98. integer t;
  99. for (t = 0; t < 4000; t = t + 1) begin
  100. if (!step[ax]) disable wait_lo;
  101. @(negedge clk);
  102. end
  103. end
  104. end
  105. // Hit open on a rise
  106. begin : wait_hi2
  107. integer t;
  108. for (t = 0; t < 4000; t = t + 1) begin
  109. if (step[ax]) disable wait_hi2;
  110. @(negedge clk);
  111. end
  112. $fatal(1, "timeout waiting final step high axis=%0d", ax);
  113. end
  114. @(negedge clk);
  115. open_limit[ax] = 1'b1;
  116. close_limit[ax] = 1'b0;
  117. gated_step[ax] = 1'b1;
  118. gated_step_rise[ax] = 1'b1;
  119. @(negedge clk);
  120. gated_step[ax] = 1'b0;
  121. gated_step_rise[ax] = 1'b0;
  122. repeat (8) @(negedge clk);
  123. end
  124. endtask
  125. integer i;
  126. initial begin
  127. #5_000_000 $fatal(1, "door_motion_controller_tb timeout");
  128. end
  129. initial begin
  130. repeat (2) @(posedge clk);
  131. rst_n = 1'b1;
  132. @(negedge clk);
  133. limit_sync_valid = 1'b1;
  134. // Serial X → Y → Z
  135. calibrate_axis(0);
  136. calibrate_axis(1);
  137. calibrate_axis(2);
  138. // Allow profile builder to finish
  139. pump_service(64);
  140. if (!startup_complete)
  141. $fatal(1, "startup_complete not set after serial cal+profile");
  142. if (calibrated_mask !== 3'b111)
  143. $fatal(1, "calibrated_mask=%b", calibrated_mask);
  144. if (fault_mask !== 3'b000)
  145. $fatal(1, "unexpected fault_mask=%b", fault_mask);
  146. @(negedge clk);
  147. status_capture = 1'b1;
  148. @(negedge clk);
  149. status_capture = 1'b0;
  150. status_rd_addr = 8'd0;
  151. @(negedge clk);
  152. if (status_rd_data !== 8'h47)
  153. $fatal(1, "status byte0=%h expect 47", status_rd_data);
  154. // Mid request 50% on X
  155. open_limit = 3'b000;
  156. @(negedge clk);
  157. percent_x = 8'd50;
  158. percent_y = 8'd0;
  159. percent_z = 8'd0;
  160. motor_request_valid = 1'b1;
  161. @(negedge clk);
  162. motor_request_valid = 1'b0;
  163. for (i = 0; i < 2000; i = i + 1) begin
  164. pulse_tick();
  165. if (step[0]) begin
  166. @(negedge clk);
  167. gated_step[0] = 1'b1;
  168. gated_step_rise[0] = 1'b1;
  169. @(negedge clk);
  170. gated_step[0] = 1'b0;
  171. gated_step_rise[0] = 1'b0;
  172. end
  173. @(negedge clk);
  174. end
  175. if (dir_close[0] !== 1'b1)
  176. $fatal(1, "X mid 50%% from open should move close");
  177. $display("door_motion_controller_tb PASS");
  178. $finish;
  179. end
  180. endmodule