convert a String to stdt_logic_vector

K

Klaus Sandner

Guest
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;
 
On Aug 10, 4:38 pm, Klaus Sandner <klaus.sand...@t-online.de> wrote:
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;
 
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;
 
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)
 
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








Tricky schrieb:
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;
 
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








Tricky schrieb:
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;
 
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








Tricky schrieb:
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;
 
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);
 

Welcome to EDABoard.com

Sponsor

Back
Top