SystemVerilog で多態性(polymorphism)のお勉強

SystemVerilogから class が追加されたので出来ます。

  • サンプルコード
class base;
  virtual function void show(int a, int b);
    $display("--- base: a = %3d, b = %3d", a, b);
  endfunction
endclass

class hoge extends base;
  virtual function void show(int a, int b=0);
    $display("--- hoge: a = %3d", a);
  endfunction
endclass

module testbench;

  int i,j;
  base  tmp_cl[2];
  base  cl_base = new;
  hoge  cl_hoge = new;

  initial begin
    i = 10; j = 4;
    tmp_cl[0] = cl_base;
    tmp_cl[1] = cl_hoge;
    tmp_cl[0].show(i,j); // base
    tmp_cl[1].show(i,j); // hoge
    $finish;
  end

endmodule
  • 実行結果
# --- base: a =  10, b =   4
# --- hoge: a =  10

解説

tmp_clは baseクラスの配列です。
実行結果を見るとわかると思いますが、tmp_cl[1]の show関数は
hogeクラスの show関数が呼び出されています。
っというような感じです。