宽度无关函数 [英] Width independent functions
问题描述
有没有可能写一个函数来自动检测输入数据的宽度?例如,考虑下面的奇偶校验函数:
Is it possible to write a function that can detect the input data width automatically? For example, consider the parity function below:
function parity;
input [31:0] data;
parity = ^ data;
endfunction
当parity(data)
被调用时,输入数据应限制为32位.
When parity(data)
is called, the input data should be limited to 32 bits.
或者,也可以写一个宏,比如`PARITY(data)
,其中系统函数$bits
可以检测数据的宽度,并使宏宽度-独立的.功能是否有可能具有相同的灵活性?
Alternatively, one could write a macro, such as `PARITY(data)
in which the system function $bits
can detect the width of data and make the macro width-independent. Is it possible to have the same flexibility for functions?
我需要我的代码是可合成的.
I need my code to be synthesizable.
推荐答案
您可以创建参数化函数.请参阅 LRM 中的第 13.8 节.看起来该函数必须在这样的类中声明:
You can create a parameterized function. See section 13.8 in the LRM. It looks like the function must be declared inside a class like this:
virtual class C #(parameter WIDTH=32);
static function parity (input [WIDTH-1:0] data);
parity=^data;
endfunction
endclass
然后,当您调用该函数时,使用 bits
任务对其进行参数化:
Then when you call the function parameterized it with the bits
task:
assign parity_bit = C#($bits(data))::parity(data);
EDA Playground 的工作示例.
这篇关于宽度无关函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!