1

I'm working with VHDL, and I'm wondering if there is any way to constrain the string size when declaring it, using initialization. For example, we declare a string as follow:

variable sequence : string(1 to 20) := "AGTAACCAATTCGCATTCGC";

I would like to know if there is any way to do something like:

variable sequence : string := "AGTAACCAATTCGCATTCGC";

Of course, second line isn't valid, because interpreter says:

[VRFC 10-1547] variable cannot be unconstrained
1
  • Once VHDL 2019 gets some support, then the second way will be valid. Commented Oct 20, 2020 at 6:44

1 Answer 1

4

Constants don't have to be constrained, so you could do this:

constant Csequence : string := "AGTAACCAATTCGCATTCGC";
variable Vsequence : string(Csequence'range) := Csequence;

https://www.edaplayground.com/x/r3wK

entity E is
end entity E;

architecture A of E is
begin
  process
    constant Csequence : string := "AGTAACCAATTCGCATTCGC";
    variable Vsequence : string(Csequence'range) := Csequence;
  begin
  wait;
  end process;
  
end architecture A;
  
Sign up to request clarification or add additional context in comments.

3 Comments

That's a nice trick! Thank you. I hope VHDL interpreters updates to 2019. Until that, I'll use your trick.
There's also a function to return the length of the string literal. variable sequence: string (1 to strlen("AGTAACCAATTCGCATTCGC")) := "AGTAACCAATTCGCATTCGC";
You can use the function to produce a range stored in a subtype declaration: subtype gac is string (1 to strlen("AGTAACCAATTCGCATTCGC")); variable sequence: string (gac'range) := "AGTAACCAATTCGCATTCGC"; (This counts on the string literal being static).

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.