Friday, February 5, 2016

System Verilog : Misc

Here I am Putting some small statement which I learn in day to day work.

1. If you don't write super.new inside new function of current class, the compiler itself add super.new() automatically.

2.  When a child class object is created by new constructor, its parent class (if its abstract then also) is created implicitly. Without parent child can never be exists.

3. You can inherit any method from base class. I mean you can call any method define in base class by child class handle (if it is not defined in child class. That is inheritance concept). But if in base class you have created new function (i.e constructor) with argument, you cant inherit that in child class. You need to define new again in child class with argument.

class animal;
   int age=-1;
  function new(int a = 20);
      age = a;
   endfunction : new
endclass : animal
class lion extends animal;
   //function new(int age);
      //super.new(age);
   //endfunction : new
endclass : lion
module test();
  animal a;
  lion l;
  initial
    l = new(10); // l = new() will work
endmodule

Output : 
Error-[TMAFTC] Too many arguments to function/task call
testbench.sv, 18
"lion::new(10)"
  The above function/task call is done with more arguments than needed.

Run Here :

Below will also work as new is defined in derived class but argument is not passed in super.new, since it has default value. If base class new did not had default value it would shout an error 



Output:
@@@@ age is 20
Below will give error as default value is not assigned in new.

Output:
Error-[TFAFTC] Too few arguments to function/task call testbench.sv, 15 "animal::new()" The above function/task call is not done with sufficient arguments.

3. A class variable can be declared as a rand type. Like rand class_abc abc; 
Thus when class in which class_abc is created, will call .randomize function, it will also randomize rand variable declared in class_abc.
See below example.
class base;
  rand int vari =1 ;
endclass
class obj;
  rand base baseh = new();
endclass
module test;
  obj objh = new() ;
  initial
  begin
    if(objh.randomize())
    begin
      $display(" Randomization is done ");
    end
    $display(" objh.baseh.var : %d ", objh.baseh.vari );
  end
endmodule

Output : 
 Randomization is done 
 objh.baseh.var :  -902462825 

Run Here


4. Constraint are by default virtual in nature. If any constraint with the same name is written in derived class, it will override the constraint written in base class. So If we assign derived class handle to base class, randomize method will solve constraint written in derived class.

class A;
rand integer x;
  constraint c { x > 0; }
endclass
class B extends A;
  constraint c { x < 0; }
endclass
module top();
  A a;
  B b;
  initial
    begin
      b = new();
      a=b;
      a.randomize();
      $display("@@@ %0d",a.x);
    end
endmodule

Output : 
@@@ -1223315946

Run Here
4. Mailbox and semaphore are classes defined in std package

5. intial and always block executes at time 0. If there are more than one block in a module all are executed concurrently and independently.   
6. Class can have an object of its own type.
See below code. 

class test;
  test self;
  function create_me();
    self = new();
    self.display("self");
  endfunction
  function display(string s = "this");
    $display("from function display %s",s);
  endfunction
endclass
 
module test_module();
  test test_ob;
  initial
    begin
      test_ob = new();
      test_ob.create_me();
      test_ob.display();
    end 
endmodule
Output : 
from function display self
from function display this
it has a class variable self that will eventually hold a handle that can reference another class object of the same type. This is a very common data structures like a queue or tree used in many other programming languages. Seehttp://www.cprogramming.com/tutorial/lesson18.html

3 comments:

  1. Hi Jinam Shah,
    Thank you for updating things which you have encountered during day 2 day work. it will help freshers like us to learn more. Please keep on updating.

    ReplyDelete
  2. Thanks man. Actually I just have written roughly for my reference only. But I will keep posting and update it in proper manner for others to understand better. You can follow my blog for more articles.

    ReplyDelete