"Invalid random variable in index expression for constraint"

I am trying to constrain an enum variable to be randomized in a class but I get the following error on Questa Sim:

** Failure: noc_prog.sv(0): Invalid random variable in index expression for constraint.

My constraint is:


constraint links {
if ((this.noc_member.router_grid[this.router_row_idx][this.router_col_idx].row_idx == 0) && (this.noc_member.router_grid[this.router_row_idx][this.router_col_idx].col_idx == 0) && (! (this.noc_member.router_grid[this.router_row_idx][this.router_col_idx].isLinkActive(EAST)) || ! (this.noc_member.router_grid[this.router_row_idx][this.router_col_idx].isLinkActive(SOUTH))))
		router_links inside {NORTH, WEST};
	else
		router_links inside {NORTH, EAST , SOUTH, WEST};
}

Where “router_links” is of type:


typedef enum {NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3, LOCAL = 4} e_ports_direction;

and “router_row_idx”, “router_col_idx”, “row_idx”, “col_idx” are of type “int”.

A glimpse of my class members:


class disable_links_random;

static rand int router_row_idx;
static rand int router_col_idx;
static rand e_ports_direction router_links;

static noc noc_member;
...

What am I doing wrong?

In reply to khaledismail:

Index expressions can include loop variables, constants, and state variables. Invalid or out-of-bounds array indices are not automatically eliminated; users must explicitly exclude these indices using predicates.

As the error message explains, random variables are not allowed as index expressions. You need a foreach loop.

constraint links { foreach(noc_member.router_grid[row,col])
   if ( row == router_row_idx && col == router_col_idx && 
        noc_member.router_grid[row][col].row_idx == 0) &&        
        noc_member.router_grid[row][col].col_idx == 0) && 
        (!noc_member.router_grid[row][col].isLinkActive(EAST) ||
        !noc_member.router_grid[row][col].isLinkActive(SOUTH) ) )
		router_links inside {NORTH, WEST};
	else
		router_links inside {NORTH, EAST , SOUTH, WEST};
}

Also, this. in your constraint is unnecessary and makes your code harder to read.

In reply to dave_59:

Thank you Dave :)