- Verilog Basics
- Verilog Introduction
- Verilog Install
- Verilog Datatypes
- Verilog Conditions
- Verilog Loops
- Verilog Objects
- Verilog Libs
- Verilog Control Flow
- Verilog Modules
- Verilog Ports
- Verilog Operators
- Verilog Procedural Blocks
- Verilog Wires
- Verilog - Advanced
- Verilog Param-Modules
- Verilog Interfaces
- Verilog Classes
- Verilog Assertions
- Verilog Generating Blocks
- Verilog Clocking Blocks
- Verilog State Machines
- Verilog Coverage
- Verilog Race Conditions
- Verilog Timing
- Verilog Testing
Verilog Clocking Blocks
Clocking Blocks in SystemVerilog are used to simplify the process of handling clocked signals and timing within verification environments. They help manage synchronization between different parts of a testbench and ensure that signals are correctly sampled or driven according to specific timing requirements.
Purpose of Clocking Blocks
- Synchronization: Align signals with specific clock edges to ensure proper timing.
- Timing Control: Define how and when signals should be sampled or driven.
- Simplify Testbenches: Make testbenches more readable and maintainable by grouping timing-related code.
Syntax and Structure
A clocking block is defined with the clocking keyword and specifies a clock and timing behavior. It includes:
- Clocking Event: The clock signal that governs the timing for the block.
- Input and Output Variables: Signals that are either sampled or driven within the block.
- Timing Controls: The timing at which signals are sampled or driven relative to the clock.
Syntax:
verilogclocking clocking_name @(posedge clk); // Input and output variables input signal_name; output signal_name; endclocking
Example: Basic Clocking Block
Here’s a simple example of a clocking block:
verilogmodule test; reg clk; reg rst; reg [7:0] data_in; wire [7:0] data_out; // Clocking block clocking cb @(posedge clk); input rst; input [7:0] data_in; output [7:0] data_out; endclocking initial begin clk = 0; rst = 1; data_in = 8'h00; #10 rst = 0; #20 data_in = 8'hFF; end always #5 clk = ~clk; // Example of using the clocking block always @(posedge clk) begin cb.data_out = cb.data_in; end endmodule
In this example:
- The clocking block
cbis defined to use the positive edge ofclk. - It includes
rst,data_in, anddata_outas signals. cb.data_outis driven with the value ofcb.data_inon each positive clock edge.
Clocking Block Timing Controls
Clocking blocks support several timing controls:
@: Specifies the clock edge when the signals are sampled or driven.inputandoutput: Define how signals are sampled and driven relative to the clock edge.
Example: Timing Control
verilogmodule test; reg clk; reg [7:0] data; reg ready; // Clocking block with timing control clocking cb @(posedge clk); input data; output ready; // Sample `data` on the positive edge of `clk` and drive `ready` on the next positive edge endclocking initial begin clk = 0; data = 8'h00; ready = 0; #10 data = 8'hFF; end always #5 clk = ~clk; // Example usage always @(posedge clk) begin cb.ready = (cb.data == 8'hFF); end endmodule
In this example:
cb.datais sampled on the positive edge ofclk.cb.readyis driven based on the sampledcb.datavalue.
Clocking Blocks with Multiple Clocks
Clocking blocks can be used with multiple clocks to handle more complex timing scenarios.
Example: Multiple Clocks
verilogmodule test; reg clk1, clk2; reg [7:0] data1, data2; wire [7:0] result; // Clocking block for clk1 clocking cb1 @(posedge clk1); input data1; output result; endclocking // Clocking block for clk2 clocking cb2 @(posedge clk2); input data2; endclocking initial begin clk1 = 0; clk2 = 0; data1 = 8'h00; data2 = 8'hFF; #10 data1 = 8'hAA; #20 data2 = 8'hBB; end always #5 clk1 = ~clk1; always #7 clk2 = ~clk2; // Example of using clocking blocks with multiple clocks always @(posedge clk1) begin cb1.result = cb1.data1 + cb2.data2; // Using data from different clock domains end endmodule
In this example:
cb1is synchronized withclk1, whilecb2is synchronized withclk2.- Data from different clock domains is used together in the clocking block.