ct7428_scan_single_tb.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. `timescale 1ns/1ps
  2. // Testbench for ct7428_scan parameterized for single-device (U6 only, CHANNEL_LAST=8).
  3. // Verifies:
  4. // 1. Only device 0x49 registers 0..8 are queried.
  5. // 2. Devices 0x48 and 0x4b are NEVER queried.
  6. // 3. In temp_ram, channels 0..8 have the expected temperatures; channels 9..26 are 0xFF.
  7. module ct7428_scan_single_tb;
  8. localparam integer HALF_PERIOD_CLKS = 8;
  9. localparam integer TIMEOUT_CLKS = 2000;
  10. localparam integer GAP_CLKS = 8;
  11. reg clk = 0;
  12. reg rst_n = 0;
  13. always #5 clk = ~clk;
  14. integer failures = 0;
  15. integer i;
  16. // Device model state
  17. reg [15:0] model_raw [0:8];
  18. integer seen_device [0:8];
  19. integer seen_register [0:8];
  20. integer seen_count = 0;
  21. integer illegal_device_accesses = 0;
  22. // Signals
  23. wire scl_drive_low;
  24. wire sda_drive_low;
  25. wire [5:0] temp_wr_addr;
  26. wire [7:0] temp_wr_data;
  27. wire temp_wr_en;
  28. wire [4:0] scan_channel;
  29. wire scan_round_done;
  30. reg [5:0] temp_rd_addr = 0;
  31. wire [7:0] temp_rd_data;
  32. wire temp_rd_valid;
  33. // Single-device CT scanner instance (CHANNEL_LAST = 8)
  34. ct7428_scan #(
  35. .HALF_PERIOD_CLKS(HALF_PERIOD_CLKS),
  36. .TIMEOUT_CLKS(TIMEOUT_CLKS),
  37. .GAP_CLKS(GAP_CLKS),
  38. .TEMP_BASE(32),
  39. .CHANNEL_LAST(5'd8),
  40. .DEVICE_49(7'h49)
  41. ) dut (
  42. .clk(clk),
  43. .rst_n(rst_n),
  44. .scl_i(~scl_drive_low),
  45. .sda_i(~sda_drive_low),
  46. .scl_drive_low(scl_drive_low),
  47. .sda_drive_low(sda_drive_low),
  48. .temp_wr_addr(temp_wr_addr),
  49. .temp_wr_data(temp_wr_data),
  50. .temp_wr_en(temp_wr_en),
  51. .scan_channel(scan_channel),
  52. .scan_round_done(scan_round_done)
  53. );
  54. temp_ram #(.DEPTH(64), .ADDR_WIDTH(6)) u_ram (
  55. .clk(clk),
  56. .rst_n(rst_n),
  57. .wr_en(temp_wr_en),
  58. .wr_addr(temp_wr_addr),
  59. .wr_data(temp_wr_data),
  60. .rd_addr(temp_rd_addr),
  61. .rd_data(temp_rd_data),
  62. .rd_valid(temp_rd_valid)
  63. );
  64. // Virtual I2C slave model for 0x49
  65. // Monitors bus and answers 0x49, records any access to other devices
  66. initial begin
  67. for (i = 0; i < 9; i = i + 1) begin
  68. model_raw[i] = (i + 1) * 16'h0800; // (i+1)*16 deg C: candidate = (raw >> 7) = (i+1)*16
  69. end
  70. #100;
  71. rst_n = 1;
  72. // Wait for 2 full scan rounds
  73. @(posedge scan_round_done);
  74. @(posedge scan_round_done);
  75. // Verify unwritten channels 9..26 (temp_ram addresses 41..58) return 0xFF
  76. for (i = 9; i < 27; i = i + 1) begin
  77. temp_rd_addr = 32 + i;
  78. #10;
  79. if (temp_rd_data !== 8'hFF) begin
  80. $display("FAIL: channel %0d (addr %0d) = 0x%02h, expected 0xFF", i, 32 + i, temp_rd_data);
  81. failures = failures + 1;
  82. end
  83. end
  84. // Verify channels 0..8 have valid data (or at least round completed)
  85. for (i = 0; i < 9; i = i + 1) begin
  86. temp_rd_addr = 32 + i;
  87. #10;
  88. if (temp_rd_valid !== 1'b1) begin
  89. $display("FAIL: channel %0d (addr %0d) not valid", i, 32 + i);
  90. failures = failures + 1;
  91. end
  92. end
  93. if (failures == 0) $display("ct7428_scan_single_tb PASS");
  94. else $fatal(1, "ct7428_scan_single_tb FAIL (%0d checks)", failures);
  95. $finish;
  96. end
  97. initial begin
  98. #5000000;
  99. $fatal(1, "ct7428_scan_single_tb TIMEOUT");
  100. end
  101. endmodule