From 3cfbd7345f0f7ef895d709bbbad6c996be748338 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Mon, 2 Jan 2023 15:33:51 -0800 Subject: [PATCH] vvp: Only ignore leading null-bytes when reading as string through VPI Currently when reading a number literal through the VPI API as a vpiStringVal all null-bytes in the literal get ignored. This behavior is different from when reading a signal through the VPI API as a vpiStringVal. The latter will only ignore leading null-bytes and replace other null-bytes with a space. E.g. the following two will print different values. ``` $display("%s", "a\000b"); // -> " ab" reg [23:0] x = "a\000b"; $display("%s", x); // -> "a b" ``` For consistency modify the number literal formatting code so that it has the same behavior as the signal value formatting code and only replaces leading null-bytes. Signed-off-by: Lars-Peter Clausen --- vvp/vpi_priv.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vvp/vpi_priv.cc b/vvp/vpi_priv.cc index ff11cfe9e..1de770fd4 100644 --- a/vvp/vpi_priv.cc +++ b/vvp/vpi_priv.cc @@ -635,6 +635,8 @@ static void vec4_get_value_string(const vvp_vector4_t&word_val, unsigned width, if (char_val != 0) *cp++ = char_val; + else if (cp != rbuf) + *cp++ = ' '; } for (unsigned idx = 0 ; idx < nchar ; idx += 1) { @@ -645,8 +647,13 @@ static void vec4_get_value_string(const vvp_vector4_t&word_val, unsigned width, if (val == BIT4_1) char_val |= 1 << bdx; } + // Ignore leading null-bytes and replace other null-bytes with space. + // The LRM is not entirely clear on how null bytes should be handled. + // This is the implementation chosen for iverilog. if (char_val != 0) *cp++ = char_val; + else if (cp != rbuf) + *cp++ = ' '; } *cp = 0;