[help] any good way to randomize compound object?

Hi all,

is there any good way to randomize compound object? which means there are object(s) inside an object.

Experience shows sometimes the sub object also randomized when invoke c_obj.randomize(), but sometimes nothing happens… is there any good way to do it? or is there any documentation talking about this, please give me some hints.

thanks a lot!

Hi,

You can use rand_mode(0/1) to enable disable randomization of sub object.

Regards,

As following example, is there any way that could generate the same kind of randomized compound object without inserting an customized “gen()” method? thanks!

you could ran this example by using
irun -ovm -sv a.v

include "ovm.svh" class a extends ovm_object; rand int int_array[]; rand int int_array_size; constraint c_int_array_size { int_array_size < 40; int_array_size > 10; int_array.size() == int_array_size; } ovm_object_utils_begin(a)
ovm_field_array_int (int_array, OVM_ALL_ON) ovm_object_utils_end
function new (string name = “a”);
super.new(name);
endfunction : new
endclass: a
class aa extends ovm_object;
rand a a_array;
rand int a_array_size;
constraint c_a_array_size {
a_array_size < 40; a_array_size > 10;
a_array.size() == a_array_size;
}
ovm_object_utils_begin(aa) ovm_field_int (a_array_size, OVM_ALL_ON)
`ovm_object_utils_end
function new(string name = “aa”);
super.new(name);
endfunction : new
function gen();
foreach (a_array[i]) a_array[i] = a::type_id::create(“a_array”, null);
foreach (a_array[i]) a_array[i].randomize();
endfunction : gen
endclass
module tb;
aa aa_obj;
a a_array2;
initial begin
aa_obj = aa::type_id::create(“aa_obj”, null);
aa_obj.randomize();
$display(“size: %d”, aa_obj.a_array.size());
aa_obj.gen();
a_array2 = aa_obj.a_array;
foreach( a_array2[i] )
$display(“- size: %d”, a_array2[i].int_array.size());
end
endmodule

Hi ,

Instead of gen you can do this using post_randomize function.

function void post_randomize();
foreach (a_array[i]) a_array[i] = a::type_id::create(“a_array”, null);
foreach (a_array[i]) void’(a_array[i].randomize());
endfunction : post_randomize

Regards,

Thanks for the help, using post_randomize is bettern than the customized one.

however, is ther any reason why OVM could not also provide the field automation macro for array object to make sub object array randomize automatically?

Hi,

This is not OVM limitation its SV language limitation.

The randomize function in SV doesn’t allocate the class object and its obvious it should not ALLOCATE class objects as your constructor arguments may differ for each class object.

Regards,