2015-06-18 48 views
2
导出输入宽度

我有一个输入,其宽度我想在精化时确定。我不想提供两个参数,而是想确定从单个参数派生的宽度。事情是这样的:SystemVerilog:从参数

module my_module #(
    COUNT = 9 
) (
    [bitwidth(COUNT)-1:0] index 
); 

我有这对我来说,如果我宣布一个reg内到我的模块我需要这通过外部端口映射驱动工作的功能。

function integer bitwidth; 
    input integer n; 
    integer exp2, width; 
begin 
    width=1; 
    for (exp2=1; exp2<<1 <= n; exp2=exp2<<1) 
    width=width+1; 
    bitwidth = width; 
end 
endfunction 

reg [bitwidth(COUNT)-1:0] this_works = COUNT; 

我该如何做到这一点?我可以利用一个接口(+ modports)来获得我想要的吗?

+0

你在想的。['$ bits'或'$ size'(http://www.asic-world.com/systemverilog/system_task_function3.html)? –

+0

'$ clog2':IEEE标准1800-2012,章节“20.8.1整数学函数” – toolic

+0

'$ bits'将会是合适的,但是当我替换'bitwidth'时,ModelSim告诉我“范围必须以常量表达式为界”。 – hancock

回答

0

这真的有助于展示一个完整的自包含示例。以下对我有用

module my_module #(
    COUNT = 9 
) (
    [bitwidth(COUNT)-1:0] index 
); 
function integer bitwidth; 
    input integer n; 
    integer exp2, width; 
begin 
    width=1; 
    for (exp2=1; exp2<<1 <= n; exp2=exp2<<1) 
    width=width+1; 
    bitwidth = width; 
end 
endfunction 
    initial $displayb(index); 
endmodule 
module top; 
    my_module #(10) mm(); 
endmodule 

如果我们假设$ clog2不存在,则关于此代码的一些评论。

  • 千万不要依赖隐式端口方向和类型,明确表示为 的清晰度和显示意图。
  • 使用ANSI风格参数列表。使用int代替integer

module my_module #( COUNT = 9 ) ( input wire [bitwidth(COUNT)-1:0] index ); function integer bitwidth(input int n); int width; width=1; for (int exp2=1; exp2<<1 <= n; exp2=exp2<<1) width=width+1; bitwidth = width; endfunction initial $displayb(index); endmodule module top; my_module #(10) mm(); endmodule