From bbd59f8a22b6b35e42da0755b0b360029d997a8c Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Wed, 14 Aug 2013 21:37:13 -0400 Subject: [PATCH] Support passing strings to DPI imports. --- include/verilated.cpp | 8 ++++++ include/verilated_heavy.h | 4 +++ test_regress/t/t_dpi_string.pl | 19 +++++++++++++ test_regress/t/t_dpi_string.v | 26 ++++++++++++++++++ test_regress/t/t_dpi_string_c.cpp | 44 +++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100755 test_regress/t/t_dpi_string.pl create mode 100644 test_regress/t/t_dpi_string.v create mode 100644 test_regress/t/t_dpi_string_c.cpp diff --git a/include/verilated.cpp b/include/verilated.cpp index 65e89e00b..5a6c909ea 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -769,6 +769,14 @@ void VL_SFORMAT_X(int obits, void* destp, const char* formatp, ...) { _VL_STRING_TO_VINT(obits, destp, (int)output.length(), output.c_str()); } +void VL_SFORMAT_X(int obits_ignored, string &output, const char* formatp, ...) { + output = ""; + va_list ap; + va_start(ap,formatp); + _vl_vsformat(output, formatp, ap); + va_end(ap); +} + string VL_SFORMATF_NX(const char* formatp, ...) { VL_STATIC_OR_THREAD string output; // static only for speed output = ""; diff --git a/include/verilated_heavy.h b/include/verilated_heavy.h index b32586c00..ec86151b1 100644 --- a/include/verilated_heavy.h +++ b/include/verilated_heavy.h @@ -41,11 +41,15 @@ inline string VL_CVT_PACK_STR_NQ(QData lhs) { IData lw[2]; VL_SET_WQ(lw, lhs); return VL_CVT_PACK_STR_NW(2, lw); } +inline string VL_CVT_PACK_STR_NQ(string lhs) { + return lhs; +} inline string VL_CVT_PACK_STR_NI(IData lhs) { IData lw[1]; lw[0] = lhs; return VL_CVT_PACK_STR_NW(1, lw); } +extern void VL_SFORMAT_X(int obits_ignored, string &output, const char* formatp, ...); extern string VL_SFORMATF_NX(const char* formatp, ...); #endif // Guard diff --git a/test_regress/t/t_dpi_string.pl b/test_regress/t/t_dpi_string.pl new file mode 100755 index 000000000..ae6cbdd77 --- /dev/null +++ b/test_regress/t/t_dpi_string.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl +if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; } +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2003 by Wilson Snyder. This program is free software; you can +# redistribute it and/or modify it under the terms of either the GNU +# Lesser General Public License Version 3 or the Perl Artistic License +# Version 2.0. + +compile ( + v_flags2 => ["t/t_dpi_string_c.cpp"], + ); + +execute ( + check_finished=>1, + ); + +ok(1); +1; diff --git a/test_regress/t/t_dpi_string.v b/test_regress/t/t_dpi_string.v new file mode 100644 index 000000000..26b731253 --- /dev/null +++ b/test_regress/t/t_dpi_string.v @@ -0,0 +1,26 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// Copyright 2009 by Wilson Snyder. This program is free software; you can +// redistribute it and/or modify it under the terms of either the GNU +// Lesser General Public License Version 3 or the Perl Artistic License +// Version 2.0. + +module t (); + + import "DPI-C" function int dpii_string(input string DSM_NAME); + + generate + begin : DSM + string SOME_STRING; + end + endgenerate + + initial begin + $sformat(DSM.SOME_STRING, "%m"); + if (dpii_string(DSM.SOME_STRING) != 5) $stop; + + $write("*-* All Finished *-*\n"); + $finish; + end + +endmodule diff --git a/test_regress/t/t_dpi_string_c.cpp b/test_regress/t/t_dpi_string_c.cpp new file mode 100644 index 000000000..cba607162 --- /dev/null +++ b/test_regress/t/t_dpi_string_c.cpp @@ -0,0 +1,44 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// +// Copyright 2009-2009 by Wilson Snyder. This program is free software; you can +// redistribute it and/or modify it under the terms of either the GNU +// Lesser General Public License Version 3 or the Perl Artistic License. +// Version 2.0. +// +// Verilator is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +//************************************************************************* + +#include +#include "svdpi.h" + +//====================================================================== + +#if defined(VERILATOR) +# include "Vt_dpi_string__Dpi.h" +#elif defined(VCS) +# include "../vc_hdrs.h" +#elif defined(CADENCE) +# define NEED_EXTERNS +#else +# error "Unknown simulator for DPI test" +#endif + +#ifdef NEED_EXTERNS +extern "C" { + + extern int dpii_string (const char* s); + +} +#endif + +//====================================================================== + +int dpii_string(const char* s) { + printf("dpii_string: %s\n",s); + return strlen(s); +}