DPI-C中遇见的export问题:%Error: unknown:0: Testbench C called 'xxxxxxx' but scope wasn't set, perhaps due to dpi import call without 'context', or missing svSetScope. See IEEE 1800-2017 35.5.3.

  • 在使用DPI-C的时候遇见了一个问题:在想使用export来在verilator中调用c的函数的时候,会报错(见下图)

  • 此时应该在main.cpp中设置写入:svSetScope(svGetScopeFromName("TOP.ysyx_22040895_top.my_ifu"));

  • 特别注意,参数应该为TOP(这个TOP不是你写的TOP,而是verilator自己生成的TOP,而后是你例化的top的名称以及你例化的模块的名称,他们之间有一个层级的关系)

  • 最后,将所有的代码附上

点击查看代码(Verilator)
`include "/home/groot/ysyx-workbench/npc/include/define.v"
`include "/home/groot/ysyx-workbench/npc/vsrc/ysyx_22040895_pc.v"

module ysyx_22040895_ifu (input wire clk,
                          input wire rst,
                          input wire[`ysyx_22040895_InstBus] inst_i_ifu,
                          input wire pcsel_i_ifu,
                          input wire[`ysyx_22040895_InstAddrBus] dnpc_i_ifu,
                          output wire[`ysyx_22040895_InstBus] inst_o_ifu,
                          output wire[`ysyx_22040895_InstAddrBus] instaddr_o_ifu,
                          output wire[`ysyx_22040895_InstAddrBus] pc_o_ifu,
                          output wire ce_o_ifu);
    
    export "DPI-C" function get_inst;

    ysyx_22040895_pc my_pc(
    .clk(clk),
    .rst(rst),
    .pc_o_pc(instaddr_o_ifu),
    .ce_o_pc(ce_o_ifu),
    .pcsel_i_pc(pcsel_i_ifu),
    .dnpc_i_pc(dnpc_i_ifu)
    );
    
    assign inst_o_ifu = inst_i_ifu;
    assign pc_o_ifu   = instaddr_o_ifu;
    
    
    function void get_inst();
        output int inst;
        inst = inst_i_ifu;
    endfunction
    
endmodule //ifu

点击查看代码(cpp)
#include "verilated_dpi.h"
#include "Vysyx_22040895_top__Dpi.h"
***
main()
{
  ***
  extern void get_inst(int* inst);
  svSetScope(svGetScopeFromName("TOP.ysyx_22040895_top.my_ifu"));
  ***
  get_inst(&inst);
  ***

}
posted @ 2022-08-09 13:39  Groot_Liu  阅读(335)  评论(0编辑  收藏  举报