Saturday, July 15, 2017

conc_cast_up_good_down_bad_parallel_not_possible

Case 1:

class B;
  virtual task print();
  $display(" CLASS B ");
  endtask
  virtual task print_master();
  $display(" MASTER ");
  endtask
endclass

class E_1 extends B;
  virtual task print();
  super.print();
  $display(" CLASS E_1 ");
  endtask
endclass

class E_2 extends B;
  virtual task print();
  $display(" CLASS E_2 ");
  endtask
endclass

program main;
  bit g1=0,g2=0,g3=0,g4=0;
  initial
  begin
    B b;
    E_1 e1;
    E_2 e2;
    e1 = new();
    g1 = $cast(b,e1);            //##DEBUG: MASTER: useful when b is not newed
    g2 = $cast(e1,b);            //##down casting is not allowed and not useful
    //##g3 = $cast(e1,e2);    //##DEBUG: MASTER: gives an error
    g4 = $cast(e2,e1);
    $display("DEBUG: g1=%0b and g2=%0b",g1,g2);
    $display("DEBUG: g3=%0b and g4=%0b",g3,g4);
    b.print();                        //DEBUG: MASTER: thus we can call any method which exists in class b
    b.print_master();           //DEBUG: MASTER: thus we can call any method which exists in class b
    e1.print();
    //##e2.print();                //DEBUG: MASTER: will give null error as its object is not created and also casting is not possible at parallel level
  end
endprogram

//##run results:
/*
ncsim> run
DEBUG: g1=1 and g2=1
DEBUG: g3=0 and g4=0
 CLASS B 
 CLASS E_1    --> this print is due to virtual effect tasks, basically parent class task get override with child class task 
 MASTER 
 CLASS B 
 CLASS E_1 
Simulation complete via implicit call to $finish(1) at time 0 FS + 1
*/

Case 2:

class B;
  task print();
  $display(" CLASS B ");
  endtask
  task print_master();
  $display(" MASTER ");
  endtask
endclass

class E_1 extends B;
  task print();
  super.print();
  $display(" CLASS E_1 ");
  endtask
endclass

class E_2 extends B;
  task print();
  $display(" CLASS E_2 ");
  endtask
endclass

program main;
  bit g1=0,g2=0,g3=0,g4=0;
  initial
  begin
    B b;
    E_1 e1;
    E_2 e2;
    e1 = new();
    g1 = $cast(b,e1);          //##DEBUG: MASTER: useful when b is not newed
    g2 = $cast(e1,b);          //##down casting is not allowed and not useful
    //##g3 = $cast(e1,e2);  //##DEBUG: MASTER: gives an error
    g4 = $cast(e2,e1);
    $display("DEBUG: g1=%0b and g2=%0b",g1,g2);
    $display("DEBUG: g3=%0b and g4=%0b",g3,g4);
    b.print(); //DEBUG: MASTER: thus we can call any method which exists in class b
    b.print_master();          //DEBUG: MASTER: thus we can call any method which exists in class b
    e1.print();
    //##e2.print();               //DEBUG: MASTER: will give null error as its object is not created and also casting is not possible at parallel level
  end
endprogram

//##run results:
/*
ncsim> run
DEBUG: g1=1 and g2=1
DEBUG: g3=0 and g4=0
 CLASS B 
 MASTER 
 CLASS B 
 CLASS E_1 
Simulation complete via implicit call to $finish(1) at time 0 FS + 1
*/

//##THUMB RULE -->  PC and C has to be new()ed, virtual will have different effect, super will have as usual effect, P=parent, C=child

No comments:

Post a Comment