英文:
SV randomization - generating non-overlapping memory regions using constraints
问题
以下是您要翻译的部分:
这个约束 if(i!=j) !((regions[i].addr_s >= regions[j].addr_s) && (regions[i].addr_s <= regions[j].addr_e));
看起来是有效的,但我想确定一下它是否会出现问题。
英文:
I want to generate random regions in memory such that they do not overlap. Below code is generating such regions but they are in order (i.e., region-0 is before region-1, region-1 is before region-2).
But I want to have the regions in random order. I can use shuffle() in post_randomize, but I want to know if we can do this using just constraints ?
class test;
typedef struct{
rand int unsigned indx;
rand bit [31:0] addr_s;
rand bit [31:0] addr_e;
rand bit [31:0] sz;
} regions_t;
rand regions_t regions[];
constraint c_regions{
regions.size() == 3;
foreach(regions[i]) {
regions[i].indx == i;
regions[i].sz inside {10, 20}; // size of each region
regions[i].addr_e == regions[i].addr_s + regions[i].sz - 1;
regions[i].addr_s < regions[i].addr_e;
regions[i].addr_e < 60; // uppler limit of all memory regions
foreach(regions[j]) {
//if(i!=j) !((regions[i].addr_s >= regions[j].addr_s) && (regions[i].addr_s <= regions[j].addr_e));
if(i<j) (regions[i].addr_e < regions[j].addr_s);
}
}
}
function void post_randomize();
//regions.shuffle();
$display($sformatf("post_randomize(): regions = %0p", regions));
endfunction : post_randomize
endclass: test
program prog;
test tst;
initial begin
tst = new();
repeat(10) begin
tst.randomize();
end
end
endprogram: prog
This constraint if(i!=j) !((regions[i].addr_s >= regions[j].addr_s) && (regions[i].addr_s <= regions[j].addr_e));
seems to be working, but wanted to sure if it will have issues.
答案1
得分: 0
将您最后的内部 foreach
循环更改为以下内容。您已经非常接近了。
typedef bit [7:0] addr_t;
class test;
typedef struct{
int unsigned indx;
addr_t addr_s;
addr_t addr_e;
int sz;
} regions_t;
rand regions_t regions[];
constraint c_regions{
regions.size() == 3;
foreach(regions[i]) {
regions[i].indx == i;
regions[i].sz inside {10, 20}; // 每个区域的大小
regions[i].addr_e == regions[i].addr_s + regions[i].sz - 1;
regions[i].addr_s < regions[i].addr_e;
regions[i].addr_e < 80; // 所有内存区域的上限
foreach(regions[j]) // 起始地址不能在另一个范围内
i!=j -> !(regions[i].addr_s inside {[regions[j].addr_s:regions[j].addr_e]});
}
}
function void post_randomize();
$display($sformatf("post_randomize(): regions = %p", regions));
endfunction : post_randomize
endclass: test
module prog;
test tst;
initial begin
tst = new();
repeat(10) begin
assert(tst.randomize());
end
end
endmodule: prog
英文:
Change your last inner foreach
loop to what I show below. You were very close/
typedef bit [7:0] addr_t;
class test;
typedef struct{
int unsigned indx;
addr_t addr_s;
addr_t addr_e;
int sz;
} regions_t;
rand regions_t regions[];
constraint c_regions{
regions.size() == 3;
foreach(regions[i]) {
regions[i].indx == i;
regions[i].sz inside {10, 20}; // size of each region
regions[i].addr_e == regions[i].addr_s + regions[i].sz - 1;
regions[i].addr_s < regions[i].addr_e;
regions[i].addr_e < 80; // upper limit of all memory regions
foreach(regions[j]) // start address cannot be inside another range
i!=j -> !(regions[i].addr_s inside {[regions[j].addr_s:regions[j].addr_e]});
}
}
function void post_randomize();
$display($sformatf("post_randomize(): regions = %p", regions));
endfunction : post_randomize
endclass: test
module prog;
test tst;
initial begin
tst = new();
repeat(10) begin
assert(tst.randomize());
end
end
endmodule: prog
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论