SV——override

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_38037810/article/details/102642368

0. Introduction

SV introduced OPP, there will be similar to C ++ in the override and overload consideration.

 

1. override rewrite

Rewriting rewriting data members and methods have to rewrite See the following example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// 来源 IEEE1800 8.14节

class Packet;

    integer i = 1;

    function integer get();

        get = i;

    endfunction

endclass

class LinkedPacket extends Packet;

    integer i = 2;

    function integer get();

        get = -i;

    endfunction

endclass

LinkedPacket lp = new;

Packet p = lp;

j = p.i; // j = 1, not 2 // 父类成员

j = p.get(); // j = 1, not -1 or –2

  

Seen from the above, the parent class data members point to the handle is a member of the parent class.

If you want to call a member function handle subclasses overridden by the parent, then you need to function in the parent class defined as virtual types.

 

2. overloaded overload

In the SV does not seem to support the heavy load (overload), is not directly supported by the same method type, different parameters.

In the following example, SV B to check the function of the A dis dis function returns the different types of error.

  

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

class A;

  virtual function int dis(string str="A");

    $display("this is %s",str);

    dis=1;

  endfunction

endclass

 

class B extends A;

  function void dis(); // 只有函数名相同

    $display("this is B");

  endfunction

endclass

A a;

B b;

initial begin

  b=new;

  b.dis();

  b.dis("B"); //在调用的时候,SV会把B的dis当作A中dis的override来处理,句柄b调用的是B中的dis。

end

// 报错:

Error-[SV-IRT] Incompatible return types

./svt.sv, 108

svt, "dis"

Definition of class function 'A::dis' does not have the same return type as

mentioned in the declaration at: "./svt.sv", 97.

 

Error-[TMAFTC] Too many arguments to function/task call

./svt.sv, 116

"b.dis("B")"

The above function/task call is done with more arguments than needed.

  

3. override conditions

Subclass want to correctly override the parent class virtual function, it is necessary to ensure that the following four points:

  1. Functions can be overloaded functions.

  2. The same function name

  3. Return of the same type

  4. The same parameter list

Subclasses to correctly reload the parent class virtual task, the need to ensure the following three points:

  1. Tasks can be overloaded task.

  2. The same task name.

  3. The same parameter list.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

// 任务被函数重载了,会出错

program automatic test;

    class A;

        virtual task dis(string str="");

          $display("this is A");

        endtask

    endclass

 

    class B extends A;

        function void dis(string str="");

          $display("this is B");

        endfunction

    endclass

    A a;

    B b;

    initial begin

      b=new;

      b.dis();

      a=b;

      a.dis();

      a.dis("A");

    end

endprogram

// 报错:

Error-[SV-ICMO] Illegal class method override

./svt.sv, 100

  Virtual task 'dis' cannot be overridden with a function.

  Virtual method declared at "./svt.sv", 100

  Overriden at "./svt.sv", 111

  

4. new override it may be

Before the interview asked about the new function can override it?

UVM mentioned in the top UG1.1 P62 page "there are limitations on overriding new () in object-oriented language such as System Verilog." This seems to explain the new can be overloaded, but there are some limitations.

   In the fifth chapter "UVMPrimer" in the mentioned, new () function it can be overloaded. such as:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/// 长方形父类

class rectangle ;

    int width;

    int height;

  function new(int width,int height);

    this.width=width;

    this.height=height;

  endfunction

endclass

 

/// 正方形

class square extends rectangle;

  int side;

  function new(int side); // 重载了new函数,只有一个参数

    super.new(side,side);

  endfunction

 

endclass

Guess you like

Origin blog.csdn.net/m0_38037810/article/details/102642368