0
\$\begingroup\$

I am trying to escape non-printable characters in string initialisations in Verilog.

In C, one can escape non-printable characters as described here. In Verilog, there is an escape mechanism, but strangely seemingly only for printable characters:

2.7.1 Escaped identifiers

Escaped identifiers shall start with the backslash character (\) and end with white space (space, tab, newline). They provide a means of including any of the printable ASCII characters in an identifier (the decimal values 33 through 126, or 21 through 7E in hexadecimal).

Neither the leading backslash character nor the terminating white space is considered to be part of the identifier. Therefore, an escaped identifier \cpu3 is treated the same as a nonescaped identifier cpu3.

Can I escape non-printable characters in string initialisations in Verilog? I'm looking to do something like:

reg [6 * 8 - 1:0] test = "Hello\x01";
// Here, \x01 would be replaced by the SOH non-printable ASCII character
asked Nov 29, 2012 at 13:05
\$\endgroup\$
2
  • \$\begingroup\$ Slightly sideways answer: if you're going to have a lot of strings in your program, it may be worth building a ROM to put them in. \$\endgroup\$ Commented Nov 29, 2012 at 13:36
  • \$\begingroup\$ @pjc50: Thanks. I don't have a large amount of fixed strings to store. Most of what I'm trying to do is basic string manipulation, extracting various fields, and concatenating snippets for which the values vary over time. \$\endgroup\$ Commented Nov 29, 2012 at 13:45

1 Answer 1

3
\$\begingroup\$

The section you quoted refers to identifiers, so you can have a named item with non-word characters in:

reg [6 * 8 - 1:0] \test"string = {"Hello",8'h01};

Yes, that's a legal identifier; don't do that unless you have to, though. That snippet also shows how a Verilogy way of embedding non-ascii in strings with concatenation.

A little experimentation shows that \xnn doesn't work but \nnn (octal) does. This may be tool-dependant.

answered Nov 29, 2012 at 13:50
\$\endgroup\$
2
  • \$\begingroup\$ Yes, such escaped identifiers are not good. Using concatenation is definitely one way of doing the initialisation. A little clunkier than C though. \$\endgroup\$ Commented Nov 29, 2012 at 13:54
  • \$\begingroup\$ But seems more portable. I've used escaped values, for example "Text000円000円000円000円" to pad a 8 character string with NULL characters to the right. It worked fine in Xilinx ISE, but it didn't in Vivado. { "Text", 8'h00, 8'h00, 8'h00, 8'h00 } works in both \$\endgroup\$ Commented Aug 3, 2020 at 11:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.