Thursday, 16 April 2015

Constraint Override in Systemverilog

Same variable is constrained in base as well as child (extended) class but constraint name is same in both base  (constraint_cn) and child (constraint_cn) class.

class base;
  rand int unsigned data;
  constraint constraint_cn
  {
    data inside {[1:100]};
  }
endclass : base

class child extends base;
  constraint constraint_cn
  {
    data inside {[101:200]};
  }
endclass : child

module top();
  child C;
  initial begin
    C = new();
    if (!C.randomize()) begin
      $display("Randomization failed");
    end
    else begin
      $display("data = %0d", C.data);
    end
  end
endmodule : top

Output:
data = 114

Tool will try to solve only child (extended) class's constraint because Child class Override base class's Constraint as both constraint is having same name.

Take another example.


Same variable is constrained in base as well as child (extended) class but constraint name is different in base (constraint_cn) and child (constraint_cn_c) class.

class base;
  rand int unsigned data;
  constraint constraint_cn
  {
    data inside {[1:100]};
  }
endclass : base

class child extends base;
  constraint constraint_cn_c
  {
    data inside {[101:200]};
  }
endclass : child

module top();
  child C;
  initial begin
    C = new();
    if (!C.randomize()) begin
      $display("Randomization failed");
    end
    else begin
      $display("data = %0d", C.data);
    end
  end
endmodule : top

Tool will give compilation error like,
Solver failed when solving following set of constraints
 
rand bit[31:0] data; // rand_mode = ON
constraint constraint_cn    // (from this) (constraint_mode = ON) (top.sv:5)
{
   (data inside {[1:100]});
}
constraint constraint_cn_c    // (from this) (constraint_mode = ON) (top.sv:14)
{
   (data inside {[101:200]});
}

Tool will consider two different constraint so try to resolve both constraints because Same variable is constrained using two different constraint (Names are different so Child class can't override Base class's constraint) has their name is different and both constraints are contradict to each other.

No comments:

Post a Comment