Verilog testbench는 HDL의 initial block과 task로 시작해도 되고, Python으로 테스트를 작성하고 싶다면 cocotb를 사용할 수 있다. 둘 다 DUT를 simulator에서 구동하고 결과를 검사하지만, 테스트 코드 재사용과 팀 환경이 선택 기준이다.
무엇을 선택할까
| 선택 | 잘 맞는 경우 |
|---|---|
| Verilog/SystemVerilog testbench | 작은 테스트, HDL 신호 동작과 timing을 바로 표현할 때 |
| cocotb | Python helper, 기존 Python 검증 코드, randomized test를 재사용하고 싶을 때 |
cocotb는 simulator와 cosimulation interface로 연결되는 Python testbench framework다. Python test에서 DUT의 handle에 접근하고 trigger를 기다린다. 실제 simulator별 지원 여부는 cocotb의 현재 compatibility 문서에서 확인한다.
첫 번째 testbench의 구성
테스트 방법에 상관없이 작은 RTL block 검증은 보통 다음 순서로 시작한다.
- DUT 입력과 기대 출력을 정한다.
- clock과 reset을 제공한다.
- 정상 입력, 경계 조건, reset 동작을 자극한다.
- 출력이 기대값과 일치하는지 assertion으로 확인한다.
- 실패 시 입력과 cycle을 식별할 수 있는 정보를 남긴다.
무작위 입력만 넣고 simulation이 끝났다고 검증이 완료되는 것은 아니다. checker가 기대 결과를 비교해야 한다.
cocotb 2.x의 최소 예
다음 예는 synchronous reset을 가진 8-bit counter를 대상으로 한다.
module counter (
input logic clk,
input logic rst,
output logic [7:0] count
);
always_ff @(posedge clk) begin
if (rst)
count <= '0;
else
count <= count + 1'b1;
end
endmodule
Python test:
import cocotb
from cocotb.clock import Clock
from cocotb.triggers import FallingEdge, RisingEdge, Timer
@cocotb.test()
async def counter_increments(dut):
dut.rst.value = 1
clock = Clock(dut.clk, 10, unit="ns")
cocotb.start_soon(clock.start())
await RisingEdge(dut.clk)
await RisingEdge(dut.clk)
await FallingEdge(dut.clk)
dut.rst.value = 0
await RisingEdge(dut.clk)
await Timer(1, unit="ns")
assert int(dut.count.value) == 1
Timer는 clock edge 뒤에 simulator가 nonblocking assignment를 처리할 시간을 두기 위해 사용했다. cocotb와 simulator의 버전을 맞추고, 설치한 release의 문서에 있는 API를 따른다.
실행 구성 확인
cocotb 2.x의 Makefile 기반 실행은 대략 다음 변수를 지정한다.
SIM ?= icarus
TOPLEVEL_LANG = verilog
VERILOG_SOURCES = $(PWD)/counter.sv
COCOTB_TOPLEVEL = counter
COCOTB_TEST_MODULES = test_counter
include $(shell cocotb-config --makefiles)/Makefile.sim
위 설정은 simulator와 Python module을 연결하는 예시다. simulator 설치, Python 환경, 선택한 cocotb version에 따라 추가 설정이 필요할 수 있다. 실행 전에 cocotb-config --version과 simulator 지원표를 확인한다.
다음 단계
counter의 reset과 첫 increment를 검사한 뒤 enable, overflow, 연속 동작처럼 요구사항에 맞는 case를 추가한다. AXI처럼 여러 channel과 backpressure가 있는 protocol은 단순한 counter test보다 protocol-aware driver와 checker를 고려한다.
공식 자료
- cocotb documentation: First Steps
- cocotb documentation: Simulator Support
- IEEE 1800 SystemVerilog Standard
원문 기록
한 줄 요약
작은 HDL 동작은 native testbench로, Python 재사용이 중요한 검증은 cocotb로 시작하되 assertion과 simulator 호환성을 확인한다.