本文介紹了cocotb的安裝、python tb文件的寫(xiě)法、用xrun仿真cocotb的腳本等,我們來(lái)看看體驗(yàn)如何。
一、準(zhǔn)備
二、寫(xiě)RTL
top.svmodule top(input wire clk,input wire rst_n,input wire [7:0] din,output reg [7:0] dout);initial begin$fsdbDumpfile("top.fsdb");top);endclk, negedge rst_n)if(!rst_n)dout <= 'd0;elsedout <= din;endmodule // top
三、寫(xiě)tb
# tb.pyimport cocotbfromcocotb.triggersimportTimer, FallingEdgeasync def gen_clk(dut):for cycle in range(100):dut.clk.value = 0await Timer(10, units="ns")dut.clk.value = 1awaitTimer(10,units="ns")async def gen_rst(dut):dut.rst_n.value = 0await Timer(22, units="ns")dut.rst_n.value = 1print("ResetDone")async def tb(dut):await cocotb.start(gen_clk(dut))await cocotb.start(gen_rst(dut))test_data_list = range(0,50, 5)for test_data in test_data_list:await FallingEdge(dut.clk)dut.din.value=test_dataawait Timer(100, units="ns")
6~11行:定義了一個(gè)時(shí)鐘,50MHz,100個(gè)周期。
13~17行:定義了一個(gè)復(fù)位信號(hào),低電平有效。復(fù)位拉高打印“Reset Done”,方便看log。
19行:用@cocotb.test()裝飾器指定了tb的頂層主函數(shù)。
22行:異步啟動(dòng)gen_clk
23行:異步啟動(dòng)gen_rst
25~28行:產(chǎn)生了一些測(cè)試數(shù)據(jù),在時(shí)鐘下降沿后驅(qū)動(dòng)dut的din。
30行:等待100ns結(jié)束仿真
四、寫(xiě)仿真腳本Makefile
SIM ?= xceliumTOPLEVEL_LANG ?= verilogVERILOG_SOURCES += ./top.svTOPLEVEL = topMODULE = tbinclude $(shell cocotb-config --makefiles)/Makefile.sim
設(shè)置默認(rèn)仿真器為cadence xcellium,RTL語(yǔ)言選verilog,指定RTL頂層模塊名字(就是dut的名字),testbench的名字為tb,最后include一個(gè)cocotb共用的makefile。
五、仿真和看波形
把top.sv、tb.py、Makefile放同一個(gè)目錄下,敲linux命令:make。不出意外的話,仿真可以正確編譯和仿真,如下圖:

由于我們?cè)赗TL頂層加入了dump fsdb波形的代碼,所以在log里可以看到有波形產(chǎn)生。280ns仿真結(jié)束,并顯示“tb passed”,并打印出匯總信息??梢?jiàn)log還是很友好的。
用verdi打開(kāi)fsdb,與預(yù)期一致:

審核編輯 :李倩
-
仿真器
+關(guān)注
關(guān)注
14文章
1053瀏覽量
88210 -
代碼
+關(guān)注
關(guān)注
30文章
4977瀏覽量
74400 -
python
+關(guān)注
關(guān)注
58文章
4888瀏覽量
90320
原文標(biāo)題:厭倦了sv/uvm?來(lái)看看用python寫(xiě)驗(yàn)證環(huán)境
文章出處:【微信號(hào):處芯積律,微信公眾號(hào):處芯積律】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
cocotb的安裝、python tb文件的寫(xiě)法
評(píng)論