Systemverilog(Green Book)Chapter 5-Overview of Classes and Objects(1)Classes

次の質問に示すように:

class Transaction
    logic [31:0] addr = 'h10;
    logic [31:0] crc,data[8];
    function new(logic [31:0] a =3, d =5);
        addr = a;
        foreach (data[i])
            data[i] = d;
        endfunction
    endclass


initial begin
    Transaction tr,tr1;
    tr = new(10);
    tr1 = new();
end

new()でオブジェクトを作成するプロセスを理解するには、まずnew(10)がクラス、addr、crc、およびdataの変数タイプに従ってスペースを開き、addrに値16を割り当てます。その後、パラメーター10を関数に渡し、addr値を10に上書きします。

new()関数が呼び出されると、外部からパラメーターが渡されないため、addr値には3が割り当てられます。

オブジェクトの作成と破棄:

class word
    byte nb[];
    function new(int n);
        nb = new[n];        //动态数组空间开辟
    endfunction
endclass

initial begin : initial_1
    word wd;
    for(int i=1;i<=4;i++) wd = new(i);   //创建了4个对象
end
initial begin : initial_2
    #1ps
    $display("How many Bytes are allocated for word instance?")
end

 wd = new(1)に必要なスペースが1Bの場合、スペースは実行後に作成されます。

wdが実行された後、ハンドルは4番目のオブジェクト4Bを指します。初期変数は静的であるため、最初の実行が完了すると、変数はそのまま残ります。

class Transaction
    static intcount= 0;
    int id;
    function new();
        id = count++;
    endfunction
endclass

Transaction t1,t2;
initial begin
    t1 = new();
    t2 = new();
    $display("Second id=%d, count = %d",t2.id,t2.count);

 

 

 

公開された14元の記事 ウォン称賛10 ビュー20000 +

おすすめ

転載: blog.csdn.net/Jay_who/article/details/105550031