1

我使用 DPI 从 C 函数返回一个字符串到 SystemVerilog。

const char* print_input_dpi(int w, int h, int p, ......int mtail){
std::stringstream ss;

ss<<"w="<<std::hex<<w<<" ";
ss<<"h="<<std::hex<<h<<" ";
ss<<"p="<<std::hex<<p<<" ";
...
ss<<"mtail="<<std::hex<<mtail<<" ";

return (char*)ss.str().c_str();
}

在 SystemVerilog 方面:

string formatted_string;
always @* begin
  if(en) begin
    formatted_string = print_input_dpi(w,h,p,...mtail)l  
end

...
always @(nededge sclk) begin
   $fdisplayb(log_file, "", formatted_string)
end

结果:有时结果是这样的:

w=1, h=3f, p=2f, ...mtail=0ã

有时我得到这个:

w=1, h=3f, p=2f, ...mtailº

我检查了 verilog 端的波形,它们是 NO X 传播。你能帮我理解为什么我得到这个错误。

4

2 回答 2

3

您如此精心构建的字符串流在函数末尾超出范围,并返回到它来自的位。这些位被重用和覆盖,可能是由 cout 打印所述位,导致损坏。老实说,你走运了。从下周二开始,它可能看起来工作正常,但一周后就崩溃了。

const char* print_input_dpi(int w, int h, int p, ......int mtail)
{
    std::stringstream ss; //<< local variable created here.
    ...    
    return (char*)ss.str().c_str();
} // out of scope and destroyed here, so the returned pointer now points to god knows what.

快速解决:

string print_input_dpi(int w, int h, int p, ......int mtail)
{
    std::stringstream ss; //<< local variable.
    ...    
    return ss.str();
} 
于 2015-06-17T23:18:47.727 回答
0

字符串流在函数结束时超出范围,并且相关的内存被覆盖。保持函数与 SV DPI 兼容性的正确修复只是更改字符串流的生命周期:

std::stringstream ss; // global variable
const char* print_input_dpi(int w, int h, int p, ......int mtail)
{
    ...
    return ss.str().c_str();
}
于 2017-02-17T21:13:45.833 回答