Constraint solving for enums

I’ve got a strange problem with constraining randomized enums and can’t figure out why my sim is hanging.

I have an enum type defined in a file, common_types.svh.

typedef enum { TXMODE_ON,
	       TXMODE_OFF } tx_mode_t;

I include common_types.svh and use it in my class “cdi”. In the class cdi, I define a random variable of the type.

rand tx_mode_t tx_mode = TXMODE_OFF;

Randomizing the class then by instantiating an object obj_name and then calling obj_name.randomize() works fine and produces values for tx_mode as you would expect. The sim runs quickly.

However, if I constrain other random variables based on the value of that enum, the simulation seems to hang. It hangs on the call to obj_name.randomize(). It may actually finish without error, but it hangs for an extremely long time and I haven’t let it run to completion because it takes SO long to get past the randomization call. For my needs, having to wait an hour for the sim to start is as good as a failure and I need to find a way around that. :)

The constraints I’ve added that cause the sim to hang are as follows…

constraint c_pps { if (tx_mode == TXMODE_ON) number_of_pps == 1;
                       else if (tx_mode == TXMODE_OFF) number_of_pps > 0 && number_of_pps <= 50; 
		       else number_of_pps == 1; }

That seems fairly straightforward and I wouldn’t expect it to cause these kinds of problems. Is there a better way to use randomized enum variables or is that not recommended for some reason? Has anyone else seen such effects?

Final note… this is a simplified case to start with. As my tests evolve, that enum type will grow to include additional values. I’m trying to use an enum because it would enhance the readability of the code for these settings and states as opposed to just using integer values and giving them specific meaning.

gb

I haven’t seen any issues doing what you describe, and the performance will depend on the complexity of your constraints, but taking a significant time to solve definitely requires investigation.

Another possible coding could be using the implication operator, along the lines of:

constraint c_pps { tx_mode == TXMODE_ON → number_of_pps == 1;
tx_mode == TXMODE_OFF → number_of_pps > 0 && number_of_pps <= 50; }

This might be easier to read and extend for additional enum states.

constraint c_pps { if (tx_mode == TXMODE_ON) number_of_pps == 1;
else if (tx_mode == TXMODE_OFF) number_of_pps > 0 && number_of_pps <= 50;
else number_of_pps == 1; }

void function post_randomize()

if (tx_mode == TXMODE_ON)
number_of_pps == 1;
else if (tx_mode == TXMODE_OFF) number_of_pps =$urandom_range( 0 ,50 );
else number_of_pps == 1

endfunction

constraint c_pps {
solve tx_mode before number_of_pps;
if (tx_mode == TXMODE_ON) number_of_pps == 1;
else if (tx_mode == TXMODE_OFF) number_of_pps > 0 && number_of_pps <= 50;
else number_of_pps == 1;

}

Please try with this and let me know.

Thanks
Saravanan

cgales,

constraint c_pps { tx_mode == TXMODE_ON → number_of_pps == 1;
tx_mode == TXMODE_OFF → number_of_pps > 0 && number_of_pps <= 50; }

That was the original form I wrote the constraint in. Was just trying explicit if/else statements because I was flailing around for some solution to the simulation hang and figured anything was worth a try.

Saravanan,

Please try with this and let me know.

Tried the solve before constraints and it didn’t change the outcome. The sim still hangs trying to solve the enum randomization. I was hoping that would work too. Oh well…

Thanks for the suggestions…

What you have posted is a very straight forward set of constraints and shouldn’t take significant effort to solve. If you are having issues with solving what you have posted, it sounds like a tool-specific issue and you should contact your tool vendor’s support team.