Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - String to std_logic_vector

 
Thread Tools Search this Thread
Old 05-30-2008, 05:16 PM   #1
Default String to std_logic_vector


I've been searching here but all the hits were for converting SLV to
string for testbenches.

I'm looking to convert a string to an array of 8bit SLVs. The idea is
I have a revision string of fixed length- something like:

ver <= "005-0805300908x2"'

and I have an array of registers declared as such:

SUBTYPE hwid_type is STD_LOGIC_VECTOR(7 DOWNTO 0);
TYPE reg_type IS ARRAY (0 TO NUM_REGS-1) OF hwid_type;
SIGNAL regs : reg_type;

What I'm looking for is a loop shown here in psuedo code:

for i in 1 to 16 loop
regs(i) <= to_slv(ver(mid$(i,1)))
end loop

Note this is for synthesis so I don't know if any of this is even
legal. I am trying to create a version register that the
microprocessor can read. An ascii string is prefered - let's pretend
for a moment it is a requirement. This is also a learning exercise
for me to deal with strings in VHDL.

Any ideas?

Shannon


Shannon
  Reply With Quote
Old 05-30-2008, 05:47 PM   #2
KJ
 
Posts: n/a
Default Re: String to std_logic_vector
On May 30, 12:16*pm, Shannon <sgo...@sbcglobal.net> wrote:
> I've been searching here but all the hits were for converting SLV to
> string for testbenches.
>
> I'm looking to convert a string to an array of 8bit SLVs. *The idea is
> I have a revision string of fixed length- something like:
>
> ver <= "005-0805300908x2"'
>
> and I have an array of registers declared as such:
>
> SUBTYPE hwid_type is STD_LOGIC_VECTOR(7 DOWNTO 0);
> TYPE reg_type IS ARRAY (0 TO NUM_REGS-1) OF hwid_type;
> SIGNAL *regs : reg_type;
>
> What I'm looking for is a loop shown here in psuedo code:
>
> for i in 1 to 16 loop
> * * regs(i) <= to_slv(ver(mid$(i,1)))
> end loop
>
> Note this is for synthesis so I don't know if any of this is even
> legal. *I am trying to create a version register that the
> microprocessor can read. *An ascii string is prefered - let's pretend
> for a moment it is a requirement. *This is also a learning exercise
> for me to deal with strings in VHDL.
>
> Any ideas?
>
> Shannon


Since your input string has some non-numeric characters in it that you
presumably want to keep in the string, then the best approach would be
to simply write a function that parses the string however you'd like.

Below is an example that simply adds the 'value' of each input
character to produce an integer output. You should be able to easily
modify this to select out whichever characters you want and perform
whatever calculations you'd like.

function Version_String_To_integer(Some_String: STRING) return integer
is
variable Return_Value: integer := 0;
begin
for i in Some_String'range loop
Some_Char := Some_String(i)
case Some_Char is
when "0" => Return_Value := Return_Value + 0;
when "1" => Return_Value := Return_Value + 1;
...
when others => null;
end case;
end loop;
return(Return_Value);
end function Version_String_To_integer;

KJ


KJ
  Reply With Quote
Old 05-30-2008, 06:03 PM   #3
Shannon
 
Posts: n/a
Default Re: String to std_logic_vector
On May 30, 9:47*am, KJ <kkjenni...@sbcglobal.net> wrote:
> On May 30, 12:16*pm, Shannon <sgo...@sbcglobal.net> wrote:
>
>
>
>
>
> > I've been searching here but all the hits were for converting SLV to
> > string for testbenches.

>
> > I'm looking to convert a string to an array of 8bit SLVs. *The idea is
> > I have a revision string of fixed length- something like:

>
> > ver <= "005-0805300908x2"'

>
> > and I have an array of registers declared as such:

>
> > SUBTYPE hwid_type is STD_LOGIC_VECTOR(7 DOWNTO 0);
> > TYPE reg_type IS ARRAY (0 TO NUM_REGS-1) OF hwid_type;
> > SIGNAL *regs : reg_type;

>
> > What I'm looking for is a loop shown here in psuedo code:

>
> > for i in 1 to 16 loop
> > * * regs(i) <= to_slv(ver(mid$(i,1)))
> > end loop

>
> > Note this is for synthesis so I don't know if any of this is even
> > legal. *I am trying to create a version register that the
> > microprocessor can read. *An ascii string is prefered - let's pretend
> > for a moment it is a requirement. *This is also a learning exercise
> > for me to deal with strings in VHDL.

>
> > Any ideas?

>
> > Shannon

