Vengineerの戯言

人生は短いけど、長いです。人生を楽しみましょう!

BluespecのRISC-V CoreをBluespecとVerilatorでシミュレーションしてみる(その2)

はじめに

  • BluespecのRISC-V CoreをBluespecとVerilatorでシミュレーションしてみる(その1)

の続き

bluespec と verilator の違いを見る

Piccolo CPUコアにて、bluespec と verilator の違いを見てみます。

まずは、bluespec の場合

$ cd builds/RV64ACDFIMSU_Piccolo_bluesim
$ make compile
INFO: Re-compiling Core (CPU, Caches)
bsc -u -elab -sim  -bdir build_dir  -simdir build_dir  -info-dir build_dir  -D RV64 -D ISA_PRIV_M  -D ISA_PRIV_U  -D ISA_PRIV_S -D SV39 -D ISA_I  -D ISA_M  -D ISA_A  -D ISA_C -D ISA_F  -D ISA_D  -D INCLUDE_FDIV  -D INCLUDE_FSQRT -D SHIFT_BARREL -D MULT_SYNTH -D Near_Mem_Caches -D FABRIC64  -keep-fires -aggressive-conditions -no-warn-action-shadowing -no-show-timestamps -check-assert -suppress-warnings G0020 +RTS -K128M -RTS  -show-range-conflict  -p :../../src_Core/CPU:../../src_Core/ISA:../../src_Core/RegFiles:../../src_Core/Core:../../src_Core/Near_Mem_VM:../../src_Core/PLIC:../../src_Core/Near_Mem_IO:../../src_Core/Debug_Module:../../src_Core/BSV_Additional_Libs:../../src_Testbench/Top:../../src_Testbench/SoC:../../src_Testbench/Fabrics/AXI4:+  ../../src_Testbench/Top/Top_HW_Side.bsv
checking package dependencies
All packages are up to date.
INFO: Re-compiled  Core (CPU, Caches)
$ cd builds/RV64ACDFIMSU_Piccolo_verilator
$ make compile
INFO: Verilog RTL generation ...
bsc -u -elab -verilog  -vdir Verilog_RTL  -bdir build_dir  -info-dir build_dir  -D RV64 -D ISA_PRIV_M  -D ISA_PRIV_U  -D ISA_PRIV_S -D SV39 -D ISA_I  -D ISA_M  -D ISA_A  -D ISA_C -D ISA_F  -D ISA_D  -D INCLUDE_FDIV  -D INCLUDE_FSQRT -D SHIFT_BARREL -D MULT_SYNTH -D Near_Mem_Caches -D FABRIC64  -keep-fires -aggressive-conditions -no-warn-action-shadowing -no-show-timestamps -check-assert -suppress-warnings G0020 +RTS -K128M -RTS  -show-range-conflict  -p :../../src_Core/CPU:../../src_Core/ISA:../../src_Core/RegFiles:../../src_Core/Core:../../src_Core/Near_Mem_VM:../../src_Core/PLIC:../../src_Core/Near_Mem_IO:../../src_Core/Debug_Module:../../src_Core/BSV_Additional_Libs:../../src_Testbench/Top:../../src_Testbench/SoC:../../src_Testbench/Fabrics/AXI4:+  ../../src_Testbench/Top/Top_HW_Side.bsv
checking package dependencies
All packages are up to date.
INFO: Verilog RTL generation finished

違いは、bsc コマンドへのオプションです。bluespec の時は、-sim オプションと -simdir オプション、verilator の時は、-verilog オプションと-vdir オプションです。

  • bluespec : bsc -u -elab -sim -simdir build_dir
  • verilator : bsc -u -elab -verilog -vdir Verilog_RTL

-sim オプションにて、シミュレーション用コードを生成し、その時のコードは -simdir オプション(build_dir) に生成されます。

-verilog オプションにて、Verilog HDLコードを生成し、その時のコードは -vdir オプション(Verilog_RTL) に生成されます。

なお、共に、build_dir ディレクトリには、XXXX.ba と XXX.bo というファイルが生成されます。

-sim オプションでは、build_dirディレクトリに、C++コード(XXX.h, XXX.cpp)とコンパイル後のオブジェクトファイル(XXX.o)が生成されます。

-verilog オプションでは、Verilog_RTLディレクトリにVerilogコードとVerilator によって生成されたC++コード(XXX.h, XXX.c) が生成されます。

共に、実行プログラム exe_HW_sim が生成されます。

実行プログラムの実行

ディレクトリにて、

$ make test

を実行するとサンプルプログラムが動きます。test ターゲットは、../Resources/Include_Common.mk の中で次のように定義されています。elf_to_hexプログラムにて、elfファイルをhexファイルに変換し、実行プログラウ exe_HW_sim を実行します。$(VERBOSITY) はデフォルトでは +v1 です。+tohost オプションを付ける必要があります。

