convert a String to stdt_logic_vector

K

Klaus Sandner

I want to send a textstring to a display.To do this I have to transform
each charakters of the string to 8 bit values.

here my Vhdl solution

constant LCD_display_len:positive:=16;
subtype LCD_display_string is string(1 to LCD_display_len);
subtype LCD_display_bit is std_logic_vector(1 to LCD_display_len*8);


process (clk)
variable data_string :LCD_display_string := "test string for ";
variable data_bit :LCD_display_bit;

now I have problems to write the right instruction

data_bit <= conv ????????????????????????????

has somebody a solution?


klaus


end process;
 
T

Tricky

I want to send a textstring to a display.To do this I have to transform
each charakters of the string to 8 bit values.

here my Vhdl solution

constant LCD_display_len:positive:=16;
subtype LCD_display_string is string(1 to LCD_display_len);
subtype LCD_display_bit is std_logic_vector(1 to LCD_display_len*8);

process (clk)
variable data_string :LCD_display_string := "test string for ";
variable data_bit :LCD_display_bit;

now I have problems to write the right instruction

data_bit <= conv ????????????????????????????

has somebody a solution?

klaus

end process;

you need a function that goes something like this.

--converts a std_logic_vector into a string.
function slv_to_string(slv : std_logic_vector) return string is
variable retString : string(1 to slv'length);
begin
for i in 0 to slv'length-1 loop
if slv(i) = '1' then

--ensure we dont write the string back to front!
if slv'ASCENDING then
retString(i+1) := '1'; --always +1 as the string range is 1 to
slv'length
else
retString(retString'high-i) := '1';
end if;
else
if slv'ASCENDING then
retString(i+1) := '0'; --always +1 as the string range is 1 to
slv'length
else
retString(retString'high-i) := '0';
end if;
end if;
end loop;

return retString;
end function slv_to_string;
 
T

Tricky

Whoops I did that the wrong way round.

The IEEE.std_logic_textio package has the ability to read/write
std_logic_vectors from/to strings. These are procedures though. If you
need a function:

function string_to_slv(s : string) return std_logic_vector is
variable ret_slv : std_logic_vector(s'length-1 downto 0);
begin
for i in 1 to s'length loop
if s(i) = '0' then
ret_slv(i-1) := '0';
elsif s(i) = '1' then
ret_slv(i-1) := '1';
else
--catch bad characters
ret_slv(i-1) := 'X';
end if;

return ret_slv;
end loop;
end function;
 
T

Tricky

OMG. Im having a bad day:

try 0 to s'length-1 as the ret_slv demensions, otherwise your bus
will be returned back to front (and in your case not work at all)
 
K

Klaus Sandner

i think your proposal is not right for my problem

if s(i) = '0' then
> ret_slv(i-1) := '0';
> elsif s(i) = '1' then
> ret_slv(i-1) := '1';


I don't understand the conversion s(i) ='0'
s(i) ='1'
s is a string and the position if i is an aski byte
 
K

Klaus Sandner

i think your proposal is not right for my problem

if s(i) = '0' then
> ret_slv(i-1) := '0';
> elsif s(i) = '1' then
> ret_slv(i-1) := '1';


I don't understand the conversion s(i) ='0'
s(i) ='1'
s is a string and the position if i is an aski byte
 
K

Klaus Sandner

i think your proposal is not right for my problem

if s(i) = '0' then
> ret_slv(i-1) := '0';
> elsif s(i) = '1' then
> ret_slv(i-1) := '1';


I don't understand the conversion s(i) ='0'
s(i) ='1'
s is a string and the position if i is an aski byte
 
T

Tricky

Ahh, Now I think I get the problem.

You cannot synthesize type STRING. You will have to make it a constant
or generic, and then have a function something like this to generate
the SLV you want to use:

function string_to_slvString(s : string) : return std_logic_vector
variable ret_slv : std_logic_vector(1 to s'length*8);
begin

for i in 0 to s'length-1 loop
--unfortunatly you will have to do every single character conversion
manually:
case s(i) is
when 'a' => ret_slv( (i*8) + 1 to (i+1)*8 ) <= x"00";
when 'b' => ret_slv( (i*8) + 1 to (i+1)*8 ) <= x"01";
--
--
--
--etc
end case
end loop;

return ret_slv;

end function;


and then call it like this:

constant data_string :LCD_display_string := "test string for ";
constant LCD_display_bit is std_logic_vector(1 to
LCD_display_len*8) := string_to_slvString(data_string);
 
Joined
Aug 17, 2007
Messages
6
Reaction score
0
the following will convert a character to an 8 bit ascii vector:

function ch2vec (
Data : character)
return std_logic_vector is
variable ch : std_logic_vector(7 downto 0);
begin
ch := conv_std_logic_vector(character'pos(Data),8);
return ch;
end ch2vec;
the 'pos attribute will give the 'position' of a character in the ascii table
example usage:
data <= ch2vec(char); where char is say, a signal of type character eg. "A"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top