Project 4, Part 3: A Testbench That Enforces the Timing
Testing memory controllers against ideal memory models creates verification vulnerabilities.
An idealized behavioral memory returns valid data immediately, masking controller timing violations.
This article constructs a timing-accurate SRAM simulation model that enforces access constraints and outputs unknown (x) states when timing rules fail.
Timing-Accurate Behavioral SRAM Model
The verification model tracks active cycles and enforces access delays:
module timing_accurate_sram #(
parameter DATA_WIDTH = 8,
parameter DEPTH = 256,
parameter T_AA_CYCLES = 2 // Matches READ_WAIT_CYCLES specification
) (
input logic clk,
input logic ce_n, oe_n, we_n,
input logic [$clog2(DEPTH)-1:0] addr,
input logic [DATA_WIDTH-1:0] data_in,
output logic [DATA_WIDTH-1:0] data_out
);
logic [DATA_WIDTH-1:0] mem [0:DEPTH-1];
logic [$clog2(DEPTH)-1:0] addr_latched;
logic access_active = 0;
integer cycles_since_access = 0;
always_ff @(posedge clk) begin
if (!ce_n && !oe_n) begin
if (!access_active) begin
access_active <= 1'b1;
cycles_since_access <= 1;
addr_latched <= addr;
end else begin
cycles_since_access <= cycles_since_access + 1;
end
end else begin
access_active <= 1'b0;
cycles_since_access <= 0;
end
if (!ce_n && !we_n) begin
mem[addr] <= data_in; // Synchronous write operation
end
end
always_comb begin
if (access_active && cycles_since_access >= T_AA_CYCLES)
data_out = mem[addr_latched]; // Timing satisfied: output valid data
else
data_out = {DATA_WIDTH{1'bx}}; // Timing violated: drive unknown X state
end
endmodule
If the controller deasserts strobes or reads data prematurely, the model returns x states.
This forces testbenches to detect timing defects during simulation.
Integrated Self-Checking Testbench
The testbench issues write transactions followed by read transactions:
module tb_sram_controller;
logic clk = 0, rst_n;
logic read_req, write_req, ready;
logic [7:0] addr, write_data, read_data;
sram_controller dut (
.clk(clk), .rst_n(rst_n), .addr(addr),
.read_req(read_req), .write_req(write_req),
.write_data(write_data), .read_data(read_data), .ready(ready)
);
timing_accurate_sram #(.T_AA_CYCLES(2)) mem (
.clk(clk), .ce_n(dut.ce_n), .oe_n(dut.oe_n), .we_n(dut.we_n),
.addr(addr), .data_in(write_data), .data_out(read_data)
);
always #5 clk = ~clk;
task automatic write_and_verify(bit [7:0] a, bit [7:0] d);
addr = a;
write_data = d;
write_req = 1'b1;
@(posedge clk);
write_req = 1'b0;
wait (ready);
@(posedge clk);
addr = a;
read_req = 1'b1;
@(posedge clk);
read_req = 1'b0;
wait (ready);
// Case equality (===) detects unknown X states explicitly
if (read_data === d) begin
$display("PASS: Addr=%0d wrote %h, read %h", a, d, read_data);
end else begin
$display("FAIL: Addr=%0d wrote %h, read %h (X indicates timing violation)",
a, d, read_data);
end
@(posedge clk);
endtask
initial begin
rst_n = 0;
read_req = 0;
write_req = 0;
repeat (3) @(posedge clk);
rst_n = 1;
write_and_verify(8'd10, 8'hA5);
write_and_verify(8'd20, 8'h3C);
write_and_verify(8'd0, 8'hFF);
$finish;
end
endmodule
The testbench uses the 4-state case equality operator (===).
Standard equality (==) evaluates to unknown when comparing x values, masking the failure.
The === operator validates that every returned bit matches the expected logic state.
Bug Injection Methodology
To prove that the testbench detects timing defects, execute a negative test using bug injection:
Instantiate a modified controller that configures READ_WAIT_CYCLES = 1 instead of 2.
When simulated against the timing_accurate_sram model, the testbench flags an immediate failure with read_data = 8'hxx.
Confirming that a testbench fails on invalid designs validates test suite effectiveness.
Practice Exercise
Explain why using the case equality operator === is necessary when evaluating outputs from a timing-accurate simulation model.
Solution
Standard logical equality == evaluates to 1'bx when any compared bit is x.
In conditional statements like if (read_data == d), an unknown condition evaluates as false.
However, in complex testbench logic, unhandled x comparisons can cause silent misevaluations.
The 4-state case equality operator === compares values strictly across 0, 1, x, and z.
If read_data contains x due to an access timing violation, read_data === d evaluates unambiguously to false, triggering the testbench failure report.
Timing-accurate memory models prevent false positives by outputting x states when access times are violated.
Using case equality comparisons ensures immediate detection of uninitialized or premature read cycles.
This completes Project 4.
Project 5 implements a standardized AXI4-Lite slave interface with internal register maps.