prod2u_p4_trace_pending_coalescing_tb.v 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. `timescale 1ns/1ps
  2. module prod2u_p4_trace_pending_coalescing_tb;
  3. localparam [7:0] EVENT_OVERFLOW = 8'h07;
  4. reg clk = 1'b0;
  5. reg rst_n = 1'b0;
  6. reg tick_1us = 1'b0;
  7. reg request_valid = 1'b0;
  8. wire request_ready;
  9. reg [7:0] request_type = 8'd0;
  10. reg [5:0] request_length = 6'd0;
  11. reg [271:0] request_payload = 272'd0;
  12. reg [15:0] request_crc16 = 16'd0;
  13. reg emergency_disable_valid = 1'b0;
  14. reg [15:0] emergency_disable_crc16 = 16'd0;
  15. wire boot_valid;
  16. reg boot_ready = 1'b1;
  17. wire [63:0] boot_payload;
  18. wire frame_valid;
  19. reg frame_ready = 1'b0;
  20. wire [7:0] frame_type;
  21. wire [7:0] frame_payload_len;
  22. wire [143:0] frame_payload;
  23. reg x_limit_fast = 1'b0;
  24. reg y_limit_fast = 1'b0;
  25. reg x_limit_active = 1'b0;
  26. reg y_limit_active = 1'b0;
  27. reg x_limit_ready = 1'b1;
  28. reg y_limit_ready = 1'b1;
  29. wire [191:0] ptc_percent;
  30. wire [31:0] fan_percent;
  31. wire [7:0] solenoid_percent;
  32. wire [15:0] state_hash16;
  33. wire [2:0] step_out;
  34. wire [2:0] dir_out;
  35. wire en_n;
  36. wire disabled;
  37. wire watchdog_timeout;
  38. reg [31:0] seq_before;
  39. reg [15:0] held_core_meta;
  40. reg [63:0] held_limit_data;
  41. reg [63:0] held_safety_data;
  42. reg [31:0] first_lost_event_seq;
  43. reg [31:0] first_post_gap_event_seq;
  44. reg [7:0] first_post_gap_event_type;
  45. reg [63:0] first_post_gap_event_data;
  46. reg [3:0] first_post_gap_selection;
  47. integer timeout;
  48. prod2u_p4_normal_v3_core #(
  49. .BUILD_ID(32'h1234abcd),
  50. .CLK_HZ(100),
  51. .WATCHDOG_SECONDS(1),
  52. .STANDARD_STEPS(8),
  53. .DIR_SETUP_US(2),
  54. .SPEED0_HALF_US(4),
  55. .SPEED1_HALF_US(3),
  56. .SPEED2_HALF_US(2),
  57. .SPEED3_HALF_US(1)
  58. ) dut (
  59. .clk(clk), .rst_n(rst_n), .tick_1us(tick_1us),
  60. .request_valid(request_valid), .request_ready(request_ready),
  61. .request_type(request_type), .request_length(request_length),
  62. .request_payload(request_payload), .request_crc16(request_crc16),
  63. .emergency_disable_valid(emergency_disable_valid),
  64. .emergency_disable_crc16(emergency_disable_crc16),
  65. .boot_valid(boot_valid), .boot_ready(boot_ready), .boot_payload(boot_payload),
  66. .frame_valid(frame_valid), .frame_ready(frame_ready), .frame_type(frame_type),
  67. .frame_payload_len(frame_payload_len), .frame_payload(frame_payload),
  68. .x_limit_fast(x_limit_fast), .y_limit_fast(y_limit_fast),
  69. .x_limit_active(x_limit_active), .y_limit_active(y_limit_active),
  70. .x_limit_ready(x_limit_ready), .y_limit_ready(y_limit_ready),
  71. .ptc_percent(ptc_percent), .fan_percent(fan_percent),
  72. .solenoid_percent(solenoid_percent), .state_hash16(state_hash16),
  73. .step_out(step_out), .dir_out(dir_out), .en_n(en_n),
  74. .disabled(disabled), .watchdog_timeout(watchdog_timeout)
  75. );
  76. always #5 clk = ~clk;
  77. task release_forces;
  78. begin
  79. release dut.state_hash16;
  80. release dut.current_safety_state;
  81. release dut.u_trace_buffer.pop_valid;
  82. release dut.u_trace_buffer.operation;
  83. end
  84. endtask
  85. task reset_and_block_trace;
  86. begin
  87. release_forces;
  88. rst_n = 1'b0;
  89. frame_ready = 1'b0;
  90. x_limit_fast = 1'b0;
  91. y_limit_fast = 1'b0;
  92. x_limit_active = 1'b0;
  93. y_limit_active = 1'b0;
  94. x_limit_ready = 1'b0;
  95. y_limit_ready = 1'b0;
  96. repeat (4) @(negedge clk);
  97. rst_n = 1'b1;
  98. timeout = 0;
  99. while (((dut.trace_event_count == 0) ||
  100. (dut.u_trace_buffer.operation != 2'd0)) && timeout < 80) begin
  101. @(posedge clk); #1;
  102. timeout = timeout + 1;
  103. end
  104. if (timeout >= 80 || dut.trace_event_count == 0) $fatal;
  105. // Keep the single BRAM port occupied so these tests isolate
  106. // pending-event collision/coalescing from normal writer service.
  107. force dut.u_trace_buffer.pop_valid = 1'b1;
  108. force dut.u_trace_buffer.operation = 2'd1;
  109. #1;
  110. if (!dut.trace_pop_valid || dut.trace_push_ready) $fatal;
  111. end
  112. endtask
  113. initial begin
  114. reset_and_block_trace;
  115. // CORE drop-new: the first pending payload is immutable and the
  116. // second observation consumes one sequence number.
  117. seq_before = dut.trace_event_seq;
  118. force dut.state_hash16 = 16'h1111;
  119. @(posedge clk); #1;
  120. if (!dut.core_event_pending || dut.core_event_meta !== 16'h1111) $fatal;
  121. if (!request_ready) begin
  122. $fatal(1, "pre-overflow command path was not ready");
  123. end
  124. held_core_meta = dut.core_event_meta;
  125. force dut.state_hash16 = 16'h2222;
  126. @(posedge clk); #1;
  127. if (!dut.core_event_pending || dut.core_event_meta !== held_core_meta) $fatal;
  128. if (dut.trace_event_seq !== seq_before + 32'd1 ||
  129. !dut.trace_overflow_pending || dut.trace_dropped_event_seq !== seq_before) $fatal;
  130. // Queue the gap explanation before accepting a later command result.
  131. // Otherwise the host sees the missing sequence on COMMAND_RESULT first.
  132. request_type = 8'h05;
  133. request_length = 6'd4;
  134. request_payload = 272'd0;
  135. request_payload[7:0] = 8'h29;
  136. request_crc16 = 16'hc001;
  137. request_valid = 1'b1;
  138. #1;
  139. if (request_ready) begin
  140. $fatal(1, "command remained ready while OVERFLOW was pending");
  141. end
  142. @(posedge clk); #1;
  143. if (dut.command_inflight || dut.command_slot_reserved) begin
  144. $fatal(1, "command was accepted before pending OVERFLOW was queued");
  145. end
  146. request_valid = 1'b0;
  147. reset_and_block_trace;
  148. // LIMIT uses drop-new coalescing. AXIS terminal records are held at
  149. // the motor-hub ready/valid boundary and are covered independently.
  150. x_limit_fast = 1'b1;
  151. @(posedge clk); #1;
  152. if (!dut.limit_event_pending) $fatal;
  153. held_limit_data = dut.limit_event_data;
  154. seq_before = dut.trace_event_seq;
  155. x_limit_active = 1'b1;
  156. @(posedge clk); #1;
  157. if (dut.limit_event_data !== held_limit_data) $fatal;
  158. if (dut.trace_event_seq !== seq_before + 32'd1 ||
  159. !dut.trace_overflow_pending || dut.trace_dropped_event_seq !== seq_before) begin
  160. $display("LIMIT_COALESCE_ACCOUNTING seq=%0d expected=%0d overflow=%0d dropped=%0d expected_dropped=%0d pending=%0d draining=%0d collision=%0d",
  161. dut.trace_event_seq, seq_before + 32'd1,
  162. dut.trace_overflow_pending, dut.trace_dropped_event_seq,
  163. seq_before, dut.limit_event_pending,
  164. dut.limit_event_draining, dut.limit_event_collision);
  165. $fatal;
  166. end
  167. reset_and_block_trace;
  168. // SAFETY evidence is critical. A second uncommitted observation is a
  169. // hard-safe condition and must not replace the first payload.
  170. force dut.current_safety_state = 4'b0010;
  171. @(posedge clk); #1;
  172. if (!dut.safety_event_pending) $fatal;
  173. held_safety_data = dut.safety_event_data;
  174. seq_before = dut.trace_event_seq;
  175. force dut.current_safety_state = 4'b0100;
  176. @(posedge clk); #1;
  177. if (dut.safety_event_data !== held_safety_data || !dut.trace_hard_safe) $fatal;
  178. if (dut.trace_event_seq !== seq_before + 32'd1) $fatal;
  179. reset_and_block_trace;
  180. // If a noncritical observation is lost, OVERFLOW is the only event
  181. // that can explain the resulting sequence gap. A SAFETY observation
  182. // becoming pending on the same edge must wait until that OVERFLOW has
  183. // been accepted, or the host will terminate on an unclassified gap.
  184. force dut.state_hash16 = 16'h3333;
  185. @(posedge clk); #1;
  186. if (!dut.core_event_pending) $fatal;
  187. first_lost_event_seq = dut.trace_event_seq;
  188. force dut.state_hash16 = 16'h4444;
  189. force dut.current_safety_state = 4'b0010;
  190. @(posedge clk); #1;
  191. if (!dut.trace_overflow_pending ||
  192. dut.trace_dropped_event_seq !== first_lost_event_seq ||
  193. !dut.safety_event_pending) begin
  194. $display("OVERFLOW_PRIORITY_PRECONDITION overflow=%0d dropped=%0d expected=%0d safety_pending=%0d seq=%0d",
  195. dut.trace_overflow_pending, dut.trace_dropped_event_seq,
  196. first_lost_event_seq, dut.safety_event_pending,
  197. dut.trace_event_seq);
  198. $fatal;
  199. end
  200. release dut.u_trace_buffer.pop_valid;
  201. release dut.u_trace_buffer.operation;
  202. timeout = 0;
  203. while (!(dut.trace_event_attempt && dut.trace_push_ready) &&
  204. timeout < 80) begin
  205. @(negedge clk);
  206. timeout = timeout + 1;
  207. end
  208. if (timeout >= 80) $fatal(1, "post-gap Trace event was not serviced");
  209. first_post_gap_event_seq = dut.trace_event_payload[31:0];
  210. first_post_gap_event_type = dut.trace_event_payload[39:32];
  211. first_post_gap_event_data = dut.trace_event_payload[127:64];
  212. first_post_gap_selection = dut.trace_selection;
  213. @(posedge clk); #1;
  214. if (!dut.trace_push_accepted) begin
  215. $fatal(1, "observed post-gap Trace candidate was not accepted");
  216. end
  217. if (first_post_gap_event_type !== EVENT_OVERFLOW ||
  218. first_post_gap_event_data !== {32'd0, first_lost_event_seq}) begin
  219. $display("OVERFLOW_PRIORITY_RED accepted_seq=%0d accepted_type=0x%02x accepted_data=0x%016x selection=%0d first_lost=%0d safety_pending=%0d overflow_pending=%0d",
  220. first_post_gap_event_seq, first_post_gap_event_type,
  221. first_post_gap_event_data, first_post_gap_selection,
  222. first_lost_event_seq, dut.safety_event_pending,
  223. dut.trace_overflow_pending);
  224. $fatal(1,
  225. "first accepted post-gap Trace must be OVERFLOW with exact first_lost_event_seq");
  226. end
  227. release_forces;
  228. $display("PASS: prod2u_p4_trace_pending_coalescing_tb");
  229. $finish;
  230. end
  231. endmodule