In reply to Bahaa Osman:
Hi Bahaa Osman, here is some quick code…
#1 not too many unnecessary objects are created(though some redundancy is still there)
#2 No need to keep constraints in sync
#3 adding another brand does not require base class modification
but adding another field to enum does cause issue-
can be fixed by constraining base class to randomize known types at that time
intentionally left size randomization outside the class to keep it simple…
did not check the code well - please point out if there are any glaring issues :)
typedef enum {HONDA, TOYOTA, FORD,TESLA} brand_t;
class car;
rand brand_t brand_e;
virtual function void print();
$display("obj handle of type car ");
endfunction
endclass
class toyota extends car;
constraint my_brand {brand_e == TOYOTA;}
// Adds other constraints, methods and variables related only to this class
virtual function void print();
$display("obj handle of type toyota");
endfunction
endclass
class honda extends car;
constraint my_brand {brand_e == HONDA;}
// Adds other constraints, methods and variables related only to this class
virtual function void print();
$display("obj handle of type honda");
endfunction
endclass
class ford extends car;
constraint my_brand {brand_e == FORD;}
// Adds other constraints, methods and variables related only to this class
virtual function void print();
$display("obj handle of type ford");
endfunction
endclass
class tesla extends car;
constraint my_brand {brand_e == TESLA;}
// Adds other constraints, methods and variables related only to this class
virtual function void print();
$display("obj handle of type tesla");
endfunction
endclass
class txn;
rand car mycars_arr[];
int size;
// I need to randomize mycar_arr. Here are my constraints:
// 1- Array size
// 2- Type of car in each array location
// 3- Then based on the types i end up in each location, I want to randomize an object of the right type of class and assign that object to the array location.
function void pre_randomize(); //create objects during pre_rand, else randomize() has no impact
mycars_arr = new[size];
for (int i=0;i<size;i++)
begin
mycars_arr[i]= new(); //objects are used only to create random list of brand_e, later in post_rand this will be loaded with apprp objects
end
endfunction
function void post_randomize(); //randomized value of brand_e is available only at post_randomize
toyota toyota_;
honda honda_;
ford ford_;
car car_;
foreach (mycars_arr[i])
begin
case(mycars_arr[i].brand_e)
TOYOTA : begin mycars_arr[i]= toyota::new(); mycars_arr[i].randomize(); end //fixed ptr issue, thnx Bahaa Osman
HONDA : begin mycars_arr[i]= honda::new() ; mycars_arr[i].randomize() ; end
FORD : begin mycars_arr[i]= ford::new() ; mycars_arr[i].randomize() ; end
default: begin default_case(mycars_arr[i]); end
endcase
end
endfunction:post_randomize
virtual function void default_case(ref car car_);
endfunction: default_case
endclass
class txn2 extends txn;
tesla tesla_;
virtual function void default_case(ref car car_);
case(car_.brand_e)
TESLA: begin car_ = tesla::new(); car_.randomize(); end
endcase
endfunction: default_case
endclass
module tb();
txn i_txn = new();
txn2 i_txn2 = new();
initial
begin
repeat(1)
begin
i_txn.size =5; //size of array is to be controlled before randomize, this is to simplify and keep object creation to min
i_txn.randomize;
foreach(i_txn.mycars_arr[i]) i_txn.mycars_arr[i].print();
$display("txn2 type randomization, adds tesla to the mix");
i_txn = i_txn2; //addition of new brand does not need any modification to base class
i_txn.size =5;
i_txn.randomize;
foreach(i_txn.mycars_arr[i]) i_txn.mycars_arr[i].print();
end
end
endmodule:tb
EDIT: Fixed ptr issue as per Bahaa Osman’s input, thnq