目录
3)soft 约束有冲突的话,不报错,加soft的约束优先级变低,以另一个为准
5)rand_mode(0),关闭某个变量的随机,会保持初始值0.
1)关键字:extends
1、子类中对同一个变量的约束要和父类有交集,如果没有交集,会报error
class father;
rand bit[10:0] data;
constraint c_data{
data >=10;
data <=30;
}
endclass
class son extends father;
constraint c_son_data{
data >=40;
data <=100;
}
endclass
module test;
initial begin
father fa;
son so;
fa = new;
so = new;
fa.randomize();
so.randomize();
$display("canli test");
end
endmodule
运行结果:
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 25
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
注意哦,只要有交集就好咯,子类对某个变量的约束不需要是子集哦。
如下所示,父类data范围10-30,子类data范围时29-250,有交集29-30,所以正常随机,随机出来是30.
class father;
rand bit[10:0] data;
constraint c_data{
data >=10;
data <=30;
}
endclass
class son extends father;
constraint c_son_data{
data >=29;
data <=250;
}
endclass
module test;
initial begin
father fa;
son so;
fa = new;
so = new;
fa.randomize();
so.randomize();
$display("father's data is %d",fa.data);
$display("son's data is %d",so.data);
end
endmodule
运行结果
father's data is 14
son's data is 30
2)constraint_mode()
1、constraint_mode(0),关闭约束,也就是说,本来约束是10-30之间,关闭之后,就是0到2的10次方-1.
2、如果在子类中把父类的c_data约束关闭(constraint_mode(0)),在子类和父类对同一个变量有冲突约束的情况下,会有什么效果呢?
class father;
rand bit[10:0] data;
constraint c_data{
data >=10;
data <=30;
}
endclass
class son extends father;
constraint c_son_data{
data >=60;
data <=250;
}
endclass
module test;
initial begin
father fa;
son so;
fa = new;
so = new;
fa.c_data.constraint_mode(0);
fa.randomize();
so.randomize();
$display("father's data is %d",fa.data);
$display("son's data is %d",so.data);
end
endmodule
运行结果:父类随意随机,子类还是会和父类的约束冲突
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 28
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
father's data is 689
son's data is 0
父类和子类都加上constraint_mode(0),此时就不会报错啦,father和son都各自随意随机
fa.c_data.constraint_mode(0);
so.c_son_data.constraint_mode(0);
运行结果
father's data is 689
son's data is 30
3)soft 约束有冲突的话,不报错,加soft的约束优先级变低,以另一个为准
1、如二所说,如果子类非要和父类的范围冲突要怎么办呢?
答案是使用soft关键字哦。如果父类中的约束中有soft,则子类是可以和父类中的约束有冲突的,此时会以子类的约束范围为准。
class father;
rand bit[10:0] data;
constraint c_data{
soft data >=10;
soft data <=30;
}
endclass
class son extends father;
constraint c_son_data{
data >=60;
data <=250;
}
endclass
module test;
initial begin
father fa;
son so;
fa = new;
so = new;
fa.randomize();
so.randomize();
$display("father's data is %d",fa.data);
$display("son's data is %d",so.data);
end
endmodule
运行结果
father's data is 19
son's data is 247
4)对pre_randomize() 方法的集成
在子类的pre_randomize方法中,若是没有调用super.pre_randomize(),则子类不会集成父类的pre_randomize方法。
class father;
function void pre_randomize();
$display("father's pre_randomize");
endfunction
endclass
class son extends father;
function void pre_randomize();
$display("son's pre_randomize");
endfunction
endclass
module test;
initial begin
father fa;
son so;
fa = new;
so = new;
fa.randomize();
so.randomize();
end
endmodule
运行结果
father's pre_randomize
son's pre_randomize
5)调用了super.pre_randomize():
class father;
function void pre_randomize();
$display("father's pre_randomize");
endfunction
endclass
class son extends father;
function void pre_randomize();
super.pre_randomize();
$display("son's pre_randomize");
endfunction
endclass
module test;
initial begin
father fa;
son so;
fa = new;
so = new;
fa.randomize();
so.randomize();
end
endmodule
运行结果
father's pre_randomize
father's pre_randomize
son's pre_randomize
5)rand_mode(0),关闭某个变量的随机,会保持初始值0.
class father;
function void pre_randomize();
$display("father's pre_randomize");
endfunction
rand bit[10:0] data;
constraint c_data{
data inside {1,100};
}
endclass
module test;
initial begin
father fa;
fa = new;a
fa.data.rand_mode(0);
fa.randomize();
$display("fa.data is %0d",fa.data);
end
endmodule
运行结果
fa.data is 0
6)inside关键字
data inside {1,100}; // 在1或者100中随机,只会随机出来两个值,1或者100
data inside {[1:100]}; //在1到100之间随机
7) constraint -> ,相当于if前置条件
(mode==0) -> range inside {[1:100]}; //如果mode=0,range在1-100中随机
(mode==1) -> range inside {[500:600]}; //如果mode=1,range在500-600中随机
// Code your testbench here
// or browse Examples
class mo;
rand bit mode;
rand bit[10:0] range;
constraint c_range{
(mode==0) -> range inside {[1:100]};
(mode==1) -> range inside {[500:600]};
}
endclass
module test;
initial begin
mo c = new;
c.mode.rand_mode(0);
c.mode = 1;
c.randomize();
$display("mode is %0d, the range is %0d",c.mode,c.range);
end
endmodule