SV随机化 – 使用约束生成不重叠的内存区域

huangapple go评论57阅读模式
英文:

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 &lt; regions[i].addr_e;
      regions[i].addr_e &lt; 60; // uppler limit of all memory regions
      foreach(regions[j]) {
        //if(i!=j) !((regions[i].addr_s &gt;= regions[j].addr_s) &amp;&amp; (regions[i].addr_s &lt;= regions[j].addr_e));
        if(i&lt;j) (regions[i].addr_e &lt; regions[j].addr_s);
      }
    }
    
  }

  function void post_randomize();
      //regions.shuffle();
      $display($sformatf(&quot;post_randomize(): regions = %0p&quot;, 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 &gt;= regions[j].addr_s) &amp;&amp; (regions[i].addr_s &lt;= 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 &lt; regions[i].addr_e;
      regions[i].addr_e &lt; 80; // upper limit of all memory regions
      foreach(regions[j]) // start address cannot be inside another range
        i!=j -&gt; !(regions[i].addr_s inside {[regions[j].addr_s:regions[j].addr_e]});
    }
  }

  function void post_randomize();
      $display($sformatf(&quot;post_randomize(): regions = %p&quot;, regions));
  endfunction : post_randomize
endclass: test

module prog;
  test tst;
  initial begin
    tst = new();
    repeat(10) begin
      assert(tst.randomize());
    end

  end
endmodule: prog

huangapple
  • 本文由 发表于 2023年2月10日 05:32:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定