02. 応用編
たくさんの引数が必要になったり、たくさんの返り値を返したいときなどは、classを使うと楽ですよ。
★taskの引数にclassを使った例
class data_items;
int unsigned id;
byte byte_data;
endclass
task byte_transfer(input data_items item);
$display("id=%0d", item.id);
$display("data=%0b", item.byte_data);
endtask
initial begin
data_items item = new;
item.id = 10;
item.byte_data = 8'b11110000;
byte_transfer(item);
end
★functionの返り値にclassを使った例…
これなんですが、コードを書いてModelSimでコンパイルしたら「その使い方できないよ」と言われまして。classの中ならOKなんだそうです。なんでだろー?以下のコード例ならOKなんで、まぁSystemVerilogの仕様ならば、こういうwrapperをかませて使うので回避するんですかね(キレイじゃないけど)。
class data_items;
int unsigned id;
byte byte_data;
function new(int unsigned id=0, byte byte_data=8'b0);
this.id = id;
this.byte_data = byte_data;
endfunction
function void disp;
$display("id=%0d", this.id);
$display("byte_data=%0b", this.byte_data);
endfunction
endclass
class func_wrapper;
function data_items func_test;
data_items item = new(10, 8'b11110000);
return item;
endfunction
endclass
initial begin
data_items item = new;
func_wrapper func = new;
item = func.func_test;
item.disp;
end