IMPORTANT NOTICE: Please be advised that the Verification Academy Forums will be offline for scheduled maintenance on Sunday, March 23rd at 4:00 US/Pacific.
I have a 3x3 array - Each entry in the array can be RED, GREEN or BLUE. Can I randomize the array such that the difference in number of RED/GREEN/BLUE is never more than one? I can achieve this using post randomize but I was wondering if I can do it within a constraint - can we use temporary variables (eg: to count each type) within a constraint?
Using function (like shown below) will not work but I’m pasting it here to hopefully make the intent clear
Thanks
AC
class rgb_matrix;
typedef enum {RED, GREEN, BLUE} val_e;
rand val_e mda[3][3];
constraint check{
count_check(mda) == 1'b0;
}
function bit count_check(val_e val[3][3]);
int num_red = 0;
int num_green = 0;
int num_blue = 0;
bit fail = 1'b0;
foreach (val[i,j]) begin
if (val[i][j] == RED) num_red++;
else if (val[i][j] == GREEN) num_green++;
else if (val[i][j] == BLUE) num_blue++;
end
if (((num_red > num_blue) && (num_red-num_blue >1)) || (num_blue-num_red)>1)) fail = 1;
if (((num_green > num_blue) && (num_green-num_blue >1)) || (num_blue-num_green)>1)) fail = 1;
if (((num_red > num_green) && (num_red-num_green >1)) || (num_green-num_red)>1)) fail = 1;
return (fail);
endfunction
endclass
Sorry I overlooked that while mapping my problem to this example (my problem would require only 2 of the differences to be <=1 which I should have articulated in this example). But your solution answers my question - Thanks a lot for the prompt response!
However I do get compile errors: Is there any switch I need to enable to support this code? I also tried with changing `default_nettype (to make sure it was not none)
Error-[IND] Identifier not declared
test.sv, 232
Identifier ‘D2’ has not been declared yet. If this error is not expected,
please check if you have set `default_nettype to none.
Error-[XMRE] Cross-module reference resolution error
test.sv, 233
Error found while trying to resolve cross-module reference.
token ‘sum’. Originating program ‘test’.
Source info: D1.sum(D2) with (byte’((D2 == BLUE)))
Was anyone able to make this work on EDAPlayground? If yes, can you pls share the link? I see a bunch of errors referring to D1 and D2 and c_diff constraint.