日常记录(25)类copy、虚类、约束等
类copy、虚类等
虚类,以下代码中若基类没有virtual声明,则输出全为基类的disp调用。
copy函数是手动实现的。有必要的时候内部实现一个copy_data函数辅助。
class ClassTest; // data or class properties static int count=0; int id; // initialization function new(); id=count++; endfunction : new endclass : ClassTest class ClsBase; // data or class properties static string base_name="cls_base name"; int a; int b; ClassTest ct=new; // initialization function new(); a=1; b=2; endfunction : new function ClsBase copy(); ClsBase cb=new; cb.a=a; cb.b=b; cb.ct = new ct; return cb; endfunction: copy virtual function void disp(); $display("this is a ClsBase display"); endfunction: disp endclass : ClsBase class ClsA extends ClsBase; // data or class properties // initialization function new(); super.new(); endfunction : new virtual function void disp(); $display("this is a ClsA display"); endfunction: disp endclass : ClsA class ClsB extends ClsBase; // data or class properties int bb; // initialization function new(); super.new; bb=1; endfunction : new function void disp(); $display("this is a ClsB display"); endfunction: disp endclass : ClsB module taa (); initial begin ClassTest array_c[10]; for(int i=0;i<10;i++) begin array_c[i]=new; $display("c1.id is %d", array_c[i].id); end end initial begin ClsBase cb=new; ClsBase cb2; ClsBase cb3; ClsA cla; ClsB clb; ClsBase clta; ClsBase cltb; cb2=new cb; cb2.a=111; cb2.ct.id=66; $display("cb.a is %d", cb.a); $display("cb.ct.id is %d", cb.ct.id); $display("cb2.a is %d", cb2.a); $display("cb2.ct.id is %d", cb2.ct.id); cb3=cb.copy(); $display("cb3.ct.id is %d", cb3.ct.id); cb3.ct.id=12; $display("cb.ct.id is %d", cb.ct.id); $display("cb3.ct.id is %d", cb3.ct.id); cla=new; clb=new; clta=cla; cltb=clb; clta.disp(); cltb.disp(); end endmodule
输出
c1.id is 1 c1.id is 2 c1.id is 3 c1.id is 4 c1.id is 5 c1.id is 6 c1.id is 7 c1.id is 8 c1.id is 9 c1.id is 10 cb.a is 1 cb.ct.id is 66 cb2.a is 111 cb2.ct.id is 66 cb3.ct.id is 66 cb.ct.id is 66 cb3.ct.id is 12 this is a ClsA display this is a ClsB display
系统时间、randc语法、$fatal、$system等
module tbb (); longint time_info; function func_rand; int fd; int cut_time; begin $display("system time is %d", $system("date +%s%N")); $system("date +%s%N > localtime.tmp"); fd=$fopen("localtime.tmp", "r"); cut_time=$fscanf(fd, "%d",time_info); $fclose(fd); $system("rm -f localtime.tmp"); $display("cut time is %d", cut_time); $display("system time is %d", time_info); end endfunction function void statistic(int arr[]); int arr_u[int]; foreach(arr[i]) if(arr_u[arr[i]]==null) arr_u[arr[i]]=1; else arr_u[arr[i]]++; foreach(arr_u[i]) $display("the final statistic %0d, count:%0d", i,arr_u[i]); endfunction: statistic class ModCls; // data or class properties rand reg [3:0] y; randc bit [1:0] z; constraint c1 {y>2; y<10;} // initialization function new(); endfunction : new endclass : ModCls initial begin int a[10000]; ModCls mc; $display("before seed is %d", time_info); func_rand; $display("after seed is %d", time_info); for (int i = 0; i < 1000; i++) begin mc = new; //mc.srandom(time_info); mc.randomize(); a[i]=mc.y; end statistic(a); $display("the randc value is "); for (int i = 0; i < 20; i++) begin mc=new; assert(mc.randomize()) else $fatal(0, "random ranc failed!"); $write(" %d", mc.z); end $display(); $display("the randc value is "); mc=new; for (int i = 0; i < 20; i++) begin assert(mc.randomize()) else $fatal(0, "random ranc failed!"); $write(" %d", mc.z); end $display(); end initial begin $display("random seed : %d", $random()); $display("random seed : %d", $random()); $display("random seed : %d", $random()); $display("random seed : %d", {$random()}); $display("random seed : %d", {$random()}); $display("random seed : %d", {$random()}); $display("random seed : %d", {$random()}); end endmodule
fscanf可能返回读取到的数据个数、$system输出的信息返回值为0表示命令执行成功,获取内容需要进行文件读写。
输出
before seed is 0 1640753939063821762 system time is 0 cut time is 1 system time is 1640753939065530219 after seed is 1640753939065530219 the final statistic 0, count:9000 the final statistic 3, count:136 the final statistic 4, count:155 the final statistic 5, count:136 the final statistic 6, count:163 the final statistic 7, count:147 the final statistic 8, count:134 the final statistic 9, count:129 the randc value is 0 2 1 2 3 0 2 1 3 3 1 1 2 2 2 0 1 0 1 1 the randc value is 1 0 2 3 3 1 2 0 3 0 2 1 0 2 3 1 0 1 2 3 random seed : 303379748 random seed : -1064739199 random seed : -2071669239 random seed : 2985317987 random seed : 112818957 random seed : 1189058957 random seed : 2999092325
类的约束、虚类、inside、srandom等
virtual class ClassCons; // data or class properties rand int a[10]; local int b; constraint c1 { a[0] dist{0:=1, [1:10]:=10}; a[1] dist{0:=1, [1:10]:/10}; a[2] inside{1, 2}; a[3] >10; a[3] <=13; a[4] >=0; a[4] <=5; a[3]==11 -> a[4]==1; a[3]==12 -> a[4]==3; a[5] >10; a[5] <=13; a[6] >=0; a[6] <=5; a[5]==11 -> a[6]==1; a[5]==12 -> a[6]==3; solve a[6] before a[5] ; foreach (a[i]) if(i>=7) a[i] inside{i, i**2,i**3}; //foreach (a[i]) // if(i>=7) // a[i] inside{i, i**2,i**3}; //for (int i = 0; i < count; i++) begin // a[i]=i //end } constraint c2 { a[7] > a[8]; } constraint c3 { a[7] < a[8]; } // initialization function new(); endfunction : new function void statistic(int arr[]); int arr_u[int]; foreach(arr[i]) if(arr_u[arr[i]]==null) arr_u[arr[i]]=1; else arr_u[arr[i]]++; foreach(arr_u[i]) $display("the final statistic %0d, count:%0d", i,arr_u[i]); endfunction: statistic endclass : ClassCons class ClassExt extends ClassCons; // data or class properties // initialization function new(); endfunction : new endclass : ClassExt module tcc (); initial begin `define STAT_SIZE 10000 //ClassCons is an abstract class ClassExt cc1=new; ClassExt cc2=new; int stats[`STAT_SIZE][$]; int stats_tmp[`STAT_SIZE]; cc1.srandom(12); cc1.rand_mode(1); cc1.c3.constraint_mode(0); for (int i = 0; i < `STAT_SIZE; i++) begin cc1.randomize(); foreach(cc1.a[j]) begin stats[i].push_back(cc1.a[j]); end end for (int j = 0; j < 10; j++) begin $display("ans------------%d",j); foreach(stats[i]) stats_tmp[i] = stats[i][j]; cc1.statistic(stats_tmp); end //cc2.rand_mode(0); foreach(stats[i]) stats[i].delete; cc2.c1.constraint_mode(1); cc2.c2.constraint_mode(0); cc2.c3.constraint_mode(0); $display("constraint mode cc2 c1 is %d", cc2.c1.constraint_mode); $display("constraint mode cc2 c2 is %d", cc2.c2.constraint_mode); for (int i = 0; i < `STAT_SIZE; i++) begin cc2.randomize(); foreach(cc2.a[j]) begin stats[i].push_back(cc2.a[j]); end end for (int j = 0; j < 10; j++) begin $display("ans------------%d",j); foreach(stats[i]) stats_tmp[i] = stats[i][j]; cc2.statistic(stats_tmp); end end initial begin ClassExt ce1=new; $display("value a:%d",ce1.a[0][0]); //$display("value b:%d",ce1.b); end endmodule
输出
过程中需要清空队列
foreach(stats[i]) stats[i].delete;
ans------------ 0 the final statistic 0, count:93 the final statistic 1, count:1009 the final statistic 2, count:1017 the final statistic 3, count:972 the final statistic 4, count:1006 the final statistic 5, count:1013 the final statistic 6, count:959 the final statistic 7, count:965 the final statistic 8, count:967 the final statistic 9, count:1028 the final statistic 10, count:971 ans------------ 1 the final statistic 0, count:912 the final statistic 1, count:851 the final statistic 2, count:881 the final statistic 3, count:946 the final statistic 4, count:903 the final statistic 5, count:900 the final statistic 6, count:918 the final statistic 7, count:946 the final statistic 8, count:904 the final statistic 9, count:898 the final statistic 10, count:941 ans------------ 2 the final statistic 1, count:4953 the final statistic 2, count:5047 ans------------ 3 the final statistic 11, count:3357 the final statistic 12, count:3319 the final statistic 13, count:3324 ans------------ 4 the final statistic 0, count:590 the final statistic 1, count:3889 the final statistic 2, count:532 the final statistic 3, count:3850 the final statistic 4, count:559 the final statistic 5, count:580 ans------------ 5 the final statistic 11, count:838 the final statistic 12, count:833 the final statistic 13, count:8329 ans------------ 6 the final statistic 0, count:1725 the final statistic 1, count:1663 the final statistic 2, count:1653 the final statistic 3, count:1619 the final statistic 4, count:1638 the final statistic 5, count:1702 ans------------ 7 the final statistic 49, count:4975 the final statistic 343, count:5025 ans------------ 8 the final statistic 8, count:7499 the final statistic 64, count:2501 ans------------ 9 the final statistic 9, count:3363 the final statistic 81, count:3314 the final statistic 729, count:3323 constraint mode cc2 c1 is 1 constraint mode cc2 c2 is 0 ans------------ 0 the final statistic 0, count:98 the final statistic 1, count:1003 the final statistic 2, count:993 the final statistic 3, count:990 the final statistic 4, count:996 the final statistic 5, count:988 the final statistic 6, count:929 the final statistic 7, count:968 the final statistic 8, count:999 the final statistic 9, count:984 the final statistic 10, count:1052 ans------------ 1 the final statistic 0, count:875 the final statistic 1, count:965 the final statistic 2, count:916 the final statistic 3, count:909 the final statistic 4, count:872 the final statistic 5, count:903 the final statistic 6, count:903 the final statistic 7, count:911 the final statistic 8, count:926 the final statistic 9, count:878 the final statistic 10, count:942 ans------------ 2 the final statistic 1, count:4915 the final statistic 2, count:5085 ans------------ 3 the final statistic 11, count:3298 the final statistic 12, count:3325 the final statistic 13, count:3377 ans------------ 4 the final statistic 0, count:570 the final statistic 1, count:3850 the final statistic 2, count:563 the final statistic 3, count:3882 the final statistic 4, count:564 the final statistic 5, count:571 ans------------ 5 the final statistic 11, count:834 the final statistic 12, count:864 the final statistic 13, count:8302 ans------------ 6 the final statistic 0, count:1667 the final statistic 1, count:1677 the final statistic 2, count:1677 the final statistic 3, count:1690 the final statistic 4, count:1580 the final statistic 5, count:1709 ans------------ 7 the final statistic 7, count:3374 the final statistic 49, count:3321 the final statistic 343, count:3305 ans------------ 8 the final statistic 8, count:3349 the final statistic 64, count:3342 the final statistic 512, count:3309 ans------------ 9 the final statistic 9, count:3329 the final statistic 81, count:3330 the final statistic 729, count:3341 value a:0
参数化类#, 约束的function,soft,with
class Transaction; rand bit [31:0] addr,data; constraint c1 {soft addr inside {[0:100],[1000:2000]};} endclass module tdd (); class ClsA #(int length); // data or class properties rand bit[2:0] a[3]; int arr=length; constraint c1 { a[0] inside{1,2,3}; } constraint c2 { a[0]==1; } int c[4]; constraint c3 {c[3]<11; c[2]< consta(); } // initialization function new(); endfunction : new function int consta(); return 10; endfunction: consta endclass : ClsA initial begin ClsA #(10) ca=new; ca.c2.constraint_mode(0); for (int i = 0; i < 20; i++) begin ca.randomize(); $display("value is %d", ca.a[0]); end $display("arr value is %d", ca.arr); ca.randomize with {ca.a[0]<5;}; end Transaction t; initial begin t= new(); assert(t.randomize() with {addr inside {[200:300]}; data inside {[10:20]}; }); end endmodule
输出
value is 1 value is 1 value is 1 value is 3 value is 1 value is 2 value is 2 value is 1 value is 1 value is 3 value is 2 value is 1 value is 2 value is 1 value is 1 value is 3 value is 1 value is 2 value is 3 value is 2 arr value is 10
Le vent se lève! . . . il faut tenter de vivre!
Le vent se lève! . . . il faut tenter de vivre!