如何随机化一个非常大的内存的内容?

时间:2013-10-14 19:17:03

标签: random verilog system-verilog

我需要随机化一个大内存。所有数据都包含在锁存模块中 - 每位1个。

如何解决以下问题?

// Quick mock up of the memory, which I can't change
`define WIDTH 64*144

module latch(
  output reg Q);
endmodule

module memory;
  wire [`WIDTH-1:0] latchData;
  latch latch[`WIDTH-1:0] (.Q(latchData[`WIDTH-1:0]));
endmodule

// My testbench, which I can change
module test;

  reg [31:0] index;

  memory memory();

  initial begin
    $display("Initial data: %0d", memory.latchData);
    injectRandomData();
    $display("Randomized data: %0d", memory.latchData);
  end

  task injectRandomData();
    // Using for loop does not work
    //for (index=0; index < `WIDTH; index = index+1) begin
    //  memory.latch[index].Q = $urandom;
    //end

    // Doing it this way seems terrible
    memory.latch[0].Q = $urandom;
    memory.latch[1].Q = $urandom;
    memory.latch[2].Q = $urandom;
    // ... a bunch more to go; don't wait up

  endtask

endmodule

EDA游乐场代码:http://www.edaplayground.com/s/4/235

2 个答案:

答案 0 :(得分:3)

您无法动态索引实例数组。有两种方法可以解决这个问题:

  1. 你真的需要在如此低的水平上建模内存吗?将内存模型更改为RTL描述。无论如何,它会更好地表现。
  2. 使用generatefor - 围绕initial块而不是for块内的initial循环。如果您需要在时间0之外的某个时间执行此操作,则可以使用event来触发它。
  3. genvar index;
    event injectRandomData;
    for (index=0; index < `WIDTH; index++) begin
       always @injectRandomData
          memory.latch[index].Q = $urandom; 
    end

答案 1 :(得分:3)

快速而肮脏的解决方案:

task injectRandomData();
  ->do_InjectRandomData;
  #0; // gen always block a change to finish;
endtask

event do_InjectRandomData;
genvar index;
generate
  for(index = 0; index < `WIDTH; index = index +1) begin : gen_loop
    always @(do_InjectRandomData) begin : set_rand
      memory.latch[index].Q = $urandom;
    end
  end
endgenerate

EDA游乐场代码:http://www.edaplayground.com/s/6/236