FC2カウンター FPGAの部屋 シミュレーション
fc2ブログ

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

FPGAの部屋

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

cocotb 実行時の python, make, iverilog のバージョン

職場の Ubuntu 18.04 のパソコンだと cocotb が make できないのだが、自宅のパソコンの python, make, iverilog のバージョンを書いておく。

python --version
Python 3.8.10

make --version
GNU Make 4.1
このプログラムは x86_64-pc-linux-gnu 用にビルドされました
Copyright (C) 1988-2014 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 以降
これはフリーソフトウェアです: 自由に変更および配布できます.
法律の許す限り、 無保証 です.

iverilog -v
Icarus Verilog version 10.1 (stable) ()

Copyright 1998-2015 Stephen Williams
  1. 2022年09月16日 04:26 |
  2. シミュレーション
  3. | トラックバック:0
  4. | コメント:0

自作回路を 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

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

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

cocotb でサンプルをやってきたが、自作回路を cocotb でシミュレーションしてみようということで、前回は、乗算器(mulitplier)のソースコードとテストベンチを貼って、Vitis HLS 2022.1 の multiplier プロジェクトを作成した。今回は、multiplier プロジェクトの C シミュレーション、C コードの合成、C/RTL 協調シミュレーションを行った。

C シミュレーションを行った。結果を示す。
cocotb_23_220913.png

C コードの合成を行った。
Latency は 3 クロックだった。
cocotb_24_220913.png

合成された Verilog HDL ファイルだが、multiplier.v と multiplier_mul_mul_16s_16s_32_4_1.v が生成された。
今回は、2 つの Verilog HDL コードのみを使用する。
multiplier.v のポート宣言部分を確認した。ブロック・レベルのインターフェースは ap_ctrl_hs で入力は ap_none, 出力は、ap_vld だった。
cocotb_25_220913.png

C/RTL 協調シミュレーションを行った。
レイテンシは 3 クロックだった。
cocotb_26_220913.png

C/RTL 協調シミュレーションの波形を示す。
cocotb_27_220913.png

multiplier.v を貼っておく。

// ==============================================================
// RTL generated by Vitis HLS - High-Level Synthesis from C, C++ and OpenCL v2022.1 (64-bit)
// Version: 2022.1
// Copyright (C) Copyright 1986-2022 Xilinx, Inc. All Rights Reserved.
// 
// ===========================================================

