PDA

View Full Version : Adding arguments to constructor



CraigA
06-07-2008, 03:40 AM
Has anyone found a good way to add arguments to the constructor on an ovm_component?

If you use `ovm_component_utils or `ovm_component_utils_begin/end
it makes you a constructor [new()] with only name and parent in it.

What if you want to add another argument in new()?

kurts
06-07-2008, 11:22 AM
Actually, those macros do not create a constructor. You still must write your own constructor, keeping in mind that you must supply the parent class constructor with a string name and an ovm_component parent argument. These macros are for factory registration and preparation for field automation macros like `ovm_field_int()

You can write a constructor like this:


function new(string name, ovm_component parent, int my_custom_arg);
super.new(name, parent); // required
// do what you want with my_custom_arg
endfunction

Keep in mind, though, that if you have extra required arguments, you will not be able to use the factory to create this component. You will have to create it yourself by calling new() and supplying the argument(s). In doing so, you will miss out on the dynamic factory override capability.

An alternate approach that I would suggest is to just write a standard constructor with the name and parent arguement, and use the factory to create the component.

Then, for any extra value that you would have passed in through the constructor, use the configuration API instead. Set the configuration value for the instance in the parent class build(), and then get the value in the object's build() function. The pattern would look something like this:


function void build(); // in hierarchical parent
set_config_int("child_inst_name","config_var_name",some_value);
$cast(child_handle, create_component("child_type","child_name"));
endfunction



function void build(); // in child component
assert(get_config_int("config_var_name",child_var));
endfunction

-Kurt