FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

riscv-isa-simに外部デバイスプラグインを導入する方法 (2. 外部プログラムでlibriscv.aを活用する方法)

前回、Spikeシミュレータで外部デバイスのライブラリを動的ロードする方法が分かったので、これを自作CPUのシミュレーション環境に導入することを考える。

自分はSpikeを取り込んだ単体でELFファイルをシミュレーションする環境を作っているのだが、これに対して外部ライブラリを導入することを考える。

int main(int argc, char **argv)
{
  compare_log_fp = fopen("spike_dpi_main.log", "w");

  initial_spike (argv[1], 64, 0, 1);
  processor_t *p = spike_core->get_core(0);

  // fprintf(compare_log_fp, "INST     CYCLE    PC\n");

  for (int i = 0; i < 10000000; i++) {
    p->step(1);
    auto iss_pc = p->get_state()->prev_pc;
    auto instret = p->get_state()->minstret;
    auto cycle = p->get_state()->mcycle;
  }

  return 0;
}

これでELFファイルをロードして、シリアルデバイスを動的ロードしてみる。

../../../spike_dpi/spike_main /home/msyksphinz/work/riscv/linux/riscv64-linux/output_hifive/images/fw_jump.elf
Unable to load extlib '../../../spike_dpi/libserialdevice.so': ../../../spike_dpi/libserialdevice.so: undefined symbol: register_mmio_plugin

このエラーで随分と悩んでしまった。spike_mainの実行バイナリの作り方が悪いのだが、これの問題の解決方法を調べるのに丸一日を費やしてしまった。バイナリ生成時のgccのコマンドに-L. -Wl,--export-dynamicを追加すればよい。

spike_main: spike_dpi_exe.o libspike_dpi.a
    g++ -L. -Wl,--export-dynamic -o $@ $^ $(EXE_CFLAGS)

これで無事に外部デバイスをロードできるようになった。

core   0: 3 0x0000000080001fee (0x14379073) c323_stval 0x0000000000000000
core   0: 3 0x0000000080001ff2 (0x00093783) x15 0x0000000080200000 mem 0x0000000080017e30
core   0: 3 0x0000000080001ff6 (0x14179073) c321_sepc 0x0000000080200000
core   0: 3 0x0000000080001ffa (0x00893783) x15 0x0000000000000002 mem 0x0000000080017e38
core   0: 3 0x0000000080001ffe (0x14279073) c322_scause 0x0000000000000002
core   0: 3 0x0000000080002002 (0x10502773) x14 0x0000000080200000
core   0: 3 0x0000000080002006 (0x1084b783) x15 0x0000000a00000900 mem 0x0000000080017ff0
core   0: 3 0x000000008000200a (0x10e4b023) mem 0x0000000080017fe8 0x0000000080200000
core   0: 3 0x000000008000200e (0x7779) x14 0xffffffffffffe000
core   0: 3 0x0000000080002010 (0x6ff70713) x14 0xffffffffffffe6ff
core   0: 3 0x0000000080002014 (0x4685) x13 0x0000000000000001
core   0: 3 0x0000000080002016 (0x8ff9) x15 0x0000000a00000000
core   0: 3 0x0000000080002018 (0x6705) x14 0x0000000000001000
core   0: 3 0x000000008000201a (0x0eda0363)
core   0: 3 0x0000000080002100 (0x90070713) x14 0x0000000000000900
core   0: 3 0x0000000080002104 (0x8fd9) x15 0x0000000a00000900
core   0: 3 0x0000000080002106 (0xbf39)
core   0: 3 0x0000000080002024 (0x0027f713) x14 0x0000000000000000
core   0: 3 0x0000000080002028 (0xfdf7f793) x15 0x0000000a00000900
core   0: 3 0x000000008000202c (0xc319)
core   0: 3 0x0000000080002032 (0x9bf5) x15 0x0000000a00000900
core   0: 3 0x0000000080002034 (0x10f4b423) mem 0x0000000080017ff0 0x0000000a00000900
core   0: 3 0x0000000080002038 (0x70a2) x 1 0x0000000080007ed6 mem 0x0000000080017e00
core   0: 3 0x000000008000203a (0x7402) x 8 0x0000000080017e78 mem 0x0000000080017df8
core   0: 3 0x000000008000203c (0x64e2) x 9 0x0000000080017ee8 mem 0x0000000080017df0
core   0: 3 0x000000008000203e (0x6942) x18 0x0000000000000003 mem 0x0000000080017de8
core   0: 3 0x0000000080002040 (0x69a2) x19 0x0000000000000000 mem 0x0000000080017de0