`timescale 1 ns / 1 ps 

(* CORE_GENERATION_INFO="multiplier_multiplier,hls_ip_2022_1,{HLS_INPUT_TYPE=cxx,HLS_INPUT_FLOAT=0,HLS_INPUT_FIXED=0,HLS_INPUT_PART=xc7z020-clg400-1,HLS_INPUT_CLOCK=10.000000,HLS_INPUT_ARCH=others,HLS_SYN_CLOCK=2.150000,HLS_SYN_LAT=3,HLS_SYN_TPT=none,HLS_SYN_MEM=0,HLS_SYN_DSP=0,HLS_SYN_FF=4,HLS_SYN_LUT=25,HLS_VERSION=2022_1}" *)

module multiplier (
        ap_clk,
        ap_rst,
        ap_start,
        ap_done,
        ap_idle,
        ap_ready,
        a,
        b,
        c,
        c_ap_vld,
        ap_return
);

parameter    ap_ST_fsm_state1 = 4'd1;
parameter    ap_ST_fsm_state2 = 4'd2;
parameter    ap_ST_fsm_state3 = 4'd4;
parameter    ap_ST_fsm_state4 = 4'd8;

input   ap_clk;
input   ap_rst;
input   ap_start;
output   ap_done;
output   ap_idle;
output   ap_ready;
input  [15:0] a;
input  [15:0] b;
output  [31:0] c;
output   c_ap_vld;
output  [31:0] ap_return;

reg ap_done;
reg ap_idle;
reg ap_ready;
reg c_ap_vld;

(* fsm_encoding = "none" *) reg   [3:0] ap_CS_fsm;
wire    ap_CS_fsm_state1;
wire  signed [31:0] grp_fu_53_p2;
wire    ap_CS_fsm_state4;
reg   [3:0] ap_NS_fsm;
reg    ap_ST_fsm_state1_blk;
wire    ap_ST_fsm_state2_blk;
wire    ap_ST_fsm_state3_blk;
wire    ap_ST_fsm_state4_blk;
wire    ap_ce_reg;

// power-on initialization
initial begin
#0 ap_CS_fsm = 4'd1;
end

multiplier_mul_mul_16s_16s_32_4_1 #(
    .ID( 1 ),
    .NUM_STAGE( 4 ),
    .din0_WIDTH( 16 ),
    .din1_WIDTH( 16 ),
    .dout_WIDTH( 32 ))
mul_mul_16s_16s_32_4_1_U1(
    .clk(ap_clk),
    .reset(ap_rst),
    .din0(b),
    .din1(a),
    .ce(1'b1),
    .dout(grp_fu_53_p2)
);

always @ (posedge ap_clk) begin
    if (ap_rst == 1'b1) begin
        ap_CS_fsm <= ap_ST_fsm_state1;
    end else begin
        ap_CS_fsm <= ap_NS_fsm;
    end
end

always @ (*) begin
    if ((ap_start == 1'b0)) begin
        ap_ST_fsm_state1_blk = 1'b1;
    end else begin
        ap_ST_fsm_state1_blk = 1'b0;
    end
end

assign ap_ST_fsm_state2_blk = 1'b0;

assign ap_ST_fsm_state3_blk = 1'b0;

assign ap_ST_fsm_state4_blk = 1'b0;

always @ (*) begin
    if ((1'b1 == ap_CS_fsm_state4)) begin
        ap_done = 1'b1;
    end else begin
        ap_done = 1'b0;
    end
end

always @ (*) begin
    if (((ap_start == 1'b0) & (1'b1 == ap_CS_fsm_state1))) begin
        ap_idle = 1'b1;
    end else begin
        ap_idle = 1'b0;
    end
end

always @ (*) begin
    if ((1'b1 == ap_CS_fsm_state4)) begin
        ap_ready = 1'b1;
    end else begin
        ap_ready = 1'b0;
    end
end

always @ (*) begin
    if ((1'b1 == ap_CS_fsm_state4)) begin
        c_ap_vld = 1'b1;
    end else begin
        c_ap_vld = 1'b0;
    end
end

always @ (*) begin
    case (ap_CS_fsm)
        ap_ST_fsm_state1 : begin
            if (((ap_start == 1'b1) & (1'b1 == ap_CS_fsm_state1))) begin
                ap_NS_fsm = ap_ST_fsm_state2;
            end else begin
                ap_NS_fsm = ap_ST_fsm_state1;
            end
        end
        ap_ST_fsm_state2 : begin
            ap_NS_fsm = ap_ST_fsm_state3;
        end
        ap_ST_fsm_state3 : begin
            ap_NS_fsm = ap_ST_fsm_state4;
        end
        ap_ST_fsm_state4 : begin
            ap_NS_fsm = ap_ST_fsm_state1;
        end
        default : begin
            ap_NS_fsm = 'bx;
        end
    endcase
end

assign ap_CS_fsm_state1 = ap_CS_fsm[32'd0];

assign ap_CS_fsm_state4 = ap_CS_fsm[32'd3];

assign ap_return = 32'd0;

assign c = grp_fu_53_p2;

endmodule //multiplier


multiplier_mul_mul_16s_16s_32_4_1.v を貼っておく。

`timescale 1 ns / 1 ps

  module multiplier_mul_mul_16s_16s_32_4_1_DSP48_0(clk, rst, ce, a, b, p);
input clk;
input rst;
input ce;
input signed [16 - 1 : 0] a;
input signed [16 - 1 : 0] b;
output signed [32 - 1 : 0] p;

reg signed [32 - 1 : 0] p_reg; 

reg signed [16 - 1 : 0] a_reg; 
reg signed [16 - 1 : 0] b_reg; 

reg signed [32 - 1 : 0] p_reg_tmp; 

always @ (posedge clk) begin
    if (ce) begin
        a_reg <= a;
        b_reg <= b;
        p_reg_tmp <= a_reg * b_reg;
        p_reg <= p_reg_tmp;
    end
end

assign p = p_reg;

