startup_close_seek_formal.v 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // Default polarity: close_limit=LS, open_limit=HS.
  46. wire [AXIS_COUNT-1:0] close_limit = limit_ls_sync;
  47. wire [AXIS_COUNT-1:0] open_limit = limit_hs_sync;
  48. startup_close_seek #(
  49. .CLOSE_STEPS(FORMAL_CLOSE_STEPS),
  50. .BACKOFF_STEPS(FORMAL_BACKOFF_STEPS),
  51. .AXIS_COUNT(AXIS_COUNT),
  52. .CLOSE_DIR_MASK(FORMAL_CLOSE_DIR_MASK)
  53. ) u_startup (
  54. .clk(clk),
  55. .rst_n(rst_n),
  56. .limit_sync_valid(formal_sync_valid),
  57. .close_limit(close_limit),
  58. .open_limit(open_limit),
  59. .startup_done(startup_done),
  60. .motor_start(motor_start),
  61. .motor_active_mask(motor_active_mask),
  62. .motor_dir_mask(motor_dir_mask),
  63. .motor_step_count(motor_step_count),
  64. .motor_stop_mask(motor_stop_mask),
  65. .motor_done(motor_done)
  66. );
  67. door_motor_engine #(
  68. .AXIS_COUNT(AXIS_COUNT),
  69. .MAX_STEPS(FORMAL_CLOSE_STEPS),
  70. .DIR_SETUP_TICKS(FORMAL_DIR_SETUP_TICKS)
  71. ) u_motor (
  72. .clk(clk),
  73. .rst_n(rst_n),
  74. .start(motor_start),
  75. .active_mask(motor_active_mask),
  76. .dir_mask(motor_dir_mask),
  77. .step_count(motor_step_count),
  78. .tick_500us(tick_500us),
  79. .abort(1'b0),
  80. .stop_mask(motor_stop_mask),
  81. .busy(motor_busy),
  82. .done(motor_done),
  83. .step(engine_step),
  84. .dir(engine_dir)
  85. );
  86. // Top-style external STEP truncate gate on selected logical limit.
  87. wire [AXIS_COUNT-1:0] selected_limit;
  88. wire [AXIS_COUNT-1:0] gated_step;
  89. assign selected_limit[0] = engine_dir[0] ? close_limit[0] : open_limit[0];
  90. assign selected_limit[1] = engine_dir[1] ? close_limit[1] : open_limit[1];
  91. assign selected_limit[2] = engine_dir[2] ? close_limit[2] : open_limit[2];
  92. assign gated_step[0] = engine_step[0] & ~selected_limit[0];
  93. assign gated_step[1] = engine_step[1] & ~selected_limit[1];
  94. assign gated_step[2] = engine_step[2] & ~selected_limit[2];
  95. wire motor_en_n = 1'b1;
  96. initial begin
  97. reset_cycles = 3'd0;
  98. saw_ls_low = 1'b0;
  99. prev_gated_step = 3'b000;
  100. past_valid = 1'b0;
  101. backoff_rising_x = 4'd0;
  102. in_backoff = 1'b0;
  103. close_phase = 1'b0;
  104. found_ls_frozen = 1'b0;
  105. shadow_found_ls = 3'b000;
  106. end
  107. always @(posedge clk) begin
  108. assume (rst_n == 1'b0 || rst_n == 1'b1);
  109. assume (tick_500us == 1'b0 || tick_500us == 1'b1);
  110. assume ((^limit_ls_sync) == 1'b0 || (^limit_ls_sync) == 1'b1);
  111. assume ((^limit_hs_sync) == 1'b0 || (^limit_hs_sync) == 1'b1);
  112. if ($initstate || reset_cycles < 3) begin
  113. assume (!rst_n);
  114. reset_cycles <= reset_cycles + 1'b1;
  115. past_valid <= 1'b0;
  116. saw_ls_low <= 1'b0;
  117. prev_gated_step <= 3'b000;
  118. backoff_rising_x <= 4'd0;
  119. in_backoff <= 1'b0;
  120. close_phase <= 1'b0;
  121. found_ls_frozen <= 1'b0;
  122. shadow_found_ls <= 3'b000;
  123. end else begin
  124. assume (rst_n);
  125. past_valid <= 1'b1;
  126. end
  127. if (rst_n) begin
  128. assert (motor_en_n == 1'b1);
  129. if (limit_ls_sync != 3'b111)
  130. saw_ls_low <= 1'b1;
  131. // Mirror DUT found_ls only during close-wait (freeze on motor_done).
  132. if (motor_start && (motor_dir_mask == FORMAL_CLOSE_DIR_MASK)) begin
  133. close_phase <= 1'b1;
  134. found_ls_frozen <= 1'b0;
  135. // Non-participants already sat on LS; OR current LS on first close beat.
  136. shadow_found_ls <= (~motor_active_mask) | limit_ls_sync;
  137. end else if (close_phase && !found_ls_frozen) begin
  138. shadow_found_ls <= shadow_found_ls | limit_ls_sync;
  139. if (motor_done)
  140. found_ls_frozen <= 1'b1;
  141. end
  142. if (startup_done || (motor_start && (motor_dir_mask == 3'b000)))
  143. close_phase <= 1'b0;
  144. // Backoff start: DIR=0 and a new motor_start.
  145. if (motor_start && (motor_dir_mask == 3'b000)) begin
  146. in_backoff <= 1'b1;
  147. backoff_rising_x <= 4'd0;
  148. assert (motor_step_count == FORMAL_BACKOFF_STEPS[2:0]);
  149. // Axes that already found LS during close must not enter backoff.
  150. assert ((motor_active_mask & shadow_found_ls) == 3'b000);
  151. end
  152. if (startup_done)
  153. in_backoff <= 1'b0;
  154. if (in_backoff && !prev_gated_step[0] && gated_step[0])
  155. backoff_rising_x <= backoff_rising_x + 1'b1;
  156. // Sticky idle after startup_done.
  157. if (!$initstate && $past(startup_done)) begin
  158. assert (startup_done);
  159. assert (motor_start == 1'b0);
  160. assert (motor_active_mask == 3'b000);
  161. assert (motor_dir_mask == 3'b000);
  162. assert (motor_stop_mask == 3'b000);
  163. end
  164. if ($past(motor_start)) assert (!motor_start);
  165. // All LS high from reset: no external STEP rising edges.
  166. if (!saw_ls_low) begin
  167. assert (gated_step == 3'b000);
  168. if (past_valid)
  169. assert (!( !prev_gated_step[0] && gated_step[0] ));
  170. end
  171. // Truncate pin path: selected logical limit => gated STEP 0 same cycle.
  172. if (selected_limit[0])
  173. assert (gated_step[0] == 1'b0);
  174. if (selected_limit[1])
  175. assert (gated_step[1] == 1'b0);
  176. if (selected_limit[2])
  177. assert (gated_step[2] == 1'b0);
  178. // Missed-axis backoff budget: at most BACKOFF_STEPS rising edges.
  179. assert (backoff_rising_x <= FORMAL_BACKOFF_STEPS[3:0]);
  180. if (past_valid) begin
  181. // Engine truncate: registered STEP cleared the cycle after stop_mask.
  182. if ($past(motor_stop_mask[0]) && $past(rst_n))
  183. assert (engine_step[0] == 1'b0);
  184. if ($past(motor_stop_mask[1]) && $past(rst_n))
  185. assert (engine_step[1] == 1'b0);
  186. if ($past(motor_stop_mask[2]) && $past(rst_n))
  187. assert (engine_step[2] == 1'b0);
  188. // During backoff, HS can terminate early (no new rise while HS high).
  189. if (in_backoff && $past(limit_hs_sync[0]) && !engine_dir[0])
  190. assert (!( !prev_gated_step[0] && gated_step[0] ));
  191. end
  192. prev_gated_step <= gated_step;
  193. end
  194. end
  195. always @(posedge clk) begin
  196. cover (rst_n && startup_done);
  197. cover (rst_n && motor_active_mask == 3'b111);
  198. cover (rst_n && startup_done && limit_ls_sync == 3'b000);
  199. cover (rst_n && motor_start && motor_dir_mask == 3'b000);
  200. cover (rst_n && !saw_ls_low && startup_done);
  201. end
  202. endmodule