RTL without a testbench is a guess. This lesson gives you a complete self-checking environment for the 11.4 RTL: clock/reset, open-drain bus wiring, a behavioral I2C slave BFM, and PASS/FAIL checks for write-with-ACK, NACK, and abort. Plus a waveform guide so you can debug when checks fail.
Run three directed tests without touching waveforms. If all pass, your byte FSM, bit engine, and CSR handshake are correct. Waveforms are for debug — not your primary pass criteria.
The slave BFM is behavioral (not synthesizable). It watches SCL/SDA and responds on the bus.
| Block | Role | Key signals |
|---|---|---|
| Clock/reset | 100 MHz sys_clk, active-low rst_n | clk, rst_n |
| Bus resolver | Wired-AND open-drain on SDA/SCL | sda, scl wires |
| Reg tasks | Drive CSR like software would | reg_write(addr, data) |
| Slave BFM | Decode addr, ACK/NACK, capture data | slave_mem[0] |
| Checker | Compare slave memory vs expected | errors, tests_run |
Both master and slave drive low or release. The TB models pull-ups and resolves multiple drivers with wired-AND logic.
// Open-drain bus resolution — instantiate in TB
module i2c_bus_wire (
input wire m_sda_out, m_sda_oe,
input wire s_sda_out, s_sda_oe,
output wire sda
);
// Wired-AND: line is low if ANY driver pulls low with oe active
assign sda = (m_sda_oe && !m_sda_out) ? 1'b0 :
(s_sda_oe && !s_sda_out) ? 1'b0 : 1'b1; // pull-up
endmodule
// SCL same pattern (slave typically doesn't drive SCL in this lab)
module i2c_scl_wire (
input wire m_scl_out, m_scl_oe,
output wire scl
);
assign scl = (m_scl_oe && !m_scl_out) ? 1'b0 : 1'b1;
endmodule
If you forget the pull-up model, sda_i floats to X and ACK sampling fails randomly. Always resolve the bus before connecting to sda_i.
A minimal slave: watches for START, shifts in 8 bits, compares address, drives ACK (pull SDA low) or releases for NACK, captures data byte on match.
// i2c_slave_bfm.v — behavioral slave (simulation only)
module i2c_slave_bfm (
input wire scl,
inout wire sda,
input wire [6:0] slave_addr, // 7-bit address
input wire force_nack, // test knob: always NACK
output reg [7:0] captured_data,
output reg got_write
);
reg sda_out, sda_oe;
assign sda = sda_oe ? sda_out : 1'bz;
reg [3:0] bit_idx;
reg [7:0] shift;
reg in_frame;
reg addr_match;
reg is_data_phase;
always @(negedge scl or posedge scl) begin
if (!scl) begin
// Release SDA by default each low phase (except ACK drive)
if (!(bit_idx == 4'd8 && addr_match && !force_nack))
sda_oe <= 1'b0;
end
end
always @(posedge scl) begin
if (sda == 1'b0 && scl == 1'b1) begin // crude START detect
in_frame <= 1'b1;
bit_idx <= 4'd0;
shift <= 8'h00;
is_data_phase <= 1'b0;
addr_match <= 1'b0;
got_write <= 1'b0;
end else if (in_frame) begin
if (bit_idx < 8) begin
shift <= {shift[6:0], sda};
bit_idx <= bit_idx + 1'b1;
end else if (bit_idx == 8) begin
// ACK slot — slave drives
if (!is_data_phase) begin
if ({shift[7:1]} == slave_addr && shift[0] == 1'b0) begin
addr_match <= 1'b1;
if (!force_nack) begin
sda_out <= 1'b0; sda_oe <= 1'b1; // ACK
end
end
is_data_phase <= 1'b1;
end else begin
captured_data <= shift;
got_write <= 1'b1;
if (!force_nack) begin
sda_out <= 1'b0; sda_oe <= 1'b1; // ACK data
end
in_frame <= 1'b0;
end
bit_idx <= 4'd0;
shift <= 8'h00;
end
end
end
endmodule
Note: This BFM is intentionally simple for teaching — it uses posedge SCL sampling, not a production bit-bang model. It is good enough to exercise your master's byte FSM.
// CSR offsets (match Lesson 11.4 / 11.2 map)
localparam OFF_CTRL = 8'h00;
localparam OFF_STAT = 8'h04;
localparam OFF_ADDR = 8'h08;
localparam OFF_TXDATA = 8'h0C;
localparam OFF_CLKDIV = 8'h14;
reg reg_wr, reg_rd;
reg [7:0] reg_addr;
reg [31:0] reg_wdata;
wire [31:0] reg_rdata;
task reg_write(input [7:0] addr; input [31:0] data);
begin
@(posedge clk);
reg_wr = 1; reg_rd = 0;
reg_addr = addr; reg_wdata = data;
@(posedge clk);
reg_wr = 0;
end
endtask
task reg_read(input [7:0] addr; output [31:0] data);
begin
@(posedge clk);
reg_rd = 1; reg_wr = 0;
reg_addr = addr;
@(posedge clk);
data = reg_rdata;
reg_rd = 0;
end
endtask
task poll_done(output bit ok; output bit nack);
reg [31:0] stat;
integer timeout;
begin
timeout = 0; ok = 0; nack = 0;
while (timeout < 50000) begin
reg_read(OFF_STAT, stat);
if (stat[0] == 0 && stat[2] == 1) begin // !BUSY && DONE
ok = 1; nack = stat[1]; return;
end
timeout = timeout + 1;
@(posedge clk);
end
$display("TIMEOUT waiting for DONE");
end
endtask
Three directed tests: successful write, NACK on wrong address, and mid-transaction abort.
module i2c_master_tb;
reg clk, rst_n;
initial clk = 0;
always #5 clk = ~clk; // 100 MHz
// DUT register bus
reg reg_wr, reg_rd;
reg [7:0] reg_addr;
reg [31:0] reg_wdata;
wire [31:0] reg_rdata;
// Pad wires
wire m_scl_out, m_scl_oe, m_sda_out, m_sda_oe;
wire scl, sda;
wire sda_i = sda;
i2c_scl_wire u_scl (.m_scl_out(m_scl_out), .m_scl_oe(m_scl_oe), .scl(scl));
// SDA: master + slave both connect via inout on slave, resolved wire for master
wire s_sda_out, s_sda_oe;
assign sda = (m_sda_oe && !m_sda_out) ? 1'b0 :
(s_sda_oe && !s_sda_out) ? 1'b0 : 1'b1;
i2c_master_top dut (
.clk(clk), .rst_n(rst_n),
.reg_wr(reg_wr), .reg_rd(reg_rd),
.reg_addr(reg_addr), .reg_wdata(reg_wdata), .reg_rdata(reg_rdata),
.scl_out(m_scl_out), .scl_oe(m_scl_oe),
.sda_out(m_sda_out), .sda_oe(m_sda_oe), .sda_i(sda_i),
.irq()
);
reg force_nack;
wire [7:0] captured_data;
wire got_write;
i2c_slave_bfm u_slave (
.scl(scl), .sda(sda),
.slave_addr(7'h50), .force_nack(force_nack),
.captured_data(captured_data), .got_write(got_write)
);
integer errors, tests_run;
// --- CSR tasks (paste from section 4) ---
localparam OFF_CTRL=8'h00, OFF_STAT=8'h04, OFF_ADDR=8'h08;
localparam OFF_TXDATA=8'h0C, OFF_CLKDIV=8'h14;
task reg_write(input [7:0] a; input [31:0] d);
begin @(posedge clk); reg_wr=1; reg_rd=0; reg_addr=a; reg_wdata=d;
@(posedge clk); reg_wr=0; end
endtask
task reg_read(input [7:0] a; output [31:0] d);
begin @(posedge clk); reg_rd=1; reg_wr=0; reg_addr=a;
@(posedge clk); d=reg_rdata; reg_rd=0; end
endtask
task do_write(input [6:0] slave7; input [7:0] data_b;
output bit ok; output bit nack);
reg [31:0] stat; integer t;
begin
reg_write(OFF_CLKDIV, 32'd10); // fast sim
reg_write(OFF_ADDR, {25'b0, slave7}); // 7-bit addr — RTL shifts
reg_write(OFF_TXDATA, {24'b0, data_b});
reg_write(OFF_CTRL, 32'h1); // START
t=0; ok=0; nack=0;
while (t < 100000) begin
reg_read(OFF_STAT, stat);
if (stat[0]==0 && stat[2]==1) begin ok=1; nack=stat[1]; return; end
t=t+1; @(posedge clk);
end
end
endtask
task check_eq(input [7:0] got, exp; input string name);
begin
tests_run = tests_run + 1;
if (got !== exp) begin
$display("FAIL [%s]: got 0x%02h expected 0x%02h", name, got, exp);
errors = errors + 1;
end else
$display("PASS [%s]: 0x%02h", name, got);
end
endtask
initial begin
errors=0; tests_run=0;
rst_n=0; reg_wr=0; reg_rd=0; force_nack=0;
repeat(5) @(posedge clk);
rst_n=1;
@(posedge clk);
// TEST 1: write byte with ACK
$display("--- TEST 1: write with ACK ---");
force_nack = 0;
begin bit ok, nack;
do_write(7'h50, 8'hA5, ok, nack);
if (!ok || nack) begin
$display("FAIL [test1_handshake]: ok=%0d nack=%0d", ok, nack);
errors++;
end else $display("PASS [test1_handshake]");
tests_run++;
check_eq(captured_data, 8'hA5, "test1_slave_data");
end
// TEST 2: NACK — wrong address
$display("--- TEST 2: NACK path ---");
begin bit ok, nack;
do_write(7'h3C, 8'h11, ok, nack);
tests_run++;
if (!ok) begin $display("FAIL [test2_timeout]"); errors++; end
else if (!nack) begin $display("FAIL [test2_expect_nack]"); errors++; end
else $display("PASS [test2_nack_detected]");
end
// TEST 3: ABORT mid-transaction
$display("--- TEST 3: ABORT ---");
begin reg [31:0] stat;
reg_write(OFF_ADDR, {25'b0, 7'h50});
reg_write(OFF_TXDATA, {24'b0, 8'h55});
reg_write(OFF_CTRL, 32'h1);
repeat(20) @(posedge clk);
reg_write(OFF_CTRL, 32'h2); // ABORT
repeat(10) @(posedge clk);
reg_read(OFF_STAT, stat);
tests_run++;
if (stat[0] !== 0) begin
$display("FAIL [test3_abort_busy]"); errors++;
end else $display("PASS [test3_abort_cleared_busy]");
end
$display("===================================");
$display("Tests: %0d Errors: %0d", tests_run, errors);
if (errors==0) $display("=== ALL I2C TESTS PASSED ===");
else $display("=== %0d TEST(S) FAILED ===", errors);
$finish;
end
endmodule
When a test fails, add these signals to your waveform viewer in this order:
| Priority | Signal | What to look for |
|---|---|---|
| 1 | scl, sda | START (SDA fall while SCL high), 8 data bits, ACK slot, STOP |
| 2 | dut.u_byte.state | FSM stuck in TX_BIT? Never reaches STOP? |
| 3 | stat_busy, stat_done | BUSY should clear when DONE sets |
| 4 | ack_sda | Should be 0 when slave ACKs; 1 on NACK test |
| 5 | cmd_valid, bit_done | One cmd_valid pulse per bit; bit_done must return |
bit_done never fires. Check clkdiv != 0 and bit engine state machine returns to IDLE.
Slave not on bus — verify sda pull-up and slave sda_oe during ACK slot. Master must release SDA (sda_oe=0).
ADDR holds 7-bit 0x50; RTL shifts to bus byte 0xA0. Don't write 0xA0 to ADDR — see 11.2 CSR map.
Read the $display log first — it tells you which named test failed. Only open waveforms after you know which handshake broke.
!==; error counter accumulates; final PASS/FAIL printed$display