endmodule
`timescale 1 ns / 1 ps
module multiplier_mul_mul_16s_16s_32_4_1(
    clk,
    reset,
    ce,
    din0,
    din1,
    dout);

parameter ID = 32'd1;
parameter NUM_STAGE = 32'd1;
parameter din0_WIDTH = 32'd1;
parameter din1_WIDTH = 32'd1;
parameter dout_WIDTH = 32'd1;
input clk;
input reset;
input ce;
input[din0_WIDTH - 1:0] din0;
input[din1_WIDTH - 1:0] din1;
output[dout_WIDTH - 1:0] dout;



multiplier_mul_mul_16s_16s_32_4_1_DSP48_0 multiplier_mul_mul_16s_16s_32_4_1_DSP48_0_U(
    .clk( clk ),
    .rst( reset ),
    .ce( ce ),
    .a( din0 ),
    .b( din1 ),
    .p( dout ));

endmodule

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

自作回路を cocotb でシミュレーションする1(Vitis HLS で multiplier を作成1)

cocotb でサンプルをやってきたが、自作回路を cocotb でシミュレーションしてみようと思う。
AXI4 インターフェースの回路をシミュレーションしてみたいと思って、検索してみたところ、”AXI interface modules for Cocotb”を見つけた。これを使用すれば、AXI4 インターフェースのシミュレーションができそうだ。AXI4 Slave Bus Functional Model も作ってあるので、スレーブも問題ない。
AXI4 インターフェースの cocotb を使ったシミュレーションをしたいのだが、何分にも Makefile の書き方がよく分かっていないので、簡単な回路から cocotb でシミュレーションをしてみよう。
今回は、Vitis HLS 2022.1 で乗算器(mulitplier)を作成する。なお、ZYBO Z7-20 用とした。この乗算器を cocotb でシミュレーションしてみたい。

multiplier のソースコードとテストベンチを示す。
ソースコードの multiplier.cpp を示す。

// multiplier.cpp
// 2022/09/13 by marsee
//

#include <stdint.h>

int multiplier(int16_t a, int16_t b, int32_t *c){

    *c = a * b;

    return(0);
}


テストベンチの multiplier_tb.cpp を示す。

// multiplier_tb.cpp
// 2022/09/13 by marsee
//

#include <stdio.h>
#include <stdint.h>

int multiplier(int16_t a, int16_t b, int32_t *c);

int main(){
    int16_t a, b;
    int32_t c;

    a = 3;
    b = 4;

    multiplier(a, b, &c);
    printf("a = %d, b = %d, c = (a*b) = %d\n", a, b, c);

    return(0);
}


ZYBO Z7-20 用の Vitis HLS 2022.1 プロジェクトの multiplier を示す。
cocotb_22_220913.png
  1. 2022年09月13日 04:48 |
  2. シミュレーション
  3. | トラックバック:0
  4. | コメント:0

cocotb を試してみる3(examples/adder)

cocotb を試してみる2(シミュレーション波形を表示)”の続き。

Python で書いたコードをテストベンチとして使用できるテストベンチ環境の cocotb を使ってみたいということで、前回は、”cocotb を試してみる1”で実行したサンプルの波形を gtkwave で表示した。今回は cocotb を git clone して、examples の adder をやってみよう。

cocotb を git clone した。
git clone https://github.com/cocotb/cocotb.git
cocotb_12_220911.png

cocotb/examples/adder に移動した。
cd cocotb/examples/adder/
tree .

cocotb_13_220911.png

hdl/adder.sv から見ていく。
4 ビット長の加算で、dump.vcd に波形を出力する。
cocotb_17_220911.png

model/adder_model.py を見た。
Python で書かれた加算だった。
cocotb_18_220911.png

tests/test_adder.py を見た。
adder_basic_test() と adder_randomised_test() の 2 つの関数(テスト)があった。
adder_basic_test() は 5 と 10 を加算して、dut の X の値を adder_model() の結果と比較する。
adder_randomised_test() はランダムな値の加算を 10 回行う。こちらも dut の X の値を adder_model() の結果と比較する。
2 つのテスト関数の時間を進めるのは、

await Timer(2, units="ns")

のようだ。つまり、1 つのテストあたり 2 ns 時間が進むようだ。
cocotb_19_220911.png

tests/Makefile を見た。
icarus verilog が指定されていないので、make 時に指定する必要があるようだ。
cocotb_20_220911.png

tests ディレクトリに行って make を行った。
cd tests
make SIM=icarus

cocotb_14_220911.png

cocotb_15_220911.png

adder_basic_test() と adder_randomised_test() の 2 つの関数(テスト)共に PASS した。

シミュレーション実行後のファイルを見ると dump.vcd があったので、gtkwave で波形を確認する。
gtkwave dump.vcd
cocotb_16_220911.png

gtkwave の波形を示す。
なお、表示は Data Format を Decimal に変更してある。
cocotb_21_220911.png

最初の結果が adder_basic_test() の 5 + 10 で、その後は adder_randomised_test() の 10 個の加算が続くようだ。
全ての加算は 2 ns の間隔になっていた。

最後に make の結果を貼っておく。

(base) masaaki@masaaki-H110M4-M01:/media/masaaki/Ubuntu_Disk/Cocotb/cocotb/examples/adder/tests$ make SIM=icarus
rm -f results.xml
make -f Makefile results.xml
make[1]: ディレクトリ '/media/masaaki/Ubuntu_Disk/Cocotb/cocotb/examples/adder/tests' に入ります
mkdir -p sim_build
/usr/bin/iverilog -o sim_build/sim.vvp -D COCOTB_SIM=1 -s adder -f sim_build/cmds.f -g2012   /media/masaaki/Ubuntu_Disk/Cocotb/cocotb/examples/adder/tests/../hdl/adder.sv
rm -f results.xml
MODULE=test_adder TESTCASE= TOPLEVEL=adder 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 1662838789
     0.00ns INFO     cocotb.regression                  Found test test_adder.adder_basic_test
     0.00ns INFO     cocotb.regression                  Found test test_adder.adder_randomised_test
     0.00ns INFO     cocotb.regression                  running adder_basic_test (1/2)
                                                          Test for 5 + 10
VCD info: dumpfile dump.vcd opened for output.
     2.00ns INFO     cocotb.regression                  adder_basic_test passed
     2.00ns INFO     cocotb.regression                  running adder_randomised_test (2/2)
                                                          Test for adding 2 random numbers multiple times
    22.00ns INFO     cocotb.regression                  adder_randomised_test passed
    22.00ns INFO     cocotb.regression                  ******************************************************************************************
                                                        ** TEST                              STATUS  SIM TIME (ns)  REAL TIME (s)  RATIO (ns/s) **
                                                        ******************************************************************************************
                                                        ** test_adder.adder_basic_test        PASS           2.00           0.00       2616.21  **
                                                        ** test_adder.adder_randomised_test   PASS          20.00           0.00      19518.44  **
                                                        ******************************************************************************************
                                                        ** TESTS=2 PASS=2 FAIL=0 SKIP=0                     22.00           0.29         76.33  **
                                                        ******************************************************************************************
                                                        
make[1]: ディレクトリ '/media/masaaki/Ubuntu_Disk/Cocotb/cocotb/examples/adder/tests' から出ます

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

cocotb を試してみる2(シミュレーション波形を表示)

cocotb を試してみる1”の続き。

Python で書いたコードをテストベンチとして使用できるテストベンチ環境の cocotb を使ってみたいということで、前回は、cocotb のインストールと最初のサンプルをやってみて成功した。今回は、前回実行したサンプルの波形を gtkwave で表示した。

シミュレータには Icarus Verilog を使用しているので、VCD ファイルを出力させたい。
”cocotb’s documentation”の”Icarus Verilog/Waveforms”にやり方が書いてあった。
そうだ、Icarus Verilog は Verilog HDL コードに VCD コードに出力するように書くんだった。
”cocotb’s documentation”の”Icarus Verilog/Waveforms”のコードの一部を引用する。

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


これを dff.sv に追加する。
cocotb/cocotb”の README の dff.sv を引用して、VCD ファイルに波形を出力するコードを追加した dff.sv を貼る。
cocotb_9_220908.png

// dff.sv

`timescale 1us/1ns

