Sunday, August 2, 2020

How to Randomize a Real Variable?

Hi Everyone !!!,
    In This session we are going to see how to Randomize a Real variable.
  Using randomize() method we can able to randomize only integral data-type variables only(bit,logic,reg,wire,integer,enum, packed struct). 
    Here I show the two ways to randomize variable of type real.

Example 1:
In below mentioned code I am trying to randomize real data types,It leads to compilation error. LRM won't support real and string data_types for Randomization.

class rand_rdata;
  rand real rdata;
endclass
module TB;
  rand_rdata r;
  initial begin
    r=new();
    r.randomize();
    repeat(5) begin
      $display(" REAL DATA %0d ",r.rdata);
    end
  end
endmodule

OUTPUT:
TimeScale is 1 ns / 1 ns

Error-[IRRVD] Illegal rand variable type
testbench.sv, 16
"rdata"
The rand variable 'rdata' must be an integral type, an enum type, a packed
struct, or of type 'bit'.
Change the type of the random variable 'rdata' to an integral, bit, packed
struct, or enum type or remove the 'rand' qualifier.

1 error

$bitstoreal converts the bit value to a real number. By using this system function real values can be generated.  Real value randomization Logic's are explained in Example 2&3.

Example 2:
class rand_real;
  rand bit [63:0] data;
  real rdata;
  function void post_randomize();
    this.rdata = $bitstoreal(data);  // This is the trick here. (Randomized data value is converted to Real value by using system function)
  endfunction
endclass

module top();
  rand_real R;
  initial begin
    R = new();
    repeat (4) begin
      if (R.randomize()) begin
        $display ("R.data=%0d, R.rdata=%e", R.data, R.rdata);
      end
      else begin
      $error ("Randomization Failed");
      end
    end
  end
endmodule

OUTPUT:
R.data=11603960314292000697, R.rdata=-1.560983e-149
R.data=5297240865832474715, R.rdata=1.397281e+46
R.data=17254093185709081402, R.rdata=-7.145857e+228
R.data=12279536430588681223, R.rdata=-2.239116e-104
V C S S i m u l a t i o n R e p o r t

Example 3:
class rand_real;
  rand integer i, j;
  real rdata;
  function void post_randomize();
    this.rdata = $bitstoreal({i, j}); //This is the trick here (Randomized i,j converted to equivalent real here)
  endfunction
endclass

module TB();
  rand_real R;
  initial begin
    R = new(); 
    repeat (5) begin
      if (R.randomize()) begin
        $display ("R.i=%0d, R.j=%0d, R.real_data=%e", R.i, R.j, R.rdata);
      end
      else begin
      $error ("Randomization Failed");
      end
    end
  end
endmodule

OUTPUT:
R.i=-384116807, R.j=-1593209748, R.real_data=-2.006518e+198
R.i=1637914715, R.j=1233360000, R.real_data=1.866503e+162
R.i=397247290, R.j=-277685674, R.real_data=1.263399e-194
R.i=-407577593, R.j=-1435914926, R.real_data=-3.717586e+191
R.i=-1298805792, R.j=-1452694196, R.real_data=-5.174229e-65
V C S S i m u l a t i o n R e p o r t


Hope this logic's will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! ๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM  


"Be a lifelong student. The more you learn, the more you earn and the more self-confidence you will have" - Brain Tracy


2 comments:

`define Macro usage in System Verilog

 Hi Everyone !!! In this blog we are going to see the usage of `define macro in System Verilog. A text macro substitution facility has been ...