Skip to main content
 首页 » 编程设计

python之Cocotb 在门级仿真中使用泛型/参数

2024年10月17日9lautakyan007

我已经为我的设计成功设置了一个 Cocotb 验证环境,我很高兴它适用于 RTL(在我的例子中是 VHDL)。

我的设计使用了泛型,我在 python 代码的几个地方(主要是在 run_test 和模型中)检索这些泛型的值,遵循模板:
my_generic = dut.GEN_NAME.value

不幸的是,这在门级仿真的情况下不起作用,因为我的综合设计不再具有泛型,因此 dut.GEN_NAME.value 不存在。

我是否应该朝着从模拟流程(Cocotb 的 makefile)中获取参数/泛型值的方向移动?

如果是这样,最干净的方法是什么?使用环境变量?

(顺便说一句,我使用 Questa,即使我不希望这方面依赖于模拟器...)

感谢您的帮助和建议...

请您参考如下方法:

将配置传递给 Python Cocotb 代码是可能的,但它很容易出错,因为您必须确保传递的值与用于合成的值相同。

另一种解决方案是将顶层实体的配置包存储在单独的文件中,例如,top_config.vhdl,内容为:

library ieee; 
use ieee.std_logic_1164.all; 
 
package top_config is 
 
  constant AA : positive := 5; 
  constant BB : integer := 10; 
 
end package top_config; 

此处定义的常量随后用作顶级实体泛型的默认值或直接在顶级实体中使用。

现在可以通过 Cocotb 测试台中的一些 Python 代码解析该包:

from re import compile as re_compile 
 
constantRegExpStr  = r"^\s*constant\s*"   # keyword and spaces 
constantRegExpStr += r"(?P<name>\w+)"     # name fo constant 
constantRegExpStr += r"\s*:\s*"           # divider and spaces 
constantRegExpStr += r"(?P<type>\w+)"     # type name 
constantRegExpStr += r"\s*:=\s*"          # assignment and spaces 
constantRegExpStr += r"(?P<value>[0-9]+)" # value 
constantRegExpStr += r"\s*;"              # end of statement 
 
constantRegExp = re_compile(constantRegExpStr) 
 
 
with open("top_config.vhdl") as f: 
    for line in f.readlines(): 
        m = constantRegExp.match(line) 
        if m is not None: 
            print("constant '{0}' with value '{1}'".format(m.group('name'), m.group('value'))) 

您可以将其添加到字典或执行其他操作,而不是打印匹配项。