FC2カウンター FPGAの部屋 2022年09月15日
fc2ブログ

FPGAやCPLDの話題やFPGA用のツールの話題などです。 マニアックです。 日記も書きます。

FPGAの部屋

FPGAの部屋の有用と思われるコンテンツのまとめサイトを作りました。Xilinx ISEの初心者の方には、FPGAリテラシーおよびチュートリアルのページをお勧めいたします。

自作回路を cocotb でシミュレーションする3(cocotb でシミュレーション)

自作回路を cocotb でシミュレーションする2(Vitis HLS で multiplier を作成2)”の続き。

cocotb でサンプルをやってきたが、自作回路を cocotb でシミュレーションしてみようということで、前回は、multiplier プロジェクトの C シミュレーション、C コードの合成、C/RTL 協調シミュレーションを行った。今回は、ファイルを用意して、make を行ったら PASS した。gtkwave で波形を表示した。

Cocotb ディレクトリの下に multiplier ディレクトリを新規作成した。
multiplier ディレクトリに multiplier.v と multiplier_mul_mul_16s_16s_32_4_1.v をコピーした。
multiplier.v と multiplier_mul_mul_16s_16s_32_4_1.v の Verilog HDL コードは”自作回路を cocotb でシミュレーションする2(Vitis HLS で multiplier を作成2)”に貼ってある。
ただし、mltiplier.v には、VCD ファイルを生成するために、コードを追加した。
cocotb_32_220915.png

// the "macro" to dump signals
`ifdef COCOTB_SIM
initial begin
  $dumpfile ("multiplier.vcd");
  $dumpvars (0, multiplier);
  #1;
end
`endif


multiplier ディレクトリに test_multiplier.py ファイルを新規作成した。
test_multiplier.py ファイルを示す。

# test_multiplier.py
# 2022/09/13 by marsee

import random
import cocotb
from cocotb.clock import Clock
from cocotb.triggers import Timer, RisingEdge

@cocotb.test()
async def test_multiplier(dut):
    dut.ap_rst = 1 # Reset
    
    clock = Clock(dut.ap_clk, 10, units="ns")  # Create a 10ns period clock on port clk
    cocotb.start_soon(clock.start())  # Start the clock
    
    await Timer(10, units='ns')
    
    dut.ap_start = 1
    dut.ap_rst = 0 # Normal Operation
    for i in range(5):
        dut.a.value = i
        dut.b.value = i+1
        for j in range(3):
            await RisingEdge(dut.ap_clk)
        await Timer(1, units='ns')
        assert dut.c.value == i*(i+1), "Invalid value for multiplication {}".format(i*(i+1))
    
    await Timer(9, units='ns')


Makefile を新規作成した。
Makefile を示す。

# Makefile
# 2022/09/13 by marsee

SIM ?= icarus

VERILOG_SOURCES = $(shell pwd)/multiplier.v
VERILOG_SOURCES += $(shell pwd)/multiplier_mul_mul_16s_16s_32_4_1.v
TOPLEVEL = multiplier
MODULE = test_multiplier

include $(shell cocotb-config --makefiles)/Makefile.sim


これで cocotb のシミュレーションに必要なファイルが用意できた。

次に
make
を行った。
cocotb_28_220915.png

cocotb_29_220915.png

PASS した成功だ。

make 後の mulitplier ディレクトリを示す。
cocotb_31_220915.png

multiplier.vcd ファイルを gtkwave で表示する。
gtkwave multiplier.vcd &
波形が表示された。
cocotb_30_220915.png

make の時のログを貼っておく。

(base) masaaki@masaaki-H110M4-M01:/media/masaaki/Ubuntu_Disk/Cocotb/multiplier$ make
rm -f results.xml
make -f Makefile results.xml
make[1]: ディレクトリ '/media/masaaki/Ubuntu_Disk/Cocotb/multiplier' に入ります
rm -f results.xml
MODULE=test_multiplier TESTCASE= TOPLEVEL=multiplier TOPLEVEL_LANG=verilog \
         /usr/bin/vvp -M /home/masaaki/anaconda3/lib/python3.8/site-packages/cocotb/libs -m libcocotbvpi_icarus   sim_build/sim.vvp 
     -.--ns INFO     gpi                                ..mbed/gpi_embed.cpp:76   in set_program_name_in_venv        Did not detect Python virtual environment. Using system-wide Python interpreter
     -.--ns INFO     gpi                                ../gpi/GpiCommon.cpp:101  in gpi_print_registered_impl       VPI registered
     0.00ns INFO     cocotb                             Running on Icarus Verilog version 10.1 (stable)
     0.00ns INFO     cocotb                             Running tests with cocotb v1.7.0 from /home/masaaki/anaconda3/lib/python3.8/site-packages/cocotb
     0.00ns INFO     cocotb                             Seeding Python random module with 1663099051
     0.00ns INFO     cocotb.regression                  Found test test_multiplier.test_multiplier
     0.00ns INFO     cocotb.regression                  running test_multiplier (1/1)
/media/masaaki/Ubuntu_Disk/Cocotb/multiplier/test_multiplier.py:11: DeprecationWarning: Setting values on handles using the ``dut.handle = value`` syntax is deprecated. Instead use the ``handle.value = value`` syntax
  dut.ap_rst = 1 # Reset
VCD info: dumpfile multiplier.vcd opened for output.
/media/masaaki/Ubuntu_Disk/Cocotb/multiplier/test_multiplier.py:19: DeprecationWarning: Setting values on handles using the ``dut.handle = value`` syntax is deprecated. Instead use the ``handle.value = value`` syntax
  dut.ap_start = 1
/media/masaaki/Ubuntu_Disk/Cocotb/multiplier/test_multiplier.py:20: DeprecationWarning: Setting values on handles using the ``dut.handle = value`` syntax is deprecated. Instead use the ``handle.value = value`` syntax
  dut.ap_rst = 0 # Normal Operation
   160.00ns INFO     cocotb.regression                  test_multiplier passed
   160.00ns INFO     cocotb.regression                  *****************************************************************************************
                                                        ** TEST                             STATUS  SIM TIME (ns)  REAL TIME (s)  RATIO (ns/s) **
                                                        *****************************************************************************************
                                                        ** test_multiplier.test_multiplier   PASS         160.00           0.00      40576.39  **
                                                        *****************************************************************************************
                                                        ** TESTS=1 PASS=1 FAIL=0 SKIP=0                   160.00           0.21        754.96  **
                                                        *****************************************************************************************
                                                        
make[1]: ディレクトリ '/media/masaaki/Ubuntu_Disk/Cocotb/multiplier' から出ます

  1. 2022年09月15日 04:54 |
  2. シミュレーション
  3. | トラックバック:0
  4. | コメント:0