|
|
@@ -0,0 +1,70 @@
|
|
|
+`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),
|
|
|
+ .response_valid(1'b0),.response_status(8'd0),.response_command(8'd0),.response_detail(16'd0),
|
|
|
+ .response_length(16'd0),.snapshot_length(16'd0),.snapshot_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
|
|
|
+ task start_bus;
|
|
|
+ begin scl=0;master_low=0;#(half_ns);scl=1;#(half_ns);master_low=1;#(half_ns);scl=0;end
|
|
|
+ endtask
|
|
|
+ task stop_bus;
|
|
|
+ begin scl=0;master_low=1;#(half_ns);scl=1;#(half_ns);master_low=0;#(half_ns);end
|
|
|
+ endtask
|
|
|
+ task send(input [7:0] byte_value,input expected_ack);
|
|
|
+ integer b;reg observed_ack;
|
|
|
+ begin
|
|
|
+ for(b=7;b>=0;b=b-1) begin master_low=!byte_value[b];#(half_ns);scl=1;#(half_ns);scl=0;end
|
|
|
+ master_low=0;#(half_ns);scl=1;#(half_ns);observed_ack=!sda;scl=0;
|
|
|
+ if(observed_ack!==expected_ack) $fatal(1,"I2C ACK byte %02x got %b expected %b",byte_value,observed_ack,expected_ack);
|
|
|
+ #(half_ns);
|
|
|
+ end
|
|
|
+ endtask
|
|
|
+ task receive(output [7:0] byte_value,input ack_more);
|
|
|
+ integer b;
|
|
|
+ begin
|
|
|
+ master_low=0;
|
|
|
+ for(b=7;b>=0;b=b-1) begin #(half_ns);scl=1;#(half_ns);byte_value[b]=sda;scl=0;end
|
|
|
+ master_low=ack_more;#(half_ns);scl=1;#(half_ns);scl=0;master_low=0;#(half_ns);
|
|
|
+ end
|
|
|
+ endtask
|
|
|
+ task pointer(input [15:0] p);
|
|
|
+ begin start_bus();send(8'h54,1);send(p[15:8],1);send(p[7:0],1);end
|
|
|
+ endtask
|
|
|
+ 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!=0) $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
|