Monday, 30 May 2016

System Verilog : Soft Constraints


 Soft constraints are default constraints which hold true until contradicted by another similar constraint.



Let us understand this concept with the following examples.

1.    We have a packet class with address variable, which is limited in between 10 and 100.

class packet;
 
  rand bit[7:0] address;
 
  constraint address_c {
    address inside {[10:100]};
  }
 
endclass

program test();
 
  packet p;
 
  initial begin
    p=new();
    assert(p.randomize());
    $display("address value is %0d" , p.address);
  end
 
endprogram


Output:  address value is 53


The output is 53 which is in between 10 and 100.

2.   Let us apply a inline constraint stating that address should be equal to 200.


class packet;
 
  rand bit[7:0] address;
 
  constraint address_c {
    address inside {[10:100]};
  }
 
endclass

program test();
 
  packet p;
 
  initial begin
    p=new();
    assert(p.randomize() with { p.address==200;} );
    $display("address value is %0d" , p.address);
  end
 
endprogram

Output: Error

Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 19
  Constraints are inconsistent and cannot be solved.
  Please check the inconsistent constraints being printed above and rewrite
  them.

"testbench.sv", 19: test.unnamed$$_3.unnamed$$_1: started at 0ns failed at 0ns
                Offending 'p.randomize() with {
(p.address == 8'hc8);
}
'
address value is 0

The output is 0, here the randomization failed due to constraint conflict i.e. we have given an inline value for address which is out of the range i.e. in between 10 to 100, in such scenarios we need to switch off the corresponding constraints as shown below.

 class packet;
 
  rand bit[7:0] address;
 
  constraint address_c {
    address inside {[10:100]};
  }
  Which
endclass

program test();
 
  packet p;
 
  initial begin
    p=new();
    p.address_c.constraint_mode(0);
    assert(p.randomize() with { p.address==200;} );
    $display("address value is %0d" , p.address);
  end
 
endprogram


output: address value is 200

3.  Let us change the code slightly and use soft keyword before address variable in constraint block as shown below.

class packet;
 
  rand bit[7:0] address;
 
  constraint address_c {
    soft address inside {[10:100]};
  }
 
endclass

program test();
 
  packet p;
 
  initial begin
    p=new();
    //p.address_c.constraint_mode(0);
    assert(p.randomize() with { p.address==101;} );
    $display("address value is %0d" , p.address);
  end
 
endprogram


output: address value is 101


In the above code there is no need to use additional constructs like constraint_mode(0) , here the inline constraint of {p.address == 101 } will have the priority and the solver solves this instead of above declarative constraint ( constraint address_c { soft address inside {[10:100]};  } )

These soft constraints can be used to check the error/illegal scenarios.
 
 So the output here is 101.


 Happy Learning.

 Please provide your feedback if any.

Thanks,
Vikas Billa


3 comments:

  1. Thanks for article. Many coders including me are not aware of "soft" keyword.

    This can be used for generating error scenarios which are out of scope of protocol.

    ReplyDelete
  2. Nice explanation with examples. TgTha you Vikas.

    ReplyDelete