# Constraining address for the memory making sure the Offset is not within the previous memory regions

**URL:** <https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486>\
**Category:** UVM\
**Tags:** SV-Constraint-Solver, UVM, SV-constraint, constrained-random-generation\
**Created:** [September 13, 2017, 1:40am UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486 "2017-09-13T01:40:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![piyer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@piyer](https://verificationacademy.com/forums/u/piyer)\
**Post date:** [September 13, 2017, 1:40am UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/1 "2017-09-13T01:40:22Z")

</div>

I want to generate the address for multiple iterations and i want to make sure i don’t generate the starting address within the previous memory regions.

Example : lets say I want to write ten words, we have memory from 32’h0 to 32’h1400. I want to generate address randomly with random size. For the sake of example lets say we randomly choose address 32’h 10 and size 10 words as well in the first iteration. When we are randomizing second time we don’t want to generate the address with in 32’h10 and 32’h10 + 10 \*4. I have used cyclic randomization but it still generates the address with the memory region.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 13, 2017, 2:39am UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/2 "2017-09-13T02:39:22Z")

</div>

_In reply to [prashanthiyer90](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486):_  
You will need to build a database of used addresses and make sure that each address in not withing the new range you want add.

```verilog
module top;
  class A;
    rand int unsigned start,size;
    int used[$];
    constraint c{0<size;size<5;start<20;
       foreach(used[addr])!(used[addr] inside {[start:start+size-1]});}
    function void post_randomize();
       for(int addr = start; addr<(start+size); addr++)
         used.push_back(addr);
       used.sort(); // makes display easier to read
    endfunction
  endclass
  A h = new;
  initial repeat(15) begin
    if (!h.randomize) $error("no more space to write");
    $display("%p",h);
  end
endmodule : top

```

---

<div class="post-metadata">

**Author:** ![piyer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@piyer](https://verificationacademy.com/forums/u/piyer)\
**Post date:** [September 14, 2017, 5:27am UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/3 "2017-09-14T05:27:54Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/2):_

Hi Dave,

Thanks a lot for the reply. It did help me to extend from your constraints code. since my address is all word aligned i made sure i save only the address which is word boundary in the data structure.

Regards,  
Prash

---

<div class="post-metadata">

**Author:** ![yasaswi93](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/919ad9/32.png) [@yasaswi93](https://verificationacademy.com/forums/u/yasaswi93)\
**Post date:** [April 1, 2019, 8:32pm UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/4 "2019-04-01T20:32:16Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/3):_

Does this have a performance impact as every time we are trying to randomize the object the foreach has to run for the whole address space that is being allocated.

I am seeing a slow down in simulation during the randomize() call. Trying to figure out if its due to the foreach in the constraint.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 1, 2019, 9:05pm UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/5 "2019-04-01T21:05:35Z")

</div>

_In reply to [yasaswi93](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/4):_

Yes, performance degrades as you start to use up addresses.

Another approach to this is generating randomly sized blocks and allocating them sequentially at beginning of your test. Then picking one block randomly during the test.

---

<div class="post-metadata">

**Author:** ![yasaswi93](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/919ad9/32.png) [@yasaswi93](https://verificationacademy.com/forums/u/yasaswi93)\
**Post date:** [April 1, 2019, 11:11pm UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/6 "2019-04-01T23:11:34Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/5):_

Thanks for the quick response Dave. While I was thinking about the alternative approach you have mentioned I had a thought:

Since UVM is a powerful methodology is there a class that can actually do the random memory generation for us? I think this a pretty common use case where people need to allocate random non overlapping memory spaces.

Let’s say my use case is the following:

1. I have 2GB of system memory
2. I need to allocate random non overlapping space from this memory that will contain data that gets transmitted or can be used to store data that is received by a DMA agent from a packet based protocol (In my case Ethernet).
3. I also need random non-overlapping chunks of memory space to store my DMA descriptors.

So, if there is such a class in which I can set start address and end address of the memory (0-2GB) and if it has methods something like:

1. get\_buffer(buf\_start\_addr,buf\_size) // Both args are output
2. get\_buffer\_of\_spec\_size(buf\_start, req\_buf\_size) // Buf\_start as output and req\_buf\_size as input

Are you aware of any such class in UVM. I want to reuse such class if it already exists.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 1, 2019, 11:52pm UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/7 "2019-04-01T23:52:07Z")

</div>

_In reply to [yasaswi93](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/6):_  
[https://verificationacademy.com/forums/uvm/uvm-memory-manager-uvmmemmam-still-around](https://verificationacademy.com/forums/uvm/uvm-memory-manager-uvmmemmam-still-around)

---

<div class="post-metadata">

**Author:** ![yasaswi93](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/919ad9/32.png) [@yasaswi93](https://verificationacademy.com/forums/u/yasaswi93)\
**Post date:** [April 2, 2019, 5:09pm UTC](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/8 "2019-04-02T17:09:59Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraining-address-for-the-memory-making-sure-the-offset-is-not-within-the-previous-memory-regions/33486/7):_

Thanks Dave.
