`timescale 1ns/1ps module host_i2c_slave_contract_tb; reg clk=0;always #20 clk=~clk; reg rst_n=0,scl=1,master_low=0,blocked=0; tri1 sda; assign sda=master_low ? 1'b0 : 1'bz; wire we,cmd,low,busy; wire [15:0] wa,ra; wire [7:0] wd,cd; wire [7:0] rd=ra[7:0]^ra[15:8]; host_i2c_register_slave dut(.clk(clk),.rst_n(rst_n),.scl_i(scl),.sda_io(sda), .write_strobe(we),.write_address(wa),.write_data(wd),.command_strobe(cmd),.command_data(cd), .command_write_blocked(blocked),.read_address(ra),.read_data(rd),.sda_drive_low(low),.busy(busy)); integer half_ns=1250,writes=0,commands=0; always @(posedge clk) begin if(we) writes=writes+1;if(cmd) commands=commands+1;end `include "tests/sim/i2c_master_tasks.vh" reg [7:0] value; integer speed,write_before; initial begin repeat(4) @(negedge clk);rst_n=1; for(speed=0;speed<2;speed=speed+1) begin half_ns=speed==0 ? 1250 : 5000; pointer(16'h0100);start_bus();send(8'h55,1);receive(value,0);stop_bus(); if(value!==8'h01) $fatal(1,"repeated-start pointer read: %02x",value); // A high pointer byte without the low byte must not mutate it. start_bus();send(8'h54,1);send(8'h03,1);stop_bus(); start_bus();send(8'h55,1);receive(value,0);stop_bus(); if(value!==8'h01) $fatal(1,"partial pointer changed address: %02x",value); pointer(16'h03FF);start_bus();send(8'h55,1); receive(value,1);if(value!==8'hFC) $fatal(1,"03FF read"); receive(value,0);if(value!==8'h04) $fatal(1,"0400 sequential boundary");stop_bus(); if(low || sda!==1'b1) $fatal(1,"SDA not released after master NACK/STOP"); write_before=writes;pointer(16'h0001);send(8'h22,0);stop_bus(); if(writes!=write_before) $fatal(1,"read-only write side effect"); blocked=1;pointer(16'h0010);send(8'h01,0);stop_bus();blocked=0; if(commands!=speed) $fatal(1,"busy COMMAND side effect"); pointer(16'h0010);send(8'h05,1);stop_bus(); if(commands!=speed+1) $fatal(1,"COMMAND strobe count"); end $display("host_i2c_slave_contract_tb PASS");$finish; end initial begin #10000000;$fatal(1,"I2C test bounded deadline");end endmodule