Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

VHDL resizing a string

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,302
Hello,

Can the VHDL "resize" function be used on stings?
If yes, please show an example.
 

Which operation would you expect to be implemented with string resize?

String is defined in standard.vhd as aray of char without any specific functions. Because char is an enumeration type, it can't be compatible with the bitvectors used in numeric_std resize().

You are free to define your own resize function for strings if you have a suitable application.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
The resize function only exists for signed/unsigned types, because it is obvious what to resize the array with. With other types, it becomes a little more configurable.

What are you trying to do? If you need dynamic string sizing use the line type (which is a pointer to a string).
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
I have an array of string.
each element ( each word ) is of different length - and this is illegal.
I wanted to make all array elements to be of the same length.
 

I have an array of string.
each element ( each word ) is of different length - and this is illegal.
Then you really don't have an array after all, so it's not clear what you really have as the starting point. It sounds like you have a bunch of strings but not collected into anything at present.
I wanted to make all array elements to be of the same length.
It's not clear what exactly you're trying to accomplish
- Are you trying to make the different size strings all be the same length so that you can put them in an array? (I think this is what you want).
- Or would you really like to keep the strings the size they are but put them in an array?

If the first case, then you simply need to define an array of strings
type arr_strings is array(1 to N) of String(1 to Max_String_Length);
The constant Max_String_Length could be derived from whatever it is that you have for your strings or you could simply compute it by hand and type in that number

If the second case, then you would need to define an array of pointers to strings. Then for each element in the array, you would have to allocate space for each string using 'new'. When you're done with the array, release the space.

Kevin Jennings
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Example of what I want:
Type x is array 0 to 3 of ( 1 to Max_Number );
When I use a short string (shorter then 100 chars) for an array element, I want the compiler to accept it as a valid option
 
Last edited:

You cant. It's against the VHDL rules, array elements must match lengths when you declare them. You cant force the compiler to "accept it"
You will need to write a function to pad the string out with something, to match the length (1 to max_number).

something like


Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function str_pad(s : string; pad_char : character; len : integer) return string is
  variable ret : string(1 to len)
begin
  assert len >= s'length report "Specified length must be longer than the specified string" severity failure;
 
  for i in 1 to s'high loop
    ret(i) := s(i);
  end loop;
 
  for i in s'high+1 to len loop
    ret(i) := pad_char;
  end loop;
 
  return ret;
end function str_pad;



Or just do as KJ said, and use an array of line types, and dynamically declare the strings (so you can have an array of different length strings).
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Thanks a lot for the example.
This is the functionally I was looking for when I asked about using the "resize" function on array elements...

Can you please post an example code for K-Js second suggestion?
 


Code VHDL - [expand]
1
2
3
4
5
6
type my_line_array_t is array(natural range <>) of line;
variable my_line_array : my_line_array_t(0 to 1);  --must be a variable, as line is an access type
....
 
my_line_array(0) := new string'("Hello everyone, Im some random string!");
my_line_array(1) := new string'("Go home Trickydicky, you're drunk");



- - - Updated - - -

I forgot the dealloaction - damn memory leaks (in more ways than 1 - 1 bottle of reisling down):


Code VHDL - [expand]
1
2
DEALLOCATE(my_line_array(0));
DEALLOCATE(my_line_array(1));

 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Thanks!

A few questions:

1. What is "line" ?
2.1. Why "my_line_array" must be a variable?
2.2. I thought that a variable can be declared only in a function/process/procedure. I see that it's not the case in your example. Please elaborate.
3. What is the purpose of the "DEALLOCATE" function?



The context in which I want to use my string of arrays is entity declaration.
In the area reserved for generics - For example, instead of writing:
Code:
generic
(
config_1: string := "ram" ;
config_2: string := "synchronous" ;
config_3: string := "non_ecc" ;
)

I want to declare the string array in a package:
Code:
type array_string is array ( natural range <> ) of string ;
and use it as follows:
Code:
generic
(
conigurations : array_string := ( "ram" , "synchronous" , "non_ecc" ) 
)
 
Last edited:

1. A line is declared in the textio package as:

type line is access string;

Access types are pointer types - just like in C:

int *ptr;

2. Thats the rules on access types - they must be a variable.

3. You are correct about where a variable must be. You are incorrect about my example - I didnt put any context around it.

Your example either needs the string pad function, or you have to specify the string to all the same length manually, by declaring them something like:

Code:
conigurations : array_string := ( "ram        " , "synchronous" , "non_ecc    " )

Or keep them as separate generics.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Thanks a lot for your help.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top