宽度无关函数 [英] Width independent functions

查看:28
本文介绍了宽度无关函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能写一个函数来自动检测输入数据的宽度?例如,考虑下面的奇偶校验函数:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