Wednesday, April 21, 2010

SystemVerilog Interview Questions 6

1)  What is the difference between $random and $urandom?

 Answer:

    The functionality of the seed arguments are different for $random and $urandom. The seed argument to $random is an inout. It updates its seed argument after each call to $random. This means the internal random number generator (RNG) state variable is a 32-bit number.

The seed argument to $urandom is an input. This seed is used to set the internal RNG to a value that is over 32-bits (typically 96-bits or greater).

In SystemVerilog, each thread has its own RNG, so only use the the seed argument on the first call to $urandom in each thread. There is also a way to set the seed without generated a random value by using the built-in process class and using the srandom() method.


class packet;
rand bit [7:0] header;

function new(int seed);
this.srandom(seed);
endfunction
endclass

initial begin
packet p=new;
p.new(33);
end


2) How do we get seed or use single seed in the VMM ?

  Method 1: Let it random by itself
   In VMM , VCS usually prints it to log file.  Recently it added a system task to get the seed, something like: $get_initial_random_seed()
  
   Use the following command to put seed back to get the same result "+nbt_random_seed=".

   Method: Fix the seed.
   The better approach is to use srandom task/function to fix the seed. We can increase the seed by 1 or use other script to generate the seed. The start seed is only start the regression. It has enough randomize with the test environment. 
   
EXAMPLE:
class Rand_seed;
 rand integer Var;
 function new (int seed);
   srandom(seed);
   $display(" SEED is initised to %0d ",seed);
 endfunction

 function void post_randomize();
   $display(": %0d :",Var);
 endfunction
endclass

program Rand_seed_p_80;
  Rand_seed rs;
  initial
  begin
    rs = new(20);
    repeat(5)
      void'(rs.randomize());
    rs = new(1);
    repeat(5)
      void'(rs.randomize());
    rs = new(20);
    repeat(5)
      void'(rs.randomize());
  end
endprogram

No comments:

Post a Comment

Search This Blog