| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- `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<length;k=k+1) begin
- send_byte(send_values[k]);c=crc_byte(c,send_values[k]);
- end
- if(bad_crc) c=c^16'h0001;
- send_byte(c[7:0]);send_byte(c[15:8]);
- end
- endtask
- // Independent continuous receive parser validates EVERY response frame,
- // including unsolicited frames emitted while a request is being sent.
- reg [7:0] wire_bytes[0:32767];
- integer frame_start[0:255], frame_type[0:255], frame_length[0:255];
- integer byte_count=0, frame_count=0, frame_cursor=0;
- integer auto_count=0, diag_count=0, pending_get=0, pending_auto=0;
- reg [7:0] rx_byte;
- integer rx_bit,rx_base=0,rx_size=0,k,p,ch;
- reg [15:0] rx_crc;
- initial forever begin
- @(negedge host_tx);
- #(1.5*BIT_NS);
- for(rx_bit=0;rx_bit<8;rx_bit=rx_bit+1) begin
- rx_byte[rx_bit]=host_tx; #(BIT_NS);
- end
- if(host_tx!==1'b1) $fatal(1,"UART stop bit invalid at nominal921600");
- if(byte_count>=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;k<byte_count-2;k=k+1) rx_crc=crc_byte(rx_crc,wire_bytes[k]);
- if({wire_bytes[byte_count-1],wire_bytes[byte_count-2]}!==rx_crc)
- $fatal(1,"response CRC mismatch type=%h",wire_bytes[rx_base+2]);
- frame_start[frame_count]=rx_base;
- frame_type[frame_count]=wire_bytes[rx_base+2];
- frame_length[frame_count]=wire_bytes[rx_base+3];
- p=rx_base+4;
- if(pending_get && wire_bytes[rx_base+2]!==8'h42)
- $fatal(1,"GET_ACK and GET_DATA group interrupted");
- if(pending_auto && wire_bytes[rx_base+2]!==8'h52)
- $fatal(1,"automatic GET_DATA and DIAGNOSTIC group interrupted");
- case(wire_bytes[rx_base+2])
- 8'h82:begin
- if(wire_bytes[rx_base+3]!==0) $fatal(1,"GET_ACK length");
- pending_get=1;
- end
- 8'h42:begin
- if(wire_bytes[rx_base+3]!==90) $fatal(1,"GET_DATA must be 90 bytes");
- if(pending_get) pending_get=0;
- else begin
- // Autonomous GET_DATA must NEVER report EPB1/SB1/SB2 buttons active!
- if (wire_bytes[p+86] !== 8'd0 || wire_bytes[p+87] !== 8'd0 || wire_bytes[p+88] !== 8'd0)
- $fatal(1,"autonomous GET_DATA leaked buttons=%h/%h/%h", wire_bytes[p+86], wire_bytes[p+87], wire_bytes[p+88]);
- pending_auto=1;auto_count=auto_count+1;
- end
- end
- 8'h52:begin
- if(!pending_auto) $fatal(1,"unexpected unpaired diagnostic");
- pending_auto=0;diag_count=diag_count+1;
- if(wire_bytes[p]!==1 || wire_bytes[p+1]!==8'h64 ||
- wire_bytes[p+2]!==3 || wire_bytes[p+3]!==3 || wire_bytes[p+4]!==0)
- $fatal(1,"diagnostic format/product/version identity");
- if(wire_bytes[rx_base+3]!==20) $fatal(1,"diagnostic length");
- if(wire_bytes[p+5][5]) begin
- ch=wire_bytes[p+7];
- if(ch>13 || 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<frame_count) begin
- if(frame_type[frame_cursor]==wanted) found=frame_cursor;
- else if(frame_type[frame_cursor]!=8'h42 && frame_type[frame_cursor]!=8'h52)
- $fatal(1,"expected%h got%h",wanted,frame_type[frame_cursor]);
- frame_cursor=frame_cursor+1;
- end
- end
- if(found<0) $fatal(1,"response timeout waiting%h",wanted);
- #(BIT_NS); // include complete final stop bit
- end
- endtask
- task get_and_check;
- integer idx,base,j;
- begin
- frame_cursor=frame_count;
- send_frame(8'h02,0,0);
- await_type(8'h82,idx);
- await_type(8'h42,idx);
- base=frame_start[idx]+4;
- for(j=0;j<32;j=j+1)
- if(wire_bytes[base+j]!==expected[j]) $fatal(1,"GET duty offset%0d=%0d expected%0d",j,wire_bytes[base+j],expected[j]);
- for(j=0;j<24;j=j+1)
- if(wire_bytes[base+32+j]!==8'hff) $fatal(1,"BMC offset%0d moved/invalid mismatch",32+j);
- for(j=0;j<27;j=j+1)
- 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));
- for(j=0;j<3;j=j+1)
- 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]);
- if(wire_bytes[base+86]!=={7'd0, estop_btn})
- $fatal(1,"GET estop_btn=%0d expected=%0d", wire_bytes[base+86], estop_btn);
- if(wire_bytes[base+87]!=={7'd0, hood_open_btn})
- $fatal(1,"GET hood_open_btn=%0d expected=%0d", wire_bytes[base+87], hood_open_btn);
- if(wire_bytes[base+88]!=={7'd0, hood_close_btn})
- $fatal(1,"GET hood_close_btn=%0d expected=%0d", wire_bytes[base+88], hood_close_btn);
- if(wire_bytes[base+89]!==expected[35])
- $fatal(1,"GET buzzer=%0d expected=%0d", wire_bytes[base+89], expected[35]);
- end
- endtask
- reg measuring_ssr = 0;
- integer ssr_counts[0:2];
- integer ssr_last_rise[0:2];
- integer ssr_high_since[0:2];
- reg [2:0] ssr_previous = 0;
- always @(ssr) begin
- if (measuring_ssr) begin
- if (^ssr === 1'bx) $fatal(1, "undefined SSR");
- begin : ssr_edge_block
- integer cur_cycle, ch_idx;
- cur_cycle = $time / 40;
- for (ch_idx = 0; ch_idx < 3; ch_idx = ch_idx + 1) begin
- if (ssr_previous[ch_idx] && !ssr[ch_idx]) begin
- ssr_counts[ch_idx] = ssr_counts[ch_idx] + (cur_cycle - ssr_high_since[ch_idx]);
- end else if (!ssr_previous[ch_idx] && ssr[ch_idx]) begin
- ssr_high_since[ch_idx] = cur_cycle;
- if (ssr_last_rise[ch_idx] >= 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<frame_count;i=i+1)
- if(frame_type[i]!=8'h42 && frame_type[i]!=8'h52)$fatal(1,"badCRC generatedreply");
- get_and_check;
- $display("PASS V3 whole-frame rejection: legacy length, late SSR/buzzer invalid value, corruptCRC preserve prior outputs");
- for(i=0;i<32;i=i+1)send_values[i]=100-2*i;
- send_values[32]=100;send_values[33]=1;send_values[34]=99;send_values[35]=1;
- commit_set;get_and_check;
- // AUTO remains active throughout this silence; it must never feed WD or leak buttons.
- #(1500000 * 40);
- @(negedge clk);
- if(ssr!==0 || ptc!==0 || buzzer!==0)$fatal(1,"watchdog failed tozero heating/buzzer outputs");
- for(i=0;i<36;i=i+1)expected[i]=(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
|