# ================================================================
# Test: run the executable on the standard RISCV ISA test specified in TEST

TESTS_DIR ?= $(REPO)/Tests

VERBOSITY ?= +v1

.PHONY: test
test:
        make -C  $(TESTS_DIR)/elf_to_hex
        $(TESTS_DIR)/elf_to_hex/elf_to_hex  $(TESTS_DIR)/isa/$(TEST)  Mem.hex
        ./exe_HW_sim  $(VERBOSITY)  +tohost

下記のように VERBOSITY= を追加すると、デバッグ用のメッセージが表示されません

 make test VERBOSITY=
make -C  ../../Tests/elf_to_hex
make[1]: Entering directory '/mnt/c/Users/haray/home/src/Piccolo/Tests/elf_to_hex'
make[1]: 'elf_to_hex' is up to date.
make[1]: Leaving directory '/mnt/c/Users/haray/home/src/Piccolo/Tests/elf_to_hex'
../../Tests/elf_to_hex/elf_to_hex  ../../Tests/isa/rv64ui-p-add  Mem.hex
c_mem_load_elf: ../../Tests/isa/rv64ui-p-add is a 64-bit ELF file
Section .text.init      : addr         80000000 to addr         80000644; size 0x     644 (= 1604) bytes
Section .tohost         : addr         80001000 to addr         80001048; size 0x      48 (= 72) bytes
Section .riscv.attributes: Ignored
Section .symtab         : Searching for addresses of '_start', 'exit' and 'tohost' symbols
Writing symbols to:    symbol_table.txt
    No 'exit' label found
Section .strtab         : Ignored
Section .shstrtab       : Ignored
Min addr:                    80000000 (hex)
Max addr:                    80001047 (hex)
Writing mem hex to file 'Mem.hex'
Subtracting 0x80000000 base from addresses
./exe_HW_sim    +tohost
Warning: file 'Mem.hex' for memory 'rf' has a gap at addresses 131 to 8388606.
Warning: RegFile 'top.mem_model.rf' -- Read address is out of bounds: 0xaaaaaaaaaaaaaaaa
1: top.soc_top.boot_rom_axi4_deburster::AXI4_Deburster.rl_reset
================================================================
Bluespec RISC-V standalone system simulation v1.2
Copyright (c) 2017-2019 Bluespec, Inc. All Rights Reserved.
================================================================
INFO: watch_tohost = 1, tohost_addr = 0x80001000
1: top.soc_top.mem0_controller_axi4_deburster::AXI4_Deburster.rl_reset
2:top.soc_top.rl_reset_start_initial ...
3: Core.rl_cpu_hart0_reset_from_soc_start
================================================================
CPU: Bluespec  RISC-V  Piccolo  v3.0 (RV64)
Copyright (c) 2016-2020 Bluespec, Inc. All Rights Reserved.
================================================================
71: top.soc_top.core.cpu.rl_reset_complete: restart at PC = 0x1000
73: Near_Mem_IO_AXI4.set_addr_map: addr_base 0x2000000 addr_lim 0x200c000
73: Core.rl_cpu_hart0_reset_complete
74: Mem_Controller.set_addr_map: addr_base 0x80000000 addr_lim 0x90000000
74:top.soc_top.rl_reset_complete_initial
1504: Mem_Controller.rl_process_wr_req: addr 0x80001000 (<tohost>) data 0x1
PASS
1505: top:.rl_terminate: soc_top status is 0x1 (= 0d1)
Simulation speed: 1504 cycles, 33073823 nsecs  = 45474 cycles/sec

おわりに

bsc コマンドのオプションにて、bluespec と verilog の切り替えをしているんですね。

$ bsc
Usage:
  bsc -help                                to get help
  bsc [flags] file.bsv                     to partially compile a Bluespec file
  bsc [flags] -verilog -g mod file.bsv     to compile a module to Verilog
  bsc [flags] -verilog -g mod -u file.bsv  to recursively compile modules to Verilog
  bsc [flags] -verilog -e topmodule        to link Verilog into a simulation model
  bsc [flags] -sim -g mod file.bsv         to compile to a Bluesim object
  bsc [flags] -sim -g mod -u file.bsv      to recursively compile to Bluesim objects
  bsc [flags] -sim -e topmodule            to link objects into a Bluesim binary
  bsc [flags] -systemc -e topmodule        to link objects into a SystemC model

あれ、SystemCモデルともくっつく?

トップ階層にて、-sim オプションの変わりに -systemc オプションを指定すると、SystemC Wrapper が生成されるようです。 ただし、BluesimのKernelも使うので、Bluespec単体の方がシミュレーション速度は速いと思います。