startup_close_seek_formal.v 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Startup close/seek BMC with REAL door_motor_engine + top-style STEP truncate
  2. // gate. Stop semantics modeled as ACTUAL RTL truncate (not finish-high):
  3. // truncate_current_high_pulse_immediately_after_synchronized_limit
  4. module startup_close_seek_formal (
  5. input wire clk,
  6. input wire rst_n,
  7. input wire tick_500us,
  8. input wire [2:0] limit_ls_sync,
  9. input wire [2:0] limit_hs_sync
  10. );
  11. localparam integer FORMAL_CLOSE_STEPS = 2;
  12. localparam integer FORMAL_BACKOFF_STEPS = 2;
  13. localparam integer FORMAL_DIR_SETUP_TICKS = 1;
  14. localparam integer AXIS_COUNT = 3;
  15. localparam [AXIS_COUNT-1:0] FORMAL_CLOSE_DIR_MASK = {AXIS_COUNT{1'b1}};
  16. wire startup_done;
  17. wire motor_start;
  18. wire [AXIS_COUNT-1:0] motor_active_mask;
  19. wire [AXIS_COUNT-1:0] motor_dir_mask;
  20. wire [AXIS_COUNT-1:0] motor_stop_mask;
  21. wire [2:0] motor_step_count;
  22. wire motor_done;
  23. wire motor_busy;
  24. wire [AXIS_COUNT-1:0] engine_step;
  25. wire [AXIS_COUNT-1:0] engine_dir;
  26. reg [2:0] reset_cycles;
  27. reg formal_sync_valid;
  28. reg saw_ls_low;
  29. reg [AXIS_COUNT-1:0] prev_gated_step;
  30. reg past_valid;
  31. reg [3:0] backoff_rising_x;
  32. reg in_backoff;
  33. reg close_phase;
  34. reg found_ls_frozen;
  35. reg [AXIS_COUNT-1:0] shadow_found_ls;
  36. always @(posedge clk or negedge rst_n) begin
  37. if (!rst_n) begin
  38. formal_sync_valid <= 1'b0;
  39. end else if (reset_cycles < 3) begin
  40. formal_sync_valid <= 1'b0;
  41. end else begin
  42. formal_sync_valid <= 1'b1;
  43. end
  44. end
  45. startup_close_seek #(
  46. .CLOSE_STEPS(FORMAL_CLOSE_STEPS),
  47. .BACKOFF_STEPS(FORMAL_BACKOFF_STEPS),
  48. .AXIS_COUNT(AXIS_COUNT),
  49. .CLOSE_DIR_MASK(FORMAL_CLOSE_DIR_MASK)
  50. ) u_startup (
  51. .clk(clk),
  52. .rst_n(rst_n),
  53. .limit_sync_valid(formal_sync_valid),
  54. .limit_ls_sync(limit_ls_sync),
  55. .limit_hs_sync(limit_hs_sync),
  56. .startup_done(startup_done),
  57. .motor_start(motor_start),
  58. .motor_active_mask(motor_active_mask),
  59. .motor_dir_mask(motor_dir_mask),
  60. .motor_step_count(motor_step_count),
  61. .motor_stop_mask(motor_stop_mask),
  62. .motor_done(motor_done)
  63. );
  64. door_motor_engine #(
  65. .AXIS_COUNT(AXIS_COUNT),
  66. .MAX_STEPS(FORMAL_CLOSE_STEPS),
  67. .DIR_SETUP_TICKS(FORMAL_DIR_SETUP_TICKS)
  68. ) u_motor (
  69. .clk(clk),
  70. .rst_n(rst_n),
  71. .start(motor_start),
  72. .active_mask(motor_active_mask),
  73. .dir_mask(motor_dir_mask),
  74. .step_count(motor_step_count),
  75. .tick_500us(tick_500us),
  76. .abort(1'b0),
  77. .stop_mask(motor_stop_mask),
  78. .busy(motor_busy),
  79. .done(motor_done),
  80. .step(engine_step),
  81. .dir(engine_dir)
  82. );
  83. // Top-style external STEP truncate gate (dir-select LS/HS).
  84. wire [AXIS_COUNT-1:0] gated_step;
  85. assign gated_step[0] = engine_step[0] &
  86. ~(engine_dir[0] ? limit_ls_sync[0] : limit_hs_sync[0]);
  87. assign gated_step[1] = engine_step[1] &
  88. ~(engine_dir[1] ? limit_ls_sync[1] : limit_hs_sync[1]);
  89. assign gated_step[2] = engine_step[2] &
  90. ~(engine_dir[2] ? limit_ls_sync[2] : limit_hs_sync[2]);
  91. wire motor_en_n = 1'b1;
  92. initial begin
  93. reset_cycles = 3'd0;
  94. saw_ls_low = 1'b0;
  95. prev_gated_step = 3'b000;
  96. past_valid = 1'b0;
  97. backoff_rising_x = 4'd0;
  98. in_backoff = 1'b0;
  99. close_phase = 1'b0;
  100. found_ls_frozen = 1'b0;
  101. shadow_found_ls = 3'b000;
  102. end
  103. always @(posedge clk) begin
  104. assume (rst_n == 1'b0 || rst_n == 1'b1);
  105. assume (tick_500us == 1'b0 || tick_500us == 1'b1);
  106. assume ((^limit_ls_sync) == 1'b0 || (^limit_ls_sync) == 1'b1);
  107. assume ((^limit_hs_sync) == 1'b0 || (^limit_hs_sync) == 1'b1);
  108. if ($initstate || reset_cycles < 3) begin
  109. assume (!rst_n);
  110. reset_cycles <= reset_cycles + 1'b1;
  111. past_valid <= 1'b0;
  112. saw_ls_low <= 1'b0;
  113. prev_gated_step <= 3'b000;
  114. backoff_rising_x <= 4'd0;
  115. in_backoff <= 1'b0;
  116. close_phase <= 1'b0;
  117. found_ls_frozen <= 1'b0;
  118. shadow_found_ls <= 3'b000;
  119. end else begin
  120. assume (rst_n);
  121. past_valid <= 1'b1;
  122. end
  123. if (rst_n) begin
  124. assert (motor_en_n == 1'b1);
  125. if (limit_ls_sync != 3'b111)
  126. saw_ls_low <= 1'b1;
  127. // Mirror DUT found_ls only during close-wait (freeze on motor_done).
  128. if (motor_start && (motor_dir_mask == FORMAL_CLOSE_DIR_MASK)) begin
  129. close_phase <= 1'b1;
  130. found_ls_frozen <= 1'b0;
  131. // Non-participants already sat on LS; OR current LS on first close beat.
  132. shadow_found_ls <= (~motor_active_mask) | limit_ls_sync;
  133. end else if (close_phase && !found_ls_frozen) begin
  134. shadow_found_ls <= shadow_found_ls | limit_ls_sync;
  135. if (motor_done)
  136. found_ls_frozen <= 1'b1;
  137. end
  138. if (startup_done || (motor_start && (motor_dir_mask == 3'b000)))
  139. close_phase <= 1'b0;
  140. // Backoff start: DIR=0 and a new motor_start.
  141. if (motor_start && (motor_dir_mask == 3'b000)) begin
  142. in_backoff <= 1'b1;
  143. backoff_rising_x <= 4'd0;
  144. assert (motor_step_count == FORMAL_BACKOFF_STEPS[2:0]);
  145. // Axes that already found LS during close must not enter backoff.
  146. assert ((motor_active_mask & shadow_found_ls) == 3'b000);
  147. end
  148. if (startup_done)
  149. in_backoff <= 1'b0;
  150. if (in_backoff && !prev_gated_step[0] && gated_step[0])
  151. backoff_rising_x <= backoff_rising_x + 1'b1;
  152. // Sticky idle after startup_done.
  153. if (!$initstate && $past(startup_done)) begin
  154. assert (startup_done);
  155. assert (motor_start == 1'b0);
  156. assert (motor_active_mask == 3'b000);
  157. assert (motor_dir_mask == 3'b000);
  158. assert (motor_stop_mask == 3'b000);
  159. end
  160. if ($past(motor_start)) assert (!motor_start);
  161. // All LS high from reset: no external STEP rising edges.
  162. if (!saw_ls_low) begin
  163. assert (gated_step == 3'b000);
  164. if (past_valid)
  165. assert (!( !prev_gated_step[0] && gated_step[0] ));
  166. end
  167. // Truncate pin path: target-side sync => gated STEP 0 same cycle.
  168. if (engine_dir[0] ? limit_ls_sync[0] : limit_hs_sync[0])
  169. assert (gated_step[0] == 1'b0);
  170. if (engine_dir[1] ? limit_ls_sync[1] : limit_hs_sync[1])
  171. assert (gated_step[1] == 1'b0);
  172. if (engine_dir[2] ? limit_ls_sync[2] : limit_hs_sync[2])
  173. assert (gated_step[2] == 1'b0);
  174. // Missed-axis backoff budget: at most BACKOFF_STEPS rising edges.
  175. assert (backoff_rising_x <= FORMAL_BACKOFF_STEPS[3:0]);
  176. if (past_valid) begin
  177. // Engine truncate: registered STEP cleared the cycle after stop_mask.
  178. if ($past(motor_stop_mask[0]) && $past(rst_n))
  179. assert (engine_step[0] == 1'b0);
  180. if ($past(motor_stop_mask[1]) && $past(rst_n))
  181. assert (engine_step[1] == 1'b0);
  182. if ($past(motor_stop_mask[2]) && $past(rst_n))
  183. assert (engine_step[2] == 1'b0);
  184. // During backoff, HS can terminate early (no new rise while HS high).
  185. if (in_backoff && $past(limit_hs_sync[0]) && !engine_dir[0])
  186. assert (!( !prev_gated_step[0] && gated_step[0] ));
  187. end
  188. prev_gated_step <= gated_step;
  189. end
  190. end
  191. always @(posedge clk) begin
  192. cover (rst_n && startup_done);
  193. cover (rst_n && motor_active_mask == 3'b111);
  194. cover (rst_n && startup_done && limit_ls_sync == 3'b000);
  195. cover (rst_n && motor_start && motor_dir_mask == 3'b000);
  196. cover (rst_n && !saw_ls_low && startup_done);
  197. end
  198. endmodule