使用Cocotb验证RTL时调用Python函数multi()遇运行时错误求助
问题分析与解决
核心错误原因
你遇到的RuntimeError: no running event loop是因为:
- 标记了
@cocotb.test()的函数是Cocotb的测试用例入口,不能直接当作普通异步函数调用 - 直接在测试用例外部调用异步函数
multi(),此时没有Cocotb的异步事件循环在运行 - 代码本身存在多处语法错误,导致执行逻辑混乱
代码中的具体问题
async def test(dut)末尾缺少冒号:for j i in range(10)语法错误,应为for j in range(10)async def multi =(a=4 , b=7)语法错误,函数定义不能用=,且辅助函数不应加@cocotb.test()装饰器(这个装饰器仅用于顶层测试用例)- 辅助异步函数必须在Cocotb测试用例内部通过
await调用,不能直接执行multi()
修正后的代码
import cocotb from cocotb.triggers import RisingEdge import asyncio async def multi(dut, a=4, b=7): await asyncio.sleep(0.1) result = a * b await RisingEdge(dut.clk_i) print("result", result) dut.value_a.value = a dut.value_b.value = b await RisingEdge(dut.clk_i) print("Dut : ", int(dut.final_value.value)) return result # 添加返回值以便外部接收 @cocotb.test() async def test(dut): for i in range(10): # something for j in range(10): # something output = await multi(dut) # 在测试用例内部通过await调用异步函数 print(output)
关键修正点
- 去掉
multi函数上的@cocotb.test()装饰器,将其改为普通异步辅助函数 - 在测试用例
test内部通过await multi(dut)调用辅助函数,确保在Cocotb的事件循环中执行 - 修复所有语法错误(函数定义的冒号、循环语句的语法)
- 给
multi函数添加返回值,以便接收计算结果
内容的提问来源于stack exchange,提问作者Neom5
相关产品推荐
相关产品推荐

