Verilog定義計算位寬的函數clogb2


在很多情況下要計算輸入輸出的位寬,比如你寫一個8*8的ram,那么地址需要三位去表示,那么這個函數的方便就體現出來了,你需要使用函數定義就好了。

//位寬計算函數
function integer clogb2 (input integer depth);
begin
    for (clogb2=0; depth>0; clogb2=clogb2+1) 
        depth = depth >>1;                          
end
endfunction

舉個栗子

parameter p_cnt_max = p_rev_time*p_clk_fre*1000_000 - 1; //翻轉時間內所需計數的最大值
//位寬計算函數
function integer clogb2 (input integer depth);
begin
    for (clogb2=0; depth>0; clogb2=clogb2+1) 
        depth = depth >>1;                          
end
endfunction

wire [clogb2(p_cnt_max)-1:0] w_cnt_max;

 

以上。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM