2

I want to make a list of strings in a Testbench to load different files for example.

I tried:

type tastring ARRAY(iADCCount_C-1 downto 0) of string;
constant Filenames     : tastring := ("file.txt",
                                      "anotherfile.txt",
                                      "jetanotherfile.txt");

Its not possible to have variable length strings in an array.

Also:

type tpstring is access string;
type tpastring is ARRAY(iADCCount_C-1 downto 0) of tpstring;
constant Filenames     : tpastring := (new string'("file.txt"),
                                       new string'(anotherfile.txt"),
                                       new string'(jetanotherfile.txt"));

Does not work! You cannot make an access type constant. Do I miss something? Is there a way to make a list of Strings without padding them to the same size?

1

2 Answers 2

2

You are almost correct :)

The second code snippet must use a variable, because access types can only be used for objects of kind variable.

type line_vector is array(iADCCount_C-1 downto 0) of line;
variable Filenames : line_vector := (
  new string'("file.txt"),
  new string'("anotherfile.txt"),
  new string'("jetanotherfile.txt")
);

Note 1: added missing " characters.
Note 2: type line is already defined in VHDL.
Note 3: type line_vector will be defined by VHDL-2017.

As an alternative, you can fill all string with character NUL. You might want to implement two functions for sizing the string to the constant's size and to trim the string (remove trailing NUL characters.

Sign up to request clarification or add additional context in comments.

2 Comments

Both the OP's second snippet and your answer are invalid VHDL, missing double quotes delimiting the beginning of the string literals in the initial value aggregate's second and third association element expressions. From IEEE Std 1076 (the LRM) revision -2000 onward a shared variable is required to be a protected type. A shared variable would allow access through protected type methods to Filenames by multiple process statements in the OP's testbench.
@user1155120 Fixed the missing quotes. It was a copy of the OP's code snippet. He has not expressed to use his code in an architecture. So if it's used in a process, it would be valid - but unusual, right?
1

See this answer. AFAIK it is impossible to have arrays of variable-length strings.

If you implement custom trim functions as suggested in the answer, try using a fixed spacing character that cannot exist in file names(like ? for windows), as it will also ensure no problems with NUL-characters or other non-prinatbles that can cause hiccoughs with different synthesis tools.

1 Comment

Yes, but his second example tries to use pointers to strings of different lengths. He just needs to use a variable instead of a constant.

Your Answer

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.