Anjali_287
Anjali_287

Reputation: 133

I don't understand this SV randomization randc behaviour

This is code for transaction class,

class transaction;

  //declaring the transaction items
  randc bit [3:0] a;
  randc bit [3:0] b;
       bit [6:0] c;
  function void display(string name);
    $display("-------------------------");
    $display("- %s ",name);
    $display("-------------------------");
    $display("- a = %0d, b = %0d",a,b);
    $display("- c = %0d",c);
    $display("-------------------------");
  endfunction
endclass

And this is code for generator class,

class generator;

  rand transaction trans;
  int  repeat_count;
  mailbox gen2driv;
  event ended;

  function new(mailbox gen2driv);

    this.gen2driv = gen2driv;
  endfunction

  task main();
    repeat(repeat_count) begin
    trans = new();
    if( !trans.randomize() ) $fatal("Gen:: trans randomization failed");
      trans.display("[ Generator ]");
      gen2driv.put(trans);
    end
    -> ended;
  endtask

endclass

The value of repeat_count that I passed is 10, and here's the output:

- a = 2, b = 0
- c = 0

- a = 1, b = 9
- c = 0

- a = 9, b = 9
- c = 0

- a = 7, b = 15
- c = 0

- a = 10, b = 15
- c = 0

- a = 3, b = 1
- c = 0

- a = 13, b = 12
- c = 0

- a = 1, b = 9
- c = 0

- a = 7, b = 5
- c = 0

- a = 3, b = 15
- c = 0

But, values during randomization are not showing cyclic repetition. It is repeating itself before all possible value can occur for variables a and b.

Upvotes: 1

Views: 556

Answers (2)

sai prasad
sai prasad

Reputation: 17

Yeah exactly to what @toolic said...initializing with new operator inside the repeat loop would create a new space every time the loop runs and thus the newly created object (transaction in your case) has no trace of the values that has been previously exercised. Thus giving out a random number making randc of no use. It though works normal with ' rand' keyword.

Upvotes: 0

toolic
toolic

Reputation: 62164

Move the transaction constructor out of the repeat loop. Change:

repeat(repeat_count) begin
trans = new();

to:

trans = new();
repeat(repeat_count) begin

It seems new resets the initial random permutation of the range values each time it is called.


Here is a self-contained, runnable example which demonstrates the issue (and the fix):

class transaction;
    randc bit [3:0] a; // 16 values: 0-15
    function void display;
        $display("a=%0d", a);
    endfunction
endclass

class generator;
    rand transaction trans;
    task main;
        //trans = new(); // Un-comment this line for fix
        repeat (16) begin
            trans = new(); // Comment out this line for fix
            if (!trans.randomize()) $fatal(1, "trans randomization failed");
            trans.display();
        end
  endtask
endclass

module tb;
    generator gen = new();
    initial gen.main();
endmodule

Upvotes: 1

Related Questions