2

我需要随机化一个大内存。所有数据都包含在锁存器模块内——每位 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 Playground 上的代码:http ://www.edaplayground.com/s/4/235

4

2 回答 2

3

您不能动态索引实例数组。解决此问题的两种方法:

  1. 您真的需要在如此低的级别上对内存进行建模吗?将内存模型更改为 RTL 描述。反正性能会好很多。
  2. 在块周围使用generate带有for-loop 的initial块,而不是在块for内使用 -loop initial。如果您需要在时间 0 以外的某个时间执行此操作,您可以使用 anevent来触发它。
genvar 指数;
事件注入随机数据;
for (index=0; index < `WIDTH; index++) 开始
   总是@injectRandomData
      memory.latch[index].Q = $urandom;
结尾
于 2013-10-14T19:42:40.253 回答
3

Quick and dirty solution:

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

Code on EDA Playground: http://www.edaplayground.com/s/6/236

于 2013-10-14T19:44:24.770 回答