module dff (
    output logic q,
    input logic clk, d
);

always @(posedge clk) begin
    q <= d;
end

`ifdef COCOTB_SIM
initial begin
  $dumpfile ("dff.vcd");
  $dumpvars (0, dff);
  #1;
end
`endif

endmodule


Makefile にも、シミュレータを指定する行を追加した。

SIM ?= icarus


cocotb/cocotb”の README の Makefile を引用して、現在の Makefile を貼った。
cocotb_8_220908.png

# Makefile

SIM ?= icarus

TOPLEVEL_LANG = verilog
VERILOG_SOURCES = $(shell pwd)/dff.sv
TOPLEVEL = dff
MODULE = test_dff

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


更にテストベンチの説明をする。
cocotb/cocotb”の README の test_dff.py を引用する。
cocotb_4_220908.png

”cocotb’s documentation”の”Creating a Test”によると、
”@cocotb.test() ”でテストする関数を指定しているようだ。
”.value = value”で信号に値を割り当てる。
”.value”で信号の値を取得できる。

これで VCD ファイルを出力する設定が終了したので、make を実行した。
make

実行後に dff_example ディレクトリを見ると dff.vcd が生成されていた。
cocotb_10_220908.png

gtkwave を起動して、dff.vcd を読み込ませる。
gtkwave dff.vcd
波形を表示するとクロックの立ち下がりで q が d をキャプチャしているのが分かる。
cocotb_11_220908.png
  1. 2022年09月10日 05:00 |
  2. シミュレーション
  3. | トラックバック:0
  4. | コメント:0

