SystemVerilog distribution Constraint

 Weighted Distribution 


With dist operator, some values can be allocated more often to an random variable. A dist operator takes a list of values and weights, sepearated by := or :/ operator.

The values and weights can be constants or variables,
  • value can be single or a range
  • the default weight of an unspecified value is := 1
  • sum of weights need not be an 100

value with the more weight will get allocated more often to an random variable.

The := operator assigns the specified weight to the item, or if the item is a range, specified weight to every value in the range.
    addr dist { 2 := 5, [10:12] := 8 };
    addr = 2 , weight 5
    addr = 10, weight 8
    addr = 11, weight 8
    addr = 12, weight 8

The :/ operator assigns the specified weight to the item, or if the item is a range, specied weight/n to every value in the range. where n is number of values in the range.
    addr dist { 2 :/ 5, [10:12] :/ 8 };
    addr = 2 , weight 5
    addr = 10, weight 8/3
    addr = 11, weight 8/3
    addr = 12, weight 8/3


 Example-1: 


In the example below,
On randomization, the probabality of 'addr' is getting the value of 10 is more than 7 and 2. because of weightage specified to each value in the constraint.

class packet;
  rand bit [3:0] addr;

  constraint addr_range { addr dist { 2 := 5, 7 := 8, 10 := 12 }; }
endclass

module constr_dist;
  initial begin
    packet pkt;
    pkt = new();
    $display("------------------------------------");
    repeat(10) begin
      pkt.randomize();
      $display("\taddr = %0d",pkt.addr);
    end
    $display("------------------------------------");
  end
endmodule

 Simulator Output  
------------------------------------
addr = 10
addr = 7
addr = 10
addr = 10
addr = 10
addr = 7
addr = 7
addr = 2
addr = 10
addr = 10
------------------------------------
Execute the above code on 

 Example-2: 


In the example below,shows diff between := and :/
  addr_1=2 weight=5, and addr_1=10 weight=8, addr_1=11 weight=8, addr_1=12 weight=8
  addr_1=2 weight=5, and addr_1=10 weight=8/3=2.66, addr_1=11 weight=2.66, addr_1=12 weight=2.66

class packet;
  rand bit [3:0] addr_1;
  rand bit [3:0] addr_2;

  constraint addr_1_range {   addr_1 dist { 2 := 5, [10:12] := 8 }; }
  constraint addr_2_range {   addr_2 dist { 2 :/ 5, [10:12] :/ 8 }; }
endclass

module constr_dist;
  initial begin
    packet pkt;
    pkt = new();

    $display("------------------------------------");
    repeat(10) begin
      pkt.randomize();
      $display("\taddr_1 = %0d",pkt.addr_1);
    end
    $display("------------------------------------");
    $display("------------------------------------");
    repeat(10) begin
      pkt.randomize();
      $display("\taddr_2 = %0d",pkt.addr_2);
    end
    $display("------------------------------------");
  end
endmodule

 Simulator Output  
------------------------------------
addr_1 = 2
addr_1 = 12
addr_1 = 12
addr_1 = 12
addr_1 = 11
addr_1 = 10
addr_1 = 11
addr_1 = 2
addr_1 = 11
addr_1 = 12
------------------------------------
------------------------------------
addr_2 = 10
addr_2 = 12
addr_2 = 2
addr_2 = 2
addr_2 = 10
addr_2 = 10
addr_2 = 2
addr_2 = 11
addr_2 = 11
addr_2 = 12
------------------------------------
Execute the above code on