1.约束
(1)注意
一般随机约束只能在类中使用;
(2)实现步骤
在定义变量时,用rand/randc关键字进行修饰;
定义约束;
创建并实例化类后,调用随机约束方法;
(3)常见的约束
A 双向约束
a 说明:约束块之间是并行的,约束语句之间也是并行;
b 例子:
class randomize_test;rand bit [31:0] src,dst,data[4];rand bit [7:0] kind;constraint cstr{src >10;src <15;}function void print();$display("src is %0d\n dst is %0d\n kind is %0d\n data is %p",src,dst,kind,data);endfunction
endclass
module tb;randomize_test p;initial beginp = new();$display("Before randomize");p.print();p.randomize();$display("After randomize");p.print();end
endmodule
c结果:
src的取值范围是10<src<15
B 内嵌约束
a 说明:
限制变量取值范围;
语法:
方法一:在约束块中:
成员变量 inside 取值范围;
方法二:
实例化随机约束句柄.randmize() with {}
b 例子
例子1:
class randomize_test;rand bit [31:0] src,dst,data[4];rand bit [7:0] kind;constraint cstr{src inside {[1:5],[7:8]};}function void print();$display("src is %0d\n dst is %0d\n kind is %0d\n data is %p",src,dst,kind,data);endfunction
endclass
module tb;randomize_test p;initial beginp = new();$display("Before randomize");p.print();p.randomize();$display("After randomize");p.print();end
endmodule
例子2:
class randomize_test;rand bit [31:0] src,dst,data[4];rand bit [7:0] kind;constraint cstr{src inside {[1:5],[7:8]};}function void print();$display("src is %0d\n dst is %0d\n kind is %0d\n data is %p",src,dst,kind,data);endfunction
endclass
module tb;randomize_test p;initial beginp = new();$display("Before randomize");p.print();p.randomize() with { 5<src;};$display("After randomize");p.print();end
endmodule
c 结果
例子一:
例子二:
C 权重约束
a 说明:
约束每个值分配的权重
语法:
变量名 dist {值:=每个值分配权重,值:/共同分配的权值};
b 例子
class randomize_test;rand bit [31:0] src,dst,data[4];rand bit [7:0] kind;constraint cstr{src dist {[1:3]:/1,[7:8]:=2,10:=5};}function void print();$display("src is %0d\n dst is %0d\n kind is %0d\n data is %p",src,dst,kind,data);endfunction
endclass
module tb;randomize_test p;initial beginp = new();$display("Before randomize");p.print();p.randomize();$display("After randomize");p.print();end
endmodule
c 结果
权重分配情况:
1,2,3共同分配1的权重,即每个对应1/3;
7,8每个分配的权重为2;
10分配的权重为5;
D 唯一标识约束
a 说明
声明后,约束的变量取值不会重复;
语法:
unique {多个成员变量};
b 例子
class randomize_test;rand bit [1:0] src,dst,data[4];rand bit [7:0] kind;constraint cstr{unique {src,dst};}function void print();$display("src is %0d\n dst is %0d\n kind is %0d\n data is %p",src,dst,kind,data);endfunction
endclass
module tb;randomize_test p;initial beginp = new();$display("Before randomize");p.print();p.randomize();$display("After randomize");p.print();end
endmodule
c 结果
E 条件约束
a 说明
如果条件满足,约束要满足;
语法:
方法一:
条件 -> 约束;
方法二:
if(条件1)
约束1;
else if(条件2)
约束2;
else
约束3;
b 例子
F 动态数组的长度约束
a 说明
约束动态数组的长度;
语法:
动态数组名.size() 约束;//可以使用各种约束,比如取值范围约束
b 例子
class randomize_test;rand bit [31:0] src,dst,data[];rand bit [7:0] kind;constraint cstr{data.size < 5;}function void print();$display("src is %0d\n dst is %0d\n kind is %0d\n data is %p",src,dst,kind,data);endfunction
endclass
module tb;randomize_test p;initial beginp = new();$display("Before randomize");p.print();p.randomize();$display("After randomize");p.print();end
endmodule
c 结果
H 软约束
a 说明
当多个约束冲突时,可以用soft关键字修饰(soft表示软约束,默认是硬约束),当软约束和硬约束冲突时会优先执行硬约束;
一般:底层约束用soft关键字修饰,额外的约束用硬约束;
注意:
soft关键字修饰的是约束语句,不是约束块;
硬约束和硬约束冲突时,约束失败;
硬约束和软约束冲突时,执行硬约束;
软约束和软约束冲突时,会就近执行;(后写的:with额外约束 -> 子类约束 ->父类约束)
b 例子
class randomize_test;rand bit [31:0] src,dst,data[4];rand bit [7:0] kind;constraint cstr{soft src inside {[1:5]};}function void print();$display("src is %0d\n dst is %0d\n kind is %0d\n data is %p",src,dst,kind,data);endfunction
endclass
module tb;randomize_test p;initial beginp = new();$display("Before randomize");p.print();p.randomize() with{src inside {[6:10]};};$display("After randomize");p.print();end
endmodule
c 结果