Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Clocking Blocks Part 1: Declaring a Clocking Block Inside an Interface

SystemVerilog Interfaces & Modports

Runnable example from the SystemVerilog Clocking Blocks Part 1 tutorial (Declaring a Clocking Block Inside an Interface)

Open in IDE
design.sv
// Design under test
module des (input logic clk, req, output logic gnt);
  always @(posedge clk)
    gnt <= req;
endmodule

// Interface with a clocking block
interface req_gnt_if (input logic clk);
  logic req;
  logic gnt;

  clocking cb @(posedge clk);
    default input #1step output #1ns;
    input  gnt;   // testbench reads: comes from design
    output req;   // testbench drives: goes into design
  endclocking
endinterface
testbench.sv
// Testbench
module tb;
  logic clk;

  // Generate a 10ns clock (posedge at 0, 10, 20 ...)
  initial clk = 0;
  always #5 clk = ~clk;

  // Instantiate the interface and the design
  req_gnt_if vif (.clk(clk));
  des dut (.clk(clk), .req(vif.req), .gnt(vif.gnt));

  initial begin
    // Drive req through the clocking block
    vif.cb.req <= 0;

    @(vif.cb);          // wait for one clock edge
    vif.cb.req <= 1;    // assert request

    @(vif.cb);          // wait one more clock
    $display("gnt = %b", vif.cb.gnt);  // read grant

    @(vif.cb);
    vif.cb.req <= 0;

    #20 $finish;
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.