处理 SystemVerilog 包中的参数化
SystemVerilog 添加了包来为常见代码段(函数、类型、常量等)提供命名空间。但由于包没有实例化,因此无法参数化,因此处理参数化成员是有问题的。在实践中,我发现这相当有限,因为我的自定义类型经常有一些参数指定字段宽度等。
我通常通过使用具有默认值的参数来处理这个问题,并且只了解我需要返回更改某些包的源代码应用程序,这对我来说似乎非常错误。但我还没有找到一种方法来更干净地处理这个问题。例如:
package my_pkg;
parameter ADDR_MSB = 7;
parameter DATA_MSB = 31;
typedef struct {
logic [ADDR_MSB:0] address;
logic [DATA_MSB:0] data;
} simple_struct_t;
endpackage
有没有人找到更干净的方法来处理这个问题?我很想听听这个,因为我认为包是 SV 的一个非常强大的补充,可以实现更安全的代码重用,但这个限制非常严重。
SystemVerilog added packages to provide namespaces for common code pieces (functions, types, constants, etc). But since packages are not instantiated, they cannot be parameterized, so dealing with parameterized members is problematic. In practice I have found this pretty limiting since very often my custom types have some parameters dictating field widths etc.
I generally deal with this by using parameters with default values and just understanding that I will need to go back change the package source code for some applications, which seems very wrong to me. But i have yet to find a way to handle this more cleanly. For example:
package my_pkg;
parameter ADDR_MSB = 7;
parameter DATA_MSB = 31;
typedef struct {
logic [ADDR_MSB:0] address;
logic [DATA_MSB:0] data;
} simple_struct_t;
endpackage
Has anyone found a cleaner way of dealing with this? I'd love to hear about it since I think packages are a very powerful addition to SV enabling safer code reuse, but this limitation is pretty severe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用参数化宏来命名具有特定宽度的类型:
然后,在代码中的某个位置,您可以定义所需的结构:
并且,然后在需要的地方使用它,仅使用名称:
You could use parameterized macros to name a type with particular widths:
Then, in some place in your code, you can define the structure(s) you need:
And, then use it wherever you need it, using only the name:
是的,我同意。这是软件包所缺少的功能。
只是在这里吐槽,但您可以将参数抽象到第二个包中,并在编译时使用正确的包来调整您的包。我知道这不是你真正想要的,但它可能会让你更接近。
我想如果我在项目中遇到这个问题,我最终会得到代表每个配置的多个包。
Yeah, I agree. That's a missing feature of packages.
Just spitballin' here, but you could abstract your parameters into a secod package and use the right one at compile-time to tweak your package. I know that's not what you really want, but it might get you close.
I think I would just end up with multiple packages representing each configuration if I faced this in my project.
我有几个想法。首先,我倾向于使用类而不是结构来对数据进行建模。类可以参数化、动态分配、随机化、包含覆盖组等。我只在需要打包结构时才使用结构。打包结构很棒,因为您可以像常规向量一样分配给它们,然后使用命名字段访问数据。很不错。 :)
其次,即使可以重新定义包参数,模拟中也只有一个包“实例”;不能像模块或类那样存在具有不同参数值的多个专业化。所以在我看来,取消参数并使用宏是一个可行的解决方案。尽管我不喜欢使用宏,但这将允许您使用新值重新编译而无需更改源代码。
I have a couple of thoughts. First, I would lean towards modeling my data using classes instead of structs. Classes can be parameterized, dynamically allocated, randomized, contain covergroups, etc. I only use structs when I want a packed struct. Packed structs are wonderful because you can assign to them like a regular vector and then access the data using the named fields. Very nice. :)
Second, even if it were possible to redefine package parameters, there is only one "instance" of a package in a simulation; there can't be multiple specializations with different parameter values like there can be for modules or classes. So it seems to me that doing away with the parameter and using a macro instead is a workable solution. Although I don't like using macros, that would allow you to recompile with new values without changing the source code.
这可能适用,也可能不适用,具体取决于您想要放入包中的内容,但如果您的工具支持,接口可以参数化并且可以综合。
有一个例子 http://www.doulos.com/knowhow/sysverilog/tutorial /接口/
This may or may not apply, depending on exactly what you have in mind to put in the package, but interfaces can be parameterized and are synthesizable if your tool supports it.
There is an example at http://www.doulos.com/knowhow/sysverilog/tutorial/interfaces/
我有同样的问题,一位同事提出了以下建议:
//defines.sv:
//mod_sub.sv:
//mod_top.sv:
我相信系统 Verilog 包是在任何模块之前详细说明的,因此它们的内容不能在编译时通过参数修改时间。
I had the same question and a coworker suggested the following:
//defines.sv:
//mod_sub.sv:
//mod_top.sv:
I believe System Verilog packages are elaborated before any modules thus their contents cannot be modified by parameters at compile time.
我不会说这是一个缺失的功能。几十年来,您尝试做的事情一直是通过 Verilog 中的宏完成的。问题是你的命名方式必须相当独特,以避免包之间的冲突。这不太好,但是很有效。
参数有点不同。它们用于在实例基础上进行定制(如 VHDL 泛型)。要么是逻辑模块,要么是测试平台类。我对它们的唯一批评是,一旦您开始使用它们,它们往往会在整个层次结构中传播,而且语法并不完全紧凑。不过非常强大,并且非常适合代码重用。
I wouldn't say it's a missing feature. What you're trying to do has been done with macros in Verilog for decades. Trouble is you've got to be rather unique in the way you name things to avoid clashes between packages. It's not nice, but it works.
Parameters are a bit different. They are for customising on an instance by instance basis (like VHDL generics). Either on modules for logic, or classes for test-benches. My only criticism of them is once you start using them they tend to propagate throughout your hierarchy, and the syntax isn't exactly compact. Very powerful though, and great for code re-use.
我很高兴这是一篇旧文章,但我认为情况没有改变。
这些天我喜欢做的是通过基于 python 的生成器(peakRDL、Cogapp、custom...)自动生成包,这些生成器从 JSON 格式的文件中获取输入,该文件充当设备树。 (或 SystemRDL,它是指定寄存器的 Accelera 标准)。
这样我就可以根据需要生成尽可能多的包风格(在相同的设计中,或针对不同的运行)。
I appreciate this is an old post, but I don't think the situation has changed.
What I like doing these days is to auto-generate the packages through a python-based generator (peakRDL, Cogapp, custom...) which take their input from a JSON-formatted file, which acts as a device tree. (or SystemRDL which is the Accelera standard to specify registers).
That way I can generate as many flavors of the package (in the same design, or for different runs) as I want.