prod2u_v3_protocol_tb.v 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. `timescale 1ns/1ps
  2. // Independent V3 real-top regression. All business inputs enter actual UART
  3. // and I2C pads; only long watchdog/beacon periods are elaboration-shortened.
  4. // No forced RTL state, RAM injection, generated expected payload or DUT CRC.
  5. module prod2u_v3_protocol_tb;
  6. localparam real BIT_NS = 1000000000.0 / 921600.0;
  7. reg clk=0;
  8. always #20 clk=~clk;
  9. reg host_rx=1;
  10. wire host_tx, broadcast_tx;
  11. tri1 scl,sda;
  12. wire [23:0] ptc;
  13. wire [3:0] front,rear;
  14. wire [2:0] ssr;
  15. reg estop_btn = 0, hood_open_btn = 0, hood_close_btn = 0;
  16. wire buzzer;
  17. wire [31:0] ct_reads;
  18. wire [8:0] ct_seen;
  19. prod2u_6400_v2_top #(.HOST_BAUD(921600), .WD_TIMEOUT_CLKS(1400000)) dut (
  20. .sys_clk(clk), .host_ttl_rx(host_rx), .host_ttl_tx(host_tx),
  21. .host_rs232_rx(1'b1), .host_rs232_tx(broadcast_tx),
  22. .bmc_rs232_rx(1'b1), .bmc_rs232_tx(),
  23. .ptc_pwm_out(ptc), .front_fan_pwm_out(front), .rear_fan_pwm_out(rear),
  24. .temp_scl(scl), .temp_sda(sda), .temp_scl_alt(), .temp_sda_alt(),
  25. .ssr1_out(ssr[0]), .ssr2_out(ssr[1]), .ssr4_out(ssr[2]),
  26. .ssr_unused_zero(), .ptc_spare_zero(),
  27. .estop_btn_in(estop_btn), .hood_open_btn_in(hood_open_btn), .hood_close_btn_in(hood_close_btn),
  28. .buzzer_out(buzzer)
  29. );
  30. defparam dut.u_core.BEACON_PERIOD = 400000;
  31. always @(dut.ssr_unused_zero) begin
  32. #1;
  33. if ($time > 100 && dut.ssr_unused_zero !== 3'b000) $fatal(1, "retired SSR pads must stay low");
  34. end
  35. ct7428_normal_slave_bfm #(.DIAGNOSTIC_REGISTERS(1)) ct (.scl(scl), .sda(sda),
  36. .completed_reads(ct_reads), .registers_seen(ct_seen));
  37. function [15:0] crc_byte;
  38. input [15:0] prior;
  39. input [7:0] value;
  40. reg [15:0] c;
  41. integer k;
  42. begin
  43. c=prior ^ {value,8'h00};
  44. for(k=0;k<8;k=k+1) c=c[15] ? ((c<<1)^16'h1021) : (c<<1);
  45. crc_byte=c;
  46. end
  47. endfunction
  48. function [7:0] ct_celsius;
  49. input integer n;
  50. begin
  51. case(n)
  52. 0:ct_celsius=0; 1:ct_celsius=25; 2:ct_celsius=40;
  53. 3:ct_celsius=70; 4:ct_celsius=100; 5:ct_celsius=15;
  54. 6:ct_celsius=16; 7:ct_celsius=17; 8:ct_celsius=18;
  55. default:ct_celsius=8'hff;
  56. endcase
  57. end
  58. endfunction
  59. function [15:0] ct_raw;
  60. input integer n;
  61. begin
  62. case(n)
  63. 0:ct_raw=16'h0000; 1:ct_raw=16'h0cb0; 2:ct_raw=16'h1400;
  64. 3:ct_raw=16'h2378; 4:ct_raw=16'h3200; 5:ct_raw=16'h07c0;
  65. 6:ct_raw=16'h0820; 7:ct_raw=16'h08e0; 8:ct_raw=16'h0900;
  66. 9:ct_raw=16'hff00; 10:ct_raw=16'hff9e; 11:ct_raw=16'h8000;
  67. 12:ct_raw=16'h5959; 13:ct_raw=16'h7428;
  68. default:ct_raw=16'hffff;
  69. endcase
  70. end
  71. endfunction
  72. function [7:0] ct_register;
  73. input integer n;
  74. begin
  75. case(n)
  76. 9:ct_register=8'h23; 10:ct_register=8'h30; 11:ct_register=8'hc4;
  77. 12:ct_register=8'hfe; 13:ct_register=8'hff;
  78. default:ct_register=n;
  79. endcase
  80. end
  81. endfunction
  82. reg [7:0] send_values[0:35], expected[0:35];
  83. task send_byte;
  84. input [7:0] value;
  85. integer k;
  86. begin
  87. host_rx=0; #(BIT_NS);
  88. for(k=0;k<8;k=k+1) begin host_rx=value[k]; #(BIT_NS); end
  89. host_rx=1; #(BIT_NS);
  90. end
  91. endtask
  92. task send_frame;
  93. input [7:0] kind;
  94. input integer length;
  95. input bad_crc;
  96. integer k;
  97. reg [15:0] c;
  98. begin
  99. c=crc_byte(crc_byte(16'hffff,kind),length);
  100. send_byte(8'ha5);send_byte(8'h5a);send_byte(kind);send_byte(length);
  101. for(k=0;k<length;k=k+1) begin
  102. send_byte(send_values[k]);c=crc_byte(c,send_values[k]);
  103. end
  104. if(bad_crc) c=c^16'h0001;
  105. send_byte(c[7:0]);send_byte(c[15:8]);
  106. end
  107. endtask
  108. // Independent continuous receive parser validates EVERY response frame,
  109. // including unsolicited frames emitted while a request is being sent.
  110. reg [7:0] wire_bytes[0:32767];
  111. integer frame_start[0:255], frame_type[0:255], frame_length[0:255];
  112. integer byte_count=0, frame_count=0, frame_cursor=0;
  113. integer auto_count=0, diag_count=0, pending_get=0, pending_auto=0;
  114. reg [7:0] rx_byte;
  115. integer rx_bit,rx_base=0,rx_size=0,k,p,ch;
  116. reg [15:0] rx_crc;
  117. initial forever begin
  118. @(negedge host_tx);
  119. #(1.5*BIT_NS);
  120. for(rx_bit=0;rx_bit<8;rx_bit=rx_bit+1) begin
  121. rx_byte[rx_bit]=host_tx; #(BIT_NS);
  122. end
  123. if(host_tx!==1'b1) $fatal(1,"UART stop bit invalid at nominal921600");
  124. if(byte_count>=32768) $fatal(1,"receive capture overflow");
  125. wire_bytes[byte_count]=rx_byte;
  126. byte_count=byte_count+1;
  127. if(byte_count-rx_base==1 && rx_byte!==8'ha5) $fatal(1,"frame preamble A5 missing");
  128. if(byte_count-rx_base==2 && rx_byte!==8'h5a) $fatal(1,"frame preamble5A missing");
  129. if(byte_count-rx_base==4) rx_size=rx_byte+6;
  130. if(rx_size!=0 && byte_count-rx_base==rx_size) begin
  131. if(frame_count>=256) $fatal(1,"frame capture overflow");
  132. rx_crc=16'hffff;
  133. for(k=rx_base+2;k<byte_count-2;k=k+1) rx_crc=crc_byte(rx_crc,wire_bytes[k]);
  134. if({wire_bytes[byte_count-1],wire_bytes[byte_count-2]}!==rx_crc)
  135. $fatal(1,"response CRC mismatch type=%h",wire_bytes[rx_base+2]);
  136. frame_start[frame_count]=rx_base;
  137. frame_type[frame_count]=wire_bytes[rx_base+2];
  138. frame_length[frame_count]=wire_bytes[rx_base+3];
  139. p=rx_base+4;
  140. if(pending_get && wire_bytes[rx_base+2]!==8'h42)
  141. $fatal(1,"GET_ACK and GET_DATA group interrupted");
  142. if(pending_auto && wire_bytes[rx_base+2]!==8'h52)
  143. $fatal(1,"automatic GET_DATA and DIAGNOSTIC group interrupted");
  144. case(wire_bytes[rx_base+2])
  145. 8'h82:begin
  146. if(wire_bytes[rx_base+3]!==0) $fatal(1,"GET_ACK length");
  147. pending_get=1;
  148. end
  149. 8'h42:begin
  150. if(wire_bytes[rx_base+3]!==90) $fatal(1,"GET_DATA must be 90 bytes");
  151. if(pending_get) pending_get=0;
  152. else begin
  153. // Autonomous GET_DATA must NEVER report EPB1/SB1/SB2 buttons active!
  154. if (wire_bytes[p+86] !== 8'd0 || wire_bytes[p+87] !== 8'd0 || wire_bytes[p+88] !== 8'd0)
  155. $fatal(1,"autonomous GET_DATA leaked buttons=%h/%h/%h", wire_bytes[p+86], wire_bytes[p+87], wire_bytes[p+88]);
  156. pending_auto=1;auto_count=auto_count+1;
  157. end
  158. end
  159. 8'h52:begin
  160. if(!pending_auto) $fatal(1,"unexpected unpaired diagnostic");
  161. pending_auto=0;diag_count=diag_count+1;
  162. if(wire_bytes[p]!==1 || wire_bytes[p+1]!==8'h64 ||
  163. wire_bytes[p+2]!==3 || wire_bytes[p+3]!==3 || wire_bytes[p+4]!==0)
  164. $fatal(1,"diagnostic format/product/version identity");
  165. if(wire_bytes[rx_base+3]!==20) $fatal(1,"diagnostic length");
  166. if(wire_bytes[p+5][5]) begin
  167. ch=wire_bytes[p+7];
  168. if(ch>13 || wire_bytes[p+8]!==8'h49 || wire_bytes[p+9]!==ct_register(ch) ||
  169. wire_bytes[p+10]!==0 || {wire_bytes[p+12],wire_bytes[p+11]}!==ct_raw(ch) ||
  170. wire_bytes[p+13]!==ct_celsius(ch))
  171. $fatal(1,"incoherent diagnostic snapshot ch=%0d reg=%0d raw=%h%h err=%0d C=%0d",ch,wire_bytes[p+9],wire_bytes[p+12],wire_bytes[p+11],wire_bytes[p+10],wire_bytes[p+13]);
  172. end
  173. end
  174. 8'h81:if(wire_bytes[rx_base+3]!==0) $fatal(1,"SET_ACK length");
  175. 8'hc0:if(wire_bytes[rx_base+3]!==2) $fatal(1,"NACK length");
  176. default:$fatal(1,"unexpected response type%h",wire_bytes[rx_base+2]);
  177. endcase
  178. frame_count=frame_count+1;
  179. rx_base=byte_count;rx_size=0;
  180. end
  181. end
  182. always @(host_tx or broadcast_tx) begin
  183. #1;
  184. if($time>100000 && host_tx!==broadcast_tx)
  185. $fatal(1,"host UART broadcast outputs diverged");
  186. end
  187. task await_type;
  188. input integer wanted;
  189. output integer found;
  190. integer cycles;
  191. begin
  192. cycles=0;found=-1;
  193. while(found<0 && cycles<250000) begin
  194. @(negedge clk);cycles=cycles+1;
  195. if(frame_cursor<frame_count) begin
  196. if(frame_type[frame_cursor]==wanted) found=frame_cursor;
  197. else if(frame_type[frame_cursor]!=8'h42 && frame_type[frame_cursor]!=8'h52)
  198. $fatal(1,"expected%h got%h",wanted,frame_type[frame_cursor]);
  199. frame_cursor=frame_cursor+1;
  200. end
  201. end
  202. if(found<0) $fatal(1,"response timeout waiting%h",wanted);
  203. #(BIT_NS); // include complete final stop bit
  204. end
  205. endtask
  206. task get_and_check;
  207. integer idx,base,j;
  208. begin
  209. frame_cursor=frame_count;
  210. send_frame(8'h02,0,0);
  211. await_type(8'h82,idx);
  212. await_type(8'h42,idx);
  213. base=frame_start[idx]+4;
  214. for(j=0;j<32;j=j+1)
  215. if(wire_bytes[base+j]!==expected[j]) $fatal(1,"GET duty offset%0d=%0d expected%0d",j,wire_bytes[base+j],expected[j]);
  216. for(j=0;j<24;j=j+1)
  217. if(wire_bytes[base+32+j]!==8'hff) $fatal(1,"BMC offset%0d moved/invalid mismatch",32+j);
  218. for(j=0;j<27;j=j+1)
  219. if(wire_bytes[base+56+j]!==ct_celsius(j)) $fatal(1,"CT offset%0d=%0d expected%0d",56+j,wire_bytes[base+56+j],ct_celsius(j));
  220. for(j=0;j<3;j=j+1)
  221. if(wire_bytes[base+83+j]!==expected[32+j]) $fatal(1,"SSR append offset%0d=%0d expected%0d",83+j,wire_bytes[base+83+j],expected[32+j]);
  222. if(wire_bytes[base+86]!=={7'd0, estop_btn})
  223. $fatal(1,"GET estop_btn=%0d expected=%0d", wire_bytes[base+86], estop_btn);
  224. if(wire_bytes[base+87]!=={7'd0, hood_open_btn})
  225. $fatal(1,"GET hood_open_btn=%0d expected=%0d", wire_bytes[base+87], hood_open_btn);
  226. if(wire_bytes[base+88]!=={7'd0, hood_close_btn})
  227. $fatal(1,"GET hood_close_btn=%0d expected=%0d", wire_bytes[base+88], hood_close_btn);
  228. if(wire_bytes[base+89]!==expected[35])
  229. $fatal(1,"GET buzzer=%0d expected=%0d", wire_bytes[base+89], expected[35]);
  230. end
  231. endtask
  232. reg measuring_ssr = 0;
  233. integer ssr_counts[0:2];
  234. integer ssr_last_rise[0:2];
  235. integer ssr_high_since[0:2];
  236. reg [2:0] ssr_previous = 0;
  237. always @(ssr) begin
  238. if (measuring_ssr) begin
  239. if (^ssr === 1'bx) $fatal(1, "undefined SSR");
  240. begin : ssr_edge_block
  241. integer cur_cycle, ch_idx;
  242. cur_cycle = $time / 40;
  243. for (ch_idx = 0; ch_idx < 3; ch_idx = ch_idx + 1) begin
  244. if (ssr_previous[ch_idx] && !ssr[ch_idx]) begin
  245. ssr_counts[ch_idx] = ssr_counts[ch_idx] + (cur_cycle - ssr_high_since[ch_idx]);
  246. end else if (!ssr_previous[ch_idx] && ssr[ch_idx]) begin
  247. ssr_high_since[ch_idx] = cur_cycle;
  248. if (ssr_last_rise[ch_idx] >= 0 && cur_cycle - ssr_last_rise[ch_idx] != 1250000)
  249. $fatal(1, "SSR%0d period=%0d clocks, expected 1250000 (50ms / 20Hz)", ch_idx, cur_cycle - ssr_last_rise[ch_idx]);
  250. ssr_last_rise[ch_idx] = cur_cycle;
  251. end
  252. end
  253. ssr_previous = ssr;
  254. end
  255. end
  256. end
  257. task measure_ssr_periods;
  258. input integer periods;
  259. integer j, start_cycle, end_cycle;
  260. begin
  261. @(negedge clk);
  262. #1;
  263. if (^ssr === 1'bx) $fatal(1, "undefined SSR");
  264. start_cycle = $time / 40;
  265. for(j=0;j<3;j=j+1)begin
  266. ssr_counts[j]=0;
  267. ssr_last_rise[j]=-1;
  268. ssr_high_since[j]=start_cycle;
  269. end
  270. ssr_previous=ssr;
  271. measuring_ssr=1;
  272. #(periods * 1250000 * 40);
  273. measuring_ssr=0;
  274. end_cycle = $time / 40;
  275. for(j=0;j<3;j=j+1) begin
  276. if (ssr_previous[j])
  277. ssr_counts[j] = ssr_counts[j] + (end_cycle - ssr_high_since[j]);
  278. if(ssr_counts[j]!=expected[32+j]*12500*periods)
  279. $fatal(1,"SSR%0d highclocks%0d expected%0d",j,ssr_counts[j],expected[32+j]*12500*periods);
  280. end
  281. end
  282. endtask
  283. task measure_ssr;
  284. begin
  285. measure_ssr_periods(1);
  286. end
  287. endtask
  288. task commit_set;
  289. integer idx,j;
  290. begin
  291. frame_cursor=frame_count;send_frame(8'h01,36,0);await_type(8'h81,idx);
  292. for(j=0;j<36;j=j+1)expected[j]=send_values[j];
  293. end
  294. endtask
  295. integer i,idx,start_frames,n;
  296. initial begin
  297. repeat(160000)@(negedge clk);
  298. if(ct_reads<9 || ct_seen!==9'h1ff)$fatal(1,"CT fixture incomplete before checks");
  299. if(ssr!==3'b000 || ptc!==24'b0 || buzzer!==1'b0)$fatal(1,"boot heating/buzzer outputs notzero");
  300. for(i=0;i<36;i=i+1)expected[i]=(i>=24 && i<32)?20:0;
  301. get_and_check;
  302. // Verify active-HIGH V3.0 buttons are reported ONLY on GET_ALL, not autonomously, and cause NO interlock
  303. estop_btn = 1'b1; hood_open_btn = 1'b1; hood_close_btn = 1'b0;
  304. repeat(10)@(negedge clk);
  305. get_and_check;
  306. hood_open_btn = 1'b0; hood_close_btn = 1'b1;
  307. repeat(10)@(negedge clk);
  308. get_and_check;
  309. for(i=0;i<24;i=i+1)send_values[i]=3*i;
  310. send_values[24]=0;send_values[25]=100;send_values[26]=1;send_values[27]=99;
  311. send_values[28]=20;send_values[29]=40;send_values[30]=60;send_values[31]=80;
  312. send_values[32]=0;send_values[33]=37;send_values[34]=100;send_values[35]=1;
  313. commit_set;
  314. begin : check_v3_buzzer_4khz
  315. integer bz_hi, bz_lo, bz_k;
  316. bz_hi = 0; bz_lo = 0;
  317. for (bz_k = 0; bz_k < 6250; bz_k = bz_k + 1) begin
  318. @(negedge clk);
  319. if (buzzer === 1'b1) bz_hi = bz_hi + 1;
  320. else if (buzzer === 1'b0) bz_lo = bz_lo + 1;
  321. end
  322. if (bz_hi !== 5000 || bz_lo !== 1250)
  323. $fatal(1,"buzzer 4000Hz 80%% mismatch on SET36 buzzer=1: high=%0d low=%0d", bz_hi, bz_lo);
  324. end
  325. get_and_check;
  326. fork
  327. measure_ssr_periods(2);
  328. begin
  329. #(600000 * 40); commit_set;
  330. #(600000 * 40); commit_set;
  331. #(600000 * 40); commit_set;
  332. end
  333. join
  334. get_and_check;
  335. $display("PASS V3 SET36/GET90 independent CRC, stable temperature offsets, SSR0/37/100 duties, EPB1/SB1/SB2 pass-through & 4000Hz 80%% buzzer");
  336. for(i=0;i<36;i=i+1)send_values[i]=100;
  337. send_values[35]=0;
  338. frame_cursor=frame_count;send_frame(8'h01,32,0);await_type(8'hc0,idx);
  339. if(wire_bytes[frame_start[idx]+4]!==1 || wire_bytes[frame_start[idx]+5]!==2)
  340. $fatal(1,"legacySET32 must rejectBAD_LENGTH");
  341. get_and_check;
  342. send_values[33]=101;
  343. frame_cursor=frame_count;send_frame(8'h01,36,0);await_type(8'hc0,idx);
  344. if(wire_bytes[frame_start[idx]+4]!==1 || wire_bytes[frame_start[idx]+5]!==3)
  345. $fatal(1,"invalidSSR101 must rejectBAD_VALUE");
  346. send_values[33]=100; send_values[35]=2;
  347. frame_cursor=frame_count;send_frame(8'h01,36,0);await_type(8'hc0,idx);
  348. if(wire_bytes[frame_start[idx]+4]!==1 || wire_bytes[frame_start[idx]+5]!==3)
  349. $fatal(1,"invalid buzzer=2 must rejectBAD_VALUE");
  350. get_and_check;
  351. send_values[35]=1;
  352. start_frames=frame_count;send_frame(8'h01,36,1);
  353. #(100000 * 40);
  354. @(negedge clk);
  355. for(i=start_frames;i<frame_count;i=i+1)
  356. if(frame_type[i]!=8'h42 && frame_type[i]!=8'h52)$fatal(1,"badCRC generatedreply");
  357. get_and_check;
  358. $display("PASS V3 whole-frame rejection: legacy length, late SSR/buzzer invalid value, corruptCRC preserve prior outputs");
  359. for(i=0;i<32;i=i+1)send_values[i]=100-2*i;
  360. send_values[32]=100;send_values[33]=1;send_values[34]=99;send_values[35]=1;
  361. commit_set;get_and_check;
  362. // AUTO remains active throughout this silence; it must never feed WD or leak buttons.
  363. #(1500000 * 40);
  364. @(negedge clk);
  365. if(ssr!==0 || ptc!==0 || buzzer!==0)$fatal(1,"watchdog failed tozero heating/buzzer outputs");
  366. for(i=0;i<36;i=i+1)expected[i]=(i>=24 && i<32)?20:0;
  367. estop_btn = 1'b0; hood_open_btn = 1'b0; hood_close_btn = 1'b0;
  368. get_and_check;
  369. if(ssr!==0 || ptc!==0 || buzzer!==0)$fatal(1,"GET resurrected expired heating/buzzer outputs");
  370. commit_set;get_and_check;measure_ssr;
  371. if(auto_count<4 || diag_count<4)$fatal(1,"insufficient automatic status/diagnostics");
  372. $display("PASS prod2u_v3_protocol_tb: nominalUART,allCRC,GET90temp-offsets,SSR0/1/37/99/100,SET36atomicity,EPB1/SB1/SB2,buzzer4kHz80,legacy32NACK,badCRC/value,recovery,watchdog,AUTO42+52=%0d/%0d",auto_count,diag_count);
  373. $finish;
  374. end
  375. initial begin #1200000000;$fatal(1,"V3 real-top regressiontimeout");end
  376. endmodule