# cocotb_framework **Repository Path**: listen-chen/cocotb_framework ## Basic Information - **Project Name**: cocotb_framework - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-05 - **Last Updated**: 2026-03-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # cocotb 验证环境 cocotb v2.0.1 Verilator v5.036 ## cocotb安装 ```bash sudo apt-get install make python3 python3-pip libpython3-dev python -m venv cocotb_env source cocotb_env/bin/activate pip install "cocotb~=2.0" pip install cocotb-test pip install pytest pip install coverage ``` ## 测试 ### 1.运行测试 `rtl`:SystemVerilog代码 `TestCase`:测试主函数以及具体的测试用例 运行: ```bash source setup_env.sh cd TestCase pytest ``` `80x86`:对于80x86项目的单元测试示例,使用Makefile测试 运行: ```bash cd 80x86/tests/ALUtest make ``` ### 2.添加测试: 1.在`rtl/`目录下创建要测试的文件夹,并加入源代码。 2.在`TestCase/`目录下创建要测试的文件夹,在测试文件夹中添加一个空的`__init__.py`文件,将测试路径打包。 3.在测试文件夹中添加`test_xxx.py`文件: ```python import os from cocotb_tools.runner import get_runner def run_xxx_test(cpu_name: str, sources: list, toplevel: str = "xxx", testmodules: list = ["test_fun1"]): rtl_dir = sources build_dir = os.getenv("BUILD_DIR", "") runner = get_runner("verilator") runner.build( sources=rtl_dir, hdl_toplevel=toplevel, build_dir=build_dir + "/" + cpu_name + "_" + toplevel + "_sim", always=True, ) for testmodule in testmodules: runner.test( hdl_toplevel=toplevel, test_module="TestCase.xxx." + testmodule, extra_env={"COCOTB_USER_COVERAGE": "1"}, ) ``` 5.在测试文件夹中添加`test_fun1.py`等具体测试用例: ```python import cocotb @cocotb.test() async def xxxtest(dut): ``` 6.在`TestCase/test_main.py`中导入测试包: ```python from TestCase.xxx.test_xxx import run_xxx_test ``` 并创建测试用例: 这里的rtl_dir路径为根目录下的`rtl/`,需要把新加入测试所需要的所有sv文件全部加入sources。 可以创建多个测试,添加到`run_xxx_test`函数的最后一个参数中。 `run_xxx_test`函数的第一个参数是生成Verilator模型路径名称,如果测试两个名称相同的模块,可以使用该参数进行区分。 ```python def test_xxx(): rtl_dir = os.getenv("RTLS", "") sources = [rtl_dir + "/xxx/xxx.sv"] run_xxx_test("", sources, "xxx", ["test_fun1"]) ```