Line 1... |
Line 1... |
|
/////////////////////////////////////////////////////////////////////
|
|
//// ////
|
|
//// WISHBONE rev.B2 compliant I2C Master controller Testbench ////
|
|
//// ////
|
|
//// ////
|
|
//// Author: Richard Herveille ////
|
|
//// richard@asics.ws ////
|
|
//// www.asics.ws ////
|
|
//// ////
|
|
//// Downloaded from: http://www.opencores.org/projects/i2c/ ////
|
|
//// ////
|
|
/////////////////////////////////////////////////////////////////////
|
|
//// ////
|
|
//// Copyright (C) 2001 Richard Herveille ////
|
|
//// richard@asics.ws ////
|
|
//// ////
|
|
//// This source file may be used and distributed without ////
|
|
//// restriction provided that this copyright statement is not ////
|
|
//// removed from the file and that any derivative work contains ////
|
|
//// the original copyright notice and the associated disclaimer.////
|
|
//// ////
|
|
//// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
|
|
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
|
|
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
|
|
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
|
|
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
|
|
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
|
|
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
|
|
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
|
|
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
|
|
//// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
|
|
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
|
|
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
|
|
//// POSSIBILITY OF SUCH DAMAGE. ////
|
|
//// ////
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// CVS Log
|
|
//
|
|
// $Id: tst_bench_top.v,v 1.2 2002-03-17 10:26:38 rherveille Exp $
|
|
//
|
|
// $Date: 2002-03-17 10:26:38 $
|
|
// $Revision: 1.2 $
|
|
// $Author: rherveille $
|
|
// $Locker: $
|
|
// $State: Exp $
|
//
|
//
|
// Testbench for wishbone i2c master module
|
// Change History:
|
//
|
// $Log: not supported by cvs2svn $
|
|
|
`include "timescale.v"
|
`include "timescale.v"
|
|
|
module tst_bench_top();
|
module tst_bench_top();
|
|
|
Line 18... |
Line 64... |
wire stb;
|
wire stb;
|
wire cyc;
|
wire cyc;
|
wire ack;
|
wire ack;
|
wire inta;
|
wire inta;
|
|
|
reg [7:0] q;
|
reg [7:0] q, qq;
|
|
|
wire scl, scl_o, scl_oen;
|
wire scl, scl_o, scl_oen;
|
wire sda, sda_o, sda_oen;
|
wire sda, sda_o, sda_oen;
|
|
|
parameter PRER_LO = 3'b000;
|
parameter PRER_LO = 3'b000;
|
Line 42... |
Line 88... |
|
|
// generate clock
|
// generate clock
|
always #5 clk = ~clk;
|
always #5 clk = ~clk;
|
|
|
// hookup wishbone master model
|
// hookup wishbone master model
|
wb_master_model u0 (
|
wb_master_model #(8, 32) u0 (
|
.clk(clk),
|
.clk(clk),
|
.rst(rstn),
|
.rst(rstn),
|
.adr(adr),
|
.adr(adr),
|
.din(dat_i),
|
.din(dat_i),
|
.dout(dat_o),
|
.dout(dat_o),
|
.cyc(cyc),
|
.cyc(cyc),
|
.stb(stb),
|
.stb(stb),
|
.we(we),
|
.we(we),
|
|
.sel(),
|
.ack(ack),
|
.ack(ack),
|
.err(1'b0),
|
.err(1'b0),
|
.rty(1'b0)
|
.rty(1'b0)
|
);
|
);
|
|
|
Line 96... |
Line 143... |
pullup p1(scl); // pullup scl line
|
pullup p1(scl); // pullup scl line
|
pullup p2(sda); // pullup sda line
|
pullup p2(sda); // pullup sda line
|
|
|
initial
|
initial
|
begin
|
begin
|
|
// force i2c_slave.debug = 1'b1; // enable i2c_slave debug information
|
|
force i2c_slave.debug = 1'b0; // disable i2c_slave debug information
|
|
|
|
$display("\nstatus: %t Testbench started\n\n", $time);
|
|
|
|
$dumpfile("bench.vcd");
|
|
$dumpvars(1, tst_bench_top);
|
|
$dumpvars(1, tst_bench_top.i2c_slave);
|
|
|
// initially values
|
// initially values
|
clk = 0;
|
clk = 0;
|
|
|
// reset system
|
// reset system
|
rstn = 1'b1; // negate reset
|
rstn = 1'b1; // negate reset
|
#2;
|
#2;
|
rstn = 1'b0; // assert reset
|
rstn = 1'b0; // assert reset
|
repeat(20) @(posedge clk);
|
repeat(20) @(posedge clk);
|
rstn = 1'b1; // negate reset
|
rstn = 1'b1; // negate reset
|
|
|
|
$display("status: %t done reset", $time);
|
|
|
@(posedge clk);
|
@(posedge clk);
|
|
|
//
|
//
|
// program core
|
// program core
|
//
|
//
|
|
|
// program internal registers
|
// program internal registers
|
u0.wb_write(1, PRER_LO, 8'hfa); // load prescaler lo-byte
|
// u0.wb_write(1, PRER_LO, 8'hfa); // load prescaler lo-byte
|
|
u0.wb_write(1, PRER_LO, 8'h3e); // load prescaler lo-byte
|
u0.wb_write(1, PRER_HI, 8'h00); // load prescaler hi-byte
|
u0.wb_write(1, PRER_HI, 8'h00); // load prescaler hi-byte
|
|
|
|
$display("status: %t programmed registers", $time);
|
|
|
|
u0.wb_cmp(0, PRER_LO, 8'h3e); // verify prescaler lo-byte
|
|
u0.wb_cmp(0, PRER_HI, 8'h00); // verify prescaler hi-byte
|
|
|
|
$display("status: %t verified registers", $time);
|
|
|
u0.wb_write(1, CTR, 8'h80); // enable core
|
u0.wb_write(1, CTR, 8'h80); // enable core
|
|
|
|
$display("status: %t enabled core", $time);
|
|
|
//
|
//
|
// access slave (write)
|
// access slave (write)
|
//
|
//
|
|
|
// drive slave address
|
// drive slave address
|
u0.wb_write(1, TXR, 8'ha0); // present slave address, set write-bit (== !read)
|
u0.wb_write(1, TXR, 8'ha0); // present slave address, set write-bit (== !read)
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
|
|
|
$display("status: %t generate 'start', write cmd a0 (slave address+write)", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(0, SR, q); // poll it until it is zero
|
u0.wb_read(0, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// send memory address
|
// send memory address
|
u0.wb_write(1, TXR, 8'h01); // present slave's memory address
|
u0.wb_write(1, TXR, 8'h01); // present slave's memory address
|
u0.wb_write(0, CR, 8'h10); // set command (write)
|
u0.wb_write(0, CR, 8'h10); // set command (write)
|
|
|
|
$display("status: %t write slave memory address 01", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(0, SR, q); // poll it until it is zero
|
u0.wb_read(0, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// send memory contents
|
// send memory contents
|
u0.wb_write(1, TXR, 8'ha5); // present data
|
u0.wb_write(1, TXR, 8'ha5); // present data
|
u0.wb_write(0, CR, 8'h10); // set command (stop, write)
|
u0.wb_write(0, CR, 8'h10); // set command (write)
|
|
|
|
$display("status: %t write data a5", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// send memory contents for next memory address (auto_inc)
|
// send memory contents for next memory address (auto_inc)
|
u0.wb_write(1, TXR, 8'h5a); // present data
|
u0.wb_write(1, TXR, 8'h5a); // present data
|
u0.wb_write(0, CR, 8'h50); // set command (stop, write)
|
u0.wb_write(0, CR, 8'h50); // set command (stop, write)
|
|
|
|
$display("status: %t write next data 5a, generate 'stop'", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
|
|
//
|
//
|
// delay
|
// delay
|
//
|
//
|
#100000; // wait for 10us.
|
#100000; // wait for 100us.
|
|
|
|
$display("status: %t wait 100us", $time);
|
|
|
//
|
//
|
// access slave (read)
|
// access slave (read)
|
//
|
//
|
|
|
// drive slave address
|
// drive slave address
|
u0.wb_write(1, TXR, 8'ha0); // present slave address, set write-bit (== !read)
|
u0.wb_write(1, TXR, 8'ha0); // present slave address, set write-bit (== !read)
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
|
|
|
$display("status: %t generate 'start', write cmd a0 (slave address+write)", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// send memory address
|
// send memory address
|
u0.wb_write(1, TXR, 8'h00); // present slave's memory address
|
u0.wb_write(1, TXR, 8'h01); // present slave's memory address
|
u0.wb_write(0, CR, 8'h10); // set command (write)
|
u0.wb_write(0, CR, 8'h10); // set command (write)
|
|
|
|
$display("status: %t write slave address 01", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// drive slave address
|
// drive slave address
|
u0.wb_write(1, TXR, 8'ha1); // present slave's address, set read-bit
|
u0.wb_write(1, TXR, 8'ha1); // present slave's address, set read-bit
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
|
|
|
$display("status: %t generate 'repeated start', write cmd a1 (slave address+read)", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// read data from slave
|
// read data from slave
|
u0.wb_write(1, CR, 8'h20); // set command (read, ack_read)
|
u0.wb_write(1, CR, 8'h20); // set command (read, ack_read)
|
|
|
|
$display("status: %t read + ack", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
|
// check data just received
|
|
u0.wb_read(1, RXR, qq);
|
|
if (qq !== 8'ha5)
|
|
$display("\nERROR: Expected a5, received %x at time %t", qq, $time);
|
|
|
// read data from slave
|
// read data from slave
|
u0.wb_write(1, CR, 8'h20); // set command (read, ack_read)
|
u0.wb_write(1, CR, 8'h20); // set command (read, ack_read)
|
|
|
|
$display("status: %t read + ack", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
|
// check data just received
|
|
u0.wb_read(1, RXR, qq);
|
|
if (qq !== 8'h5a)
|
|
$display("\nERROR: Expected 5a, received %x at time %t", qq, $time);
|
|
|
// read data from slave
|
// read data from slave
|
u0.wb_write(1, CR, 8'h20); // set command (read, ack_read)
|
u0.wb_write(1, CR, 8'h20); // set command (read, ack_read)
|
|
|
|
$display("status: %t read + ack", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
|
// check data just received
|
|
u0.wb_read(1, RXR, qq);
|
|
$display("status: %t received %x from 3rd read address", $time, qq);
|
|
|
// read data from slave
|
// read data from slave
|
u0.wb_write(1, CR, 8'h28); // set command (read, nack_read)
|
u0.wb_write(1, CR, 8'h28); // set command (read, nack_read)
|
|
$display("status: %t read + nack", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
|
// check data just received
|
|
u0.wb_read(1, RXR, qq);
|
|
$display("status: %t received %x from 4th read address", $time, qq);
|
|
|
//
|
//
|
// check invalid slave memory address
|
// check invalid slave memory address
|
//
|
//
|
|
|
// drive slave address
|
// drive slave address
|
u0.wb_write(1, TXR, 8'ha0); // present slave address, set write-bit (== !read)
|
u0.wb_write(1, TXR, 8'ha0); // present slave address, set write-bit (== !read)
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
u0.wb_write(0, CR, 8'h90); // set command (start, write)
|
|
|
|
$display("status: %t generate 'start', write cmd a0 (slave address+write). Check invalid address", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// send memory address
|
// send memory address
|
u0.wb_write(1, TXR, 8'h10); // present slave's memory address
|
u0.wb_write(1, TXR, 8'h10); // present slave's memory address
|
u0.wb_write(0, CR, 8'h10); // set command (write)
|
u0.wb_write(0, CR, 8'h10); // set command (write)
|
|
|
|
$display("status: %t write slave memory address 10", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
// slave should have send NACK
|
// slave should have send NACK
|
|
$display("status: %t Check for nack", $time);
|
if (!q[7])
|
if (!q[7])
|
$display("\nERROR: Expected NACK, received ACK\n");
|
$display("\nERROR: Expected NACK, received ACK\n");
|
|
|
// read data from slave
|
// read data from slave
|
u0.wb_write(1, CR, 8'h40); // set command (stop)
|
u0.wb_write(1, CR, 8'h40); // set command (stop)
|
|
|
|
$display("status: %t generate 'stop'", $time);
|
|
|
// check tip bit
|
// check tip bit
|
u0.wb_read(1, SR, q);
|
u0.wb_read(1, SR, q);
|
while (q[1])
|
while (q[1])
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
u0.wb_read(1, SR, q); // poll it until it is zero
|
|
|
|
$display("status: %t tip==0", $time);
|
|
|
|
|
|
#25000; // wait 25us
|
|
|
|
$display("\n\nstatus: %t Testbench done", $time);
|
|
|
|
$stop;
|
end
|
end
|
|
|
endmodule
|
endmodule
|
|
|
|
|