ct7428_diagnostics_tb.v 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. `timescale 1ns/1ps
  2. // External-pin test: successful reads, unplugged chip, stuck-low bus and recovery.
  3. // No DUT register forcing; expected raw words are independent model literals.
  4. module ct7428_diagnostics_tb;
  5. reg clk=0, rst_n=0;
  6. always #20 clk=~clk; // production 25 MHz
  7. tri1 scl, sda, sensor_sda;
  8. reg connected=1, stuck_scl=0;
  9. wire scl_low, sda_low, write_en, round_done;
  10. wire [5:0] write_addr;
  11. wire [7:0] write_data;
  12. wire [103:0] diag;
  13. assign scl = (scl_low || stuck_scl) ? 1'b0 : 1'bz;
  14. assign sda = sda_low ? 1'b0 : 1'bz;
  15. tranif1 sensor_link(sda,sensor_sda,connected);
  16. ct7428_normal_slave_bfm model(.scl(scl),.sda(sensor_sda),.completed_reads(),.registers_seen());
  17. ct7428_scan #(.CHANNEL_LAST(5'd8)) dut(
  18. .clk(clk),.rst_n(rst_n),.scl_i(scl),.sda_i(sda),
  19. .scl_drive_low(scl_low),.sda_drive_low(sda_low),
  20. .temp_wr_addr(write_addr),.temp_wr_data(write_data),.temp_wr_en(write_en),
  21. .scan_channel(),.scan_round_done(round_done),.diagnostic_data(diag));
  22. function [7:0] byte_at;
  23. input integer n;
  24. begin byte_at=diag[n*8+:8]; end
  25. endfunction
  26. task round_check;
  27. input [7:0] expected_round, expected_error, expected_temp, expected_results;
  28. input [15:0] expected_raw, expected_valid;
  29. begin
  30. @(posedge round_done); #1;
  31. if (byte_at(11)!==expected_round || byte_at(5)!==expected_error ||
  32. byte_at(8)!==expected_temp || byte_at(12)!==expected_results ||
  33. {byte_at(7),byte_at(6)}!==expected_raw ||
  34. {byte_at(10),byte_at(9)}!==expected_valid ||
  35. byte_at(2)!==8 || byte_at(3)!==8'h49 || byte_at(4)!==8)
  36. $fatal(1,"diagnostic mismatch round=%0d err=%0d temp=%0d results=%h raw=%h valid=%h channel=%0d addr=%h reg=%h",
  37. byte_at(11),byte_at(5),byte_at(8),byte_at(12),{byte_at(7),byte_at(6)},
  38. {byte_at(10),byte_at(9)},byte_at(2),byte_at(3),byte_at(4));
  39. if (!diag[5]) $fatal(1,"completed sample not marked seen");
  40. end
  41. endtask
  42. integer samples=0;
  43. reg [5:0] sampled_addr;
  44. reg [7:0] sampled_data;
  45. always @(posedge clk) begin
  46. if (rst_n && write_en) begin
  47. sampled_addr=write_addr;
  48. sampled_data=write_data;
  49. #1;
  50. if (byte_at(2)!==(sampled_addr-6'd32) || byte_at(8)!==sampled_data)
  51. $fatal(1,"diagnostic channel/value mispaired with temperature write");
  52. samples=samples+1;
  53. end
  54. end
  55. initial begin
  56. repeat(4) @(negedge clk);
  57. if (diag[5]!==0 || byte_at(2)!==8'hff || byte_at(5)!==8'h0f ||
  58. {byte_at(7),byte_at(6)}!==16'hffff || byte_at(12)!==0)
  59. $fatal(1,"boot diagnostic must distinguish never sampled");
  60. rst_n=1;
  61. round_check(1,0,18,1,16'h0900,16'h01ff);
  62. connected=0;
  63. round_check(2,1,8'hff,3,16'h0000,0);
  64. stuck_scl=1;
  65. repeat(5) @(negedge clk);
  66. if (diag[0]!==0) $fatal(1,"diagnostic SCL sample does not show stuck low");
  67. round_check(3,5,8'hff,8'h23,16'h0000,0);
  68. stuck_scl=0;
  69. connected=1;
  70. round_check(4,0,18,8'h23,16'h0900,16'h01ff);
  71. if(samples!=36) $fatal(1,"wrong number of completed samples %0d",samples);
  72. $display("PASS ct7428_diagnostics_tb: 36 external-pin reads, success/NACK/stuck/recovery; coherent channel/raw/error; production I2C timing");
  73. $finish;
  74. end
  75. initial begin #100000000; $fatal(1,"diagnostic test timeout"); end
  76. endmodule