prod2u_6400_v2_tb.v 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. `timescale 1ns/1ps
  2. // 6400 V2.0 End-to-End Simulation Testbench
  3. //
  4. // Drives the 6400 V2.0 top module (prod2u_6400_v2_top) over CON12 TTL UART
  5. // (J4 RX, K2 TX) at 921600 8N1 (27 clocks/bit at 25 MHz) and verifies:
  6. // 1. SSR1, SSR2, SSR4 outputs (F3, F1, G4) reset to zero.
  7. // 2. SET_ALL (35 bytes) -> receives valid SET_ACK (81 00).
  8. // 3. GET_ALL (0 bytes) -> receives GET_ACK (82 00) followed by GET_DATA (42 56):
  9. // - 24 PTC duties match set duties.
  10. // - 4 Front and 4 Rear fan duties match set duties.
  11. // - CT sensor channels 9..26 (offsets 65..82) return 0xFF.
  12. // 4. GET returns the expected duty-bank bytes after each SET.
  13. // 5. SSRs follow the100/0/100 commands at the explicit checkpoints.
  14. // This test uses integer UART timing and response-header checks. It does not
  15. // verify response CRCs, physical ADC mapping or the first PWM commit edge.
  16. module prod2u_6400_v2_tb;
  17. localparam integer CLKS_PER_BIT = 27;
  18. localparam integer BIT_CLKS = CLKS_PER_BIT;
  19. reg clk;
  20. initial begin
  21. clk = 0;
  22. forever #20 clk = ~clk; // 25 MHz (40 ns)
  23. end
  24. // DUT signals
  25. reg host_ttl_rx;
  26. wire host_ttl_tx;
  27. reg host_rs232_rx;
  28. wire host_rs232_tx;
  29. reg bmc_rs232_rx;
  30. wire bmc_rs232_tx;
  31. wire [23:0] ptc_pwm_out;
  32. wire [3:0] front_fan_pwm_out;
  33. wire [3:0] rear_fan_pwm_out;
  34. wire temp_scl;
  35. wire temp_sda;
  36. wire ssr1_out;
  37. wire ssr2_out;
  38. wire ssr4_out;
  39. reg estop_btn_in;
  40. reg hood_open_btn_in;
  41. reg hood_close_btn_in;
  42. wire buzzer_out;
  43. pullup(temp_scl);
  44. pullup(temp_sda);
  45. prod2u_6400_v2_top #(
  46. .FAN_BOARD_INVERT_MASK(8'hFF)
  47. ) u_dut (
  48. .sys_clk(clk),
  49. .host_ttl_rx(host_ttl_rx),
  50. .host_ttl_tx(host_ttl_tx),
  51. .host_rs232_rx(host_rs232_rx),
  52. .host_rs232_tx(host_rs232_tx),
  53. .bmc_rs232_rx(bmc_rs232_rx),
  54. .bmc_rs232_tx(bmc_rs232_tx),
  55. .ptc_pwm_out(ptc_pwm_out),
  56. .front_fan_pwm_out(front_fan_pwm_out),
  57. .rear_fan_pwm_out(rear_fan_pwm_out),
  58. .temp_scl(temp_scl),
  59. .temp_sda(temp_sda),
  60. .temp_scl_alt(),
  61. .temp_sda_alt(),
  62. .ssr1_out(ssr1_out),
  63. .ssr2_out(ssr2_out),
  64. .ssr4_out(ssr4_out),
  65. .ssr_unused_zero(),
  66. .ptc_spare_zero(),
  67. .estop_btn_in(estop_btn_in),
  68. .hood_open_btn_in(hood_open_btn_in),
  69. .hood_close_btn_in(hood_close_btn_in),
  70. .buzzer_out(buzzer_out)
  71. );
  72. // Frame buffers
  73. reg [7:0] frame_bytes [0:255];
  74. integer frame_len;
  75. // Independent CRC calculation
  76. function [15:0] crc_over_frame;
  77. input integer count;
  78. integer i, b;
  79. reg [15:0] c;
  80. reg [7:0] d;
  81. begin
  82. c = 16'hFFFF;
  83. for (i = 2; i < count; i = i + 1) begin
  84. d = frame_bytes[i];
  85. for (b = 7; b >= 0; b = b - 1)
  86. c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021)
  87. : {c[14:0], 1'b0};
  88. end
  89. crc_over_frame = c;
  90. end
  91. endfunction
  92. // UART transmit via CON12 TTL RX
  93. task uart_send_byte;
  94. input [7:0] value;
  95. integer b;
  96. begin
  97. host_ttl_rx = 1'b0; // start bit
  98. repeat (BIT_CLKS) @(posedge clk);
  99. for (b = 0; b < 8; b = b + 1) begin
  100. host_ttl_rx = value[b];
  101. repeat (BIT_CLKS) @(posedge clk);
  102. end
  103. host_ttl_rx = 1'b1; // stop bit
  104. repeat (BIT_CLKS) @(posedge clk);
  105. end
  106. endtask
  107. task uart_send_frame;
  108. input integer n;
  109. integer i;
  110. reg [15:0] c;
  111. begin
  112. c = crc_over_frame(n);
  113. for (i = 0; i < n; i = i + 1)
  114. uart_send_byte(frame_bytes[i]);
  115. uart_send_byte(c[7:0]);
  116. uart_send_byte(c[15:8]);
  117. end
  118. endtask
  119. // UART transmit via CON134 RS-232 RX
  120. task uart_send_byte_rs232;
  121. input [7:0] value;
  122. integer b;
  123. begin
  124. host_rs232_rx = 1'b0; // start bit
  125. repeat (BIT_CLKS) @(posedge clk);
  126. for (b = 0; b < 8; b = b + 1) begin
  127. host_rs232_rx = value[b];
  128. repeat (BIT_CLKS) @(posedge clk);
  129. end
  130. host_rs232_rx = 1'b1; // stop bit
  131. repeat (BIT_CLKS) @(posedge clk);
  132. end
  133. endtask
  134. task uart_send_frame_rs232;
  135. input integer n;
  136. integer i;
  137. reg [15:0] c;
  138. begin
  139. c = crc_over_frame(n);
  140. for (i = 0; i < n; i = i + 1)
  141. uart_send_byte_rs232(frame_bytes[i]);
  142. uart_send_byte_rs232(c[7:0]);
  143. uart_send_byte_rs232(c[15:8]);
  144. end
  145. endtask
  146. task uart_send_frame_rs232_bad_crc;
  147. input integer n;
  148. integer i;
  149. begin
  150. for (i = 0; i < n; i = i + 1)
  151. uart_send_byte_rs232(frame_bytes[i]);
  152. uart_send_byte_rs232(8'hDE); // corrupted CRC
  153. uart_send_byte_rs232(8'hAD);
  154. end
  155. endtask
  156. task uart_send_byte_bad_stop;
  157. input [7:0] value;
  158. integer b;
  159. begin
  160. host_ttl_rx = 1'b0; // start bit
  161. repeat (BIT_CLKS) @(posedge clk);
  162. for (b = 0; b < 8; b = b + 1) begin
  163. host_ttl_rx = value[b];
  164. repeat (BIT_CLKS) @(posedge clk);
  165. end
  166. host_ttl_rx = 1'b0; // bad stop bit
  167. repeat (BIT_CLKS) @(posedge clk);
  168. host_ttl_rx = 1'b1; // return to idle
  169. repeat (BIT_CLKS) @(posedge clk);
  170. end
  171. endtask
  172. // UART receive sampler on CON12 TTL TX
  173. reg [7:0] rx_bytes [0:255];
  174. integer rx_count;
  175. reg [3:0] rx_bit;
  176. reg [7:0] rx_shift;
  177. integer rx_clk_cnt;
  178. reg rx_busy;
  179. initial begin
  180. rx_busy = 0;
  181. rx_clk_cnt = 0;
  182. rx_count = 0;
  183. forever begin
  184. @(posedge clk);
  185. if (!rx_busy && host_ttl_tx === 1'b0) begin
  186. rx_busy = 1;
  187. rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  188. rx_bit = 0;
  189. end else if (rx_busy) begin
  190. if (rx_clk_cnt == 0) begin
  191. if (rx_bit < 8) begin
  192. rx_shift[rx_bit] = host_ttl_tx;
  193. rx_bit = rx_bit + 1;
  194. rx_clk_cnt = BIT_CLKS - 1;
  195. end else begin
  196. rx_bytes[rx_count] = rx_shift;
  197. rx_count = rx_count + 1;
  198. rx_busy = 0;
  199. end
  200. end else begin
  201. rx_clk_cnt = rx_clk_cnt - 1;
  202. end
  203. end
  204. end
  205. end
  206. // UART receive sampler on CON134 RS-232 TX (broadcast check)
  207. reg [7:0] rs232_rx_bytes [0:255];
  208. integer rs232_rx_count;
  209. reg [3:0] rs232_rx_bit;
  210. reg [7:0] rs232_rx_shift;
  211. integer rs232_rx_clk_cnt;
  212. reg rs232_rx_busy;
  213. initial begin
  214. rs232_rx_busy = 0;
  215. rs232_rx_clk_cnt = 0;
  216. rs232_rx_count = 0;
  217. forever begin
  218. @(posedge clk);
  219. if (!rs232_rx_busy && host_rs232_tx === 1'b0) begin
  220. rs232_rx_busy = 1;
  221. rs232_rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  222. rs232_rx_bit = 0;
  223. end else if (rs232_rx_busy) begin
  224. if (rs232_rx_clk_cnt == 0) begin
  225. if (rs232_rx_bit < 8) begin
  226. rs232_rx_shift[rs232_rx_bit] = host_rs232_tx;
  227. rs232_rx_bit = rs232_rx_bit + 1;
  228. rs232_rx_clk_cnt = BIT_CLKS - 1;
  229. end else begin
  230. rs232_rx_bytes[rs232_rx_count] = rs232_rx_shift;
  231. rs232_rx_count = rs232_rx_count + 1;
  232. rs232_rx_busy = 0;
  233. end
  234. end else begin
  235. rs232_rx_clk_cnt = rs232_rx_clk_cnt - 1;
  236. end
  237. end
  238. end
  239. end
  240. integer failures = 0;
  241. integer i;
  242. initial begin
  243. host_ttl_rx = 1'b1;
  244. host_rs232_rx = 1'b1;
  245. bmc_rs232_rx = 1'b1;
  246. estop_btn_in = 1'b0;
  247. hood_open_btn_in = 1'b0;
  248. hood_close_btn_in = 1'b0;
  249. repeat (3000) @(posedge clk);
  250. // Check 1: SSR1, SSR2, SSR4 and buzzer reset defaults are zero
  251. if (ssr1_out !== 1'b0 || ssr2_out !== 1'b0 || ssr4_out !== 1'b0 || buzzer_out !== 1'b0) begin
  252. $display("FAIL: SSR/buzzer boot outputs not zero: %b %b %b %b", ssr1_out, ssr2_out, ssr4_out, buzzer_out);
  253. failures = failures + 1;
  254. end else begin
  255. $display("PASS: SSR1, SSR2, SSR4 and buzzer are zero at boot");
  256. end
  257. // Check 2: Build and send SET_ALL (36 bytes: 35 duties + buzzer=1)
  258. // PTC 0..23 = 50%, Front 0..3 = 40%, Rear 0..3 = 60%, SSR = 100/0/100, buzzer = 1
  259. frame_bytes[0] = 8'hA5;
  260. frame_bytes[1] = 8'h5A;
  261. frame_bytes[2] = 8'h01; // SET_ALL
  262. frame_bytes[3] = 8'd36; // LEN
  263. for (i = 0; i < 24; i = i + 1) frame_bytes[4 + i] = 8'd50;
  264. for (i = 0; i < 4; i = i + 1) frame_bytes[28 + i] = 8'd40;
  265. for (i = 0; i < 4; i = i + 1) frame_bytes[32 + i] = 8'd60;
  266. frame_bytes[36] = 100; frame_bytes[37] = 0; frame_bytes[38] = 100;
  267. frame_bytes[39] = 1; // buzzer ON
  268. frame_len = 40;
  269. $display("Sending SET_ALL (36B, buzzer=1) via CON12 TTL...");
  270. rx_count = 0;
  271. rs232_rx_count = 0;
  272. uart_send_frame(frame_len);
  273. // Expect SET_ACK (A5 5A 81 00 A6 35 = 6 bytes)
  274. while (rx_count < 6) @(posedge clk);
  275. if (rx_bytes[0] !== 8'hA5 || rx_bytes[1] !== 8'h5A ||
  276. rx_bytes[2] !== 8'h81 || rx_bytes[3] !== 8'h00) begin
  277. $display("FAIL: SET_ACK header incorrect: %02h %02h %02h %02h",
  278. rx_bytes[0], rx_bytes[1], rx_bytes[2], rx_bytes[3]);
  279. failures = failures + 1;
  280. end else begin
  281. $display("PASS: Received SET_ACK (81 00)");
  282. end
  283. begin : check_bz_wave
  284. integer bz_hi, bz_lo, bz_k;
  285. bz_hi = 0; bz_lo = 0;
  286. for (bz_k = 0; bz_k < 6250; bz_k = bz_k + 1) begin
  287. @(posedge clk);
  288. if (buzzer_out === 1'b1) bz_hi = bz_hi + 1;
  289. else if (buzzer_out === 1'b0) bz_lo = bz_lo + 1;
  290. end
  291. if (bz_hi !== 5000 || bz_lo !== 1250) begin
  292. $display("FAIL: buzzer_out 4000Hz 80%% mismatch: high=%0d low=%0d", bz_hi, bz_lo);
  293. failures = failures + 1;
  294. end else begin
  295. $display("PASS: buzzer_out 4000Hz 80%% duty verified (5000 HIGH / 1250 LOW)");
  296. end
  297. end
  298. // Check 3: Press EPB1 (F8)=1, SB1 (C11)=1, SB2 (A11)=0 and send GET_ALL (A5 5A 02 00 6D 7B)
  299. estop_btn_in = 1'b1;
  300. hood_open_btn_in = 1'b1;
  301. hood_close_btn_in = 1'b0;
  302. repeat (10) @(posedge clk);
  303. frame_bytes[0] = 8'hA5;
  304. frame_bytes[1] = 8'h5A;
  305. frame_bytes[2] = 8'h02; // GET_ALL
  306. frame_bytes[3] = 8'd0;
  307. frame_len = 4;
  308. $display("Sending GET_ALL via CON12 TTL...");
  309. rx_count = 0;
  310. rs232_rx_count = 0;
  311. uart_send_frame(frame_len);
  312. // Expect GET_ACK (6 bytes) + GET_DATA (96 bytes: A5 5A 42 5A ... CRC) = 102 bytes total
  313. while (rx_count < 102) @(posedge clk);
  314. // Check GET_ACK
  315. if (rx_bytes[0] !== 8'hA5 || rx_bytes[1] !== 8'h5A ||
  316. rx_bytes[2] !== 8'h82 || rx_bytes[3] !== 8'h00) begin
  317. $display("FAIL: GET_ACK incorrect: %02h %02h %02h %02h",
  318. rx_bytes[0], rx_bytes[1], rx_bytes[2], rx_bytes[3]);
  319. failures = failures + 1;
  320. end else begin
  321. $display("PASS: Received GET_ACK (82 00)");
  322. end
  323. // Check GET_DATA header (90 bytes = 0x5A)
  324. if (rx_bytes[6] !== 8'hA5 || rx_bytes[7] !== 8'h5A ||
  325. rx_bytes[8] !== 8'h42 || rx_bytes[9] !== 8'd90) begin
  326. $display("FAIL: GET_DATA header incorrect: %02h %02h %02h %02h",
  327. rx_bytes[6], rx_bytes[7], rx_bytes[8], rx_bytes[9]);
  328. failures = failures + 1;
  329. end else begin
  330. $display("PASS: Received GET_DATA (42 5A)");
  331. // Verify PTC duties (offsets 10..33) match 50
  332. for (i = 0; i < 24; i = i + 1) begin
  333. if (rx_bytes[10 + i] !== 8'd50) begin
  334. $display("FAIL: PTC[%0d] = %0d, expected 50", i, rx_bytes[10 + i]);
  335. failures = failures + 1;
  336. end
  337. end
  338. // Verify Front fan duties (offsets 34..37) match 40
  339. for (i = 0; i < 4; i = i + 1) begin
  340. if (rx_bytes[34 + i] !== 8'd40) begin
  341. $display("FAIL: FrontFan[%0d] = %0d, expected 40", i, rx_bytes[34 + i]);
  342. failures = failures + 1;
  343. end
  344. end
  345. // Verify Rear fan duties (offsets 38..41) match 60
  346. for (i = 0; i < 4; i = i + 1) begin
  347. if (rx_bytes[38 + i] !== 8'd60) begin
  348. $display("FAIL: RearFan[%0d] = %0d, expected 60", i, rx_bytes[38 + i]);
  349. failures = failures + 1;
  350. end
  351. end
  352. // Verify CT sensor unwritten channels 9..26 (offsets 66+9..66+26 = 75..92) are 0xFF
  353. for (i = 9; i < 27; i = i + 1) begin
  354. if (rx_bytes[66 + i] !== 8'hFF) begin
  355. $display("FAIL: CT[%0d] (offset %0d) = %02h, expected 0xFF", i, 66 + i, rx_bytes[66 + i]);
  356. failures = failures + 1;
  357. end
  358. end
  359. // Verify EPB1(86->96), SB1(87->97), SB2(88->98), buzzer(89->99)
  360. if (rx_bytes[96] !== 8'd1 || rx_bytes[97] !== 8'd1 ||
  361. rx_bytes[98] !== 8'd0 || rx_bytes[99] !== 8'd1) begin
  362. $display("FAIL: buttons/buzzer mismatch: epb=%02h sb1=%02h sb2=%02h bz=%02h",
  363. rx_bytes[96], rx_bytes[97], rx_bytes[98], rx_bytes[99]);
  364. failures = failures + 1;
  365. end
  366. $display("PASS: Verified GET_DATA content (PTC 50%%, Front 40%%, Rear 60%%, CT9..26 0xFF, EPB1=1, SB1=1, SB2=0, buzzer=1)");
  367. end
  368. // Verify EPB1=1 caused NO CPLD safety interlock before releasing buttons
  369. if (ssr1_out !== 1'b1 || ssr2_out !== 1'b0 || ssr4_out !== 1'b1) begin
  370. $display("FAIL: SSR altered while EPB1=1: %b %b %b", ssr1_out, ssr2_out, ssr4_out);
  371. failures = failures + 1;
  372. end else begin
  373. $display("PASS: SSRs follow 100/0/100 command with no EPB1 interlock");
  374. end
  375. estop_btn_in = 1'b0;
  376. hood_open_btn_in = 1'b0;
  377. hood_close_btn_in = 1'b0;
  378. // Check 5: Broadcast verification - RS-232 TX received same GET_ACK/GET_DATA as TTL TX
  379. if (rs232_rx_count < 102) begin
  380. $display("FAIL: RS-232 TX did not receive full broadcast reply (%0d < 102)", rs232_rx_count);
  381. failures = failures + 1;
  382. end else if (rs232_rx_bytes[0] !== 8'hA5 || rs232_rx_bytes[1] !== 8'h5A ||
  383. rs232_rx_bytes[2] !== 8'h82) begin
  384. $display("FAIL: RS-232 TX broadcast mismatch");
  385. failures = failures + 1;
  386. end else begin
  387. $display("PASS: Verified TX broadcast on both CON12 TTL and CON134 RS-232");
  388. end
  389. // Check 6: CON134 RS-232 legacy 35B SET_ALL (PTC 70, Front 30, Rear 80; buzzer defaults 0)
  390. frame_bytes[0] = 8'hA5;
  391. frame_bytes[1] = 8'h5A;
  392. frame_bytes[2] = 8'h01; // SET_ALL
  393. frame_bytes[3] = 8'd35;
  394. for (i = 0; i < 24; i = i + 1) frame_bytes[4 + i] = 8'd70;
  395. for (i = 0; i < 4; i = i + 1) frame_bytes[28 + i] = 8'd30;
  396. for (i = 0; i < 4; i = i + 1) frame_bytes[32 + i] = 8'd80;
  397. frame_bytes[36] = 100; frame_bytes[37] = 0; frame_bytes[38] = 100;
  398. frame_len = 39;
  399. $display("Sending SET_ALL via CON134 RS-232...");
  400. rx_count = 0;
  401. rs232_rx_count = 0;
  402. uart_send_frame_rs232(frame_len);
  403. while (rs232_rx_count < 6) @(posedge clk);
  404. if (rs232_rx_bytes[2] !== 8'h81 || rs232_rx_bytes[3] !== 8'h00) begin
  405. $display("FAIL: CON134 SET_ACK incorrect: %02h %02h", rs232_rx_bytes[2], rs232_rx_bytes[3]);
  406. failures = failures + 1;
  407. end else begin
  408. $display("PASS: Received SET_ACK via CON134 RS-232");
  409. end
  410. if (buzzer_out !== 1'b0) begin
  411. $display("FAIL: buzzer_out not cleared on legacy 35B SET_ALL");
  412. failures = failures + 1;
  413. end
  414. // Check 7: CON134 RS-232 GET_ALL to verify newly applied duties & released buttons=0
  415. frame_bytes[0] = 8'hA5;
  416. frame_bytes[1] = 8'h5A;
  417. frame_bytes[2] = 8'h02; // GET_ALL
  418. frame_bytes[3] = 8'd0;
  419. frame_len = 4;
  420. $display("Sending GET_ALL via CON134 RS-232...");
  421. rx_count = 0;
  422. rs232_rx_count = 0;
  423. uart_send_frame_rs232(frame_len);
  424. while (rs232_rx_count < 102) @(posedge clk);
  425. if (rs232_rx_bytes[2] !== 8'h82 || rs232_rx_bytes[8] !== 8'h42 ||
  426. rs232_rx_bytes[10] !== 8'd70 || rs232_rx_bytes[34] !== 8'd30 ||
  427. rs232_rx_bytes[38] !== 8'd80 || rs232_rx_bytes[96] !== 8'd0 ||
  428. rs232_rx_bytes[97] !== 8'd0 || rs232_rx_bytes[98] !== 8'd0 ||
  429. rs232_rx_bytes[99] !== 8'd0) begin
  430. $display("FAIL: CON134 GET_DATA values incorrect");
  431. failures = failures + 1;
  432. end else begin
  433. $display("PASS: CON134 GET_DATA confirmed applied duties (PTC 70%%, Front 30%%, Rear 80%%, buttons=0, buzzer=0)");
  434. end
  435. // Check 8: Bad CRC on CON134 RS-232 is discarded and does not produce ACK/NACK
  436. $display("Sending Bad-CRC frame via CON134 RS-232...");
  437. rx_count = 0;
  438. rs232_rx_count = 0;
  439. uart_send_frame_rs232_bad_crc(frame_len);
  440. repeat (10000) @(posedge clk);
  441. if (rs232_rx_count != 0) begin
  442. $display("FAIL: Bad CRC frame produced unexpected reply (%0d bytes)", rs232_rx_count);
  443. failures = failures + 1;
  444. end else begin
  445. $display("PASS: Bad CRC frame safely discarded without reply");
  446. end
  447. // Check 9: Bad stop bit on CON12 TTL is discarded and recoverable
  448. $display("Sending Bad-Stop-Bit byte via CON12 TTL...");
  449. rx_count = 0;
  450. uart_send_byte_bad_stop(8'hA5);
  451. repeat (2000) @(posedge clk);
  452. if (rx_count != 0) begin
  453. $display("FAIL: Bad stop bit produced unexpected reply");
  454. failures = failures + 1;
  455. end else begin
  456. $display("PASS: Bad stop bit discarded cleanly");
  457. end
  458. // Check 10: Inter-byte timeout recovery (half frame abandoned after timeout)
  459. $display("Testing Inter-byte timeout recovery on CON12 TTL...");
  460. uart_send_byte(8'hA5);
  461. uart_send_byte(8'h5A);
  462. uart_send_byte(8'h01); // partial header
  463. // Wait for inter-byte timeout (> 250,000 cycles = 10 ms)
  464. repeat (260000) @(posedge clk);
  465. $display(" Inter-byte timeout elapsed; sending valid GET_ALL to verify recovery...");
  466. rx_count = 0;
  467. rs232_rx_count = 0;
  468. frame_bytes[0] = 8'hA5;
  469. frame_bytes[1] = 8'h5A;
  470. frame_bytes[2] = 8'h02;
  471. frame_bytes[3] = 8'd0;
  472. frame_len = 4;
  473. uart_send_frame(frame_len);
  474. while (rx_count < 102) @(posedge clk);
  475. if (rx_bytes[2] !== 8'h82) begin
  476. $display("FAIL: Failed to recover after inter-byte timeout");
  477. failures = failures + 1;
  478. end else begin
  479. $display("PASS: Successfully recovered from inter-byte timeout");
  480. end
  481. // Check 11: Cross-port switching: CON12 TTL SET_ALL followed by CON134 RS-232 GET_ALL
  482. $display("Testing Cross-port switching: CON12 TTL SET -> CON134 RS-232 GET...");
  483. frame_bytes[0] = 8'hA5;
  484. frame_bytes[1] = 8'h5A;
  485. frame_bytes[2] = 8'h01;
  486. frame_bytes[3] = 8'd36;
  487. for (i = 0; i < 24; i = i + 1) frame_bytes[4 + i] = 8'd25;
  488. for (i = 0; i < 4; i = i + 1) frame_bytes[28 + i] = 8'd55;
  489. for (i = 0; i < 4; i = i + 1) frame_bytes[32 + i] = 8'd75;
  490. frame_bytes[36] = 100; frame_bytes[37] = 0; frame_bytes[38] = 100;
  491. frame_bytes[39] = 0;
  492. frame_len = 40;
  493. rx_count = 0;
  494. rs232_rx_count = 0;
  495. uart_send_frame(frame_len); // via CON12 TTL
  496. while (rx_count < 6) @(posedge clk);
  497. // Immediate next transaction via CON134 RS-232
  498. frame_bytes[0] = 8'hA5;
  499. frame_bytes[1] = 8'h5A;
  500. frame_bytes[2] = 8'h02;
  501. frame_bytes[3] = 8'd0;
  502. frame_len = 4;
  503. rx_count = 0;
  504. rs232_rx_count = 0;
  505. uart_send_frame_rs232(frame_len); // via CON134 RS-232
  506. while (rs232_rx_count < 102) @(posedge clk);
  507. if (rs232_rx_bytes[10] !== 8'd25 || rs232_rx_bytes[34] !== 8'd55 || rs232_rx_bytes[38] !== 8'd75) begin
  508. $display("FAIL: Cross-port switching duty mismatch");
  509. failures = failures + 1;
  510. end else begin
  511. $display("PASS: Cross-port switching verified cleanly (PTC 25%%, Front 55%%, Rear 75%%)");
  512. end
  513. #2000;
  514. if (failures == 0) begin
  515. $display("prod2u_6400_v2_tb PASS");
  516. end else begin
  517. $fatal(1, "prod2u_6400_v2_tb FAIL (%0d errors)", failures);
  518. end
  519. $finish;
  520. end
  521. initial begin
  522. #30000000; // 30 ms timeout
  523. $fatal(1, "prod2u_6400_v2_tb TIMEOUT");
  524. end
  525. endmodule