`timescale 1ns/1ps // Independent V3 real-top regression. All business inputs enter actual UART // and I2C pads; only long watchdog/beacon periods are elaboration-shortened. // No forced RTL state, RAM injection, generated expected payload or DUT CRC. module prod2u_v3_protocol_tb; localparam real BIT_NS = 1000000000.0 / 921600.0; reg clk=0; always #20 clk=~clk; reg host_rx=1; wire host_tx, broadcast_tx; tri1 scl,sda; wire [23:0] ptc; wire [3:0] front,rear; wire [2:0] ssr; reg estop_btn = 0, hood_open_btn = 0, hood_close_btn = 0; wire buzzer; wire [31:0] ct_reads; wire [8:0] ct_seen; prod2u_6400_v2_top #(.HOST_BAUD(921600), .WD_TIMEOUT_CLKS(1400000)) dut ( .sys_clk(clk), .host_ttl_rx(host_rx), .host_ttl_tx(host_tx), .host_rs232_rx(1'b1), .host_rs232_tx(broadcast_tx), .bmc_rs232_rx(1'b1), .bmc_rs232_tx(), .ptc_pwm_out(ptc), .front_fan_pwm_out(front), .rear_fan_pwm_out(rear), .temp_scl(scl), .temp_sda(sda), .temp_scl_alt(), .temp_sda_alt(), .ssr1_out(ssr[0]), .ssr2_out(ssr[1]), .ssr4_out(ssr[2]), .ssr_unused_zero(), .ptc_spare_zero(), .estop_btn_in(estop_btn), .hood_open_btn_in(hood_open_btn), .hood_close_btn_in(hood_close_btn), .buzzer_out(buzzer) ); defparam dut.u_core.BEACON_PERIOD = 400000; always @(dut.ssr_unused_zero) begin #1; if ($time > 100 && dut.ssr_unused_zero !== 3'b000) $fatal(1, "retired SSR pads must stay low"); end ct7428_normal_slave_bfm #(.DIAGNOSTIC_REGISTERS(1)) ct (.scl(scl), .sda(sda), .completed_reads(ct_reads), .registers_seen(ct_seen)); function [15:0] crc_byte; input [15:0] prior; input [7:0] value; reg [15:0] c; integer k; begin c=prior ^ {value,8'h00}; for(k=0;k<8;k=k+1) c=c[15] ? ((c<<1)^16'h1021) : (c<<1); crc_byte=c; end endfunction function [7:0] ct_celsius; input integer n; begin case(n) 0:ct_celsius=0; 1:ct_celsius=25; 2:ct_celsius=40; 3:ct_celsius=70; 4:ct_celsius=100; 5:ct_celsius=15; 6:ct_celsius=16; 7:ct_celsius=17; 8:ct_celsius=18; default:ct_celsius=8'hff; endcase end endfunction function [15:0] ct_raw; input integer n; begin case(n) 0:ct_raw=16'h0000; 1:ct_raw=16'h0cb0; 2:ct_raw=16'h1400; 3:ct_raw=16'h2378; 4:ct_raw=16'h3200; 5:ct_raw=16'h07c0; 6:ct_raw=16'h0820; 7:ct_raw=16'h08e0; 8:ct_raw=16'h0900; 9:ct_raw=16'hff00; 10:ct_raw=16'hff9e; 11:ct_raw=16'h8000; 12:ct_raw=16'h5959; 13:ct_raw=16'h7428; default:ct_raw=16'hffff; endcase end endfunction function [7:0] ct_register; input integer n; begin case(n) 9:ct_register=8'h23; 10:ct_register=8'h30; 11:ct_register=8'hc4; 12:ct_register=8'hfe; 13:ct_register=8'hff; default:ct_register=n; endcase end endfunction reg [7:0] send_values[0:35], expected[0:35]; task send_byte; input [7:0] value; integer k; begin host_rx=0; #(BIT_NS); for(k=0;k<8;k=k+1) begin host_rx=value[k]; #(BIT_NS); end host_rx=1; #(BIT_NS); end endtask task send_frame; input [7:0] kind; input integer length; input bad_crc; integer k; reg [15:0] c; begin c=crc_byte(crc_byte(16'hffff,kind),length); send_byte(8'ha5);send_byte(8'h5a);send_byte(kind);send_byte(length); for(k=0;k=32768) $fatal(1,"receive capture overflow"); wire_bytes[byte_count]=rx_byte; byte_count=byte_count+1; if(byte_count-rx_base==1 && rx_byte!==8'ha5) $fatal(1,"frame preamble A5 missing"); if(byte_count-rx_base==2 && rx_byte!==8'h5a) $fatal(1,"frame preamble5A missing"); if(byte_count-rx_base==4) rx_size=rx_byte+6; if(rx_size!=0 && byte_count-rx_base==rx_size) begin if(frame_count>=256) $fatal(1,"frame capture overflow"); rx_crc=16'hffff; for(k=rx_base+2;k13 || wire_bytes[p+8]!==8'h49 || wire_bytes[p+9]!==ct_register(ch) || wire_bytes[p+10]!==0 || {wire_bytes[p+12],wire_bytes[p+11]}!==ct_raw(ch) || wire_bytes[p+13]!==ct_celsius(ch)) $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]); end end 8'h81:if(wire_bytes[rx_base+3]!==0) $fatal(1,"SET_ACK length"); 8'hc0:if(wire_bytes[rx_base+3]!==2) $fatal(1,"NACK length"); default:$fatal(1,"unexpected response type%h",wire_bytes[rx_base+2]); endcase frame_count=frame_count+1; rx_base=byte_count;rx_size=0; end end always @(host_tx or broadcast_tx) begin #1; if($time>100000 && host_tx!==broadcast_tx) $fatal(1,"host UART broadcast outputs diverged"); end task await_type; input integer wanted; output integer found; integer cycles; begin cycles=0;found=-1; while(found<0 && cycles<250000) begin @(negedge clk);cycles=cycles+1; if(frame_cursor= 0 && cur_cycle - ssr_last_rise[ch_idx] != 1250000) $fatal(1, "SSR%0d period=%0d clocks, expected 1250000 (50ms / 20Hz)", ch_idx, cur_cycle - ssr_last_rise[ch_idx]); ssr_last_rise[ch_idx] = cur_cycle; end end ssr_previous = ssr; end end end task measure_ssr_periods; input integer periods; integer j, start_cycle, end_cycle; begin @(negedge clk); #1; if (^ssr === 1'bx) $fatal(1, "undefined SSR"); start_cycle = $time / 40; for(j=0;j<3;j=j+1)begin ssr_counts[j]=0; ssr_last_rise[j]=-1; ssr_high_since[j]=start_cycle; end ssr_previous=ssr; measuring_ssr=1; #(periods * 1250000 * 40); measuring_ssr=0; end_cycle = $time / 40; for(j=0;j<3;j=j+1) begin if (ssr_previous[j]) ssr_counts[j] = ssr_counts[j] + (end_cycle - ssr_high_since[j]); if(ssr_counts[j]!=expected[32+j]*12500*periods) $fatal(1,"SSR%0d highclocks%0d expected%0d",j,ssr_counts[j],expected[32+j]*12500*periods); end end endtask task measure_ssr; begin measure_ssr_periods(1); end endtask task commit_set; integer idx,j; begin frame_cursor=frame_count;send_frame(8'h01,36,0);await_type(8'h81,idx); for(j=0;j<36;j=j+1)expected[j]=send_values[j]; end endtask integer i,idx,start_frames,n; initial begin repeat(160000)@(negedge clk); if(ct_reads<9 || ct_seen!==9'h1ff)$fatal(1,"CT fixture incomplete before checks"); if(ssr!==3'b000 || ptc!==24'b0 || buzzer!==1'b0)$fatal(1,"boot heating/buzzer outputs notzero"); for(i=0;i<36;i=i+1)expected[i]=(i>=24 && i<32)?20:0; get_and_check; // Verify active-HIGH V3.0 buttons are reported ONLY on GET_ALL, not autonomously, and cause NO interlock estop_btn = 1'b1; hood_open_btn = 1'b1; hood_close_btn = 1'b0; repeat(10)@(negedge clk); get_and_check; hood_open_btn = 1'b0; hood_close_btn = 1'b1; repeat(10)@(negedge clk); get_and_check; for(i=0;i<24;i=i+1)send_values[i]=3*i; send_values[24]=0;send_values[25]=100;send_values[26]=1;send_values[27]=99; send_values[28]=20;send_values[29]=40;send_values[30]=60;send_values[31]=80; send_values[32]=0;send_values[33]=37;send_values[34]=100;send_values[35]=1; commit_set; begin : check_v3_buzzer_4khz integer bz_hi, bz_lo, bz_k; bz_hi = 0; bz_lo = 0; for (bz_k = 0; bz_k < 6250; bz_k = bz_k + 1) begin @(negedge clk); if (buzzer === 1'b1) bz_hi = bz_hi + 1; else if (buzzer === 1'b0) bz_lo = bz_lo + 1; end if (bz_hi !== 5000 || bz_lo !== 1250) $fatal(1,"buzzer 4000Hz 80%% mismatch on SET36 buzzer=1: high=%0d low=%0d", bz_hi, bz_lo); end get_and_check; fork measure_ssr_periods(2); begin #(600000 * 40); commit_set; #(600000 * 40); commit_set; #(600000 * 40); commit_set; end join get_and_check; $display("PASS V3 SET36/GET90 independent CRC, stable temperature offsets, SSR0/37/100 duties, EPB1/SB1/SB2 pass-through & 4000Hz 80%% buzzer"); for(i=0;i<36;i=i+1)send_values[i]=100; send_values[35]=0; frame_cursor=frame_count;send_frame(8'h01,32,0);await_type(8'hc0,idx); if(wire_bytes[frame_start[idx]+4]!==1 || wire_bytes[frame_start[idx]+5]!==2) $fatal(1,"legacySET32 must rejectBAD_LENGTH"); get_and_check; send_values[33]=101; frame_cursor=frame_count;send_frame(8'h01,36,0);await_type(8'hc0,idx); if(wire_bytes[frame_start[idx]+4]!==1 || wire_bytes[frame_start[idx]+5]!==3) $fatal(1,"invalidSSR101 must rejectBAD_VALUE"); send_values[33]=100; send_values[35]=2; frame_cursor=frame_count;send_frame(8'h01,36,0);await_type(8'hc0,idx); if(wire_bytes[frame_start[idx]+4]!==1 || wire_bytes[frame_start[idx]+5]!==3) $fatal(1,"invalid buzzer=2 must rejectBAD_VALUE"); get_and_check; send_values[35]=1; start_frames=frame_count;send_frame(8'h01,36,1); #(100000 * 40); @(negedge clk); for(i=start_frames;i=24 && i<32)?20:0; estop_btn = 1'b0; hood_open_btn = 1'b0; hood_close_btn = 1'b0; get_and_check; if(ssr!==0 || ptc!==0 || buzzer!==0)$fatal(1,"GET resurrected expired heating/buzzer outputs"); commit_set;get_and_check;measure_ssr; if(auto_count<4 || diag_count<4)$fatal(1,"insufficient automatic status/diagnostics"); $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); $finish; end initial begin #1200000000;$fatal(1,"V3 real-top regressiontimeout");end endmodule