Macro to generate a list of coverpoints

I have defines some coverpoints using a numbered MACRO.
e.g.
MY_MACRO_0
MY_MACRO_1
MY_MACRO_2

In every MACRO I defined a different coverpoint.
e.g.
`define MY_MACRO_0
my_cg0: coverpoint x0 {
bins my_bins = {0,1};
}

I would like to have a macro that iterates through all MACROs to generate a unification of all the coverpoints.
Is there a way to do it?

Regards, Yagel

1 Like

There is no way to do any kind of iterations with a macro. There might be a completely different approach to what you were after if we just knew what that was. See https://xyproblem.info

I manged to solve it using the following:

`FOR_LOOP(0,LAST_IDX,MY_MACRO,GENERATE_COVERPOINT)

`define GENERATE_COVERPOINT(NAME,IDX) `NAME``_``IDX

`define IF_ELSE(COND, THEN, ELSE) `ifdef COND THEN `else ELSE `endif

`define FOR_LOOP(START, END, NAME, BODY) \
   `define FOR_LOOP_``END\
   `IF_ELSE(FOR_LOOP_``START, `BODY(NAME,START) , \
   `BODY(NAME,START) \
   `FOR_LOOP_01(1, END, NAME, BODY) \
   ) \
   `undef FOR_LOOP_``END

`define FOR_LOOP_01(START, END, NAME, BODY) `IF_ELSE(FOR_LOOP_``START, `BODY(NAME,START) , `BODY(NAME,START) `FOR_LOOP_02(2, END, NAME, BODY))
`define FOR_LOOP_02(START, END, NAME, BODY) `IF_ELSE(FOR_LOOP_``START, `BODY(NAME,START) , `BODY(NAME,START) `FOR_LOOP_03(3, END, NAME, BODY))
`define FOR_LOOP_03(START, END, NAME, BODY) `IF_ELSE(FOR_LOOP_``START, `BODY(NAME,START) , `BODY(NAME,START) `FOR_LOOP_04(4, END, NAME, BODY))
`define FOR_LOOP_04(START, END, NAME, BODY) `IF_ELSE(FOR_LOOP_``START, `BODY(NAME,START) , `BODY(NAME,START) `FOR_LOOP_05(5, END, NAME, BODY))
`define FOR_LOOP_05(START, END, NAME, BODY) `IF_ELSE(FOR_LOOP_``START, `BODY(NAME,START) , `BODY(NAME,START) `FOR_LOOP_06(6, END, NAME, BODY))

This is quite an interesting use of recursion to create iterations!

BTW, this code takes advantage of a clarification in the IEEE 1800-2023 SystemVerilog LRM allowing combinations of multiple compiler directives on the same text line.