ct7428_scan_tb.v 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. `timescale 1ns/1ps
  2. // Testbench for ct7428_scan + temp_ram with a bit-level virtual CT7428 device
  3. // model. Expected bytes are literal constants computed independently from the
  4. // converter contract (raw >> 3 counts 0.0625 C, so the integer Celsius byte is
  5. // raw >> 7; negative / >= 255 C / failed transactions are 0xFF) - the TB never
  6. // derives an expectation from a DUT output.
  7. //
  8. // Scenarios (default mode):
  9. // 1. every channel is 0xFF before the first sample (never sampled).
  10. // 2. a full 27-channel round with 0 C, fractional values, the 254/255 C
  11. // boundary, negative-zero 0x8000 and 0xffff (negative).
  12. // 3. the I2C address/register order the device model observes is exactly
  13. // 0x49 regs 0..8, 0x48 regs 0..8, 0x4b regs 0..8.
  14. // 4. a NACK on one channel writes 0xFF only there and the scan continues.
  15. // 5. a device that holds SCL low makes the master time out; that channel
  16. // becomes 0xFF, the device releases the bus while the next request is
  17. // already pending and the next channel still receives its own data (a
  18. // late response can never land on the next channel).
  19. // 6. later rounds with new device values update every byte (continuous
  20. // scanning), including 0x00 for a real 0 C reading.
  21. //
  22. // With +VECTOR_FILE=<path> the TB reads 27 "<raw_hex> <err>" lines, runs one
  23. // round and prints "RESULT <channel> <byte_hex>" for tests/unit to compare
  24. // against independently computed expectations.
  25. module ct7428_scan_tb;
  26. localparam integer HALF_PERIOD_CLKS = 8;
  27. localparam integer TIMEOUT_CLKS = 2000;
  28. localparam integer GAP_CLKS = 8;
  29. // The hung device releases the bus a little after the master timeout, i.e.
  30. // while the master's next request is already waiting - this is the "late
  31. // response" that must never be applied to the next channel.
  32. localparam integer STRETCH_CLKS = TIMEOUT_CLKS + 20;
  33. reg clk = 0;
  34. reg rst_n = 0;
  35. always #5 clk = ~clk;
  36. integer failures = 0;
  37. integer i;
  38. reg [7:0] value;
  39. integer rounds = 0;
  40. // ---------------- device model state ----------------
  41. reg [15:0] model_raw [0:26];
  42. reg model_err [0:26];
  43. reg model_stuck [0:26];
  44. integer seen_device [0:26];
  45. integer seen_register [0:26];
  46. integer seen_count = 0;
  47. task model_load_round_one;
  48. begin
  49. for (i = 0; i < 27; i = i + 1) begin
  50. model_err[i] = 0;
  51. model_stuck[i] = 0;
  52. model_raw[i] = 16'h0000;
  53. end
  54. model_raw[0] = 16'h0000; // 0.0000 C -> 0
  55. model_raw[1] = 16'h0cb0; // 25.3750 C -> 25
  56. model_raw[2] = 16'h1400; // 40.0000 C -> 40
  57. model_raw[3] = 16'h2378; // 70.9375 C -> 70
  58. model_raw[4] = 16'h3200; // 100.0000 C -> 100
  59. model_raw[5] = 16'h0007; // 0.0547 C -> 0 (fraction dropped)
  60. model_raw[6] = 16'h0008; // 0.0625 C -> 0
  61. model_raw[7] = 16'h00ff; // 1.9922 C -> 1
  62. model_raw[8] = 16'h7f00; // 254.0000 C -> 254
  63. model_raw[9] = 16'h7f80; // 255.0000 C -> FF (>= 255 C)
  64. model_raw[10] = 16'h7fff; // 255.9922 C -> FF
  65. model_raw[11] = 16'h8000; // -0.0000 C -> FF (negative zero)
  66. model_raw[12] = 16'h8008; // -0.0625 C -> FF
  67. model_raw[13] = 16'hffff; // -0.0625 C -> FF
  68. model_raw[14] = 16'h1234; // 36.4063 C -> 36
  69. model_raw[15] = 16'h0780; // 15 C
  70. model_raw[16] = 16'h0800; // 16 C
  71. model_raw[17] = 16'h0880; // 17 C
  72. model_raw[18] = 16'h0900; // 18 C
  73. model_raw[19] = 16'h0980; // 19 C
  74. model_raw[20] = 16'h1900; // 50 C, but the device NACKs
  75. model_raw[21] = 16'h0a80; // 21 C
  76. model_raw[22] = 16'h0b00; // 22 C, but the device holds SCL low
  77. model_raw[23] = 16'h0b80; // 23 C
  78. model_raw[24] = 16'h0c00; // 24 C
  79. model_raw[25] = 16'h0c80; // 25 C
  80. model_raw[26] = 16'h0d00; // 26 C
  81. model_err[20] = 1; // NACK the read address
  82. model_stuck[22] = 1; // clock-stretch past the master timeout
  83. end
  84. endtask
  85. task model_load_round_two;
  86. begin
  87. for (i = 0; i < 27; i = i + 1) begin
  88. model_raw[i] = (100 + i) * 128; // raw for (100 + i) C
  89. end
  90. model_raw[20] = 16'h1900; // still NACKed -> FF
  91. model_raw[22] = 16'h0b00; // still stuck -> FF
  92. end
  93. endtask
  94. // ---------------- open-drain bus ----------------
  95. reg slave_scl_low = 0;
  96. reg slave_sda_low = 0;
  97. wire scl = (scl_drive_low | slave_scl_low) ? 1'b0 : 1'b1;
  98. wire sda = (sda_drive_low | slave_sda_low) ? 1'b0 : 1'b1;
  99. wire scl_drive_low;
  100. wire sda_drive_low;
  101. wire [5:0] temp_wr_addr;
  102. wire [7:0] temp_wr_data;
  103. wire temp_wr_en;
  104. wire [4:0] scan_channel;
  105. wire scan_round_done;
  106. reg [5:0] temp_rd_addr = 0;
  107. wire [7:0] temp_rd_data;
  108. wire temp_rd_valid;
  109. ct7428_scan #(
  110. .HALF_PERIOD_CLKS(HALF_PERIOD_CLKS),
  111. .TIMEOUT_CLKS(TIMEOUT_CLKS),
  112. .GAP_CLKS(GAP_CLKS),
  113. .TEMP_BASE(32)
  114. ) dut (
  115. .clk(clk), .rst_n(rst_n), .scl_i(scl), .sda_i(sda),
  116. .scl_drive_low(scl_drive_low), .sda_drive_low(sda_drive_low),
  117. .temp_wr_addr(temp_wr_addr), .temp_wr_data(temp_wr_data),
  118. .temp_wr_en(temp_wr_en),
  119. .scan_channel(scan_channel), .scan_round_done(scan_round_done)
  120. );
  121. temp_ram #(.DEPTH(64), .ADDR_WIDTH(6)) u_ram (
  122. .clk(clk), .rst_n(rst_n), .wr_en(temp_wr_en), .wr_addr(temp_wr_addr),
  123. .wr_data(temp_wr_data), .rd_addr(temp_rd_addr),
  124. .rd_data(temp_rd_data), .rd_valid(temp_rd_valid)
  125. );
  126. always @(posedge clk) if (scan_round_done) rounds = rounds + 1;
  127. // ---------------- virtual CT7428 device ----------------
  128. localparam [1:0] S_IDLE = 2'd0;
  129. localparam [1:0] S_RX = 2'd1;
  130. localparam [1:0] S_TX = 2'd2;
  131. reg [1:0] sl_state = S_IDLE;
  132. reg [3:0] sample_count = 0; // data bits sampled in the current byte
  133. reg ack_window = 0; // inside the ACK window (S_RX)
  134. reg [3:0] tx_bit_index = 0; // 7..0 data bits, 8 = ACK window (S_TX)
  135. reg [7:0] shift = 0;
  136. reg [1:0] byte_index = 0; // 0 address, 1 register (write phase)
  137. reg read_phase = 0;
  138. reg dev_match = 0;
  139. reg [6:0] cur_device = 0;
  140. reg [7:0] reg_addr_q = 0;
  141. reg [7:0] tx_byte = 0;
  142. reg tx_low_phase = 0;
  143. reg master_ack = 0;
  144. reg respond_ok = 0;
  145. reg stretch_active = 0;
  146. integer stretch_count = 0;
  147. wire [15:0] cur_raw = model_raw[channel_of(cur_device, reg_addr_q)];
  148. function [4:0] channel_of;
  149. input [6:0] dev;
  150. input [7:0] reg_addr;
  151. begin
  152. if (dev == 7'h49) channel_of = reg_addr[4:0];
  153. else if (dev == 7'h48) channel_of = 5'd9 + reg_addr[4:0];
  154. else channel_of = 5'd18 + reg_addr[4:0];
  155. end
  156. endfunction
  157. function is_known_device;
  158. input [6:0] dev;
  159. begin
  160. is_known_device = (dev == 7'h49) || (dev == 7'h48) || (dev == 7'h4b);
  161. end
  162. endfunction
  163. // START: SDA falls while SCL is high.
  164. always @(negedge sda) begin
  165. if (scl && !stretch_active) begin
  166. sl_state <= S_RX;
  167. sample_count <= 4'd0;
  168. ack_window <= 1'b0;
  169. shift <= 8'd0;
  170. byte_index <= 2'd0;
  171. read_phase <= 1'b0;
  172. dev_match <= 1'b0;
  173. respond_ok <= 1'b0;
  174. end
  175. end
  176. // STOP: SDA rises while SCL is high.
  177. always @(posedge sda) begin
  178. if (scl && !stretch_active) begin
  179. sl_state <= S_IDLE;
  180. sample_count <= 4'd0;
  181. ack_window <= 1'b0;
  182. tx_bit_index <= 4'd0;
  183. slave_sda_low <= 1'b0;
  184. end
  185. end
  186. // Data bits are sampled while SCL is high.
  187. always @(posedge scl) begin
  188. if (!stretch_active) begin
  189. if (sl_state == S_RX && !ack_window && sample_count < 4'd8) begin
  190. shift <= {shift[6:0], sda};
  191. sample_count <= sample_count + 1'b1;
  192. end
  193. if (sl_state == S_TX && tx_bit_index == 4'd8)
  194. master_ack <= ~sda;
  195. end
  196. end
  197. // Every SCL falling edge starts a new bit window.
  198. always @(negedge scl) begin
  199. if (!stretch_active) begin
  200. case (sl_state)
  201. S_RX: begin
  202. if (ack_window) begin
  203. // The ACK window has just ended: start the next byte or the
  204. // read data phase.
  205. ack_window <= 1'b0;
  206. sample_count <= 4'd0;
  207. if (byte_index == 2'd0) begin
  208. if (read_phase) begin
  209. if (respond_ok) begin
  210. // transmit the two data bytes
  211. tx_byte <= cur_raw[15:8];
  212. tx_bit_index <= 4'd7;
  213. tx_low_phase <= 1'b0;
  214. slave_sda_low <= ~cur_raw[15];
  215. sl_state <= S_TX;
  216. seen_device[seen_count] = cur_device;
  217. seen_register[seen_count] = reg_addr_q;
  218. seen_count = seen_count + 1;
  219. end else begin
  220. sl_state <= S_IDLE;
  221. slave_sda_low <= 1'b0;
  222. end
  223. end else begin
  224. byte_index <= 2'd1;
  225. slave_sda_low <= 1'b0;
  226. end
  227. end else begin
  228. sl_state <= S_IDLE; // register byte done; STOP follows
  229. slave_sda_low <= 1'b0;
  230. end
  231. end else if (sample_count == 4'd8) begin
  232. // The eight data bits are complete in `shift`; the ACK window
  233. // starts now (releasing SDA means NACK).
  234. ack_window <= 1'b1;
  235. if (byte_index == 2'd0) begin
  236. read_phase <= shift[0];
  237. dev_match <= is_known_device(shift[7:1]);
  238. cur_device <= shift[7:1];
  239. if (!is_known_device(shift[7:1]))
  240. slave_sda_low <= 1'b0;
  241. else if (!shift[0])
  242. slave_sda_low <= 1'b1;
  243. else if (model_err[channel_of(shift[7:1], reg_addr_q)])
  244. slave_sda_low <= 1'b0;
  245. else if (model_stuck[channel_of(shift[7:1], reg_addr_q)]) begin
  246. slave_sda_low <= 1'b0;
  247. slave_scl_low <= 1'b1;
  248. stretch_active <= 1'b1;
  249. stretch_count <= STRETCH_CLKS;
  250. end else
  251. slave_sda_low <= 1'b1;
  252. respond_ok <= is_known_device(shift[7:1]) &&
  253. !model_err[channel_of(shift[7:1], reg_addr_q)] &&
  254. !model_stuck[channel_of(shift[7:1], reg_addr_q)];
  255. end else begin
  256. reg_addr_q <= shift;
  257. slave_sda_low <= 1'b1;
  258. end
  259. end
  260. end
  261. S_TX: begin
  262. if (tx_bit_index > 4'd0 && tx_bit_index < 4'd8) begin
  263. tx_bit_index <= tx_bit_index - 1'b1;
  264. slave_sda_low <= ~tx_byte[tx_bit_index - 1'b1];
  265. end else if (tx_bit_index == 4'd0) begin
  266. tx_bit_index <= 4'd8; // ACK window begins
  267. slave_sda_low <= 1'b0;
  268. end else begin
  269. tx_bit_index <= 4'd7;
  270. if (master_ack && !tx_low_phase) begin
  271. tx_low_phase <= 1'b1;
  272. tx_byte <= cur_raw[7:0];
  273. slave_sda_low <= ~cur_raw[7];
  274. end else begin
  275. sl_state <= S_IDLE;
  276. slave_sda_low <= 1'b0;
  277. end
  278. end
  279. end
  280. default: ;
  281. endcase
  282. end
  283. end
  284. // Clock stretch / stuck device: hold SCL low, then release and abandon the
  285. // aborted transaction. The master must have timed out before the release.
  286. always @(posedge clk) begin
  287. if (stretch_active) begin
  288. if (stretch_count <= 1) begin
  289. stretch_active <= 1'b0;
  290. slave_scl_low <= 1'b0;
  291. slave_sda_low <= 1'b0;
  292. sl_state <= S_IDLE;
  293. sample_count <= 4'd0;
  294. ack_window <= 1'b0;
  295. tx_bit_index <= 4'd0;
  296. stretch_count <= 0;
  297. end else begin
  298. stretch_count <= stretch_count - 1;
  299. end
  300. end
  301. end
  302. // ---------------- helpers ----------------
  303. task read_ram;
  304. input [5:0] addr;
  305. output [7:0] result;
  306. begin
  307. @(negedge clk);
  308. temp_rd_addr = addr;
  309. @(posedge clk); #1;
  310. result = temp_rd_data;
  311. if (temp_rd_valid !== 1'b1) begin
  312. $display("FAIL ct7428_scan_tb: rd_valid low for channel address %0d", addr);
  313. failures = failures + 1;
  314. end
  315. end
  316. endtask
  317. task check_byte;
  318. input [5:0] addr;
  319. input [7:0] want;
  320. input [8*48-1:0] what;
  321. begin
  322. read_ram(addr, value);
  323. if (value !== want) begin
  324. $display("FAIL ct7428_scan_tb: %0s channel %0d = %02h, expected %02h",
  325. what, addr - 6'd32, value, want);
  326. failures = failures + 1;
  327. end
  328. end
  329. endtask
  330. task check_round_one;
  331. begin
  332. check_byte(6'd32, 8'h00, "round1"); // 0.0000 C
  333. check_byte(6'd33, 8'h19, "round1"); // 25.3750 C
  334. check_byte(6'd34, 8'h28, "round1"); // 40.0000 C
  335. check_byte(6'd35, 8'h46, "round1"); // 70.9375 C
  336. check_byte(6'd36, 8'h64, "round1"); // 100.0000 C
  337. check_byte(6'd37, 8'h00, "round1"); // 0.0547 C fraction dropped
  338. check_byte(6'd38, 8'h00, "round1"); // 0.0625 C fraction dropped
  339. check_byte(6'd39, 8'h01, "round1"); // 1.9922 C fraction dropped
  340. check_byte(6'd40, 8'hfe, "round1"); // 254.0000 C highest valid
  341. check_byte(6'd41, 8'hff, "round1"); // 255.0000 C invalid
  342. check_byte(6'd42, 8'hff, "round1"); // 255.9922 C invalid
  343. check_byte(6'd43, 8'hff, "round1"); // -0.0000 C negative zero
  344. check_byte(6'd44, 8'hff, "round1"); // -0.0625 C
  345. check_byte(6'd45, 8'hff, "round1"); // -0.0625 C (0xffff)
  346. check_byte(6'd46, 8'h24, "round1"); // 36.4063 C
  347. check_byte(6'd47, 8'h0f, "round1");
  348. check_byte(6'd48, 8'h10, "round1");
  349. check_byte(6'd49, 8'h11, "round1");
  350. check_byte(6'd50, 8'h12, "round1");
  351. check_byte(6'd51, 8'h13, "round1");
  352. check_byte(6'd52, 8'hff, "round1"); // 50 C but the device NACKs
  353. check_byte(6'd53, 8'h15, "round1");
  354. check_byte(6'd54, 8'hff, "round1"); // 22 C but the device hangs SCL
  355. check_byte(6'd55, 8'h17, "round1"); // next channel still its own data
  356. check_byte(6'd56, 8'h18, "round1");
  357. check_byte(6'd57, 8'h19, "round1");
  358. check_byte(6'd58, 8'h1a, "round1");
  359. end
  360. endtask
  361. task check_round_three;
  362. begin
  363. for (i = 0; i < 27; i = i + 1) begin
  364. if (i == 20 || i == 22)
  365. check_byte(6'd32 + i[5:0], 8'hff, "round3"); // still failing
  366. else
  367. check_byte(6'd32 + i[5:0], 8'd100 + i[7:0], "round3");
  368. end
  369. end
  370. endtask
  371. task check_address_map;
  372. integer expected_device;
  373. integer expected_register;
  374. integer expected_count;
  375. begin
  376. expected_count = 0;
  377. for (i = 0; i < 27; i = i + 1)
  378. if (i != 20 && i != 22) expected_count = expected_count + 1;
  379. if (seen_count != expected_count) begin
  380. $display("FAIL ct7428_scan_tb: device model saw %0d reads, expected %0d",
  381. seen_count, expected_count);
  382. failures = failures + 1;
  383. end else begin
  384. expected_count = 0;
  385. for (i = 0; i < 27; i = i + 1) begin
  386. if (i != 20 && i != 22) begin // NACKed / hung device
  387. expected_device = (i < 9) ? 7'h49 : ((i < 18) ? 7'h48 : 7'h4b);
  388. expected_register = (i < 9) ? i : ((i < 18) ? (i - 9) : (i - 18));
  389. if (seen_device[expected_count] !== expected_device ||
  390. seen_register[expected_count] !== expected_register) begin
  391. $display("FAIL ct7428_scan_tb: read %0d = device %02h register %0d, expected device %02h register %0d",
  392. expected_count, seen_device[expected_count], seen_register[expected_count],
  393. expected_device, expected_register);
  394. failures = failures + 1;
  395. end
  396. expected_count = expected_count + 1;
  397. end
  398. end
  399. end
  400. end
  401. endtask
  402. // ---------------- main ----------------
  403. reg [1023:0] vector_file;
  404. integer fd;
  405. integer code;
  406. reg [1023:0] line;
  407. integer raw_value;
  408. integer err_value;
  409. initial begin
  410. vector_file = 0;
  411. code = $value$plusargs("VECTOR_FILE=%s", vector_file);
  412. model_load_round_one();
  413. repeat (4) @(negedge clk);
  414. rst_n = 1;
  415. // 1. Nothing has been sampled yet: every CT channel reads FF.
  416. for (i = 0; i < 27; i = i + 1) begin
  417. read_ram(6'd32 + i[5:0], value);
  418. if (value !== 8'hff) begin
  419. $display("FAIL ct7428_scan_tb: never-sampled channel %0d reads %02h", i, value);
  420. failures = failures + 1;
  421. end
  422. end
  423. if (code != 0) begin
  424. fd = $fopen(vector_file, "r");
  425. if (fd == 0)
  426. $fatal(1, "ct7428_scan_tb: cannot open vector file");
  427. for (i = 0; i < 27; i = i + 1) begin
  428. code = $fgets(line, fd);
  429. raw_value = 0;
  430. err_value = 0;
  431. code = $sscanf(line, "%h %h", raw_value, err_value);
  432. model_raw[i] = raw_value[15:0];
  433. model_err[i] = err_value[0];
  434. model_stuck[i] = 1'b0;
  435. end
  436. $fclose(fd);
  437. wait (rounds == 1);
  438. for (i = 0; i < 27; i = i + 1) begin
  439. read_ram(6'd32 + i[5:0], value);
  440. $display("RESULT %0d %02h", i, value);
  441. end
  442. if (failures == 0) $display("ct7428_scan_tb PASS");
  443. else $fatal(1, "ct7428_scan_tb FAIL (%0d checks)", failures);
  444. $finish;
  445. end
  446. wait (rounds == 1);
  447. check_round_one();
  448. check_address_map();
  449. // 2. New device values: every channel must update on a later round.
  450. model_load_round_two();
  451. wait (rounds == 3);
  452. check_round_three();
  453. if (failures == 0) $display("ct7428_scan_tb PASS");
  454. else $fatal(1, "ct7428_scan_tb FAIL (%0d checks)", failures);
  455. $finish;
  456. end
  457. initial begin
  458. #40000000;
  459. $fatal(1, "ct7428_scan_tb FAIL: timeout waiting for the scan");
  460. end
  461. endmodule