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

Constraint Blocks: Simple Constraint Block

SystemVerilog Constraints

Runnable example from the SystemVerilog Constraint Blocks tutorial (Simple Constraint Block)

Open in IDE
design.sv
// Let's declare a new class called "Packet" with a single variable that
// can be randomized. We want this variable to have values between 2 and 6
// when randomized. This is achieved by a constraint called "c_mode" (you
// can name it anything else).
class Packet;
    rand bit [3:0] mode;

    constraint c_mode {
        mode > 2;
        mode <= 6;
    }
endclass
testbench.sv
module tb;
    Packet pkt = new();

    initial begin
        for (int i = 0; i < 5; i++) begin
            assert(pkt.randomize()) else $fatal(1, "Randomization failed");
            $display("mode = %0d", pkt.mode);
        end
    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.