>
> Since your input string has some non-numeric characters in it that you
> presumably want to keep in the string, then the best approach would be
> to simply write a function that parses the string however you'd like.
>
> Below is an example that simply adds the 'value' of each input
> character to produce an integer output. *You should be able to easily
> modify this to select out whichever characters you want and perform
> whatever calculations you'd like.
>
> function Version_String_To_integer(Some_String: STRING) return integer
> is
> variable Return_Value: integer := 0;
> begin
> for i in Some_String'range loop
> * Some_Char := Some_String(i)
> * case Some_Char is
> * * *when "0" => Return_Value := Return_Value + 0;
> * * *when "1" => Return_Value := Return_Value + 1;
> * * *...
> * * *when others => null;
> * end case;
> end loop;
> return(Return_Value);
> end function Version_String_To_integer;
>
> KJ- Hide quoted text -
>
> - Show quoted text -


As I suspected. So to flesh things out a bit....

function Version_Char_To_slv(Some_Char: character) return slv
is
variable Return_Value: slv(7 downto 0) := (others => 0);
begin
case Some_Char is
when "0" => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(0,;
when "1" => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(1,;
...
when "a" => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(97,;
...
when "-" => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(45,;
...
when others => null;
end case;
return(Return_Value);
end function Version_Char_To_slv;

and then back in my main process....

for i in 1 upto 16 loop
regs(i) <= Version_Char_To_slv(ver(i));
end loop;


Or something like that?


Shannon
  Reply With Quote
Old 05-30-2008, 06:24 PM   #4
Mike Treseler
 
Posts: n/a
Default Re: String to std_logic_vector
Shannon wrote:

> Or something like that?


-- or this:

type char2std_t is array(character) of std_ulogic;

constant char2std_c : char2std_t := (
'U' => 'U',
'X' => 'X',
'0' => '0',
'1' => '1',
'Z' => 'Z',
'W' => 'W',
'L' => 'L',
'H' => 'H',
'-' => '-',
others => 'X');

function str2std(arg : string) return std_logic_vector is
variable result : std_logic_vector(arg'length - 1 downto 0);
variable j : integer;
begin
j := arg'length -1;
for i in arg'range loop
result(j) := char2std_c(arg(i));
j := j -1;
end loop;
return result;
end function;



-- Mike Treseler


Mike Treseler
  Reply With Quote
Old 05-30-2008, 06:31 PM   #5
KJ
 
Posts: n/a
Default Re: String to std_logic_vector
On May 30, 1:03*pm, Shannon <sgo...@sbcglobal.net> wrote:
> On May 30, 9:47*am, KJ <kkjenni...@sbcglobal.net> wrote:


>
> Or something like that?
>


Except that each character only returns a nibble not a byte.

KJ


KJ
  Reply With Quote
Old 05-30-2008, 07:09 PM   #6
Shannon
 
Posts: n/a
Default Re: String to std_logic_vector
On May 30, 10:31*am, KJ <kkjenni...@sbcglobal.net> wrote:
> On May 30, 1:03*pm, Shannon <sgo...@sbcglobal.net> wrote:
>
> > On May 30, 9:47*am, KJ <kkjenni...@sbcglobal.net> wrote:

>
> > Or something like that?

>
> Except that each character only returns a nibble not a byte.
>
> KJ


My finished version:

FUNCTION char_to_slv (c : CHARACTER) RETURN STD_LOGIC_VECTOR IS
variable Return_Value : STD_LOGIC_VECTOR(7 downto 0);
BEGIN
case c is
when '0' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(0,);
when '1' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(1,);
when '2' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(2,);
when '3' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(3,);
when '4' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(4,);
when '5' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(5,);
when '6' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(6,);
when '7' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(7,);
when '8' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(8,);
when '9' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(9,);
when '-' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(45,);
when others => Return_Value := (OTHERS => '1');
end case;

Later I will add the lowercase letters but this gets me through the
day. Thanks for you help everyone.

Mike -- Your answers are always amazing to me. You find super compact
ways of doing clever things. I confess I don't understand your
version this time but as usual, I will synthesize it and look at the
RTL viewer to see how yours does the same thing as mine in a tenth the
number of lines!

Shannon
return(Return_Value);

END char_to_slv;


Shannon
  Reply With Quote
Old 05-30-2008, 07:40 PM   #7
Mike Treseler
 
Posts: n/a
Default Re: String to std_logic_vector
Shannon wrote:

> Mike -- Your answers are always amazing to me. You find super compact
> ways of doing clever things. I confess I don't understand your
> version this time but as usual, I will synthesize it and look at the
> RTL viewer to see how yours does the same thing as mine in a tenth the
> number of lines!


Thanks, but it is just a related example
of how to use a constant array for conversions.
It only covers the std_ulogic bits as is.
Your function looks fine for what you are doing.

-- Mike Treseler


Mike Treseler
  Reply With Quote
Old 05-30-2008, 08:59 PM   #8
Shannon
 
Posts: n/a
Default Re: String to std_logic_vector
Little mistake there. For clarity those should have been ascii
values:

FUNCTION char_to_slv (c : CHARACTER) RETURN STD_LOGIC_VECTOR IS
variable Return_Value : STD_LOGIC_VECTOR(7 downto 0);
BEGIN
case c is
when '0' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(48,);
when '1' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(49,);
when '2' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(50,);
when '3' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(51,);
when '4' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(52,);
when '5' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(53,);
when '6' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(54,);
when '7' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(55,);
when '8' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(56,);
when '9' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(57,);
when '-' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(45,);
when others => Return_Value := (OTHERS => '1');
end case;
return(Return_Value);
END char_to_slv;

And it's working in hardware! yeah! Now...to figure out how to write
a TCL script or something to push my revision number into a constant
in my package. The fun never ends!

Shannon


Shannon
  Reply With Quote
Old 05-30-2008, 09:55 PM   #9
KJ
 
Posts: n/a
Default Re: String to std_logic_vector

"Shannon" <> wrote in message
news:1e2083c0-63f4-4cf8-b41d-...
> Little mistake there. For clarity those should have been ascii
> values:
>
> FUNCTION char_to_slv (c : CHARACTER) RETURN STD_LOGIC_VECTOR IS
> variable Return_Value : STD_LOGIC_VECTOR(7 downto 0);
> BEGIN
> case c is
> when '0' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(48,);
> when '1' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(49,);
> when '2' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(50,);
> when '3' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(51,);
> when '4' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(52,);
> when '5' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(53,);
> when '6' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(54,);
> when '7' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(55,);
> when '8' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(56,);
> when '9' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(57,);
> when '-' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(45,);
> when others => Return_Value := (OTHERS => '1');
> end case;
> return(Return_Value);
> END char_to_slv;
>
> And it's working in hardware! yeah! Now...to figure out how to write
> a TCL script or something to push my revision number into a constant
> in my package. The fun never ends!
>


Well if all you wanted was the ASCII value of the charcters in the string
(as it appears from your code that you do) then it's even simpler

for i in 1 to 16 loop
Some_Slv <= std_logic_vector(to_unsigned(character'pos(Some_St ring(i)),
);
end loop;

KJ




KJ
  Reply With Quote
Old 05-30-2008, 10:06 PM   #10
KJ
 
Posts: n/a
Default Re: String to std_logic_vector

"Shannon" <> wrote in message
news:1e2083c0-63f4-4cf8-b41d-...
> Little mistake there. For clarity those should have been ascii
> values:
>
> FUNCTION char_to_slv (c : CHARACTER) RETURN STD_LOGIC_VECTOR IS
> variable Return_Value : STD_LOGIC_VECTOR(7 downto 0);
> BEGIN
> case c is
> when '0' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(48,);
> when '1' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(49,);
> when '2' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(50,);
> when '3' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(51,);
> when '4' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(52,);
> when '5' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(53,);
> when '6' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(54,);
> when '7' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(55,);
> when '8' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(56,);
> when '9' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(57,);
> when '-' => Return_Value := STD_LOGIC_VECTOR(TO_UNSIGNED(45,);
> when others => Return_Value := (OTHERS => '1');
> end case;
> return(Return_Value);
> END char_to_slv;
>
> And it's working in hardware! yeah! Now...to figure out how to write
> a TCL script or something to push my revision number into a constant
> in my package. The fun never ends!
>
> Shannon


Well if all you wanted was the ASCII value of the charcters in the string
(as it appears from your code that you do) then it's even simpler

for i in 1 to 16 loop
Some_Slv_String(i) <=
std_logic_vector(to_unsigned(character'pos(Some_St ring(i)),
);
end loop;

KJ




KJ
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
urgent help....need urgent help on say string task.. pooja Software 0 03-03-2009 06:16 AM
Java String Problems rbnbenjamin General Help Related Topics 0 02-03-2009 11:02 PM
ASP.NET: Asign Users in Roles(Array.IndexOf(Of String) method) msandlana Software 0 04-25-2008 06:37 AM
Hidden linebreaks in string? VB.NET Jiggy Software 0 04-23-2008 02:18 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46