Hello Folks,
Have a scenario where I need to generate random address to access memories around the 4KB memory boundary inside a 4GB memory.
I tried the below code and just wanted to know if this is a right way or is there any better solution ?
module memory_range_rand;
class addr_rand;
rand bit [31:0] addr; // Random Address to be Generated
rand bit [31:0] memory_density; // To Hold Number of 4Kb memories inside 4GB
bit [31:0] addr_temp;
constraint memory_density_c {
memory_density inside {[0:1000000]}; // The total number of 4Kb slices inside 4GB memory is 1000000.
// So randomly generating any particular 4Kb slice.
}
function void post_randomize();
addr_temp = memory_density * 1000; // To access one 4K memory location, the address value is 1000 [i.e.
// decimal value. So I try to randomly select which 4K memory slice
// and multiply by 1000 to get the respective address value]
if (addr_temp == 0) addr = $urandom_range(0, (addr_temp+5)); // Then if the slice is 0, I just do
// addr_temp + 5 to access the memory
else addr = $urandom_range((addr_temp-5), (addr_temp+5)); // Then if the slice selected is more than
// 0, then I do generate an address -5 or +5 of
// the temporary address
endfunction: post_randomize
function void display();
$display("Value of memory_density = %0d\n", memory_density);
$display("Value of addr_temp = %0d\n", addr_temp);
$display("Value of addr = %0d\n", addr);
endfunction: display
endclass: addr_rand
initial begin
addr_rand ar;
ar = new();
ar.randomize();
ar.display();
end
endmodule: memory_range_rand
Share in your comments !
Thanks