| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- `timescale 1ns/1ps
- // External-pin test: successful reads, unplugged chip, stuck-low bus and recovery.
- // No DUT register forcing; expected raw words are independent model literals.
- module ct7428_diagnostics_tb;
- reg clk=0, rst_n=0;
- always #20 clk=~clk; // production 25 MHz
- tri1 scl, sda, sensor_sda;
- reg connected=1, stuck_scl=0;
- wire scl_low, sda_low, write_en, round_done;
- wire [5:0] write_addr;
- wire [7:0] write_data;
- wire [103:0] diag;
- assign scl = (scl_low || stuck_scl) ? 1'b0 : 1'bz;
- assign sda = sda_low ? 1'b0 : 1'bz;
- tranif1 sensor_link(sda,sensor_sda,connected);
- ct7428_normal_slave_bfm model(.scl(scl),.sda(sensor_sda),.completed_reads(),.registers_seen());
- ct7428_scan #(.CHANNEL_LAST(5'd8)) dut(
- .clk(clk),.rst_n(rst_n),.scl_i(scl),.sda_i(sda),
- .scl_drive_low(scl_low),.sda_drive_low(sda_low),
- .temp_wr_addr(write_addr),.temp_wr_data(write_data),.temp_wr_en(write_en),
- .scan_channel(),.scan_round_done(round_done),.diagnostic_data(diag));
- function [7:0] byte_at;
- input integer n;
- begin byte_at=diag[n*8+:8]; end
- endfunction
- task round_check;
- input [7:0] expected_round, expected_error, expected_temp, expected_results;
- input [15:0] expected_raw, expected_valid;
- begin
- @(posedge round_done); #1;
- if (byte_at(11)!==expected_round || byte_at(5)!==expected_error ||
- byte_at(8)!==expected_temp || byte_at(12)!==expected_results ||
- {byte_at(7),byte_at(6)}!==expected_raw ||
- {byte_at(10),byte_at(9)}!==expected_valid ||
- byte_at(2)!==8 || byte_at(3)!==8'h49 || byte_at(4)!==8)
- $fatal(1,"diagnostic mismatch round=%0d err=%0d temp=%0d results=%h raw=%h valid=%h channel=%0d addr=%h reg=%h",
- byte_at(11),byte_at(5),byte_at(8),byte_at(12),{byte_at(7),byte_at(6)},
- {byte_at(10),byte_at(9)},byte_at(2),byte_at(3),byte_at(4));
- if (!diag[5]) $fatal(1,"completed sample not marked seen");
- end
- endtask
- integer samples=0;
- reg [5:0] sampled_addr;
- reg [7:0] sampled_data;
- always @(posedge clk) begin
- if (rst_n && write_en) begin
- sampled_addr=write_addr;
- sampled_data=write_data;
- #1;
- if (byte_at(2)!==(sampled_addr-6'd32) || byte_at(8)!==sampled_data)
- $fatal(1,"diagnostic channel/value mispaired with temperature write");
- samples=samples+1;
- end
- end
- initial begin
- repeat(4) @(negedge clk);
- if (diag[5]!==0 || byte_at(2)!==8'hff || byte_at(5)!==8'h0f ||
- {byte_at(7),byte_at(6)}!==16'hffff || byte_at(12)!==0)
- $fatal(1,"boot diagnostic must distinguish never sampled");
- rst_n=1;
- round_check(1,0,18,1,16'h0900,16'h01ff);
- connected=0;
- round_check(2,1,8'hff,3,16'h0000,0);
- stuck_scl=1;
- repeat(5) @(negedge clk);
- if (diag[0]!==0) $fatal(1,"diagnostic SCL sample does not show stuck low");
- round_check(3,5,8'hff,8'h23,16'h0000,0);
- stuck_scl=0;
- connected=1;
- round_check(4,0,18,8'h23,16'h0900,16'h01ff);
- if(samples!=36) $fatal(1,"wrong number of completed samples %0d",samples);
- $display("PASS ct7428_diagnostics_tb: 36 external-pin reads, success/NACK/stuck/recovery; coherent channel/raw/error; production I2C timing");
- $finish;
- end
- initial begin #100000000; $fatal(1,"diagnostic test timeout"); end
- endmodule
|