SystemVerilog DPI 在末尾返回从 C++ 到 verilog 的字符串 - ASCII 字符
SystemVerilog DPI returning string from C++ to verilog - ASCII charaters at the end?
我使用 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端的波形,它们没有X传播。你能帮我理解为什么我得到这个错误吗?
您如此精心构建的字符串流在函数结束时超出了范围,并返回到了它来的位。这些位被重用和覆盖,可能是通过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();
}
字符串流在函数结束时超出范围,关联的内存被覆盖。保持函数与 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();
}