Constraint for memories

A memory is of size 8192 bytes and it is byte addressable. I need to generate 3 different start and end addresses within this memory space. Each of these 3 sub areas can be a min of 1 byte and a max of 8192 bytes. These need to be non overlapping.

I have tried creating a size variable and start addr variable with solve before constraint for size before address. This is not working for me.

Any help in creating a constraint for this is appreciated.

1 Like

In reply to kernalmode1:

class address;
   int N = 3;
   int memLimit = 8192;
   rand bit [15:0] addrs[];
   rand bit [15:0] sizes[]
   function new;
      addrs = new[N];
      sizes = new[N]
   endfunction
 
   constraint c{
		foreach(addrs[Range]) {if (Range != N-1)
		   addrs[Range] + sizes[Range] < addrs[Range+1];
		else
		   addrs[Range] + sizes[Range] < memLimit;
               sizes[Range] inside {[1:memLimit-1]};
		}
 
endclass

In reply to dave_59:

Thanks Dave.

class address;
   int N = 3;
   int memLimit = 8192;
   rand bit [15:0] addrs[];
   rand bit [15:0] sizes[];
   function new();
     addrs = new[N];
     sizes = new[N];
   endfunction
 
   constraint c {
		foreach(addrs[Range]) {
                  if (Range != N-1)
		    addrs[Range] + sizes[Range] < addrs[Range+1];
		  else
		    addrs[Range] + sizes[Range] < memLimit;
                  sizes[Range] inside {[1:memLimit-1]};
		}
   }
endclass

module top;
  initial begin
  address fc;
  fc = new();
    repeat(10) begin
      fc.randomize();
      $display("%p %p", fc.addrs, fc.sizes);
    end
  end
endmodule

In reply to dave_59:

In reply to kernalmode1:

class address;
int N = 3;
int memLimit = 8192;
rand bit [15:0] addrs[];
rand bit [15:0] sizes[]
function new;
addrs = new[N];
sizes = new[N]
endfunction
constraint c{
foreach(addrs[Range]) {if (Range != N-1)
addrs[Range] + sizes[Range] < addrs[Range+1];
else
addrs[Range] + sizes[Range] < memLimit;
sizes[Range] inside {[1:memLimit-1]};
}
endclass

Hi Dave,
What is “Range” variable here?

In reply to seenu:

It is the foreach loop iterator variable.

I am getting these as my results
'{'hfe6a, 'hf38c, 'h1092} '{'h523, 'he62, 'h808}

doesn’t this exceed 8192 bytes - 'hfe6a + 'h523 = 66445(in binary greater than 8192?) I think my understanding is wrong please help

Good catch. Remove the else in the constraint, or simply write

  constraint c {
		foreach(addrs[Range]) {
          if (Range != N-1)
		    addrs[Range] + sizes[Range] < addrs[Range+1];

          addrs[Range] + sizes[Range] < memLimit;
        }
   }
2 Likes
program p1;
  class c1;
    parameter int N=3;
    int memlimit = 8192;
    rand bit [15:0] addr[];
    rand bit [15:0] size[];
    function new;
      addr = new[N];
      size = new[N];
    endfunction
   constraint c1{
     foreach (addr[range]){
       if(range!=N-1)
         addr[range]+size[range]<addr[range+1];
       addr[range]+size[range]<memlimit;
     }
       }
    function void post_randomize();
     $display("memory 8192 details %0p", obj);
    endfunction
     
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (1)
    obj.randomize();
  end
endprogram 

output of the above code is

memory 8192 details 
'{memlimit:8192, addr:'{'h100b, 'h1014, 'h1d75}, size:'{'h2, 'h8a6, 'h177}}

Total memory size is 8192 (8KB).
But when I add sum of all sizes, its not 8KB.
Do I need to add one more constraint like this?

size.sum()==8192

I need to understand how above code is forming 8KB, could someone please point?

Thank you,

I think so that the questions asks to generate 3 different space under 8129 bytes boundary , it doesn’t mean that you have to have different space such that it sums to the memory space.
@atalur you can give some clarity on this!