2016-09-16 118 views
1

我正在創建一個需要對測試臺的配置對象進行引用的類。由於配置在整個模擬過程中必須保持完整,我將它作爲const ref對象傳遞。這裏是我想要運行的sudo代碼:Systemverilog構造對象時的const ref arg位置

class tb_config; 
    int unsigned rate; 
    int unsigned chnls[]; 
    const int unsigned nb_chnls; 

    function new (int unsigned rate, int unsigned nb_chnls); 
    this.rate  = rate; 
    this.nb_chnls = nb_chnls; 
    chnls   = new[nb_chnls]; 
    endfunction 
endclass 

class tx_phy; 
    static int phy_id; 
    tb_config cfg; 

    function new (int unsigned phy_id, const ref tb_config cfg); 
     this.phy_id = phy_id; 
     this.cfg = cfg; 
    endfunction 

endclass 


module test; 
    tb_config cfg = new(100, 4); 
    tx_phy phy = new(1234, cfg); 

endmodule 

上面的代碼工作得很好,符合我的期望。但是,如果我將tx_phy :: new中的參數更改爲函數new(const ref tb_config cfg,int unsigned phy_id);並將值傳遞給構造相應我得到Cadence公司的Incisive以下錯誤:

invalid ref argument usage because actual argument is not a variable. 

而且當我在edaplayground與Aldec公司的測試同樣的事情發生:https://www.edaplayground.com/x/5PWV

我認爲這是一個語言的限制,但是有沒有其他原因?

回答

2

原因是因爲如果未指定參數種類是隱含的。您爲第一個參數指定了const ref,但第二個參數沒有指定,因此它也隱含地爲const ref。在第二個參數聲明中添加input可修復此問題。

function new (const ref tb_config cfg, input int unsigned phy_id); 

我也想補充const ref tb_config cfg相當於寫

function new (tb_config cfg, int unsigned phy_id); 

這些論點都隱含input參數,這意味着它們在進入複製。

一個類變量已經是一個引用。通過ref傳遞類變量意味着您可以更新函數內類變量所具有的句柄。使參數const ref意味着您將無法更新類變量,但仍可以更新該類的成員變量引用。沒有機制來阻止更新類對象的成員,除非聲明它們爲protectedlocal

在SystemVerilog中通過ref傳遞函數參數唯一有意義的地方就是當參數是像數組那樣的大數據結構時的優化,而且您只需訪問數組的一些元素。當參數需要在任務生命週期內更新時(即將時鐘作爲參數傳遞)時,您可以使用任務ref參數。

+0

非常感謝!現在它是有道理的。我只添加了ref,因爲編譯器在我沒有指定它的時候給了我錯誤:期望的記號:'ref'。「」testbench.sv「 – maskarih