I have a dynamic array of objects. Each object has an enum variable, let’s call it instruction, values can be ADD, SUB, MUL, DIV for example. When trying to randomize the transaction which includes the dynamic array, i want to constraint the number of objects that has instruction set to ADD.
Pseudo code looks something like this:
class c;
rand enum (ADD, SUB, MUL, DIV) e;
endclass
class txn;
rand c c_inst;
constraint add_less_than_5 {(c_inst.find with (item.e == ADD)) <= 5)}
endclass
The result of all the equality/relational/logical operators are all 1-bit 1’b1 or 1’b0. The result of the sum() method has the same type as each element.
class c;
rand enum {ADD, SUB, MUL, DIV} e;
endclass
class txn;
rand c c_inst[];
function void pre_randomize();
for(int i=0;i<10;i++)begin
c_inst[i]= new();
c_inst[i].randomize();
end
endfunction
constraint add_less_than_5 {c_inst.sum() with (int'(item.e == c::ADD)) == 5;}
endclass
module abc;
txn t1;
initial begin
t1=new();
repeat(10) begin
assert(t1.randomize());
$display("%p",t1);
end
end
endmodule
but getting this
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
# Time: 0 ns Iteration: 0 Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
# Time: 0 ns Iteration: 0 Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
# Time: 0 ns Iteration: 0 Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
# Time: 0 ns Iteration: 0 Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
# Time: 0 ns Iteration: 0 Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
# Time: 0 ns Iteration: 0 Process: /abc/#INITIAL#24 File: testbench.sv Line: 13
# ** Error: (vsim-7100) Invalid call to class::randomize() via a null class reference.
function void pre_randomize();
c_inst = new[10]; // 10 elements at default value 'null'
foreach(c_inst[i])
begin
c_inst[i]= new();
c_inst[i].randomize(); // Not Needed !!
end
endfunction