prod2u_mrdimm_ttl_e2e_tb.v 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. `timescale 1ns/1ps
  2. // Full E2E bit-level simulation testbench for MRDIMM Host TTL Interface (CON146: Pin1/H6=TX, Pin3/N7=RX, Pin2/4=GND).
  3. //
  4. // This testbench verifies:
  5. // 1. Bit-level UART communication at 921600 baud (25 MHz clock) through host_ttl_rx.
  6. // 2. Broadcast reply transmission through host_ttl_tx (and mirrored on host_rs232_tx).
  7. // 3. Complete GET_ALL / GET_ACK / GET_DATA cycle initiated over TTL.
  8. // 4. SET_ALL cycle over TTL with motor movement (Axis 0) and thermal PWM duty changes.
  9. // 5. Limit detection and clean MOTOR_DONE reporting over TTL.
  10. // 6. STOP_CLEAR abort over TTL.
  11. // 7. Interleaved dual-port operation: TTL and RS232 taking turns seamlessly.
  12. // 8. Corrupted / Bad CRC frame recovery over TTL.
  13. module prod2u_mrdimm_ttl_e2e_tb;
  14. localparam integer BIT_CLKS = 27; // 921600 baud @ 25 MHz
  15. reg clk = 1'b0;
  16. reg rst_n = 1'b0;
  17. // Both UART lines
  18. reg rs232_rx = 1'b1;
  19. wire rs232_tx;
  20. reg ttl_rx = 1'b1;
  21. wire ttl_tx;
  22. reg [11:0] limit_lower_raw = 12'd0;
  23. reg [11:0] limit_upper_raw = 12'd0;
  24. reg hood_switch_in = 1'b0;
  25. wire buzzer_out;
  26. wire [23:0] ptc_pwm_out;
  27. wire [3:0] fan_pwm_out;
  28. wire motor_en_n;
  29. wire [11:0] motor_step_gate;
  30. wire [11:0] motor_dir_gate;
  31. wire x_step = motor_step_gate[0];
  32. wire x_dir = motor_dir_gate[0];
  33. wire y_step = motor_step_gate[1];
  34. wire y_dir = motor_dir_gate[1];
  35. wire z_step = motor_step_gate[2];
  36. wire z_dir = motor_dir_gate[2];
  37. always #20 clk = ~clk; // 25 MHz (40ns period)
  38. prod2u_mrdimm_final_top #(
  39. .CLOCK_HZ(25000000),
  40. .HOST_BAUD(921600),
  41. .BMC_BAUD(115200),
  42. .PWM_PERIOD_CLKS(5000),
  43. .PWM_CLKS_PER_PERCENT(50),
  44. .HOST_TIMEOUT_CYCLES(2500)
  45. ) dut (
  46. .sys_clk(clk),
  47. .host_rs232_rx(rs232_rx),
  48. .host_rs232_tx(rs232_tx),
  49. .host_ttl_rx(ttl_rx),
  50. .host_ttl_tx(ttl_tx),
  51. .bmc_rs232_rx(1'b1),
  52. .bmc_rs232_tx(),
  53. .temp_scl(),
  54. .temp_sda(),
  55. .limit_lower_raw(limit_lower_raw),
  56. .limit_upper_raw(limit_upper_raw),
  57. .ptc_pwm_out(ptc_pwm_out),
  58. .fan_pwm_out(fan_pwm_out),
  59. .motor_en_n(motor_en_n),
  60. .motor_step_gate(motor_step_gate),
  61. .motor_dir_gate(motor_dir_gate),
  62. .hood_switch_in(hood_switch_in),
  63. .buzzer_out(buzzer_out)
  64. );
  65. // Byte-wise CRC over TYPE, LENGTH and PAYLOAD (excludes header A5 5A)
  66. reg [7:0] frame_bytes [0:255];
  67. function [15:0] crc_over_frame;
  68. input integer count;
  69. integer i;
  70. integer b;
  71. reg [15:0] c;
  72. reg [7:0] d;
  73. begin
  74. c = 16'hFFFF;
  75. for (i = 2; i < count; i = i + 1) begin
  76. d = frame_bytes[i];
  77. for (b = 7; b >= 0; b = b - 1)
  78. c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021)
  79. : {c[14:0], 1'b0};
  80. end
  81. crc_over_frame = c;
  82. end
  83. endfunction
  84. // UART TX byte task to TTL RX
  85. task ttl_send_byte;
  86. input [7:0] value;
  87. integer b;
  88. begin
  89. ttl_rx = 1'b0;
  90. repeat (BIT_CLKS) @(posedge clk);
  91. for (b = 0; b < 8; b = b + 1) begin
  92. ttl_rx = value[b];
  93. repeat (BIT_CLKS) @(posedge clk);
  94. end
  95. ttl_rx = 1'b1;
  96. repeat (BIT_CLKS) @(posedge clk);
  97. end
  98. endtask
  99. // UART TX frame task to TTL RX
  100. task ttl_send_frame;
  101. input integer n;
  102. integer i;
  103. reg [15:0] c;
  104. begin
  105. c = crc_over_frame(n);
  106. for (i = 0; i < n; i = i + 1)
  107. ttl_send_byte(frame_bytes[i]);
  108. ttl_send_byte(c[7:0]);
  109. ttl_send_byte(c[15:8]);
  110. end
  111. endtask
  112. // UART TX frame task to RS232 RX (for dual-port tests)
  113. task rs232_send_byte;
  114. input [7:0] value;
  115. integer b;
  116. begin
  117. rs232_rx = 1'b0;
  118. repeat (BIT_CLKS) @(posedge clk);
  119. for (b = 0; b < 8; b = b + 1) begin
  120. rs232_rx = value[b];
  121. repeat (BIT_CLKS) @(posedge clk);
  122. end
  123. rs232_rx = 1'b1;
  124. repeat (BIT_CLKS) @(posedge clk);
  125. end
  126. endtask
  127. task rs232_send_frame;
  128. input integer n;
  129. integer i;
  130. reg [15:0] c;
  131. begin
  132. c = crc_over_frame(n);
  133. for (i = 0; i < n; i = i + 1)
  134. rs232_send_byte(frame_bytes[i]);
  135. rs232_send_byte(c[7:0]);
  136. rs232_send_byte(c[15:8]);
  137. end
  138. endtask
  139. // UART RX monitor from TTL TX
  140. reg [7:0] ttl_rx_bytes [0:511];
  141. integer ttl_rx_count = 0;
  142. reg [3:0] ttl_rx_bit = 0;
  143. reg [7:0] ttl_rx_shift = 0;
  144. integer ttl_rx_clk_cnt = 0;
  145. reg ttl_rx_busy = 0;
  146. initial begin
  147. forever begin
  148. @(posedge clk);
  149. if (!ttl_rx_busy && ttl_tx === 1'b0) begin
  150. ttl_rx_busy = 1'b1;
  151. ttl_rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  152. ttl_rx_bit = 0;
  153. end else if (ttl_rx_busy) begin
  154. if (ttl_rx_clk_cnt == 0) begin
  155. if (ttl_rx_bit < 8) begin
  156. ttl_rx_shift[ttl_rx_bit] = ttl_tx;
  157. ttl_rx_bit = ttl_rx_bit + 1;
  158. ttl_rx_clk_cnt = BIT_CLKS - 1;
  159. end else begin
  160. ttl_rx_bytes[ttl_rx_count] = ttl_rx_shift;
  161. ttl_rx_count = ttl_rx_count + 1;
  162. ttl_rx_busy = 1'b0;
  163. end
  164. end else begin
  165. ttl_rx_clk_cnt = ttl_rx_clk_cnt - 1;
  166. end
  167. end
  168. end
  169. end
  170. // UART RX monitor from RS232 TX (to verify TX broadcast)
  171. reg [7:0] rs232_rx_bytes [0:511];
  172. integer rs232_rx_count = 0;
  173. reg [3:0] rs232_rx_bit = 0;
  174. reg [7:0] rs232_rx_shift = 0;
  175. integer rs232_rx_clk_cnt = 0;
  176. reg rs232_rx_busy = 0;
  177. initial begin
  178. forever begin
  179. @(posedge clk);
  180. if (!rs232_rx_busy && rs232_tx === 1'b0) begin
  181. rs232_rx_busy = 1'b1;
  182. rs232_rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  183. rs232_rx_bit = 0;
  184. end else if (rs232_rx_busy) begin
  185. if (rs232_rx_clk_cnt == 0) begin
  186. if (rs232_rx_bit < 8) begin
  187. rs232_rx_shift[rs232_rx_bit] = rs232_tx;
  188. rs232_rx_bit = rs232_rx_bit + 1;
  189. rs232_rx_clk_cnt = BIT_CLKS - 1;
  190. end else begin
  191. rs232_rx_bytes[rs232_rx_count] = rs232_rx_shift;
  192. rs232_rx_count = rs232_rx_count + 1;
  193. rs232_rx_busy = 1'b0;
  194. end
  195. end else begin
  196. rs232_rx_clk_cnt = rs232_rx_clk_cnt - 1;
  197. end
  198. end
  199. end
  200. end
  201. // Step edge counters
  202. integer x_steps_count = 0;
  203. integer y_steps_count = 0;
  204. integer z_steps_count = 0;
  205. always @(posedge x_step) x_steps_count = x_steps_count + 1;
  206. always @(posedge y_step) y_steps_count = y_steps_count + 1;
  207. always @(posedge z_step) z_steps_count = z_steps_count + 1;
  208. // Timeout guard
  209. initial begin
  210. #40000000; // 40ms simulation time
  211. $display("FATAL: Simulation timeout!");
  212. $fflush;
  213. $finish;
  214. end
  215. integer start_idx;
  216. integer i;
  217. initial begin
  218. $display("==================================================");
  219. $display("=== STARTING MRDIMM HOST TTL E2E TESTBENCH ===");
  220. $display("=== Port: CON146 (H6=TX, N7=RX) @ 921600 8N1 ===");
  221. $display("==================================================");
  222. $fflush;
  223. rst_n = 1'b0;
  224. limit_lower_raw = 3'b000;
  225. limit_upper_raw = 3'b000;
  226. repeat (100) @(posedge clk);
  227. rst_n = 1'b1;
  228. repeat (2000) @(posedge clk);
  229. // --------------------------------------------------------------------
  230. // TEST 1: Legal GET_ALL sent over host_ttl_rx
  231. // --------------------------------------------------------------------
  232. $display("[TEST 1] Sending GET_ALL over host_ttl_rx...");
  233. $fflush;
  234. frame_bytes[0] = 8'hA5;
  235. frame_bytes[1] = 8'h5A;
  236. frame_bytes[2] = 8'h02; // GET_ALL
  237. frame_bytes[3] = 8'h00; // LEN 0
  238. start_idx = ttl_rx_count;
  239. ttl_send_frame(4);
  240. // Expect GET_ACK (6 bytes: A5 5A 82 00 F5 60) + GET_DATA (71 bytes: A5 5A 42 41 ... CRC)
  241. // Total bytes = 77 bytes
  242. wait(ttl_rx_count >= start_idx + 77);
  243. $display(" -> Received 77 bytes from GET_ALL on host_ttl_tx");
  244. $fflush;
  245. // Verify GET_ACK on TTL TX
  246. if (ttl_rx_bytes[start_idx] !== 8'hA5 || ttl_rx_bytes[start_idx+1] !== 8'h5A ||
  247. ttl_rx_bytes[start_idx+2] !== 8'h82 || ttl_rx_bytes[start_idx+3] !== 8'h00) begin
  248. $fatal(1, "FAIL 1: GET_ACK mismatch on ttl_tx: %02h %02h %02h %02h",
  249. ttl_rx_bytes[start_idx], ttl_rx_bytes[start_idx+1],
  250. ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
  251. end
  252. $display(" -> Verified GET_ACK (82 00) on host_ttl_tx");
  253. // Verify GET_DATA header on TTL TX
  254. start_idx = start_idx + 6;
  255. if (ttl_rx_bytes[start_idx] !== 8'hA5 || ttl_rx_bytes[start_idx+1] !== 8'h5A ||
  256. ttl_rx_bytes[start_idx+2] !== 8'h42 || ttl_rx_bytes[start_idx+3] !== 8'h41) begin
  257. $fatal(1, "FAIL 1: GET_DATA header mismatch on ttl_tx: %02h %02h %02h %02h",
  258. ttl_rx_bytes[start_idx], ttl_rx_bytes[start_idx+1],
  259. ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
  260. end
  261. // Verify FAN default = 20% on all 4 channels
  262. if (ttl_rx_bytes[start_idx+4] !== 8'd20 || ttl_rx_bytes[start_idx+5] !== 8'd20 ||
  263. ttl_rx_bytes[start_idx+6] !== 8'd20 || ttl_rx_bytes[start_idx+7] !== 8'd20) begin
  264. $fatal(1, "FAIL 1: default fan duty wrong on ttl_tx");
  265. end
  266. // Verify PTC default = 0% on all 4 channels
  267. if (ttl_rx_bytes[start_idx+8] !== 8'd0 || ttl_rx_bytes[start_idx+9] !== 8'd0 ||
  268. ttl_rx_bytes[start_idx+10] !== 8'd0 || ttl_rx_bytes[start_idx+11] !== 8'd0) begin
  269. $fatal(1, "FAIL 1: default ptc duty wrong on ttl_tx");
  270. end
  271. // Verify hood_switch=0, buzzer=0 defaults
  272. if (ttl_rx_bytes[start_idx+4+63] !== 8'd0 || ttl_rx_bytes[start_idx+4+64] !== 8'd0) begin
  273. $fatal(1, "FAIL 1: default hood/buzzer wrong on ttl_tx");
  274. end
  275. // Verify TX broadcast: rs232_tx must receive the exact same 77 bytes!
  276. wait(rs232_rx_count >= 77);
  277. for (i = 0; i < 77; i = i + 1) begin
  278. if (rs232_rx_bytes[i] !== ttl_rx_bytes[i]) begin
  279. $fatal(1, "FAIL 1: Broadcast mismatch at byte %0d: ttl=%02h, rs232=%02h",
  280. i, ttl_rx_bytes[i], rs232_rx_bytes[i]);
  281. end
  282. end
  283. $display(" -> Verified TX Broadcast: rs232_tx perfectly matches ttl_tx (77 bytes)!");
  284. $display(" -> TEST 1 PASSED!");
  285. $fflush;
  286. // --------------------------------------------------------------------
  287. // TEST 2: SET_ALL sent over host_ttl_rx (FAN 50%, PTC 30%, Axis 0 6 steps)
  288. // --------------------------------------------------------------------
  289. $display("[TEST 2] Sending SET_ALL over host_ttl_rx: FAN 50%%, PTC 30%%, Axis 0 PUSH_DOWN 6 steps...");
  290. $fflush;
  291. frame_bytes[0] = 8'hA5;
  292. frame_bytes[1] = 8'h5A;
  293. frame_bytes[2] = 8'h01; // SET_ALL
  294. frame_bytes[3] = 8'd68; // LEN 68
  295. // Payload 0..3: FAN 50%
  296. frame_bytes[4] = 8'd50; frame_bytes[5] = 8'd50; frame_bytes[6] = 8'd50; frame_bytes[7] = 8'd50;
  297. // Payload 4..7: PTC 30%
  298. frame_bytes[8] = 8'd30; frame_bytes[9] = 8'd30; frame_bytes[10] = 8'd30; frame_bytes[11] = 8'd30;
  299. // Axis 0: payload 8..12
  300. frame_bytes[12] = 8'h01; // action PUSH_DOWN
  301. frame_bytes[13] = 8'd6; // steps low = 6
  302. frame_bytes[14] = 8'd0; // steps high
  303. frame_bytes[15] = 8'd20; // half_period low = 20 us
  304. frame_bytes[16] = 8'd0; // half_period high
  305. // Axis 1..11: NOTHING
  306. for (i = 17; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
  307. start_idx = ttl_rx_count;
  308. x_steps_count = 0;
  309. ttl_send_frame(72);
  310. // Expect SET_ACK (6 bytes: A5 5A 81 00 A6 35) on TTL TX
  311. wait(ttl_rx_count >= start_idx + 6);
  312. if (ttl_rx_bytes[start_idx+2] !== 8'h81 || ttl_rx_bytes[start_idx+3] !== 8'h00) begin
  313. $fatal(1, "FAIL 2: expected SET_ACK (81 00), got %02h %02h",
  314. ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
  315. end
  316. $display(" -> Verified SET_ACK (81 00) on host_ttl_tx");
  317. $fflush;
  318. // Expect MOTOR_DONE (10 bytes) on TTL TX
  319. wait(ttl_rx_count >= start_idx + 6 + 10);
  320. start_idx = start_idx + 6;
  321. if (ttl_rx_bytes[start_idx+2] !== 8'h50 || ttl_rx_bytes[start_idx+3] !== 8'h04) begin
  322. $fatal(1, "FAIL 2: expected MOTOR_DONE (50 04), got %02h %02h",
  323. ttl_rx_bytes[start_idx+2], ttl_rx_bytes[start_idx+3]);
  324. end
  325. $display(" -> Got MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d",
  326. ttl_rx_bytes[start_idx+4], ttl_rx_bytes[start_idx+5],
  327. {ttl_rx_bytes[start_idx+7], ttl_rx_bytes[start_idx+6]});
  328. $fflush;
  329. if (ttl_rx_bytes[start_idx+4] !== 8'd0) $fatal(1, "FAIL 2: wrong axis, expected 0");
  330. if (ttl_rx_bytes[start_idx+5] !== 8'd0) $fatal(1, "FAIL 2: wrong reason, expected 0 (complete)");
  331. if ({ttl_rx_bytes[start_idx+7], ttl_rx_bytes[start_idx+6]} !== 16'd6)
  332. $fatal(1, "FAIL 2: wrong steps_done in MOTOR_DONE, expected 6");
  333. if (x_steps_count !== 6)
  334. $fatal(1, "FAIL 2: physical pulse count mismatch on x_step pin: expected 6, got %0d", x_steps_count);
  335. $display(" -> Exactly 6 pulses emitted on x_step and completed normally!");
  336. $display(" -> TEST 2 PASSED!");
  337. $fflush;
  338. // --------------------------------------------------------------------
  339. // TEST 3: Interleaved commands between host_rs232_rx and host_ttl_rx
  340. // --------------------------------------------------------------------
  341. $display("[TEST 3] Interleaved communication test...");
  342. // 3A: Send GET_ALL via RS232 RX
  343. $display(" -> 3A: Sending GET_ALL via host_rs232_rx...");
  344. frame_bytes[0] = 8'hA5;
  345. frame_bytes[1] = 8'h5A;
  346. frame_bytes[2] = 8'h02;
  347. frame_bytes[3] = 8'h00;
  348. start_idx = ttl_rx_count;
  349. rs232_send_frame(4);
  350. wait(ttl_rx_count >= start_idx + 77);
  351. if (ttl_rx_bytes[start_idx+2] !== 8'h82) $fatal(1, "FAIL 3A: RS232 request didn't yield GET_ACK");
  352. $display(" -> 3A OK: RS232 request answered and broadcast on ttl_tx");
  353. // 3B: Send GET_ALL via TTL RX immediately afterwards
  354. $display(" -> 3B: Sending GET_ALL via host_ttl_rx...");
  355. start_idx = ttl_rx_count;
  356. ttl_send_frame(4);
  357. wait(ttl_rx_count >= start_idx + 77);
  358. if (ttl_rx_bytes[start_idx+2] !== 8'h82) $fatal(1, "FAIL 3B: TTL request didn't yield GET_ACK");
  359. $display(" -> 3B OK: TTL request answered and broadcast on ttl_tx");
  360. $display(" -> TEST 3 PASSED! Dual ports switch and operate seamlessly.");
  361. $fflush;
  362. // --------------------------------------------------------------------
  363. // TEST 4: Limit switch trip during move sent via host_ttl_rx
  364. // --------------------------------------------------------------------
  365. $display("[TEST 4] Limit trip test over TTL: Axis 1 moving, limit trips at step 3...");
  366. $fflush;
  367. frame_bytes[0] = 8'hA5;
  368. frame_bytes[1] = 8'h5A;
  369. frame_bytes[2] = 8'h01; // SET_ALL
  370. frame_bytes[3] = 8'd68; // LEN 68
  371. for (i = 4; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
  372. frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20;
  373. // Axis 1: payload 13..17 (frame_bytes 17..21)
  374. frame_bytes[17] = 8'h01; // PUSH_DOWN
  375. frame_bytes[18] = 8'd40; // 40 steps
  376. frame_bytes[19] = 8'd0;
  377. frame_bytes[20] = 8'd25; // half_period = 25 us
  378. frame_bytes[21] = 8'd0;
  379. start_idx = ttl_rx_count;
  380. y_steps_count = 0;
  381. ttl_send_frame(72);
  382. // Wait for SET_ACK
  383. wait(ttl_rx_count >= start_idx + 6);
  384. $display(" -> SET_ACK received, motor Y running...");
  385. // Trip lower limit when y_steps_count reaches 3
  386. wait(y_steps_count == 3);
  387. @(negedge y_step);
  388. limit_lower_raw[1] = 1'b1; // assert lower limit on Axis 1
  389. $display(" -> Asserted Axis 1 lower limit at y_steps_count=3");
  390. // Expect MOTOR_DONE with reason=1 (lower limit)
  391. wait(ttl_rx_count >= start_idx + 6 + 10);
  392. start_idx = start_idx + 6;
  393. if (ttl_rx_bytes[start_idx+2] !== 8'h50 || ttl_rx_bytes[start_idx+4] !== 8'd1) begin
  394. $fatal(1, "FAIL 4: expected MOTOR_DONE for axis 1");
  395. end
  396. if (ttl_rx_bytes[start_idx+5] !== 8'd1) begin
  397. $fatal(1, "FAIL 4: expected reason=1 (lower limit), got %0d", ttl_rx_bytes[start_idx+5]);
  398. end
  399. $display(" -> Got MOTOR_DONE: axis=1, reason=1 (lower limit), steps_done=%0d",
  400. {ttl_rx_bytes[start_idx+7], ttl_rx_bytes[start_idx+6]});
  401. limit_lower_raw[1] = 1'b0; // release limit
  402. $display(" -> TEST 4 PASSED!");
  403. $fflush;
  404. // --------------------------------------------------------------------
  405. // TEST 5: STOP_CLEAR abort sent over host_ttl_rx
  406. // --------------------------------------------------------------------
  407. $display("[TEST 5] STOP_CLEAR test over TTL: start long move on Axis 0, then abort...");
  408. $fflush;
  409. frame_bytes[0] = 8'hA5;
  410. frame_bytes[1] = 8'h5A;
  411. frame_bytes[2] = 8'h01; // SET_ALL
  412. frame_bytes[3] = 8'd68; // LEN 68
  413. for (i = 4; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
  414. frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20;
  415. // Axis 0: 500 steps
  416. frame_bytes[12] = 8'h01;
  417. frame_bytes[13] = 8'd244;
  418. frame_bytes[14] = 8'd1;
  419. frame_bytes[15] = 8'd30;
  420. frame_bytes[16] = 8'd0;
  421. start_idx = ttl_rx_count;
  422. x_steps_count = 0;
  423. ttl_send_frame(72);
  424. wait(ttl_rx_count >= start_idx + 6);
  425. $display(" -> Long move started on Axis 0...");
  426. // Wait until motor has made 5 steps
  427. wait(x_steps_count >= 5);
  428. $display(" -> Emitted %0d steps, issuing STOP_CLEAR (action=0xFF)...", x_steps_count);
  429. // Send STOP_CLEAR
  430. frame_bytes[0] = 8'hA5;
  431. frame_bytes[1] = 8'h5A;
  432. frame_bytes[2] = 8'h01;
  433. frame_bytes[3] = 8'd68;
  434. for (i = 4; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0;
  435. frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20;
  436. frame_bytes[12] = 8'hFF; // STOP_CLEAR
  437. start_idx = ttl_rx_count;
  438. ttl_send_frame(72);
  439. wait(ttl_rx_count >= start_idx + 6);
  440. if (ttl_rx_bytes[start_idx+2] !== 8'h81) $fatal(1, "FAIL 5: expected SET_ACK for STOP_CLEAR");
  441. $display(" -> Received SET_ACK for STOP_CLEAR");
  442. // Verify motor stops and no more step pulses are produced
  443. i = x_steps_count;
  444. repeat (1000) @(posedge clk);
  445. if (x_steps_count !== i) $fatal(1, "FAIL 5: motor continued stepping after STOP_CLEAR!");
  446. $display(" -> Motor step pulses halted immediately at count %0d", x_steps_count);
  447. $display(" -> TEST 5 PASSED!");
  448. $fflush;
  449. // --------------------------------------------------------------------
  450. // TEST 6: Bad CRC corruption over host_ttl_rx
  451. // --------------------------------------------------------------------
  452. $display("[TEST 6] Bad CRC rejection & recovery test over TTL...");
  453. $fflush;
  454. start_idx = ttl_rx_count;
  455. // Send a frame with bad CRC
  456. ttl_send_byte(8'hA5);
  457. ttl_send_byte(8'h5A);
  458. ttl_send_byte(8'h02); // GET_ALL
  459. ttl_send_byte(8'h00);
  460. ttl_send_byte(8'hDE); // corrupted CRC
  461. ttl_send_byte(8'hAD);
  462. // Wait a timeout period
  463. repeat (3000) @(posedge clk);
  464. if (ttl_rx_count !== start_idx) begin
  465. $fatal(1, "FAIL 6: Corrupted frame should NOT produce any reply or ACK!");
  466. end
  467. $display(" -> Corrupted CRC frame correctly ignored (0 replies produced)");
  468. // Now send a valid GET_ALL to verify recovery
  469. start_idx = ttl_rx_count;
  470. frame_bytes[0] = 8'hA5;
  471. frame_bytes[1] = 8'h5A;
  472. frame_bytes[2] = 8'h02;
  473. frame_bytes[3] = 8'h00;
  474. ttl_send_frame(4);
  475. wait(ttl_rx_count >= start_idx + 77);
  476. if (ttl_rx_bytes[start_idx+2] !== 8'h82) begin
  477. $fatal(1, "FAIL 6: Link failed to recover after bad CRC frame!");
  478. end
  479. $display(" -> Subsequent valid GET_ALL succeeded with GET_ACK (82 00)");
  480. $display(" -> TEST 6 PASSED!");
  481. $fflush;
  482. // --------------------------------------------------------------------
  483. // TEST 7: Noise/glitch on host_ttl_rx does NOT lock out CON78 RS232
  484. // --------------------------------------------------------------------
  485. $display("[TEST 7] TTL glitch/noise immunity test for RS232 link...");
  486. $fflush;
  487. // Inject a glitch / floating pulse on TTL RX
  488. ttl_rx = 1'b0;
  489. repeat (10) @(posedge clk);
  490. ttl_rx = 1'b1;
  491. // Wait for owner_idle_counter to time out and release rx_owner
  492. repeat (3000) @(posedge clk);
  493. // Now issue SET_ALL via RS232 CON78 (69B including buzzer=1)
  494. $display(" -> Sending 69B SET_ALL on CON78 RS232 with 4 chamber duties: 50%%, 30%%, 70%%, 90%%, buzzer=1...");
  495. start_idx = rs232_rx_count;
  496. frame_bytes[0] = 8'hA5;
  497. frame_bytes[1] = 8'h5A;
  498. frame_bytes[2] = 8'h01; // SET_ALL
  499. frame_bytes[3] = 8'd69; // LEN 69
  500. for (i = 4; i < 4 + 69; i = i + 1) frame_bytes[i] = 8'd0;
  501. // FAN 0..3: 20%, 30%, 40%, 50%
  502. frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd30; frame_bytes[6] = 8'd40; frame_bytes[7] = 8'd50;
  503. // Chamber 1..4 PTC: 50%, 30%, 70%, 90%
  504. frame_bytes[8] = 8'd50; frame_bytes[9] = 8'd30; frame_bytes[10] = 8'd70; frame_bytes[11] = 8'd90;
  505. frame_bytes[4 + 68] = 8'd1; // buzzer ON
  506. rs232_send_frame(73);
  507. wait(rs232_rx_count >= start_idx + 6);
  508. if (rs232_rx_bytes[start_idx+2] !== 8'h81) begin
  509. $fatal(1, "FAIL 7: CON78 RS232 failed to get SET_ACK after TTL glitch!");
  510. end
  511. $display(" -> CON78 RS232 successfully received SET_ACK (81 00) after TTL glitch!");
  512. $display(" -> TEST 7 PASSED!");
  513. $fflush;
  514. // --------------------------------------------------------------------
  515. // TEST 8: Verify all 24 physical PTC outputs driven across 4 chambers
  516. // --------------------------------------------------------------------
  517. $display("[TEST 8] 24-channel physical PTC output verification...");
  518. $fflush;
  519. // Advance into next PWM period so duties are active
  520. repeat (6000) @(posedge clk);
  521. // Sample ptc_pwm_out across multiple cycles
  522. for (i = 0; i < 100; i = i + 1) begin
  523. @(posedge clk);
  524. // Verify Chamber 1 (pins 0..5) all match
  525. if (ptc_pwm_out[0] !== ptc_pwm_out[1] || ptc_pwm_out[1] !== ptc_pwm_out[2] ||
  526. ptc_pwm_out[2] !== ptc_pwm_out[3] || ptc_pwm_out[3] !== ptc_pwm_out[4] ||
  527. ptc_pwm_out[4] !== ptc_pwm_out[5]) begin
  528. $fatal(1, "FAIL 8: Chamber 1 physical heaters mismatch: %b", ptc_pwm_out[5:0]);
  529. end
  530. // Verify Chamber 2 (pins 6..11) all match
  531. if (ptc_pwm_out[6] !== ptc_pwm_out[7] || ptc_pwm_out[7] !== ptc_pwm_out[8] ||
  532. ptc_pwm_out[8] !== ptc_pwm_out[9] || ptc_pwm_out[9] !== ptc_pwm_out[10] ||
  533. ptc_pwm_out[10] !== ptc_pwm_out[11]) begin
  534. $fatal(1, "FAIL 8: Chamber 2 physical heaters mismatch: %b", ptc_pwm_out[11:6]);
  535. end
  536. // Verify Chamber 3 (pins 12..17) all match
  537. if (ptc_pwm_out[12] !== ptc_pwm_out[13] || ptc_pwm_out[13] !== ptc_pwm_out[14] ||
  538. ptc_pwm_out[14] !== ptc_pwm_out[15] || ptc_pwm_out[15] !== ptc_pwm_out[16] ||
  539. ptc_pwm_out[16] !== ptc_pwm_out[17]) begin
  540. $fatal(1, "FAIL 8: Chamber 3 physical heaters mismatch: %b", ptc_pwm_out[17:12]);
  541. end
  542. // Verify Chamber 4 (pins 18..23) all match
  543. if (ptc_pwm_out[18] !== ptc_pwm_out[19] || ptc_pwm_out[19] !== ptc_pwm_out[20] ||
  544. ptc_pwm_out[20] !== ptc_pwm_out[21] || ptc_pwm_out[21] !== ptc_pwm_out[22] ||
  545. ptc_pwm_out[22] !== ptc_pwm_out[23]) begin
  546. $fatal(1, "FAIL 8: Chamber 4 physical heaters mismatch: %b", ptc_pwm_out[23:18]);
  547. end
  548. end
  549. $display(" -> All 24 physical PTC channels strictly track their respective chamber PWMs!");
  550. // Assert hood_switch_in, verify 4000Hz 80% buzzer waveform, and send GET_ALL on RS232
  551. hood_switch_in = 1'b1;
  552. repeat (20) @(posedge clk);
  553. begin : check_mr_ttl_bz
  554. integer bz_hi, bz_lo, bz_k;
  555. bz_hi = 0; bz_lo = 0;
  556. for (bz_k = 0; bz_k < 6250; bz_k = bz_k + 1) begin
  557. @(posedge clk);
  558. if (buzzer_out === 1'b1) bz_hi = bz_hi + 1;
  559. else if (buzzer_out === 1'b0) bz_lo = bz_lo + 1;
  560. end
  561. if (bz_hi !== 5000 || bz_lo !== 1250)
  562. $fatal(1, "FAIL 8: buzzer_out 4000Hz 80%% mismatch: high=%0d low=%0d", bz_hi, bz_lo);
  563. end
  564. start_idx = rs232_rx_count;
  565. frame_bytes[0] = 8'hA5;
  566. frame_bytes[1] = 8'h5A;
  567. frame_bytes[2] = 8'h02; // GET_ALL
  568. frame_bytes[3] = 8'h00;
  569. rs232_send_frame(4);
  570. wait(rs232_rx_count >= start_idx + 77);
  571. if (rs232_rx_bytes[start_idx+2] !== 8'h82) $fatal(1, "FAIL 8: expected GET_ACK on RS232");
  572. if (rs232_rx_bytes[start_idx+8] !== 8'h42 || rs232_rx_bytes[start_idx+9] !== 8'h41)
  573. $fatal(1, "FAIL 8: expected GET_DATA (65B) on RS232");
  574. // Check FAN duties (bytes 10..13)
  575. if (rs232_rx_bytes[start_idx+10] !== 8'd20 || rs232_rx_bytes[start_idx+11] !== 8'd30 ||
  576. rs232_rx_bytes[start_idx+12] !== 8'd40 || rs232_rx_bytes[start_idx+13] !== 8'd50) begin
  577. $fatal(1, "FAIL 8: GET_DATA FAN duties mismatch");
  578. end
  579. // Check Chamber PTC duties (bytes 14..17)
  580. if (rs232_rx_bytes[start_idx+14] !== 8'd50 || rs232_rx_bytes[start_idx+15] !== 8'd30 ||
  581. rs232_rx_bytes[start_idx+16] !== 8'd70 || rs232_rx_bytes[start_idx+17] !== 8'd90) begin
  582. $fatal(1, "FAIL 8: GET_DATA PTC duties mismatch");
  583. end
  584. if (rs232_rx_bytes[start_idx+10+63] !== 8'd1 || rs232_rx_bytes[start_idx+10+64] !== 8'd1) begin
  585. $fatal(1, "FAIL 8: GET_DATA hood/buzzer mismatch: hood=%h buzzer=%h",
  586. rs232_rx_bytes[start_idx+10+63], rs232_rx_bytes[start_idx+10+64]);
  587. end
  588. $display(" -> GET_DATA duties read back on RS232: FAN=[20,30,40,50], PTC=[50,30,70,90], hood=1, buzzer=1");
  589. $display(" -> TEST 8 PASSED!");
  590. $fflush;
  591. $display("==================================================");
  592. $display("=== ALL 8 HOST TTL & RS232 TESTS PASSED! ===");
  593. $display("prod2u_mrdimm_ttl_e2e_tb PASS");
  594. $display("==================================================");
  595. $fflush;
  596. $finish;
  597. end
  598. endmodule