Random Generation of Address To access around the 4KB Memory Boundary inside a 4GB Memory !?!

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

In reply to desperadorocks:
Try this

class addr_rand;
   rand bit [31:0] addr;
   int delta = 5;
   int density = 1000;
   constraint c{addr % density inside {[0:delta],[density-delta:density]};}
endclass

In reply to dave_59:

Thanks Dave !! Appreciate that !! :-)

In reply to dave_59:

Trying to understand the code below, What is delta ? and why is it 5? i didnt understand why density is 1000. Please help me understand this


class addr_rand;
   rand bit [31:0] addr;
   int delta = 5;
   int density = 1000;
   constraint c{addr % density inside {[0:delta],[density-delta:density]};}
endclass

In reply to n347:

Same question. Could you please explain delta and density?

In reply to desperadorocks:

Hi

Could you please help me with the idea behind adding and subtracting 5?

Thanks
Jyoti

Hi ! Delta is basically a range of address you wanted to compute before and after the 4KB address boundary.