| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- `timescale 1ns/1ps
- // Testbench for ct7428_scan + temp_ram with a bit-level virtual CT7428 device
- // model. Expected bytes are literal constants computed independently from the
- // converter contract (raw >> 3 counts 0.0625 C, so the integer Celsius byte is
- // raw >> 7; negative / >= 255 C / failed transactions are 0xFF) - the TB never
- // derives an expectation from a DUT output.
- //
- // Scenarios (default mode):
- // 1. every channel is 0xFF before the first sample (never sampled).
- // 2. a full 27-channel round with 0 C, fractional values, the 254/255 C
- // boundary, negative-zero 0x8000 and 0xffff (negative).
- // 3. the I2C address/register order the device model observes is exactly
- // 0x49 regs 0..8, 0x48 regs 0..8, 0x4b regs 0..8.
- // 4. a NACK on one channel writes 0xFF only there and the scan continues.
- // 5. a device that holds SCL low makes the master time out; that channel
- // becomes 0xFF, the device releases the bus while the next request is
- // already pending and the next channel still receives its own data (a
- // late response can never land on the next channel).
- // 6. later rounds with new device values update every byte (continuous
- // scanning), including 0x00 for a real 0 C reading.
- //
- // With +VECTOR_FILE=<path> the TB reads 27 "<raw_hex> <err>" lines, runs one
- // round and prints "RESULT <channel> <byte_hex>" for tests/unit to compare
- // against independently computed expectations.
- module ct7428_scan_tb;
- localparam integer HALF_PERIOD_CLKS = 8;
- localparam integer TIMEOUT_CLKS = 2000;
- localparam integer GAP_CLKS = 8;
- // The hung device releases the bus a little after the master timeout, i.e.
- // while the master's next request is already waiting - this is the "late
- // response" that must never be applied to the next channel.
- localparam integer STRETCH_CLKS = TIMEOUT_CLKS + 20;
- reg clk = 0;
- reg rst_n = 0;
- always #5 clk = ~clk;
- integer failures = 0;
- integer i;
- reg [7:0] value;
- integer rounds = 0;
- // ---------------- device model state ----------------
- reg [15:0] model_raw [0:26];
- reg model_err [0:26];
- reg model_stuck [0:26];
- integer seen_device [0:26];
- integer seen_register [0:26];
- integer seen_count = 0;
- task model_load_round_one;
- begin
- for (i = 0; i < 27; i = i + 1) begin
- model_err[i] = 0;
- model_stuck[i] = 0;
- model_raw[i] = 16'h0000;
- end
- model_raw[0] = 16'h0000; // 0.0000 C -> 0
- model_raw[1] = 16'h0cb0; // 25.3750 C -> 25
- model_raw[2] = 16'h1400; // 40.0000 C -> 40
- model_raw[3] = 16'h2378; // 70.9375 C -> 70
- model_raw[4] = 16'h3200; // 100.0000 C -> 100
- model_raw[5] = 16'h0007; // 0.0547 C -> 0 (fraction dropped)
- model_raw[6] = 16'h0008; // 0.0625 C -> 0
- model_raw[7] = 16'h00ff; // 1.9922 C -> 1
- model_raw[8] = 16'h7f00; // 254.0000 C -> 254
- model_raw[9] = 16'h7f80; // 255.0000 C -> FF (>= 255 C)
- model_raw[10] = 16'h7fff; // 255.9922 C -> FF
- model_raw[11] = 16'h8000; // -0.0000 C -> FF (negative zero)
- model_raw[12] = 16'h8008; // -0.0625 C -> FF
- model_raw[13] = 16'hffff; // -0.0625 C -> FF
- model_raw[14] = 16'h1234; // 36.4063 C -> 36
- model_raw[15] = 16'h0780; // 15 C
- model_raw[16] = 16'h0800; // 16 C
- model_raw[17] = 16'h0880; // 17 C
- model_raw[18] = 16'h0900; // 18 C
- model_raw[19] = 16'h0980; // 19 C
- model_raw[20] = 16'h1900; // 50 C, but the device NACKs
- model_raw[21] = 16'h0a80; // 21 C
- model_raw[22] = 16'h0b00; // 22 C, but the device holds SCL low
- model_raw[23] = 16'h0b80; // 23 C
- model_raw[24] = 16'h0c00; // 24 C
- model_raw[25] = 16'h0c80; // 25 C
- model_raw[26] = 16'h0d00; // 26 C
- model_err[20] = 1; // NACK the read address
- model_stuck[22] = 1; // clock-stretch past the master timeout
- end
- endtask
- task model_load_round_two;
- begin
- for (i = 0; i < 27; i = i + 1) begin
- model_raw[i] = (100 + i) * 128; // raw for (100 + i) C
- end
- model_raw[20] = 16'h1900; // still NACKed -> FF
- model_raw[22] = 16'h0b00; // still stuck -> FF
- end
- endtask
- // ---------------- open-drain bus ----------------
- reg slave_scl_low = 0;
- reg slave_sda_low = 0;
- wire scl = (scl_drive_low | slave_scl_low) ? 1'b0 : 1'b1;
- wire sda = (sda_drive_low | slave_sda_low) ? 1'b0 : 1'b1;
- wire scl_drive_low;
- wire sda_drive_low;
- wire [5:0] temp_wr_addr;
- wire [7:0] temp_wr_data;
- wire temp_wr_en;
- wire [4:0] scan_channel;
- wire scan_round_done;
- reg [5:0] temp_rd_addr = 0;
- wire [7:0] temp_rd_data;
- wire temp_rd_valid;
- ct7428_scan #(
- .HALF_PERIOD_CLKS(HALF_PERIOD_CLKS),
- .TIMEOUT_CLKS(TIMEOUT_CLKS),
- .GAP_CLKS(GAP_CLKS),
- .TEMP_BASE(32)
- ) dut (
- .clk(clk), .rst_n(rst_n), .scl_i(scl), .sda_i(sda),
- .scl_drive_low(scl_drive_low), .sda_drive_low(sda_drive_low),
- .temp_wr_addr(temp_wr_addr), .temp_wr_data(temp_wr_data),
- .temp_wr_en(temp_wr_en),
- .scan_channel(scan_channel), .scan_round_done(scan_round_done)
- );
- temp_ram #(.DEPTH(64), .ADDR_WIDTH(6)) u_ram (
- .clk(clk), .rst_n(rst_n), .wr_en(temp_wr_en), .wr_addr(temp_wr_addr),
- .wr_data(temp_wr_data), .rd_addr(temp_rd_addr),
- .rd_data(temp_rd_data), .rd_valid(temp_rd_valid)
- );
- always @(posedge clk) if (scan_round_done) rounds = rounds + 1;
- // ---------------- virtual CT7428 device ----------------
- localparam [1:0] S_IDLE = 2'd0;
- localparam [1:0] S_RX = 2'd1;
- localparam [1:0] S_TX = 2'd2;
- reg [1:0] sl_state = S_IDLE;
- reg [3:0] sample_count = 0; // data bits sampled in the current byte
- reg ack_window = 0; // inside the ACK window (S_RX)
- reg [3:0] tx_bit_index = 0; // 7..0 data bits, 8 = ACK window (S_TX)
- reg [7:0] shift = 0;
- reg [1:0] byte_index = 0; // 0 address, 1 register (write phase)
- reg read_phase = 0;
- reg dev_match = 0;
- reg [6:0] cur_device = 0;
- reg [7:0] reg_addr_q = 0;
- reg [7:0] tx_byte = 0;
- reg tx_low_phase = 0;
- reg master_ack = 0;
- reg respond_ok = 0;
- reg stretch_active = 0;
- integer stretch_count = 0;
- wire [15:0] cur_raw = model_raw[channel_of(cur_device, reg_addr_q)];
- function [4:0] channel_of;
- input [6:0] dev;
- input [7:0] reg_addr;
- begin
- if (dev == 7'h49) channel_of = reg_addr[4:0];
- else if (dev == 7'h48) channel_of = 5'd9 + reg_addr[4:0];
- else channel_of = 5'd18 + reg_addr[4:0];
- end
- endfunction
- function is_known_device;
- input [6:0] dev;
- begin
- is_known_device = (dev == 7'h49) || (dev == 7'h48) || (dev == 7'h4b);
- end
- endfunction
- // START: SDA falls while SCL is high.
- always @(negedge sda) begin
- if (scl && !stretch_active) begin
- sl_state <= S_RX;
- sample_count <= 4'd0;
- ack_window <= 1'b0;
- shift <= 8'd0;
- byte_index <= 2'd0;
- read_phase <= 1'b0;
- dev_match <= 1'b0;
- respond_ok <= 1'b0;
- end
- end
- // STOP: SDA rises while SCL is high.
- always @(posedge sda) begin
- if (scl && !stretch_active) begin
- sl_state <= S_IDLE;
- sample_count <= 4'd0;
- ack_window <= 1'b0;
- tx_bit_index <= 4'd0;
- slave_sda_low <= 1'b0;
- end
- end
- // Data bits are sampled while SCL is high.
- always @(posedge scl) begin
- if (!stretch_active) begin
- if (sl_state == S_RX && !ack_window && sample_count < 4'd8) begin
- shift <= {shift[6:0], sda};
- sample_count <= sample_count + 1'b1;
- end
- if (sl_state == S_TX && tx_bit_index == 4'd8)
- master_ack <= ~sda;
- end
- end
- // Every SCL falling edge starts a new bit window.
- always @(negedge scl) begin
- if (!stretch_active) begin
- case (sl_state)
- S_RX: begin
- if (ack_window) begin
- // The ACK window has just ended: start the next byte or the
- // read data phase.
- ack_window <= 1'b0;
- sample_count <= 4'd0;
- if (byte_index == 2'd0) begin
- if (read_phase) begin
- if (respond_ok) begin
- // transmit the two data bytes
- tx_byte <= cur_raw[15:8];
- tx_bit_index <= 4'd7;
- tx_low_phase <= 1'b0;
- slave_sda_low <= ~cur_raw[15];
- sl_state <= S_TX;
- seen_device[seen_count] = cur_device;
- seen_register[seen_count] = reg_addr_q;
- seen_count = seen_count + 1;
- end else begin
- sl_state <= S_IDLE;
- slave_sda_low <= 1'b0;
- end
- end else begin
- byte_index <= 2'd1;
- slave_sda_low <= 1'b0;
- end
- end else begin
- sl_state <= S_IDLE; // register byte done; STOP follows
- slave_sda_low <= 1'b0;
- end
- end else if (sample_count == 4'd8) begin
- // The eight data bits are complete in `shift`; the ACK window
- // starts now (releasing SDA means NACK).
- ack_window <= 1'b1;
- if (byte_index == 2'd0) begin
- read_phase <= shift[0];
- dev_match <= is_known_device(shift[7:1]);
- cur_device <= shift[7:1];
- if (!is_known_device(shift[7:1]))
- slave_sda_low <= 1'b0;
- else if (!shift[0])
- slave_sda_low <= 1'b1;
- else if (model_err[channel_of(shift[7:1], reg_addr_q)])
- slave_sda_low <= 1'b0;
- else if (model_stuck[channel_of(shift[7:1], reg_addr_q)]) begin
- slave_sda_low <= 1'b0;
- slave_scl_low <= 1'b1;
- stretch_active <= 1'b1;
- stretch_count <= STRETCH_CLKS;
- end else
- slave_sda_low <= 1'b1;
- respond_ok <= is_known_device(shift[7:1]) &&
- !model_err[channel_of(shift[7:1], reg_addr_q)] &&
- !model_stuck[channel_of(shift[7:1], reg_addr_q)];
- end else begin
- reg_addr_q <= shift;
- slave_sda_low <= 1'b1;
- end
- end
- end
- S_TX: begin
- if (tx_bit_index > 4'd0 && tx_bit_index < 4'd8) begin
- tx_bit_index <= tx_bit_index - 1'b1;
- slave_sda_low <= ~tx_byte[tx_bit_index - 1'b1];
- end else if (tx_bit_index == 4'd0) begin
- tx_bit_index <= 4'd8; // ACK window begins
- slave_sda_low <= 1'b0;
- end else begin
- tx_bit_index <= 4'd7;
- if (master_ack && !tx_low_phase) begin
- tx_low_phase <= 1'b1;
- tx_byte <= cur_raw[7:0];
- slave_sda_low <= ~cur_raw[7];
- end else begin
- sl_state <= S_IDLE;
- slave_sda_low <= 1'b0;
- end
- end
- end
- default: ;
- endcase
- end
- end
- // Clock stretch / stuck device: hold SCL low, then release and abandon the
- // aborted transaction. The master must have timed out before the release.
- always @(posedge clk) begin
- if (stretch_active) begin
- if (stretch_count <= 1) begin
- stretch_active <= 1'b0;
- slave_scl_low <= 1'b0;
- slave_sda_low <= 1'b0;
- sl_state <= S_IDLE;
- sample_count <= 4'd0;
- ack_window <= 1'b0;
- tx_bit_index <= 4'd0;
- stretch_count <= 0;
- end else begin
- stretch_count <= stretch_count - 1;
- end
- end
- end
- // ---------------- helpers ----------------
- task read_ram;
- input [5:0] addr;
- output [7:0] result;
- begin
- @(negedge clk);
- temp_rd_addr = addr;
- @(posedge clk); #1;
- result = temp_rd_data;
- if (temp_rd_valid !== 1'b1) begin
- $display("FAIL ct7428_scan_tb: rd_valid low for channel address %0d", addr);
- failures = failures + 1;
- end
- end
- endtask
- task check_byte;
- input [5:0] addr;
- input [7:0] want;
- input [8*48-1:0] what;
- begin
- read_ram(addr, value);
- if (value !== want) begin
- $display("FAIL ct7428_scan_tb: %0s channel %0d = %02h, expected %02h",
- what, addr - 6'd32, value, want);
- failures = failures + 1;
- end
- end
- endtask
- task check_round_one;
- begin
- check_byte(6'd32, 8'h00, "round1"); // 0.0000 C
- check_byte(6'd33, 8'h19, "round1"); // 25.3750 C
- check_byte(6'd34, 8'h28, "round1"); // 40.0000 C
- check_byte(6'd35, 8'h46, "round1"); // 70.9375 C
- check_byte(6'd36, 8'h64, "round1"); // 100.0000 C
- check_byte(6'd37, 8'h00, "round1"); // 0.0547 C fraction dropped
- check_byte(6'd38, 8'h00, "round1"); // 0.0625 C fraction dropped
- check_byte(6'd39, 8'h01, "round1"); // 1.9922 C fraction dropped
- check_byte(6'd40, 8'hfe, "round1"); // 254.0000 C highest valid
- check_byte(6'd41, 8'hff, "round1"); // 255.0000 C invalid
- check_byte(6'd42, 8'hff, "round1"); // 255.9922 C invalid
- check_byte(6'd43, 8'hff, "round1"); // -0.0000 C negative zero
- check_byte(6'd44, 8'hff, "round1"); // -0.0625 C
- check_byte(6'd45, 8'hff, "round1"); // -0.0625 C (0xffff)
- check_byte(6'd46, 8'h24, "round1"); // 36.4063 C
- check_byte(6'd47, 8'h0f, "round1");
- check_byte(6'd48, 8'h10, "round1");
- check_byte(6'd49, 8'h11, "round1");
- check_byte(6'd50, 8'h12, "round1");
- check_byte(6'd51, 8'h13, "round1");
- check_byte(6'd52, 8'hff, "round1"); // 50 C but the device NACKs
- check_byte(6'd53, 8'h15, "round1");
- check_byte(6'd54, 8'hff, "round1"); // 22 C but the device hangs SCL
- check_byte(6'd55, 8'h17, "round1"); // next channel still its own data
- check_byte(6'd56, 8'h18, "round1");
- check_byte(6'd57, 8'h19, "round1");
- check_byte(6'd58, 8'h1a, "round1");
- end
- endtask
- task check_round_three;
- begin
- for (i = 0; i < 27; i = i + 1) begin
- if (i == 20 || i == 22)
- check_byte(6'd32 + i[5:0], 8'hff, "round3"); // still failing
- else
- check_byte(6'd32 + i[5:0], 8'd100 + i[7:0], "round3");
- end
- end
- endtask
- task check_address_map;
- integer expected_device;
- integer expected_register;
- integer expected_count;
- begin
- expected_count = 0;
- for (i = 0; i < 27; i = i + 1)
- if (i != 20 && i != 22) expected_count = expected_count + 1;
- if (seen_count != expected_count) begin
- $display("FAIL ct7428_scan_tb: device model saw %0d reads, expected %0d",
- seen_count, expected_count);
- failures = failures + 1;
- end else begin
- expected_count = 0;
- for (i = 0; i < 27; i = i + 1) begin
- if (i != 20 && i != 22) begin // NACKed / hung device
- expected_device = (i < 9) ? 7'h49 : ((i < 18) ? 7'h48 : 7'h4b);
- expected_register = (i < 9) ? i : ((i < 18) ? (i - 9) : (i - 18));
- if (seen_device[expected_count] !== expected_device ||
- seen_register[expected_count] !== expected_register) begin
- $display("FAIL ct7428_scan_tb: read %0d = device %02h register %0d, expected device %02h register %0d",
- expected_count, seen_device[expected_count], seen_register[expected_count],
- expected_device, expected_register);
- failures = failures + 1;
- end
- expected_count = expected_count + 1;
- end
- end
- end
- end
- endtask
- // ---------------- main ----------------
- reg [1023:0] vector_file;
- integer fd;
- integer code;
- reg [1023:0] line;
- integer raw_value;
- integer err_value;
- initial begin
- vector_file = 0;
- code = $value$plusargs("VECTOR_FILE=%s", vector_file);
- model_load_round_one();
- repeat (4) @(negedge clk);
- rst_n = 1;
- // 1. Nothing has been sampled yet: every CT channel reads FF.
- for (i = 0; i < 27; i = i + 1) begin
- read_ram(6'd32 + i[5:0], value);
- if (value !== 8'hff) begin
- $display("FAIL ct7428_scan_tb: never-sampled channel %0d reads %02h", i, value);
- failures = failures + 1;
- end
- end
- if (code != 0) begin
- fd = $fopen(vector_file, "r");
- if (fd == 0)
- $fatal(1, "ct7428_scan_tb: cannot open vector file");
- for (i = 0; i < 27; i = i + 1) begin
- code = $fgets(line, fd);
- raw_value = 0;
- err_value = 0;
- code = $sscanf(line, "%h %h", raw_value, err_value);
- model_raw[i] = raw_value[15:0];
- model_err[i] = err_value[0];
- model_stuck[i] = 1'b0;
- end
- $fclose(fd);
- wait (rounds == 1);
- for (i = 0; i < 27; i = i + 1) begin
- read_ram(6'd32 + i[5:0], value);
- $display("RESULT %0d %02h", i, value);
- end
- if (failures == 0) $display("ct7428_scan_tb PASS");
- else $fatal(1, "ct7428_scan_tb FAIL (%0d checks)", failures);
- $finish;
- end
- wait (rounds == 1);
- check_round_one();
- check_address_map();
- // 2. New device values: every channel must update on a later round.
- model_load_round_two();
- wait (rounds == 3);
- check_round_three();
- if (failures == 0) $display("ct7428_scan_tb PASS");
- else $fatal(1, "ct7428_scan_tb FAIL (%0d checks)", failures);
- $finish;
- end
- initial begin
- #40000000;
- $fatal(1, "ct7428_scan_tb FAIL: timeout waiting for the scan");
- end
- endmodule
|