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.
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!
class mem_blocks;
int num = 4;
parameter int mem_boundary = 4096;
rand bit [12:0] sizes;
// 13-bit to hold up to 4096
rand bit [12:0] memory;
function new();
memory = new[num];
sizes = new[num];
endfunction
constraint memory_c {
sizes.size() == num;
memory.size() == num;
// First block starts at 0
memory[0] == 0;
foreach (memory[i]) {
// Each size is 1024 (swap with inside{[1:...]} for random)
sizes[i] inside {[1:mem_boundary-1]};
// Contiguous: next block starts exactly where this one ends
if (i < num - 1)
memory[i+1] == memory[i] + sizes[i];
}
// Last block ends exactly at boundary
memory[num-1] + sizes[num-1] == mem_boundary;
}
function void print();
foreach (memory[i])
$display("addr[%0d] = %0d,\tsize = %0d -> ends at %0d",
i, memory[i], sizes[i], memory[i] + sizes[i] - 1);
$display("Total size = %0d bytes", sizes.sum());
endfunction
function void post_randomize();
$display("--- Randomization Successful ---\n");
print();
endfunction
endclass
module testbench;
initial begin
mem_blocks blk = new();
blk.randomize();
end
endmodule
-– Randomization Successful —
addr[0] = 0, size = 660 → ends at 659
addr[1] = 660, size = 1131 → ends at 1790
addr[2] = 1791, size = 847 → ends at 2637
addr[3] = 2638, size = 1458 → ends at 4095
Total size = 4096 bytes