@dave_59 - If ‘data’ is not declared as rand, will pre_randomize be called? In the above case, we can shift the initialization and shuffle part to post_randomize.
class A;
rand bit [11:0] data_in [99:0];
constraint data_range { foreach(data[i]) data_in[i] inside {[0:500]}; }
endclass
I want to make sure that all the 100 elements in ‘data_in’ are unique 12 bit values.
‘data’ does not need to be declared as rand. pre_randomize() is called when you call randomize() on that object regardless of it having any rand variables. If the object being randomized contains a class variable referencing another class object, that class variable must be declared rand to call the pre_randomize() method of the other class.
I still don’t understand your last question. Can you give an example with all the data declarations and show what you would like to see as the set of values that randomize() should generate?
Please refer to the class declaration in post #6. I can use unique identifier now. I was just interested in the approach adopted earlier. I don’t want to do something like data_in[i] = i and then shuffle.
SSingh,
I see my previous solution using old SV-2005/9 syntax doesn’t fully meet your requirement, sorry spoke too fast perhaps. However when you say:
Since the order has to be random,
constraint data_values { foreach(data[i]) data[i+1] > data [i] ;}
is not something I want.
Why not use shuffle in post_randomize? Point is - you are asking the solver to go and do all the hardwork by writing some fancy expression to simply do:
Fill all values from 0…15
Shuffle the order
IMHO - this is bad for performance and an unnecessary overkill of available horse power. Sure Vishu’s code looks nice, but hopefully you agree it is not so intuitive. We generally recommend KISS principle (KISS principle - Wikipedia). Consider the below code - this would achieve what you intended in a simpler manner - relatively speaking. But - you do have a fancy code choice, thanks to Vishu!
constraint cst_ascend { foreach(data[i])
i > 0 ->
(data[i] > data [i-1]) ;}
function void post_randomize;
this.data.shuffle;
$display("%p",this);
endfunction : post_randomize
The above code scales nicely for 1 - to - SIZE-1 array size. Since it uses one foreach loop in constraints, tools will have it easy and is easier to read/maintain/explain to others.
Reading the above link for I tried to generate unique random numbers, but the following code doesn’t seem to work, dont know why?
package env_constants;
localparam PDW_INDEX_BITS = 6;
localparam DCD_INDEX_BITS = 3;
endpackage: env_constants
module dcd2pdw;
import env_constants::*;
class A;
rand bit[PDW_INDEX_BITS-1:0] dcd2pdw[bit[DCD_INDEX_BITS-1:0]];
constraint uniq {unique {dcd2pdw};}
endclass:A
initial begin
A a;
a = new();
assert(a.randomize()) else begin
$display("Randomization failure...");
end
$display("size of array: %d",a.dcd2pdw.size());
foreach(a.dcd2pdw[i]) begin
$display("PDW: %0d, at dcd_id: %0d",a.dcd2pdw[i],i);
end
end
endmodule: dcd2pdw
Size of array is getting printed as: zero. What is the problem here ?
You have declared dcd2pdw as an associative array whose index type is bit[DCD_INDEX_BITS-1:0]. You cannot randomize the size of an associative array. Did you mean to declare a fixed sized array?
Ok I have declared it as an associative array.
I actually need 2**DCD_INDEX_BITS as my array depth. But if I say [DCD_INDEX_BITS-1:0], the array depth would be 2 items only.
Hi Dave, i have a new question that is related but it is a little different. I haven’t found any solution in the forum so maybe you can help.
What i want is to generate an array with unique ranges. This is typically useful for addressing ranges that are not overlapping. What is the best and efficient way to do that?
In a paper of the DVCon from Mentor i have seen: http://proceedings.dvcon-europe.org/2014/presentations_and_papers/T5_1_paper.pdf
There is the following code:
rand bit [31:0] start_addr;
rand bit [5:0] length;
bit [31:0] start_end_addr_hash [bit[31:0]];
constraint c { //Generate Non-Overlapping address ranges
if (start_end_addr_hash.num()) {
foreach (start_end_addr_hash *) {
if (i >= length ) {
!(start_addr inside {[i-length+1 : start_end_addr_hash [i]]});
} else {
!(start_addr inside {[0 : start_end_addr_hash [i]]});
}
}
}
length == 6'h10;
}
...
start_end_addr_hash [start_addr] = start_addr + length - 1;
Is that the best way? Besides, it is not clear for me where i have to put the code.
Another option is using only random variables. The advantage is that the user can define the start and length by himself for each non overlapping part ([i]notice that it uses a dynamic array and not only one value for start_address and lengt*h). I think the previous example doesn’t allow you to do that because is all coded in the hash iteratively.
Besides, if you do constraint “length[j]==1” then this code generates a vector with unique values in the start_address. That means, this somehow supersedes the codes above at cost of efficiency and memory.
rand bit [31:0] start_address[]; //notice that it is a dynamic array
rand bit [5:0] length[]; //dynamic array, to specify many different ranges
constraint c {
start_address.size()==length.size();
if (start_address.size()) {
foreach (start_address[j]) {//j is the chosen start address
// end_address [j] == start_address[j] + length[j] - 1;
foreach (start_address[i]) {//i is the chosen start address
if (i!=j) {
///Constraint start_address (first byte of the buffer)
if (start_address [j] >= length[i] ) {
!(start_address[i] inside {[start_address [j] - length[i]+1 : start_address [j]+length[j]-1 ]}) ;
} else {
!(start_address[i] inside {[0 : start_address [j]+length[j]-1 ]});
}
} //end if i!=j
} //end foreach i
} //end foreach j
} //end if
} //end contraint c
In the paper’s example, the constraint about the length (i.e. length==6’h10) is just for demonstration, you can remove that to get the variable length you want. In reality length limitation constraints do exist. And yes you can add the following line in any procedural block after a successful randomization attempt (post_randomize is a good place).
Back to the debate about performance, the introduced example (second one) shall generate all address ranges upfront. Although I am fan of late-randomization (randomize at the right time to take the DUT and testbench states into consideration), this is not just the problem I see with the second code. The second code has a nested foreach loops, this is a solver performance overkill, especially if size of the arrays are large enough. An interesting exercise is to measure the time the solver will solve the second example with array sizes of 100 and above.
The first code (code in the paper) kind of escape from this problem by only randomizing a single attempt at a time, then saving the start and end addresses in a hash table acting as a back-log/guiding reference for the next randomization attempt. Hence only one foreach is used in constraint, which still possess a performance bottleneck, however much faster than the second solution.
That said, if I were to write the code I would neither use the first nor the second solutions to solve the simple problem of non-overlapping address ranges. I would just remove the constraint completely and embrace the constraint with a checking loop like so.
class c;
rand bit [31:0] start_addr;
rand bit [5:0] length;
bit [31:0] start_end_addr_hash [bit[31:0]];
endclass
...
c c1 = new;
...
forever begin
duplicate = 0;
assert(c1.randomize());
foreach (c1.start_end_addr_hash [i])
if ((c1.start_addr <= c1.start_end_addr_hash [i]) && ((c1.start_addr + c1.length -1) >=i )) begin
duplicate = 1;
break;
end
if (!duplicate) break;
end
c1.start_end_addr_hash [c1.start_addr] = c1.start_addr + c1.length - 1;
The code above shall free the solver from complicated constraints allowing it to focus on the distribution. If we assumed an ideal solver distribution (e.g. BDD solver will be efficient to solve the simplified problem above), the third solution will be much faster than both solutions above (especially the second one).
Same stands true for the original question of this post; how to generate an array of random unique elements. Well you can also use procedural code and minimal solver work if possible, like so:
class dummy_c;
randc bit [31:0] val;
endclass
class trans;
rand bit [31:0] addrs [];
function void post_randomize();
dummy_c dc = new;
foreach (addrs[i]) begin
assert (dc.randomize);
addrs[i] = dc.val;
end
endfunction
endclass
Thanks for your detailed answer. I agree with you. For long vectors your last solution has much better performance. Thanks for the insight in the constraint solver performance.
I have also noticed that if we would have been talking about a transaction item class, then the user can also create random variables and a flag (e.g. fullrandom_or_definedbyuser) so that he can assign their user-values in the post randomization phase. In that way, the user can decide if he want to find a new solution (using your last code) or test a previous solution defined (hardcoded) in the explicit part of the item randomization.
For example:
class c;
rand bit [31:0] start_addr[];
rand bit [5:0] length[];//size of
rand bit user_define_all_rand_members=0;
bit [31:0] start_end_addr_hash [bit[31:0]];
constraint c {
length.size()==start_addr.size();
soft user_define_all_rand_members==0;
} //end contraint c
endclass
...
c c1 = new;
...
repeat(number_of_blocks2generate) begin
forever begin
duplicate = 0;
assert(c1.randomize( ) with {
start_addr.size()==2;
user_define_all_rand_members==1;
start_addr = { 32'hFFFF, 32'h0000 };
} );//try to add a block of 2 ranges, you can define more
//Start check of the random value
foreach (c1.start_end_addr_hash [i]) begin
foreach (c1.start_addr [j]) begin
if ((c1.start_addr[j] <= c1.start_end_addr_hash [i]) && ((c1.start_addr[j] + c1.length[j] -1) >=i )) begin
duplicate = 1;
if (user_define_all_rand_members==1) begin
$fatal (0, "Randomization defined by user failed")
end
break;
end
//ADD A NEW RANGE-ELEMENT START->END ADDRESS TO THE ASSOCIATIVE ARRAY
if (!duplicate) begin
c1.start_end_addr_hash [c1.start_addr[j]] = c1.start_addr[j] + c1.length[j] - 1;
end
end
if (!duplicate) break;
end//end for associative array
end//end forever begin
end//end repeat number of blocks to add to the associative array
With this version, the user can still defines/direct the randomization with several ranges in one shot (using the with clause). These “with” constraints are checked by the forever loop without using the constraint solver, improving the performance.
In reply to mseyunni:
You have declared dcd2pdw as an associative array whose index type is bit[DCD_INDEX_BITS-1:0]. You cannot randomize the size of an associative array. Did you mean to declare a fixed sized array?
Hi Dave, does this mean that an associative array cannot be randomized? If it can be, can we have constraints on elements of the associative array? What happens when a rand object containing an associative array is randomized?
You can randomize the existing elements of an associative array, just like any other array using the foreach constraint. You cannot add or delete elements, or change the index values of an associative array.