what is the difference of creating object inside initial block or outside block

Status
Not open for further replies.

ajaz

Newbie level 2
Joined
Apr 16, 2020
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
19

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
module top;
  cons h; 
  
  initial
    repeat(2)
      begin
        h = new();
        h.randomize();
        $display("values of x:%0d",h.x);      
       end
endmodule




Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
module top;
  cons h = new(); 
  
  initial
    repeat(2)
      begin
         h.randomize();
        $display("values of x:%0d",h.x);
      end
endmodule

 
Last edited by a moderator:

objects created inside a block exist in that block and not elsewhere - looks like a local variable

by objects created "outside" a block, i assume you mean in main or equivalent
these look like global variables - they are available everywhere

review the "scope of variables" section of your language manual
 

That is not correct. In both cases, a class object gets constructed and gets assigned to the static variable h, which is globally available through hierarchical references.

You won't see any functional differences with these small examples The main difference you need to understand is how the objects get their initial random seed and how code changes affect random stability (the measure of how much code can be modified and still get have the exact same sequence of random values generated).

The first example is constructing a new object for each iteration and getting the seed from the initial block thread. If you add construction of other objects to additional class variables in the loop, you disturb random stability of the h.randomize() method.

In the second example, there is only one object that gets seeded outside the initial block. Each time you call randomize(), the object gets a new seed. Constructing other objects to other varibles has no affect on the stability of h. But is you add additional static variables with initializations outside the initial block. you may disturb the seeding.

The is no "right way" of doing this. You just need to be aware of the coding choices and how they affect random stability.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…