prod2u_mrdimm_final_e2e_tb.v 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. `timescale 1ns/1ps
  2. // Full E2E bit-level simulation testbench for MRDIMM Final Architecture.
  3. module prod2u_mrdimm_final_e2e_tb;
  4. localparam integer BIT_CLKS = 27; // 921600 baud @ 25 MHz
  5. reg clk = 1'b0;
  6. reg rst_n = 1'b0;
  7. reg rs232_rx = 1'b1;
  8. wire rs232_tx;
  9. reg [2:0] limit_lower_raw = 3'd0;
  10. reg [2:0] limit_upper_raw = 3'd0;
  11. wire [7:0] ptc_pwm_out;
  12. wire [3:0] fan_pwm_out;
  13. wire motor_en_n;
  14. wire x_step, x_dir;
  15. wire y_step, y_dir;
  16. wire z_step, z_dir;
  17. always #20 clk = ~clk; // 25 MHz
  18. prod2u_mrdimm_final_top #(
  19. .CLOCK_HZ(25000000),
  20. .HOST_BAUD(921600),
  21. .BMC_BAUD(115200),
  22. .PWM_PERIOD_CLKS(5000),
  23. .PWM_CLKS_PER_PERCENT(50),
  24. .HOST_TIMEOUT_CYCLES(2500)
  25. ) dut (
  26. .sys_clk(clk),
  27. .host_rs232_rx(rs232_rx),
  28. .host_rs232_tx(rs232_tx),
  29. .host_ttl_rx(1'b1),
  30. .host_ttl_tx(),
  31. .bmc_rs232_rx(1'b1),
  32. .bmc_rs232_tx(),
  33. .temp_scl(),
  34. .temp_sda(),
  35. .limit_lower_raw(limit_lower_raw),
  36. .limit_upper_raw(limit_upper_raw),
  37. .ptc_pwm_out(ptc_pwm_out),
  38. .fan_pwm_out(fan_pwm_out),
  39. .motor_en_n(motor_en_n),
  40. .x_step(x_step), .x_dir(x_dir),
  41. .y_step(y_step), .y_dir(y_dir),
  42. .z_step(z_step), .z_dir(z_dir)
  43. );
  44. // Byte-wise CRC over TYPE, LENGTH and PAYLOAD (excludes header A5 5A)
  45. reg [7:0] frame_bytes [0:255];
  46. function [15:0] crc_over_frame;
  47. input integer count;
  48. integer i;
  49. integer b;
  50. reg [15:0] c;
  51. reg [7:0] d;
  52. begin
  53. c = 16'hFFFF;
  54. for (i = 2; i < count; i = i + 1) begin
  55. d = frame_bytes[i];
  56. for (b = 7; b >= 0; b = b - 1)
  57. c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021)
  58. : {c[14:0], 1'b0};
  59. end
  60. crc_over_frame = c;
  61. end
  62. endfunction
  63. // UART TX to DUT
  64. task uart_send_byte;
  65. input [7:0] value;
  66. integer b;
  67. begin
  68. rs232_rx = 1'b0;
  69. repeat (BIT_CLKS) @(posedge clk);
  70. for (b = 0; b < 8; b = b + 1) begin
  71. rs232_rx = value[b];
  72. repeat (BIT_CLKS) @(posedge clk);
  73. end
  74. rs232_rx = 1'b1;
  75. repeat (BIT_CLKS) @(posedge clk);
  76. end
  77. endtask
  78. task uart_send_frame;
  79. input integer n;
  80. integer i;
  81. reg [15:0] c;
  82. begin
  83. c = crc_over_frame(n);
  84. for (i = 0; i < n; i = i + 1)
  85. uart_send_byte(frame_bytes[i]);
  86. uart_send_byte(c[7:0]);
  87. uart_send_byte(c[15:8]);
  88. end
  89. endtask
  90. // UART RX from DUT
  91. reg [7:0] rx_bytes [0:511];
  92. integer rx_count = 0;
  93. reg [3:0] rx_bit = 0;
  94. reg [7:0] rx_shift = 0;
  95. integer rx_clk_cnt = 0;
  96. reg rx_busy = 0;
  97. initial begin
  98. forever begin
  99. @(posedge clk);
  100. if (!rx_busy && rs232_tx === 1'b0) begin
  101. rx_busy = 1'b1;
  102. rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  103. rx_bit = 0;
  104. end else if (rx_busy) begin
  105. if (rx_clk_cnt == 0) begin
  106. if (rx_bit < 8) begin
  107. rx_shift[rx_bit] = rs232_tx;
  108. rx_bit = rx_bit + 1;
  109. rx_clk_cnt = BIT_CLKS - 1;
  110. end else begin
  111. rx_bytes[rx_count] = rx_shift;
  112. rx_count = rx_count + 1;
  113. rx_busy = 1'b0;
  114. end
  115. end else begin
  116. rx_clk_cnt = rx_clk_cnt - 1;
  117. end
  118. end
  119. end
  120. end
  121. // Step edge counters
  122. integer x_steps_count = 0;
  123. integer y_steps_count = 0;
  124. integer z_steps_count = 0;
  125. always @(posedge x_step) x_steps_count = x_steps_count + 1;
  126. always @(posedge y_step) y_steps_count = y_steps_count + 1;
  127. always @(posedge z_step) z_steps_count = z_steps_count + 1;
  128. // Timeout guard
  129. initial begin
  130. #30000000; // 30ms simulation time
  131. $display("FATAL: Simulation timeout!");
  132. $fflush;
  133. $finish;
  134. end
  135. integer start_idx;
  136. integer i;
  137. initial begin
  138. $display("=== STARTING MRDIMM FINAL E2E TESTBENCH ===");
  139. $fflush;
  140. rst_n = 1'b0;
  141. limit_lower_raw = 3'b000;
  142. limit_upper_raw = 3'b000;
  143. repeat (100) @(posedge clk);
  144. rst_n = 1'b1;
  145. repeat (2000) @(posedge clk);
  146. // --------------------------------------------------------------------
  147. // TEST A: Legal GET_ALL at reset
  148. // --------------------------------------------------------------------
  149. $display("[TEST A] Sending GET_ALL request...");
  150. $fflush;
  151. frame_bytes[0] = 8'hA5;
  152. frame_bytes[1] = 8'h5A;
  153. frame_bytes[2] = 8'h02; // GET_ALL
  154. frame_bytes[3] = 8'h00; // LEN 0
  155. start_idx = rx_count;
  156. uart_send_frame(4);
  157. // Expect GET_ACK (6 bytes: A5 5A 82 00 F5 60) + GET_DATA (69 bytes: A5 5A 42 3F ... CRC)
  158. // Total bytes = 6 + 69 = 75 bytes
  159. wait(rx_count >= start_idx + 75);
  160. $display(" -> Received 75 bytes from GET_ALL");
  161. $fflush;
  162. // Verify GET_ACK
  163. if (rx_bytes[start_idx] !== 8'hA5 || rx_bytes[start_idx+1] !== 8'h5A ||
  164. rx_bytes[start_idx+2] !== 8'h82 || rx_bytes[start_idx+3] !== 8'h00) begin
  165. $fatal(1, "FAIL A: GET_ACK mismatch: %02h %02h %02h %02h",
  166. rx_bytes[start_idx], rx_bytes[start_idx+1], rx_bytes[start_idx+2], rx_bytes[start_idx+3]);
  167. end
  168. $display(" -> Verified GET_ACK (82 00)");
  169. $fflush;
  170. // Verify GET_DATA
  171. start_idx = start_idx + 6;
  172. if (rx_bytes[start_idx] !== 8'hA5 || rx_bytes[start_idx+1] !== 8'h5A ||
  173. rx_bytes[start_idx+2] !== 8'h42 || rx_bytes[start_idx+3] !== 8'h3F) begin
  174. $fatal(1, "FAIL A: GET_DATA header mismatch: %02h %02h %02h %02h",
  175. rx_bytes[start_idx], rx_bytes[start_idx+1], rx_bytes[start_idx+2], rx_bytes[start_idx+3]);
  176. end
  177. // Check FAN 0..3 defaults = 20 (0x14) at payload 0..3 (indices start_idx+4 .. +7)
  178. if (rx_bytes[start_idx+4] !== 8'd20 || rx_bytes[start_idx+5] !== 8'd20 ||
  179. rx_bytes[start_idx+6] !== 8'd20 || rx_bytes[start_idx+7] !== 8'd20) begin
  180. $fatal(1, "FAIL A: fan defaults wrong");
  181. end
  182. // Check PTC 0..3 defaults = 0 at payload 4..7 (indices start_idx+8 .. +11)
  183. if (rx_bytes[start_idx+8] !== 8'd0 || rx_bytes[start_idx+9] !== 8'd0 ||
  184. rx_bytes[start_idx+10] !== 8'd0 || rx_bytes[start_idx+11] !== 8'd0) begin
  185. $fatal(1, "FAIL A: ptc defaults wrong");
  186. end
  187. $display(" -> TEST A PASSED! GET_ACK and GET_DATA verified.");
  188. $fflush;
  189. // --------------------------------------------------------------------
  190. // TEST B: SET_ALL with PUSH_DOWN on Axis 0 (X) for 5 steps
  191. // --------------------------------------------------------------------
  192. $display("[TEST B] Sending SET_ALL: Axis 0 PUSH_DOWN 5 steps (half_period 20us)...");
  193. $fflush;
  194. frame_bytes[0] = 8'hA5;
  195. frame_bytes[1] = 8'h5A;
  196. frame_bytes[2] = 8'h01; // SET_ALL
  197. frame_bytes[3] = 8'd68; // LEN 68
  198. // Payload 0..3: FAN 20
  199. frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20;
  200. // Payload 4..7: PTC 0
  201. frame_bytes[8] = 8'd0; frame_bytes[9] = 8'd0; frame_bytes[10] = 8'd0; frame_bytes[11] = 8'd0;
  202. // Axis 0: payload 8..12 (frame_bytes 12..16)
  203. frame_bytes[12] = 8'h01; // action PUSH_DOWN
  204. frame_bytes[13] = 8'd5; // steps low
  205. frame_bytes[14] = 8'd0; // steps high
  206. frame_bytes[15] = 8'd20; // half_period low = 20 us
  207. frame_bytes[16] = 8'd0; // half_period high
  208. // Axis 1..11: NOTHING (0)
  209. for (i = 17; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
  210. start_idx = rx_count;
  211. x_steps_count = 0;
  212. uart_send_frame(72);
  213. // Expect SET_ACK (6 bytes: A5 5A 81 00 A6 35)
  214. wait(rx_count >= start_idx + 6);
  215. if (rx_bytes[start_idx+2] !== 8'h81 || rx_bytes[start_idx+3] !== 8'h00) begin
  216. $fatal(1, "FAIL B: expected SET_ACK (81 00), got %02h %02h", rx_bytes[start_idx+2], rx_bytes[start_idx+3]);
  217. end
  218. $display(" -> Verified SET_ACK (81 00)");
  219. $fflush;
  220. // Expect MOTOR_DONE (10 bytes: A5 5A 50 04 <axis> <reason> <steps_lo> <steps_hi> <crc_lo> <crc_hi>)
  221. wait(rx_count >= start_idx + 6 + 10);
  222. start_idx = start_idx + 6;
  223. if (rx_bytes[start_idx+2] !== 8'h50 || rx_bytes[start_idx+3] !== 8'h04) begin
  224. $fatal(1, "FAIL B: expected MOTOR_DONE (50 04), got %02h %02h", rx_bytes[start_idx+2], rx_bytes[start_idx+3]);
  225. end
  226. $display(" -> Got MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d",
  227. rx_bytes[start_idx+4], rx_bytes[start_idx+5], {rx_bytes[start_idx+7], rx_bytes[start_idx+6]});
  228. $fflush;
  229. if (rx_bytes[start_idx+4] !== 8'd0) $fatal(1, "FAIL B: wrong axis, expected 0");
  230. if (rx_bytes[start_idx+5] !== 8'd0) $fatal(1, "FAIL B: wrong reason, expected 0");
  231. if ({rx_bytes[start_idx+7], rx_bytes[start_idx+6]} !== 16'd5) $fatal(1, "FAIL B: wrong steps_done, expected 5");
  232. if (x_steps_count !== 5) $fatal(1, "FAIL B: x_step edge count mismatch, expected 5 got %0d", x_steps_count);
  233. $display(" -> TEST B PASSED! Exactly 5 pulses emitted on x_step and completed normally.");
  234. $fflush;
  235. // --------------------------------------------------------------------
  236. // TEST C: Limit switch triggered during motion on Axis 1 (Y)
  237. // --------------------------------------------------------------------
  238. $display("[TEST C] Sending SET_ALL: Axis 1 PUSH_DOWN 50 steps, tripping limit...");
  239. $fflush;
  240. // Axis 0: NOTHING
  241. frame_bytes[12] = 8'd0; frame_bytes[13] = 8'd0; frame_bytes[14] = 8'd0; frame_bytes[15] = 8'd0; frame_bytes[16] = 8'd0;
  242. // Axis 1: payload 13..17 (frame_bytes 17..21)
  243. frame_bytes[17] = 8'h01; // action PUSH_DOWN
  244. frame_bytes[18] = 8'd50; // 50 steps
  245. frame_bytes[19] = 8'd0;
  246. frame_bytes[20] = 8'd20; // 20 us
  247. frame_bytes[21] = 8'd0;
  248. start_idx = rx_count;
  249. y_steps_count = 0;
  250. uart_send_frame(72);
  251. // Wait for SET_ACK
  252. wait(rx_count >= start_idx + 6);
  253. $display(" -> Got SET_ACK, motor Y running...");
  254. $fflush;
  255. // Wait for 3 steps, then assert lower limit
  256. wait(y_steps_count >= 3);
  257. @(posedge clk);
  258. $display(" -> Asserting Axis 1 lower limit at y_steps_count=%0d", y_steps_count);
  259. $fflush;
  260. limit_lower_raw[1] = 1'b1;
  261. // Wait for MOTOR_DONE
  262. wait(rx_count >= start_idx + 6 + 10);
  263. start_idx = start_idx + 6;
  264. $display(" -> Got MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d",
  265. rx_bytes[start_idx+4], rx_bytes[start_idx+5], {rx_bytes[start_idx+7], rx_bytes[start_idx+6]});
  266. $fflush;
  267. if (rx_bytes[start_idx+4] !== 8'd1) $fatal(1, "FAIL C: wrong axis, expected 1");
  268. if (rx_bytes[start_idx+5] !== 8'd1) $fatal(1, "FAIL C: wrong reason, expected 1 (lower limit)");
  269. if ({rx_bytes[start_idx+7], rx_bytes[start_idx+6]} !== y_steps_count) begin
  270. $fatal(1, "FAIL C: steps_done mismatch: got %0d, counted %0d",
  271. {rx_bytes[start_idx+7], rx_bytes[start_idx+6]}, y_steps_count);
  272. end
  273. $display(" -> TEST C PASSED! Limit aborted motion cleanly.");
  274. $fflush;
  275. limit_lower_raw[1] = 1'b0;
  276. // --------------------------------------------------------------------
  277. // TEST D: Blocked at start on Axis 2 (Z)
  278. // --------------------------------------------------------------------
  279. $display("[TEST D] Asserting upper limit on Axis 2, then sending PUSH_UP...");
  280. $fflush;
  281. limit_upper_raw[2] = 1'b1;
  282. repeat (100) @(posedge clk);
  283. // Axis 1: NOTHING
  284. frame_bytes[17] = 8'd0; frame_bytes[18] = 8'd0; frame_bytes[19] = 8'd0; frame_bytes[20] = 8'd0; frame_bytes[21] = 8'd0;
  285. // Axis 2: payload 18..22 (frame_bytes 22..26)
  286. frame_bytes[22] = 8'h02; // PUSH_UP
  287. frame_bytes[23] = 8'd50; // 50 steps
  288. frame_bytes[24] = 8'd0;
  289. frame_bytes[25] = 8'd20; // 20 us
  290. frame_bytes[26] = 8'd0;
  291. start_idx = rx_count;
  292. z_steps_count = 0;
  293. uart_send_frame(72);
  294. // Wait for SET_ACK + MOTOR_DONE (16 bytes)
  295. wait(rx_count >= start_idx + 16);
  296. $display(" -> Got SET_ACK and MOTOR_DONE");
  297. start_idx = start_idx + 6;
  298. $display(" -> MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d",
  299. rx_bytes[start_idx+4], rx_bytes[start_idx+5], {rx_bytes[start_idx+7], rx_bytes[start_idx+6]});
  300. $fflush;
  301. if (rx_bytes[start_idx+4] !== 8'd2) $fatal(1, "FAIL D: wrong axis, expected 2");
  302. if (rx_bytes[start_idx+5] !== 8'd2) $fatal(1, "FAIL D: wrong reason, expected 2 (upper limit)");
  303. if ({rx_bytes[start_idx+7], rx_bytes[start_idx+6]} !== 16'd0) $fatal(1, "FAIL D: expected 0 steps done");
  304. if (z_steps_count !== 0) $fatal(1, "FAIL D: z_step emitted pulses when blocked!");
  305. $display(" -> TEST D PASSED! Emitted 0 pulses when blocked at start.");
  306. $fflush;
  307. limit_upper_raw[2] = 1'b0;
  308. // --------------------------------------------------------------------
  309. // TEST E: STOP_CLEAR on Axis 0
  310. // --------------------------------------------------------------------
  311. $display("[TEST E] Starting long move on Axis 0, then issuing STOP_CLEAR...");
  312. $fflush;
  313. // Axis 2: NOTHING
  314. frame_bytes[22] = 8'd0; frame_bytes[23] = 8'd0; frame_bytes[24] = 8'd0; frame_bytes[25] = 8'd0; frame_bytes[26] = 8'd0;
  315. // Axis 0: 1000 steps
  316. frame_bytes[12] = 8'h01; // PUSH_DOWN
  317. frame_bytes[13] = 8'hE8; // 1000 steps low
  318. frame_bytes[14] = 8'h03; // 1000 steps high
  319. frame_bytes[15] = 8'd20; // 20 us
  320. frame_bytes[16] = 8'd0;
  321. start_idx = rx_count;
  322. x_steps_count = 0;
  323. uart_send_frame(72);
  324. wait(rx_count >= start_idx + 6);
  325. $display(" -> Got SET_ACK, motor running...");
  326. $fflush;
  327. wait(x_steps_count >= 5);
  328. $display(" -> Sending STOP_CLEAR (action=0xFF) for Axis 0...");
  329. $fflush;
  330. frame_bytes[12] = 8'hFF; // STOP_CLEAR
  331. frame_bytes[13] = 8'd0; frame_bytes[14] = 8'd0; frame_bytes[15] = 8'd0; frame_bytes[16] = 8'd0;
  332. start_idx = rx_count;
  333. uart_send_frame(72);
  334. wait(rx_count >= start_idx + 6);
  335. $display(" -> Got SET_ACK for STOP_CLEAR");
  336. $fflush;
  337. repeat (2000) @(posedge clk);
  338. if (x_step !== 1'b0) $fatal(1, "FAIL E: x_step not low after STOP_CLEAR");
  339. $display(" -> TEST E PASSED! Motor stopped immediately and queue cleared.");
  340. $fflush;
  341. // --------------------------------------------------------------------
  342. // ALL TESTS PASSED!
  343. // --------------------------------------------------------------------
  344. $display("\n==================================================");
  345. $display("PROD2U MRDIMM FINAL E2E TESTBENCH: ALL TESTS PASS!");
  346. $display("==================================================");
  347. $fflush;
  348. $finish;
  349. end
  350. endmodule