2013-10-14 34 views
2

我需要隨機化一個大內存。所有數據都包含在鎖存模塊內 - 每位1個。如何隨機化一個非常大的內存的內容?

如何解決以下問題?在EDA遊樂場

// 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 

代碼:​​

回答

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

3

不能動態指數實例的數組。有兩種方法可以解決這個問題:

  1. 您是否真的需要在如此低的級別建模內存?將內存模型更改爲RTL描述。無論如何,它的性能會更好。
  2. 使用generate塊與for -loop圍繞initial塊,而不是for -loop的initial塊內。如果您需要在時間0以外的某個時間執行此操作,則可以使用event來觸發它。
genvar index; 
event injectRandomData; 
for (index=0; index < `WIDTH; index++) begin 
    always @injectRandomData 
     memory.latch[index].Q = $urandom; 
end