Hello !
- I have the following below example of constraint code and trying to create some constraints with inter-dependency b/w constraints of two different classes.
`include "uvm_macros.svh"
import uvm_pkg::*;
module nested_constraints;
typedef struct {
rand bit [31:0] blka_addr;
} blka_var_s;
typedef struct {
rand bit [31:0] top_addr;
rand blka_var_s blka_var;
} top_var_s;
class policy_base#(type ITEM=uvm_object);
ITEM item;
virtual function void set_item(ITEM item);
this.item = item;
endfunction
function pre_randomize();
get_plusargs();
endfunction: pre_randomize
virtual function get_plusargs();
endfunction: get_plusargs
endclass: policy_base
class policy_list#(type ITEM=uvm_object) extends policy_base #(ITEM);
rand policy_base#(ITEM) policy[$];
function void add(policy_base#(ITEM) pcy);
policy.push_back(pcy);
endfunction
function void set_item(ITEM item);
foreach(policy[i]) begin
policy[i].set_item(item);
end
endfunction
endclass: policy_list
// Sub Block class with struct variable as random
class blka_gen_base;
rand policy_base #(blka_gen_base) blka_policy[$];
rand blka_var_s blka_var;
function pre_randomize();
foreach (blka_policy[i]) begin
blka_policy[i].set_item(this);
end
endfunction: pre_randomize
endclass: blka_gen_base
// Adding a constraint for this block level address to be between 5 and 100
class blka_addr_cstr extends policy_base #(blka_gen_base);
constraint blka_addr_c {
soft {item.blka_var.blka_addr > 5 && item.blka_var.blka_addr < 100};
}
endclass: blka_addr_cstr
// A class where adding the above constraint to the policy
class blka_gen extends blka_gen_base;
policy_list#(blka_gen_base) blka_pcy;
function new();
blka_addr_cstr baac;
blka_pcy = new;
baac = new();
blka_pcy.add(baac);
blka_policy.push_back(blka_pcy);
endfunction: new
endclass: blka_gen
// Top Level Class with top level struct variable, and also instanced the block blka class as rand to be randomized
class top_gen_base;
rand policy_base #(top_gen_base) top_policy[$];
rand blka_gen blka_g;
rand top_var_s top_var;
function new();
blka_g = new();
endfunction: new
function pre_randomize();
foreach (top_policy[i]) begin
top_policy[i].set_item(this);
end
endfunction: pre_randomize
function post_randomize();
$display("top_var.blka_var = %0p, blka_g.blka_var = %0p\n", top_var.blka_var, blka_g.blka_var);
top_var.blka_var = blka_g.blka_var;
endfunction: post_randomize
endclass: top_gen_base
// Adding a constraint for top level class variable where the top addr to be always 5
class top_addr_cstr extends policy_base #(top_gen_base);
constraint top_addr_c {
// soft {item.top_var.top_addr > 5 && item.top_var.top_addr < 100};
{item.top_var.top_addr == 5};
}
endclass: top_addr_cstr
// Adding a constraint where if the top level addr is 5, wanted the block level address to be > 100 < 150.
class top_mix_addr_cstr extends policy_base #(top_gen_base);
constraint addr_mix_c {
(item.top_var.top_addr == 5) -> (item.blka_g.blka_var.blka_addr > 100 && item.blka_g.blka_var.blka_addr < 150);
// if (item.top_var.top_addr == 5) (item.blka_g.blka_var.blka_addr > 100 && item.blka_g.blka_var.blka_addr < 150);
}
endclass: top_mix_addr_cstr
class top_gen extends top_gen_base;
policy_list#(top_gen_base) top_pcy;
function new();
top_addr_cstr tac;
top_mix_addr_cstr tmac;
top_pcy = new;
tac = new();
tmac = new();
top_pcy.add(tac);
top_pcy.add(tmac);
top_policy.push_back(top_pcy);
endfunction: new
endclass: top_gen
initial begin
top_gen tg = new();
assert(tg.randomize());
$display("tg = %0p\n", tg);
end
endmodule: nested_constraints
- But when the above code is executed I get the following results
top_var.blka_var = '{blka_addr:'h35520161}, blka_g.blka_var = '{blka_addr:'h73}
tg = '{super:'{top_policy:'{nested_constraints.policy_base#(top_gen_base)@1807_1}, blka_g:nested_constraints.blka_gen@1802_1, top_var:'{top_addr:'h5, blka_var:'{blka_addr:'h73}}}, top_pcy:nested_constraints.policy_list#(top_gen_base)@1807_1}
Note:
- Main intent is lets say at the block level have created bunch of constraints using the policy.
- Wanted to re-use all the block level constraints at the top level and then use the values accordingly.
Any specific reason why the inter-depedency b/w the constraints i.e. top constraint vs the constraint inside the sub block class isn’t getting equated ? Please clarify.