motor_engine_tb.v 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. `timescale 1ns/1ps
  2. // Motor engine testbench (2026 contract section 10).
  3. //
  4. // Sim-friendly parameters (small min half periods); production parameter
  5. // relations are checked separately against the generated params header.
  6. // Coverage: power-on seek, abort, relative count completion with the
  7. // final-high-pulse rule, hold semantics, batch validation atomicity,
  8. // preempt, lower-limit reference + hit statistics, event ring reads.
  9. module motor_engine_tb;
  10. reg clk = 1'b0;
  11. always #10 clk = ~clk; // 50 MHz for faster waves
  12. reg rst_n = 1'b0;
  13. reg tick_us = 1'b0;
  14. integer half_ticks = 25; // 1 us = 25 cycles at 50 MHz
  15. reg [2:0] limit_lower_raw = 3'b000;
  16. reg [2:0] limit_upper_raw = 3'b000;
  17. reg qw_byte_we = 1'b0;
  18. reg [6:0] qw_byte_addr = 7'd0;
  19. reg [7:0] qw_byte_data = 8'd0;
  20. reg qw_submit = 1'b0;
  21. reg [3:0] qw_count = 4'd0;
  22. wire qw_accept;
  23. wire [7:0] qw_status;
  24. wire [15:0] qw_detail;
  25. reg ctl_valid = 1'b0;
  26. reg [2:0] ctl_op = 3'd0;
  27. reg [15:0] ctl_axis_mask = 16'd0;
  28. reg [7:0] ctl_flags = 8'd0;
  29. reg [31:0] ctl_value = 32'd0;
  30. wire ctl_accept;
  31. wire [7:0] ctl_status;
  32. wire [15:0] ctl_detail;
  33. reg startup_seek = 1'b0;
  34. wire [7:0] ev_rd_index_w = 8'd0;
  35. wire [127:0] ev_rd_data;
  36. wire [7:0] ev_count, ev_overflow;
  37. wire [31:0] ev_time_us;
  38. wire [2:0] step_bus, dir_bus;
  39. // watch the engine time base through a tick generator
  40. always begin
  41. #(half_ticks * 10) tick_us = 1'b1;
  42. #10 tick_us = 1'b0;
  43. end
  44. motor_engine #(
  45. .AXIS_COUNT(3),
  46. .X_DEFAULT_HALF_US(32'd4), .Y_DEFAULT_HALF_US(32'd4), .Z_DEFAULT_HALF_US(32'd8),
  47. .X_MIN_HALF_US(32'd2), .Y_MIN_HALF_US(32'd2), .Z_MIN_HALF_US(32'd4),
  48. .DIR_SETUP_TICKS(3)
  49. ) dut (
  50. .clk(clk), .rst_n(rst_n), .tick_us(tick_us),
  51. .limit_lower_raw(limit_lower_raw), .limit_upper_raw(limit_upper_raw),
  52. .qw_byte_we(qw_byte_we), .qw_byte_addr(qw_byte_addr),
  53. .qw_byte_data(qw_byte_data), .qw_submit(qw_submit), .qw_count(qw_count),
  54. .qw_accept(qw_accept), .qw_status(qw_status), .qw_detail(qw_detail),
  55. .ctl_valid(ctl_valid), .ctl_op(ctl_op), .ctl_axis_mask(ctl_axis_mask),
  56. .ctl_flags(ctl_flags), .ctl_value(ctl_value),
  57. .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
  58. .startup_seek(startup_seek),
  59. .ev_rd_index(8'd0), .ev_hold(1'b0),
  60. .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
  61. .ev_time_us(ev_time_us),
  62. .ev_clear_n(1'b0), .ev_clear_count(8'd0),
  63. .axis_telemetry(),
  64. .step_out(step_bus), .dir_out(dir_bus)
  65. );
  66. integer i;
  67. reg [127:0] seg;
  68. task push_byte(input [6:0] a, input [7:0] d);
  69. begin
  70. @(negedge clk);
  71. qw_byte_addr = a; qw_byte_data = d; qw_byte_we = 1'b1;
  72. @(negedge clk);
  73. qw_byte_we = 1'b0;
  74. end
  75. endtask
  76. task send_seg(input [1:0] axis, input [2:0] mode, input [3:0] flags,
  77. input [31:0] count, input [31:0] half, input [15:0] tag, input integer slot);
  78. begin
  79. seg = {112'd0, tag, half, count, 4'd0, flags, 3'b0, mode, 2'b0, axis};
  80. for (i = 15; i >= 0; i = i - 1)
  81. push_byte(slot * 16 + i, seg[i*8 +: 8]);
  82. end
  83. endtask
  84. task submit(input [3:0] n);
  85. begin
  86. @(negedge clk);
  87. qw_count = n; qw_submit = 1'b1;
  88. @(negedge clk);
  89. qw_submit = 1'b0;
  90. wait (qw_accept);
  91. @(negedge clk);
  92. end
  93. endtask
  94. task ctl(input [2:0] op, input [15:0] mask, input [7:0] flags, input [31:0] value);
  95. begin
  96. @(negedge clk);
  97. ctl_op = op; ctl_axis_mask = mask; ctl_flags = flags; ctl_value = value;
  98. ctl_valid = 1'b1;
  99. @(negedge clk);
  100. ctl_valid = 1'b0;
  101. wait (ctl_accept);
  102. @(negedge clk);
  103. end
  104. endtask
  105. task check(input cond, input [255:0] name);
  106. if (!cond) begin
  107. $display("TB_FAIL: %0s", name);
  108. $fatal(1, "testbench assertion failed");
  109. end
  110. endtask
  111. initial begin
  112. repeat (5) @(posedge clk);
  113. rst_n = 1'b1;
  114. repeat (5) @(posedge clk);
  115. // ---- 1. power-on seek on all axes, then abort X only ----
  116. startup_seek = 1'b1;
  117. @(negedge clk);
  118. startup_seek = 1'b0;
  119. repeat (40) @(posedge clk);
  120. check(dut.g_axis[0].u_axis.active, "X seek active after power-on");
  121. check(dut.g_axis[1].u_axis.active, "Y seek active after power-on");
  122. check(dut.g_axis[2].u_axis.active, "Z seek active after power-on");
  123. check(dut.g_axis[0].u_axis.r_half_us == 32'd4, "X uses default half period");
  124. check(dut.g_axis[2].u_axis.r_half_us == 32'd8, "Z uses default half period");
  125. ctl(3'd1, 16'b001, 8'd0, 32'd0); // ABORT X
  126. repeat (10) @(posedge clk);
  127. check(!dut.g_axis[0].u_axis.active, "X aborted");
  128. check(dut.g_axis[1].u_axis.active, "Y unaffected by X abort");
  129. check(ev_count >= 8'd1, "abort event recorded");
  130. ctl(3'd1, 16'b111, 8'd0, 32'd0); // ABORT all
  131. repeat (10) @(posedge clk);
  132. // ---- 2. relative segment: 6 steps, final high pulse completes ----
  133. // X: axis 0, relative, dir lower (flags bit0=0), count 6, half 3us
  134. send_seg(2'd0, 3'd0, 4'd0, 32'd6, 32'd3, 16'h0042, 0);
  135. submit(4'd1);
  136. check(qw_status == 8'd0, "relative batch accepted");
  137. repeat (3000) @(posedge clk);
  138. check(dut.g_axis[0].u_axis.r_executed == 32'd6, "exactly 6 executed rises");
  139. check(!dut.g_axis[0].u_axis.active, "relative segment done");
  140. check(dut.g_axis[0].u_axis.last_stop_reason == 4'd1, "stop reason COUNT");
  141. check(dut.g_axis[0].u_axis.step_out == 1'b0, "step low after completion");
  142. // ---- 3. hold semantics ----
  143. // hold with half_period != 0 must be rejected whole: second segment
  144. // is invalid so the batch (and its valid first segment) is a no-op
  145. send_seg(2'd1, 3'd4, 4'd0, 32'd3, 32'd0, 16'h0001, 0); // valid hold
  146. send_seg(2'd1, 3'd0, 4'd0, 32'd0, 32'd3, 16'h0002, 1); // invalid: rel count 0
  147. submit(4'd2);
  148. check(qw_status == 8'd3, "bad batch rejected BAD_VALUE");
  149. check(dut.g_axis[1].u_axis.r_active == 1'b0 || dut.depth_r[1] == 6'd0,
  150. "rejected batch changed nothing");
  151. // valid hold alone completes after 3 us with no STEP
  152. send_seg(2'd1, 3'd4, 4'd0, 32'd3, 32'd0, 16'h0003, 0);
  153. submit(4'd1);
  154. check(qw_status == 8'd0, "hold batch accepted");
  155. check(dut.g_axis[1].u_axis.step_out == 1'b0, "hold emits no STEP");
  156. repeat (400) @(posedge clk);
  157. check(!dut.g_axis[1].u_axis.active, "hold completed");
  158. check(dut.g_axis[1].u_axis.last_stop_reason == 4'd9, "stop reason HOLD");
  159. // ---- 4. lower limit: termination, zero point, hit statistics ----
  160. // queue an unlimited lower seek on Y (direction toward lower)
  161. send_seg(2'd1, 3'd1, 4'd0, 32'd0, 32'd3, 16'h0007, 0);
  162. submit(4'd1);
  163. repeat (100) @(posedge clk);
  164. check(dut.g_axis[1].u_axis.active, "seek lower running");
  165. limit_lower_raw[1] = 1'b1;
  166. repeat (10) @(posedge clk);
  167. check(!dut.g_axis[1].u_axis.active, "seek terminated by lower limit");
  168. check(dut.g_axis[1].u_axis.last_stop_reason == 4'd2, "stop reason LOWER_LIMIT");
  169. check(dut.g_axis[1].u_axis.position == 32'd0, "position zeroed at reference");
  170. check(dut.g_axis[1].u_axis.r_ref_seen == 1'b1, "reference seen");
  171. check(dut.g_axis[1].u_axis.lower_hit_count == 16'd1, "one hit counted");
  172. // hold the limit high: no repeated counting while active
  173. repeat (2000) @(posedge clk);
  174. check(dut.g_axis[1].u_axis.lower_hit_count == 16'd1, "held limit counts once");
  175. limit_lower_raw[1] = 1'b0;
  176. repeat (10) @(posedge clk);
  177. check(dut.g_axis[1].u_axis.lower_hit_count == 16'd1, "release adds no count");
  178. // ---- 5. preempt: running segment replaced by urgent segment ----
  179. // X runs a long relative segment
  180. send_seg(2'd0, 3'd0, 4'd0, 32'd500, 32'd3, 16'h00A0, 0);
  181. submit(4'd1);
  182. repeat (500) @(posedge clk);
  183. check(dut.g_axis[0].u_axis.active, "long segment running");
  184. // preempting batch: first segment carries flags bit1; direction
  185. // flipped (upper) so the DIR change is observable in the wave
  186. send_seg(2'd0, 3'd0, 4'b0010, 32'd20, 32'd3, 16'h00A1, 0);
  187. submit(4'd1);
  188. repeat (30) @(posedge clk);
  189. check(dut.g_axis[0].u_axis.active, "preempted segment became active");
  190. check(dut.g_axis[0].u_axis.r_dir_upper == 1'b1, "urgent segment direction");
  191. repeat (4000) @(posedge clk);
  192. check(dut.g_axis[0].u_axis.r_executed == 32'd20, "preempted segment ran to completion");
  193. check(dut.g_axis[0].u_axis.last_stop_reason == 4'd1, "count complete after preempt");
  194. // ---- 6. event ring ----
  195. check(ev_count >= 8'd5, "several events recorded");
  196. check(ev_overflow == 8'd0, "no overflow at this depth");
  197. // clear-all is a control-side op; here just verify count stable
  198. // without writes while idle
  199. repeat (100) @(posedge clk);
  200. check(ev_count >= 8'd5, "records retained (read does not delete)");
  201. $display("motor_engine_tb PASS");
  202. $finish;
  203. end
  204. endmodule