Hi There,
I want to generate a value req.a which should not be inside a range of values (range_of_values)
Provided each value from req.a to req.a + req.s should not be inside the range_of_values
I am trying something like the below syntax but I am getting syntax error.
req.randomize() with {!((req.a : req.a+req.s) inside {range_of_values}) ;};
I also tried something like the following without any success in compiling.
req.randomize() with {!( foreach (i < req.s)
(req.a+i) inside {range_of_values}
);};
Please help.
Thanks,
Sunil.
In reply to KumarSunilB:
Please provide declarations for all variables involved. Which variables are random and which are non-random? Can you provide examples of the results you are expecting?
In reply to dave_59:
Hi Dave,
Here are the details.
class packet;
rand bit [7:0] a;
rand bit [2:0] s;
endclass: packet
packet req;
bit [7:0] range_of_values[$];
The Queue stores the values of a to a+s after each randomization call.
Next time when we randomize the generated value a to a+s should be excluded from the random generation.
So lets say after 1st randomization call a = 50 and s = 5, then range_of_values = {50, 51, 52, 53, 54, 55};
So next time after the randomization call a = 47 ans s = 5 is illegl but a = 47 and s = 2 is legal.
In reply to KumarSunilB:
Thanks, that is very clear now. Try this:
class packet;
rand bit [7:0] a;
rand bit [2:0] s;
bit [7:0] range_of_values[$];
function void post_randomize;
for(int ii=a; ii<=a+s; ii++)
range_of_values.push_back(ii);
endfunction
constraint not_in_range {
foreach(range_of_values[ii])
!(range_of_values[ii] inside {[a:a+s]});}
constraint no_overflow { 8'(a+s) == int'(a+s);}
endclass : packet
In reply to dave_59:
Thanks Dave for the solution.
I modified the code a bit.
module top();
bit [7:0] range_of_values[$];
class packet;
rand bit [7:0] a;
rand bit [2:0] s;
function void post_randomize;
for(int ii=a; ii<=a+s; ii++)
range_of_values.push_back(ii);
endfunction
constraint no_overflow { 8'(a+s) == int'(a+s);}
endclass : packet
initial
begin
packet pp;
pp = new();
pp.randomize() with { // pp.a != 5 &&
foreach(range_of_values[ii])
!(range_of_values[ii] inside {[a:a+s]});
};
end
endmodule
The above code works.
If I uncomment the part “pp.a !=5 &&” I see compile error
** Error: (vlog-13069) aa.sv(23): near “foreach”: syntax error, unexpected foreach.
Let me know how to avoid it.
Sunil.
In reply to dave_59:
constraint not_in_range {
foreach(range_of_values[ii])
!(range_of_values[ii] inside {[a:a+s]});}
constraint no_overflow { 8'(a+s) == int'(a+s);}
Hi Dave, Can you explain this line of code
By Subha
In reply to Mobi.Subha:
constraint not_in_range {
foreach(range_of_values[ii])
!(range_of_values[ii] inside {[a:a+s]});
}
Every item in range_of_values array will not be in range [a:a+s].
constraint no_overflow {
8'(a+s) == int'(a+s);
}
I think Dave wanted to make sure the (a+s) will never be overflow and the range [a:a+s] is always valid.
For example:
If a = 8’hff and s = 3’h2. Then the sum 8’(a+s) = 8’h1 and the range [a:a+s] will be [8’hff:8’h01] which is invalid. He didn’t want this case happen.