cocotb を試してみる1

テストベンチが Python で書かれた cocotb を試してみようと思う。
cocotb を知ったのは Adam Talyor さんの記事” MicroZed Chronicles: Getting Started with Cocotb”を読んだからだった。この記事を試してみようと思ったのだが、シミュレータに ModelSim を使っていた。ModelSim は持っていないので、そこで本家?の”cocotb/cocotb”の README のコードを試してみることにした。

cocotb のドキュメントは”docs.cocotb.org”にある。その翻訳の一部を引用する。

cocotbは、Pythonを使用してVHDL および SystemVerilog RTLを検証するためのCOroutineベースのCOsimulation TestBench環境です。


今回のシミュレータは icarus verilog を使用する。
まずは、icarus verilog をインストールするのだが、すでに gtkwave も含めてインストール済みだった。
cocotb_1_220908.png

インストールしていない場合は、”Icarus Verilogの導入とAND回路のシミュレーション”が参考になる。

さて、”cocotb/cocotb”の README を参照して、cocotb を Ubuntu 18.04 にインストールする。
pip install cocotb
cocotb_2_220908.png

インストール成功した。

cocotb/cocotb”の README のコードをコピー & ペーストして、/media/masaaki/Ubuntu_Disk/Cocotb/dff_example ディレクトリの下に dff.sv, test_dff.py, Makefile を作成した。
cocotb_3_220908.png

cocotb_4_220908.png

cocotb_5_220908.png

cocotb_6_220908.png

make をしてみよう。
make SIM=icarus
を実行した。
PASS した。
cocotb_7_220908.png

make 時のログを示す。

(base) masaaki@masaaki-H110M4-M01:/media/masaaki/Ubuntu_Disk/Cocotb/dff_example$ make SIM=icarus
rm -f results.xml
make -f Makefile results.xml
make[1]: ディレクトリ '/media/masaaki/Ubuntu_Disk/Cocotb/dff_example' に入ります
mkdir -p sim_build
/usr/bin/iverilog -o sim_build/sim.vvp -D COCOTB_SIM=1 -s dff -f sim_build/cmds.f -g2012   /media/masaaki/Ubuntu_Disk/Cocotb/dff_example/dff.sv
rm -f results.xml
MODULE=test_dff TESTCASE= TOPLEVEL=dff 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 1662639569
     0.00ns INFO     cocotb.regression                  Found test test_dff.test_dff_simple
     0.00ns INFO     cocotb.regression                  running test_dff_simple (1/1)
                                                          Test that d propagates to q
 95001.00ns INFO     cocotb.regression                  test_dff_simple passed
 95001.00ns INFO     cocotb.regression                  **************************************************************************************
                                                        ** TEST                          STATUS  SIM TIME (ns)  REAL TIME (s)  RATIO (ns/s) **
                                                        **************************************************************************************
                                                        ** test_dff.test_dff_simple       PASS       95001.00           0.00   33260690.68  **
                                                        **************************************************************************************
                                                        ** TESTS=1 PASS=1 FAIL=0 SKIP=0              95001.00           2.71      35015.19  **
                                                        **************************************************************************************
                                                        
make[1]: ディレクトリ '/media/masaaki/Ubuntu_Disk/Cocotb/dff_example' から出ます

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