How to pass a value to macro

Hi,

To create multiple instance of my parameterized class, I use following code :

//-------------------
define PARAMS(WIDTH) ``WIDTH,``WIDTH for(int i =0; i<2; i++) begin mon#(PARAMS(i)) mon_inst[i];
mon_inst[i] = mon#(`PARAMS(i))::type_id()::create(“$psprintf(“mon_inst_%0d”,i),this);
end

It expands to : mon_inst[i] = mon#(i,i)::type_id()::create(“$psprintf(“mon_inst_%0d”,i),this);
I wanted it to as :
mon_inst[i] = mon#(0,0)::type_id()::create(“$psprintf(“mon_inst_%0d”,i),this);
mon_inst[i] = mon#(1,1)::type_id()::create(“$psprintf(“mon_inst_%0d”,i),this);

//-------------------

It doesn’t pass the value of “i”. just pass the “i” in macro expand. Please let me know if it’s possible OR is there any other way to solve this.

Thanks & Regards,
Krupa

One significant issue is that your re-declare the mon_inst array inside your loop with different parameterizations and array sizes. Also, since it is declared inside the for loop, it won’t exist when the loop exits.

You can’t create an array of monitors which have different parameterizations since they are considered different types. You will need a unique declaration for each type.

For Class,

  • You can not.This is known issue,
  • It will treat whatever you pass as a “String” not value
  • So it will take “i” not value of i.

For Module,

  • You can. Use generate block,genvar and for loop.

Regards,
Supal Shah

It is possible to create a unique string name to register each specialization of a parameterized class with the factory. See my blog post. You will still need to declare each specialization manually, or use a generate loop to get each specialization registered, then you can use a for loop in your procedural code to create what you need by a string name.